static int run_wait_event_process(shell_t *shell, int fd, char **pbuf) { periph_item_t *item = periph_fd_retrieve(run_periph, fd); char *buf; /* Should never occur... */ if ( item == NULL ) { shell_error(shell, "!! PANIC !! Unable to retrieve peripheral from fd=%d\n", fd); return -1; } #ifdef DEBUG_SELECT if ( debug_flag ) { /* For speedup in non-debug mode */ debug("Something to read from '%s' (fd=%d)\n", item->id, fd); } #endif /* Fetch data from peripheral: buf will be set to NULL if no complete message is available */ run_prompt_cr(shell); if ( result_dump_periph(item, &buf) == -1 ) return -1; run_prompt(shell); /* Check awaited patterns if a complete message arrived */ if ( buf != NULL ) { trig_update(item, buf); } if ( pbuf != NULL ) *pbuf = buf; return 0; }
static int run_exec_timeout(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; /* Check arguments */ if ( argc > 3 ) { shell_error(shell, "%s: too many arguments\n", argv[0]); shell_std_help(shell, argv[0]); return -1; } /* Setup new timeout value if supplied */ if ( argc > 1 ) { if ( timer_value(shell, argv[1], argv[2], &run_timeout) ) return -1; } /* Write info to result file */ if ( run_timeout > 0 ) result_dump_engine(tag, "%ld.%03ld s", run_timeout / 1000, run_timeout % 1000); else result_dump_engine(tag, "No timeout defined"); return 0; }
static int run_exec_periph(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; periph_item_t *item; char *info; /* User asked to show the whole peripheral list */ if ( argc < 2 ) { periph_info(run_periph, result_header_engine(tag), (periph_info_method_t *) result_puts); return 0; } /* User asked to add or show a peripheral */ item = periph_retrieve(run_periph, strupper(argv[1])); if ( argc < 3 ) { if ( item != NULL ) result_dump_engine(tag, periph_item_info(item)); return 0; } /* Check arguments */ if ( argc > 5 ) { shell_error(shell, "%s: Too many arguments\n", argv[0]); shell_std_help(shell, argv[0]); return -1; } /* User asked to add a new peripheral: check it is not already declared */ if ( item != NULL ) { debug("Redeclaring peripheral: %s\n", argv[1]); /* Peripheral already declared: close it and remove it */ run_exec_close_item(item, TAG_CLOSE); if ( (item = periph_item_init(argv+1, item)) == NULL ) return -1; } else { debug("New peripheral: %s\n", argv[1]); /* Create new peripheral in list */ if ( (item = periph_item_init(argv+1, NULL)) == NULL ) return -1; periph_item_closed_set(item, (periph_item_closed_t *) run_wait_event_disconnect, (void *) shell); periph_new(run_periph, item); } /* Display new peripheral */ info = periph_item_info(item); result_dump_engine(tag, info); /* Also dump wait synchronization output */ if ( run_wait_output != NULL ) { fputs(info, run_wait_output); fputs(_LF, run_wait_output); } return 0; }
int my_execvpe(char* full, char* args[]) { struct stat access; if (stat(full, &access) == 0) if(execvpe(full, args, NULL) == -1) shell_error("execvpe"); return 0; }
static int run_exec_unknown(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { /* OS-shell command ? */ if ( cmd_argv->argv[0][0] == '!' ) { /* Check availability */ if ( ! shell->interactive ) { shell_error(shell, "Cannot invoke OS-shell command in non-interactive mode\n"); return -1; } return shell_std_system(shell, cmd_argv); } shell_error(shell, "Unknown command '%s'\n", cmd_argv->argv[0]); shell_std_help(shell, NULL); return -1; }
void prepare_to_execute(char* args[], char* envp[], int in, int out) { int pid = fork(); switch(pid) { case -1: shell_error("fork()"); break; case 0: open_streams(in, out); execute(args, envp); break; default: close_streams(in, out); if (wait(0) == -1) shell_error("wait()"); } }
static int run_exec_verdict(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; char *verdict; int i; /* Check arguments */ if ( (argc < 2) || (argc > 3) ) { shell_error(shell, "%s: too %s arguments\n", argv[0], (argc < 2) ? "few":"many"); shell_std_help(shell, argv[0]); return -1; } /* Check CASE/DONE nesting */ if ( run_exec_case_running() ) { shell_error(shell, "%s: Test case in progress (%s)\n", argv[0], run_exec_case_buf->buf); return -1; } /* Check result lexical correctness */ strupper(argv[1]); verdict = NULL; for (i = 0; (verdict == NULL) && (run_exec_verdict_tab[i] != NULL); i++) { verdict = (char *) run_exec_verdict_tab[i]; if ( strncmp(verdict, argv[1], strlen(argv[1])) != 0 ) verdict = NULL; } if ( verdict == NULL ) { shell_error(shell, "%s: Illegal verdict result '%s'\n", argv[0], argv[1]); return -1; } /* Dump message */ if ( argc > 2 ) result_dump_engine(tag, "%s %s", verdict, argv[2]); else result_dump_engine(tag, "%s", verdict); return 0; }
static int run_wait_input(shell_t *shell, int input_fd) { fd_set rd; int max; int fd; int count; max = 0; do { /* Wait for incoming events */ max = periph_fd_set(run_periph, &rd); /* Wait for incoming commands */ FD_SET(input_fd, &rd); if ( input_fd >= max ) max = input_fd + 1; count = select(max, &rd, NULL, NULL, NULL); if ( (count == -1) && (errno != EINTR) ) { shell_error(shell, "Select i/o error: %s\n", strerror(errno)); return -1; } } while ( count == -1 ); if ( count > 0 ) { #ifdef DEBUG_SELECT if ( debug_flag ) { /* For speedup in non-debug mode */ debug("Select returned something to read (%d)\n", count); } #endif /* Process file descriptors that need to be read */ for (fd = 0; fd < max; fd++) { if ( FD_ISSET(fd, &rd) ) { if ( fd == input_fd ) { #ifdef DEBUG_SELECT /* Command from input*/ if ( debug_flag ) { /* For speedup in non-debug mode */ debug("Something to read from input\n"); } #endif return 1; } else { /* Event from peripheral */ if ( run_wait_event_process(shell, fd, NULL) ) return -1; } } } } return 0; }
int timer_value(shell_t *shell, char *s_value, char *s_unit, long *pt) { char *err; long t; /* Get timeout value */ t = strtol(s_value, &err, 0); if ( t < 0 ) { shell_error(shell, "Illegal expression '%s' for timeout value\n", s_value); return -1; } err = strskip_spaces(err); if ( *err != NUL ) { if ( s_unit != NULL ) { shell_error(shell, "Illegal expression '%s' for timeout value\n", s_value); return -1; } s_unit = err; } /* Time unit qualifier ? */ if ( s_unit != NULL ) { /* Adjust with given time unit */ if ( strcmp(s_unit, "h") == 0 ) t *= TIMER_ONE_HOUR; else if ( strcmp(s_unit, "min") == 0 ) t *= TIMER_ONE_MINUTE; else if ( strcmp(s_unit, "s") == 0 ) t *= TIMER_ONE_SECOND; else if ( strcmp(s_unit, "ms") == 0 ) t *= TIMER_ONE_MS; else { shell_error(shell, "Illegal time unit '%s'. Please choose among {h,min,s,ms}.\n", s_unit); return -1; } } *pt = t; return 0; }
void *shell_allocdebug(const char *file, unsigned int line, unsigned long size) { void *ptr = debug_alloc(file, line, size); /* exit if failed */ if(ptr == NULL) { shell_error("malloc"); exit(1); } /* return pointer otherwise */ return ptr; }
static int run_exec_command(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; char *args = argv[1]; periph_item_t *item; int len; /* Check arguments */ if ( (argc != 2) && (argc != 3) ) { shell_error(shell, "%s: too %s arguments\n", argv[0], (argc > 3) ? "many" : "few"); shell_std_help(shell, argv[0]); return -1; } /* Retrieve target peripheral */ if ( args[0] == '@' ) { args++; /* Get peripheral descriptor */ item = run_retrieve_peripheral(shell, args); if ( item == NULL ) return -1; /* Get command to be sent to the peripheral */ args = argv[2]; } else { /* Check there is a default peripheral */ if ( run_with == NULL ) { result_dump_engine(tag, "No default peripheral selected. Please use command 'with'"); return 0; } item = run_with; } /* Do nothing if the peripheral is not connected */ if ( ! periph_item_connected(item) ) { result_dump_engine(tag, "Target peripheral '%s' not connected. Please use command 'open'", item->id); return 0; } /* Send instruction to peripheral */ len = strlen(args); args[len] = '\n'; periph_item_write(item, args, len+1); args[len] = NUL; return 0; }
void create_processes(char* buffer, char commands[][CMD_SIZE], char* envp[]) { int size = parse_cmds(buffer, commands); int pip[2], total = 0, pipe_in = 0; switch(fork()) { case -1: shell_error("fork()"); case 0: while(total != size) { if(pipe(pip) == -1) shell_error("pipe()"); //printf("pip : %d, %d\n", pip[0], pip[1]); char* temp[CMD_SIZE]; char* args[CMD_SIZE]; int used = separate_commands(total, commands, size, temp); get_arguments(temp, used, args); //NULL TERMINATED //print_args(temp, used); //print_args2(args); int in = get_stdin(temp, used, pipe_in); //get instream and outstream. int out = get_stdout(temp, used, pip); //printf("%d : %d\n", in, out); prepare_to_execute(args, envp, in, out); pipe_in = has_pipe(temp, used, pip); //printf("[%d]\n", pipe_in); total += used; } exit(1); default: if(!has_bg(commands, size)) if (wait(0) == -1) shell_error("wait()"); } }
static int run_exec_version(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { /* Check arguments */ if ( cmd_argv->argc > 1 ) { shell_error(shell, "%s: too many arguments\n", cmd_argv->argv[0]); shell_std_help(shell, cmd_argv->argv[0]); return -1; } /* Display version */ result_dump_engine(tag, VERSION " (" __DATE__ ")"); return 0; }
static int run_exec_open(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; int i; /* Connect to peripherals if required */ if ( argc > 1 ) { for (i = 1; i < argc; i++) { /* Retrieve peripheral item */ periph_item_t *item = periph_retrieve(run_periph, strupper(argv[i])); if ( item == NULL ) { shell_error(shell, "Peripheral '%s' not declared. Please use command 'periph'\n", argv[i]); return -1; } /* Open peripheral connection */ if ( periph_open(run_periph, item) == -1 ) { shell_error(shell, "Cannot connect to peripheral '%s'\n", argv[i]); return -1; } /* Report connections status for newly connected peripherals */ result_dump_engine(tag, periph_item_info(item)); } } else { /* Report connections status for all connected peripherals */ for (i = 0; i < run_periph->fd_count; i++) { periph_item_t *item = run_periph->fd_tab[i]; if ( item != NULL ) result_dump_engine(tag, periph_item_info(item)); } } return 0; }
static int open_tty(void) { char *tty_file_name; int fd; if (isatty(STDIN)) { tty_file_name = ttyname(STDIN); fd = open(tty_file_name, O_RDWR); if (fd != -1 && isatty(fd)) return (fd); } shell_error(ERR_NO_TTY, 0); shell_exit(get_context(), EXIT_ERROR); return (-1); }
static periph_item_t *run_retrieve_peripheral(shell_t *shell, char *id) { periph_item_t *item; /* Upper case only */ strupper(id); /* Retrieve peripheral item */ item = periph_retrieve(run_periph, id); if ( item == NULL ) { shell_error(shell, "Unknown target peripheral '%s'\n", id); return NULL; } return item; }
static int run_exec_help(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; /* Check availability */ if ( ! shell->interactive ) { shell_error(shell, "Command '%s' not allowed in non-interactive mode\n", argv[0]); return -1; } /* Check arguments */ if ( argc == 2 ) shell_std_help(shell, argv[1]); else shell_std_help(shell, NULL); return 0; }
static long run_wait_event(shell_t *shell, char *tag, struct timeval *ptv) { fd_set rd; int max; int fd; int count; char *msg; /* Wait for incoming data */ max = 0; do { /* Build fd set */ max = periph_fd_set(run_periph, &rd); count = select(max, &rd, NULL, NULL, ptv); if ( (count == -1) && (errno != EINTR) ) { shell_error(shell, "Select i/o error: %s\n", strerror(errno)); return -1; } } while ( count == -1 ); /* Timeout ? */ if ( count == 0 ) return 0; #ifdef DEBUG_SELECT if ( debug_flag ) { /* For speedup in non-debug mode */ debug("Select returned something to read (%d)\n", count); } #endif /* Process file descriptors that need to be read */ for (fd = 0; fd < max; fd++) { if ( FD_ISSET(fd, &rd) ) { if ( run_wait_event_process(shell, fd, &msg) ) return -1; } } return 1; }
static int run_wait_event_disconnect(periph_item_t *item, shell_t *shell) { /* Close peripheral connection */ if ( periph_close(run_periph, item) == -1 ) { shell_error(shell, "!! PANIC !! Cannot close peripheral '%s'\n", item->id); return -1; } /* Report connections status for newly connected peripherals */ run_prompt_cr(shell); result_dump_engine(TAG_CLOSE, periph_item_info(item)); /* Remove peripheral from WITH specifications */ if ( item == run_with ) { run_with = NULL; result_dump_engine(TAG_WITH, "NULL"); } run_prompt(shell); return 0; }
static int run_exec_close(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; char *str = NULL; int len = 0; int i; /* Check arguments */ if ( argc < 2 ) { shell_error(shell, "%s: too few arguments\n", argv[0]); shell_std_help(shell, argv[0]); return -1; } /* Disconnect from peripherals */ for (i = 1; i < argc; i++) { periph_item_t *item = periph_retrieve(run_periph, strupper(argv[i])); if ( run_exec_close_item(item, tag) ) { int idx = len; len += strlen(item->id) + 1; str = realloc(str, len+1); sprintf(str+idx, "%s ", item->id); } } /* Dump wait synchronization output */ if ( run_wait_output != NULL ) { if ( str != NULL ) fputs(str, run_wait_output); fputs(_LF, run_wait_output); } if ( str != NULL ) free(str); return 0; }
static int run_exec_with(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; /* Check arguments */ if ( argc > 2 ) { shell_error(shell, "%s: too many arguments\n", argv[0]); shell_std_help(shell, argv[0]); return -1; } /* Select default peripheral if requested */ if ( argc == 2 ) { if ( run_exec_with_periph(shell, strupper(argv[1])) ) return -1; } /* Write info to result file */ result_dump_engine(tag, (run_with != NULL) ? run_with->id : "NULL"); return 0; }
static int run_exec_with_periph(shell_t *shell, char *s) { periph_item_t *item; /* Clear previously selected peripheral */ run_with = NULL; /* NULL peripheral ? */ if ( strcmp(s, "NULL") == 0 ) { debug("Default WITH target set to NULL\n"); return 0; } /* Retrieve peripheral item */ if ( (item = periph_retrieve(run_periph, s)) == NULL ) { shell_error(shell, "Unknown target peripheral '%s'\n", s); return -1; } /* Set default peripheral item */ run_with = item; return 0; }
static int run_exec_wait(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; listener_t *listener; char *s; struct timeval tv0; long sec, usec; int unblock, timeout, error; /* Set and check wait timeout */ run_wait_timeout = run_timeout; if ( (argc > 1) && (argv[1][0] == '-') ) { if ( timer_value(shell, &(argv[1][1]), NULL, &run_wait_timeout) ) return -1; argv++; argc--; } if ( run_wait_timeout == -1 ) { shell_error(shell, "No timeout value initialized. Please use command 'timeout' before 'wait'\n"); return -1; } /* Setup wait conditions */ argv++; argc--; if ( (listener = listener_new(argc, argv)) == NULL ) return -1; /* Report what we are waiting for */ s = listener_str(listener); if ( debug_flag ) { /* For speedup in non-debug mode */ debug("Wait: '%s'\n", s); } result_puts(result_header_engine(tag)); result_puts(s); result_puts("\n"); free(s); /* Init timeout counter */ gettimeofday(&tv0, NULL); sec = run_wait_timeout / 1000; usec = (run_wait_timeout % 1000) * 1000; /* Wait until an unblocking condition is met */ unblock = 0; timeout = 0; error = 0; while ( ! (unblock || timeout || error) ) { /* Check unblocking conditions */ if ( listener_check(listener) ) { unblock = 1; } else { struct timeval tv1, tv; /* Update timeout counter */ gettimeofday(&tv1, NULL); tv_sub(&tv1, &tv0); /*fprintf(stderr, "** %ld.%06ld\n", tv1.tv_sec, tv1.tv_usec);*/ tv.tv_sec = sec; tv.tv_usec = usec; if ( tv_sub(&tv, &tv1) ) { timeout = 1; } else { /*fprintf(stderr, " %ld.%06ld\n", tv.tv_sec, tv.tv_usec);*/ /* Wait for events to arrive, and process incoming data */ switch ( run_wait_event(shell, tag, &tv) ) { case -1: error = 1; break; case 0: timeout = 1; break; default : break; } } } } if ( unblock ) { run_wait_event_unblock(shell, tag, listener); } else if ( timeout ) { run_wait_event_timeout(shell, tag); } /* Clear wait conditions */ listener_destroy(listener); return error ? -1 : 0; }
static int run_exec_trig(shell_t *shell, shell_argv_t *cmd_argv, char *tag) { int argc = cmd_argv->argc; char **argv = cmd_argv->argv; int ret = 0; /* No arguments: show triggers */ if ( argc < 2 ) { shell_error(shell, "%s: too fee arguments\n", argv[0]); shell_std_help(shell, argv[0]); return -1; } /* trig def [<id> [@[<periph>]] [<regex>]] */ if ( strcmp(argv[1], "def") == 0 ) { periph_item_t *periph; char *id = argv[2]; int argc2 = argc - 3; char **argv2 = argv + 3; /* Retrieve peripheral source */ periph = run_with; if ( (argc2 > 0) && (argv2[0][0] == '@') ) { char *args = &(argv2[0][1]); if ( args[0] != NUL ) { periph = run_retrieve_peripheral(shell, args); if ( periph == NULL ) return -1; } else { periph = NULL; } argv2++; argc2--; if ( argc2 <= 0 ) { shell_error(shell, "%s: missing <regex> argument\n", argv[0]); shell_std_help(shell, argv[0]); ret = -1; } } if ( ret == 0 ) ret = trig_def(shell, id, periph, argv2, argc2); } /* trig undef [<id> ...] */ else if ( strcmp(argv[1], "undef") == 0 ) { ret = trig_undef(argv+2, argc-2); } /* trig info <id>*/ else if ( strcmp(argv[1], "info") == 0 ) { if ( argc == 3 ) { char *id = strupper(argv[2]); char *info; if ( trig_info(id, &info) == -1 ) { shell_error(shell, "%s: unknown trigger '%s'\n", argv[0], id); ret = -1; } else { if ( info == NULL ) { info = ""; } /* Dump message */ result_dump_engine(tag, "%s '%s'", id, info); /* Also dump wait synchronization output */ if ( run_wait_output != NULL ) { fputs(info, run_wait_output); fputs(_LF, run_wait_output); } } } else { shell_error(shell, "%s: too %s arguments\n", argv[0], (argc < 3) ? "few" : "many"); shell_std_help(shell, argv[0]); ret = -1; } } /* trig count <id> */ else if ( strcmp(argv[1], "count") == 0 ) { if ( argc == 3 ) { char *id = strupper(argv[2]); int count = trig_count(id); if ( count < 0 ) { shell_error(shell, "%s: unknown trigger '%s'\n", argv[0], id); ret = -1; } else { result_dump_engine(tag, "%s %d", id, count); /* Dump wait synchronization output */ if ( run_wait_output != NULL ) fprintf(run_wait_output, "%d\n", count); } } else { shell_error(shell, "%s: too %s arguments\n", argv[0], (argc < 3) ? "few" : "many"); shell_std_help(shell, argv[0]); ret = -1; } } /* trig clear [<id> ...] */ else if ( strcmp(argv[1], "clear") == 0 ) { ret = trig_clear(argv+2, argc-2); } /* Unknown operation */ else { shell_error(shell, "%s: illegal trigger operation (%s)\n", argv[0], argv[1]); shell_std_help(shell, argv[0]); ret = -1; } return ret; }