+
+/*========== option parsing ==========*/
+
+enum OptFlags {
+ of_seconds= 001000u;
+ of_boolean= 002000u;
+};
+
+typedef struct Option Option;
+typedef void OptionParser(const Option*, const char *val);
+
+struct Option {
+ int short;
+ const char *long;
+ void *store;
+ OptionParser *fn;
+ int noarg;
+};
+
+void op_integer(const Option *o, const char *val) {
+ char *ep;
+ errno= 0;
+ unsigned long ul= strtoul(val,&ep,10);
+ if (*ep || ep==val || errno || ul>INT_MAX)
+ badusage("bad integer value for %s",o->long);
+ int *store= o->store;
+ *store= ul;
+}
+
+void op_double(const Option *o, const char *val) {
+ int *store= o->store;
+ char *ep;
+ errno= 0;
+ *store= strtod(val, &ep);
+ if (*ep || ep==val || errno)
+ badusage("bad floating point value for %s",o->long);
+}
+
+void op_string(const Option *o, const char *val) {
+ char **store= o->store;
+ free(*store);
+ *store= val;
+}
+
+void op_seconds(const Option *o, const char *val) {
+ int *store= o->store;
+ char *ep;
+
+ double v= strtod(val,&ep);
+ if (ep==val) badusage("bad time/duration value for %s",o->long);
+
+ if (!*ep || !strcmp(ep,"s")) unit= 1;
+ else if (!strcmp(ep,"m")) unit= 60;
+ else if (!strcmp(ep,"h")) unit= 3600;
+ else if (!strcmp(ep,"d")) unit= 86400;
+ else badusage("bad units %s for time/duration value for %s",ep,o->long);
+
+ v *= unit;
+ v= ceil(v);
+ if (v > INT_MAX) badusage("time/duration value for %s out of range",o->long);
+ *store= v;
+}
+
+void op_periods_rndup(const Option *o, const char *val) {
+ int *store= o->store;
+ op_seconds(o,val);
+ *store += PERIOD_SECONDS-1;
+ *store /= PERIOD_SECONDS;
+}
+
+void op_periods_booltrue(const Option *o, const char *val) {
+ int *store= o->store;
+ *store= 1;
+}
+void op_periods_boolfalse(const Option *o, const char *val) {
+ int *store= o->store;
+ *store= 0;
+}
+
+static const Option options[]= {
+{ 0, "max-connections", &max_connections op_integer },
+{ 0, "streaming", &try_stream, op_booltrue, 1 },
+{ 0, "no-streaming", &try_stream, op_boolfalse, 1 },
+{'h',"host", &remote_host, op_string },
+{'P',"port", &port op_integer },
+{ 0, "inndconf", &inndconffile, op_string },
+{'f',"feedfile", &feedfile, op_string },
+{'q',"quiet-multiple", &quiet_if_locked, op_booltrue, 1 },
+{ 0, "no-quiet-multiple", &quiet_if_locked, op_boolfalse, 1 },
+{'d',"daemon", &become_daemon, op_booltrue, 1 },
+{ 0, "no-daemon", &become_daemon, op_boolfalse, 1 },
+
+{ 0, "no-check-proportion", &nocheck_thresh_pct, op_double },
+{ 0, "no-check-filter", &nocheck_decay_articles, op_double },
+
+{ 0, "max-queue-size", &max_queue_per_conn op_integer },
+{ 0, "reconnect-interval", &reconnect_delay_periods, op_periods_rndup },
+{ 0, "flush-retry-interval", &flushfail_retry_periods, op_periods_rndup },
+{ 0, "feedfile-open-timeout", &open_wait_periods, op_periods_rndup },
+{ 0, "connection-timeout", &connection_timeout, op_seconds },
+{ 0, "inndcomm-timeout", &inndcomm_flush_timeout, op_seconds },
+};
+
+int main(int argc, char **argv) {
+ const char *arg;
+
+ for (;;) {
+ arg= *++argv;
+ if (!arg) break;
+ if (*arg != '-') break;
+ if (!strcmp(arg,"--")) { arg= *++argv; break; }
+ int a;
+ while ((a= *++arg)) {
+ const Option *o;
+ if (a=='-') {
+ arg++;
+ char *equals= strchr(arg,'=');
+ int len= equals ? (equals - arg) : strlen(arg);
+ for (o=options; o->long; o++)
+ if (strlen(o->long) == len && !memcmp(o->long,arg,len))
+ goto found_long;
+ badusage("unknown long option --%s",arg);
+ found_long:
+ if (o->noarg) {
+ if (equals) badusage("option --%s does not take a value",o->long);
+ arg= 0;
+ } else if (equals) {
+ arg= equals+1;
+ } else {
+ arg= *++argv;
+ if (!arg) badusage("option --%s needs a value",o->long);
+ }
+ o->fn(o, arg);
+ break; /* eaten the whole argument now */
+ }
+ for (o=options; o->long; o++)
+ if (a == o->short)
+ goto found_short;
+ badusage("unknown short option -%c",a);
+ found_short:
+ if (o->noarg) {
+ o->fn(o,0);
+ } else {
+ if (!*++arg) {
+ arg= *++argv;
+ if (!arg) badusage("option -%c needs a value",o->short);
+ }
+ o->fn(o,arg);
+ break; /* eaten the whole argument now */
+ }
+ }
+ }
+
+ if (!arg) badusage("need site name argument");
+ if (*++argv) badusage("too many non-option arguments");
+ sitename= arg;
+
+ if (nocheck_thresh_pct < 0 || nocheck_thresh_pct > 100)
+ badusage("nocheck threshold percentage must be between 0..100");
+ nocheck_thresh= nocheck_thresh_pct * 0.01;
+
+ if (nocheck_decay_articles < 0.1)
+ badusage("nocheck decay articles must be at least 0.1");
+ nocheck_decay= 1 - 1/nocheck_decay_articles;
+
+ innconf_read(inndconffile);
+
+ if (!feedfile)
+ feedfile= xasprintf("%s/%s",pathoutgoing,sitename);
+ else if (!feedfile[0])
+ badusage("feed filename must be nonempty");
+ else if (feedfile[strlen(feedfile)-1]=='/')
+ feedfile= xasprintf("%s%s",feedfile,sitename);
+
+ const char *feedfile_forbidden= "?*[";
+ int c;
+ while ((c= *feedfile_forbidden++))
+ if (strchr(feedfile, c))
+ badusage("feed filename may not contain glob metacharacter %c",c);
+
+ if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
+ sysdie("could not ignore SIGPIPE");
+
+ if (become_daemon) {
+ for (i=3; i<255; i++)
+ /* do this now before we open syslog, etc. */
+ close(i);
+ openlog("innduct",LOG_NDELAY|LOG_PID,LOG_NEWS);
+
+ int null= open("/dev/null",O_RDWR);
+ if (null<0) sysdie("failed to open /dev/null");
+ dup2(null,0);
+ dup2(null,1);
+ dup2(null,2);
+ close(null);
+
+ pid_t child1= xfork("daemonise first fork");
+ if (child1) _exit(0);
+
+ pid_t sid= setsid();
+ if (sid != child1) sysdie("setsid failed");
+
+ pid_t child2= xfork("daemonise second fork");
+ if (child2) _exit(0);
+ }
+
+ notice("starting");
+
+ if (!filemon_init()) {
+ warn("no file monitoring available, polling");