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