2 #define I3__FILE__ "commands_parser.c"
38 #define y(x, ...) (command_output.json_gen != NULL ? yajl_gen_##x(command_output.json_gen, ##__VA_ARGS__) : 0)
39 #define ystr(str) (command_output.json_gen != NULL ? yajl_gen_string(command_output.json_gen, (unsigned char *)str, strlen(str)) : 0)
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);
214 const char *beginning = *walk;
219 for (; **walk !=
'\0' && **walk !=
'"'; (*walk)++)
220 if (**walk ==
'\\' && *(*walk + 1) !=
'\0')
228 while (**walk !=
';' && **walk !=
',' &&
229 **walk !=
'\0' && **walk !=
'\r' &&
236 while (**walk !=
' ' && **walk !=
'\t' &&
237 **walk !=
']' && **walk !=
',' &&
238 **walk !=
';' && **walk !=
'\r' &&
239 **walk !=
'\n' && **walk !=
'\0')
243 if (*walk == beginning)
246 char *str =
scalloc(*walk - beginning + 1);
249 for (inpos = 0, outpos = 0;
250 inpos < (*walk - beginning);
255 if (beginning[inpos] ==
'\\' && (beginning[inpos + 1] ==
'"' || beginning[inpos + 1] ==
'\\'))
257 str[outpos] = beginning[inpos];
271 DLOG(
"COMMAND: *%s*\n", input);
281 const char *walk = input;
282 const size_t len = strlen(input);
294 while ((
size_t)(walk - input) <= len) {
296 while ((*walk ==
' ' || *walk ==
'\t' ||
297 *walk ==
'\r' || *walk ==
'\n') &&
302 token_handled =
false;
303 for (c = 0; c < ptr->
n; c++) {
304 token = &(ptr->
array[c]);
307 if (token->
name[0] ==
'\'') {
308 if (strncasecmp(walk, token->
name + 1, strlen(token->
name) - 1) == 0) {
311 walk += strlen(token->
name) - 1;
313 token_handled =
true;
319 if (strcmp(token->
name,
"string") == 0 ||
320 strcmp(token->
name,
"word") == 0) {
330 token_handled =
true;
335 if (strcmp(token->
name,
"end") == 0) {
336 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
338 token_handled =
true;
345 if (*walk ==
'\0' || *walk ==
';')
354 if (!token_handled) {
358 for (c = 0; c < ptr->
n; c++)
359 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
365 char *possible_tokens =
smalloc(tokenlen + 1);
366 char *tokenwalk = possible_tokens;
367 for (c = 0; c < ptr->
n; c++) {
368 token = &(ptr->
array[c]);
369 if (token->
name[0] ==
'\'') {
373 strcpy(tokenwalk, token->
name + 1);
374 tokenwalk += strlen(token->
name + 1);
380 strcpy(tokenwalk, token->
name);
381 tokenwalk += strlen(token->
name);
384 if (c < (ptr->
n - 1)) {
390 sasprintf(&errormessage,
"Expected one of these tokens: %s",
392 free(possible_tokens);
396 char *position =
smalloc(len + 1);
397 for (
const char *copywalk = input; *copywalk !=
'\0'; copywalk++)
398 position[(copywalk - input)] = (copywalk >= walk ?
'^' :
' ');
399 position[len] =
'\0';
401 ELOG(
"%s\n", errormessage);
402 ELOG(
"Your command: %s\n", input);
403 ELOG(
" %s\n", position);
421 ystr(
"errorposition");
465 fprintf(stdout,
"# ");
466 vfprintf(stdout, fmt, args);
474 vfprintf(stderr, fmt, args);
478 int main(
int argc,
char *argv[]) {
480 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
483 yajl_gen gen = yajl_gen_alloc(NULL);
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
static struct CommandResultIR command_output
static void GENERATED_call(const int call_identifier, struct CommandResultIR *result)
#define TAILQ_REMOVE(head, elm, field)
static void next_state(const cmdp_token *token)
struct tokenptr cmdp_token_ptr
CommandResult * parse_command(const char *input, yajl_gen gen)
Parses and executes the given command.
#define TAILQ_FIRST(head)
void command_result_free(CommandResult *result)
Frees a CommandResult.
static Match current_match
static struct stack_entry stack[10]
A struct that contains useful information about the result of a command as a whole (e...
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
static char * get_string(const char *identifier)
static void clear_stack(void)
char * parse_string(const char **walk, bool as_word)
Parses a string (or word, if as_word is true).
void errorlog(char *fmt,...)
static void push_string(const char *identifier, char *str)
#define TAILQ_EMPTY(head)
#define TAILQ_ENTRY(type)
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
static struct CommandResultIR subcommand_output
void debuglog(char *fmt,...)
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_INSERT_TAIL(head, elm, field)
#define TAILQ_HEAD_INITIALIZER(head)
void cmd_criteria_init(I3_CMD)
Initializes the specified 'Match' data structure and the initial state of commands.c for matching target windows of a command.
int main(int argc, char *argv[])
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_HEAD(name, type)
static cmdp_token_ptr tokens[53]