1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: header.c,v 2.17 2003/06/30 14:59:11 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* header.c - routines for reading and writing information headers. |
6 |
* |
7 |
* Externals declared in resolu.h |
8 |
* |
9 |
* newheader(t,fp) start new information header identified by string t |
10 |
* isheadid(s) returns true if s is a header id line |
11 |
* headidval(r,s) copy header identifier value in s to r |
12 |
* dateval(t,s) get capture date value |
13 |
* isdate(s) returns true if s is a date line |
14 |
* fputdate(t,fp) put out the given capture date and time |
15 |
* fputnow(fp) put out the current date and time |
16 |
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
17 |
* isformat(s) returns true if s is of the form "FORMAT=*" |
18 |
* formatval(r,s) copy the format value in s to r |
19 |
* fputformat(s,fp) write "FORMAT=%s" to fp |
20 |
* getheader(fp,f,p) read header from fp, calling f(s,p) on each line |
21 |
* globmatch(pat, str) check for glob match of str against pat |
22 |
* checkheader(i,p,o) check header format from i against p and copy to o |
23 |
* |
24 |
* To copy header from input to output, use getheader(fin, fputs, fout) |
25 |
*/ |
26 |
|
27 |
#include "copyright.h" |
28 |
|
29 |
#include <time.h> |
30 |
#include <ctype.h> |
31 |
|
32 |
#include "rtio.h" |
33 |
#include "resolu.h" |
34 |
|
35 |
#define MAXLINE 512 |
36 |
|
37 |
char HDRSTR[] = "#?"; /* information header magic number */ |
38 |
|
39 |
char FMTSTR[] = "FORMAT="; /* format identifier */ |
40 |
|
41 |
char TMSTR[] = "CAPDATE="; /* capture date identifier */ |
42 |
|
43 |
static int mycheck(); |
44 |
|
45 |
|
46 |
void |
47 |
newheader(s, fp) /* identifying line of information header */ |
48 |
char *s; |
49 |
register FILE *fp; |
50 |
{ |
51 |
fputs(HDRSTR, fp); |
52 |
fputs(s, fp); |
53 |
putc('\n', fp); |
54 |
} |
55 |
|
56 |
|
57 |
int |
58 |
headidval(r,s) /* get header id (return true if is id) */ |
59 |
register char *r, *s; |
60 |
{ |
61 |
register char *cp = HDRSTR; |
62 |
|
63 |
while (*cp) if (*cp++ != *s++) return(0); |
64 |
if (r == NULL) return(1); |
65 |
while (*s && !isspace(*s)) *r++ = *s++; |
66 |
*r = '\0'; |
67 |
return(1); |
68 |
} |
69 |
|
70 |
|
71 |
int |
72 |
isheadid(s) /* check to see if line is header id */ |
73 |
char *s; |
74 |
{ |
75 |
return(headidval(NULL, s)); |
76 |
} |
77 |
|
78 |
|
79 |
int |
80 |
dateval(tloc, s) /* get capture date value */ |
81 |
time_t *tloc; |
82 |
char *s; |
83 |
{ |
84 |
struct tm tms; |
85 |
register char *cp = TMSTR; |
86 |
|
87 |
while (*cp) if (*cp++ != *s++) return(0); |
88 |
while (isspace(*s)) s++; |
89 |
if (!*s) return(0); |
90 |
if (sscanf(s, "%d:%d:%d %d:%d:%d", |
91 |
&tms.tm_year, &tms.tm_mon, &tms.tm_mday, |
92 |
&tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) |
93 |
return(0); |
94 |
if (tloc == NULL) |
95 |
return(1); |
96 |
tms.tm_mon--; |
97 |
tms.tm_year -= 1900; |
98 |
tms.tm_isdst = -1; /* ask mktime() to figure out DST */ |
99 |
*tloc = mktime(&tms); |
100 |
return(1); |
101 |
} |
102 |
|
103 |
|
104 |
int |
105 |
isdate(s) /* is the given line a capture date? */ |
106 |
char *s; |
107 |
{ |
108 |
return(dateval(NULL, s)); |
109 |
} |
110 |
|
111 |
|
112 |
void |
113 |
fputdate(tv, fp) /* write out the given time value */ |
114 |
time_t tv; |
115 |
FILE *fp; |
116 |
{ |
117 |
struct tm *tm = localtime(&tv); |
118 |
if (tm == NULL) |
119 |
return; |
120 |
fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, |
121 |
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, |
122 |
tm->tm_hour, tm->tm_min, tm->tm_sec); |
123 |
} |
124 |
|
125 |
|
126 |
void |
127 |
fputnow(fp) /* write out the current time */ |
128 |
FILE *fp; |
129 |
{ |
130 |
time_t tv; |
131 |
time(&tv); |
132 |
fputdate(tv, fp); |
133 |
} |
134 |
|
135 |
|
136 |
void |
137 |
printargs(ac, av, fp) /* print arguments to a file */ |
138 |
int ac; |
139 |
char **av; |
140 |
FILE *fp; |
141 |
{ |
142 |
while (ac-- > 0) { |
143 |
fputword(*av++, fp); |
144 |
fputc(ac ? ' ' : '\n', fp); |
145 |
} |
146 |
} |
147 |
|
148 |
|
149 |
int |
150 |
formatval(r, s) /* get format value (return true if format) */ |
151 |
register char *r; |
152 |
register char *s; |
153 |
{ |
154 |
register char *cp = FMTSTR; |
155 |
|
156 |
while (*cp) if (*cp++ != *s++) return(0); |
157 |
while (isspace(*s)) s++; |
158 |
if (!*s) return(0); |
159 |
if (r == NULL) return(1); |
160 |
do |
161 |
*r++ = *s++; |
162 |
while(*s && !isspace(*s)); |
163 |
*r = '\0'; |
164 |
return(1); |
165 |
} |
166 |
|
167 |
|
168 |
int |
169 |
isformat(s) /* is line a format line? */ |
170 |
char *s; |
171 |
{ |
172 |
return(formatval(NULL, s)); |
173 |
} |
174 |
|
175 |
|
176 |
void |
177 |
fputformat(s, fp) /* put out a format value */ |
178 |
char *s; |
179 |
FILE *fp; |
180 |
{ |
181 |
fputs(FMTSTR, fp); |
182 |
fputs(s, fp); |
183 |
putc('\n', fp); |
184 |
} |
185 |
|
186 |
|
187 |
int |
188 |
getheader(fp, f, p) /* get header from file */ |
189 |
FILE *fp; |
190 |
int (*f)(); |
191 |
char *p; |
192 |
{ |
193 |
char buf[MAXLINE]; |
194 |
|
195 |
for ( ; ; ) { |
196 |
buf[MAXLINE-2] = '\n'; |
197 |
if (fgets(buf, MAXLINE, fp) == NULL) |
198 |
return(-1); |
199 |
if (buf[0] == '\n') |
200 |
return(0); |
201 |
#ifdef MSDOS |
202 |
if (buf[0] == '\r' && buf[1] == '\n') |
203 |
return(0); |
204 |
#endif |
205 |
if (buf[MAXLINE-2] != '\n') { |
206 |
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
207 |
buf[MAXLINE-2] = '\0'; |
208 |
} |
209 |
if (f != NULL && (*f)(buf, p) < 0) |
210 |
return(-1); |
211 |
} |
212 |
} |
213 |
|
214 |
|
215 |
struct check { |
216 |
FILE *fp; |
217 |
char fs[64]; |
218 |
}; |
219 |
|
220 |
|
221 |
static int |
222 |
mycheck(s, cp) /* check a header line for format info. */ |
223 |
char *s; |
224 |
register struct check *cp; |
225 |
{ |
226 |
if (!formatval(cp->fs, s) && cp->fp != NULL) |
227 |
fputs(s, cp->fp); |
228 |
return(0); |
229 |
} |
230 |
|
231 |
|
232 |
int |
233 |
globmatch(p, s) /* check for match of s against pattern p */ |
234 |
register char *p, *s; |
235 |
{ |
236 |
int setmatch; |
237 |
|
238 |
do { |
239 |
switch (*p) { |
240 |
case '?': /* match any character */ |
241 |
if (!*s++) |
242 |
return(0); |
243 |
break; |
244 |
case '*': /* match any string */ |
245 |
while (p[1] == '*') p++; |
246 |
do |
247 |
if ( (p[1]=='?' || p[1]==*s) && |
248 |
globmatch(p+1,s) ) |
249 |
return(1); |
250 |
while (*s++); |
251 |
return(0); |
252 |
case '[': /* character set */ |
253 |
setmatch = *s == *++p; |
254 |
if (!*p) |
255 |
return(0); |
256 |
while (*++p != ']') { |
257 |
if (!*p) |
258 |
return(0); |
259 |
if (*p == '-') { |
260 |
setmatch += p[-1] <= *s && *s <= p[1]; |
261 |
if (!*++p) |
262 |
break; |
263 |
} else |
264 |
setmatch += *p == *s; |
265 |
} |
266 |
if (!setmatch) |
267 |
return(0); |
268 |
s++; |
269 |
break; |
270 |
case '\\': /* literal next */ |
271 |
p++; |
272 |
/* fall through */ |
273 |
default: /* normal character */ |
274 |
if (*p != *s) |
275 |
return(0); |
276 |
s++; |
277 |
break; |
278 |
} |
279 |
} while (*p++); |
280 |
return(1); |
281 |
} |
282 |
|
283 |
|
284 |
/* |
285 |
* Checkheader(fin,fmt,fout) returns a value of 1 if the input format |
286 |
* matches the specification in fmt, 0 if no input format was found, |
287 |
* and -1 if the input format does not match or there is an |
288 |
* error reading the header. If fmt is empty, then -1 is returned |
289 |
* if any input format is found (or there is an error), and 0 otherwise. |
290 |
* If fmt contains any '*' or '?' characters, then checkheader |
291 |
* does wildcard expansion and copies a matching result into fmt. |
292 |
* Be sure that fmt is big enough to hold the match in such cases, |
293 |
* and that it is not a static, read-only string! |
294 |
* The input header (minus any format lines) is copied to fout |
295 |
* if fout is not NULL. |
296 |
*/ |
297 |
|
298 |
int |
299 |
checkheader(fin, fmt, fout) |
300 |
FILE *fin; |
301 |
char *fmt; |
302 |
FILE *fout; |
303 |
{ |
304 |
struct check cdat; |
305 |
register char *cp; |
306 |
|
307 |
cdat.fp = fout; |
308 |
cdat.fs[0] = '\0'; |
309 |
if (getheader(fin, mycheck, (char *)&cdat) < 0) |
310 |
return(-1); |
311 |
if (!cdat.fs[0]) |
312 |
return(0); |
313 |
for (cp = fmt; *cp; cp++) /* check for globbing */ |
314 |
if (*cp == '?' | *cp == '*') |
315 |
if (globmatch(fmt, cdat.fs)) { |
316 |
strcpy(fmt, cdat.fs); |
317 |
return(1); |
318 |
} else |
319 |
return(-1); |
320 |
return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */ |
321 |
} |