1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
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 |
* |
12 |
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
13 |
* isformat(s) returns true if s is of the form "FORMAT=*" |
14 |
* formatval(r,s) copy the format value in s to r |
15 |
* fputformat(s,fp) write "FORMAT=%s" to fp |
16 |
* getheader(fp,f,p) read header from fp, calling f(s,p) on each line |
17 |
* checkheader(i,p,o) check header format from i against p and copy to o |
18 |
* |
19 |
* To copy header from input to output, use getheader(fin, fputs, fout) |
20 |
*/ |
21 |
|
22 |
#include <stdio.h> |
23 |
#include <ctype.h> |
24 |
|
25 |
#define MAXLINE 512 |
26 |
|
27 |
#ifndef BSD |
28 |
#define index strchr |
29 |
#endif |
30 |
|
31 |
extern char *index(); |
32 |
|
33 |
char FMTSTR[] = "FORMAT="; |
34 |
int FMTSTRL = 7; |
35 |
|
36 |
|
37 |
printargs(ac, av, fp) /* print arguments to a file */ |
38 |
int ac; |
39 |
char **av; |
40 |
register FILE *fp; |
41 |
{ |
42 |
int quote; |
43 |
|
44 |
while (ac-- > 0) { |
45 |
if (index(*av, ' ') != NULL) { /* quote it */ |
46 |
if (index(*av, '\'') != NULL) |
47 |
quote = '"'; |
48 |
else |
49 |
quote = '\''; |
50 |
putc(quote, fp); |
51 |
fputs(*av++, fp); |
52 |
putc(quote, fp); |
53 |
} else |
54 |
fputs(*av++, fp); |
55 |
putc(' ', fp); |
56 |
} |
57 |
putc('\n', fp); |
58 |
} |
59 |
|
60 |
|
61 |
isformat(s) /* is line a format line? */ |
62 |
char *s; |
63 |
{ |
64 |
return(!strncmp(s,FMTSTR,FMTSTRL)); |
65 |
} |
66 |
|
67 |
|
68 |
formatval(r, s) /* return format value */ |
69 |
register char *r; |
70 |
register char *s; |
71 |
{ |
72 |
s += FMTSTRL; |
73 |
while (isspace(*s)) s++; |
74 |
if (!*s) { *r = '\0'; return; } |
75 |
while(*s) *r++ = *s++; |
76 |
while (isspace(r[-1])) r--; |
77 |
*r = '\0'; |
78 |
} |
79 |
|
80 |
|
81 |
fputformat(s, fp) /* put out a format value */ |
82 |
char *s; |
83 |
FILE *fp; |
84 |
{ |
85 |
fputs(FMTSTR, fp); |
86 |
fputs(s, fp); |
87 |
putc('\n', fp); |
88 |
} |
89 |
|
90 |
|
91 |
getheader(fp, f, p) /* get header from file */ |
92 |
FILE *fp; |
93 |
int (*f)(); |
94 |
char *p; |
95 |
{ |
96 |
char buf[MAXLINE]; |
97 |
|
98 |
for ( ; ; ) { |
99 |
buf[MAXLINE-2] = '\n'; |
100 |
if (fgets(buf, sizeof(buf), fp) == NULL) |
101 |
return(-1); |
102 |
if (buf[0] == '\n') |
103 |
return(0); |
104 |
if (buf[MAXLINE-2] != '\n') { |
105 |
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
106 |
buf[MAXLINE-2] = '\0'; |
107 |
} |
108 |
if (f != NULL) |
109 |
(*f)(buf, p); |
110 |
} |
111 |
} |
112 |
|
113 |
|
114 |
struct check { |
115 |
FILE *fp; |
116 |
char fs[64]; |
117 |
}; |
118 |
|
119 |
|
120 |
static |
121 |
mycheck(s, cp) /* check a header line for format info. */ |
122 |
char *s; |
123 |
register struct check *cp; |
124 |
{ |
125 |
if (!strncmp(s,FMTSTR,FMTSTRL)) |
126 |
formatval(cp->fs, s); |
127 |
else if (cp->fp != NULL) /* don't copy format info. */ |
128 |
fputs(s, cp->fp); |
129 |
} |
130 |
|
131 |
|
132 |
/* |
133 |
* Copymatch(pat,str) checks pat for wildcards, and |
134 |
* copies str into pat if there is a match (returning true). |
135 |
*/ |
136 |
|
137 |
#ifdef COPYMATCH |
138 |
copymatch(pat, str) |
139 |
char *pat, *str; |
140 |
{ |
141 |
int docopy = 0; |
142 |
register char *p = pat, *s = str; |
143 |
|
144 |
do { |
145 |
switch (*p) { |
146 |
case '?': /* match any character */ |
147 |
if (!*s++) |
148 |
return(0); |
149 |
docopy++; |
150 |
break; |
151 |
case '*': /* match any string */ |
152 |
while (p[1] == '*') p++; |
153 |
do |
154 |
if ( (p[1]=='?' || p[1]==*s) |
155 |
&& copymatch(p+1,s) ) { |
156 |
strcpy(pat, str); |
157 |
return(1); |
158 |
} |
159 |
while (*s++); |
160 |
return(0); |
161 |
case '\\': /* literal next */ |
162 |
p++; |
163 |
/* fall through */ |
164 |
default: /* normal character */ |
165 |
if (*p != *s) |
166 |
return(0); |
167 |
s++; |
168 |
break; |
169 |
} |
170 |
} while (*p++); |
171 |
if (docopy) |
172 |
strcpy(pat, str); |
173 |
return(1); |
174 |
} |
175 |
#else |
176 |
#define copymatch(pat, s) (!strcmp(pat, s)) |
177 |
#endif |
178 |
|
179 |
|
180 |
/* |
181 |
* Checkheader(fin,fmt,fout) returns a value of 1 if the input format |
182 |
* matches the specification in fmt, 0 if no input format was found, |
183 |
* and -1 if the input format does not match or there is an |
184 |
* error reading the header. If fmt is empty, then -1 is returned |
185 |
* if any input format is found (or there is an error), and 0 otherwise. |
186 |
* If fmt contains any '*' or '?' characters, then checkheader |
187 |
* does wildcard expansion and copies a matching result into fmt. |
188 |
* Be sure that fmt is big enough to hold the match in such cases! |
189 |
* The input header (minus any format lines) is copied to fout |
190 |
* if fout is not NULL. |
191 |
*/ |
192 |
|
193 |
checkheader(fin, fmt, fout) |
194 |
FILE *fin; |
195 |
char *fmt; |
196 |
FILE *fout; |
197 |
{ |
198 |
struct check cdat; |
199 |
|
200 |
cdat.fp = fout; |
201 |
cdat.fs[0] = '\0'; |
202 |
if (getheader(fin, mycheck, &cdat) < 0) |
203 |
return(-1); |
204 |
if (cdat.fs[0] != '\0') |
205 |
return(copymatch(fmt, cdat.fs) ? 1 : -1); |
206 |
return(0); |
207 |
} |