Annotation of researchv8dc/cmd/sh/print.c, revision 1.1

1.1     ! root        1: /*     @(#)print.c     1.5     */
        !             2: /*
        !             3:  * UNIX shell
        !             4:  *
        !             5:  * Bell Telephone Laboratories
        !             6:  *
        !             7:  */
        !             8: 
        !             9: #include       "defs.h"
        !            10: #define                HZ      60
        !            11: 
        !            12: #define                BUFLEN          256
        !            13: 
        !            14: static char    buffer[BUFLEN];
        !            15: static int     index = 0;
        !            16: char           numbuf[12];
        !            17: 
        !            18: extern void    prc_buff();
        !            19: extern void    prs_buff();
        !            20: extern void    prs_2buff();
        !            21: extern void    prn_buff();
        !            22: extern void    prs_cntl();
        !            23: extern void    prn_buff();
        !            24: 
        !            25: /*
        !            26:  * printing and io conversion
        !            27:  */
        !            28: prp()
        !            29: {
        !            30:        if ((flags & prompt) == 0 && cmdadr)
        !            31:        {
        !            32:                prs_cntl(cmdadr);
        !            33:                prs(colon);
        !            34:        }
        !            35: }
        !            36: 
        !            37: prs(as)
        !            38: char   *as;
        !            39: {
        !            40:        register char   *s;
        !            41: 
        !            42:        if ((s = as) && length(s) > 1)
        !            43:                write(output, s, length(s) - 1);
        !            44: }
        !            45: 
        !            46: prc(c)
        !            47: char   c;
        !            48: {
        !            49:        if (c)
        !            50:                write(output, &c, 1);
        !            51: }
        !            52: 
        !            53: prt(t)
        !            54: long   t;
        !            55: {
        !            56:        register int hr, min, sec;
        !            57: 
        !            58:        t += HZ / 2;
        !            59:        t /= HZ;
        !            60:        sec = t % HZ;
        !            61:        t /= HZ;
        !            62:        min = t % HZ;
        !            63: 
        !            64:        if (hr = t / HZ)
        !            65:        {
        !            66:                prn_buff(hr);
        !            67:                prc_buff('h');
        !            68:        }
        !            69: 
        !            70:        prn_buff(min);
        !            71:        prc_buff('m');
        !            72:        prn_buff(sec);
        !            73:        prc_buff('s');
        !            74: }
        !            75: 
        !            76: prn(n)
        !            77:        int     n;
        !            78: {
        !            79:        itos(n);
        !            80: 
        !            81:        prs(numbuf);
        !            82: }
        !            83: 
        !            84: itos(n)
        !            85: {
        !            86:        register char *abuf;
        !            87:        register unsigned a, i;
        !            88:        int pr, d;
        !            89: 
        !            90:        abuf = numbuf;
        !            91: 
        !            92:        pr = FALSE;
        !            93:        a = n;
        !            94:        for (i = 10000; i != 1; i /= 10)
        !            95:        {
        !            96:                if ((pr |= (d = a / i)))
        !            97:                        *abuf++ = d + '0';
        !            98:                a %= i;
        !            99:        }
        !           100:        *abuf++ = a + '0';
        !           101:        *abuf++ = 0;
        !           102: }
        !           103: 
        !           104: stoi(icp)
        !           105: char   *icp;
        !           106: {
        !           107:        register char   *cp = icp;
        !           108:        register int    r = 0;
        !           109:        register char   c;
        !           110: 
        !           111:        while ((c = *cp, digit(c)) && c && r >= 0)
        !           112:        {
        !           113:                r = r * 10 + c - '0';
        !           114:                cp++;
        !           115:        }
        !           116:        if (r < 0 || cp == icp)
        !           117:                failed(icp, badnum);
        !           118:        else
        !           119:                return(r);
        !           120: }
        !           121: 
        !           122: prl(n)
        !           123: long n;
        !           124: {
        !           125:        int i;
        !           126: 
        !           127:        i = 11;
        !           128:        while (n > 0 && --i >= 0)
        !           129:        {
        !           130:                numbuf[i] = n % 10 + '0';
        !           131:                n /= 10;
        !           132:        }
        !           133:        numbuf[11] = '\0';
        !           134:        prs_buff(&numbuf[i]);
        !           135: }
        !           136: 
        !           137: void
        !           138: flushb()
        !           139: {
        !           140:        if (index)
        !           141:        {
        !           142:                buffer[index] = '\0';
        !           143:                write(1, buffer, length(buffer) - 1);
        !           144:                index = 0;
        !           145:        }
        !           146: }
        !           147: 
        !           148: void
        !           149: prc_buff(c)
        !           150:        char c;
        !           151: {
        !           152:        if (c)
        !           153:        {
        !           154:                if (index + 1 >= BUFLEN)
        !           155:                        flushb();
        !           156: 
        !           157:                buffer[index++] = c;
        !           158:        }
        !           159:        else
        !           160:        {
        !           161:                flushb();
        !           162:                write(1, &c, 1);
        !           163:        }
        !           164: }
        !           165: 
        !           166: /* rob */
        !           167: void
        !           168: prs_2buff(s, t)
        !           169:        char *s, *t;
        !           170: {
        !           171:        prs_buff(s);
        !           172:        prs_buff(t);
        !           173: }
        !           174: 
        !           175: void
        !           176: prs_buff(s)
        !           177:        char *s;
        !           178: {
        !           179:        register int len = length(s) - 1;
        !           180: 
        !           181:        if (index + len >= BUFLEN)
        !           182:                flushb();
        !           183: 
        !           184:        if (len >= BUFLEN)
        !           185:                write(1, s, len);
        !           186:        else
        !           187:        {
        !           188:                movstr(s, &buffer[index]);
        !           189:                index += len;
        !           190:        }
        !           191: }
        !           192: 
        !           193: 
        !           194: clear_buff()
        !           195: {
        !           196:        index = 0;
        !           197: }
        !           198: 
        !           199: 
        !           200: void
        !           201: prs_cntl(s)
        !           202:        char *s;
        !           203: {
        !           204:        register char *ptr = buffer;
        !           205:        register char c;
        !           206: 
        !           207:        while (*s != '\0') 
        !           208:        {
        !           209:                c = (*s & 0177) ;
        !           210:                
        !           211:                /* translate a control character into a printable sequence */
        !           212: 
        !           213:                if (c < '\040') 
        !           214:                {       /* assumes ASCII char */
        !           215:                        *ptr++ = '^';
        !           216:                        *ptr++ = (c + 0100);    /* assumes ASCII char */
        !           217:                }
        !           218:                else if (c == 0177) 
        !           219:                {       /* '\0177' does not work */
        !           220:                        *ptr++ = '^';
        !           221:                        *ptr++ = '?';
        !           222:                }
        !           223:                else 
        !           224:                {       /* printable character */
        !           225:                        *ptr++ = c;
        !           226:                }
        !           227: 
        !           228:                ++s;
        !           229:        }
        !           230: 
        !           231:        *ptr = '\0';
        !           232:        prs(buffer);
        !           233: }
        !           234: 
        !           235: 
        !           236: void
        !           237: prn_buff(n)
        !           238:        int     n;
        !           239: {
        !           240:        itos(n);
        !           241: 
        !           242:        prs_buff(numbuf);
        !           243: }
        !           244: char *
        !           245: quotedstring(s)
        !           246:        register char *s;
        !           247: {
        !           248:        register char *t = s;
        !           249:        register char *outp=locstak();
        !           250:        register quoting=0;
        !           251:        while(*t)
        !           252:                if(any(*t++, " \t\n\\\"'`;&|$*[](){}<>")){
        !           253:                        while(*s){
        !           254:                                if(*s == '\''){
        !           255:                                        if(quoting)
        !           256:                                                *outp++ = *s;   /* end quote */
        !           257:                                        *outp++ = '\\';
        !           258:                                        quoting=0;
        !           259:                                }else if(!quoting){
        !           260:                                        *outp++ = '\'';
        !           261:                                        quoting=1;
        !           262:                                }
        !           263:                                *outp++ = *s++;
        !           264:                        }
        !           265:                        if(quoting)
        !           266:                                *outp++ = '\'';
        !           267:                        break;
        !           268:                }
        !           269:        (void)movstr(s, outp);
        !           270:        return curstak();
        !           271: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.