82 for (
int c = 0; c < 10; c++) {
83 if (
stack[c].identifier != NULL)
94 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
95 "in the code, or a new command which contains more than "
96 "10 identified tokens.\n");
103 DLOG(
"Getting string %s from stack...\n", identifier);
104 for (
int c = 0; c < 10; c++) {
105 if (
stack[c].identifier == NULL)
107 if (strcmp(identifier,
stack[c].identifier) == 0)
114 DLOG(
"clearing stack.\n");
115 for (
int c = 0; c < 10; c++) {
130 typedef struct criterion {
137 static
TAILQ_HEAD(criteria_head, criterion) criteria =
145 static
void push_criterion(
void *unused_criteria, const
char *type,
147 struct criterion *criterion = malloc(
sizeof(
struct criterion));
148 criterion->type = strdup(type);
149 criterion->value = strdup(value);
158 static void clear_criteria(
void *unused_criteria) {
159 struct criterion *criterion;
162 free(criterion->type);
163 free(criterion->value);
186 DLOG(
"should call stuff, yay. call_id = %d\n",
218 DLOG(
"new parser handling: %s\n", input);
223 const char *walk = input;
224 const size_t len = strlen(input);
236 while ((walk - input) <= len) {
238 while ((*walk ==
' ' || *walk ==
'\t' ||
239 *walk ==
'\r' || *walk ==
'\n') && *walk !=
'\0')
242 DLOG(
"remaining input = %s\n", walk);
245 token_handled =
false;
246 for (c = 0; c < ptr->
n; c++) {
247 token = &(ptr->
array[c]);
248 DLOG(
"trying token %d = %s\n", c, token->
name);
251 if (token->
name[0] ==
'\'') {
253 if (strncasecmp(walk, token->
name + 1, strlen(token->
name) - 1) == 0) {
254 DLOG(
"found literal, moving to next state\n");
257 walk += strlen(token->
name) - 1;
259 token_handled =
true;
265 if (strcmp(token->
name,
"string") == 0 ||
266 strcmp(token->
name,
"word") == 0) {
267 DLOG(
"parsing this as a string\n");
268 const char *beginning = walk;
273 while (*walk !=
'\0' && (*walk !=
'"' || *(walk-1) ==
'\\'))
276 if (token->
name[0] ==
's') {
281 while (*walk !=
';' && *walk !=
',' &&
282 *walk !=
'\0' && *walk !=
'\r' &&
289 while (*walk !=
' ' && *walk !=
'\t' &&
290 *walk !=
']' && *walk !=
',' &&
291 *walk !=
';' && *walk !=
'\r' &&
292 *walk !=
'\n' && *walk !=
'\0')
296 if (walk != beginning) {
297 char *str =
scalloc(walk-beginning + 1);
300 for (inpos = 0, outpos = 0;
301 inpos < (walk-beginning);
306 if (beginning[inpos] ==
'\\' && beginning[inpos+1] ==
'"')
308 str[outpos] = beginning[inpos];
312 DLOG(
"str is \"%s\"\n", str);
318 token_handled =
true;
323 if (strcmp(token->
name,
"end") == 0) {
324 DLOG(
"checking for the end token.\n");
325 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
326 DLOG(
"yes, indeed. end\n");
328 token_handled =
true;
335 if (*walk ==
'\0' || *walk ==
';')
344 if (!token_handled) {
348 for (c = 0; c < ptr->
n; c++)
349 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
355 char *possible_tokens =
smalloc(tokenlen + 1);
356 char *tokenwalk = possible_tokens;
357 for (c = 0; c < ptr->
n; c++) {
358 token = &(ptr->
array[c]);
359 if (token->
name[0] ==
'\'') {
363 strcpy(tokenwalk, token->
name + 1);
364 tokenwalk += strlen(token->
name + 1);
370 strcpy(tokenwalk, token->
name);
371 tokenwalk += strlen(token->
name);
374 if (c < (ptr->
n - 1)) {
380 sasprintf(&errormessage,
"Expected one of these tokens: %s",
382 free(possible_tokens);
386 char *position =
smalloc(len + 1);
387 for (
const char *copywalk = input; *copywalk !=
'\0'; copywalk++)
388 position[(copywalk - input)] = (copywalk >= walk ?
'^' :
' ');
389 position[len] =
'\0';
391 printf(
"%s\n", errormessage);
392 printf(
"Your command: %s\n", input);
393 printf(
" %s\n", position);
424 void debuglog(uint64_t lev,
char *fmt, ...) {
428 fprintf(stderr,
"# ");
429 vfprintf(stderr, fmt, args);
433 int main(
int argc,
char *argv[]) {
435 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);