/* Parse a command line, and add it to the config file database. * If the command line was invalid or the user requested help, a help string * will be printed and an error will be returned. */ g_error commandline_parse(int argc, char **argv) { int c; g_error e; while (1) { c = getopt(argc,argv,"hlnv:m:i:t:c:-:s:"); if (c==-1) break; switch (c) { case 'n': /* Ignore config data */ configfile_free(); break; case '-': /* config option */ { char *section, *key, *value; /* Treat --help as an exception */ if (!strcmp(optarg,"help")) return commandline_help(); if ((key = strchr(optarg,'.'))) { *key = 0; key++; section = optarg; } else { /* Default section */ key = optarg; section = "pgserver"; } if ((value = strchr(key,'='))) { *value = 0; value++; } else /* Default value */ value = "1"; e = set_param_str(section,key,value); errorcheck; } break; case 'v': /* Video driver */ e = set_param_str("pgserver","video",optarg); errorcheck; break; case 'm': /* Video mode */ e = set_param_str("pgserver","mode",optarg); errorcheck; break; case 's': /* Session manager */ e = set_param_str("pgserver","session",optarg); errorcheck; break; case 'c': /* Config file */ e = configfile_parse(optarg); errorcheck; break; case 'l': /* List */ e = commandline_list(); errorcheck; break; case 'i': /* Input */ e = append_param_str("pgserver","input"," ",optarg); errorcheck; break; case 't': /* Theme */ e = append_param_str("pgserver","themes"," ",optarg); errorcheck; break; default: /* Need help */ return commandline_help(); } } if (optind < argc) /* extra options */ return commandline_help(); return success; }
int configfile_loadfile(FILE *f) { char line[2048]; char *start, *key, *value; int i, l, linenum = 0; int numhostconf = 0; char *endptr; struct configfile_hostopts *chostconf = NULL; memset(&global_config, 0, sizeof(struct configfile_globalopts)); global_config.id = -1; host_config = NULL; // Keep reading lines while(fgets(line, 2048, f)) { linenum++; l = strlen(line); // Strip off trailing newline for(i = 0; i < l; i++) if(line[i] == '\n') line[i] = 0; l = strlen(line); // Eliminate starting whitespace for(i = 0; line[i] == ' ' || line[i] == '\t'; i++); start = line + i; // Skip comments and blank lines if(start[0] == '#') continue; if(!start[0]) continue; // Go through characters until a = is found key = start; for(value = key; *value != '=' && *value; value++); if(!*value) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } // Setting this to a NULL makes the key a string *value = 0; // The value starts here, ends at the end of the line value++; // Check if it's a "node" option, for specific host options if(strcmp(key, "node") == 0) { host_config = realloc(host_config, (numhostconf + 2) * sizeof(struct configfile_hostopts)); chostconf = &host_config[numhostconf]; numhostconf++; host_config[numhostconf].node = NULL; chostconf->node = malloc(strlen(value) + 1); strcpy(chostconf->node, value); } else { // If chostconf is NULL, check for global options if(chostconf == NULL) { if(strcmp(key, "name") == 0) { global_config.name = malloc(strlen(value) + 1); strcpy(global_config.name, value); } else if(strcmp(key, "devname") == 0) { global_config.devname = malloc(strlen(value) + 1); strcpy(global_config.devname, value); } else if(strcmp(key, "cert") == 0) { global_config.cert = malloc(strlen(value) + 1); strcpy(global_config.cert, value); } else if(strcmp(key, "privkey") == 0) { global_config.privkey = malloc(strlen(value) + 1); strcpy(global_config.privkey, value); } else if(strcmp(key, "cacert") == 0) { global_config.cacert = malloc(strlen(value) + 1); strcpy(global_config.cacert, value); } else if(strcmp(key, "restrictouname") == 0) { global_config.restrictouname = malloc(strlen(value) + 1); strcpy(global_config.restrictouname, value); } else if(strcmp(key, "nodeinfofile") == 0) { global_config.nodeinfofile = malloc(strlen(value) + 1); strcpy(global_config.nodeinfofile, value); } else if(strcmp(key, "bnlpubkey") == 0) { global_config.bnlpubkey = strdup(value); // Reminder: Make all the other string copy things strdup() } else if(strcmp(key, "bnlprivkey") == 0) { global_config.bnlprivkey = strdup(value); } else if(strcmp(key, "upcmd") == 0) { global_config.upcmd = strdup(value); } else if(strcmp(key, "subnetupcmd") == 0) { global_config.subnetupcmd = strdup(value); } else if(strcmp(key, "mysubnetupcmd") == 0) { global_config.mysubnetupcmd = strdup(value); } else if(strcmp(key, "logfile") == 0) { global_config.logfile = strdup(value); } else if(strcmp(key, "logmethod") == 0) { global_config.logmethod = strdup(value); } else if(strcmp(key, "id") == 0) { global_config.id = strtol(value, &endptr, 10); if(*endptr) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else if(strcmp(key, "loglevel") == 0) { global_config.loglevel = strtol(value, &endptr, 10); if(*endptr) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else if(strcmp(key, "port") == 0) { global_config.port = strtol(value, &endptr, 10); if(*endptr) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else if(strcmp(key, "cryptalgos") == 0) { global_config.cryptalgos = configfile_parsealgopref(value, 1); if(global_config.cryptalgos == NULL) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else if(strcmp(key, "compalgos") == 0) { global_config.compalgos = configfile_parsealgopref(value, 2); if(global_config.compalgos == NULL) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else if(strcmp(key, "disableipv6") == 0) { global_config.disableipv6 = configfile_parsebool(value); if(global_config.disableipv6 == 2) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else if(strcmp(key, "preferipv6") == 0) { global_config.preferipv6 = configfile_parsebool(value); if(global_config.preferipv6 == 2) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else { if(strcmp(key, "cryptalgos") == 0) { chostconf->cryptalgos = configfile_parsealgopref(value, 1); if(chostconf->cryptalgos == NULL) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else if(strcmp(key, "compalgos") == 0) { chostconf->compalgos = configfile_parsealgopref(value, 2); if(chostconf->compalgos == NULL) { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } else { logpreinitmsg("Malformed configuration file: Line %d", linenum); configfile_free(); return CONFIGFILE_MALFORMED; } } } } // Check for required entries if(!global_config.name) { logpreinitmsg("Configuration file missing \"name\" entry."); configfile_free(); return CONFIGFILE_MALFORMED; } /*if(global_config.id == -1) { logpreinitmsg("Configuration file missing \"id\" entry."); configfile_free(); return CONFIGFILE_MALFORMED; }*/ return CONFIGFILE_OK; }
int main (int argc, char *argv[]) { char *home, *conffile; pghandle msgscroll, msgbox; pghandle wToolbar, wScroll, wItem; row = 0; pgInit (argc, argv); picomailapp = pgRegisterApp (PG_APP_NORMAL, "PicoMail", 0); home = getenv ("HOME"); conffile = malloc (strlen (home) + strlen (CONFIG_FILE) + 1); if (home && conffile) { strcpy (conffile, home); strcat (conffile, CONFIG_FILE); if (!configfile_parse (conffile)) { pgMessageDialog ("PicoMail", "Configuration file could not be parsed.\n" "Please make sure you have a good '~/.picomail.conf'!", PG_MSGBTN_OK || PG_MSGICON_ERROR); exit (1); } free (conffile); } wToolbar = pgNewWidget (PG_WIDGET_TOOLBAR, 0, 0); wScroll = pgNewWidget (PG_WIDGET_SCROLL, 0, 0); wBox = pgNewWidget (PG_WIDGET_BOX, 0, 0); pgSetWidget (PGDEFAULT, PG_WP_SIDE, PG_S_ALL, 0); pgSetWidget (wScroll, PG_WP_BIND, wBox, 0); pgNewWidget (PG_WIDGET_BUTTON, PG_DERIVE_INSIDE, wToolbar); pgSetWidget (PGDEFAULT, PG_WP_TEXT, pgNewString ("Read Message"), PG_WP_SIDE, PG_S_LEFT, PG_WP_EXTDEVENTS, PG_EXEV_PNTR_DOWN, 0); pgBind (PGDEFAULT, PG_WE_PNTR_DOWN, &readMessage, NULL); pgNewWidget (PG_WIDGET_BUTTON, PG_DERIVE_INSIDE, wToolbar); pgSetWidget (PGDEFAULT, PG_WP_TEXT, pgNewString ("Delete Message"), PG_WP_SIDE, PG_S_LEFT, PG_WP_EXTDEVENTS, PG_EXEV_PNTR_DOWN, 0); pgBind (PGDEFAULT, PG_WE_PNTR_DOWN, &deleteMessage, NULL); pgNewWidget (PG_WIDGET_BUTTON, PG_DERIVE_INSIDE, wToolbar); pgSetWidget (PGDEFAULT, PG_WP_TEXT, pgNewString ("Get list of messages!"), PG_WP_SIDE, PG_S_LEFT, PG_WP_EXTDEVENTS, PG_EXEV_PNTR_DOWN, 0); pgBind (PGDEFAULT, PG_WE_PNTR_DOWN, &getList, NULL); pgBind (PGBIND_ANY, PG_WE_CLOSE, &closeboxHandler, NULL); /* message viewer */ mesgviewer = pgRegisterApp (PG_APP_NORMAL, "PicoMail Message", 0); pgBind (mesgviewer, PG_WE_CLOSE, &viewerCloseboxHandler, NULL); pgSetWidget (mesgviewer, PG_WP_SIZE, 0, 0); msgscroll = pgNewWidget (PG_WIDGET_SCROLL, 0, 0); msgbox = pgNewWidget (PG_WIDGET_BOX, 0, 0); pgSetWidget (msgbox, PG_WP_SIDE, PG_S_ALL, 0); pgSetWidget (msgscroll, PG_WP_BIND, msgbox, 0); messagebox = pgNewWidget (PG_WIDGET_TEXTBOX, PG_DERIVE_INSIDE, PGDEFAULT); // pgSetWidget(msgscroll, PG_WP_BIND,messagebox, 0); pgSetWidget (messagebox, PG_WP_TEXT, pgNewString ("Hier is uw bericht!"), 0); pgEventLoop (); configfile_free (); return 0; }