29 #define y(x, ...) (command_output.json_gen != NULL ? yajl_gen_##x(command_output.json_gen, ##__VA_ARGS__) : 0)
30 #define ystr(str) (command_output.json_gen != NULL ? yajl_gen_string(command_output.json_gen, (unsigned char *)str, strlen(str)) : 0)
40 #include "GENERATED_command_enums.h"
57 #include "GENERATED_command_tokens.h"
86 for (
int c = 0; c < 10; c++) {
99 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
100 "in the code, or a new command which contains more than "
101 "10 identified tokens.\n");
107 for (
int c = 0; c < 10; c++) {
121 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
122 "in the code, or a new command which contains more than "
123 "10 identified tokens.\n");
129 for (
int c = 0; c < 10; c++) {
140 for (
int c = 0; c < 10; c++) {
152 for (
int c = 0; c < 10; c++) {
172 #include "GENERATED_command_call.h"
190 if (
state == INITIAL) {
202 const char *beginning = *walk;
207 for (; **walk !=
'\0' && **walk !=
'"'; (*walk)++)
208 if (**walk ==
'\\' && *(*walk + 1) !=
'\0')
216 while (**walk !=
';' && **walk !=
',' &&
217 **walk !=
'\0' && **walk !=
'\r' &&
224 while (**walk !=
' ' && **walk !=
'\t' &&
225 **walk !=
']' && **walk !=
',' &&
226 **walk !=
';' && **walk !=
'\r' &&
227 **walk !=
'\n' && **walk !=
'\0')
231 if (*walk == beginning)
234 char *str =
scalloc(*walk - beginning + 1, 1);
237 for (inpos = 0, outpos = 0;
238 inpos < (*walk - beginning);
243 if (beginning[inpos] ==
'\\' && (beginning[inpos + 1] ==
'"' || beginning[inpos + 1] ==
'\\'))
245 str[outpos] = beginning[inpos];
259 DLOG(
"COMMAND: *%.4000s*\n", input);
271 const char *walk = input;
272 const size_t len = strlen(input);
284 while ((
size_t)(walk - input) <= len) {
286 while ((*walk ==
' ' || *walk ==
'\t' ||
287 *walk ==
'\r' || *walk ==
'\n') &&
292 token_handled =
false;
293 for (c = 0; c < ptr->
n; c++) {
303 token_handled =
true;
309 if (strcmp(
token->
name,
"number") == 0) {
313 long int num = strtol(walk, &end, 10);
314 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
315 (errno != 0 && num == 0))
328 token_handled =
true;
332 if (strcmp(
token->
name,
"string") == 0 ||
343 token_handled =
true;
349 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
351 token_handled =
true;
358 if (*walk ==
'\0' || *walk ==
';')
367 if (!token_handled) {
371 for (c = 0; c < ptr->
n; c++)
372 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
378 char *possible_tokens =
smalloc(tokenlen + 1);
379 char *tokenwalk = possible_tokens;
380 for (c = 0; c < ptr->
n; c++) {
397 if (c < (ptr->
n - 1)) {
403 sasprintf(&errormessage,
"Expected one of these tokens: %s",
405 free(possible_tokens);
409 char *position =
smalloc(len + 1);
410 for (
const char *copywalk = input; *copywalk !=
'\0'; copywalk++)
411 position[(copywalk - input)] = (copywalk >= walk ?
'^' :
' ');
412 position[len] =
'\0';
414 ELOG(
"%s\n", errormessage);
415 ELOG(
"Your command: %s\n", input);
416 ELOG(
" %s\n", position);
434 ystr(
"errorposition");
478 fprintf(stdout,
"# ");
479 vfprintf(stdout, fmt, args);
487 vfprintf(stderr, fmt, args);
491 int main(
int argc,
char *argv[]) {
493 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
496 yajl_gen gen = yajl_gen_alloc(NULL);
static void push_string(const char *identifier, char *str)
static void clear_stack(void)
static void push_long(const char *identifier, long num)
struct tokenptr cmdp_token_ptr
static Match current_match
CommandResult * parse_command(const char *input, yajl_gen gen, ipc_client *client)
Parses and executes the given command.
static const char * get_string(const char *identifier)
static long get_long(const char *identifier)
static struct CommandResultIR command_output
char * parse_string(const char **walk, bool as_word)
Parses a string (or word, if as_word is true).
static struct stack_entry stack[10]
static struct CommandResultIR subcommand_output
void command_result_free(CommandResult *result)
Frees a CommandResult.
static void next_state(const cmdp_token *token)
void errorlog(char *fmt,...)
void debuglog(char *fmt,...)
int main(int argc, char *argv[])
void cmd_criteria_init(I3_CMD)
Initializes the specified 'Match' data structure and the initial state of commands....
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
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 * 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...
union stack_entry::@2 val
enum stack_entry::@1 type
Holds an intermediate represenation of the result of a call to any command.
A struct that contains useful information about the result of a command as a whole (e....
A "match" is a data structure which acts like a mask or expression to match certain windows or not.