From: Olivier Matz Date: Fri, 6 May 2011 10:11:51 +0000 (+0200) Subject: cmdline-examples: remove basic_char_loop() and use cmdline_interact() X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=0c0e81f94cfed584ea3b4baeb5735f4dd0366187;p=libcmdline.git cmdline-examples: remove basic_char_loop() and use cmdline_interact() Signed-off-by: Olivier Matz --- diff --git a/src/calculator_server/main.c b/src/calculator_server/main.c index 77fbe28..0406a06 100644 --- a/src/calculator_server/main.c +++ b/src/calculator_server/main.c @@ -48,30 +48,15 @@ extern cmdline_parse_ctx_t main_ctx[]; struct cmdline *cl; -/*** main */ - -void basic_char_loop(void) -{ - char c; - - c = -1; - while (1) { - read(cl->s_in, &c, 1); - if (cmdline_in(cl, &c, 1) < 0) - break; - } - cmdline_free(cl); - printf("\n"); -} - -/* #define STANDALONE */ - -int main(void) +int main(void) { int s; s = cmdline_tcpv4_listen(INADDR_ANY, 1234); cl = cmdline_accept(main_ctx, "example> ", s); - basic_char_loop(); + if (cl == NULL) + return 1; + cmdline_interact(cl); + cmdline_free(cl); return 0; } diff --git a/src/calculator_standalone/main.c b/src/calculator_standalone/main.c index de5a752..aa56e40 100644 --- a/src/calculator_standalone/main.c +++ b/src/calculator_standalone/main.c @@ -54,27 +54,10 @@ extern cmdline_parse_ctx_t main_ctx[]; struct cmdline *cl; -/*** main */ - -void basic_char_loop(void) -{ - char c; - - c = -1; - while (1) { - read(cl->s_in, &c, 1); - if (cmdline_in(cl, &c, 1) < 0) - break; - } - cmdline_free(cl); - printf("\n"); -} - -/* #define STANDALONE */ - -int main(void) +int main(void) { struct termios oldterm, term; + int err = 0; tcgetattr(0, &oldterm); memcpy(&term, &oldterm, sizeof(term)); @@ -82,9 +65,17 @@ int main(void) tcsetattr(0, TCSANOW, &term); setbuf(stdin, NULL); cl = cmdline_stdin_new(main_ctx, "calc> "); - basic_char_loop(); + if (cl == NULL) { + err = 1; + goto fail; + } + + cmdline_interact(cl); + cmdline_free(cl); + + fail: tcsetattr(0, TCSANOW, &oldterm); - return 0; + return err; } diff --git a/src/extension_example/main.c b/src/extension_example/main.c index ed8c871..7d1a031 100644 --- a/src/extension_example/main.c +++ b/src/extension_example/main.c @@ -59,24 +59,10 @@ struct cmdline *cl; /*** main */ -void basic_char_loop(void) -{ - char c; - - c = -1; - while (1) { - read(cl->s_in, &c, 1); - if (cmdline_in(cl, &c, 1) < 0) { - break; - } - } - cmdline_free(cl); - printf("\n"); -} - -int main(void) +int main(void) { struct termios oldterm, term; + int err = 0; tcgetattr(0, &oldterm); memcpy(&term, &oldterm, sizeof(term)); @@ -85,10 +71,16 @@ int main(void) setbuf(stdin, NULL); cl = cmdline_stdin_new(main_ctx, "example> "); - basic_char_loop(); + if (cl == NULL) { + err = 1; + goto fail; + } + cmdline_interact(cl); + cmdline_free(cl); + fail: tcsetattr(fileno(stdin), TCSANOW, &oldterm); - return 0; + return err; } diff --git a/src/genconf/main.c b/src/genconf/main.c index 3748710..95d0b78 100644 --- a/src/genconf/main.c +++ b/src/genconf/main.c @@ -59,22 +59,6 @@ struct cmdline *cl; /*** main */ -void basic_char_loop(void) -{ - char c; - - c = -1; - /* XXX */ - while (1) { - read(cl->s_in, &c, 1); - if (cmdline_in(cl, &c, 1) < 0) { - break; - } - } - cmdline_free(cl); - printf("\n"); -} - int main(void) { struct termios oldterm, term; @@ -92,7 +76,8 @@ int main(void) setbuf(stdin, NULL); cl = cmdline_stdin_new(main_ctx, "root> "); - basic_char_loop(); + cmdline_interact(cl); + cmdline_free(cl); tcsetattr(fileno(stdin), TCSANOW, &oldterm); return 0;