[pacman-dev] [PATCH 5/9] conf.c: pass _parse_directive as a callback
Andrew Gregory
andrew.gregory.8 at gmail.com
Mon Jul 29 09:19:54 EDT 2013
On 07/29/13 at 02:18pm, Allan McRae wrote:
> On 22/07/13 16:46, Andrew Gregory wrote:
> > This will allow passing arbitrary key/value handlers.
>
> I have now gone through this entire patchset and this is the only patch
> that confuses me. What do we gain by doing this? Are you envisioning
> having _parse_directive and _parse_hook or something? Will that be needed?
>
> Allan
>
Yes, _parse_hook is exactly what I was envisioning. This patch completely
decouples the ini parser from the handling of the key/value pairs, making it
easy to reuse for any file that uses our format simply by passing a different
key/value handler. Aside from hooks, I also hope to eventually make the parser
available to other pacman-related projects so that they can all easily use the
same config format.
apg
> > Signed-off-by: Andrew Gregory <andrew.gregory.8 at gmail.com>
> > ---
> > src/pacman/conf.c | 20 ++++++++++++--------
> > 1 file changed, 12 insertions(+), 8 deletions(-)
> >
> > diff --git a/src/pacman/conf.c b/src/pacman/conf.c
> > index ab8706c..704ce13 100644
> > --- a/src/pacman/conf.c
> > +++ b/src/pacman/conf.c
> > @@ -841,8 +841,9 @@ cleanup:
> > }
> >
> > static int _parse_directive(const char *file, int linenum, const char *name,
> > - char *key, char *value, struct section_t *section)
> > + char *key, char *value, void *data)
> > {
> > + struct section_t *section = data;
> > if(!key && !value) {
> > int ret = finish_section(section);
> > pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name);
> > @@ -872,6 +873,9 @@ static int _parse_directive(const char *file, int linenum, const char *name,
> > return 0;
> > }
> >
> > +typedef int (ini_parser_fn)(const char *file, int line, const char *section,
> > + char *key, char *value, void *data);
> > +
> > /** The "real" parseconfig. Each "Include" directive will recall this method so
> > * recursion and stack depth are limited to 10 levels. The publicly visible
> > * parseconfig calls this with a NULL section argument so we can recall from
> > @@ -881,7 +885,7 @@ static int _parse_directive(const char *file, int linenum, const char *name,
> > * @param depth the current recursion depth
> > * @return 0 on success, 1 on failure
> > */
> > -static int _parseconfig(const char *file, struct section_t *section,
> > +static int _parseconfig(const char *file, ini_parser_fn cb, void *data,
> > char **section_name, int depth)
> > {
> > FILE *fp = NULL;
> > @@ -936,7 +940,7 @@ static int _parseconfig(const char *file, struct section_t *section,
> > name = strdup(line + 1);
> > name[line_len - 2] = '\0';
> >
> > - ret = _parse_directive(file, linenum, name, NULL, NULL, section);
> > + ret = cb(file, linenum, name, NULL, NULL, data);
> > free(*section_name);
> > *section_name = name;
> >
> > @@ -994,7 +998,7 @@ static int _parseconfig(const char *file, struct section_t *section,
> > for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
> > pm_printf(ALPM_LOG_DEBUG, "config file %s, line %d: including %s\n",
> > file, linenum, globbuf.gl_pathv[gindex]);
> > - _parseconfig(globbuf.gl_pathv[gindex], section,
> > + _parseconfig(globbuf.gl_pathv[gindex], cb, data,
> > section_name, depth + 1);
> > }
> > break;
> > @@ -1002,13 +1006,13 @@ static int _parseconfig(const char *file, struct section_t *section,
> > globfree(&globbuf);
> > continue;
> > }
> > - if((ret = _parse_directive(file, linenum, *section_name, key, value, section)) != 0) {
> > + if((ret = cb(file, linenum, *section_name, key, value, data)) != 0) {
> > goto cleanup;
> > }
> > }
> >
> > if(depth == 0) {
> > - ret = _parse_directive(NULL, 0, NULL, NULL, NULL, section);
> > + ret = cb(NULL, 0, NULL, NULL, NULL, data);
> > }
> >
> > cleanup:
> > @@ -1041,7 +1045,7 @@ int parseconfig(const char *file)
> > /* call the real parseconfig function with a null section & db argument */
> > pm_printf(ALPM_LOG_DEBUG, "parseconfig: options pass\n");
> > section.parse_options = 1;
> > - if((ret = _parseconfig(file, §ion, §ion_name, 0))) {
> > + if((ret = _parseconfig(file, _parse_directive, §ion, §ion_name, 0))) {
> > return ret;
> > }
> > if((ret = setup_libalpm())) {
> > @@ -1050,7 +1054,7 @@ int parseconfig(const char *file)
> > /* second pass, repo section parsing */
> > pm_printf(ALPM_LOG_DEBUG, "parseconfig: repo pass\n");
> > section.parse_options = 0;
> > - return _parseconfig(file, §ion, §ion_name, 0);
> > + return _parseconfig(file, _parse_directive, §ion, §ion_name, 0);
> > }
> >
> > /* vim: set ts=2 sw=2 noet: */
More information about the pacman-dev
mailing list