/* * Handler for the -x option. */ void opt_x(int type, char *s) { extern int tabstops[]; extern int ntabstops; extern int tabdefault; char msg[60+(4*TABSTOP_MAX)]; int i; PARG p; switch (type) { case INIT: case TOGGLE: /* Start at 1 because tabstops[0] is always zero. */ for (i = 1; i < TABSTOP_MAX; ) { int n = 0; s = skipsp(s); while (*s >= '0' && *s <= '9') n = (10 * n) + (*s++ - '0'); if (n > tabstops[i-1]) tabstops[i++] = n; s = skipsp(s); if (*s++ != ',') break; } if (i < 2) return; ntabstops = i; tabdefault = tabstops[ntabstops-1] - tabstops[ntabstops-2]; break; case QUERY: (void) strlcpy(msg, "Tab stops ", sizeof(msg)); if (ntabstops > 2) { for (i = 1; i < ntabstops; i++) { if (i > 1) strlcat(msg, ",", sizeof(msg)); (void) snprintf(msg+strlen(msg), sizeof(msg)-strlen(msg), "%d", tabstops[i]); } (void) snprintf(msg+strlen(msg), sizeof(msg)-strlen(msg), " and then "); } (void) snprintf(msg+strlen(msg), sizeof(msg)-strlen(msg), "every %d spaces", tabdefault); p.p_string = msg; error("%s", &p); break; } }
/* * Handler for -t option. */ void opt_t(int type, char *s) { IFILE save_ifile; off_t pos; switch (type) { case INIT: tagoption = s; /* Do the rest in main() */ break; case TOGGLE: if (secure) { error("tags support is not available", NULL_PARG); break; } findtag(skipsp(s)); save_ifile = save_curr_ifile(); /* * Try to open the file containing the tag * and search for the tag in that file. */ if (edit_tagfile() || (pos = tagsearch()) == -1) { /* Failed: reopen the old file. */ reedit_ifile(save_ifile); break; } unsave_ifile(save_ifile); jump_loc(pos, jump_sline); break; } }
static int process_script(CexpParserCtx ctx, char *name, int quiet) { int rval = 0; FILE *filestack[ST_DEPTH]; int sp=-1; char buf[500]; /* limit line length to 500 chars :-( */ int i; sprintf(buf,"<%s\n",name); goto skipFirstPrint; do { register char *p; if ( !quiet ) { for ( i=0; i<=sp; i++ ) printf(" "); printf("%s", buf); fflush(stdout); } skipFirstPrint: p = skipsp(buf); if ( '<' == *p ) { /* extract path */ p = getp(p+1); /* PUSH a new script on the stack */ if ( !quiet ) { for ( i=0; i<=sp; i++ ) fputc('<',stdout); printf("'%s':\n",p); } if ( ++sp >= ST_DEPTH || !(filestack[sp] = fopen(p,"r")) ) { if ( sp >= ST_DEPTH ) { fprintf(stderr,"Scripts too deply nested (only %i levels supported)\n",ST_DEPTH); rval = CEXP_MAIN_NO_MEM; } else { rval = CEXP_MAIN_NO_SCRIPT; perror("opening script file"); } sp--; } } else { /* handle simple comments as a courtesy... */ if ( '#' != *p ) { cexpResetParserCtx(ctx,buf); cexpparse((void*)ctx); } } while ( sp >= 0 && !fgets(buf, sizeof(buf), filestack[sp] ) ) { /* EOF reached; POP script off */ fclose(filestack[sp]); sp--; } } while ( sp >= 0 ); return rval; }
/* * Handler for -T option. */ void opt__T(int type, char *s) { PARG parg; switch (type) { case INIT: tags = s; break; case TOGGLE: s = skipsp(s); tags = lglob(s); break; case QUERY: parg.p_string = tags; error("Tags file \"%s\"", &parg); break; } }
int readreq() { int r; if (skipsp()) goto readend; c = suck(); if (c == EOF) goto readend; if (c == '\n') goto readend; if (c == '.') { while (1) { c = suck(); if (c == EOF) break; if (c == '\n') break; } goto readend; } r = c << 9; c = suck(); if (c != EOF) { if (c != '\n') { r = r | c; } } if (r==0157150) c = tread(ohead); /* oh */ if (r==0163160) { /* sp */ o_sp=nread(); writebreak(); } if (r==0164141) do_ta(); /* ta */ if (r==0165154) o_ul=nread(); /* ul */ while (1) { if (c == EOF) break; if (c == '\n') break; c= suck(); } readend: if (c != EOF) return(1); else return(0); }
static char *getp(register char *p) { char *e; char term; p = skipsp(p); if ( (term='\'') == *p || (term='"') == *p ) { p++; if ( (e = strchr(p,term)) ) { /* '..' or ".." commented string -- no escapes supported */ *e=0; return p; } /* no terminator found; treat normally */ p--; } for ( e=p; *e && !isspace(*e); e++ ) ; *e = 0; return p; }
int nread() // OK { int i=0; int f; f = 0; if (skipsp()==0) while(1) { c = suck(); if (c == EOF) break; if (isspace(c)) break; if (isdigit(c)) { f++; i = i * 10 + c - '0'; } else break; } if (f==0) i = 1; return(i); }
/* * Handler for -o option. */ void opt_o(int type, char *s) { PARG parg; if (secure) { error("log file support is not available", NULL_PARG); return; } switch (type) { case INIT: namelogfile = s; break; case TOGGLE: if (ch_getflags() & CH_CANSEEK) { error("Input is not a pipe", NULL_PARG); return; } if (logfile >= 0) { error("Log file is already in use", NULL_PARG); return; } s = skipsp(s); namelogfile = lglob(s); use_logfile(namelogfile); sync_logfile(); break; case QUERY: if (logfile < 0) { error("No log file", NULL_PARG); } else { parg.p_string = namelogfile; error("Log file \"%s\"", &parg); } break; } }
int cexp_main1(int argc, char **argv, void (*callback)(int argc, char **argv, CexpContext ctx)) { CexpContextRec context; /* the public parts of this instance's context */ CexpContext myContext; char *line=0, *prompt=0, *tmp; char *symfile=0, *script=0; int rval=CEXP_MAIN_INVAL_ARG, quiet=0; MyGetOptCtxtRec oc={0}; /* must be initialized */ int opt; #ifdef HAVE_TECLA #define rl_context context.gl #else #define rl_context 0 #endif char optstr[]={ 'h', 'v', 's',':', 'a',':', 'p',':', #ifdef YYDEBUG 'd', #endif 'q', '\0' }; context.prompt = 0; context.parser = 0; while ((opt=mygetopt_r(argc, argv, optstr,&oc))>=0) { switch (opt) { default: fprintf(stderr,"Unknown Option %c\n",opt); case 'h': usage(argv[0]); case 'v': version(argv[0]); return 0; #ifdef YYDEBUG case 'd': cexpdebug=1; break; #endif case 'q': quiet=1; break; case 's': symfile=oc.optarg; break; case 'a': cexpBuiltinCpuArch = oc.optarg; break; case 'p': free(context.prompt); context.prompt = strdup(oc.optarg); break; } } if (argc>oc.optind) script=argv[oc.optind]; /* make sure vital code is initialized */ { static int initialized=0; cexpContextRunOnce(&initialized, cexpInit); } if (!cexpSystemModule) { if (!symfile) { /* try to find a builtin table */ if ( !cexpModuleLoad(0,0) ) fprintf(stderr,"No builtin symbol table -- need a symbol file argument\n"); } else if (!cexpModuleLoad(symfile,"SYSTEM")) fprintf(stderr,"Unable to load system symbol table\n"); if (!cexpSystemModule) { usage(argv[0]); return CEXP_MAIN_NO_SYMS; } } #ifdef USE_MDBG mdbgInit(); #endif /* initialize the public context */ context.next=0; #ifdef HAVE_BFD_DISASSEMBLER { extern void cexpDisassemblerInit(); cexpDisassemblerInit(&context.dinfo, stdout); } #endif cexpContextGetCurrent(&myContext); if (!myContext) { /* topmost frame */ #ifdef HAVE_TECLA context.gl = new_GetLine(200,2000); if (!context.gl) { fprintf(stderr,"Unable to create line editor\n"); return CEXP_MAIN_NO_MEM; } /* mute warnings about being unable to * read ~/.teclarc */ gl_configure_getline(context.gl,0,0,0); #endif /* register first instance running in this thread's context; */ cexpContextRegister(); if (!quiet) hello(); } else { #ifdef HAVE_TECLA /* re-use caller's line editor */ context.gl = myContext->gl; #endif } /* push our frame to the top */ context.next = myContext; myContext = &context; cexpContextSetCurrent(myContext); /* See if there is an ancestor with a local prompt * and inherit */ if ( !context.prompt && context.next && context.next->prompt ) context.prompt = strdup(context.next->prompt); do { if (!(context.parser=cexpCreateParserCtx(quiet ? 0 : stdout))) { fprintf(stderr,"Unable to create parser context\n"); usage(argv[0]); rval = CEXP_MAIN_NO_MEM; goto cleanup; } #ifdef HAVE_TECLA { CPL_MATCH_FN(cexpSymComplete); gl_customize_completion(context.gl, context.parser, cexpSymComplete); } #endif if (cexpSigHandlerInstaller) cexpSigHandlerInstaller(sighandler); if (!(rval=setjmp(context.jbuf))) { /* call them back to pass the jmpbuf */ if (callback) callback(argc, argv, &context); if (script) { if ( (rval = process_script(context.parser, script, quiet)) ) goto cleanup; } else { while ( (line=readline_r( checkPrompt( &context, &prompt, argc > 0 ? argv[0] : "Cexp" ), rl_context)) ) { /* skip empty lines */ if (*line) { if ( '<' == *(tmp=skipsp(line)) ) { process_script(context.parser,tmp+1,quiet); } else { /* interactively process this line */ cexpResetParserCtx(context.parser,line); cexpparse((void*)context.parser); add_history(line); } } free(line); line=0; } } } else { fprintf(stderr,"\nOops, exception caught\n"); /* setjmp passes 0: first time * 1: longjmp(buf,0) or longjmp(buf,1) * other: longjmp(buf,other) */ rval = (rval<2 ? -1 : CEXP_MAIN_KILLED); } cleanup: script=0; /* become interactive if script is killed */ free(line); line=0; free(prompt); prompt=0; cexpFreeParserCtx(context.parser); context.parser=0; } while (-1==rval); free(context.prompt); /* pop our stack context from the chained list anchored * at the running thread */ myContext = myContext->next; cexpContextSetCurrent(myContext); if ( ! myContext ) { /* we'll exit the topmost instance */ #ifdef HAVE_TECLA del_GetLine(context.gl); #endif cexpContextUnregister(); } return rval; }