Ответ: Ответ: LIBBB: parse_config()

Tito farmatito at tiscali.it
Tue Jul 8 22:41:27 UTC 2008


On Tuesday 08 July 2008 23:40:14 Vladimir Dronnikov wrote:
> Cool!
> 
> But we need to not just process continuations and comments but also to
> parse the line into some (possibly variable) amount of
> whitespaces-delimited tokens.
> 
> And, as I mentioned earlier, we need to use the collected tokens in
> unobtrusive way in the applets.
> 
> Can you go on and code these features?
> 
> --
> Vladimir

I see....
The only (COMPLICATED) way I'm able to see now is to pass a struct to parse_config
(rather than return a linked list)

typedef struct option_t {
	char *option_name;
	char  **option_tokens;
	struct option_t *next_option;
} option_t;

so that we can strstr for "option_name:" or "option_name="
and add the token (if single or quoted) or the tokens to option_tokens 
(and set the last to NULL to make parsing in the caller easier).
A corner case are options without args (without = or :)
maybe in this case we can set option_tokens[0] to some default 
value to signal it was found............(maybe option_name itself??)

Maybe there is some simpler solution...will think about it tomorrow.
now it's time for a beer and a bed ;-)


Ciao,
Tito 
> 
> 2008/7/8, Tito <farmatito at tiscali.it>:
> > On Monday 07 July 2008 22:29:38 Vladimir Dronnikov wrote:
> >> Sure :)
> >> But some help is needed. I am trying to convert inotifyd. mdev is
> >> "saint animal". Natanael mentioned ifup/down. There is crond that uses
> >> plain config. There gotta be savings en gros if we cope this together.
> >>
> >> --
> >> Vladimir
> >>
> >>
> >> 2008/7/7, Bernhard Fischer <rep.dot.nop at gmail.com>:
> >> > On Mon, Jul 07, 2008 at 12:31:29PM -0700, dronnikov at gmail.com wrote:
> >> >>A helper function for parsing vanilla config files is added.
> >> >>I see mdev, crond and others can use it to uniformly process their
> >> >> configs.
> >> >
> >> > It would be better if you would convert a few users to this new
> >> > parse_config() and show the size-savings. The more you save, the more we
> >> > all will like it :)
> >> >
> >> > Please don't forget to check that you don't accidentally introduce bugs
> >> > in the course.
> >> >
> >> > cheers,
> >> >
> >
> > Hi,
> > just for fun I've done my own version of parse_config(),
> > it returns a linked list of all the options found.
> > The list is then evaluated and freed by the caller.
> > At the moment there is just an indipendent executable
> > to compile in test.c and a config file i used for testing.
> > Usage is: ./test PATH_TO_CONFIG_FILE
> > and all the options should be printed to the screen:
> >
> >  ./test config.cfg
> > option=1
> > option=2
> > option=corner-case1
> > option=corner-case3
> > option=corner-case2
> > option=after_new_lines
> > option 3
> > option 3        3       3       3
> > option abracadabra
> > option=multiline-option
> >
> > Just my 0,2 cents....in the hope it could be somehow useful.... :-)
> >
> > Ciao,
> > Tito
> >
> > /* Lines starting with "#" are ignored. Note that end-of-line
> > *  comments are supported.
> > *  Blank lines are ignored.
> > *  Lines may be indented freely.
> > *  A "\" character at the very end of the line indicates the next line
> > *  should be treated as a continuation of the current one.
> > */
> >
> > llist_t *parse_config(const char *filename)
> > {
> > 	char *line;
> > 	char *next_line;
> > 	char *p;
> > 	char *t;
> > 	llist_t *option_list = NULL;
> > 	FILE *file = fopen(filename, "r");
> > 	
> > 	if (file) {
> > 		while ((line = xmalloc_fgetline(file))) {
> > 			while ((p = last_char_is(line, '\\'))){
> > 					/* Multi-line object */
> > 					*p = '\0'; /* Remove '\' */
> > 					next_line = xmalloc_fgetline(file);
> > 					line = xasprintf("%s%s", line, (next_line) ? next_line : "");
> > 					free(next_line);
> > 			}
> > 			p = skip_whitespace(line); /* Remove leading whitespace */
> > 			if (*p && *p != '#') { /* Not a comment or indented comment */
> > 				if ((t = strchr(p, '#'))) /* End of line comment - cut it */
> > 					*t = '\0';
> > 				/* What remains is an option so add it to the linked list */
> > 				llist_add_to_end(&option_list, xstrdup(p));
> > 				continue;
> > 			} /* else empty line */
> > 			free(line);
> > 		} /*EOF */	
> > 		fclose(file);
> > 	}  /* else cannot read  - return NULL */
> > 	return option_list;
> > }
> >
> >
> 





More information about the busybox mailing list