From: Sebastian Harl Date: Sun, 12 Jan 2014 21:43:02 +0000 (+0100) Subject: sysdb: Store a global input handler object in the input module. X-Git-Tag: sysdb-0.1.0~226^2 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3dfbb8002ceb61be3cb61a7764c98fef4b6c011a;p=sysdb.git sysdb: Store a global input handler object in the input module. … rather than in the scanner. All input related code should be in the input module. --- diff --git a/src/tools/sysdb/input.c b/src/tools/sysdb/input.c index 939a514..e08ad5b 100644 --- a/src/tools/sysdb/input.c +++ b/src/tools/sysdb/input.c @@ -56,19 +56,25 @@ # 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); @@ -78,26 +84,34 @@ input_readline(sdb_input_t *input) 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; @@ -106,17 +120,19 @@ sdb_input_readline(sdb_input_t *input, char *buf, } 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; diff --git a/src/tools/sysdb/input.h b/src/tools/sysdb/input.h index 00b915b..ef60bf4 100644 --- a/src/tools/sysdb/input.h +++ b/src/tools/sysdb/input.h @@ -41,6 +41,19 @@ typedef struct { #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 @@ -53,8 +66,7 @@ typedef struct { * - 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: @@ -62,14 +74,7 @@ sdb_input_readline(sdb_input_t *input, char *buf, * 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 06f4de2..9b575cc 100644 --- a/src/tools/sysdb/main.c +++ b/src/tools/sysdb/main.c @@ -241,7 +241,7 @@ main(int argc, char **argv) } input.input = sdb_strbuf_create(2048); - sdb_input_set(&input); + sdb_input_init(&input); yylex(); if (hist_file[0] != '\0') { diff --git a/src/tools/sysdb/scanner.l b/src/tools/sysdb/scanner.l index 518d299..ba5d3b1 100644 --- a/src/tools/sysdb/scanner.l +++ b/src/tools/sysdb/scanner.l @@ -40,15 +40,13 @@ # 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 @@ -89,17 +87,11 @@ identifier ([A-Za-z_][A-Za-z_0-9$]*) * 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 : */