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
125
126
127
128
129
|
/*
* args.c: Read arguments from files
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* Original version written by Lars Hanisch <dvb@flensrocker.de>.
*
* $Id: args.c 1.1 2014/04/14 12:02:38 kls Exp $
*/
#include "args.h"
#include <unistd.h>
cArgs::cArgs(const char *Argv0)
{
argv0 = Argv0;
argc = 0;
argv = NULL;
}
cArgs::~cArgs(void)
{
if (argv != NULL)
delete [] argv;
}
bool cArgs::AddArg(const char *s)
{
if (inVdrSection)
args.Append(strdup(s));
else if (*lastArg == NULL)
return false;
else
lastArg = cString::sprintf("%s %s", *lastArg, s);
return true;
}
bool cArgs::ReadDirectory(const char *Directory)
{
if (argv != NULL)
delete [] argv;
argc = 0;
argv = NULL;
args.Clear();
lastArg = NULL;
inVdrSection = false;
cFileNameList files(Directory, false);
if (files.Size() == 0)
return false;
for (int i = 0; i < files.Size(); i++) {
const char *fileName = files.At(i);
if (startswith(fileName, ".") || !endswith(fileName, ".conf"))
continue;
cString fullFileName = AddDirectory(Directory, fileName);
struct stat fs;
if ((access(*fullFileName, F_OK) != 0) || (stat(*fullFileName, &fs) != 0) || S_ISDIR(fs.st_mode))
continue;
bool ok = true;
int line = 0;
FILE *f = fopen(*fullFileName, "r");
if (f) {
char *s;
cReadLine ReadLine;
while ((s = ReadLine.Read(f)) != NULL) {
line++;
s = stripspace(skipspace(s));
if (!isempty(s) && (s[0] != '#')) {
if (startswith(s, "[") && endswith(s, "]")) {
s[strlen(s) - 1] = 0;
s++;
if (*lastArg) {
args.Append(strdup(*lastArg));
lastArg = NULL;
}
if (strcmp(s, "vdr") == 0)
inVdrSection = true;
else {
inVdrSection = false;
lastArg = cString::sprintf("--plugin=%s", s);
}
}
else {
if ((strlen(s) > 2) && (s[0] == '-') && (s[1] != '-')) { // short option, split at first space
char *p = strchr(s, ' ');
if (p == NULL) {
ok = AddArg(s);
if (!ok)
break;
}
else {
*p = 0;
p++;
ok = AddArg(s);
if (!ok)
break;
ok = AddArg(p);
if (!ok)
break;
}
}
else {
ok = AddArg(s);
if (!ok)
break;
}
}
}
}
fclose(f);
}
if (!ok) {
esyslog("ERROR: args file %s, line %d", *fullFileName, line);
return false;
}
}
if (*lastArg) {
args.Append(strdup(*lastArg));
lastArg = NULL;
}
argv = new char*[args.Size() + 1];
argv[0] = strdup(*argv0);
argc = 1;
for (int i = 0; i < args.Size(); i++) {
argv[argc] = args.At(i);
argc++;
}
return true;
}
|