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"
65 for (
int c = 0; c < 10; c++) {
78 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
79 "in the code, or a new command which contains more than "
80 "10 identified tokens.\n");
86 for (
int c = 0; c < 10; c++) {
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");
108 for (
int c = 0; c < 10; c++) {
119 for (
int c = 0; c < 10; c++) {
131 for (
int c = 0; c < 10; c++) {
154#include "GENERATED_command_call.h"
172 if (
state == INITIAL) {
184 const char *beginning = *walk;
189 for (; **walk !=
'\0' && **walk !=
'"'; (*walk)++)
190 if (**walk ==
'\\' && *(*walk + 1) !=
'\0')
198 while (**walk !=
';' && **walk !=
',' &&
199 **walk !=
'\0' && **walk !=
'\r' &&
206 while (**walk !=
' ' && **walk !=
'\t' &&
207 **walk !=
']' && **walk !=
',' &&
208 **walk !=
';' && **walk !=
'\r' &&
209 **walk !=
'\n' && **walk !=
'\0')
213 if (*walk == beginning)
216 char *str =
scalloc(*walk - beginning + 1, 1);
219 for (inpos = 0, outpos = 0;
220 inpos < (*walk - beginning);
225 if (beginning[inpos] ==
'\\' && (beginning[inpos + 1] ==
'"' || beginning[inpos + 1] ==
'\\'))
227 str[outpos] = beginning[inpos];
241 DLOG(
"COMMAND: *%.4000s*\n", input);
253 const char *walk = input;
254 const size_t len = strlen(input);
266 while ((
size_t)(walk - input) <= len) {
268 while ((*walk ==
' ' || *walk ==
'\t' ||
269 *walk ==
'\r' || *walk ==
'\n') &&
274 token_handled =
false;
275 for (c = 0; c < ptr->
n; c++) {
286 token_handled =
true;
292 if (strcmp(
token->
name,
"number") == 0) {
296 long int num = strtol(walk, &end, 10);
297 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
298 (errno != 0 && num == 0))
312 token_handled =
true;
316 if (strcmp(
token->
name,
"string") == 0 ||
328 token_handled =
true;
334 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
336 token_handled =
true;
343 if (*walk ==
'\0' || *walk ==
';')
352 if (!token_handled) {
356 for (c = 0; c < ptr->
n; c++)
357 tokenlen += strlen(ptr->
array[c].
name) + strlen(
"'', ");
363 char *possible_tokens =
smalloc(tokenlen + 1);
364 char *tokenwalk = possible_tokens;
365 for (c = 0; c < ptr->
n; c++) {
382 if (c < (ptr->
n - 1)) {
388 sasprintf(&errormessage,
"Expected one of these tokens: %s",
390 free(possible_tokens);
394 char *position =
smalloc(len + 1);
395 for (
const char *copywalk = input; *copywalk !=
'\0'; copywalk++)
396 position[(copywalk - input)] = (copywalk >= walk ?
'^' :
' ');
397 position[len] =
'\0';
399 ELOG(
"%s\n", errormessage);
400 ELOG(
"Your command: %s\n", input);
401 ELOG(
" %s\n", position);
419 ystr(
"errorposition");
463 fprintf(stdout,
"# ");
464 vfprintf(stdout, fmt, args);
472 vfprintf(stderr, fmt, args);
476int main(
int argc,
char *argv[]) {
478 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
481 yajl_gen gen = yajl_gen_alloc(NULL);
int main(int argc, char *argv[])
void errorlog(char *fmt,...)
void debuglog(char *fmt,...)
struct tokenptr cmdp_token_ptr
CommandResult * parse_command(const char *input, yajl_gen gen, ipc_client *client)
Parses and executes the given command.
static Match current_match
static void push_long(struct stack *stack, const char *identifier, long num)
static void push_string(struct stack *stack, const char *identifier, char *str)
static const char * get_string(struct stack *stack, const char *identifier)
static long get_long(struct stack *stack, const char *identifier)
char * parse_string(const char **walk, bool as_word)
Parses a string (or word, if as_word is true).
static struct CommandResultIR command_output
static void clear_stack(struct stack *stack)
static struct CommandResultIR subcommand_output
void command_result_free(CommandResult *result)
Frees a CommandResult.
static void next_state(const cmdp_token *token)
char * sstrdup(const char *str)
Safe-wrapper around strdup 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...
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 * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
void cmd_criteria_init(I3_CMD)
Initializes the specified 'Match' data structure and the initial state of commands....
Holds an intermediate representation 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....
union stack_entry::@3 val
enum stack_entry::@2 type
struct stack_entry stack[10]
A "match" is a data structure which acts like a mask or expression to match certain windows or not.