From de0ca5908fbdfdd14d48ae733b39a323336b62df Mon Sep 17 00:00:00 2001 From: crunchy Date: Sat, 12 Oct 2002 18:31:45 +0000 Subject: add completion for files on the mpio memory cards --- mpiosh/mpiosh.c | 361 +++++++++----------------------------------------------- 1 file changed, 57 insertions(+), 304 deletions(-) (limited to 'mpiosh/mpiosh.c') diff --git a/mpiosh/mpiosh.c b/mpiosh/mpiosh.c index 508bf3d..ff6b9d8 100644 --- a/mpiosh/mpiosh.c +++ b/mpiosh/mpiosh.c @@ -2,7 +2,7 @@ /* * - * $Id: mpiosh.c,v 1.18 2002/09/23 22:38:03 germeier Exp $ + * $Id: mpiosh.c,v 1.19 2002/10/12 18:31:45 crunchy Exp $ * * Author: Andreas Büsching * @@ -26,17 +26,15 @@ * */ #include -#include #include #include -#include -#include - #include "libmpio/debug.h" #include "libmpio/mpio.h" #include "callback.h" +#include "command.h" +#include "readline.h" #include "mpiosh.h" /* structure containing current state */ @@ -52,132 +50,87 @@ const char *PROMPT_EXT = "\033[;1mmpio \033[m "; mpiosh_cmd_t commands[] = { { "debug", "[level|file|on|off] ", - "modify debugging options", - mpiosh_cmd_debug }, + " modify debugging options", + mpiosh_cmd_debug, NULL }, { "ver", NULL, - "version of mpio package", - mpiosh_cmd_version }, + " version of mpio package", + mpiosh_cmd_version, NULL }, { "help", "[]", - "show information about known commands or just about ", - mpiosh_cmd_help }, + " show information about known commands or just about ", + mpiosh_cmd_help, NULL }, { "dir", NULL, - "list content of current memory card", - mpiosh_cmd_dir }, + " list content of current memory card", + mpiosh_cmd_dir, NULL }, { "info", NULL, - "show information about MPIO player", - mpiosh_cmd_info }, + " show information about MPIO player", + mpiosh_cmd_info, NULL }, { "mem", "[i|e]", - "set current memory card. 'i' selects the internal and 'e' " - "selects the external memory card (smart media card)", - mpiosh_cmd_mem }, + " set current memory card. 'i' selects the internal and 'e'\n" + " selects the external memory card (smart media card)", + mpiosh_cmd_mem, NULL }, { "open", NULL, - "open connect to MPIO player", - mpiosh_cmd_open }, + " open connect to MPIO player", + mpiosh_cmd_open, NULL }, { "close", NULL, - "close connect to MPIO player", - mpiosh_cmd_close }, + " close connect to MPIO player", + mpiosh_cmd_close, NULL }, { "quit", " or exit", "exit mpiosh and close the device", - mpiosh_cmd_quit }, + mpiosh_cmd_quit, NULL }, { "exit", NULL, NULL, mpiosh_cmd_quit }, { "get", "", "read from memory card", - mpiosh_cmd_get }, + mpiosh_cmd_get, mpiosh_readline_comp_mpio_file }, { "mget", "", - "read all files matching the regular expression " - "from the selected memory card", - mpiosh_cmd_mget }, + " read all files matching the regular expression\n" + " from the selected memory card", + mpiosh_cmd_mget, mpiosh_readline_comp_mpio_file }, { "put", "", - "write to memory card", - mpiosh_cmd_put }, + " write to memory card", + mpiosh_cmd_put, mpiosh_readline_comp_mpio_file }, { "mput", "", - "write all local files matching the regular expression " - "to the selected memory card", - mpiosh_cmd_mput }, + " write all local files matching the regular expression\n" + " to the selected memory card", + mpiosh_cmd_mput, mpiosh_readline_comp_mpio_file }, { "del", "", - "deletes from memory card", - mpiosh_cmd_del }, + " deletes from memory card", + mpiosh_cmd_del, mpiosh_readline_comp_mpio_file }, { "mdel", "", - "deletes all files matching the regular expression " - "from the selected memory card", - mpiosh_cmd_mdel }, + " deletes all files matching the regular expression\n" + " from the selected memory card", + mpiosh_cmd_mdel, mpiosh_readline_comp_mpio_file }, { "dump", NULL, - "get all files of current memory card", - mpiosh_cmd_dump }, + " get all files of current memory card", + mpiosh_cmd_dump, NULL }, { "free", NULL, - "display amount of available bytes of current memory card", - mpiosh_cmd_free }, + " display amount of available bytes of current memory card", + mpiosh_cmd_free, NULL }, { "format", NULL, - "format current memory card", - mpiosh_cmd_format }, + " format current memory card", + mpiosh_cmd_format, NULL }, /* { "switch", " ", */ /* "switches the order of two files", */ /* mpiosh_cmd_switch }, */ { "ldir", NULL, - "list local directory", - mpiosh_cmd_ldir }, + " list local directory", + mpiosh_cmd_ldir, NULL }, { "lpwd", NULL, - "print current working directory", - mpiosh_cmd_lpwd }, + " print current working directory", + mpiosh_cmd_lpwd, NULL }, { "lcd", NULL, - "change the current working directory", - mpiosh_cmd_lcd }, + " change the current working directory", + mpiosh_cmd_lcd, NULL }, { "lmkdir", NULL, - "create a local directory", - mpiosh_cmd_lmkdir }, + " create a local directory", + mpiosh_cmd_lmkdir, NULL }, { "dump_memory", NULL, - "dump FAT, directory, spare area and the first 0x100 of the selected memory card", - mpiosh_cmd_dump_mem }, - { NULL, NULL, NULL, NULL } + " dump FAT, directory, spare area and the first 0x100 of the\n" + " selected memory card", + mpiosh_cmd_dump_mem, NULL }, + { NULL, NULL, NULL, NULL, NULL } }; -/* readline extensaions */ -void -mpiosh_readline_init(void) -{ - rl_readline_name = "mpio"; - rl_catch_signals = 0; - rl_filename_quote_characters = " "; - rl_attempted_completion_function = mpiosh_readline_completion; - rl_event_hook = mpiosh_readline_cancel; -} - -char * -mpiosh_readline_comp_cmd(const char *text, int state) -{ - static mpiosh_cmd_t *cmd = NULL; - char *cmd_text = NULL; - - if (state == 0) { - cmd = commands; - } - - while (cmd->cmd) { - if ((*text == '\0') || (strstr(cmd->cmd, text) == cmd->cmd)) { - cmd_text = strdup(cmd->cmd); - cmd++; - break; - } - cmd++; - } - - return cmd_text; -} - -char ** -mpiosh_readline_completion(const char *text, int start, int end) -{ - char **matches = (char**)NULL; - - UNUSED(end); - - if (start == 0) - matches = rl_completion_matches(text, mpiosh_readline_comp_cmd); - - return matches; -} - -/* helper functions */ +/* mpiosh core functions */ void mpiosh_init(void) { @@ -186,200 +139,6 @@ mpiosh_init(void) mpiosh.prompt = PROMPT_INT; } -char ** -mpiosh_command_split_line(char *line) -{ - char **cmds, *cmd; - int count = 1; - char *help, *copy = strdup(line); - - help = copy; - - while (*help) - if ((*help++ == ';') && (*(help) != '\0')) count++; - cmds = malloc(sizeof(char *) * (count + 1)); - - cmd = help = copy, count = 0; - while (*cmd == ' ') cmd++; - while (help) { - if (*help == '"') { - help++; - while (*help != '\0' && *help != '"') help++; - } - - if (*help == '\0') break; - if (*help == ';') { - *help++ = '\0'; - if (*help == '\0') break; - cmds[count++] = strdup(cmd); - while (*help == ' ') help++; - cmd = help; - } - help++; - } - if (cmd != '\0') { - cmds[count++] = strdup(cmd); - } - - cmds[count] = NULL; - free(copy); - - return cmds; -} - -mpiosh_cmd_t * -mpiosh_command_find(char *line) -{ - mpiosh_cmd_t *cmd = commands; - - while (cmd->cmd) { - if (strstr(line, cmd->cmd) == line) { - if (line[strlen(cmd->cmd)] == ' ' || - line[strlen(cmd->cmd)] == '\0') - return cmd; - } - cmd++; - } - - return NULL; -} - -void -mpiosh_command_regex_fix(char *argv[]) -{ - char **walk = argv; - char *new_pos, *help; - char buffer[512]; - - while (*walk) { - memset(buffer, '\0', 512); - help = *walk; - new_pos = buffer; - *new_pos++ = '^'; - while (*help != '\0') { - if (*help == '*' && ((help == *walk) || (*(help - 1) != '.'))) { - *new_pos++ = '.'; - *new_pos = *help; - } else if ((*help == '.') && (*(help + 1) != '*')) { - *new_pos++ = '\\'; - *new_pos = *help; - } else if (*help == '?' && ((help == *walk) || (*(help - 1) != '\\'))) { - *new_pos = '.'; - } else { - *new_pos = *help; - } - - help++, new_pos++; - } - *new_pos = '$'; - free(*walk); - *walk = strdup(buffer); - - walk++; - } -} - -char ** -mpiosh_command_get_args(char *line) -{ - char **args; - char *arg_start, *copy, *help, *prev; - int count = 0, i = 0, go = 1, in_quote = 0; - - copy = strdup(line); - arg_start = strchr(copy, ' '); - - if (arg_start == NULL) { - args = malloc(sizeof(char *)); - args[0] = NULL; - return args; - } - - while (*arg_start == ' ') arg_start++; - - help = arg_start; - while (help <= (copy + strlen(copy))) { - if (*help == '"') { - help++;count++; - - while (*help != '\0' && *help != '"') - help++; - help++; - while (*help == ' ') help++; - if (*help == '\0') break; - in_quote = 1; - } else if (((help > arg_start) && (*help == '\0')) || - (*help == ' ' && (*(help + 1) != '\0') && (*(help + 1) != ' '))) { - count++; - in_quote = 0; - help++; - } else - help++; - } - - args = malloc(sizeof(char *) * (count + 1)); - - help = prev = arg_start; - in_quote = 0; - while (go) { - if (*help == '"') in_quote = !in_quote, help++; - if (((*help == ' ') && !in_quote) || (in_quote && *help == '"') || - (*help == '\0')) { - if (*help == '\0') { - go = 0; - if (*prev == '\0') break; - } - - if (*prev == '"') { - if (*(help - 1) == '"') - *(help - 1) = '\0'; - else - *help = '\0'; - - args[i++] = strdup(prev + 1); - } else { - *help = '\0'; - args[i++] = strdup(prev); - } - - if (go) { - help++; - if (in_quote) { - while (*help != '"') help++; - help++; - in_quote = 0; - } else - while (*help == ' ') help++; - prev = help; - } - } else - help++; - } - args[i] = NULL; - - free(copy); - - return args; -} - -void -mpiosh_command_free_args(char **args) -{ - char **arg = args; - - while (*arg) free(*arg++); - - free(args); -} - -int -mpiosh_readline_cancel(void) -{ - if (mpiosh_cancel) rl_done = 1; - - return 0; -} - void mpiosh_signal_handler(int signal) { @@ -387,11 +146,6 @@ mpiosh_signal_handler(int signal) mpiosh_cancel_ack = 0; } -void -mpiosh_noredisplay(void) -{ -} - int main(int argc, char *argv[]) { char * line; @@ -430,8 +184,7 @@ main(int argc, char *argv[]) { if (!isatty(fileno(stdin))) { interactive = 0; mpiosh.prompt = NULL; - rl_redisplay_function = mpiosh_noredisplay; - rl_event_hook = NULL; + mpiosh_readline_pipe(); } if (!mpiosh.dev && interactive) { @@ -462,10 +215,10 @@ main(int argc, char *argv[]) { help = args; if (!interactive) debug("running... '%s'\n", *walk); - cmd->func(args); + cmd->cmd_func(args); mpiosh_command_free_args(args); } else - printf("unknown command: '%s'\n", *walk); + fprintf(stderr, "unknown command: '%s'\n", *walk); /* if ((idx = history_search(line, -1)) != -1) */ /* history_set_pos(idx); */ -- cgit v1.2.3