summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e8f6d4b)
raw | patch | inline | side by side (parent: e8f6d4b)
author | Sebastian Harl <sh@tokkee.org> | |
Sun, 12 Jan 2014 21:43:02 +0000 (22:43 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Sun, 12 Jan 2014 21:43:02 +0000 (22:43 +0100) |
… rather than in the scanner. All input related code should be in the input
module.
module.
src/tools/sysdb/input.c | patch | blob | history | |
src/tools/sysdb/input.h | patch | blob | history | |
src/tools/sysdb/main.c | patch | blob | history | |
src/tools/sysdb/scanner.l | patch | blob | history |
index 939a5146a9ef2369c39de5d7c7f217f62a2dcc96..e08ad5b40da52d63ce9b16ebda9e0916193b6db0 100644 (file)
--- a/src/tools/sysdb/input.c
+++ b/src/tools/sysdb/input.c
# endif
#endif /* READLINEs */
+/*
+ * public variables
+ */
+
+sdb_input_t *sysdb_input = NULL;
+
/*
* private helper functions
*/
static size_t
-input_readline(sdb_input_t *input)
+input_readline(void)
{
const char *prompt = "sysdb=> ";
char *line;
size_t len;
- if (input->query_len)
+ if (sysdb_input->query_len)
prompt = "sysdb-> ";
line = readline(prompt);
len = strlen(line) + 1;
- sdb_strbuf_append(input->input, line);
- sdb_strbuf_append(input->input, "\n");
+ sdb_strbuf_append(sysdb_input->input, line);
+ sdb_strbuf_append(sysdb_input->input, "\n");
free(line);
return len;
} /* input_readline */
/*
- * API
+ * public API
*/
+int
+sdb_input_init(sdb_input_t *input)
+{
+ /* register input handler */
+ sysdb_input = input;
+ return 0;
+} /* sdb_input_init */
+
ssize_t
-sdb_input_readline(sdb_input_t *input, char *buf,
- int *n_chars, size_t max_chars)
+sdb_input_readline(char *buf, int *n_chars, size_t max_chars)
{
+ const char *data;
size_t len;
- len = sdb_strbuf_len(input->input) - input->tokenizer_pos;
+ len = sdb_strbuf_len(sysdb_input->input) - sysdb_input->tokenizer_pos;
if (! len) {
- size_t n = input_readline(input);
+ size_t n = input_readline();
if (! n) {
*n_chars = 0; /* YY_NULL */
return 0;
}
len = (len < max_chars) ? len : max_chars;
- strncpy(buf, sdb_strbuf_string(input->input) + input->tokenizer_pos, len);
- input->tokenizer_pos += len;
- *n_chars = (int)len;
+ data = sdb_strbuf_string(sysdb_input->input);
+ data += sysdb_input->tokenizer_pos;
+ strncpy(buf, data, len);
+ sysdb_input->tokenizer_pos += len;
+ *n_chars = (int)len;
return (ssize_t)len;
} /* sdb_input_readline */
int
-sdb_input_exec_query(sdb_input_t *input)
+sdb_input_exec_query()
{
- char *query = sdb_command_exec(input);
+ char *query = sdb_command_exec(sysdb_input);
HIST_ENTRY *current_hist;
const char *hist_line = NULL;
index 00b915beabea13735804bc3c1ebc480630d1aa84..ef60bf4d22fa47c0f800c60d4c7da9e192daf02a 100644 (file)
--- a/src/tools/sysdb/input.h
+++ b/src/tools/sysdb/input.h
#define SDB_INPUT_INIT { NULL, NULL, 0, 0 }
+/*
+ * sysdb_input:
+ * Input object to be used by the 'sysdb' command line tool.
+ */
+extern sdb_input_t *sysdb_input;
+
+/*
+ * sdb_input_init:
+ * Initialize the input handler.
+ */
+int
+sdb_input_init(sdb_input_t *input);
+
/*
* sdb_input_readline:
* This function is supposed to be used with a flex scanner's YY_INPUT. It
* - A negative value in case of an error.
*/
ssize_t
-sdb_input_readline(sdb_input_t *input, char *buf,
- int *n_chars, size_t max_chars);
+sdb_input_readline(char *buf, int *n_chars, size_t max_chars);
/*
* sdb_input_exec_query:
* server's reply and prints errors or returned data to standard output.
*/
int
-sdb_input_exec_query(sdb_input_t *input);
-
-/*
- * scanner
- */
-
-void
-sdb_input_set(sdb_input_t *new_input);
+sdb_input_exec_query(void);
#endif /* SYSDB_INPUT_H */
diff --git a/src/tools/sysdb/main.c b/src/tools/sysdb/main.c
index 06f4de2f0fb858152a74849300173627281de1cb..9b575cc825746c47548b87968090c65c0d77b6b2 100644 (file)
--- a/src/tools/sysdb/main.c
+++ b/src/tools/sysdb/main.c
}
input.input = sdb_strbuf_create(2048);
- sdb_input_set(&input);
+ sdb_input_init(&input);
yylex();
if (hist_file[0] != '\0') {
index 518d299bea4707eabd1843b82f375e0c58f6f6c3..ba5d3b1b57313ca5e1e88cacd1e059e6f127a4f7 100644 (file)
# undef YY_INPUT
#endif
#define YY_INPUT(buf, result, max_size) \
- sdb_input_readline(sdb_input, (buf), &(result), (max_size))
+ sdb_input_readline((buf), &(result), (max_size))
#define APPEND() \
do { \
- sdb_input->query_len += strlen(yytext); \
+ sysdb_input->query_len += strlen(yytext); \
} while (0)
-static sdb_input_t *sdb_input;
-
%}
%option interactive
* The following rules are specific to the command line tool.
*/
-";" { APPEND(); sdb_input_exec_query(sdb_input); }
+";" { APPEND(); sdb_input_exec_query(); }
. { APPEND(); }
%%
-void
-sdb_input_set(sdb_input_t *new_input)
-{
- sdb_input = new_input;
-} /* sdb_input_set */
-
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */