plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

misc.c (4241B)


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