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