summaryrefslogtreecommitdiffstats
path: root/polyp/cmdline.c
blob: 9935e7b66de3a84a20f41e6136274790648b1f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio 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 of the License,
  or (at your option) any later version.
 
  polypaudio 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 polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>

#include "cmdline.h"
#include "util.h"
#include "strbuf.h"
#include "xmalloc.h"

void pa_cmdline_help(const char *argv0) {
    const char *e;

    if ((e = strrchr(argv0, '/')))
        e++;
    else
        e = argv0;
    
    printf("%s [options]\n"
           "  -r         Try to set high process priority (only available as root)\n"
           "  -R         Don't drop root if SETUID root\n"
           "  -L MODULE  Load the specified plugin module with the specified argument\n"
           "  -F FILE    Run the specified script\n"
           "  -C         Open a command line on the running TTY\n"
           "  -D         Daemonize after loading the modules\n"
           "  -f         Dont quit when the startup fails\n"
           "  -v         Verbose startup\n"
           "  -h         Show this help\n"
           "  -V         Show version\n", e);
}

struct pa_cmdline* pa_cmdline_parse(int argc, char * const argv []) {
    char c;
    struct pa_cmdline *cmdline = NULL;
    struct pa_strbuf *buf = NULL;
    assert(argc && argv);

    cmdline = pa_xmalloc(sizeof(struct pa_cmdline));
    cmdline->daemonize = cmdline->help = cmdline->verbose = cmdline->high_priority = cmdline->stay_root = cmdline->version = 0;
    cmdline->fail = 1;

    buf = pa_strbuf_new();
    assert(buf);
    
    while ((c = getopt(argc, argv, "L:F:CDhfvrRV")) != -1) {
        switch (c) {
            case 'L':
                pa_strbuf_printf(buf, "load %s\n", optarg);
                break;
            case 'F':
                pa_strbuf_printf(buf, ".include %s\n", optarg);
                break;
            case 'C':
                pa_strbuf_puts(buf, "load module-cli\n");
                break;
            case 'D':
                cmdline->daemonize = 1;
                break;
            case 'h':
                cmdline->help = 1;
                break;
            case 'f':
                cmdline->fail = 0;
                break;
            case 'v':
                cmdline->verbose = 1;
                break;
            case 'r':
                cmdline->high_priority = 1;
                break;
            case 'R':
                cmdline->stay_root = 1;
                break;
            case 'V':
                cmdline->version = 1;
                break;
                
            default:
                goto fail;
        }
    }

    cmdline->cli_commands = pa_strbuf_tostring_free(buf);
    return cmdline;
    
fail:
    if (cmdline)
        pa_cmdline_free(cmdline);
    if (buf)
        pa_strbuf_free(buf);
    return NULL;
}

void pa_cmdline_free(struct pa_cmdline *cmd) {
    assert(cmd);
    pa_xfree(cmd->cli_commands);
    pa_xfree(cmd);
}