2 #define I3__FILE__ "commands_parser.c"
38 #define y(x, ...) yajl_gen_ ## x (command_output.json_gen, ##__VA_ARGS__)
39 #define ystr(str) yajl_gen_string(command_output.json_gen, (unsigned char*)str, strlen(str))
88 for (
int c = 0; c < 10; c++) {
89 if (
stack[c].identifier != NULL)
100 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
101 "in the code, or a new command which contains more than "
102 "10 identified tokens.\n");
109 for (
int c = 0; c < 10; c++) {
110 if (
stack[c].identifier == NULL)
112 if (strcmp(identifier,
stack[c].identifier) == 0)
119 for (
int c = 0; c < 10; c++) {
134 typedef struct criterion {
141 static
TAILQ_HEAD(criteria_head, criterion) criteria =
149 static
void push_criterion(
void *unused_criteria, const
char *type,
151 struct criterion *criterion = malloc(
sizeof(
struct criterion));
152 criterion->type = strdup(type);
153 criterion->value = strdup(value);
162 static void clear_criteria(
void *unused_criteria) {
163 struct criterion *criterion;
166 free(criterion->type);
167 free(criterion->value);
208 DLOG(
"COMMAND: *%s*\n", input);
221 const char *walk = input;
222 const size_t len = strlen(input);
234 while ((walk - input) <= len) {
236 while ((*walk ==
' ' || *walk ==
'\t' ||
237 *walk ==
'\r' || *walk ==
'\n') && *walk !=
'\0')
241 token_handled =
false;
242 for (c = 0; c < ptr->
n; c++) {
243 token = &(ptr->
array[c]);
246 if (token->
name[0] ==
'\'') {
247 if (strncasecmp(walk, token->
name + 1, strlen(token->
name) - 1) == 0) {
250 walk += strlen(token->
name) - 1;
252 token_handled =
true;
258 if (strcmp(token->
name,
"string") == 0 ||
259 strcmp(token->
name,
"word") == 0) {
260 const char *beginning = walk;
265 while (*walk !=
'\0' && (*walk !=
'"' || *(walk-1) ==
'\\'))
268 if (token->
name[0] ==
's') {
273 while (*walk !=
';' && *walk !=
',' &&
274 *walk !=
'\0' && *walk !=
'\r' &&
281 while (*walk !=
' ' && *walk !=
'\t' &&
282 *walk !=
']' && *walk !=
',' &&
283 *walk !=
';' && *walk !=
'\r' &&
284 *walk !=
'\n' && *walk !=
'\0')
288 if (walk != beginning) {
289 char *str =
scalloc(walk-beginning + 1);
292 for (inpos = 0, outpos = 0;
293 inpos < (walk-beginning);
298 if (beginning[inpos] ==
'\\' && beginning[inpos+1] ==
'"')
300 str[outpos] = beginning[inpos];
309 token_handled =
true;
314 if (strcmp(token->
name,
"end") == 0) {
315 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
317 token_handled =
true;
324 if (*walk ==
'\0' || *walk ==
';')
333 if (!token_handled) {
337 for (c = 0; c < ptr->
n; c++)
338 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
344 char *possible_tokens =
smalloc(tokenlen + 1);
345 char *tokenwalk = possible_tokens;
346 for (c = 0; c < ptr->
n; c++) {
347 token = &(ptr->
array[c]);
348 if (token->
name[0] ==
'\'') {
352 strcpy(tokenwalk, token->
name + 1);
353 tokenwalk += strlen(token->
name + 1);
359 strcpy(tokenwalk, token->
name);
360 tokenwalk += strlen(token->
name);
363 if (c < (ptr->
n - 1)) {
369 sasprintf(&errormessage,
"Expected one of these tokens: %s",
371 free(possible_tokens);
375 char *position =
smalloc(len + 1);
376 for (
const char *copywalk = input; *copywalk !=
'\0'; copywalk++)
377 position[(copywalk - input)] = (copywalk >= walk ?
'^' :
' ');
378 position[len] =
'\0';
380 ELOG(
"%s\n", errormessage);
381 ELOG(
"Your command: %s\n", input);
382 ELOG(
" %s\n", position);
397 ystr(
"errorposition");
430 fprintf(stdout,
"# ");
431 vfprintf(stdout, fmt, args);
439 vfprintf(stderr, fmt, args);
443 int main(
int argc,
char *argv[]) {
445 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);