| 1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1994 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 9 |
|
* |
| 10 |
|
* 8/19/88 |
| 11 |
|
* |
| 12 |
< |
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
| 12 |
> |
* 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 |
> |
* printargs(ac,av,fp) print an argument list to fp, followed by '\n' |
| 16 |
|
* 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 |
+ |
* globmatch(pat, str) check for glob match of str against pat |
| 21 |
|
* 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) |
| 26 |
|
#include <stdio.h> |
| 27 |
|
#include <ctype.h> |
| 28 |
|
|
| 29 |
< |
#define MAXLINE 512 |
| 29 |
> |
#define MAXLINE 512 |
| 30 |
|
|
| 31 |
|
#ifndef BSD |
| 32 |
< |
#define index strchr |
| 32 |
> |
#define index strchr |
| 33 |
|
#endif |
| 34 |
|
|
| 35 |
|
extern char *index(); |
| 36 |
|
|
| 37 |
< |
char FMTSTR[] = "FORMAT="; |
| 34 |
< |
int FMTSTRL = 7; |
| 37 |
> |
char HDRSTR[] = "#?"; /* information header magic number */ |
| 38 |
|
|
| 39 |
+ |
char FMTSTR[] = "FORMAT="; /* format identifier */ |
| 40 |
|
|
| 41 |
+ |
|
| 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 |
+ |
int |
| 53 |
+ |
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 |
+ |
while (*s) *r++ = *s++; |
| 61 |
+ |
*r = '\0'; |
| 62 |
+ |
return(1); |
| 63 |
+ |
} |
| 64 |
+ |
|
| 65 |
+ |
|
| 66 |
+ |
int |
| 67 |
+ |
isheadid(s) /* check to see if line is header id */ |
| 68 |
+ |
char *s; |
| 69 |
+ |
{ |
| 70 |
+ |
return(headidval(NULL, s)); |
| 71 |
+ |
} |
| 72 |
+ |
|
| 73 |
+ |
|
| 74 |
|
printargs(ac, av, fp) /* print arguments to a file */ |
| 75 |
|
int ac; |
| 76 |
|
char **av; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
|
| 98 |
< |
isformat(s) /* is line a format line? */ |
| 99 |
< |
char *s; |
| 63 |
< |
{ |
| 64 |
< |
return(!strncmp(s,FMTSTR,FMTSTRL)); |
| 65 |
< |
} |
| 66 |
< |
|
| 67 |
< |
|
| 68 |
< |
formatval(r, s) /* return format value */ |
| 98 |
> |
int |
| 99 |
> |
formatval(r, s) /* get format value (return true if format) */ |
| 100 |
|
register char *r; |
| 101 |
|
register char *s; |
| 102 |
|
{ |
| 103 |
< |
s += FMTSTRL; |
| 103 |
> |
register char *cp = FMTSTR; |
| 104 |
> |
|
| 105 |
> |
while (*cp) if (*cp++ != *s++) return(0); |
| 106 |
|
while (isspace(*s)) s++; |
| 107 |
< |
if (!*s) { *r = '\0'; return; } |
| 108 |
< |
while(*s) *r++ = *s++; |
| 109 |
< |
while (isspace(r[-1])) r--; |
| 107 |
> |
if (!*s) return(0); |
| 108 |
> |
if (r == NULL) return(1); |
| 109 |
> |
do |
| 110 |
> |
*r++ = *s++; |
| 111 |
> |
while(*s && !isspace(*s)); |
| 112 |
|
*r = '\0'; |
| 113 |
+ |
return(1); |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
|
| 117 |
+ |
int |
| 118 |
+ |
isformat(s) /* is line a format line? */ |
| 119 |
+ |
char *s; |
| 120 |
+ |
{ |
| 121 |
+ |
return(formatval(NULL, s)); |
| 122 |
+ |
} |
| 123 |
+ |
|
| 124 |
+ |
|
| 125 |
|
fputformat(s, fp) /* put out a format value */ |
| 126 |
|
char *s; |
| 127 |
|
FILE *fp; |
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
|
| 135 |
+ |
int |
| 136 |
|
getheader(fp, f, p) /* get header from file */ |
| 137 |
|
FILE *fp; |
| 138 |
|
int (*f)(); |
| 142 |
|
|
| 143 |
|
for ( ; ; ) { |
| 144 |
|
buf[MAXLINE-2] = '\n'; |
| 145 |
< |
if (fgets(buf, sizeof(buf), fp) == NULL) |
| 145 |
> |
if (fgets(buf, MAXLINE, fp) == NULL) |
| 146 |
|
return(-1); |
| 147 |
|
if (buf[0] == '\n') |
| 148 |
|
return(0); |
| 149 |
+ |
#ifdef MSDOS |
| 150 |
+ |
if (buf[0] == '\r' && buf[1] == '\n') |
| 151 |
+ |
return(0); |
| 152 |
+ |
#endif |
| 153 |
|
if (buf[MAXLINE-2] != '\n') { |
| 154 |
|
ungetc(buf[MAXLINE-2], fp); /* prevent false end */ |
| 155 |
|
buf[MAXLINE-2] = '\0'; |
| 171 |
|
char *s; |
| 172 |
|
register struct check *cp; |
| 173 |
|
{ |
| 174 |
< |
if (!strncmp(s,FMTSTR,FMTSTRL)) |
| 126 |
< |
formatval(cp->fs, s); |
| 127 |
< |
else if (cp->fp != NULL) /* don't copy format info. */ |
| 174 |
> |
if (!formatval(cp->fs, s) && cp->fp != NULL) |
| 175 |
|
fputs(s, cp->fp); |
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
|
| 179 |
< |
/* |
| 180 |
< |
* 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) |
| 179 |
> |
int |
| 180 |
> |
globmatch(pat, str) /* check for glob match of str against pat */ |
| 181 |
|
char *pat, *str; |
| 182 |
|
{ |
| 141 |
– |
int docopy = 0; |
| 183 |
|
register char *p = pat, *s = str; |
| 184 |
|
|
| 185 |
|
do { |
| 187 |
|
case '?': /* match any character */ |
| 188 |
|
if (!*s++) |
| 189 |
|
return(0); |
| 149 |
– |
docopy++; |
| 190 |
|
break; |
| 191 |
|
case '*': /* match any string */ |
| 192 |
|
while (p[1] == '*') p++; |
| 193 |
|
do |
| 194 |
< |
if ( (p[1]=='?' || p[1]==*s) |
| 195 |
< |
&& copymatch(p+1,s) ) { |
| 156 |
< |
strcpy(pat, str); |
| 194 |
> |
if ( (p[1]=='?' || p[1]==*s) && |
| 195 |
> |
globmatch(p+1,s) ) |
| 196 |
|
return(1); |
| 158 |
– |
} |
| 197 |
|
while (*s++); |
| 198 |
|
return(0); |
| 199 |
|
case '\\': /* literal next */ |
| 206 |
|
break; |
| 207 |
|
} |
| 208 |
|
} while (*p++); |
| 171 |
– |
if (docopy) |
| 172 |
– |
strcpy(pat, str); |
| 209 |
|
return(1); |
| 210 |
|
} |
| 175 |
– |
#else |
| 176 |
– |
#define copymatch(pat, s) (!strcmp(pat, s)) |
| 177 |
– |
#endif |
| 211 |
|
|
| 212 |
|
|
| 213 |
|
/* |
| 218 |
|
* if any input format is found (or there is an error), and 0 otherwise. |
| 219 |
|
* If fmt contains any '*' or '?' characters, then checkheader |
| 220 |
|
* does wildcard expansion and copies a matching result into fmt. |
| 221 |
< |
* Be sure that fmt is big enough to hold the match in such cases! |
| 221 |
> |
* Be sure that fmt is big enough to hold the match in such cases, |
| 222 |
> |
* and that it is not a static, read-only string! |
| 223 |
|
* The input header (minus any format lines) is copied to fout |
| 224 |
|
* if fout is not NULL. |
| 225 |
|
*/ |
| 226 |
|
|
| 227 |
+ |
int |
| 228 |
|
checkheader(fin, fmt, fout) |
| 229 |
|
FILE *fin; |
| 230 |
|
char *fmt; |
| 231 |
|
FILE *fout; |
| 232 |
|
{ |
| 233 |
|
struct check cdat; |
| 234 |
+ |
register char *cp; |
| 235 |
|
|
| 236 |
|
cdat.fp = fout; |
| 237 |
|
cdat.fs[0] = '\0'; |
| 238 |
|
if (getheader(fin, mycheck, &cdat) < 0) |
| 239 |
|
return(-1); |
| 240 |
< |
if (cdat.fs[0] != '\0') |
| 241 |
< |
return(copymatch(fmt, cdat.fs) ? 1 : -1); |
| 242 |
< |
return(0); |
| 240 |
> |
if (!cdat.fs[0]) |
| 241 |
> |
return(0); |
| 242 |
> |
for (cp = fmt; *cp; cp++) /* check for globbing */ |
| 243 |
> |
if (*cp == '?' | *cp == '*') |
| 244 |
> |
if (globmatch(fmt, cdat.fs)) { |
| 245 |
> |
strcpy(fmt, cdat.fs); |
| 246 |
> |
return(1); |
| 247 |
> |
} else |
| 248 |
> |
return(-1); |
| 249 |
> |
return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */ |
| 250 |
|
} |