Annotation of researchv10no/cmd/postscript/common/misc.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  *
                      3:  * General purpose routines.
                      4:  *
                      5:  */
                      6: 
                      7: #include <stdio.h>
                      8: #include <ctype.h>
                      9: #include <fcntl.h>
                     10: 
                     11: #include "gen.h"
                     12: #include "ext.h"
                     13: #include "path.h"
                     14: 
                     15: int    nolist = 0;                     /* number of specified ranges */
                     16: int    olist[50];                      /* processing range pairs */
                     17: 
                     18: /*****************************************************************************/
                     19: 
                     20: out_list(str)
                     21: 
                     22:     char       *str;
                     23: 
                     24: {
                     25: 
                     26:     int                start, stop;
                     27: 
                     28: /*
                     29:  *
                     30:  * Grab page ranges from str, save them in olist[], and update the nolist
                     31:  * count. Range syntax matches nroff/troff syntax.
                     32:  *
                     33:  */
                     34: 
                     35:     while ( *str && nolist < sizeof(olist) - 2 ) {
                     36:        start = stop = str_convert(&str, 0);
                     37: 
                     38:        if ( *str == '-' && *str++ )
                     39:            stop = str_convert(&str, 9999);
                     40: 
                     41:        if ( start > stop )
                     42:            error(FATAL, "illegal range %d-%d", start, stop);
                     43: 
                     44:        olist[nolist++] = start;
                     45:        olist[nolist++] = stop;
                     46: 
                     47:        if ( *str != '\0' ) str++;
                     48:     }  /* End while */
                     49: 
                     50:     olist[nolist] = 0;
                     51: 
                     52: }   /* End of out_list */
                     53: 
                     54: /*****************************************************************************/
                     55: 
                     56: in_olist(num)
                     57: 
                     58:     int                num;
                     59: 
                     60: {
                     61: 
                     62:     int                i;
                     63: 
                     64: /*
                     65:  *
                     66:  * Return ON if num is in the current page range list. Print everything if
                     67:  * there's no list.
                     68:  *
                     69:  */
                     70:     if ( nolist == 0 )
                     71:        return(ON);
                     72: 
                     73:     for ( i = 0; i < nolist; i += 2 )
                     74:        if ( num >= olist[i] && num <= olist[i+1] )
                     75:            return(ON);
                     76: 
                     77:     return(OFF);
                     78: 
                     79: }   /* End of in_olist */
                     80: 
                     81: /*****************************************************************************/
                     82: 
                     83: setencoding(name)
                     84: 
                     85:     char       *name;
                     86: 
                     87: {
                     88: 
                     89:     char       path[150];
                     90: 
                     91: /*
                     92:  *
                     93:  * Include the font encoding file selected by name. It's a full pathname if
                     94:  * it begins with /, otherwise append suffix ".enc" and look for the file in
                     95:  * ENCODINGDIR. Missing files are silently ignored.
                     96:  *
                     97:  */
                     98: 
                     99:     if ( name == NULL )
                    100:        name = "Default";
                    101: 
                    102:     if ( *name == '/' )
                    103:        strcpy(path, name);
                    104:     else sprintf(path, "%s/%s.enc", ENCODINGDIR, name);
                    105: 
                    106:     cat(path);
                    107: 
                    108: }   /* End of setencoding */
                    109: 
                    110: /*****************************************************************************/
                    111: 
                    112: cat(file)
                    113: 
                    114:     char       *file;
                    115: 
                    116: {
                    117: 
                    118:     int                fd_in;
                    119:     int                fd_out;
                    120:     char       buf[512];
                    121:     int                count;
                    122: 
                    123: /*
                    124:  *
                    125:  * Copy *file to stdout. Return FALSE is there was a problem.
                    126:  *
                    127:  */
                    128: 
                    129:     fflush(stdout);
                    130: 
                    131:     if ( (fd_in = open(file, O_RDONLY)) == -1 )
                    132:        return(FALSE);
                    133: 
                    134:     fd_out = fileno(stdout);
                    135:     while ( (count = read(fd_in, buf, sizeof(buf))) > 0 )
                    136:        write(fd_out, buf, count);
                    137: 
                    138:     close(fd_in);
                    139: 
                    140:     return(TRUE);
                    141: 
                    142: }   /* End of cat */
                    143: 
                    144: /*****************************************************************************/
                    145: 
                    146: str_convert(str, err)
                    147: 
                    148:     char       **str;
                    149:     int                err;
                    150: 
                    151: {
                    152: 
                    153:     int                i;
                    154: 
                    155: /*
                    156:  *
                    157:  * Grab the next integer from **str and return its value or err if *str
                    158:  * isn't an integer. *str is modified after each digit is read.
                    159:  *
                    160:  */
                    161: 
                    162:     if ( ! isdigit(**str) )
                    163:        return(err);
                    164: 
                    165:     for ( i = 0; isdigit(**str); *str += 1 )
                    166:        i = 10 * i + **str - '0';
                    167: 
                    168:     return(i);
                    169: 
                    170: }   /* End of str_convert */
                    171: 
                    172: /*****************************************************************************/
                    173: 
                    174: error(kind, mesg, a1, a2, a3)
                    175: 
                    176:     int                kind;
                    177:     char       *mesg;
                    178:     unsigned   a1, a2, a3;
                    179: 
                    180: {
                    181: 
                    182: /*
                    183:  *
                    184:  * Print an error message and quit if kind is FATAL.
                    185:  *
                    186:  */
                    187: 
                    188:     if ( mesg != NULL && *mesg != '\0' ) {
                    189:        fprintf(stderr, "%s: ", prog_name);
                    190:        fprintf(stderr, mesg, a1, a2, a3);
                    191:        if ( lineno > 0 )
                    192:            fprintf(stderr, " (line %d)", lineno);
                    193:        if ( position > 0 )
                    194:            fprintf(stderr, " (near byte %d)", position);
                    195:        putc('\n', stderr);
                    196:     }  /* End if */
                    197: 
                    198:     if ( kind == FATAL && ignore == OFF ) {
                    199:        if ( temp_file != NULL )
                    200:            unlink(temp_file);
                    201:        exit(x_stat | 01);
                    202:     }  /* End if */
                    203: 
                    204: }   /* End of error */
                    205: 
                    206: /*****************************************************************************/
                    207: 
                    208: void interrupt(sig)
                    209: 
                    210:     int                sig;
                    211: 
                    212: {
                    213: 
                    214: /*
                    215:  *
                    216:  * Signal handler for translators.
                    217:  *
                    218:  */
                    219: 
                    220:     if ( temp_file != NULL )
                    221:        unlink(temp_file);
                    222: 
                    223:     exit(1);
                    224: 
                    225: }   /* End of interrupt */
                    226: 
                    227: /*****************************************************************************/
                    228: 

unix.superglobalmegacorp.com

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