vdr 2.6.9
args.c
Go to the documentation of this file.
1/*
2 * args.c: Read arguments from files
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * Original version written by Lars Hanisch <dvb@flensrocker.de>.
8 *
9 * $Id: args.c 1.1 2014/04/14 12:02:38 kls Exp $
10 */
11
12#include "args.h"
13#include <unistd.h>
14
15cArgs::cArgs(const char *Argv0)
16{
17 argv0 = Argv0;
18 argc = 0;
19 argv = NULL;
20}
21
23{
24 if (argv != NULL)
25 delete [] argv;
26}
27
28bool cArgs::AddArg(const char *s)
29{
30 if (inVdrSection)
31 args.Append(strdup(s));
32 else if (*lastArg == NULL)
33 return false;
34 else
35 lastArg = cString::sprintf("%s %s", *lastArg, s);
36 return true;
37}
38
39bool cArgs::ReadDirectory(const char *Directory)
40{
41 if (argv != NULL)
42 delete [] argv;
43 argc = 0;
44 argv = NULL;
45 args.Clear();
46 lastArg = NULL;
47 inVdrSection = false;
48 cFileNameList files(Directory, false);
49 if (files.Size() == 0)
50 return false;
51 for (int i = 0; i < files.Size(); i++) {
52 const char *fileName = files.At(i);
53 if (startswith(fileName, ".") || !endswith(fileName, ".conf"))
54 continue;
55 cString fullFileName = AddDirectory(Directory, fileName);
56 struct stat fs;
57 if ((access(*fullFileName, F_OK) != 0) || (stat(*fullFileName, &fs) != 0) || S_ISDIR(fs.st_mode))
58 continue;
59 bool ok = true;
60 int line = 0;
61 FILE *f = fopen(*fullFileName, "r");
62 if (f) {
63 char *s;
64 cReadLine ReadLine;
65 while ((s = ReadLine.Read(f)) != NULL) {
66 line++;
67 s = stripspace(skipspace(s));
68 if (!isempty(s) && (s[0] != '#')) {
69 if (startswith(s, "[") && endswith(s, "]")) {
70 s[strlen(s) - 1] = 0;
71 s++;
72 if (*lastArg) {
73 args.Append(strdup(*lastArg));
74 lastArg = NULL;
75 }
76 if (strcmp(s, "vdr") == 0)
77 inVdrSection = true;
78 else {
79 inVdrSection = false;
80 lastArg = cString::sprintf("--plugin=%s", s);
81 }
82 }
83 else {
84 if ((strlen(s) > 2) && (s[0] == '-') && (s[1] != '-')) { // short option, split at first space
85 char *p = strchr(s, ' ');
86 if (p == NULL) {
87 ok = AddArg(s);
88 if (!ok)
89 break;
90 }
91 else {
92 *p = 0;
93 p++;
94 ok = AddArg(s);
95 if (!ok)
96 break;
97 ok = AddArg(p);
98 if (!ok)
99 break;
100 }
101 }
102 else {
103 ok = AddArg(s);
104 if (!ok)
105 break;
106 }
107 }
108 }
109 }
110 fclose(f);
111 }
112 if (!ok) {
113 esyslog("ERROR: args file %s, line %d", *fullFileName, line);
114 return false;
115 }
116 }
117 if (*lastArg) {
118 args.Append(strdup(*lastArg));
119 lastArg = NULL;
120 }
121 argv = new char*[args.Size() + 1];
122 argv[0] = strdup(*argv0);
123 argc = 1;
124 for (int i = 0; i < args.Size(); i++) {
125 argv[argc] = args.At(i);
126 argc++;
127 }
128 return true;
129}
~cArgs(void)
Definition args.c:22
bool inVdrSection
Definition args.h:22
cString argv0
Definition args.h:19
cStringList args
Definition args.h:20
cArgs(const char *Argv0)
Definition args.c:15
bool AddArg(const char *s)
Definition args.c:28
cString lastArg
Definition args.h:21
char ** argv
Definition args.h:24
bool ReadDirectory(const char *Directory)
Definition args.c:39
int argc
Definition args.h:23
char * Read(FILE *f)
Definition tools.c:1512
virtual void Clear(void)
Definition tools.c:1624
static cString sprintf(const char *fmt,...) __attribute__((format(printf
Definition tools.c:1180
int Size(void) const
Definition tools.h:767
virtual void Append(T Data)
Definition tools.h:787
T & At(int Index) const
Definition tools.h:744
bool isempty(const char *s)
Definition tools.c:354
bool startswith(const char *s, const char *p)
Definition tools.c:334
char * stripspace(char *s)
Definition tools.c:224
bool endswith(const char *s, const char *p)
Definition tools.c:343
cString AddDirectory(const char *DirName, const char *FileName)
Definition tools.c:407
char * skipspace(const char *s)
Definition tools.h:244
#define esyslog(a...)
Definition tools.h:35