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)
95 for (
int c = 0; c < 10; c++) {
96 if (
stack[c].identifier != NULL)
108 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
109 "in the code, or a new command which contains more than "
110 "10 identified tokens.\n");
116 for (
int c = 0; c < 10; c++) {
117 if (
stack[c].identifier != NULL) {
130 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
131 "in the code, or a new command which contains more than "
132 "10 identified tokens.\n");
138 for (
int c = 0; c < 10; c++) {
139 if (
stack[c].identifier == NULL)
141 if (strcmp(identifier,
stack[c].identifier) == 0)
149 for (
int c = 0; c < 10; c++) {
150 if (
stack[c].identifier == NULL)
152 if (strcmp(identifier,
stack[c].identifier) == 0)
161 for (
int c = 0; c < 10; c++) {
210 const char *beginning = *walk;
215 for (; **walk !=
'\0' && **walk !=
'"'; (*walk)++)
216 if (**walk ==
'\\' && *(*walk + 1) !=
'\0')
224 while (**walk !=
';' && **walk !=
',' &&
225 **walk !=
'\0' && **walk !=
'\r' &&
232 while (**walk !=
' ' && **walk !=
'\t' &&
233 **walk !=
']' && **walk !=
',' &&
234 **walk !=
';' && **walk !=
'\r' &&
235 **walk !=
'\n' && **walk !=
'\0')
239 if (*walk == beginning)
242 char *str =
scalloc(*walk - beginning + 1, 1);
245 for (inpos = 0, outpos = 0;
246 inpos < (*walk - beginning);
251 if (beginning[inpos] ==
'\\' && (beginning[inpos + 1] ==
'"' || beginning[inpos + 1] ==
'\\'))
253 str[outpos] = beginning[inpos];
267 DLOG(
"COMMAND: *%s*\n", input);
277 const char *walk = input;
278 const size_t len = strlen(input);
290 while ((
size_t)(walk - input) <= len) {
292 while ((*walk ==
' ' || *walk ==
'\t' ||
293 *walk ==
'\r' || *walk ==
'\n') &&
298 token_handled =
false;
299 for (c = 0; c < ptr->
n; c++) {
300 token = &(ptr->
array[c]);
303 if (token->
name[0] ==
'\'') {
304 if (strncasecmp(walk, token->
name + 1, strlen(token->
name) - 1) == 0) {
307 walk += strlen(token->
name) - 1;
309 token_handled =
true;
315 if (strcmp(token->
name,
"number") == 0) {
319 long int num = strtol(walk, &end, 10);
320 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
321 (errno != 0 && num == 0))
334 token_handled =
true;
338 if (strcmp(token->
name,
"string") == 0 ||
339 strcmp(token->
name,
"word") == 0) {
349 token_handled =
true;
354 if (strcmp(token->
name,
"end") == 0) {
355 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
357 token_handled =
true;
364 if (*walk ==
'\0' || *walk ==
';')
373 if (!token_handled) {
377 for (c = 0; c < ptr->
n; c++)
378 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
384 char *possible_tokens =
smalloc(tokenlen + 1);
385 char *tokenwalk = possible_tokens;
386 for (c = 0; c < ptr->
n; c++) {
387 token = &(ptr->
array[c]);
388 if (token->
name[0] ==
'\'') {
392 strcpy(tokenwalk, token->
name + 1);
393 tokenwalk += strlen(token->
name + 1);
399 strcpy(tokenwalk, token->
name);
400 tokenwalk += strlen(token->
name);
403 if (c < (ptr->
n - 1)) {
409 sasprintf(&errormessage,
"Expected one of these tokens: %s",
411 free(possible_tokens);
415 char *position =
smalloc(len + 1);
416 for (
const char *copywalk = input; *copywalk !=
'\0'; copywalk++)
417 position[(copywalk - input)] = (copywalk >= walk ?
'^' :
' ');
418 position[len] =
'\0';
420 ELOG(
"%s\n", errormessage);
421 ELOG(
"Your command: %s\n", input);
422 ELOG(
" %s\n", position);
440 ystr(
"errorposition");
484 fprintf(stdout,
"# ");
485 vfprintf(stdout, fmt, args);
493 vfprintf(stderr, fmt, args);
497 int main(
int argc,
char *argv[]) {
499 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
502 yajl_gen gen = yajl_gen_alloc(NULL);
static cmdp_token_ptr tokens[59]
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...
enum stack_entry::@1 type
static struct stack_entry stack[10]
static const char * get_string(const char *identifier)
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
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 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.
void debuglog(char *fmt,...)
struct tokenptr cmdp_token_ptr
CommandResult * parse_command(const char *input, yajl_gen gen)
Parses and executes the given command.
void command_result_free(CommandResult *result)
Frees a CommandResult.
static Match current_match
static long get_long(const char *identifier)
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 void push_string(const char *identifier, char *str)
union stack_entry::@2 val
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
static void GENERATED_call(const int call_identifier, struct CommandResultIR *result)
static struct CommandResultIR subcommand_output
static struct CommandResultIR command_output
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
static void next_state(const cmdp_token *token)
void errorlog(char *fmt,...)
static void push_long(const char *identifier, long num)