36 #define y(x, ...) (command_output.json_gen != NULL ? yajl_gen_##x(command_output.json_gen, ##__VA_ARGS__) : 0) 37 #define ystr(str) (command_output.json_gen != NULL ? yajl_gen_string(command_output.json_gen, (unsigned char *)str, strlen(str)) : 0) 47 #include "GENERATED_command_enums.h" 64 #include "GENERATED_command_tokens.h" 93 for (
int c = 0; c < 10; c++) {
94 if (
stack[c].identifier != NULL)
106 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug " 107 "in the code, or a new command which contains more than " 108 "10 identified tokens.\n");
114 for (
int c = 0; c < 10; c++) {
115 if (
stack[c].identifier != NULL) {
128 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug " 129 "in the code, or a new command which contains more than " 130 "10 identified tokens.\n");
136 for (
int c = 0; c < 10; c++) {
137 if (
stack[c].identifier == NULL)
139 if (strcmp(identifier,
stack[c].identifier) == 0)
147 for (
int c = 0; c < 10; c++) {
148 if (
stack[c].identifier == NULL)
150 if (strcmp(identifier,
stack[c].identifier) == 0)
159 for (
int c = 0; c < 10; c++) {
179 #include "GENERATED_command_call.h" 196 if (
state == INITIAL) {
208 const char *beginning = *walk;
213 for (; **walk !=
'\0' && **walk !=
'"'; (*walk)++)
214 if (**walk ==
'\\' && *(*walk + 1) !=
'\0')
222 while (**walk !=
';' && **walk !=
',' &&
223 **walk !=
'\0' && **walk !=
'\r' &&
230 while (**walk !=
' ' && **walk !=
'\t' &&
231 **walk !=
']' && **walk !=
',' &&
232 **walk !=
';' && **walk !=
'\r' &&
233 **walk !=
'\n' && **walk !=
'\0')
237 if (*walk == beginning)
240 char *str =
scalloc(*walk - beginning + 1, 1);
243 for (inpos = 0, outpos = 0;
244 inpos < (*walk - beginning);
249 if (beginning[inpos] ==
'\\' && (beginning[inpos + 1] ==
'"' || beginning[inpos + 1] ==
'\\'))
251 str[outpos] = beginning[inpos];
265 DLOG(
"COMMAND: *%s*\n", input);
275 const char *walk = input;
276 const size_t len = strlen(input);
288 while ((
size_t)(walk - input) <= len) {
290 while ((*walk ==
' ' || *walk ==
'\t' ||
291 *walk ==
'\r' || *walk ==
'\n') &&
296 token_handled =
false;
297 for (c = 0; c < ptr->
n; c++) {
298 token = &(ptr->
array[c]);
301 if (token->
name[0] ==
'\'') {
302 if (strncasecmp(walk, token->
name + 1, strlen(token->
name) - 1) == 0) {
305 walk += strlen(token->
name) - 1;
307 token_handled =
true;
313 if (strcmp(token->
name,
"number") == 0) {
317 long int num = strtol(walk, &end, 10);
318 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
319 (errno != 0 && num == 0))
332 token_handled =
true;
336 if (strcmp(token->
name,
"string") == 0 ||
337 strcmp(token->
name,
"word") == 0) {
347 token_handled =
true;
352 if (strcmp(token->
name,
"end") == 0) {
353 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
355 token_handled =
true;
362 if (*walk ==
'\0' || *walk ==
';')
371 if (!token_handled) {
375 for (c = 0; c < ptr->
n; c++)
376 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
382 char *possible_tokens =
smalloc(tokenlen + 1);
383 char *tokenwalk = possible_tokens;
384 for (c = 0; c < ptr->
n; c++) {
385 token = &(ptr->
array[c]);
386 if (token->
name[0] ==
'\'') {
390 strcpy(tokenwalk, token->
name + 1);
391 tokenwalk += strlen(token->
name + 1);
397 strcpy(tokenwalk, token->
name);
398 tokenwalk += strlen(token->
name);
401 if (c < (ptr->
n - 1)) {
407 sasprintf(&errormessage,
"Expected one of these tokens: %s",
409 free(possible_tokens);
413 char *position =
smalloc(len + 1);
414 for (
const char *copywalk = input; *copywalk !=
'\0'; copywalk++)
415 position[(copywalk - input)] = (copywalk >= walk ?
'^' :
' ');
416 position[len] =
'\0';
418 ELOG(
"%s\n", errormessage);
419 ELOG(
"Your command: %s\n", input);
420 ELOG(
" %s\n", position);
438 ystr(
"errorposition");
482 fprintf(stdout,
"# ");
483 vfprintf(stdout, fmt, args);
491 vfprintf(stderr, fmt, args);
495 int main(
int argc,
char *argv[]) {
497 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
500 yajl_gen gen = yajl_gen_alloc(NULL);
union stack_entry::@2 val
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...
char * parse_string(const char **walk, bool as_word)
Parses a string (or word, if as_word is true).
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
static const char * get_string(const char *identifier)
static struct CommandResultIR subcommand_output
static struct CommandResultIR command_output
void debuglog(char *fmt,...)
struct tokenptr cmdp_token_ptr
enum stack_entry::@1 type
void command_result_free(CommandResult *result)
Frees a CommandResult.
void errorlog(char *fmt,...)
static void push_string(const char *identifier, char *str)
static void clear_stack(void)
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
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...
A struct that contains useful information about the result of a command as a whole (e...
int main(int argc, char *argv[])
CommandResult * parse_command(const char *input, yajl_gen gen)
Parses and executes the given command.
static struct stack_entry stack[10]
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
static Match current_match
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.
static long get_long(const char *identifier)
static void push_long(const char *identifier, long num)