/* Process the command line options and arguments. */ static lists_t_strs *process_command_line (int argc, char *argv[], struct parameters *params, lists_t_strs *deferred) { int ret, opt_index = 0; const char *jump_type; lists_t_strs *result; struct option long_options[] = { { "version", 0, NULL, 'V' }, { "help", 0, NULL, 'h' }, #ifndef NDEBUG { "debug", 0, NULL, 'D' }, #endif { "server", 0, NULL, 'S' }, { "foreground", 0, NULL, 'F' }, { "sound-driver", 1, NULL, 'R' }, { "music-dir", 0, NULL, 'm' }, { "append", 0, NULL, 'a' }, { "enqueue", 0, NULL, 'q' }, { "clear", 0, NULL, 'c' }, { "play", 0, NULL, 'p' }, { "playit", 0, NULL, 'l' }, { "stop", 0, NULL, 's' }, { "next", 0, NULL, 'f' }, { "previous", 0, NULL, 'r' }, { "exit", 0, NULL, 'x' }, { "theme", 1, NULL, 'T' }, { "config", 1, NULL, 'C' }, { "set-option", 1, NULL, 'O' }, { "moc-dir", 1, NULL, 'M' }, { "pause", 0, NULL, 'P' }, { "unpause", 0, NULL, 'U' }, { "toggle-pause", 0, NULL, 'G' }, { "sync", 0, NULL, 'y' }, { "nosync", 0, NULL, 'n' }, { "ascii", 0, NULL, 'A' }, { "info", 0, NULL, 'i' }, { "recursively", 0, NULL, 'e' }, { "seek", 1, NULL, 'k' }, { "jump", 1, NULL, 'j' }, { "format", 1, NULL, 'Q' }, { "volume", 1, NULL, 'v' }, { "toggle", 1, NULL, 't' }, { "on", 1, NULL, 'o' }, { "off", 1, NULL, 'u' }, { "playnum", 1, NULL, 'N' }, { 0, 0, 0, 0 } }; assert (argc >= 0); assert (argv != NULL); assert (argv[argc] == NULL); assert (params != NULL); assert (deferred != NULL); while ((ret = getopt_long(argc, argv, "VhDSFR:macpsxT:C:O:M:PUynArfiGelk:j:v:t:o:u:Q:qN:", long_options, &opt_index)) != -1) { switch (ret) { case 'V': show_version (); exit (EXIT_SUCCESS); case 'h': show_usage (argv[0]); exit (EXIT_SUCCESS); #ifndef NDEBUG case 'D': params->debug = 1; break; #endif case 'S': params->only_server = 1; break; case 'F': params->foreground = 1; params->only_server = 1; break; case 'R': if (!options_check_list ("SoundDriver", optarg)) fatal ("No such sound driver: %s", optarg); options_set_list ("SoundDriver", optarg, false); options_ignore_config ("SoundDriver"); break; case 'm': options_set_int ("StartInMusicDir", 1); options_ignore_config ("StartInMusicDir"); break; case 'a': case 'e': params->append = 1; params->dont_run_iface = 1; break; case 'q': params->enqueue = 1; params->dont_run_iface = 1; break; case 'c': params->clear = 1; params->dont_run_iface = 1; break; case 'i': params->get_file_info = 1; params->dont_run_iface = 1; break; case 'p': params->play = 1; params->dont_run_iface = 1; break; case 'l': params->playit = 1; params->dont_run_iface = 1; break; case 's': params->stop = 1; params->dont_run_iface = 1; break; case 'f': params->next = 1; params->dont_run_iface = 1; break; case 'r': params->previous = 1; params->dont_run_iface = 1; break; case 'x': params->exit = 1; params->dont_run_iface = 1; break; case 'P': params->pause = 1; params->dont_run_iface = 1; break; case 'U': params->unpause = 1; params->dont_run_iface = 1; break; case 'T': options_set_str ("ForceTheme", optarg); break; case 'C': params->config_file = xstrdup (optarg); break; case 'O': override_config_option (optarg, deferred); break; case 'M': options_set_str ("MOCDir", optarg); options_ignore_config ("MOCDir"); break; case 'y': options_set_int ("SyncPlaylist", 1); options_ignore_config ("SyncPlaylist"); break; case 'n': options_set_int ("SyncPlaylist", 0); options_ignore_config ("SyncPlaylist"); break; case 'A': options_set_int ("ASCIILines", 1); options_ignore_config ("ASCIILines"); break; case 'G': params->toggle_pause = 1; params->dont_run_iface = 1; break; case 'k': params->seek_by = get_num_param (optarg, NULL); params->dont_run_iface = 1; break; case 'j': params->jump_to = get_num_param (optarg, &jump_type); if (*jump_type) if (!jump_type[1]) if (*jump_type == '%' || tolower (*jump_type) == 's') { params->jump_type = tolower (*jump_type); params->dont_run_iface = 1; break; } //TODO: Add message explaining the error show_usage (argv[0]); exit (EXIT_FAILURE); case 'v' : params->adj_volume = optarg; params->dont_run_iface = 1; break; case 't' : params->toggle = optarg; params->dont_run_iface = 1; break; case 'o' : params->on = optarg; params->dont_run_iface = 1; break; case 'u' : params->off = optarg; params->dont_run_iface = 1; break; case 'Q': params->formatted_into_param = optarg; params->get_formatted_info = 1; params->dont_run_iface = 1; break; case 'N' : params->play_num = optarg; params->dont_run_iface = 1; break; default: show_usage (argv[0]); exit (EXIT_FAILURE); } } result = lists_strs_new (argc - optind); lists_strs_load (result, argv + optind); return result; }
/** Parse command line options. */ static int kd_options_parse(int argc, char * argv[]) { int c; char *oarg; opterr = 0; /* Parse command-line */ while (1) { c = getopt(argc, argv, "P:v:p:VdfD"); if (c == -1) break; switch (c) { /* Listening port */ case 'p': oarg = optarg; if (oarg != NULL) { uint16_t port; if (sscanf(oarg, "%hd", &port) < 1) { fprintf(stderr, "Invalid port number: %s", oarg); exit(1); } options_set_uint16("server.port", port); } else { fprintf(stderr, "The -p option requires a parameter."); exit(1); } break; /* Verbosity */ case 'v': oarg = optarg; if (oarg != NULL) { uint32_t v; if (sscanf(oarg, "%d", &v) < 1) { fprintf(stderr, "Invalid value for logging verbosity: %s", oarg); exit(1); } options_set_uint32("server.log_verbosity", v); } else { fprintf(stderr, "The -v option requires a parameter."); exit(1); } break; /* Create PID file. */ case 'P': oarg = optarg; if (oarg != NULL) options_set_str("server.pid_path", oarg); else { fprintf(stderr, "The -P option requires a parameter."); exit(1); } break; /* Version */ case 'V': kd_info(); return -1; case 'D': /* DEBUG mode, don't detach nor fork and log to stderr. */ options_set_bool("server.detach", 0); options_set_bool("server.fork", 0); options_set_str("server.log_driver", "stderr"); break; case 'd': /* Detach from controlling terminal? */ options_set_bool("server.detach", 1); break; case 'f': case '1': /* Fork new clients? */ options_set_bool("server.fork", 1); break; default: kd_options_print(); return -1; } } return 0; }