Blob


1 /*
2 *
3 * General purpose routines.
4 *
5 */
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>
16 #include "gen.h"
17 #include "ext.h"
18 #include "path.h"
20 int nolist = 0; /* number of specified ranges */
21 int olist[50]; /* processing range pairs */
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);
27 /*****************************************************************************/
29 void
30 out_list(str)
32 char *str;
34 {
36 int start, stop;
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 */
45 while ( *str && nolist < sizeof(olist) - 2 ) {
46 start = stop = str_convert(&str, 0);
48 if ( *str == '-' && *str++ )
49 stop = str_convert(&str, 9999);
51 if ( start > stop )
52 error(FATAL, "illegal range %d-%d", start, stop, 0);
54 olist[nolist++] = start;
55 olist[nolist++] = stop;
57 if ( *str != '\0' ) str++;
58 } /* End while */
60 olist[nolist] = 0;
62 } /* End of out_list */
64 /*****************************************************************************/
65 int
66 in_olist(num)
68 int num;
70 {
72 int i;
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);
83 for ( i = 0; i < nolist; i += 2 )
84 if ( num >= olist[i] && num <= olist[i+1] )
85 return(ON);
87 return(OFF);
89 } /* End of in_olist */
91 /*****************************************************************************/
92 void
93 setencoding(name)
95 char *name;
97 {
99 char path[150];
101 /*
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.
107 */
109 if ( name == NULL )
110 name = "Default";
112 if ( *name == '/' )
113 strcpy(path, name);
114 else sprintf(path, "%s/%s.enc", ENCODINGDIR, name);
116 if ( cat(path) == TRUE )
117 writing = strncmp(name, "UTF", 3) == 0;
119 } /* End of setencoding */
121 /*****************************************************************************/
122 int
123 cat(file)
125 char *file;
129 int fd_in;
130 int fd_out;
131 char buf[512];
132 int count;
134 /*
136 * Copy *file to stdout. Return FALSE is there was a problem.
138 */
140 fflush(stdout);
142 if ( (fd_in = open(file, O_RDONLY)) == -1 )
143 return(FALSE);
145 fd_out = fileno(stdout);
146 while ( (count = read(fd_in, buf, sizeof(buf))) > 0 )
147 write(fd_out, buf, count);
149 close(fd_in);
151 return(TRUE);
153 } /* End of cat */
155 /*****************************************************************************/
157 int
158 str_convert(str, err)
160 char **str;
161 int err;
165 int i;
167 /*
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.
172 */
174 if ( ! isdigit((uchar)**str) )
175 return(err);
177 for ( i = 0; isdigit((uchar)**str); *str += 1 )
178 i = 10 * i + **str - '0';
180 return(i);
182 } /* End of str_convert */
184 /*****************************************************************************/
186 void
187 error(kind, mesg, a1, a2, a3)
189 int kind;
190 char *mesg;
191 unsigned a1, a2, a3;
195 /*
197 * Print an error message and quit if kind is FATAL.
199 */
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 */
211 if ( kind == FATAL && ignore == OFF ) {
212 if ( temp_file != NULL )
213 unlink(temp_file);
214 exit(x_stat | 01);
215 } /* End if */
217 } /* End of error */
219 /*****************************************************************************/
221 void interrupt(sig)
223 int sig;
227 /*
229 * Signal handler for translators.
231 */
233 if ( temp_file != NULL )
234 unlink(temp_file);
236 exit(1);
238 } /* End of interrupt */
240 /*****************************************************************************/