/* This file is part of tagr. Copyright (C) 2000, 2005, Max Bouglacoff, Sergey Poznyakoff This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /***************************************************************************** * * readconfig.c -- trafcollector * *****************************************************************************/ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #define obstack_chunk_alloc malloc #define obstack_chunk_free free #include #include #include #define ARG_UNUSED __attribute__ ((__unused__)) struct obstack sd_stack; SD *sd; size_t sd_count; static size_t line; struct command_table { char *command; int nargs; void (*handler) (void *data, int argc, char **argv); void *data; }; static void port_handler(void *data ARG_UNUSED, int argc, char **argv) { port = strtoul (argv[1], NULL, 10); } static void string_handler(void *data, int argc, char **argv) { char **p = data; *p = strdup(argv[1]); } static void router_handler(void *data, int argc, char **argv) { char *p; SD *sd; long max = strtoul(argv[4], &p, 10); if (*p) { fprintf (stderr, "%s:%lu: bad maximum speed value\n", configfile, (unsigned long) line); return; } sd = calloc(1, sizeof(*sd)); if (!sd) { fprintf (stderr, "cannot allocate %lu bytes\n", (unsigned long) sizeof(*sd)); abort(); } strncpy(sd->id, argv[1], sizeof(sd->id)); strncpy(sd->name, argv[2], sizeof(sd->name)); strncpy(sd->dir, argv[3], sizeof(sd->dir)); sd->max = max; obstack_grow(&sd_stack, sd, sizeof *sd); sd_count++; } static struct command_table command_table[] = { { "basedir", 2, string_handler, &basedir }, { "user", 2, string_handler, &user }, { "template", 2, string_handler, &html_template }, { "port", 2, port_handler, NULL }, { "router", 5, router_handler, NULL }, { NULL, } }; struct command_table * find_command(const char *name) { struct command_table *p; for (p = command_table; p->command; p++) if (strcmp(p->command, name) == 0) return p; return NULL; } void handle_command(int argc, char **argv) { struct command_table *p = find_command(argv[0]); if (!p) fprintf (stderr, "%s:%lu: unknown statement\n", configfile, (unsigned long) line); else if (p->nargs > argc) fprintf (stderr, "%s:%lu: too few arguments to `%s'\n", configfile, (unsigned long) line, argv[0]); else if (p->nargs < argc) fprintf (stderr, "%s:%lu: too many arguments to `%s'\n", configfile, (unsigned long) line, argv[0]); else p->handler(p->data, argc, argv); } int readconfig () { FILE *fp; char *buf = NULL; size_t size = 0; int errcnt; fp = fopen ( configfile, "r" ); if (!fp) { fprintf ( stderr, "cannot open file %s: %s\n", configfile, strerror(errno) ); return -1; } line = 0; sd_count = 0; obstack_init(&sd_stack); while (getline(&buf, &size, fp) > 0) { int argc; char **argv; line++; if (argcv_get (buf, "", "#", &argc, &argv)) fprintf ( stderr, "%s:%lu: cannot parse line\n", configfile, (unsigned long) line); else if (argc > 0) handle_command(argc, argv); argcv_free (argc, argv); } fclose(fp); free(buf); errcnt = 0; if (!basedir) { fprintf(stderr, "%s: `basedir' not defined\n", configfile); errcnt++; } if (!html_template) { fprintf(stderr, "%s: `template' not defined\n", configfile); errcnt++; } if ( port <= 0 ) fprintf(stderr, "%s: `port' not defined. Using 5671\n", configfile); if ( sd_count == 0 ) { fprintf (stderr, "%s: no routers are declared. Nothing to do\n", configfile); errcnt++; } if (errcnt) return 1; sd = obstack_finish(&sd_stack); return 0; }