static void parse_args(int ac, char **av) { int c; gl_env.history_size = 0; gl_env.quiet = 0; gl_env.last_update_time = 0; gl_env.line_by_line = 0; gl_env.interval = 1; while (1) { int option_index = 0; static struct option long_options[] = { {"size", 1, 0, 's'}, {"quiet", 0, 0, 'q'}, {"help", 0, 0, 'h'}, {"version", 0, 0, 'v'}, {"line-by-line", 1, 0, 'l'}, {"interval", 1, 0, 'i'}, {0, 0, 0, 0} }; c = getopt_long(ac, av, "qhvl:s:i:", long_options, &option_index); if (c == -1) break; switch (c) { case 'l': gl_env.line_by_line = atoi(optarg); break; case 'q': gl_env.quiet = 1; break; case 's': gl_env.history_size = atoi(optarg); break; case 'i': gl_env.interval = atoi(optarg); break; case 'v': version_and_exit(); case 'h': usage_and_exit(EXIT_SUCCESS); default: usage_and_exit(EXIT_FAILURE); } } if (isatty(fileno(stdin))) usage_and_exit(EXIT_FAILURE); if (gl_env.history_size == 0) gl_env.history_size = DEFAULT_HISTORY_SIZE; }
static void parse_long_option( const int argc, const char *const argv[], int *i, struct argument_flags *flags) { if (!strcmp(argv[*i], "--help")) { help_and_exit(flags->program_name); } else if (!strcmp(argv[*i], "--version")) { version_and_exit(); } else if (!strcmp(argv[*i], "--foreground")) { flags->daemonize = false; } else if (!strcmp(argv[*i], "--config")) { if (++(*i) == argc) { fprintf(stderr, "You must specify a configuration file.\n"); cleanup(EXIT_ARGUMENTS, true); } flags->conf_file = argv[*i]; } else if (!strcmp(argv[*i], "--noconfig")) { flags->conf_file = NULL; } else if (!strcmp(argv[*i], "--lax")) { fprintf(stderr, "Note: --lax has been enabled. Security checks will *not* be performed.\n"); flags->strict = false; } else if (!strcmp(argv[*i], "--pidfile")) { if (++(*i) == argc) { fprintf(stderr, "You must specify a pid file.\n"); cleanup(EXIT_ARGUMENTS, true); } flags->pid_file = argv[*i]; } else if (!strcmp(argv[*i], "--quotes")) { if (++(*i) == argc) { fprintf(stderr, "You must specify a quotes file.\n"); cleanup(EXIT_ARGUMENTS, true); } flags->quotes_file = argv[*i]; } else if (!strcmp(argv[*i], "--journal")) { if (++(*i) == argc) { fprintf(stderr, "You must specify a journal file.\n"); cleanup(EXIT_ARGUMENTS, true); } flags->journal_file = argv[*i]; } else if (!strcmp(argv[*i], "--ipv4")) { if (flags->iproto == PROTOCOL_IPv6) { fprintf(stderr, "Conflicting options passed: -4 and -6.\n"); cleanup(EXIT_ARGUMENTS, true); } flags->iproto = PROTOCOL_IPv4; } else if (!strcmp(argv[*i], "--ipv6")) { if (flags->iproto == PROTOCOL_IPv4) { fprintf(stderr, "Conflicting options passed: -4 and -6.\n"); cleanup(EXIT_ARGUMENTS, true); } flags->iproto = PROTOCOL_IPv6; } else if (!strcmp(argv[*i], "--tcp")) { if (flags->tproto == PROTOCOL_UDP) { fprintf(stderr, "Conflicting options passed: -t and -u.\n"); cleanup(EXIT_ARGUMENTS, true); } flags->tproto = PROTOCOL_TCP; } else if (!strcmp(argv[*i], "--udp")) { if (flags->tproto == PROTOCOL_TCP) { fprintf(stderr, "Conflicting options passed: -t and -u.\n"); cleanup(EXIT_ARGUMENTS, true); } flags->tproto = PROTOCOL_UDP; } else if (!strcmp(argv[*i], "--quiet")) { close_journal(); } else { printf("Unrecognized long option: %s.\n", argv[*i]); usage_and_exit(flags->program_name); } }