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
|
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include "cmdline.h"
#include "util.h"
void pa_cmdline_help(const char *argv0) {
const char *e;
if ((e = strrchr(argv0, '/')))
e++;
else
e = argv0;
printf("%s [options]\n"
" -L MODULE Load the specified plugin module with the specified argument\n"
" -F FILE A shortcut for '-L module-cli file=FILE', i.e. run the specified script after startup\n"
" -C A shortcut for '-L module-cli', i.e. open a command line on the running TTY\n"
" -D Daemonize after loading the modules\n"
" -h Show this help\n", e);
}
static void add_module(struct pa_cmdline *cmdline, char *name, char *arguments) {
struct pa_cmdline_module *m;
assert(cmdline && name);
m = malloc(sizeof(struct pa_cmdline_module));
assert(m);
m->name = name;
m->arguments = name;
m->next = NULL;
if (cmdline->last_module)
cmdline->last_module->next = m;
else {
assert(!cmdline->first_module);
cmdline->first_module = m;
}
cmdline->last_module = m;
}
struct pa_cmdline* pa_cmdline_parse(int argc, char * const argv []) {
char c;
struct pa_cmdline *cmdline = NULL;
assert(argc && argv);
cmdline = malloc(sizeof(struct pa_cmdline));
assert(cmdline);
cmdline->daemonize = cmdline->help = 0;
cmdline->first_module = cmdline->last_module = NULL;
while ((c = getopt(argc, argv, "L:F:CDh")) != -1) {
switch (c) {
case 'L': {
char *space;
if ((space = strchr(optarg, ' ')))
add_module(cmdline, strndup(optarg, space-optarg), space+1);
else
add_module(cmdline, strdup(optarg), NULL);
break;
}
case 'F':
add_module(cmdline, strdup("module-cli"), pa_sprintf_malloc("file='%s'", optarg));
break;
case 'C':
add_module(cmdline, strdup("module-cli"), NULL);
break;
case 'D':
cmdline->daemonize = 1;
break;
case 'h':
cmdline->help = 1;
break;
default:
goto fail;
}
}
return cmdline;
fail:
if (cmdline)
pa_cmdline_free(cmdline);
return NULL;
}
void pa_cmdline_free(struct pa_cmdline *cmd) {
struct pa_cmdline_module *m;
assert(cmd);
while ((m = cmd->first_module)) {
cmd->first_module = m->next;
free(m->name);
free(m->arguments);
free(m);
}
free(cmd);
}
|