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