char *cmdv = "Interactive command parser CTOS/BTOS Version-2.00, Sep 1991"; /* C K C M D -- Interactive command package for Unix */ /* Modelled after the DECSYSTEM-20 command parser (the COMND JSYS) Features: . parses and verifies keywords, text strings, numbers, and other data . displays appropriate menu or help message when user types "?" . does keyword and filename completion when user types ESC . accepts any unique abbreviation for a keyword . allows keywords to have attributes, like "invisible" . can supply defaults for fields omitted by user . provides command line editing (character, word, and line deletion) . accepts input from keyboard, command files, or redirected stdin . allows for full or half duplex operation, character or line input . settable prompt, protected from deletion Functions: cmsetp - Set prompt cmsavp - Save current prompt prompt - Issue prompt cmini - Clear the command buffer (before parsing a new command) cmres - Reset command buffer pointers (before reparsing) cmkey - Parse a keyword cmnum - Parse a number cmifi - Parse an input file name cmofi - Parse an output file name cmfld - Parse an arbitrary field cmtxt - Parse a text string cmcfm - Parse command confirmation (end of line) stripq - Strip out backslash quotes from a string. ckvar - Check if a name is valid for a variable expvar - Expand a variable into its string savvar - Save a value to a specified variable buffer clrkbd - Clears the keyboard buffer Return codes: -3: no input provided when required -2: input was invalid -1: reparse required (user deleted into a preceding field) 0 or greater: success See individual functions for greater detail. Before using these routines, the caller should #include ckcmd.h, and set the program's prompt by calling cmsetp(). If the file parsing functions cmifi and cmofi are to be used, this module must be linked with a ckz??? file system support module for the appropriate system, e.g. ckzbsd for Berkeley Unix. If the caller puts the terminal in character wakeup ("cbreak") mode with no echo, then these functions will provide line editing -- character, word, and line deletion, as well as keyword and filename completion upon ESC and help strings, keyword, or file menus upon '?'. If the caller puts the terminal into character wakeup/noecho mode, care should be taken to restore it before exit from or interruption of the program. If the character wakeup mode is not set, the system's own line editor may be used. Author: Frank da Cruz (SY.FDC@CU20B), Columbia University Center for Computing Activities, Jan 1985. Copyright (C) 1985, Trustees of Columbia University in the City of New York. Permission is granted to any individual or institution to copy or use this software except for explicitly commercial purposes, provided this copyright notice is retained. */ /* Modifications: PUSHCMD function added to support IF command SAVVAR function added to support variables CKVAR function added to support variables EXPVAR function added to support variables CLRKBD function added to support interactive IF command D. Drury, E. Arnerich 1991 */ /* Includes */ #include /* Standard C I/O package */ #include /* Character types */ #include "ctcmd.h" /* Command parsing definitions */ #include "ctdebu.h" /* Formats for debug() */ #ifndef MAXVARS #define MAXVARS 26 #endif #ifndef MAXVARL #define MAXVARL 21 #endif extern char savbuf[CMDBL+4]; extern char g_vars[MAXVARS][MAXVARL]; /* global variable buffers */ extern int expand_var; extern int dsplon; /* error msg display flag 1 = display defined in CTUSR3*/ /* Local variables */ int psetf = 0, /* Flag that prompt has been set */ cc = 0, /* Character count */ dpx = 0; /* Duplex (0 = full) */ int hw = HLPLW, /* Help line width */ hc = HLPCW, /* Help line column width */ hh, /* Current help column number */ hx; /* Current help line position */ #define PROML 60 /* Maximum length for prompt */ char cmprom[PROML+1]; /* Program's prompt */ char *dfprom = "Command? "; /* Default prompt */ int cmflgs; /* Command flags */ char cmdbuf[CMDBL+4]; /* Command buffer */ char inicmd[MAXINI][CMDBL]; /* Initial commands from -O option */ int inicmd_count = 0; int inicmd_index = 0; char hlpbuf[HLPBL+4]; /* Help string buffer */ char atmbuf[ATMBL+4]; /* Atom buffer */ char filbuf[ATMBL+4]; /* File name buffer */ /* Command buffer pointers */ static char *bp, /* Current command buffer position */ *pp, /* Start of current field */ *np; /* Start of next field */ /* C M S E T P -- Set the program prompt. */ cmsetp(s) char *s; { char *strncpy(); psetf = 1; /* Flag that prompt has been set. */ strncpy(cmprom,s,PROML - 1); /* Copy the string. */ cmprom[PROML] = NUL; } /* C M S A V P -- Save a copy of the current prompt. */ cmsavp(s,n) int n; char s[]; { strncpy(s,cmprom,n-1); s[n] = NUL; } /* P R O M P T -- Issue the program prompt. */ prompt() { if (psetf == 0) cmsetp(dfprom); /* If no prompt set, set default. */ printf("\r%s",cmprom); /* Print the prompt. */ } /* C M R E S -- Reset pointers to beginning of command buffer. */ cmres() { cc = 0; /* Reset character counter. */ pp = cmdbuf; pp = np = bp = cmdbuf; /* Point to command buffer. */ cmflgs = -5; /* Parse not yet started. */ } /* C M I N I -- Clear the command and atom buffers, reset pointers. */ /* The argument specifies who is to echo the user's typein -- 1 means the cmd package echoes 0 somebody else (system, front end, terminal) echoes */ cmini(d) int d; { for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL; *atmbuf = NUL; dpx = d; cmres(); } stripq(s) char *s; { /* Function to strip '\' quotes */ char *t; while (*s) { if (*s == '\\') { for (t = s; *t != '\0'; t++) *t = *(t+1); } s++; } } /* C M N U M -- Parse a number in the indicated radix */ /* For now, only works for positive numbers in base 10. */ /* Returns -3 if no input present when required, -2 if user typed an illegal number, -1 if reparse needed, 0 otherwise, with n set to number that was parsed */ cmnum(xhlp,xdef,radix,n) char *xhlp, *xdef; int radix, *n; { int x; char *s; if (radix != 10) { /* Just do base 10 for now */ printf("cmnum: illegal radix - %d\n",radix); return(-1); } x = cmfld(xhlp,xdef,&s); debug(F101,"cmnum: cmfld","",x); if (x < 0) return(x); /* Parse a field */ if (digits(atmbuf)) { /* Convert to number */ *n = atoi(atmbuf); return(x); } else { printf("\n?not a number - %s\n",s); return(-2); } } /* C M O F I -- Parse the name of an output file */ /* Depends on the external function zchko(); if zchko() not available, use cmfld() to parse output file names. Returns -3 if no input present when required, -2 if permission would be denied to create the file, -1 if reparse needed, 0 or 1 otherwise, with xp pointing to name. */ cmofi(xhlp,xdef,xp) char *xhlp, *xdef, **xp; { int x; char *s; if (*xhlp == NUL) xhlp = "Output file"; *xp = ""; if ((x = cmfld(xhlp,xdef,&s)) < 0) return(x); if (chkwld(s)) { printf("\n?Wildcards not allowed - %s\n",s); return(-2); } if (zchko(s) < 0) { printf("\n?Write permission denied - %s\n",s); return(-2); } else { *xp = s; return(x); } } /* C M I F I -- Parse the name of an existing file */ /* This function depends on the external functions: zchki() - Check if input file exists and is readable. zxpand() - Expand a wild file specification into a list. znext() - Return next file name from list. If these functions aren't available, then use cmfld() to parse filenames. */ /* Returns -4 EOF -3 if no input present when required, -2 if file does not exist or is not readable, -1 if reparse needed, 0 or 1 otherwise, with: xp pointing to name, wild = 1 if name contains '*' or '?', 0 otherwise. */ cmifi(xhlp,xdef,xp,wild) char *xhlp, *xdef, **xp; int *wild; { int i, x, xc, y; char *sp; cc = xc = 0; /* Initialize counts & pointers */ *xp = ""; if ((x = cmflgs) != 1) { /* Already confirmed? */ x = getwd(); /* No, get a word */ } else { cc = setatm(xdef); /* If so, use default, if any. */ } *xp = atmbuf; /* Point to result. */ *wild = chkwld(*xp); while (1) { xc += cc; /* Count the characters. */ debug(F111,"cmifi: getwd",atmbuf,xc); switch (x) { case -4: /* EOF */ case -2: /* Out of space. */ case -1: /* Reparse needed */ return(x); /* cont'd... */ /* ...cmifi(), cont'd */ case 0: /* SP or NL */ case 1: if (xc == 0) *xp = xdef; /* If no input, return default. */ else *xp = atmbuf; if (**xp == NUL) return(-3); /* If field empty, return -3. */ /* If filespec is wild, see if there are any matches */ *wild = chkwld(*xp); debug(F101," *wild","",*wild); if (*wild != 0) { y = zxpand(*xp); if (y == 0) { if (dsplon) printf("\n?No files match - %s\n",*xp); return(-2); } else if (y < 0) { if (dsplon) printf("\n?Too many files match - %s\n",*xp); return(-2); } else return(x); } /* If not wild, see if it exists and is readable. */ y = zchki(*xp); if (y == -3) { if (dsplon) printf("\n?Read permission denied - %s\n",*xp); return(-2); } else if (y == -2) { if (dsplon) printf("\n?File not readable - %s\n",*xp); return(-2); } else if (y < 0) { if (dsplon) printf("\n?File not found - %s\n",*xp); return(-2); } return(x); /* cont'd... */ /* ...cmifi(), cont'd */ case 2: /* ESC */ if (xc == 0) { if (*xdef != '\0') { printf("%s ",xdef); /* If at beginning of field, */ addbuf(xdef); /* supply default. */ cc = setatm(xdef); } else { /* No default */ putchar(BEL); } break; } if (*wild = chkwld(*xp)) { /* No completion if wild */ putchar(BEL); break; } sp = atmbuf + cc; *sp++ = '*'; *sp-- = '\0'; y = zxpand(atmbuf); /* Add * and expand list. */ *sp = '\0'; /* Remove *. */ if (y == 0) { if (dsplon) printf("\n?No files match - %s\n",atmbuf); return(-2); } else if (y < 0) { if (dsplon) printf("\n?Too many files match - %s\n",atmbuf); return(-2); } else if (y > 1) { /* Not unique, just beep. */ putchar(BEL); } else { /* Unique, complete it. */ znext(filbuf); /* Get whole name of file. */ sp = filbuf + cc; /* Point past what user typed. */ printf("%s ",sp); /* Complete the name. */ addbuf(sp); /* Add the characters to cmdbuf. */ setatm(pp); /* And to atmbuf. */ *xp = atmbuf; /* Return pointer to atmbuf. */ return(cmflgs = 0); } break; /* cont'd... */ /* ...cmifi(), cont'd */ case 3: /* Question mark */ if (*xhlp == NUL) printf(" Input file specification"); else printf(" %s",xhlp); if (xc > 0) { sp = atmbuf + cc; /* Insert * at end */ *sp++ = '*'; *sp-- = '\0'; y = zxpand(atmbuf); *sp = '\0'; if (y == 0) { if (dsplon) printf("\n?No files match - %s\n",atmbuf); return(-2); } else if (y < 0) { if (dsplon) printf("\n?Too many file match - %s\n",atmbuf); return(-2); } else { printf(", one of the following:\n"); clrhlp(); for (i = 0; i < y; i++) { znext(filbuf); addhlp(filbuf); } dmphlp(); } } else printf("\n"); printf("%s%s",cmprom,cmdbuf); break; } x = getwd(); } } /* C H K W L D -- Check for wildcard characters '*' or '?' */ chkwld(s) char *s; { for ( ; *s != '\0'; s++) { if ((*s == '*') || (*s == '?')) return(1); } return(0); } /* C M F L D -- Parse an arbitrary field */ /* Returns -3 if no input present when required, -2 if field too big for buffer, -1 if reparse needed, 0 otherwise, xp pointing to string result. */ cmfld(xhlp,xdef,xp) char *xhlp, *xdef, **xp; { int x, xc; cc = xc = 0; /* Initialize counts & pointers */ *xp = ""; if ((x = cmflgs) != 1) { /* Already confirmed? */ x = getwd(); /* No, get a word */ } else { cc = setatm(xdef); /* If so, use default, if any. */ } *xp = atmbuf; /* Point to result. */ while (1) { xc += cc; /* Count the characters. */ debug(F111,"cmfld: getwd",atmbuf,xc); debug(F101," x","",x); switch (x) { case -4: /* EOF */ case -2: /* Out of space. */ case -1: /* Reparse needed */ return(x); case 0: /* SP or NL */ case 1: if (xc == 0) *xp = xdef; /* If no input, return default. */ else *xp = atmbuf; if (**xp == NUL) x = -3; /* If field empty, return -3. */ return(x); case 2: /* ESC *** (maybe treat as SP) */ if (xc == 0) { printf("%s ",xdef); /* If at beginning of field, */ addbuf(xdef); /* supply default. */ cc = setatm(xdef); } else { putchar(BEL); /* Beep if already into field. */ } break; case 3: /* Question mark */ if (*xhlp == NUL) printf(" Please complete this field"); else printf(" %s",xhlp); printf("\n%s%s",cmprom,cmdbuf); break; } x = getwd(); } } /* C M T X T -- Get a text string, including confirmation */ /* Print help message 'xhlp' if ? typed, supply default 'xdef' if null string typed. Returns -1 if reparse needed or buffer overflows. 1 otherwise. with cmflgs set to return code, and xp pointing to result string. */ cmtxt(xhlp,xdef,xp) char *xhlp; char *xdef; char **xp; { int x, xc; cc = xc = 0; /* Start counters off at 0 */ *xp = ""; /* And pointer to null string. */ *atmbuf = NUL; /* And empty atom buffer. */ if ((x = cmflgs) != 1) { x = getwd(); /* Get first word. */ *xp = pp; /* Save pointer to it. */ } while (1) { xc += cc; /* Accumulate count. */ debug(F111,"cmtxt: getwd",atmbuf,xc); switch (x) { case -4: /* EOF */ case -2: /* Overflow */ case -1: /* Deletion */ return(x); case 0: /* Space */ break; case 1: /* CR or LF */ if (xc == 0) *xp = xdef; return(x); case 2: /* ESC */ if (xc == 0) { printf("%s ",xdef); cc = addbuf(xdef); } else { putchar(BEL); } break; case 3: /* Question Mark */ if (*xhlp == NUL) printf(" Text string"); else printf(" %s",xhlp); printf("\n%s%s",cmprom,cmdbuf); break; default: printf("\n?Unexpected return code from getwd() - %d\n",x); return(-2); } x = getwd(); } } /* C M K E Y -- Parse a keyword */ /* Call with: table -- keyword table, in 'struct keytab' format; n -- number of entries in table; xhlp -- pointer to help string; xdef -- pointer to default keyword; Returns: -3 -- no input supplied and no default available -2 -- input doesn't uniquely match a keyword in the table -1 -- user deleted too much, command reparse required n >= 0 -- value associated with keyword */ cmkey(table,n,xhlp,xdef) struct keytab table[]; int n; char *xhlp, *xdef; { int i, y, z, zz, xc; char *xp; xc = cc = 0; /* Clear character counters. */ if ((zz = cmflgs) == 1) /* Command already entered? */ setatm(xdef); else zz = getwd(); debug(F101,"cmkey: table length","",n); debug(F101," cmflgs","",cmflgs); debug(F101," zz","",zz); while (1) { xc += cc; debug(F111,"cmkey: getwd",atmbuf,xc); switch(zz) { case -4: /* EOF */ case -2: /* Buffer overflow */ case -1: /* Or user did some deleting. */ return(zz); case 0: /* User terminated word with space */ case 1: /* or newline */ if (cc == 0) setatm(xdef); y = lookup(table,atmbuf,n,&z); switch (y) { case -2: printf("\n?Ambiguous - %s\n",atmbuf); return(cmflgs = -2); case -1: printf("\n?Invalid - %s\n",atmbuf); return(cmflgs = -2); default: break; } return(y); /* cont'd... */ /* ...cmkey(), cont'd */ case 2: /* User terminated word with ESC */ if (cc == 0) { if (*xdef != NUL) { /* Nothing in atmbuf */ printf("%s ",xdef); /* Supply default if any */ addbuf(xdef); cc = setatm(xdef); debug(F111,"cmkey: default",atmbuf,cc); } else { putchar(BEL); /* No default, just beep */ break; } } y = lookup(table,atmbuf,n,&z); /* Something in atmbuf */ debug(F111,"cmkey: esc",atmbuf,y); if (y == -2) { putchar(BEL); break; } if (y == -1) { printf("\n?Invalid - %s\n",atmbuf); return(cmflgs = -2); } xp = table[z].kwd + cc; printf("%s ",xp); addbuf(xp); debug(F110,"cmkey: addbuf",cmdbuf,0); return(y); /* cont'd... */ /* ...cmkey(), cont'd */ case 3: /* User terminated word with "?" */ y = lookup(table,atmbuf,n,&z); if (y > -1) { printf(" %s\n%s%s",table[z].kwd,cmprom,cmdbuf); break; } else if (y == -1) { printf("\n?Invalid\n"); return(cmflgs = -2); } if (*xhlp == NUL) printf(" One of the following:\n"); else printf(" %s, one of the following:\n",xhlp); clrhlp(); for (i = 0; i < n; i++) { if (!strncmp(table[i].kwd,atmbuf,cc) && !test(table[i].flgs,CM_INV)) addhlp(table[i].kwd); } dmphlp(); printf("%s%s", cmprom, cmdbuf); break; default: printf("\n%d - Unexpected return code from getwd\n",zz); return(cmflgs = -2); } zz = getwd(); } } /* C M C F M -- Parse command confirmation (end of line) */ /* Returns -2: User typed anything but whitespace or newline -1: Reparse needed 0: Confirmation was received */ cmcfm() { int x, xc; debug(F101,"cmcfm: cmflgs","",cmflgs); xc = cc = 0; if (cmflgs == 1) return(0); while (1) { x = getwd(); xc += cc; debug(F111,"cmcfm: getwd",atmbuf,xc); switch (x) { case -4: /* EOF */ case -2: case -1: return(x); case 0: /* Space */ continue; case 1: /* End of line */ if (xc > 0) { printf("?Not confirmed - %s\n",atmbuf); return(-2); } else return(0); case 2: putchar(BEL); continue; case 3: if (xc > 0) { printf("\n?Not confirmed - %s\n",atmbuf); return(-2); } printf("\n Type a carriage return to confirm the command\n"); printf("%s%s",cmprom,cmdbuf); continue; } } } /* Keyword help routines */ /* C L R H L P -- Initialize/Clear the help line buffer */ clrhlp() { /* Clear the help buffer */ hlpbuf[0] = NUL; hh = hx = 0; } /* A D D H L P -- Add a string to the help line buffer */ addhlp(s) char *s; { /* Add a word to the help buffer */ int j; hh++; /* Count this column */ for (j = 0; j < hc; j++) { /* Fill the column */ if (*s != NUL) /* First with chars from the string */ hlpbuf[hx++] = *s++; else { if (hh < (hw / hc)) /* Then with spaces */ hlpbuf[hx++] = SP; else { hlpbuf[hx++] = NUL; /* If last column, no spaces. */ dmphlp(); /* Print it. */ return; } } } if (*s != NUL) /* Still some chars left in string? */ hlpbuf[hx-1] = '+'; /* Mark as too long for column. */ } /* D M P H L P -- Dump the help line buffer */ dmphlp() { /* Print the help buffer */ hlpbuf[hx++] = NUL; printf(" %s\n",hlpbuf); clrhlp(); } /* L O O K U P -- Lookup the string in the given array of strings */ /* Call this way: v = lookup(table,word,n,&x); table - a 'struct keytab' table. cmd - the target string to look up in the table. n - the number of elements in the table. x - address of an integer for returning the table array index. The keyword table must be arranged in ascending alphabetical order, and all letters must be lowercase. Returns the keyword's associated value ( zero or greater ) if found, with the variable x set to the array index, or: -3 if nothing to look up (target was null), -2 if ambiguous, -1 if not found. A match is successful if the target matches a keyword exactly, or if the target is a prefix of exactly one keyword. It is ambiguous if the target matches two or more keywords from the table. */ lookup(table,cmd,n,x) char *cmd; struct keytab table[]; int n, *x; { int i, v, cmdlen; /* Lowercase & get length of target, if it's null return code -3. */ if ((((cmdlen = lower(cmd))) == 0) || (n < 1)) return(-3); /* Not null, look it up */ for (i = 0; i < n-1; i++) { if (!strcmp(table[i].kwd,cmd) || ((v = !strncmp(table[i].kwd,cmd,cmdlen)) && strncmp(table[i+1].kwd,cmd,cmdlen))) { *x = i; return(table[i].val); } if (v) return(-2); } /* Last (or only) element */ if (!strncmp(table[n-1].kwd,cmd,cmdlen)) { *x = n-1; return(table[n-1].val); } else return(-1); } /* G E T W D -- Gets a "word" from the command input stream */ /* Usage: retcode = getwd(); Returns: -4 if end of file (e.g. pipe broken) -2 if command buffer overflows -1 if user did some deleting 0 if word terminates with SP or tab 1 if ... CR 2 if ... ESC 3 if ... ? With: pp pointing to beginning of word in buffer bp pointing to after current position atmbuf containing a copy of the word cc containing the number of characters in the word copied to atmbuf */ getwd() { int y = -1; /* Used for variable expansion */ int c; /* Current char */ static int inword = 0; /* Flag for start of word found */ int quote = 0; /* Flag for quote character */ int echof = 0; /* Flag for whether to echo */ int ignore = 0; pp = np; /* Start of current field */ debug(F101,"getwd: cmdbuf","",(int) cmdbuf); debug(F101," bp","",(int) bp); debug(F101," pp","",(int) pp); debug(F110," cmdbuf",cmdbuf,0); while (bp < cmdbuf+CMDBL) { /* Loop */ ignore = echof = 0; /* Flag for whether to echo */ if ((c = *bp) == NUL) { /* Get next character */ if (dpx) echof = 1; /* from reparse buffer */ c = getchar(); /* or from tty. */ debug(F101,"getwd: c","",(int) c); if (c == EOF) return(-4); } else ignore = 1; if (quote == 0) { if (!ignore && (c == '\\')) { /* Quote character */ quote = 1; continue; } if (c == FF) { /* Formfeed. */ c = NL; /* Replace with newline */ printf("\377C\001\001\377EF"); /* and clear the screen. */ } if (c == HT) c = SP; /* Substitute space for tab. */ /* cont'd... */ /* ...getwd(), cont'd */ if (c == SP) { /* If space */ *bp++ = c; /* deposit it in buffer. */ if (echof) putchar(c); /* echo it. */ if (inword == 0) { /* If leading, gobble it. */ pp++; continue; } else { /* If terminating, return. */ np = bp; setatm(pp); inword = 0; debug(F110," getwd np",np,0); return(cmflgs = 0); } } if (c == NL) { /* CR, LF */ if (echof) putchar(c); /* Echo the typein */ np = bp; /* Where to start next field. */ /* code added to implement variables */ /* this code is only used if the variable is the last item on a command */ /* line. in that case, we expand the variable and fool the parser into */ /* thinking its not done with the line. we have to manually add the CR */ /* back into the cmdbuf so that the interactive parser won't wait for */ /* it to be entered from the keyboard. np and bp are then adjusted by */ /* the additional characters generated by the variable expansion - thats*/ /* how the parser is fooled. */ if (expand_var) { y = ckvar(pp); if (y >= 0) { /* if var(s) present, */ /* y = expvar(cmdbuf, CMDBL); expand var(s) */ y = expvar(pp, CMDBL); debug(F101," y","",y); y = ((int) pp - (int) cmdbuf) + y; cmdbuf[y] = '\n'; /* add the new line character */ debug(F110," Variable expanded to",cmdbuf,0); expand_var = 0; echof = 0; debug(F101," pp","",(int) pp); } } /* to here */ setatm(pp); /* Copy this field to atom buffer.*/ inword = 0; if (y <= 0) { /* no variable was found or it was null */ *bp = NUL; /* End the string */ return(cmflgs = 1); /*end of line if no var found */ } /* new code for variables */ else { np = pp + cc; /* reset the buffer pointers */ bp = np; return(cmflgs = 0); /* to continue parsing the line */ } /* to here */ } if (!ignore && (c == '?')) { /* Question mark */ putchar(c); *bp = NUL; setatm(pp); return(cmflgs = 3); } if (c == ESC) { /* ESC */ *bp = NUL; setatm(pp); return(cmflgs = 2); } if (c == BS || c == RUB) { /* Character deletion */ if (bp > cmdbuf) { /* If still in buffer... */ printf("\b \b"); /* erase character from screen, */ bp--; /* point behind it, */ if (*bp == SP) inword = 0; /* Flag if current field gone */ *bp = NUL; /* Erase character from buffer. */ } else { /* Otherwise, */ putchar(BEL); /* beep, */ cmres(); /* and start parsing a new command. */ } if (pp < bp) continue; else return(cmflgs = -1); } if (c == LDEL) { /* ^U, line deletion */ while ((bp--) > cmdbuf) { printf("\b \b"); *bp = NUL; } cmres(); /* Restart the command. */ inword = 0; return(cmflgs = -2); } /* cont'd... */ /* ...getwd(), cont'd */ if (c == WDEL) { /* ^W, word deletion */ if (bp <= cmdbuf) { /* Beep if nothing to delete */ putchar(BEL); cmres(); return(cmflgs = -1); } bp--; for ( ; (bp >= cmdbuf) && (*bp == SP) ; bp--) { printf("\b \b"); *bp = NUL; } for ( ; (bp >= cmdbuf) && (*bp != SP) ; bp--) { printf("\b \b"); *bp = NUL; } *bp++ = NUL; inword = 0; return(cmflgs = -1); } if (c == RDIS) { /* ^R, redisplay */ *bp = NUL; printf("\n%s%s",cmprom,cmdbuf); continue; } } if (echof) putchar(c); /* If tty input, echo. */ inword = 1; /* Flag we're in a word. */ quote = 0; /* Turn off quote. */ *bp++ = c; /* And deposit it. */ } /* end of big while */ putchar(BEL); /* Get here if... */ printf("\n?Buffer full\n"); return(cmflgs = -2); } /* Utility functions */ /* A D D B U F -- Add the string pointed to by cp to the command buffer */ addbuf(cp) char *cp; { int len = 0; while ((*cp != NUL) && (bp < cmdbuf+CMDBL)) { *bp++ = *cp++; /* Copy and */ len++; /* count the characters. */ } *bp++ = SP; /* Put a space at the end */ *bp = NUL; /* Terminate with a null */ np = bp; /* Update the next-field pointer */ return(len); /* Return the length */ } /* S E T A T M -- Deposit a string in the atom buffer */ setatm(cp) char *cp; { int y; char *ap; cc = 0; ap = atmbuf; *ap = NUL; /* new code to handle variables */ if (expand_var) { y = ckvar(cp); if (y >= 0) { y = expvar(cmdbuf, CMDBL); /* expand w/o mod to cmdbuf pointer */ debug(F111,"setatm: expvar",cmdbuf,y); } } /* to here */ while (*cp == SP) cp++; while ((*cp != SP) && (*cp != NL) && (*cp != NUL)) { *ap++ = *cp++; cc++; } *ap++ = NUL; if (y >= 0) { /* if variable expanded, adjust cmdbuf pointers */ np = pp + cc; bp = np; } return(cc); /* Return length */ } /* D I G I T S -- Verify that all the characters in line are digits */ digits(s) char *s; { while (*s) { if (!isdigit(*s)) return(0); s++; } return(1); } /* L O W E R -- Lowercase a string */ lower(s) char *s; { int n = 0; while (*s) { if (isupper(*s)) *s = tolower(*s); s++, n++; } return(n); } /* T E S T -- Bit test */ test(x,m) int x, m; { /* Returns 1 if any bits from m are on in x, else 0 */ return((x & m) ? 1 : 0); } /* P U S H C M D -- Put remainder of command line into SAVBUF */ pushcmd(s) char *s; { int x = 0; strcpy(savbuf,s); /* push the command */ while (savbuf[x] != '\0') x++; savbuf[x++] = '\n'; /* replace null with CR to make valid cmd */ debug(F110," pushcmd savbuf",savbuf,0); cmres(); /* and clear the buffer */ } /* E X P V A R -- Expand variable(s) in pointed string */ expvar(string, max_size) char *string; int max_size; { char work_str[CMDBL+4]; int g_index; char *g_vars_ptr; int work_index = 0; int str_index = 0; while (string[str_index] && work_index < (max_size-1)) { if (!strncmp(&string[str_index], "\\%", 2) && isalpha(string[str_index+2])) { g_index = ckvar(&string[str_index]); if (g_index >= 0 && expand_var) { str_index += 3; g_vars_ptr = g_vars[g_index]; while (*g_vars_ptr && work_index < (max_size-1)) work_str[work_index++] = *g_vars_ptr++; } else if (g_index >= 0) { work_str[work_index++] = string[str_index++]; expand_var = 1; } } else work_str[work_index++] = string[str_index++]; } if (strlen(string) > 0 && work_index > 0) { if (work_index > max_size) work_index = max_size; strncpy(string, work_str, work_index); string[work_index] = NUL; } return(work_index); } /* C K V A R -- Determine if pointed to string is a variable name */ ckvar(string) char *string; { int i; char c, *buf; buf = string; /* while (*buf != '\\' && *buf) buf++; skip to var, if there */ if (*buf == '\\') buf++; else return(-3); if (*buf == '%') buf++; else return(-2); c = *buf; if (islower(c)) c = toupper(c); if (!isalpha(c)) return(-1); i = (int) (c - 'A'); return(i); /* return the global var index */ } /* S A V V A R -- Save variable value to a global variable buffer from command string passed */ savvar(index, string) int index; char *string; { int i; char *str_tmp; str_tmp = string; /* SKIP TO: */ while (*str_tmp && isspace(*str_tmp)) str_tmp++; /* ldng. space */ while (*str_tmp && !isspace(*str_tmp)) str_tmp++; /* first space */ while (*str_tmp && isspace(*str_tmp)) str_tmp++; /* second atom */ while (*str_tmp && !isspace(*str_tmp)) str_tmp++; /* second space */ while (*str_tmp && isspace(*str_tmp)) str_tmp++; /* third atom */ if ((i = strlen(str_tmp)) >= MAXVARL) i = MAXVARL - 1; if (i > 0) strncpy(g_vars[index], str_tmp, i); g_vars[index][i] = '\0'; return(0); } /* C L R K B D -- Clear the keyboard buffer of any residual characters */ clrkbd() { int c; int erc; c = getchar(); /* get 1st char */ debug(F101,"clrkbd: character","",(int) c); while (c != 10 && c != 0) { c = getchar(); } return(0); }