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++) {
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++) {
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++) {
147 for (
int c = 0; c < 10; c++) {
159 for (
int c = 0; c < 10; c++) {
179 #include "GENERATED_command_call.h" 197 if (
state == INITIAL) {
209 const char *beginning = *walk;
214 for (; **walk !=
'\0' && **walk !=
'"'; (*walk)++)
215 if (**walk ==
'\\' && *(*walk + 1) !=
'\0')
223 while (**walk !=
';' && **walk !=
',' &&
224 **walk !=
'\0' && **walk !=
'\r' &&
231 while (**walk !=
' ' && **walk !=
'\t' &&
232 **walk !=
']' && **walk !=
',' &&
233 **walk !=
';' && **walk !=
'\r' &&
234 **walk !=
'\n' && **walk !=
'\0')
238 if (*walk == beginning)
241 char *str =
scalloc(*walk - beginning + 1, 1);
244 for (inpos = 0, outpos = 0;
245 inpos < (*walk - beginning);
250 if (beginning[inpos] ==
'\\' && (beginning[inpos + 1] ==
'"' || beginning[inpos + 1] ==
'\\'))
252 str[outpos] = beginning[inpos];
266 DLOG(
"COMMAND: *%s*\n", input);
278 const char *walk = input;
279 const size_t len = strlen(input);
291 while ((
size_t)(walk - input) <= len) {
293 while ((*walk ==
' ' || *walk ==
'\t' ||
294 *walk ==
'\r' || *walk ==
'\n') &&
299 token_handled =
false;
300 for (c = 0; c < ptr->
n; c++) {
310 token_handled =
true;
316 if (strcmp(
token->
name,
"number") == 0) {
320 long int num = strtol(walk, &end, 10);
321 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
322 (errno != 0 && num == 0))
335 token_handled =
true;
339 if (strcmp(
token->
name,
"string") == 0 ||
350 token_handled =
true;
356 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
358 token_handled =
true;
365 if (*walk ==
'\0' || *walk ==
';')
374 if (!token_handled) {
378 for (c = 0; c < ptr->
n; c++)
379 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
385 char *possible_tokens =
smalloc(tokenlen + 1);
386 char *tokenwalk = possible_tokens;
387 for (c = 0; c < ptr->
n; c++) {
404 if (c < (ptr->
n - 1)) {
410 sasprintf(&errormessage,
"Expected one of these tokens: %s",
412 free(possible_tokens);
416 char *position =
smalloc(len + 1);
417 for (
const char *copywalk = input; *copywalk !=
'\0'; copywalk++)
418 position[(copywalk - input)] = (copywalk >= walk ?
'^' :
' ');
419 position[len] =
'\0';
421 ELOG(
"%s\n", errormessage);
422 ELOG(
"Your command: %s\n", input);
423 ELOG(
" %s\n", position);
441 ystr(
"errorposition");
485 fprintf(stdout,
"# ");
486 vfprintf(stdout, fmt, args);
494 vfprintf(stderr, fmt, args);
498 int main(
int argc,
char *argv[]) {
500 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
503 yajl_gen gen = yajl_gen_alloc(NULL);
char * parse_string(const char **walk, bool as_word)
Parses a string (or word, if as_word is true).
CommandResult * parse_command(const char *input, yajl_gen gen, ipc_client *client)
Parses and executes the given command.
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...
static const char * get_string(const char *identifier)
Holds an intermediate represenation of the result of a call to any command.
static struct CommandResultIR subcommand_output
static struct CommandResultIR command_output
void debuglog(char *fmt,...)
struct tokenptr cmdp_token_ptr
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...
void command_result_free(CommandResult *result)
Frees a CommandResult.
enum stack_entry::@1 type
void errorlog(char *fmt,...)
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 push_string(const char *identifier, char *str)
static void clear_stack(void)
A struct that contains useful information about the result of a command as a whole (e...
int main(int argc, char *argv[])
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 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 struct stack_entry stack[10]
static Match current_match
static long get_long(const char *identifier)
static void push_long(const char *identifier, long num)