|
From: Juli M. <jma...@Fr...> - 2002-11-19 19:57:22
|
* De: Jose Nazario <jo...@mo...> [ Data: 2002-11-19 ] [ Subjecte: Re: [Unbound-core] proposed diff: vcli_loop() ] > new diff attached, with juli's suggestions this compiles cleanly and unit > tests correctly. > > a couple of followups: > > > I don't understand why we strlcpy? Also, strlcpy was used wrong, I > > think, so I added an XXX. > > trucation occurs to enforce a usable size of prompt. If a CLI app wants to do a long long long prompt, I don't see why we should enforce otherwise. It should enforce usable length, as long it knows how to truncate properly... Which we don't. We may truncate away useful information and at least will truncate away something like the '>' or whatever representing where input goes. The one I added an XXX for is bogus (i.e. misused) anyway. > comments in between sections in cli.3 to visually break it up. makes > editing easier. > > this code will need some commenting soon ... > > please comment on the diff and give further suggestions as needed. > > ___________________________ > jose nazario, ph.d. jo...@mo... > http://www.monkey.org/~jose/ > ? test/test2.c > Index: cli.c > =================================================================== > RCS file: /cvsroot/unbound/unbound/lib/cli/cli.c,v > retrieving revision 1.5 > diff -u -r1.5 cli.c > --- cli.c 19 Nov 2002 05:30:54 -0000 1.5 > +++ cli.c 19 Nov 2002 19:42:20 -0000 > @@ -68,7 +68,7 @@ > char *input, *forward[MAX_TOKENS]; > > if (streamout != NULL) > - fprintf(streamout, "%s", prompt); /* XXX */ > + fprintf(streamout, "%s", cli_prompt); /* XXX */ > > if ((input = fparseln(streamin, NULL, NULL, NULL, 0)) == NULL) { > break; > @@ -89,4 +89,54 @@ > fclose(streamout); > fclose(streamin); > return (0); > +} > + > +int > +vcli_loop(int fdin, int fdout, prompt_handler p_callback, cli_handler callback) { > + char *p; > + FILE *streamin; > + FILE *streamout = NULL; > + size_t len; > + > + if ((streamin = fdopen(fdin, "r")) == NULL) { > + warn("Failed to open specified file descriptor"); > + return(errno); > + } > + > + if (fdout > -1) > + if ((streamout = fdopen(fdout, "w+")) == NULL) { > + warn("Failed to open specific file descriptor"); > + return(errno); > + } > + > + for (;;) { > + int i = 0; > + char *input, *forward[MAX_TOKENS]; > + char cli_prompt[MAX_PROMPT_SIZE]; > + > + if (streamout != NULL) { > + strlcpy((char *)cli_prompt, (const char *)p_callback(), Don't need the first cast, and the prompt things should prolly return const, actually. > + MAX_PROMPT_SIZE); > + fprintf(streamout, "%s", cli_prompt); > + } > + > + if ((input = fparseln(streamin, NULL, NULL, NULL, 0)) == NULL) { > + break; > + } > + > + for (; i < MAX_TOKENS-1 && > + (forward[i] = strsep(&input, " \t")) != NULL;) > + if (*forward[i] != '\0') > + i++; > + > + if (i > 0) > + if (callback (i, forward) < 0) > + return (-1); > + free(input); > + } > + > + if (streamout != NULL) > + fclose(streamout); > + fclose(streamin); > + return(0); > } > Index: include/cli.h > =================================================================== > RCS file: /cvsroot/unbound/unbound/lib/cli/include/cli.h,v > retrieving revision 1.3 > diff -u -r1.3 cli.h > --- include/cli.h 19 Nov 2002 04:43:39 -0000 1.3 > +++ include/cli.h 19 Nov 2002 19:42:20 -0000 > @@ -30,6 +30,8 @@ > > typedef int (*cli_handler)(int, char *[]); > int cli_loop(int, int, const char *, cli_handler); > +typedef *(*prompt_handler)(); typedef const char *(*prompt_handler)(void) actually, or typedef char *(*prompt_handler)(void) if we don't need it to be const (though we probably want it to). > +int vcli_loop(int, int, prompt_handler, cli_handler); > > #define MAX_PROMPT_SIZE 255 > #define MAX_TOKENS 512 > Index: man/cli.3 > =================================================================== > RCS file: /cvsroot/unbound/unbound/lib/cli/man/cli.3,v > retrieving revision 1.4 > diff -u -r1.4 cli.3 > --- man/cli.3 19 Nov 2002 04:43:39 -0000 1.4 > +++ man/cli.3 19 Nov 2002 19:42:20 -0000 > @@ -13,6 +13,9 @@ > .Fd #include <cli.h> > .Ft int > .Fn cli_loop "int fdin" "int fdout" "const char *prompt" "cli_handler callback" > +.Ft int > +.Fn vcli_loop "int fdin" "int fdout" "prompt_handler p_callback" "cli_handler callback" > +.\" > .Sh DESCRIPTION > .Nm > provides a simple API to develop command line based utilities. > @@ -36,13 +39,28 @@ > as an error. Setting > .Fa fdout > to -1 causes the output from cli_loop to be silenced. This is designed for > -non interactive shells. > +non interactive shells. The maximum size of > +.Fa prompt > +is > +.Fa MAX_PROMPT_SIZE > +with truncation ocurring for longer prompts. > +.Pp > +.Fn vcli_loop > +is like > +.Fn cli_loop > +only the displayed prompt is evaluated by > +.Fa p_callback > +each time to determine its format. This is useful for systems where the > +prompt changes in response to some environmental factor, such as priviledge > +level or mode. > +.\" > .Sh BUGS > To link completely against > .Nm > you will also have to link against the > .Fa libutil > library. No other bugs known, aside from the fact that this is a dumb library. > +.\" > .Sh AUTHORS > Jose Nazario > .Aq jo...@mo... Thanks, juli. -- Juli Mallett <jma...@Fr...> OpenDarwin, Mono, FreeBSD Developer. ircd-hybrid Developer, EFnet addict. FreeBSD on MIPS-Anything on FreeBSD. |