?? ckucmd.c
字號:
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) { printf2("?Not confirmed - %s\n",atmbuf); return(-2); } else return(0); case 2: putchar(BEL); continue; case 3: if (xc > 0) { printf2("\n?Not confirmed - %s\n",atmbuf); return(-2); } printf("\n Type a carriage return to confirm the command\n"); printf3("%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) && (*s != NUL); j++) { /* Fill the column */ hlpbuf[hx++] = *s++; } if (*s != NUL) /* Still some chars left in string? */ hlpbuf[hx-1] = '+'; /* Mark as too long for column. */ if (hh < (hw / hc)) { /* Pad col with spaces if necessary */ for (; j < hc; j++) { hlpbuf[hx++] = SP; } } else { /* If last column, */ hlpbuf[hx++] = NUL; /* no spaces. */ dmphlp(); /* Print it. */ return; }}/* D M P H L P -- Dump the help line buffer */dmphlp() { /* Print the help buffer */ hlpbuf[hx++] = NUL; printf2(" %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. word - 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 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. */ 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 */ system("clear"); /* 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; return(cmflgs = 0); } } if (c == NL || c == CR) { /* CR, LF */ *bp = NUL; /* End the string */ if (echof) { /* If echoing, */ putchar(c); /* echo the typein */#ifdef aegis if (c == CR) putchar(NL);#endif } np = bp; /* Where to start next field. */ setatm(pp); /* Copy this field to atom buffer. */ inword = 0; return(cmflgs = 1); } 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 = -1); }/* 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++; inword = 0; return(cmflgs = -1); } if (c == RDIS) { /* ^R, redisplay */ *bp = NUL; printf3("\n%s%s",cmprom,cmdbuf); continue; } } if (echof) putchar(c); /* If tty input, echo. */ inword = 1; /* Flag we're in a word. */ if (quote == 0 || c != NL) *bp++ = c; /* And deposit it. */ quote = 0; /* Turn off quote. */ } /* 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; { char *ap; cc = 0; ap = atmbuf; *ap = NUL; while (*cp == SP) cp++; while ((*cp != SP) && (*cp != NL) && (*cp != NUL) && (*cp != CR)) { *ap++ = *cp++; cc++; } *ap++ = NUL; 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);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -