//执行指定指令的函数 void console_runcmd(char *cmdline, CONSOLE *console, int *fat, unsigned int mem_total) { if(0 == strcmp(cmdline, "mem")) { cmd_mem(console, mem_total); } else if(0 == strcmp(cmdline, "clear")) { cmd_clear(console); } else if(0 == strcmp(cmdline, "ls")) { cmd_ls(console); } else if(0 == strncmp(cmdline, "type ", 5)) { cmd_type(console, fat, cmdline); } //不是命令但也不是空行 else if(0 != cmdline[0]) { //运行那个应用程序的任务已经在cmd_app中执行了,返回值只是告诉这里是否正常运行了而已 if(0 == cmd_app(console, fat, cmdline)) { //如果到这里了,说明这个命令对应的既不是命令,又不是应用程序,而且不是空行 console_putstring_toend(console, "Bad Command!\n\n"); } } return ; }
//执行指定指令的函数 void console_runcmd(char *cmdline, CONSOLE *console, int *fat, unsigned int mem_total) { if(0 == strcmp(cmdline, "mem")) { cmd_mem(console, mem_total); } else if(0 == strcmp(cmdline, "clear")) { cmd_clear(console); } else if(0 == strcmp(cmdline, "ls")) { cmd_ls(console); } else if(0 == strncmp(cmdline, "type ", 5)) { cmd_type(console, fat, cmdline); } else if(0 == strcmp(cmdline, "hlt")) { cmd_hlt(console, fat); } //不是命令但也不是空行 else if(0 != cmdline[0]) { displayStrings_atLayer(console->layer, 8, console->cursor_y, COL8_FFFFFF, COL8_000000, "Bad command!"); console_newline(console); console_newline(console); } return ; }
int run_command(char* cmdline) { char* argv[BUFFER_SIZE]; int argc = tokenize(cmdline, argv); if (argc == 0) { return 0; } if (strcmp(argv[0], "wait") == 0) { return cmd_wait(argc, argv); } else if (strcmp(argv[0], "ls") == 0) { return cmd_ls(argc, argv); } else if (strcmp(argv[0], "cp") == 0) { return cmd_cp(argc, argv); } else if (strcmp(argv[0], "show") == 0) { return cmd_show(argc, argv); } else if (strcmp(argv[0], "rm") == 0) { return cmd_rm(argc, argv); } else if (strcmp(argv[0], "cmp") == 0) { return cmd_cmp(argc, argv); } else if (strcmp(argv[0], "echo") == 0) { return cmd_echo(argc, argv); } else if (strcmp(argv[0], "exit") == 0) { syscall_exit(0); return 1; // not reached } else { int k = strlen(argv[argc-1]); if (argv[argc-1][k-1] == '&') { argv[argc-1][k-1] = '\0'; return background_run(cmdline); } else { return cmd_run(cmdline); } } }
void console_runcmd(console_t* console, char* cmdline, int* fat, unsigned int memtotal) { /* execute command */ if (0 == strcmp("mem", cmdline)) { /* check memory command */ cmd_mem(console, memtotal); } else if (0 == strcmp("clear", cmdline)) { /* clear command */ cmd_clear(console); } else if (0 == strcmp("ls", cmdline)) { /* ls(dir) command */ cmd_ls(console); } else if (0 == strncmp("cat ", cmdline, 4)) { /* cat command */ cmd_cat(console, fat, cmdline); } else if (0 != cmdline[0]) { if (0 == cmd_app(console, fat, cmdline)) { /* neither command nor null string */ console_putstr0(console, "Bad Command.\n\n"); } } }
static void cmd_cd(conn_item* item) { int len; cmd_header* ch; cmd_header cmd; cmd_header* respond = &cmd; char to[256]; assert(item && item->current_path && item->cmd_rd_buf); ch = (cmd_header*)item->cmd_rd_buf; // assert(cmd->cmd_type == CMD_CD_DIR); respond->magic = htonl(MAGIC_NUM); snprintf(to, item->cmd_rd_curr - sizeof(cmd_header) + 1, "%s", item->cmd_rd_buf + sizeof(cmd_header)); if(strcmp(to, "..") == 0) { if(strcmp(item->current_path, DEFAULT_FILE_PATH) == 0) { respond->cmd_type = htonl(CMD_REQUEST_ERROR); len = sprintf(item->data_wt_buf + sizeof(cmd_header), "already reach the root dir\n"); item->data_wt_curr = len + sizeof(cmd_header); respond->package_len = htonl(item->data_wt_curr); *(cmd_header*)item->data_wt_buf = cmd; return; } up_dir(item->current_path); cmd_ls(item); return; } if(is_in_dir(item->current_path, to) == T_FTP_FAIL) { respond->cmd_type = htonl(CMD_REQUEST_ERROR); len = sprintf(item->data_wt_buf + sizeof(cmd_header), "%s is not a dir\n", to); item->data_wt_curr = len + sizeof(cmd_header); respond->package_len = htonl(item->data_wt_curr); *((cmd_header*)item->data_wt_buf) = cmd; return; } sprintf(item->current_path + strlen(item->current_path), "/%s", to); cmd_ls(item); }
/** * Start a child process. */ void run_child(char *const *args, const char *command) {/* Child */ if (strcmp(args[0], BI_LS) == 0) { cmd_ls(); } else { if (execvp(command, args) == -1) { printf("Unknown Command: %s\n", command); exit(EXIT_FAILURE); } } exit(99); }
void cmd_help(void) { displayBanner(); showdict(helptext); showdict(reservedwords); msgp(M_functions); #ifdef LONG_ALIASES showdict(aliasdict); speol(); #endif showdict(functiondict); speol(); show_user_functions(); speol(); void cmd_ls(void); cmd_ls(); }
void cmd_help(void) { displayBanner(); showdict(helptext); // TODO: extern this puppy extern prog_char reservedwords[]; showdict(reservedwords); msgp(M_functions); #ifdef LONG_ALIASES showdict(aliasdict); speol(); #endif showdict(functiondict); speol(); speol(); void cmd_ls(void); cmd_ls(); }
static int do_cmd(int cmd, int argc, const char *argv[]) { switch (cmd) { case CMD_INTERACTIVE: case CMD_UNKNOWN: return cmd_loop(argc, argv); break; case CMD_CD: return cmd_cd(argv[0]); break; case CMD_PWD: return cmd_pwd(); break; case CMD_LS: return cmd_ls(argc, argv); break; case CMD_MKDIR: return cmd_mkdir(argc, argv); break; case CMD_LN: return cmd_ln(argc, argv); break; case CMD_RM: return cmd_rm(argc, argv); break; case CMD_MV: return cmd_mv(argc, argv); break; case CMD_CP: return cmd_cp(argc, argv); break; case CMD_CAT: return cmd_cat(argc, argv); break; case CMD_STAT: return cmd_stat(argc, argv); break; case CMD_QUIT: exit(EXIT_SUCCESS); break; default: return -1; break; } }
int read_cmd(int sock, char *cmd) { if (ft_strequ(cmd, "quit")) return (1); else if (ft_strequ(cmd, "pwd")) cmd_pwd(sock); else if (ft_strnequ(cmd, "ls", 2)) cmd_ls(sock); else if (ft_strnequ(cmd, "cd", 2)) cmd_cd(sock); else if (ft_strnequ(cmd, "get ", 4)) cmd_get(sock, cmd); else if (ft_strnequ(cmd, "put ", 4)) cmd_put(sock, cmd); else cmd_unknow(cmd); return (0); }
void parse_input(char *cmd) { if(strcmp("ps", cmd) == 0) { cmd_ps(); } else if (strcmp("lsdev", cmd) == 0) { cmd_lsdev(); } else if (strncmp("ls ", cmd, 3) == 0) { cmd_ls(cmd + 3); } else if (strcmp("exit", cmd) == 0) { cmd_exit(); } else if (strcmp("testmt", cmd) == 0) { cmd_testmt(); } else if (strncmp("cat ", cmd, 4) == 0) { cmd_cat(cmd + 4); } else if (strncmp("stat ", cmd, 5) == 0) { cmd_stat(cmd + 5); } else if (strncmp("elf ", cmd, 4) == 0) { cmd_elf(cmd + 4); } }
int run_command(char* cmdline) { char* argv[BUFFER_SIZE]; int argc = tokenize(cmdline, argv); if (argc == 0) { return 0; } if (strcmp(argv[0], "wait") == 0) { return cmd_wait(argc, argv); } else if (strcmp(argv[0], "ls") == 0) { return cmd_ls(argc, argv); } else if (strcmp(argv[0], "cp") == 0) { return cmd_cp(argc, argv); } else if (strcmp(argv[0], "show") == 0) { return cmd_show(argc, argv); } else if (strcmp(argv[0], "rm") == 0) { return cmd_rm(argc, argv); } else if (strcmp(argv[0], "pwd") == 0) { return cmd_pwd(argc, argv); } else if (strcmp(argv[0], "cd") == 0) { return cmd_cd(argc, argv); } else if (strcmp(argv[0], "cmp") == 0) { return cmd_cmp(argc, argv); } else if (strcmp(argv[0], "echo") == 0) { return cmd_echo(argc, argv); } else if (strcmp(argv[0], "mkdir") == 0) { return cmd_mkdir(argc, argv); } else if (strcmp(argv[0], "rmdir") == 0) { return cmd_rmdir(argc, argv); } else { int k = strlen(argv[argc-1]); if (argv[argc-1][k-1] == '&') { argv[argc-1][k-1] = '\0'; return background_run(cmdline); } else { return cmd_run(cmdline); } } }
int parse_cmd(int fd, conn_item* item) { cmd_header* ch; assert(fd > 0 && item && item->cmd_rd_buf); ch = (cmd_header*)item->cmd_rd_buf; switch(ch->cmd_type) { case CMD_LS_DIR: cmd_ls(item); item->state = TFTP_SEND_DATA; break; case CMD_CD_DIR: cmd_cd(item); item->state = TFTP_SEND_DATA; break; case CMD_UPLOAD_FILE: if(try_cmd_upload(item) == T_FTP_FAIL) { item->state = TFTP_SEND_DATA; } else { item->state = TFTP_UPLOAD_FILE; } break; case CMD_DOWNLOAD_FILE: break; case CMD_CANCLE_TRANS: break; default: break; } return T_FTP_SUCCESS; }
void exe_cmd(struct fcrondyn_cl *client) /* read command, and call corresponding function */ { long int *cmd; int fd; int is_root = 0; is_root = (strcmp(client->fcl_user, ROOTNAME) == 0) ? 1 : 0; /* to make things clearer (avoid repeating client->fcl_ all the time) */ cmd = client->fcl_cmd; fd = client->fcl_sock_fd; /* */ debug("exe_cmd [0,1,2] : %d %d %d", cmd[0], cmd[1], cmd[2]); /* */ switch ( cmd[0] ) { case CMD_SEND_SIGNAL: case CMD_RENICE: cmd_on_exeq(client, cmd, fd, is_root); break; case CMD_DETAILS: case CMD_LIST_JOBS: case CMD_LIST_LAVGQ: case CMD_LIST_SERIALQ: case CMD_LIST_EXEQ: cmd_ls(client, cmd, fd, is_root); break; default: Send_err_msg_end(fd, err_cmd_unknown_str); } }
/* execute command */ EXPORT INT exec_cmd(B *cmd) { INT ac; B *av[N_ARGS]; ac = setup_param(cmd, av); if (ac < 1) return 0; if (strcmp(av[0], "date") == 0) { cmd_date(ac, av); } else if (strcmp(av[0], "attach") == 0) { cmd_attach(ac, av); } else if (strcmp(av[0], "detach") == 0) { cmd_detach(ac, av); } else if (strcmp(av[0], "mkdir") == 0) { cmd_mkdir(ac, av); } else if (strcmp(av[0], "rmdir") == 0) { cmd_rmdir(ac, av); } else if (strcmp(av[0], "pwd") == 0) { cmd_pwd(ac, av); } else if (strcmp(av[0], "cd") == 0) { cmd_cd(ac, av); } else if (strcmp(av[0], "rm") == 0) { cmd_rm(ac, av); } else if (strcmp(av[0], "mv") == 0) { cmd_mv(ac, av); } else if (strcmp(av[0], "ls") == 0) { cmd_ls(ac, av); } else if (strcmp(av[0], "tp") == 0 || strcmp(av[0], "tpx") == 0) { cmd_tp(ac, av); } else if (strcmp(av[0], "cp") == 0) { cmd_cp(ac, av); } else if (strcmp(av[0], "trunc") == 0) { cmd_trunc(ac, av); } else if (strcmp(av[0], "df") == 0) { cmd_df(ac, av); } else if (strcmp(av[0], "sync") == 0) { cmd_sync(ac, av); } else if (strcmp(av[0], "chmod") == 0) { cmd_chmod(ac, av); } else if (strcmp(av[0], "ref") == 0) { cmd_ref(ac, av); } else if (strcmp(av[0], "load") == 0) { cmd_load(ac, av); } else if (strcmp(av[0], "loadspg") == 0) { cmd_loadspg(ac, av); } else if (strcmp(av[0], "unload") == 0) { cmd_unload(ac, av); } else if (strcmp(av[0], "call") == 0) { cmd_call(ac, av); } else if (strncmp(av[0], "?", 1) == 0) { P("date [y m d [h m s]]\n"); P("attach devnm connm\n"); P("detach connm\n"); P("cd dir\n"); P("pwd \n"); P("ls [-t][-l][dir]\n"); P("mkdir dir [mode]\n"); P("rmdir dir\n"); P("rm path\n"); P("mv o-path n-path\n"); P("trunc path len\n"); P("df path\n"); P("sync [path [d]]\n"); P("chmod path mode\n"); P("tp path [ofs len]\n"); P("tpx path [ofs len]\n"); P("cp s-path d-path/dir [wofs [wlen]]\n"); P("ref [item]\n"); P("call addr [p1 p2 p3]\n"); P("load path\n"); P("loadspg path [arg ...]\n"); P("unload progid\n"); #ifdef NET_SAMPLE P("net execute network sample\n"); } else if (strcmp(av[0], "net") == 0) { IMPORT void net_test(void); net_test(); #endif } else { return 0; } return 1; }
int main (void) { sei(); uart_init(UART_BAUD_SELECT(9600, F_CPU)); uart_puts("\r\n\r\nTHIS PROGRAM USES 57600 BAUD - PLEASE CHANGE\r\n\r\n"); _delay_ms(100); uart_init(UART_BAUD_SELECT(57600, F_CPU)); cmd_clear(); uart_puts_P("\n\n\n\n\r"); uart_puts_P("--------------------------\r\n"); uart_puts_P("System [OK]\r\n"); systimer_init(); uart_puts_P("System Timer [OK]\r\n"); /* SD Karte Initilisieren */ cmd_init(); uart_puts_P("--------------------------\r\n\r\n"); _delay_ms(100); char input[50]={0}; static char parameters[10][32]; while(1) { for (uint8_t i = 0; i<=10; *parameters[++i]=0x00); // Input uart_puts_P("> "); uart_input(input); // Process Input getparams(input, 10, 32, parameters); char *cmd = parameters[0]; // Call Functions if (strings_equal(cmd, "ls")) { uint32_t cluster = 0; if (parameters[1][0] != 0x00) cluster = str2num(parameters[1], 16); cmd_ls(cluster); } else if (strings_equal(cmd, "cat")) { if (parameters[1][0] == 0x00 || parameters[2][0] == 0x00) cmd_help("cat"); else { uint64_t cluster = str2num(parameters[1], 16); uint64_t size = str2num(parameters[2], 16); cmd_cat(cluster, size); } } else if (strings_equal(cmd, "readtest")) { if (parameters[1][0] == 0x00 || parameters[2][0] == 0x00) cmd_help("readtest"); else { uint64_t cluster = str2num(parameters[1], 16); uint64_t size = str2num(parameters[2], 16); cmd_readtest(cluster, size); } } else if (strings_equal(cmd, "write")) { if (parameters[1][0] == 0x00 || parameters[2][0] == 0x00) cmd_help("write"); else { uint64_t cluster = str2num(parameters[1], 16); uint8_t adcport = str2num(parameters[2], 10); uint64_t maxsize = str2num(parameters[3], 16); uint8_t speed = str2num(parameters[4], 10); if (parameters[3][0] == 0x00) maxsize = 512; if (parameters[4][0] == 0x00) speed = 50; cmd_write(cluster, adcport, maxsize, speed); } } else if (strings_equal(cmd, "clear")) cmd_clear(); else if (strings_equal(cmd, "time")) cmd_time(); else if (strings_equal(cmd, "init")) cmd_init(); else if (strings_equal(cmd, "help")) cmd_help(parameters[1]); else uart_puts_P("Error: Command not found\n\r"); } while (1); return (1); }
// Get a statement numvar getstatement(void) { numvar retval = 0; //char *fetchmark; numvar fetchmark; chkbreak(); if (sym == s_while) { // at this point sym is pointing at s_while, before the conditional expression // save fetchptr so we can restart parsing from here as the while iterates //fetchmark = fetchptr; fetchmark = markparsepoint(); for (;;) { //fetchptr = fetchmark; // restore to mark //primec(); // set up for mr. getsym() returntoparsepoint(fetchmark, 0); getsym(); // fetch the start of the conditional if (getnum()) { retval = getstatement(); if (sym == s_returning) break; // exit if we caught a return } else { skipstatement(); break; } } } else if (sym == s_if) { getsym(); // eat "if" if (getnum()) { retval = getstatement(); if (sym == s_else) { getsym(); // eat "else" skipstatement(); } } else { skipstatement(); if (sym == s_else) { getsym(); // eat "else" retval = getstatement(); } } } else if (sym == s_lcurly) { getsym(); // eat "{" while ((sym != s_eof) && (sym != s_returning) && (sym != s_rcurly)) retval = getstatement(); if (sym == s_rcurly) getsym(); // eat "}" } else if (sym == s_return) { getsym(); // eat "return" if ((sym != s_eof) && (sym != s_semi)) retval = getnum(); sym = s_returning; // signal we're returning up the line } else if (sym == s_switch) retval = getswitchstatement(); else if (sym == s_function) cmd_function(); else if (sym == s_run) { // run macroname getsym(); if ((sym != s_script_eeprom) && (sym != s_script_progmem) && (sym != s_script_file)) unexpected(M_id); // address of macroid is in symval via parseid // check for [,snoozeintervalms] getsym(); // eat macroid to check for comma; symval untouched if (sym == s_comma) { vpush(symval); getsym(); // eat the comma getnum(); // get a number or else startTask(vpop(), expval); } else startTask(symval, 0); } else if (sym == s_stop) { getsym(); if (sym == s_mul) { // stop * stops all tasks initTaskList(); getsym(); } else if ((sym == s_semi) || (sym == s_eof)) { if (background) stopTask(curtask); // stop with no args stops the current task IF we're in back else initTaskList(); // in foreground, stop all } else stopTask(getnum()); } else if (sym == s_boot) reboot(); else if (sym == s_rm) { // rm "sym" or rm * getsym(); if (sym == s_script_eeprom) { eraseentry(idbuf); } else if (sym == s_mul) nukeeeprom(); else if (sym != s_undef) expected(M_id); getsym(); } else if (sym == s_ps) { getsym(); showTaskList(); } else if (sym == s_peep) { getsym(); cmd_peep(); } else if (sym == s_ls) { getsym(); cmd_ls(); } else if (sym == s_help) { getsym(); cmd_help(); } else if (sym == s_print) { getsym(); cmd_print(); } else if (sym == s_semi) { ; } // ;) #ifdef HEX_UPLOAD // a line beginning with a colon is treated as a hex record // containing data to upload to eeprom // // TODO: verify checksum // else if (sym == s_colon) { // fetchptr points at the byte count byte byteCount = gethex(2); // 2 bytes byte count int addr = gethex(4); // 4 bytes address byte recordType = gethex(2); // 2 bytes record type; now fetchptr -> data if (recordType == 1) reboot(); // reboot on EOF record (01) if (recordType != 0) return; // we only handle the data record (00) if (addr == 0) nukeeeprom(); // auto-clear eeprom on write to 0000 while (byteCount--) eewrite(addr++, gethex(2)); // update the eeprom gethex(2); // discard the checksum getsym(); // and re-prime the parser } #endif else getexpression(); if (sym == s_semi) getsym(); // eat trailing ';' return retval; }
int main(int argc, const char *argv[]) { io_func* io; Volume* volume; TestByteOrder(); if(argc < 3) { printf("usage: %s <image-file> <ls|cat|mv|symlink|mkdir|add|rm|chmod|extract|extractall|rmall|addall|grow|getattr|debug> <arguments>\n", argv[0]); return 0; } io = openFlatFile(argv[1]); if(io == NULL) { fprintf(stderr, "error: Cannot open image-file.\n"); return 1; } volume = openVolume(io); if(volume == NULL) { fprintf(stderr, "error: Cannot open volume.\n"); CLOSE(io); return 1; } if(argc > 1) { if(strcmp(argv[2], "ls") == 0) { cmd_ls(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "cat") == 0) { cmd_cat(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "mv") == 0) { cmd_mv(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "symlink") == 0) { cmd_symlink(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "mkdir") == 0) { cmd_mkdir(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "add") == 0) { cmd_add(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "rm") == 0) { cmd_rm(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "chmod") == 0) { cmd_chmod(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "extract") == 0) { cmd_extract(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "extractall") == 0) { cmd_extractall(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "rmall") == 0) { cmd_rmall(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "addall") == 0) { cmd_addall(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "grow") == 0) { cmd_grow(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "getattr") == 0) { cmd_getattr(volume, argc - 2, argv + 2); } else if(strcmp(argv[2], "debug") == 0) { if(argc > 3 && strcmp(argv[3], "verbose") == 0) { debugBTree(volume->catalogTree, TRUE); } else { debugBTree(volume->catalogTree, FALSE); } } } closeVolume(volume); CLOSE(io); return 0; }
// Get a statement numvar getstatement(void) { numvar retval = 0; char *fetchmark; chkbreak(); //#define LINEMODE #ifdef LINEMODE if (sym == s_while) { // at this point sym is pointing at s_while, before the conditional expression // save fetchptr so we can restart parsing from here as the while iterates char *fetchmark = fetchptr; for (;;) { fetchptr = fetchmark; // restore to mark primec(); // set up for mr. getsym() getsym(); // fetch the start of the conditional if (!getnum()) { //longjmp(env, X_EXIT); // get the conditional; exit on false sym = s_eof; // we're finished here. move along. return; } if (sym != s_colon) expectedchar(':'); getsym(); // eat : getstatementlist(); } } else if (sym == s_if) { getsym(); // fetch the start of the conditional if (!getnum()) { //longjmp(env, X_EXIT); // get the conditional; exit on false sym = s_eof; return; } if (sym != s_colon) expectedchar(':'); getsym(); // eat : getstatementlist(); } // The switch statement: call one of N macros based on a selector value // switch <numval>: macroid1, macroid2,.., macroidN // numval < 0: numval = 0 // numval > N: numval = N else if (sym == s_switch) { getsym(); // eat "switch" numvar selector = getnum(); // evaluate the switch value if (selector < 0) selector = 0; if (sym != s_colon) expectedchar(':'); // we sit before the first macroid // scan and discard the <selector>'s worth of macro ids // that sit before the one we want for (;;) { getsym(); // get an id, sets symval to its eeprom addr as a side effect if (sym != s_macro) expected (6); // TODO: define M_macro instead of 6 getsym(); // eat id, get separator; assume symval is untouched if ((sym == s_semi) || (sym == s_eof)) break; // last case is default so we exit always if (sym != s_comma) expectedchar(','); if (!selector) break; // ok, this is the one we want to execute selector--; // one down... } // call the macro whose addr is squirreled in symval all this time // on return, the parser is ready to pick up where we left off domacrocall(symval); // scan past the rest of the unused switch options, if any // TODO: syntax checking for non-chosen options could be made much tighter at the cost of some space while ((sym != s_semi) && (sym != s_eof)) getsym(); // scan to end of statement without executing } #else // new statement handling if (sym == s_while) { // at this point sym is pointing at s_while, before the conditional expression // save fetchptr so we can restart parsing from here as the while iterates fetchmark = fetchptr; for (;;) { fetchptr = fetchmark; // restore to mark primec(); // set up for mr. getsym() getsym(); // fetch the start of the conditional if (getnum()) { retval = getstatement(); if (sym == s_returning) break; // exit if we caught a return } else { skipstatement(); break; } } } else if (sym == s_if) { getsym(); // eat "if" if (getnum()) { retval = getstatement(); if (sym == s_else) { getsym(); // eat "else" skipstatement(); } } else { skipstatement(); if (sym == s_else) { getsym(); // eat "else" retval = getstatement(); } } } else if (sym == s_lcurly) { getsym(); // eat "{" while ((sym != s_eof) && (sym != s_returning) && (sym != s_rcurly)) retval = getstatement(); if (sym == s_rcurly) getsym(); // eat "}" } else if (sym == s_return) { getsym(); // eat "return" if ((sym != s_eof) && (sym != s_semi)) retval = getnum(); sym = s_returning; // signal we're returning up the line } else if (sym == s_switch) retval = getswitchstatement(); else if (sym == s_function) cmd_function(); #endif else if (sym == s_run) { // run macroname getsym(); if (sym != s_macro) unexpected(M_id); // address of macroid is in symval via parseid // check for [,snoozeintervalms] getsym(); // eat macroid to check for comma; symval untouched if (sym == s_comma) { vpush(symval); getsym(); // eat the comma getnum(); // get a number or else startTask(kludge(vpop()), expval); } else startTask(kludge(symval), 0); } else if (sym == s_stop) { getsym(); if (sym == s_mul) { // stop * stops all tasks initTaskList(); getsym(); } else if ((sym == s_semi) || (sym == s_eof)) { if (background) stopTask(curtask); // stop with no args stops the current task IF we're in back else initTaskList(); // in foreground, stop all } else stopTask(getnum()); } else if (sym == s_boot) reboot(); #if !defined(TINY85) else if (sym == s_rm) { // rm "sym" or rm * getsym(); if (sym == s_macro) { eraseentry(idbuf); } else if (sym == s_mul) nukeeeprom(); else if (sym != s_undef) expected(M_id); getsym(); } else if (sym == s_ps) { getsym(); showTaskList(); } else if (sym == s_peep) { getsym(); cmd_peep(); } else if (sym == s_ls) { getsym(); cmd_ls(); } else if (sym == s_help) { getsym(); cmd_help(); } else if (sym == s_print) { getsym(); cmd_print(); } else if (sym == s_semi) { ; } // ;) #endif #ifdef HEX_UPLOAD // a line beginning with a colon is treated as a hex record // containing data to upload to eeprom // // TODO: verify checksum // else if (sym == s_colon) { // fetchptr points at the byte count byte byteCount = gethex(2); // 2 bytes byte count int addr = gethex(4); // 4 bytes address byte recordType = gethex(2); // 2 bytes record type; now fetchptr -> data if (recordType == 1) reboot(); // reboot on EOF record (01) if (recordType != 0) return; // we only handle the data record (00) if (addr == 0) nukeeeprom(); // auto-clear eeprom on write to 0000 while (byteCount--) eewrite(addr++, gethex(2)); // update the eeprom gethex(2); // discard the checksum getsym(); // and re-prime the parser } #endif else getexpression(); if (sym == s_semi) getsym(); // eat trailing ';' return retval; }
// Get a statement void getstatement(void) { #if !defined(TINY85) chkbreak(); #endif if (sym == s_while) { // at this point sym is pointing at s_while, before the conditional expression // save fetchptr so we can restart parsing from here as the while iterates char *fetchmark = fetchptr; for (;;) { fetchptr = fetchmark; // restore to mark primec(); // set up for mr. getsym() getsym(); // fetch the start of the conditional if (!getnum()) { //longjmp(env, X_EXIT); // get the conditional; exit on false sym = s_eof; // we're finished here. move along. return; } if (sym != s_colon) expectedchar(':'); getsym(); // eat : getstatementlist(); } } else if (sym == s_if) { getsym(); // fetch the start of the conditional if (!getnum()) { //longjmp(env, X_EXIT); // get the conditional; exit on false sym = s_eof; return; } if (sym != s_colon) expectedchar(':'); getsym(); // eat : getstatementlist(); } #if SKETCH // The switch statement: call one of N macros based on a selector value // switch <numval>: macroid1, macroid2,.., macroidN // numval < 0: numval = 0 // numval > N: numval = N else if (sym == s_switch) { getsym(); // eat "switch" numvar selector = getnum(); // evaluate the switch value if (selector < 0) selector = 0; if (sym != s_colon) expectedchar(':'); // we sit before the first macroid // scan and discard the <selector>'s worth of macro ids // that sit before the one we want for (;;) { getsym(); // get an id, sets symval to its eeprom addr as a side effect if (sym != s_macro) expected (6); // TODO: define M_macro instead of 6 getsym(); // eat id, get separator; assume symval is untouched if ((sym == s_semi) || (sym == s_eof)) break; // last case is default so we exit always if (sym != s_comma) expectedchar(','); if (!selector) break; // ok, this is the one we want to execute selector--; // one down... } // call the macro whose addr is squirreled in symval all this time // on return, the parser is ready to pick up where we left off doMacroCall(symval); // scan past the rest of the unused switch options, if any // TODO: syntax checking for non-chosen options could be made much tighter at the cost of some space while ((sym != s_semi) && (sym != s_eof)) getsym(); // scan to end of statement without executing } #endif else if ((sym == s_macro) || (sym == s_undef)) { // macro def or ref getsym(); // scan past macro name to next symbol: ; or := if (sym == s_define) { // macro definition: macroid := strvalue // to define the macro, we need to copy the id somewhere on the stack // to avoid having this local buffer in every getstatement stack frame, // we break out defineMacro here to a separate function that only eats that // stack in the case that a macro is being defined #ifdef TINY85 unexpected(M_defmacro); #else defineMacro(); #endif } else if ((sym == s_semi) || (sym == s_eof)) { // valid macro reference: let's call it #if SKETCH doMacroCall(symval); // parseid stashes the macro address in symval #else char op = sym; // save sym for restore expval = findKey(idbuf); // assumes id in idbuf isn't clobbered since getsym() above if (expval >= 0) { char *fetchmark = fetchptr; // save the current parse pointer // call the macro calleeprommacro(findend(expval)); // register the macro into the parser stream getsym(); getstatementlist(); // parse and execute the macro code here if (sym != s_eof) expected(M_eof); // restore parsing context so we can resume cleanly fetchptr = fetchmark; // restore pointer primec(); // and inchar sym = op; // restore saved sym: s_semi or s_eof } else unexpected(M_id); #endif } else expectedchar(';'); //else getexpression(); // assume it was macro1+32+macro2... } else if (sym == s_run) { // run macroname getsym(); if (sym != s_macro) unexpected(M_id); #if 0 // address of macroid is in symval via parseid startTask(kludge(symval)); getsym(); #else // address of macroid is in symval via parseid // check for [,snoozeintervalms] getsym(); // eat macroid to check for comma; symval untouched if (sym == s_comma) { vpush(symval); getsym(); // eat the comma getnum(); // get a number or else startTask(kludge(vpop()), expval); } else startTask(kludge(symval), 0); #endif } else if (sym == s_stop) { getsym(); if (sym == s_mul) { // stop * stops all tasks initTaskList(); getsym(); } else if ((sym == s_semi) || (sym == s_eof)) { if (background) stopTask(curtask); // stop with no args stops the current task IF we're in back else initTaskList(); // in foreground, stop all } else stopTask(getnum()); } else if (sym == s_boot) reboot(); #if !defined(TINY85) else if (sym == s_rm) { // rm "sym" or rm * getsym(); if (sym == s_macro) { eraseentry(idbuf); } else if (sym == s_mul) nukeeeprom(); else expected(M_id); getsym(); } else if (sym == s_ps) showTaskList(); else if (sym == s_peep) { getsym(); cmd_peep(); } else if (sym == s_ls) { getsym(); cmd_ls(); } else if (sym == s_help) { getsym(); cmd_help(); } else if (sym == s_print) { getsym(); cmd_print(); } #endif #ifdef HEX_UPLOAD // a line beginning with a colon is treated as a hex record // containing data to upload to eeprom // // TODO: verify checksum // else if (sym == s_colon) { // fetchptr points at the byte count byte byteCount = gethex(2); // 2 bytes byte count int addr = gethex(4); // 4 bytes address byte recordType = gethex(2); // 2 bytes record type; now fetchptr -> data if (recordType == 1) reboot(); // reboot on EOF record (01) if (recordType != 0) return; // we only handle the data record (00) if (addr == 0) nukeeeprom(); // auto-clear eeprom on write to 0000 while (byteCount--) eewrite(addr++, gethex(2)); // update the eeprom gethex(2); // discard the checksum getsym(); // and re-prime the parser } #endif else { getexpression(); } }
int main(int argc, const char *argv[]) { io_func* io; Volume* volume; AbstractFile* image; int argOff; TestByteOrder(); if(argc < 3) { printf("usage: %s <image-file> (-k <key>) <ls|cat|mv|mkdir|add|rm|chmod|extract|extractall|rmall|addall|grow|untar> <arguments>\n", argv[0]); return 0; } argOff = 2; if(strstr(argv[1], ".dmg")) { image = createAbstractFileFromFile(fopen(argv[1], "rb")); if(argc > 3) { if(strcmp(argv[2], "-k") == 0) { image = createAbstractFileFromFileVault(image, argv[3]); argOff = 4; } } io = openDmgFilePartition(image, -1); } else { io = openFlatFile(argv[1]); } if(io == NULL) { fprintf(stderr, "error: Cannot open image-file.\n"); return 1; } volume = openVolume(io); if(volume == NULL) { fprintf(stderr, "error: Cannot open volume.\n"); CLOSE(io); return 1; } if(argc > argOff) { if(strcmp(argv[argOff], "ls") == 0) { cmd_ls(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "cat") == 0) { cmd_cat(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "mv") == 0) { cmd_mv(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[2], "symlink") == 0) { cmd_symlink(volume, argc - 2, argv + 2); } else if(strcmp(argv[argOff], "mkdir") == 0) { cmd_mkdir(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "add") == 0) { cmd_add(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "rm") == 0) { cmd_rm(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "chmod") == 0) { cmd_chmod(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "extract") == 0) { cmd_extract(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "extractall") == 0) { cmd_extractall(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "rmall") == 0) { cmd_rmall(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "addall") == 0) { cmd_addall(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "grow") == 0) { cmd_grow(volume, argc - argOff, argv + argOff); } else if(strcmp(argv[argOff], "untar") == 0) { cmd_untar(volume, argc - argOff, argv + argOff); } } closeVolume(volume); CLOSE(io); return 0; }