Annotation of researchv10no/cmd/basic/bite/src/iprintf.c, revision 1.1.1.1

1.1       root        1: /* Copyright Bell Telephone Laboratories Whippany, N.J.
                      2: 
                      3:  *     /////////////////////////////////////
                      4:  *     /////////////////////////////////////
                      5:  *     ///////////// iprintf.c /////////////
                      6:  *     /// J. P. Hawkins WH X4610 8C-001 ///
                      7:  *     ///// Fri Aug 24 17:38:43 1979 //////
                      8:  *     /////////////////////////////////////
                      9:  *     /////////////////////////////////////
                     10: 
                     11:  * BITE PRINTF
                     12:  * INTERPRETIVE PRINTF FOR "BITE" BASIC f and g FORMATS ALLOWED ONLY
                     13:  *
                     14:  */
                     15: /*   "@(#) iprintf.c:  V 1.3  9/22/81" */
                     16: int fd;
                     17: #include       "bas.h"
                     18: extern struct FILTBL filtbl[];
                     19: 
                     20: iprintf()
                     21: {
                     22:        int     prerr;
                     23:        char    s[100];
                     24:        register x;
                     25:        prerr = isprintf(s);
                     26:        if(fd == 1)                     /* normal printf */
                     27:                printf(s);
                     28:        else                            /* file printf */
                     29:        {
                     30:                for(x=0; s[x] != '\0'; x++); /* count chars */
                     31:                write(fd, s, x);        /* printf to file */
                     32:        }
                     33:        return(prerr);
                     34: }
                     35: /*
                     36:  *
                     37:  *
                     38:  */
                     39: isprintf(s)
                     40: char   s[];
                     41: {
                     42:        char    format[80];     /* format string */
                     43:        char    expbfr[40];     /* expression buffer */
                     44:        double  evalx();
                     45:        int     fildes;
                     46:        int     i;
                     47:        char    *ptr;
                     48:        int     fcount;         /* number of args format string asks for */
                     49:        double  arg[10];
                     50: 
                     51: 
                     52:        ptr = expr;
                     53:        fd = 1;         /* set fd for normal printf */
                     54: 
                     55:        if(*ptr == '_' && (*(ptr+1) >= '0' && *(ptr+1) <= '8'))
                     56:        {
                     57:                *ptr++;
                     58:                fildes = *ptr - '1';    /* get BASIC file designator */
                     59:                fd = filtbl[fildes].fildes;
                     60:                *ptr++;
                     61:                if(filtbl[fildes].mode != 'w')  /* file must be open
                     62:                                                     for writing */
                     63:                {
                     64:                        error(inst.thing.linno, 36); /* FILE NOT OPEN FOR WRITE */
                     65:                        return(-1);
                     66:                }
                     67:        }
                     68:        /*
                     69:         * COPY FORMAT STRING
                     70:         */
                     71:        if(*ptr++ != '"')
                     72:        {
                     73:                error(inst.thing.linno, 11); /* missing quotes */
                     74:                return(-1);
                     75:        }
                     76:        fcount = 0;
                     77: 
                     78:        for(i=0; (*ptr != '"') && (*ptr != '\0'); )
                     79:        {
                     80:                if(*ptr == '%') /* format delimiter */
                     81:                {
                     82:                        fcount += 1; /* bump expected arg count */
                     83:                        format[i++] = *ptr++;
                     84:                }
                     85:                else if(*ptr == '\\') /* special character */
                     86:                {
                     87:                        ptr++;  /* look at next char */
                     88:                        switch(*ptr)
                     89:                        {
                     90:                                case 'n': /* NEWLINE */
                     91:                                        format[i] = '\n';
                     92:                                        break;
                     93:                                case 'r': /* CARRIAGE RET */
                     94:                                        format[i] = '\r';
                     95:                                        break;
                     96:                                case 't': /* TAB */
                     97:                                        format[i] = '\t';
                     98:                                        break;
                     99:                                case 'b': /* BACKSPACE */
                    100:                                        format[i] = '\b';
                    101:                                        break;
                    102:                                default:
                    103:                                        format[i] = *ptr;
                    104:                                        break;
                    105:                        }
                    106:                        ptr++;
                    107:                        i++;
                    108:                }
                    109:                else
                    110:                        format[i++] = *ptr++;
                    111:        }
                    112:        format[i] = '\0';
                    113:        if(*ptr == '\0')
                    114:        {
                    115:                error(inst.thing.linno, 11); /* unbalanced quotes */
                    116:                return(-1);
                    117:        }
                    118:        ptr++;
                    119:        if(*ptr != ',' && fcount == 0)
                    120:        {
                    121:                sprintf(s,format);
                    122:                return(0);
                    123:        }
                    124:        i = 0;
                    125:        while(*ptr != '\0')
                    126:        {
                    127:                ptr++;
                    128:                ptr = (char *)prncpy(expbfr,ptr); /* get nxt expr field */
                    129:                arg[i++] = evalx(expbfr);
                    130:        }
                    131:        if(i != fcount)         /* no. args == no. expected args */
                    132:        {
                    133:                error(inst.thing.linno, 39); /* arg count mismatch */
                    134:                return(-1);
                    135:        }
                    136:        if(fcount > 10)
                    137:        {
                    138:                error(inst.thing.linno, 40); /* MORE THAN 10 ARGS */
                    139:                return(-1);
                    140:        }
                    141:        sprintf(s,format,arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
                    142:                arg[7],arg[8],arg[9]);
                    143:        return(0);
                    144: }

unix.superglobalmegacorp.com

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