1 |
greg |
2.8 |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
greg |
1.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* header.c - routines for reading and writing information headers. |
9 |
|
|
* |
10 |
|
|
* 8/19/88 |
11 |
greg |
1.3 |
* |
12 |
greg |
2.4 |
* newheader(t,fp) start new information header identified by string t |
13 |
|
|
* isheadid(s) returns true if s is a header id line |
14 |
|
|
* headidval(r,s) copy header identifier value in s to r |
15 |
greg |
2.3 |
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
16 |
greg |
1.3 |
* isformat(s) returns true if s is of the form "FORMAT=*" |
17 |
|
|
* formatval(r,s) copy the format value in s to r |
18 |
|
|
* fputformat(s,fp) write "FORMAT=%s" to fp |
19 |
|
|
* getheader(fp,f,p) read header from fp, calling f(s,p) on each line |
20 |
greg |
2.6 |
* globmatch(pat, str) check for glob match of str against pat |
21 |
greg |
1.3 |
* checkheader(i,p,o) check header format from i against p and copy to o |
22 |
|
|
* |
23 |
|
|
* To copy header from input to output, use getheader(fin, fputs, fout) |
24 |
greg |
1.1 |
*/ |
25 |
|
|
|
26 |
|
|
#include <stdio.h> |
27 |
greg |
1.3 |
#include <ctype.h> |
28 |
greg |
1.1 |
|
29 |
greg |
2.3 |
#define MAXLINE 512 |
30 |
greg |
1.1 |
|
31 |
greg |
2.2 |
#ifndef BSD |
32 |
greg |
2.3 |
#define index strchr |
33 |
greg |
2.2 |
#endif |
34 |
|
|
|
35 |
|
|
extern char *index(); |
36 |
|
|
|
37 |
greg |
2.4 |
char HDRSTR[] = "#?"; /* information header magic number */ |
38 |
greg |
1.2 |
|
39 |
greg |
2.4 |
char FMTSTR[] = "FORMAT="; /* format identifier */ |
40 |
greg |
1.2 |
|
41 |
greg |
2.4 |
|
42 |
|
|
newheader(s, fp) /* identifying line of information header */ |
43 |
|
|
char *s; |
44 |
|
|
register FILE *fp; |
45 |
|
|
{ |
46 |
|
|
fputs(HDRSTR, fp); |
47 |
|
|
fputs(s, fp); |
48 |
|
|
putc('\n', fp); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
|
52 |
greg |
2.5 |
int |
53 |
greg |
2.4 |
headidval(r,s) /* get header id (return true if is id) */ |
54 |
|
|
register char *r, *s; |
55 |
|
|
{ |
56 |
|
|
register char *cp = HDRSTR; |
57 |
|
|
|
58 |
|
|
while (*cp) if (*cp++ != *s++) return(0); |
59 |
|
|
if (r == NULL) return(1); |
60 |
gregl |
2.9 |
while (*s && !isspace(*s)) *r++ = *s++; |
61 |
greg |
2.4 |
*r = '\0'; |
62 |
|
|
return(1); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
greg |
2.5 |
int |
67 |
greg |
2.4 |
isheadid(s) /* check to see if line is header id */ |
68 |
|
|
char *s; |
69 |
|
|
{ |
70 |
|
|
return(headidval(NULL, s)); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
|
74 |
greg |
1.1 |
printargs(ac, av, fp) /* print arguments to a file */ |
75 |
|
|
int ac; |
76 |
|
|
char **av; |
77 |
greg |
2.2 |
register FILE *fp; |
78 |
greg |
1.1 |
{ |
79 |
greg |
2.2 |
int quote; |
80 |
|
|
|
81 |
greg |
1.1 |
while (ac-- > 0) { |
82 |
greg |
2.2 |
if (index(*av, ' ') != NULL) { /* quote it */ |
83 |
|
|
if (index(*av, '\'') != NULL) |
84 |
|
|
quote = '"'; |
85 |
|
|
else |
86 |
|
|
quote = '\''; |
87 |
|
|
putc(quote, fp); |
88 |
|
|
fputs(*av++, fp); |
89 |
|
|
putc(quote, fp); |
90 |
|
|
} else |
91 |
|
|
fputs(*av++, fp); |
92 |
greg |
2.8 |
putc(ac ? ' ' : '\n', fp); |
93 |
greg |
1.1 |
} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
|
97 |
greg |
2.5 |
int |
98 |
greg |
2.4 |
formatval(r, s) /* get format value (return true if format) */ |
99 |
greg |
1.2 |
register char *r; |
100 |
|
|
register char *s; |
101 |
|
|
{ |
102 |
greg |
2.4 |
register char *cp = FMTSTR; |
103 |
|
|
|
104 |
|
|
while (*cp) if (*cp++ != *s++) return(0); |
105 |
greg |
1.3 |
while (isspace(*s)) s++; |
106 |
greg |
2.4 |
if (!*s) return(0); |
107 |
|
|
if (r == NULL) return(1); |
108 |
greg |
2.5 |
do |
109 |
|
|
*r++ = *s++; |
110 |
|
|
while(*s && !isspace(*s)); |
111 |
greg |
1.2 |
*r = '\0'; |
112 |
greg |
2.4 |
return(1); |
113 |
greg |
1.2 |
} |
114 |
|
|
|
115 |
|
|
|
116 |
greg |
2.5 |
int |
117 |
greg |
2.4 |
isformat(s) /* is line a format line? */ |
118 |
|
|
char *s; |
119 |
|
|
{ |
120 |
|
|
return(formatval(NULL, s)); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
|
124 |
greg |
1.2 |
fputformat(s, fp) /* put out a format value */ |
125 |
|
|
char *s; |
126 |
greg |
1.1 |
FILE *fp; |
127 |
greg |
1.2 |
{ |
128 |
|
|
fputs(FMTSTR, fp); |
129 |
|
|
fputs(s, fp); |
130 |
|
|
putc('\n', fp); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
|
134 |
greg |
2.5 |
int |
135 |
greg |
1.2 |
getheader(fp, f, p) /* get header from file */ |
136 |
|
|
FILE *fp; |
137 |
greg |
1.1 |
int (*f)(); |
138 |
greg |
1.2 |
char *p; |
139 |
greg |
1.1 |
{ |
140 |
|
|
char buf[MAXLINE]; |
141 |
|
|
|
142 |
|
|
for ( ; ; ) { |
143 |
|
|
buf[MAXLINE-2] = '\n'; |
144 |
greg |
2.3 |
if (fgets(buf, MAXLINE, fp) == NULL) |
145 |
greg |
1.1 |
return(-1); |
146 |
|
|
if (buf[0] == '\n') |
147 |
|
|
return(0); |
148 |
greg |
2.3 |
#ifdef MSDOS |
149 |
|
|
if (buf[0] == '\r' && buf[1] == '\n') |
150 |
|
|
return(0); |
151 |
|
|
#endif |
152 |
greg |
1.1 |
if (buf[MAXLINE-2] != '\n') { |
153 |
|
|
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
154 |
|
|
buf[MAXLINE-2] = '\0'; |
155 |
|
|
} |
156 |
gwlarson |
2.10 |
if (f != NULL && (*f)(buf, p) < 0) |
157 |
|
|
return(-1); |
158 |
greg |
1.1 |
} |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
|
162 |
greg |
1.2 |
struct check { |
163 |
|
|
FILE *fp; |
164 |
greg |
1.3 |
char fs[64]; |
165 |
greg |
1.2 |
}; |
166 |
greg |
1.1 |
|
167 |
greg |
1.2 |
|
168 |
greg |
1.1 |
static |
169 |
greg |
1.2 |
mycheck(s, cp) /* check a header line for format info. */ |
170 |
greg |
1.1 |
char *s; |
171 |
greg |
1.2 |
register struct check *cp; |
172 |
greg |
1.1 |
{ |
173 |
greg |
2.4 |
if (!formatval(cp->fs, s) && cp->fp != NULL) |
174 |
greg |
1.2 |
fputs(s, cp->fp); |
175 |
greg |
1.1 |
} |
176 |
|
|
|
177 |
|
|
|
178 |
greg |
2.5 |
int |
179 |
greg |
2.6 |
globmatch(pat, str) /* check for glob match of str against pat */ |
180 |
greg |
1.3 |
char *pat, *str; |
181 |
|
|
{ |
182 |
|
|
register char *p = pat, *s = str; |
183 |
|
|
|
184 |
|
|
do { |
185 |
|
|
switch (*p) { |
186 |
|
|
case '?': /* match any character */ |
187 |
|
|
if (!*s++) |
188 |
|
|
return(0); |
189 |
|
|
break; |
190 |
|
|
case '*': /* match any string */ |
191 |
|
|
while (p[1] == '*') p++; |
192 |
|
|
do |
193 |
greg |
2.6 |
if ( (p[1]=='?' || p[1]==*s) && |
194 |
|
|
globmatch(p+1,s) ) |
195 |
greg |
1.3 |
return(1); |
196 |
|
|
while (*s++); |
197 |
|
|
return(0); |
198 |
|
|
case '\\': /* literal next */ |
199 |
|
|
p++; |
200 |
|
|
/* fall through */ |
201 |
|
|
default: /* normal character */ |
202 |
|
|
if (*p != *s) |
203 |
|
|
return(0); |
204 |
|
|
s++; |
205 |
|
|
break; |
206 |
|
|
} |
207 |
|
|
} while (*p++); |
208 |
|
|
return(1); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
|
212 |
|
|
/* |
213 |
|
|
* Checkheader(fin,fmt,fout) returns a value of 1 if the input format |
214 |
|
|
* matches the specification in fmt, 0 if no input format was found, |
215 |
|
|
* and -1 if the input format does not match or there is an |
216 |
greg |
1.4 |
* error reading the header. If fmt is empty, then -1 is returned |
217 |
greg |
1.3 |
* if any input format is found (or there is an error), and 0 otherwise. |
218 |
|
|
* If fmt contains any '*' or '?' characters, then checkheader |
219 |
|
|
* does wildcard expansion and copies a matching result into fmt. |
220 |
greg |
2.7 |
* Be sure that fmt is big enough to hold the match in such cases, |
221 |
|
|
* and that it is not a static, read-only string! |
222 |
greg |
1.3 |
* The input header (minus any format lines) is copied to fout |
223 |
|
|
* if fout is not NULL. |
224 |
|
|
*/ |
225 |
|
|
|
226 |
greg |
2.5 |
int |
227 |
greg |
1.3 |
checkheader(fin, fmt, fout) |
228 |
greg |
1.2 |
FILE *fin; |
229 |
|
|
char *fmt; |
230 |
|
|
FILE *fout; |
231 |
greg |
1.1 |
{ |
232 |
greg |
1.2 |
struct check cdat; |
233 |
greg |
2.7 |
register char *cp; |
234 |
greg |
1.2 |
|
235 |
|
|
cdat.fp = fout; |
236 |
|
|
cdat.fs[0] = '\0'; |
237 |
|
|
if (getheader(fin, mycheck, &cdat) < 0) |
238 |
greg |
1.3 |
return(-1); |
239 |
greg |
2.6 |
if (!cdat.fs[0]) |
240 |
|
|
return(0); |
241 |
greg |
2.7 |
for (cp = fmt; *cp; cp++) /* check for globbing */ |
242 |
|
|
if (*cp == '?' | *cp == '*') |
243 |
|
|
if (globmatch(fmt, cdat.fs)) { |
244 |
|
|
strcpy(fmt, cdat.fs); |
245 |
|
|
return(1); |
246 |
|
|
} else |
247 |
|
|
return(-1); |
248 |
|
|
return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */ |
249 |
greg |
1.1 |
} |