static int docmd(BW *bw, unsigned char *s, void *object, int *notify) { MACRO *mac; int ret = -1; mac = mparse(NULL, s, &ret,0); if (ret < 0) { msgnw(bw->parent,joe_gettext(_("No such command"))); } else { ret = exmacro(mac, 1); rmmacro(mac); } #ifdef junk CMD *cmd = findcmd(s); vsrm(s); /* allocated in pw.c::rtnpw() */ if (!cmd) msgnw(bw->parent,joe_gettext(_("No such command"))); else { mac = mkmacro(-1, 0, 0, cmd); ret = exmacro(mac, 1); rmmacro(mac); } #endif if (notify) *notify = 1; return ret; }
void cmd_help(int argc, const char **argv) { unsigned char curr; command_t *cmd; /* help <command>. invoke <command> with 'help' as an argument */ if (argc == 2) { if (strncmp(argv[1], "help", strlen(argv[1])) == 0) { printf("Are you kidding?\n"); return; } cmd = findcmd(argv[1]); if (cmd == NULL) { return; } printf(" %s\n", cmd->help); return; } printf("Usage:\n"); curr = 0; while (cmd_tbl[curr] != NULL) { printf(" %s\n", cmd_tbl[curr]->help); curr++; } argv[1] = NULL; }
/* * help - tell about commands, or details of a particular command */ static void help( struct parse *pcmd, FILE *fp ) { struct xcmd *xcp; char *cmd; const char *list[100]; size_t word, words; size_t row, rows; size_t col, cols; size_t length; if (pcmd->nargs == 0) { words = 0; for (xcp = builtins; xcp->keyword != 0; xcp++) { if (*(xcp->keyword) != '?') list[words++] = xcp->keyword; } for (xcp = opcmds; xcp->keyword != 0; xcp++) list[words++] = xcp->keyword; qsort((void *)list, (size_t)words, sizeof(list[0]), helpsort); col = 0; for (word = 0; word < words; word++) { length = strlen(list[word]); col = max(col, length); } cols = SCREENWIDTH / ++col; rows = (words + cols - 1) / cols; fprintf(fp, "ntpdc commands:\n"); for (row = 0; row < rows; row++) { for (word = row; word < words; word += rows) fprintf(fp, "%-*.*s", col, col-1, list[word]); fprintf(fp, "\n"); } } else { cmd = pcmd->argval[0].string; words = findcmd(cmd, builtins, opcmds, &xcp); if (words == 0) { fprintf(stderr, "Command `%s' is unknown\n", cmd); return; } else if (words >= 2) { fprintf(stderr, "Command `%s' is ambiguous\n", cmd); return; } fprintf(fp, "function: %s\n", xcp->comment); printusage(xcp, fp); } }
/* execute a function */ void execfunc(int argc, const char **argv) { command_t *cmd = findcmd(argv[0]); if (cmd == NULL) { return; } DBG_EXECFUNC("execcmd: cmd=%s, argc=%d\n", argv[0], argc); cmd->cmdfunc(argc, argv); }
void do_auto_scroll() { static CMD *scrup = 0; static CMD *scrdn = 0; static CMD *drag = 0; if (!scrup) { scrup = findcmd(USTR "upslide"); scrdn = findcmd(USTR "dnslide"); drag = findcmd(USTR "defmdrag"); } if (auto_scroll > 0) execmd(scrdn,0); else if (auto_scroll < 0) execmd(scrup,0); execmd(drag,0); reset_trig_time(); }
/* * docmd - decode the command line and execute a command */ static void docmd( const char *cmdline ) { char *tokens[1+MAXARGS+MOREARGS+2]; struct parse pcmd; int ntok; int i, ti; int rval; struct xcmd *xcmd; ai_fam_templ = ai_fam_default; /* * Tokenize the command line. If nothing on it, return. */ tokenize(cmdline, tokens, &ntok); if (ntok == 0) return; /* * Find the appropriate command description. */ i = findcmd(tokens[0], builtins, opcmds, &xcmd); if (i == 0) { (void) fprintf(stderr, "***Command `%s' unknown\n", tokens[0]); return; } else if (i >= 2) { (void) fprintf(stderr, "***Command `%s' ambiguous\n", tokens[0]); return; } /* * Save the keyword, then walk through the arguments, interpreting * as we go. */ pcmd.keyword = tokens[0]; pcmd.nargs = 0; ti = 1; for (i = 0; i < MAXARGS && xcmd->arg[i] != NO;) { if ((i+ti) >= ntok) { if (!(xcmd->arg[i] & OPT)) { printusage(xcmd, stderr); return; } break; } if ((xcmd->arg[i] & OPT) && (*tokens[i+ti] == '>')) break; rval = getarg(tokens[i+ti], (int)xcmd->arg[i], &pcmd.argval[i]); if (rval == -1) { ti++; continue; } if (rval == 0) return; pcmd.nargs++; i++; } /* Any extra args are assumed to be "OPT|NTP_STR". */ for ( ; i < MAXARGS + MOREARGS;) { if ((i+ti) >= ntok) break; rval = getarg(tokens[i+ti], (int)(OPT|NTP_STR), &pcmd.argval[i]); if (rval == -1) { ti++; continue; } if (rval == 0) return; pcmd.nargs++; i++; } i += ti; if (i < ntok && *tokens[i] == '>') { char *fname; if (*(tokens[i]+1) != '\0') fname = tokens[i]+1; else if ((i+1) < ntok) fname = tokens[i+1]; else { (void) fprintf(stderr, "***No file for redirect\n"); return; } current_output = fopen(fname, "w"); if (current_output == NULL) { (void) fprintf(stderr, "***Error opening %s: ", fname); perror(""); return; } } else { current_output = stdout; } if (interactive && setjmp(interrupt_buf)) { return; } else { jump = 1; (xcmd->handler)(&pcmd, current_output); jump = 0; if (current_output != stdout) (void) fclose(current_output); current_output = NULL; } }
int filesubstr(char **namptr, int assign) { #define isend(c) ( !(c) || (c)=='/' || (c)==Inpar || (assign && (c)==':') ) #define isend2(c) ( !(c) || (c)==Inpar || (assign && (c)==':') ) char *str = *namptr; if (*str == Tilde && str[1] != '=' && str[1] != Equals) { char *ptr; int val; val = zstrtol(str + 1, &ptr, 10); if (isend(str[1])) { /* ~ */ *namptr = dyncat(home, str + 1); return 1; } else if (str[1] == '+' && isend(str[2])) { /* ~+ */ *namptr = dyncat(pwd, str + 2); return 1; } else if (str[1] == '-' && isend(str[2])) { /* ~- */ char *tmp; *namptr = dyncat((tmp = oldpwd) ? tmp : pwd, str + 2); return 1; } else if (!inblank(str[1]) && isend(*ptr) && (!idigit(str[1]) || (ptr - str < 4))) { char *ds; if (val < 0) val = -val; ds = dstackent(str[1], val); if (!ds) return 0; *namptr = dyncat(ds, ptr); return 1; } else if (iuser(str[1])) { /* ~foo */ char *ptr, *hom, save; for (ptr = ++str; *ptr && iuser(*ptr); ptr++); save = *ptr; if (!isend(save)) return 0; *ptr = 0; if (!(hom = getnameddir(str))) { if (isset(NOMATCH)) zerr("no such user or named directory: %s", str, 0); *ptr = save; return 0; } *ptr = save; *namptr = dyncat(hom, ptr); return 1; } } else if (*str == Equals && isset(EQUALS) && str[1]) { /* =foo */ char sav, *pp, *cnam; for (pp = str + 1; !isend2(*pp); pp++); sav = *pp; *pp = 0; if (!(cnam = findcmd(str + 1))) { Alias a = (Alias) aliastab->getnode(aliastab, str + 1); if (a) cnam = ztrdup(a->text); else { if (isset(NOMATCH)) zerr("%s not found", str + 1, 0); return 0; } } *namptr = dupstring(cnam); zsfree(cnam); if (sav) { *pp = sav; *namptr = dyncat(*namptr, pp); } return 1; } return 0; #undef isend #undef isend2 }