| 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 |
#include <config.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <assert.h> |
| 35 |
#include <regex.h> |
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
#include "arg.h" |
| 39 |
#include "misc.h" |
| 40 |
|
| 41 |
arg_t *arg_alloc(void) { |
| 42 |
arg_t *arg = (arg_t*)malloc(sizeof(arg_t));; |
| 43 |
|
| 44 |
arg_default(arg); |
| 45 |
|
| 46 |
arg->verbose = 0; |
| 47 |
arg->showempty = 1; |
| 48 |
arg->debug = 0; |
| 49 |
arg->quiet = 0; |
| 50 |
arg->run = 0; |
| 51 |
arg->exportcsv = 0; |
| 52 |
arg->preload = 3; |
| 53 |
|
| 54 |
|
| 55 |
#ifdef HAVE_GUI |
| 56 |
arg->use_gui = 1; |
| 57 |
#else |
| 58 |
arg->use_gui = 0; |
| 59 |
#endif |
| 60 |
|
| 61 |
arg->cfname = 0; |
| 62 |
arg->hfname = 0; |
| 63 |
arg->ofname = 0; |
| 64 |
arg->xfname = 0; |
| 65 |
arg->sfname = 0; |
| 66 |
arg->lfname = 0; |
| 67 |
|
| 68 |
arg->tblstr = 0; |
| 69 |
arg->samdir = 0; |
| 70 |
arg->stats = 0; |
| 71 |
arg->ssave = 0; |
| 72 |
|
| 73 |
arg->lfile = stdout; |
| 74 |
|
| 75 |
arg->table_path = list_alloc(); |
| 76 |
arg->table_str_conf = list_alloc(); |
| 77 |
arg->table_str_cmd = list_alloc(); |
| 78 |
|
| 79 |
return arg; |
| 80 |
} |
| 81 |
|
| 82 |
void arg_free(arg_t *arg) { |
| 83 |
list_free(arg->table_path); |
| 84 |
list_free(arg->table_str_conf); |
| 85 |
list_free(arg->table_str_cmd); |
| 86 |
free(arg); |
| 87 |
} |
| 88 |
|
| 89 |
void arg_default(arg_t *arg) { |
| 90 |
arg->nthreads = 8; |
| 91 |
arg->nhredux = 50000; |
| 92 |
arg->mdqueue = 500; |
| 93 |
arg->bforce = 1; |
| 94 |
arg->bforce_len = 4; |
| 95 |
arg->hideuname = 0; |
| 96 |
arg->auditmode = 0; |
| 97 |
} |
| 98 |
|
| 99 |
int arg_read_conf(arg_t *arg) { |
| 100 |
assert(arg->cfname != 0); |
| 101 |
|
| 102 |
FILE *cfile = fopen(arg->cfname, "r"); |
| 103 |
if (cfile == 0) return -1; |
| 104 |
|
| 105 |
static const char *pattern[11] = { |
| 106 |
"^[ \t]*#", |
| 107 |
"^[ \t]*$", |
| 108 |
"^[ \t]*table[ \t]+([^\t]+)[ \t]*$", |
| 109 |
"^[ \t]*nthreads[ \t]+([^ \t]+)[ \t]*$", |
| 110 |
"^[ \t]*nhredux[ \t]+([^ \t]+)[ \t]*$", |
| 111 |
"^[ \t]*maxdqueue[ \t]+([^ \t]+)[ \t]*$", |
| 112 |
"^[ \t]*bforce[ \t]+([^ \t]+)[ \t]*$", |
| 113 |
"^[ \t]*session_file[ \t]+([^ \t]+)[ \t]*$", |
| 114 |
"^[ \t]*session_save[ \t]+([^ \t]+)[ \t]*$", |
| 115 |
"^[ \t]*hideuname[ \t]+([^ \t]+)[ \t]*$", |
| 116 |
"^[ \t]*auditmode[ \t]+([^ \t]+)[ \t]*$"}; |
| 117 |
|
| 118 |
regex_t regex[11]; |
| 119 |
regmatch_t match[2]; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
int n = sizeof(regex) / sizeof(regex_t); |
| 124 |
int i; |
| 125 |
|
| 126 |
for (i=0; i<n; ++i) |
| 127 |
regcomp(regex+i, pattern[i], REG_EXTENDED); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
char buff[STR_BUFF_SIZE]; |
| 132 |
char tmp[STR_BUFF_SIZE]; |
| 133 |
int count = 0; |
| 134 |
|
| 135 |
while (fgets(buff, sizeof(buff), cfile) != 0 && ++count) { |
| 136 |
|
| 137 |
|
| 138 |
int len = strlen(buff); |
| 139 |
if (buff[len-1] == '\n') buff[len-1] = '\0'; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
for (i=0; i<n; ++i) |
| 144 |
if (regexec(regex+i, buff, 2, match, 0) == 0) break; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if (i == n) { |
| 149 |
printf("Invalid parameter at line %d of %s\n", count, arg->cfname); |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if (i < 2) continue; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
int so = match[1].rm_so; |
| 160 |
int eo = match[1].rm_eo; |
| 161 |
|
| 162 |
len = eo-so+1; |
| 163 |
snprintf(tmp, len+1, "%s", buff+so); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (i == 2) |
| 168 |
list_add_tail(arg->table_str_conf, strdup(tmp)); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
else if (i == 3) |
| 173 |
arg->nthreads = atoi(tmp); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
else if (i == 4) |
| 178 |
arg->nhredux = atoi(tmp); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
else if (i == 5) |
| 183 |
arg->mdqueue = atoi(tmp); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
else if (i == 6) |
| 188 |
arg->bforce = atoi(tmp); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
else if (i == 7) |
| 193 |
arg->sfname = strdup(tmp); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
else if (i == 8) |
| 198 |
arg->ssave = atoi(tmp); |
| 199 |
|
| 200 |
|
| 201 |
else if (i == 9) |
| 202 |
arg->hideuname = atoi(tmp); |
| 203 |
|
| 204 |
|
| 205 |
else if (i == 10) |
| 206 |
arg->auditmode = atoi(tmp); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
for (i=0; i<n; ++i) regfree(regex+i); |
| 211 |
fclose(cfile); |
| 212 |
|
| 213 |
return 0; |
| 214 |
} |