int XttTbl::command( char* input_str) { char command[1000]; int sts, sym_sts; char symbol_value[DCLI_SYM_VALUE_SIZE]; if ( input_str[0] == '@') { sts = dcli_replace_symbol( input_str, command, sizeof(command)); if (EVEN(sts)) return sts; /* Read command file */ sts = readcmdfile( &command[1]); if ( sts == DCLI__NOFILE) { message('E',"Unable to open file"); return DCLI__SUCCESS; } else if ( EVEN(sts)) return sts; return DCLI__SUCCESS; } dcli_toupper( input_str, input_str); sts = dcli_replace_symbol( input_str, command, sizeof(command)); if ( EVEN(sts)) return sts; sts = dcli_cli( (dcli_tCmdTable *)&xtttbl_command_table, command, (void *) this, 0); if ( sts == DCLI__COM_NODEF) { /* Try to find a matching symbol */ sym_sts = dcli_get_symbol_cmd( command, symbol_value); if ( ODD(sym_sts)) { if ( symbol_value[0] == '@') { /* Read command file */ sts = readcmdfile( &symbol_value[1]); if ( sts == DCLI__NOFILE) { message('E',"Unable to open file"); return DCLI__SUCCESS; } else if ( EVEN(sts)) return sts; return DCLI__SUCCESS; } sts = dcli_cli( (dcli_tCmdTable *)&xtttbl_command_table, symbol_value, (void *) this, 0); } else if ( sym_sts == DCLI__SYMBOL_AMBIG) sts = sym_sts; } if (sts == DCLI__COM_AMBIG) message('E',"Ambiguous command"); else if (sts == DCLI__COM_NODEF) message('E',"Undefined command"); else if (sts == DCLI__QUAL_AMBIG) message('E',"Ambiguous qualifier"); else if (sts == DCLI__QUAL_NODEF) message('E',"Undefined qualifier"); else if (sts == DCLI__SYMBOL_AMBIG) message('E', "Ambiguous symbol abbrevation"); command_sts = sts; return DCLI__SUCCESS; }
/* * This command is provided since POSIX decided to standardize * the Korn shell fc command. Oh well... */ int histcmd(volatile int argc, char ** volatile argv) { int ch; const char * volatile editor = NULL; HistEvent he; volatile int lflg = 0, nflg = 0, rflg = 0, sflg = 0; int i, retval; const char *firststr, *laststr; int first, last, direction; char * volatile pat = NULL, * volatile repl; /* ksh "fc old=new" crap */ static int active = 0; struct jmploc jmploc; struct jmploc *volatile savehandler; char editfile[MAXPATHLEN + 1]; FILE * volatile efp; #ifdef __GNUC__ repl = NULL; /* XXX gcc4 */ efp = NULL; /* XXX gcc4 */ #endif if (hist == NULL) error("history not active"); if (argc == 1) error("missing history argument"); optreset = 1; optind = 1; /* initialize getopt */ while (not_fcnumber(argv[optind]) && (ch = getopt(argc, argv, ":e:lnrs")) != -1) switch ((char)ch) { case 'e': editor = optionarg; break; case 'l': lflg = 1; break; case 'n': nflg = 1; break; case 'r': rflg = 1; break; case 's': sflg = 1; break; case ':': error("option -%c expects argument", optopt); /* NOTREACHED */ case '?': default: error("unknown option: -%c", optopt); /* NOTREACHED */ } argc -= optind, argv += optind; /* * If executing... */ if (lflg == 0 || editor || sflg) { lflg = 0; /* ignore */ editfile[0] = '\0'; /* * Catch interrupts to reset active counter and * cleanup temp files. */ savehandler = handler; if (setjmp(jmploc.loc)) { active = 0; if (*editfile) unlink(editfile); handler = savehandler; longjmp(handler->loc, 1); } handler = &jmploc; if (++active > MAXHISTLOOPS) { active = 0; displayhist = 0; error("called recursively too many times"); } /* * Set editor. */ if (sflg == 0) { if (editor == NULL && (editor = bltinlookup("FCEDIT", 1)) == NULL && (editor = bltinlookup("EDITOR", 1)) == NULL) editor = DEFEDITOR; if (editor[0] == '-' && editor[1] == '\0') { sflg = 1; /* no edit */ editor = NULL; } } } /* * If executing, parse [old=new] now */ if (lflg == 0 && argc > 0 && ((repl = strchr(argv[0], '=')) != NULL)) { pat = argv[0]; *repl++ = '\0'; argc--, argv++; } /* * If -s is specified, accept only one operand */ if (sflg && argc >= 2) error("too many args"); /* * determine [first] and [last] */ switch (argc) { case 0: firststr = lflg ? "-16" : "-1"; laststr = "-1"; break; case 1: firststr = argv[0]; laststr = lflg ? "-1" : argv[0]; break; case 2: firststr = argv[0]; laststr = argv[1]; break; default: error("too many args"); /* NOTREACHED */ } /* * Turn into event numbers. */ first = str_to_event(firststr, 0); last = str_to_event(laststr, 1); if (rflg) { i = last; last = first; first = i; } /* * XXX - this should not depend on the event numbers * always increasing. Add sequence numbers or offset * to the history element in next (diskbased) release. */ direction = first < last ? H_PREV : H_NEXT; /* * If editing, grab a temp file. */ if (editor) { int fd; INTOFF; /* easier */ snprintf(editfile, sizeof(editfile), "%s_shXXXXXX", _PATH_TMP); if ((fd = mkstemp(editfile)) < 0) error("can't create temporary file %s", editfile); if ((efp = fdopen(fd, "w")) == NULL) { close(fd); error("can't allocate stdio buffer for temp"); } } /* * Loop through selected history events. If listing or executing, * do it now. Otherwise, put into temp file and call the editor * after. * * The history interface needs rethinking, as the following * convolutions will demonstrate. */ history(hist, &he, H_FIRST); retval = history(hist, &he, H_NEXT_EVENT, first); for (;retval != -1; retval = history(hist, &he, direction)) { if (lflg) { if (!nflg) out1fmt("%5d ", he.num); out1str(he.str); } else { const char *s = pat ? fc_replace(he.str, pat, repl) : he.str; if (sflg) { if (displayhist) { out2str(s); } evalstring(strcpy(stalloc(strlen(s) + 1), s), 0); if (displayhist && hist) { /* * XXX what about recursive and * relative histnums. */ history(hist, &he, H_ENTER, s); } break; } else fputs(s, efp); } /* * At end? (if we were to lose last, we'd sure be * messed up). */ if (he.num == last) break; } if (editor) { char *editcmd; size_t cmdlen; fclose(efp); cmdlen = strlen(editor) + strlen(editfile) + 2; editcmd = stalloc(cmdlen); snprintf(editcmd, cmdlen, "%s %s", editor, editfile); evalstring(editcmd, 0); /* XXX - should use no JC command */ INTON; readcmdfile(editfile); /* XXX - should read back - quick tst */ unlink(editfile); } if (lflg == 0 && active > 0) --active; if (displayhist) displayhist = 0; return 0; }