⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ckucmd.c

📁 操作系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
     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;    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.   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 = gtword(); 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*/gtword() {     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;#ifdef datageneral    extern int termtype;                /* DG terminal type flag */    extern int con_reads_mt;            /* Console read asynch is active */    if (con_reads_mt) connoi_mt();      /* Task would interfere w/cons read */#endif      pp = np;                            /* Start of current field */    debug(F101,"gtword: 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 */#ifdef datageneral            {               char ch;               c = dgncinb(0,&ch,1);    /* -1 is EOF, -2 TO,                                          * -c is AOS/VS error */               if (c == -2) {           /* timeout was enabled? */                    resto(channel(0));  /* reset timeouts */                    c = dgncinb(0,&ch,1); /* retry this now! */               }               if (c < 0) return(-4);    /* EOF or some error */               else c = (int) ch & 0177; /* Get char without parity */               echof = 1;            }#else            c = getchar();              /* or from tty. */            if (c == EOF) {/***		perror("ckucmd getchar");  (just return silently) ***/		return(-4);	    }#endif        } else ignore = 1;         if (quote == 0) {             if (!ignore && (c == '\\')) { /* Quote character */               quote = 1;               continue;            }            if (c == FF) {              /* Formfeed. */                c = NL;                 /* Replace with newline */#ifdef apollo                putchar(FF);#else#ifdef AMIGA                putchar(FF);#else#ifdef datageneral                putchar(FF);#else                system("clear");        /* and clear the screen. */#endif#endif#endif            }             if (c == HT) c = SP;        /* Substitute space for tab. */ /* cont'd... */ /* ...gtword(), 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 apollo                    if (c == CR) putchar(NL);#endif#ifdef AMIGA                    if (c == CR) putchar(NL);#endif#ifdef datageneral                    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... */#ifdef datageneral                    /* DG '\b' is EM (^y or \031) */                    if (termtype == 1)                         /* Erase a character from non-DG screen, */                         dgncoub(1,"\010 \010",3);                    else#endif                    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 */#ifdef datageneral                /* DG '\b' is EM (^y or \031) */                if (termtype == 1)                    /* Erase a character from a non-DG screen, */                    while ((bp--) > cmdbuf) {                         dgncoub(1,"\010 \010",3);                         *bp = NUL;                    }                else#endif /* datageneral */                while ((bp--) > cmdbuf) {                    printf("\b \b");                    *bp = NUL;                }                cmres();                /* Restart the command. */                inword = 0;                return(cmflgs = -1);            } /* cont'd... */ /* ...gtword(), cont'd */             if (c == WDEL) {            /* ^W, word deletion */                if (bp <= cmdbuf) {     /* Beep if nothing to delete */                    putchar(BEL);                    cmres();                    return(cmflgs = -1);                }                bp--;#ifdef datageneral                /* DG '\b' is EM (^y or \031) */                if (termtype == 1) {                    /* Erase a character from a non-DG screen, */                    for ( ; (bp >= cmdbuf) && (*bp == SP) ; bp--) {                         dgncoub(1,"\010 \010",3);                         *bp = NUL;                    }                    for ( ; (bp >= cmdbuf) && (*bp != SP) ; bp--) {                         dgncoub(1,"\010 \010",3);                         *bp = NUL;                    }                }                else {#endif /* datageneral */                for ( ; (bp >= cmdbuf) && (*bp == SP) ; bp--) {                    printf("\b \b");                    *bp = NUL;                }                for ( ; (bp >= cmdbuf) && (*bp != SP) ; bp--) {                    printf("\b \b");                    *bp = NUL;                }#ifdef datageneral                }   /* Termtype == 1 */#endif                bp++;                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. */        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 + -