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