Blob


1 /*
2 *
3 * General purpose routines.
4 *
5 */
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <sys/types.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <string.h>
14 #include "gen.h"
15 #include "ext.h"
16 #include "path.h"
18 int nolist = 0; /* number of specified ranges */
19 int olist[50]; /* processing range pairs */
21 int str_convert(char **str, int err);
22 void error(int kind, char *mesg, unsigned int a1, unsigned int a2, unsigned int a3);
23 int cat(char *file);
25 /*****************************************************************************/
26 extern int str_convert();
28 void
29 out_list(str)
31 char *str;
33 {
35 int start, stop;
37 /*
38 *
39 * Grab page ranges from str, save them in olist[], and update the nolist
40 * count. Range syntax matches nroff/troff syntax.
41 *
42 */
44 while ( *str && nolist < sizeof(olist) - 2 ) {
45 start = stop = str_convert(&str, 0);
47 if ( *str == '-' && *str++ )
48 stop = str_convert(&str, 9999);
50 if ( start > stop )
51 error(FATAL, "illegal range %d-%d", start, stop, 0);
53 olist[nolist++] = start;
54 olist[nolist++] = stop;
56 if ( *str != '\0' ) str++;
57 } /* End while */
59 olist[nolist] = 0;
61 } /* End of out_list */
63 /*****************************************************************************/
64 int
65 in_olist(num)
67 int num;
69 {
71 int i;
73 /*
74 *
75 * Return ON if num is in the current page range list. Print everything if
76 * there's no list.
77 *
78 */
79 if ( nolist == 0 )
80 return(ON);
82 for ( i = 0; i < nolist; i += 2 )
83 if ( num >= olist[i] && num <= olist[i+1] )
84 return(ON);
86 return(OFF);
88 } /* End of in_olist */
90 /*****************************************************************************/
91 void
92 setencoding(name)
94 char *name;
96 {
98 char path[150];
100 /*
102 * Include the font encoding file selected by name. It's a full pathname if
103 * it begins with /, otherwise append suffix ".enc" and look for the file in
104 * ENCODINGDIR. Missing files are silently ignored.
106 */
108 if ( name == NULL )
109 name = "Default";
111 if ( *name == '/' )
112 strcpy(path, name);
113 else sprintf(path, "%s/%s.enc", ENCODINGDIR, name);
115 if ( cat(path) == TRUE )
116 writing = strncmp(name, "UTF", 3) == 0;
118 } /* End of setencoding */
120 /*****************************************************************************/
121 int
122 cat(file)
124 char *file;
128 int fd_in;
129 int fd_out;
130 char buf[512];
131 int count;
133 /*
135 * Copy *file to stdout. Return FALSE is there was a problem.
137 */
139 fflush(stdout);
141 if ( (fd_in = open(file, O_RDONLY)) == -1 )
142 return(FALSE);
144 fd_out = fileno(stdout);
145 while ( (count = read(fd_in, buf, sizeof(buf))) > 0 )
146 write(fd_out, buf, count);
148 close(fd_in);
150 return(TRUE);
152 } /* End of cat */
154 /*****************************************************************************/
156 int
157 str_convert(str, err)
159 char **str;
160 int err;
164 int i;
166 /*
168 * Grab the next integer from **str and return its value or err if *str
169 * isn't an integer. *str is modified after each digit is read.
171 */
173 if ( ! isdigit(**str) )
174 return(err);
176 for ( i = 0; isdigit(**str); *str += 1 )
177 i = 10 * i + **str - '0';
179 return(i);
181 } /* End of str_convert */
183 /*****************************************************************************/
185 void
186 error(kind, mesg, a1, a2, a3)
188 int kind;
189 char *mesg;
190 unsigned a1, a2, a3;
194 /*
196 * Print an error message and quit if kind is FATAL.
198 */
200 if ( mesg != NULL && *mesg != '\0' ) {
201 fprintf(stderr, "%s: ", prog_name);
202 fprintf(stderr, mesg, a1, a2, a3);
203 if ( lineno > 0 )
204 fprintf(stderr, " (line %ld)", lineno);
205 if ( position > 0 )
206 fprintf(stderr, " (near byte %ld)", position);
207 putc('\n', stderr);
208 } /* End if */
210 if ( kind == FATAL && ignore == OFF ) {
211 if ( temp_file != NULL )
212 unlink(temp_file);
213 exit(x_stat | 01);
214 } /* End if */
216 } /* End of error */
218 /*****************************************************************************/
220 void interrupt(sig)
222 int sig;
226 /*
228 * Signal handler for translators.
230 */
232 if ( temp_file != NULL )
233 unlink(temp_file);
235 exit(1);
237 } /* End of interrupt */
239 /*****************************************************************************/