| 1 | 
greg | 
1.4 | 
/* Copyright (c) 1991 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 | 
  | 
  | 
 *  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 | 
greg | 
1.1 | 
 */ | 
| 21 | 
  | 
  | 
 | 
| 22 | 
  | 
  | 
#include  <stdio.h> | 
| 23 | 
greg | 
1.3 | 
#include  <ctype.h> | 
| 24 | 
greg | 
1.1 | 
 | 
| 25 | 
greg | 
1.2 | 
#define  MAXLINE        512 | 
| 26 | 
greg | 
1.1 | 
 | 
| 27 | 
greg | 
1.2 | 
char  FMTSTR[] = "FORMAT="; | 
| 28 | 
  | 
  | 
int  FMTSTRL = 7; | 
| 29 | 
  | 
  | 
 | 
| 30 | 
  | 
  | 
 | 
| 31 | 
greg | 
1.1 | 
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 | 
greg | 
1.2 | 
isformat(s)                     /* is line a format line? */ | 
| 45 | 
  | 
  | 
char  *s; | 
| 46 | 
  | 
  | 
{ | 
| 47 | 
  | 
  | 
        return(!strncmp(s,FMTSTR,FMTSTRL)); | 
| 48 | 
  | 
  | 
} | 
| 49 | 
greg | 
1.1 | 
 | 
| 50 | 
greg | 
1.2 | 
 | 
| 51 | 
  | 
  | 
formatval(r, s)                 /* return format value */ | 
| 52 | 
  | 
  | 
register char  *r; | 
| 53 | 
  | 
  | 
register char  *s; | 
| 54 | 
  | 
  | 
{ | 
| 55 | 
  | 
  | 
        s += FMTSTRL; | 
| 56 | 
greg | 
1.3 | 
        while (isspace(*s)) s++; | 
| 57 | 
  | 
  | 
        if (!*s) { *r = '\0'; return; } | 
| 58 | 
  | 
  | 
        while(*s) *r++ = *s++; | 
| 59 | 
  | 
  | 
        while (isspace(r[-1])) r--; | 
| 60 | 
greg | 
1.2 | 
        *r = '\0'; | 
| 61 | 
  | 
  | 
} | 
| 62 | 
  | 
  | 
 | 
| 63 | 
  | 
  | 
 | 
| 64 | 
  | 
  | 
fputformat(s, fp)               /* put out a format value */ | 
| 65 | 
  | 
  | 
char  *s; | 
| 66 | 
greg | 
1.1 | 
FILE  *fp; | 
| 67 | 
greg | 
1.2 | 
{ | 
| 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 | 
greg | 
1.1 | 
int  (*f)(); | 
| 77 | 
greg | 
1.2 | 
char  *p; | 
| 78 | 
greg | 
1.1 | 
{ | 
| 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 | 
greg | 
1.2 | 
                        (*f)(buf, p); | 
| 93 | 
greg | 
1.1 | 
        } | 
| 94 | 
  | 
  | 
} | 
| 95 | 
  | 
  | 
 | 
| 96 | 
  | 
  | 
 | 
| 97 | 
greg | 
1.2 | 
struct check { | 
| 98 | 
  | 
  | 
        FILE    *fp; | 
| 99 | 
greg | 
1.3 | 
        char    fs[64]; | 
| 100 | 
greg | 
1.2 | 
}; | 
| 101 | 
greg | 
1.1 | 
 | 
| 102 | 
greg | 
1.2 | 
 | 
| 103 | 
greg | 
1.1 | 
static | 
| 104 | 
greg | 
1.2 | 
mycheck(s, cp)                  /* check a header line for format info. */ | 
| 105 | 
greg | 
1.1 | 
char  *s; | 
| 106 | 
greg | 
1.2 | 
register struct check  *cp; | 
| 107 | 
greg | 
1.1 | 
{ | 
| 108 | 
greg | 
1.2 | 
        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 | 
greg | 
1.1 | 
} | 
| 113 | 
  | 
  | 
 | 
| 114 | 
  | 
  | 
 | 
| 115 | 
greg | 
1.3 | 
/* | 
| 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 | 
greg | 
1.4 | 
                                if ( (p[1]=='?' || p[1]==*s) | 
| 138 | 
greg | 
1.3 | 
                                                && 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 | 
greg | 
1.4 | 
 * error reading the header.  If fmt is empty, then -1 is returned | 
| 168 | 
greg | 
1.3 | 
 * 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 | 
greg | 
1.2 | 
FILE  *fin; | 
| 178 | 
  | 
  | 
char  *fmt; | 
| 179 | 
  | 
  | 
FILE  *fout; | 
| 180 | 
greg | 
1.1 | 
{ | 
| 181 | 
greg | 
1.2 | 
        struct check    cdat; | 
| 182 | 
  | 
  | 
 | 
| 183 | 
  | 
  | 
        cdat.fp = fout; | 
| 184 | 
  | 
  | 
        cdat.fs[0] = '\0'; | 
| 185 | 
  | 
  | 
        if (getheader(fin, mycheck, &cdat) < 0) | 
| 186 | 
greg | 
1.3 | 
                return(-1); | 
| 187 | 
  | 
  | 
        if (cdat.fs[0] != '\0') | 
| 188 | 
  | 
  | 
                return(copymatch(fmt, cdat.fs) ? 1 : -1); | 
| 189 | 
greg | 
1.2 | 
        return(0); | 
| 190 | 
greg | 
1.1 | 
} |