void command_free(command_t *cmd) { int i; // It's OK to command_free(NULL). if (!cmd) return; /* Your code here. */ i=0; while(cmd->argv[i]!=NULL&&i<MAXTOKENS+1){ free(cmd->argv[i++]); } i=0; while(i<3){ if (cmd->redirect_filename[i]!=NULL) { free(cmd->redirect_filename[i]); } i++; } if(cmd->subshell!=NULL){ command_free(cmd->subshell); } if(cmd->next!=NULL){ command_free(cmd->next); } free(cmd); }
static void command_parse_args(command_t *self, int argc, char **argv) { int literal = 0; int i, j; for (i = 1; i < argc; ++i) { const char *arg = argv[i]; for (j = 0; j < self->option_count; ++j) { command_option_t *option = &self->options[j]; // match flag if (!strcmp(arg, option->small) || !strcmp(arg, option->large)) { self->arg = NULL; // required if (option->required_arg) { arg = argv[++i]; if (!arg || '-' == arg[0]) { fprintf(stderr, "%s %s argument required\n", option->large, option->argname); command_free(self); exit(1); } self->arg = arg; } // optional if (option->optional_arg) { if (argv[i + 1] && '-' != argv[i + 1][0]) { self->arg = argv[++i]; } } // invoke callback option->cb(self); goto match; } } // -- if ('-' == arg[0] && '-' == arg[1] && 0 == arg[2]) { literal = 1; goto match; } // unrecognized if ('-' == arg[0] && !literal) { fprintf(stderr, "unrecognized flag %s\n", arg); command_free(self); exit(1); } int n = self->argc++; if (n == COMMANDER_MAX_ARGS) { command_free(self); error("Maximum number of arguments exceeded"); } self->argv[n] = (char *) arg; match:; } }
static void execute(char *buf) { struct command *cmd = command_new(); char *arg; char *rest; if (!cmd) { fprintf(stderr, "Memory exhausted\n"); return; } while (*buf && *buf == ' ') buf++; if (!*buf) { command_free(cmd); return; } arg = strtok_r(buf, " ", &rest); while (arg) { if (command_append(cmd, arg) < 0) { command_free(cmd); fprintf(stderr, "Memory exhausted\n"); return; } while (*rest && *rest == ' ') rest++; arg = strtok_r(NULL, " ", &rest); } command_append(cmd, NULL); command_execute(cmd); command_free(cmd); }
enum SamError sam_cmd(Vis *vis, const char *s) { enum SamError err = SAM_ERR_OK; if (!s) return err; Command *cmd = sam_parse(vis, s, &err); if (!cmd) { if (err == SAM_ERR_OK) err = SAM_ERR_MEMORY; return err; } Filerange range = text_range_empty(); sam_execute(vis, vis->win, cmd, NULL, &range); if (vis->win) { bool completed = true; for (Cursor *c = view_cursors(vis->win->view); c; c = view_cursors_next(c)) { Filerange sel = view_cursors_selection_get(c); if (text_range_valid(&sel)) { completed = false; break; } } vis_mode_switch(vis, completed ? VIS_MODE_NORMAL : VIS_MODE_VISUAL); } command_free(cmd); return err; }
int Fill3f(Display *display, int r, int g, int b) { /* There's probably more error checking to do here since * changing the colorMode actually defines the max values * acceptable here. Default is 1.0 so we'll start with that... */ if (r > 1.0 || g > 1.0 || b > 1.0) { return -1; } Command *cmd = NULL; Socket *socket = NULL; socket = display->socket; /* cmd = command_format("FI_F %f %f %f", r, g, b); */ cmd = command_format_json("FI_F", "%f %f %f", r, g, b); if (cmd == NULL) return -1; if (command_send(cmd, socket) != 0) { command_free(cmd); return -1; } return 0; }
int main(int argc, char **argv) { int rc = 1; opts.spaces = 2; command_t cmd; command_init(&cmd, argv[0], "1.0.0"); command_option(&cmd , "-v" , "--verbose" , "enable verbose stuff" , set_verbose); command_option(&cmd , "-s" , "--spaces [count]" , "optional number of spaces (defaults to 2)" , set_spaces); command_parse(&cmd, argc, argv); for (int i = 0; i < cmd.argc; ++i) { if (-1 == convert_file(cmd.argv[i])) goto cleanup; } rc = 0; cleanup: command_free(&cmd); return rc; }
/* * This is a general purpose way to call jquery from C. * * Just a front for $("#id").css("name", "value"); */ int TextAreaSetCss(Display *display, const char *id, const char *name, const char *value) { Command *cmd = NULL; Socket *socket = NULL; socket = display->socket; if (id == NULL) return -1; if (strlen(id) == 0 || strlen(name) == 0 || strlen(value) == 0) return 0; cmd = command_format_json("TXT_AREA_CSS", "\"%s\" \"%s\" \"%s\"", id, name, value); if (cmd == NULL) return -1; if (command_send(cmd, socket) != 0) { command_free(cmd); return -1; } return 0; }
/* Create a text area over the canvas to receive text directly */ int CreateTextArea(Display *display, const char *id, int x, int y, int width, int height, int readonly) { Command *cmd = NULL; Socket *socket = NULL; socket = display->socket; if (id == NULL) return -1; if (strlen(id) == 0) return 0; if (readonly != TRUE && readonly != FALSE) return 0; cmd = command_format_json("NEW_TXT_AREA", "\"%s\" %d %d %d %d %d", id, x, y, width, height, readonly); if (cmd == NULL) return -1; if (command_send(cmd, socket) != 0) { command_free(cmd); return -1; } return 0; }
int AppendTextArea(Display *display, const char *target, const char *text) { Command *cmd = NULL; Socket *socket = NULL; socket = display->socket; if (target == NULL || text == NULL) return -1; if (strlen(target) == 0) return 0; if (strlen(text) == 0) return 0; cmd = command_format_json("APPEND", "\"%s\" \"%s\"", target, text); if (cmd == NULL) return -1; if (command_send(cmd, socket) != 0) { command_free(cmd); return -1; } return 0; }
static struct command *command_new(struct command_context *cmd_ctx, struct command *parent, const struct command_registration *cr) { assert(cr->name); struct command *c = calloc(1, sizeof(struct command)); if (NULL == c) return NULL; c->name = strdup(cr->name); if (cr->help) c->help = strdup(cr->help); if (cr->usage) c->usage = strdup(cr->usage); if (!c->name || (cr->help && !c->help) || (cr->usage && !c->usage)) goto command_new_error; c->parent = parent; c->handler = cr->handler; c->jim_handler = cr->jim_handler; c->jim_handler_data = cr->jim_handler_data; c->mode = cr->mode; command_add_child(command_list_for_parent(cmd_ctx, parent), c); return c; command_new_error: command_free(c); return NULL; }
/* TODO: remove this */ int SendText(Display *display, int x, int y, char *text) { Command *cmd = NULL; Socket *socket = NULL; int textlen = 0; socket = display->socket; if (text == NULL) return -1; textlen = strlen(text); if (textlen == 0) return 0; //command_format("TXT %d %d %s", x, y, text); cmd = command_format_json("TXT", "\"%s\" %d %d", text, x, y); if (cmd == NULL) return -1; if (command_send(cmd, socket) != 0) { command_free(cmd); return -1; } return 0; }
static gint sieve_pop_send_queue(SieveSession *session) { SieveCommand *cmd; GSList *send_queue = session->send_queue; if (session->current_cmd) { command_free(session->current_cmd); session->current_cmd = NULL; } if (!send_queue) return SE_OK; cmd = (SieveCommand *)send_queue->data; session->send_queue = g_slist_next(send_queue); g_slist_free_1(send_queue); log_send(session, cmd); session->state = cmd->next_state; session->current_cmd = cmd; if (session_send_msg(SESSION(session), SESSION_SEND, cmd->msg) < 0) return SE_ERROR; return SE_OK; }
void help(command_t* self) { (void)(self); std::cout << " _ __ _ __ _ ____ \n" "| |\\ | ( (` | |\\ | / /\\ | |_/ | |_ \n" "|_| \\| _)_) |_| \\| /_/--\\ |_| \\ |_|__ \n" "v" VERSION " (built " DATE ")\n" "\n" "nsnake is the classical snake game on the\n" "terminal.\n" "It's settings and scores are stored at:\n" " ~/.local/share/nsnake/\n" "\n" "Usage:\n" " nsnake [options]\n" "\n" " -h, --help Show this message\n" " -v, --version Show game version and contact info\n" "\n" "Thank you for playing this game :)\n"; command_free(self); exit(EXIT_SUCCESS); }
int SendKeyboardCallbackMsg(Display *display, char* type, char* list) { Command *cmd = NULL; Socket *socket = NULL; int i,size; char *str, args[10]; socket = display->socket; if (list == NULL) { cmd = command_format_json(type, "\"%s\"", "ALL"); } else { size = strlen(list); str = calloc(size*5, sizeof(char)); for (i = 0; i < size; i ++) { if (i != size -1) sprintf(args, "%hu,", list[i]); else sprintf(args, "%hu", list[i]); strcat(str, args); } cmd = command_format_json(type, "%s", str); free(str); } if (cmd == NULL) return -1; if (command_send(cmd, socket) != 0) { command_free(cmd); return -1; } return 0; }
void input_driver_deinit_command(void) { #ifdef HAVE_COMMAND if (input_driver_command) command_free(input_driver_command); input_driver_command = NULL; #endif }
int main(int argc, char *argv[]) { int quiet = 0; char input[BUFSIZ]; int r = 0; // Check for '-q' option: be quiet -- print no prompts if (argc > 1 && strcmp(argv[1], "-q") == 0) quiet = 1; signal(SIGCHLD,signal_handler); while (!feof(stdin)) { parsestate_t parsestate; command_t *cmdlist; // Print the prompt if (!quiet) { printf("prog1$ "); fflush(stdout); } // Read a string, checking for error or EOF if (fgets(input, BUFSIZ, stdin) == NULL) { if (ferror(stdin)) // This function prints a description of the // error, preceded by 'cs111_fall07: '. perror("prog1"); break; } // TODO: invoke some function(s) in cmdline.c for parsing the read string. parse_init(&parsestate,input); cmdlist=command_line_parse(&parsestate,0); if (!cmdlist) { printf("Syntax error\n"); continue; } // print the command list if (!quiet) { // TODO: invoke some function(s) in cmdline.c for printing out the command for debugging. command_print(cmdlist,1); // why do we need to do this? fflush(stdout); } // and run it! int not_impotant=0; waitpid(-1,¬_impotant,WNOHANG);//kill zombies if (cmdlist) command_line_exec(cmdlist); if (cmdlist) command_free(cmdlist); } return 0; }
/* Dispatch actions according to program flags (NOT commands or * command options). */ static void cli_context_flag_dispatch (cli_context_t *ctx, gint in_argc, gchar **in_argv) { command_t *cmd; gboolean check; GOptionEntry flagdefs[] = { { "help", 'h', 0, G_OPTION_ARG_NONE, NULL, _("Display this help and exit."), NULL }, { "version", 'v', 0, G_OPTION_ARG_NONE, NULL, _("Output version information and exit."), NULL }, { NULL } }; /* Include one command token as a workaround for the bug that * the option parser does not parse commands starting with a * flag properly (e.g. "-p foo arg1"). Will be skipped by the * command utils. */ cmd = command_new (flagdefs, in_argc + 1, in_argv - 1); if (!cmd) { /* An error message has already been printed, so we just return. */ return; } if (command_flag_boolean_get (cmd, "help", &check) && check) { if (command_arg_count (cmd) >= 1) { help_command (ctx, cli_context_command_names (ctx), command_argv_get (cmd), command_arg_count (cmd), CMD_TYPE_COMMAND); } else { /* FIXME: explain -h and -v flags here (reuse help_command code?) */ g_printf (_("usage: xmms2 [<command> [args]]\n\n")); g_printf (_("XMMS2 CLI, the awesome command-line XMMS2 client from the future, " "v" XMMS_VERSION ".\n\n")); g_printf (_("If given a command, runs it inline and exit.\n")); g_printf (_("If not, enters a shell-like interface to execute commands.\n\n")); g_printf (_("Type 'help <command>' for detailed help about a command.\n")); } } else if (command_flag_boolean_get (cmd, "version", &check) && check) { g_printf (_("XMMS2 CLI version " XMMS_VERSION "\n")); g_printf (_("Copyright (C) 2008-2014 XMMS2 Team\n")); g_printf (_("This is free software; see the source for copying conditions.\n")); g_printf (_("There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n" "PARTICULAR PURPOSE.\n")); /* FIXME: compiled against? use RL_READLINE_VERSION? */ g_printf (_(" Using readline version %s\n"), rl_library_version); } else { /* Call help to print the "no such command" error */ /* FIXME: Could be a more helpful "invalid flag"?*/ help_command (ctx, cli_context_command_names (ctx), in_argv, in_argc, CMD_TYPE_COMMAND); } command_free (cmd); }
/* This is partly controlled by the BUFSIZE (currently 128) in command.c and partly due to JSON overhead. */ int OverwriteTextArea(Display *display, const char *target, const char *text) { Command *cmd = NULL; Socket *socket = NULL; int i,j,count, len; char *tmp; socket = display->socket; if (target == NULL || text == NULL || strchr(target, '\n') != NULL) return -1; if (strlen(target) == 0) return 0; len = strlen(text); if (len == 0) return 0; if (strchr(text, '\n') || strchr(text, '\r')) { /* at least one newline to deal with */ count = 0; j = 0; for(i = 0; i < len; i++) { if (text[i] == '\n' || text[i] == '\r') count++; } tmp = calloc(len+count, sizeof(char)); for(i = 0; i < len; i++) { if (text[i] == '\n') { tmp[j++] = '\\'; tmp[j] = 'n'; } else if (text[i] == '\r') { tmp[j++] = '\\'; tmp[j] = 'r'; } else { tmp[j] = text[i]; } j++; } cmd = command_format_json("OVERWRITE", "\"%s\" \"%s\"", target, tmp); free(tmp); } else { cmd = command_format_json("OVERWRITE", "\"%s\" \"%s\"", target, text); } if (cmd == NULL) return -1; if (command_send(cmd, socket) != 0) { command_free(cmd); return -1; } return 0; }
static void cli_context_command_dispatch (cli_context_t *ctx, gint in_argc, gchar **in_argv) { command_action_t *action; command_trie_match_type_t match; gint argc; gchar *tmp_argv[in_argc+1]; gchar **argv; /* The arguments will be updated by command_trie_find and * init_context_from_args, so we make a copy. */ memcpy (tmp_argv, in_argv, in_argc * sizeof (gchar *)); tmp_argv[in_argc] = NULL; argc = in_argc; argv = tmp_argv; /* This updates argv and argc so that they start at the first non-command * token. */ match = cli_context_find_command (ctx, &argv, &argc, &action); if (match == COMMAND_TRIE_MATCH_ACTION) { gboolean help; gboolean need_io; command_t *cmd; /* Include one command token as a workaround for the bug that * the option parser does not parse commands starting with a * flag properly (e.g. "-p foo arg1"). Will be skipped by the * command utils. */ cmd = command_new (action->argdefs, argc + 1, argv - 1); if (cmd) { if (command_flag_boolean_get (cmd, "help", &help) && help) { /* Help flag passed, bypass action and show help */ /* FIXME(g): select aliasnames list if it's an alias */ help_command (ctx, cli_context_command_names (ctx), in_argv, in_argc, CMD_TYPE_COMMAND); } else if (cli_context_command_runnable (ctx, action)) { /* All fine, run the command */ command_name_set (cmd, action->name); cli_context_loop_suspend (ctx); need_io = action->callback (ctx, cmd); if (!need_io) { cli_context_loop_resume (ctx); } } command_free (cmd); } } else { /* Call help to print the "no such command" error */ help_command (ctx, cli_context_command_names (ctx), in_argv, in_argc, CMD_TYPE_COMMAND); } }
/* Function: test_redirect Test commands with io redirect */ void test_redirect() { #ifdef DEBUG_TEST printf("TEST: Checking I/O redirecting\n"); #endif cmd = command_parse("a < b > c\n"); assert(cmd != NULL); assert(cmd->token >= 1); assert(strcmp(cmd->argv[0], "a") == 0); assert(strcmp(cmd->infile, "b") == 0); assert(strcmp(cmd->outfile, "c") == 0); assert(cmd->fdmode == (O_RDWR|O_TRUNC)); command_free(cmd); cmd = command_parse("a<b>> c\n"); assert(cmd != NULL); assert(cmd->token >= 1); assert(strcmp(cmd->argv[0], "a") == 0); assert(strcmp(cmd->infile, "b") == 0); assert(strcmp(cmd->outfile, "c") == 0); assert(cmd->fdmode == (O_RDWR|O_APPEND)); command_free(cmd); cmd = command_parse("a<b>c\n"); assert(cmd != NULL); assert(cmd->token >= 1); assert(strcmp(cmd->argv[0], "a") == 0); assert(strcmp(cmd->infile, "b") == 0); assert(strcmp(cmd->outfile, "c") == 0); assert(cmd->fdmode == (O_RDWR|O_TRUNC)); command_free(cmd); cmd = command_parse("a <b >>c\n"); assert(cmd != NULL); assert(cmd->token >= 1); assert(strcmp(cmd->argv[0], "a") == 0); assert(strcmp(cmd->infile, "b") == 0); assert(strcmp(cmd->outfile, "c") == 0); assert(cmd->fdmode == (O_RDWR|O_APPEND)); command_free(cmd); }
void Arguments::parse(int argc, char* argv[]) { // commander internal data structure command_t cmd; command_init(&cmd, argv[0], VERSION); command_option(&cmd, "-v", "--version", "Show game version and build date", version); command_option(&cmd, "-h", "--help", "Show instructions", help); command_parse(&cmd, argc, argv); command_free(&cmd); }
int xlib_free(int ret) { #ifdef module_sorter sorter_free(); #endif #ifdef module_cursor cursor_free(); #endif render_free(); command_free(); XDestroyWindow(dpy, topWin); return ret; }
/* Function: test_pipe Test command with pipes */ void test_pipe() { #ifdef DEBUG_TEST printf("TEST: Checking pipes\n"); #endif cmd = command_parse("a | b | c \n"); assert(cmd != NULL); assert(strcmp(cmd->argv[0], "a") == 0); assert(strcmp(cmd->next->argv[0], "b") == 0); assert(strcmp(cmd->next->next->argv[0], "c") == 0); command_free(cmd); cmd = command_parse("a| b |c \n"); assert(cmd != NULL); assert(strcmp(cmd->argv[0], "a") == 0); assert(strcmp(cmd->next->argv[0], "b") == 0); assert(strcmp(cmd->next->next->argv[0], "c") == 0); command_free(cmd); cmd = command_parse("a|b|c \n"); assert(cmd != NULL); assert(strcmp(cmd->argv[0], "a") == 0); assert(strcmp(cmd->next->argv[0], "b") == 0); assert(strcmp(cmd->next->next->argv[0], "c") == 0); command_free(cmd); cmd = command_parse("a<in|b|c > out &\n"); assert(cmd != NULL); assert(strcmp(cmd->argv[0], "a") == 0); assert(strcmp(cmd->infile, "in") == 0); assert(strcmp(cmd->next->argv[0], "b") == 0); assert(strcmp(cmd->next->next->argv[0], "c") == 0); assert(strcmp(cmd->next->next->outfile, "out") == 0); assert(cmd->background == TRUE); assert(cmd->next->background == TRUE); assert(cmd->next->next->background == TRUE); command_free(cmd); }
int main(int argc, char *argv[]) { command_t program; command_init(&program, "clib-search", CLIB_VERSION); program.usage = "[options] [query ...]"; command_parse(&program, argc, argv); for (int i = 0; i < program.argc; i++) case_lower(program.argv[i]); char *html = wiki_html_cache(); if (NULL == html) { command_free(&program); fprintf(stderr, "Failed to fetch wiki HTML\n"); return 1; } list_t *pkgs = wiki_registry_parse(html); free(html); list_node_t *node; list_iterator_t *it = list_iterator_new(pkgs, LIST_HEAD); printf("\n"); while ((node = list_iterator_next(it))) { wiki_package_t *pkg = (wiki_package_t *) node->val; if (matches(program.argc, program.argv, pkg)) { cc_fprintf(CC_FG_DARK_CYAN, stdout, " %s\n", pkg->repo); printf(" url: "); cc_fprintf(CC_FG_DARK_GRAY, stdout, "%s\n", pkg->href); printf(" desc: "); cc_fprintf(CC_FG_DARK_GRAY, stdout, "%s\n", pkg->description); printf("\n"); } wiki_package_free(pkg); } list_iterator_destroy(it); list_destroy(pkgs); command_free(&program); return 0; }
/* Function: test_standard Several standard command */ void test_standard() { #ifdef DEBUG_TEST printf("TEST: Checking standard command\n"); #endif cmd = command_parse("cat testfile1 testfile2\n"); assert(cmd != NULL); assert(cmd->token >= 3); assert(strcmp(cmd->argv[0], "cat") == 0); assert(strcmp(cmd->argv[1], "testfile1") == 0); assert(strcmp(cmd->argv[2], "testfile2") == 0); command_free(cmd); cmd = command_parse("echo line&\n"); assert(cmd != NULL); assert(cmd->token >= 2); assert(cmd->background == TRUE); assert(strcmp(cmd->argv[0], "echo") == 0); assert(strcmp(cmd->argv[1], "line") == 0); command_free(cmd); }
static Command *sam_parse(Vis *vis, const char *cmd, enum SamError *err) { const char **s = &cmd; Command *c = command_parse(vis, s, 0, err); if (!c) return NULL; Command *sel = command_new("s"); if (!sel) { command_free(c); return NULL; } sel->cmd = c; sel->cmddef = &cmddef_select; return sel; }
void eval_line(const char* s) { int type; char* token; // Your code here! // build the command command* c = command_alloc(); while ((s = parse_shell_token(s, &type, &token)) != NULL) command_append_arg(c, token); // execute it if (c->argc) run_list(c); command_free(c); }
/* Function: direct_input */ void direct_input() { #ifdef DEBUG_TEST printf("TEST: Read and parse command from STDIN\n"); #endif char buffer[CMD_MAX]; while(TRUE) { printf("# "); fgets(buffer, CMD_MAX, stdin); cmd = command_parse(buffer); command_print(cmd); command_free(cmd); } }
int unregister_all_commands(struct command_context *context, struct command *parent) { if (context == NULL) return ERROR_OK; struct command **head = command_list_for_parent(context, parent); while (NULL != *head) { struct command *tmp = *head; *head = tmp->next; command_free(tmp); } return ERROR_OK; }
static void command_free(struct command *c) { /** @todo if command has a handler, unregister its jim command! */ while (NULL != c->children) { struct command *tmp = c->children; c->children = tmp->next; command_free(tmp); } free(c->name); free(c->help); free(c->usage); free(c); }