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++) {
79 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
80 "in the code, or a new command which contains more than "
81 "10 identified tokens.\n");
87 for (
int c = 0; c < 10; c++) {
101 fprintf(stderr,
"BUG: commands_parser stack full. This means either a bug "
102 "in the code, or a new command which contains more than "
103 "10 identified tokens.\n");
109 for (
int c = 0; c < 10; c++) {
122 for (
int c = 0; c < 10; c++) {
136 for (
int c = 0; c < 10; c++) {
160#include "GENERATED_command_call.h"
179 if (
state == INITIAL) {
191 const char *beginning = *walk;
196 for (; **walk !=
'\0' && **walk !=
'"'; (*walk)++) {
197 if (**walk ==
'\\' && *(*walk + 1) !=
'\0') {
207 while (**walk !=
';' && **walk !=
',' &&
208 **walk !=
'\0' && **walk !=
'\r' &&
216 while (**walk !=
' ' && **walk !=
'\t' &&
217 **walk !=
']' && **walk !=
',' &&
218 **walk !=
';' && **walk !=
'\r' &&
219 **walk !=
'\n' && **walk !=
'\0') {
224 if (*walk == beginning) {
228 char *str =
scalloc(*walk - beginning + 1, 1);
231 for (inpos = 0, outpos = 0;
232 inpos < (*walk - beginning);
237 if (beginning[inpos] ==
'\\' && (beginning[inpos + 1] ==
'"' || beginning[inpos + 1] ==
'\\')) {
240 str[outpos] = beginning[inpos];
254 DLOG(
"COMMAND: *%.4000s*\n", input);
266 const char *walk = input;
267 const size_t len = strlen(input);
279 while ((
size_t)(walk - input) <= len) {
281 while ((*walk ==
' ' || *walk ==
'\t' ||
282 *walk ==
'\r' || *walk ==
'\n') &&
288 token_handled =
false;
289 for (c = 0; c < ptr->
n; c++) {
300 token_handled =
true;
306 if (strcmp(
token->
name,
"number") == 0) {
310 long int num = strtol(walk, &end, 10);
311 if ((errno == ERANGE && (num == LONG_MIN || num == LONG_MAX)) ||
312 (errno != 0 && num == 0)) {
328 token_handled =
true;
332 if (strcmp(
token->
name,
"string") == 0 ||
345 token_handled =
true;
351 if (*walk ==
'\0' || *walk ==
',' || *walk ==
';') {
353 token_handled =
true;
360 if (*walk ==
'\0' || *walk ==
';') {
370 if (!token_handled) {
374 for (c = 0; c < ptr->
n; c++) {
375 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++) {
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 ?
'^' :
' ');
417 position[len] =
'\0';
419 ELOG(
"%s\n", errormessage);
420 ELOG(
"Your command: %s\n", input);
421 ELOG(
" %s\n", position);
439 ystr(
"errorposition");
459 if (result == NULL) {
484 fprintf(stdout,
"# ");
485 vfprintf(stdout, fmt, args);
493 vfprintf(stderr, fmt, args);
497int main(
int argc,
char *argv[]) {
499 fprintf(stderr,
"Syntax: %s <command>\n", argv[0]);
502 yajl_gen gen = yajl_gen_alloc(NULL);
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)
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 * 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...
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.