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

unix.superglobalmegacorp.com

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