| 1 | greg | 1.1 | #ifndef lint | 
| 2 | greg | 2.16 | static const char       RCSid[] = "$Id: header.c,v 2.15 2003/06/20 00:25:49 greg Exp $"; | 
| 3 | greg | 1.1 | #endif | 
| 4 |  |  | /* | 
| 5 |  |  | *  header.c - routines for reading and writing information headers. | 
| 6 |  |  | * | 
| 7 | greg | 2.11 | *  Externals declared in resolu.h | 
| 8 | greg | 1.3 | * | 
| 9 | greg | 2.4 | *  newheader(t,fp)     start new information header identified by string t | 
| 10 |  |  | *  isheadid(s)         returns true if s is a header id line | 
| 11 |  |  | *  headidval(r,s)      copy header identifier value in s to r | 
| 12 | greg | 2.11 | *  dateval(t,s)        get capture date value | 
| 13 |  |  | *  isdate(s)           returns true if s is a date line | 
| 14 |  |  | *  fputdate(t,fp)      put out the given capture date and time | 
| 15 |  |  | *  fputnow(fp)         put out the current date and time | 
| 16 | greg | 2.3 | *  printargs(ac,av,fp) print an argument list to fp, followed by '\n' | 
| 17 | greg | 1.3 | *  isformat(s)         returns true if s is of the form "FORMAT=*" | 
| 18 |  |  | *  formatval(r,s)      copy the format value in s to r | 
| 19 |  |  | *  fputformat(s,fp)    write "FORMAT=%s" to fp | 
| 20 |  |  | *  getheader(fp,f,p)   read header from fp, calling f(s,p) on each line | 
| 21 | greg | 2.6 | *  globmatch(pat, str) check for glob match of str against pat | 
| 22 | greg | 1.3 | *  checkheader(i,p,o)  check header format from i against p and copy to o | 
| 23 |  |  | * | 
| 24 |  |  | *  To copy header from input to output, use getheader(fin, fputs, fout) | 
| 25 | greg | 1.1 | */ | 
| 26 |  |  |  | 
| 27 | greg | 2.12 | #include "copyright.h" | 
| 28 | greg | 2.16 | #include "resolu.h" | 
| 29 | greg | 2.11 |  | 
| 30 | greg | 1.1 | #include  <stdio.h> | 
| 31 | greg | 2.11 | #include  <string.h> | 
| 32 |  |  | #include  <time.h> | 
| 33 | greg | 1.3 | #include  <ctype.h> | 
| 34 | schorsch | 2.14 |  | 
| 35 | greg | 2.3 | #define  MAXLINE        512 | 
| 36 | greg | 2.2 |  | 
| 37 | greg | 2.4 | char  HDRSTR[] = "#?";          /* information header magic number */ | 
| 38 | greg | 1.2 |  | 
| 39 | greg | 2.4 | char  FMTSTR[] = "FORMAT=";     /* format identifier */ | 
| 40 | greg | 1.2 |  | 
| 41 | greg | 2.11 | char  TMSTR[] = "CAPDATE=";     /* capture date identifier */ | 
| 42 |  |  |  | 
| 43 | greg | 2.16 | extern void     fputword(char *s, FILE *fp); | 
| 44 |  |  |  | 
| 45 | greg | 2.11 | static int mycheck(); | 
| 46 | greg | 2.4 |  | 
| 47 | greg | 2.11 |  | 
| 48 |  |  | void | 
| 49 | greg | 2.4 | newheader(s, fp)                /* identifying line of information header */ | 
| 50 |  |  | char  *s; | 
| 51 |  |  | register FILE  *fp; | 
| 52 |  |  | { | 
| 53 |  |  | fputs(HDRSTR, fp); | 
| 54 |  |  | fputs(s, fp); | 
| 55 |  |  | putc('\n', fp); | 
| 56 |  |  | } | 
| 57 |  |  |  | 
| 58 |  |  |  | 
| 59 | greg | 2.5 | int | 
| 60 | greg | 2.4 | headidval(r,s)                  /* get header id (return true if is id) */ | 
| 61 |  |  | register char  *r, *s; | 
| 62 |  |  | { | 
| 63 |  |  | register char  *cp = HDRSTR; | 
| 64 |  |  |  | 
| 65 |  |  | while (*cp) if (*cp++ != *s++) return(0); | 
| 66 |  |  | if (r == NULL) return(1); | 
| 67 | gregl | 2.9 | while (*s && !isspace(*s)) *r++ = *s++; | 
| 68 | greg | 2.4 | *r = '\0'; | 
| 69 |  |  | return(1); | 
| 70 |  |  | } | 
| 71 |  |  |  | 
| 72 |  |  |  | 
| 73 | greg | 2.5 | int | 
| 74 | greg | 2.4 | isheadid(s)                     /* check to see if line is header id */ | 
| 75 |  |  | char  *s; | 
| 76 |  |  | { | 
| 77 |  |  | return(headidval(NULL, s)); | 
| 78 |  |  | } | 
| 79 |  |  |  | 
| 80 |  |  |  | 
| 81 | greg | 2.11 | int | 
| 82 |  |  | dateval(tloc, s)                /* get capture date value */ | 
| 83 |  |  | time_t  *tloc; | 
| 84 |  |  | char    *s; | 
| 85 |  |  | { | 
| 86 |  |  | struct tm       tms; | 
| 87 |  |  | register char  *cp = TMSTR; | 
| 88 |  |  |  | 
| 89 |  |  | while (*cp) if (*cp++ != *s++) return(0); | 
| 90 |  |  | while (isspace(*s)) s++; | 
| 91 |  |  | if (!*s) return(0); | 
| 92 |  |  | if (sscanf(s, "%d:%d:%d %d:%d:%d", | 
| 93 |  |  | &tms.tm_year, &tms.tm_mon, &tms.tm_mday, | 
| 94 |  |  | &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) | 
| 95 |  |  | return(0); | 
| 96 |  |  | if (tloc == NULL) | 
| 97 |  |  | return(1); | 
| 98 |  |  | tms.tm_mon--; | 
| 99 |  |  | tms.tm_year -= 1900; | 
| 100 |  |  | tms.tm_isdst = -1;      /* ask mktime() to figure out DST */ | 
| 101 |  |  | *tloc = mktime(&tms); | 
| 102 |  |  | return(1); | 
| 103 |  |  | } | 
| 104 |  |  |  | 
| 105 |  |  |  | 
| 106 |  |  | int | 
| 107 |  |  | isdate(s)                       /* is the given line a capture date? */ | 
| 108 |  |  | char *s; | 
| 109 |  |  | { | 
| 110 |  |  | return(dateval(NULL, s)); | 
| 111 |  |  | } | 
| 112 |  |  |  | 
| 113 |  |  |  | 
| 114 |  |  | void | 
| 115 |  |  | fputdate(tv, fp)                /* write out the given time value */ | 
| 116 |  |  | time_t  tv; | 
| 117 |  |  | FILE    *fp; | 
| 118 |  |  | { | 
| 119 |  |  | struct tm       *tm = localtime(&tv); | 
| 120 |  |  | if (tm == NULL) | 
| 121 |  |  | return; | 
| 122 |  |  | fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, | 
| 123 |  |  | tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, | 
| 124 |  |  | tm->tm_hour, tm->tm_min, tm->tm_sec); | 
| 125 |  |  | } | 
| 126 |  |  |  | 
| 127 |  |  |  | 
| 128 |  |  | void | 
| 129 |  |  | fputnow(fp)                     /* write out the current time */ | 
| 130 |  |  | FILE    *fp; | 
| 131 |  |  | { | 
| 132 |  |  | time_t  tv; | 
| 133 |  |  | time(&tv); | 
| 134 |  |  | fputdate(tv, fp); | 
| 135 |  |  | } | 
| 136 |  |  |  | 
| 137 |  |  |  | 
| 138 |  |  | void | 
| 139 | greg | 1.1 | printargs(ac, av, fp)           /* print arguments to a file */ | 
| 140 |  |  | int  ac; | 
| 141 |  |  | char  **av; | 
| 142 | greg | 2.11 | FILE  *fp; | 
| 143 | greg | 1.1 | { | 
| 144 |  |  | while (ac-- > 0) { | 
| 145 | greg | 2.11 | fputword(*av++, fp); | 
| 146 |  |  | fputc(ac ? ' ' : '\n', fp); | 
| 147 | greg | 1.1 | } | 
| 148 |  |  | } | 
| 149 |  |  |  | 
| 150 |  |  |  | 
| 151 | greg | 2.5 | int | 
| 152 | greg | 2.4 | formatval(r, s)                 /* get format value (return true if format) */ | 
| 153 | greg | 1.2 | register char  *r; | 
| 154 |  |  | register char  *s; | 
| 155 |  |  | { | 
| 156 | greg | 2.4 | register char  *cp = FMTSTR; | 
| 157 |  |  |  | 
| 158 |  |  | while (*cp) if (*cp++ != *s++) return(0); | 
| 159 | greg | 1.3 | while (isspace(*s)) s++; | 
| 160 | greg | 2.4 | if (!*s) return(0); | 
| 161 |  |  | if (r == NULL) return(1); | 
| 162 | greg | 2.5 | do | 
| 163 |  |  | *r++ = *s++; | 
| 164 |  |  | while(*s && !isspace(*s)); | 
| 165 | greg | 1.2 | *r = '\0'; | 
| 166 | greg | 2.4 | return(1); | 
| 167 | greg | 1.2 | } | 
| 168 |  |  |  | 
| 169 |  |  |  | 
| 170 | greg | 2.5 | int | 
| 171 | greg | 2.4 | isformat(s)                     /* is line a format line? */ | 
| 172 |  |  | char  *s; | 
| 173 |  |  | { | 
| 174 |  |  | return(formatval(NULL, s)); | 
| 175 |  |  | } | 
| 176 |  |  |  | 
| 177 |  |  |  | 
| 178 | greg | 2.11 | void | 
| 179 | greg | 1.2 | fputformat(s, fp)               /* put out a format value */ | 
| 180 |  |  | char  *s; | 
| 181 | greg | 1.1 | FILE  *fp; | 
| 182 | greg | 1.2 | { | 
| 183 |  |  | fputs(FMTSTR, fp); | 
| 184 |  |  | fputs(s, fp); | 
| 185 |  |  | putc('\n', fp); | 
| 186 |  |  | } | 
| 187 |  |  |  | 
| 188 |  |  |  | 
| 189 | greg | 2.5 | int | 
| 190 | greg | 1.2 | getheader(fp, f, p)             /* get header from file */ | 
| 191 |  |  | FILE  *fp; | 
| 192 | greg | 1.1 | int  (*f)(); | 
| 193 | greg | 1.2 | char  *p; | 
| 194 | greg | 1.1 | { | 
| 195 |  |  | char  buf[MAXLINE]; | 
| 196 |  |  |  | 
| 197 |  |  | for ( ; ; ) { | 
| 198 |  |  | buf[MAXLINE-2] = '\n'; | 
| 199 | greg | 2.3 | if (fgets(buf, MAXLINE, fp) == NULL) | 
| 200 | greg | 1.1 | return(-1); | 
| 201 |  |  | if (buf[0] == '\n') | 
| 202 |  |  | return(0); | 
| 203 | greg | 2.3 | #ifdef MSDOS | 
| 204 |  |  | if (buf[0] == '\r' && buf[1] == '\n') | 
| 205 |  |  | return(0); | 
| 206 |  |  | #endif | 
| 207 | greg | 1.1 | if (buf[MAXLINE-2] != '\n') { | 
| 208 |  |  | ungetc(buf[MAXLINE-2], fp);     /* prevent false end */ | 
| 209 |  |  | buf[MAXLINE-2] = '\0'; | 
| 210 |  |  | } | 
| 211 | gwlarson | 2.10 | if (f != NULL && (*f)(buf, p) < 0) | 
| 212 |  |  | return(-1); | 
| 213 | greg | 1.1 | } | 
| 214 |  |  | } | 
| 215 |  |  |  | 
| 216 |  |  |  | 
| 217 | greg | 1.2 | struct check { | 
| 218 |  |  | FILE    *fp; | 
| 219 | greg | 1.3 | char    fs[64]; | 
| 220 | greg | 1.2 | }; | 
| 221 | greg | 1.1 |  | 
| 222 | greg | 1.2 |  | 
| 223 | greg | 2.11 | static int | 
| 224 | greg | 1.2 | mycheck(s, cp)                  /* check a header line for format info. */ | 
| 225 | greg | 1.1 | char  *s; | 
| 226 | greg | 1.2 | register struct check  *cp; | 
| 227 | greg | 1.1 | { | 
| 228 | greg | 2.4 | if (!formatval(cp->fs, s) && cp->fp != NULL) | 
| 229 | greg | 1.2 | fputs(s, cp->fp); | 
| 230 | greg | 2.11 | return(0); | 
| 231 | greg | 1.1 | } | 
| 232 |  |  |  | 
| 233 |  |  |  | 
| 234 | greg | 2.5 | int | 
| 235 | greg | 2.11 | globmatch(p, s)                 /* check for match of s against pattern p */ | 
| 236 |  |  | register char   *p, *s; | 
| 237 | greg | 1.3 | { | 
| 238 | greg | 2.11 | int     setmatch; | 
| 239 | greg | 1.3 |  | 
| 240 |  |  | do { | 
| 241 |  |  | switch (*p) { | 
| 242 |  |  | case '?':                       /* match any character */ | 
| 243 |  |  | if (!*s++) | 
| 244 |  |  | return(0); | 
| 245 |  |  | break; | 
| 246 |  |  | case '*':                       /* match any string */ | 
| 247 |  |  | while (p[1] == '*') p++; | 
| 248 |  |  | do | 
| 249 | greg | 2.6 | if ( (p[1]=='?' || p[1]==*s) && | 
| 250 |  |  | globmatch(p+1,s) ) | 
| 251 | greg | 1.3 | return(1); | 
| 252 |  |  | while (*s++); | 
| 253 |  |  | return(0); | 
| 254 | greg | 2.11 | case '[':                       /* character set */ | 
| 255 |  |  | setmatch = *s == *++p; | 
| 256 |  |  | if (!*p) | 
| 257 |  |  | return(0); | 
| 258 |  |  | while (*++p != ']') { | 
| 259 |  |  | if (!*p) | 
| 260 |  |  | return(0); | 
| 261 |  |  | if (*p == '-') { | 
| 262 |  |  | setmatch += p[-1] <= *s && *s <= p[1]; | 
| 263 |  |  | if (!*++p) | 
| 264 |  |  | break; | 
| 265 |  |  | } else | 
| 266 |  |  | setmatch += *p == *s; | 
| 267 |  |  | } | 
| 268 |  |  | if (!setmatch) | 
| 269 |  |  | return(0); | 
| 270 |  |  | s++; | 
| 271 |  |  | break; | 
| 272 | greg | 1.3 | case '\\':                      /* literal next */ | 
| 273 |  |  | p++; | 
| 274 |  |  | /* fall through */ | 
| 275 |  |  | default:                        /* normal character */ | 
| 276 |  |  | if (*p != *s) | 
| 277 |  |  | return(0); | 
| 278 |  |  | s++; | 
| 279 |  |  | break; | 
| 280 |  |  | } | 
| 281 |  |  | } while (*p++); | 
| 282 |  |  | return(1); | 
| 283 |  |  | } | 
| 284 |  |  |  | 
| 285 |  |  |  | 
| 286 |  |  | /* | 
| 287 |  |  | * Checkheader(fin,fmt,fout) returns a value of 1 if the input format | 
| 288 |  |  | * matches the specification in fmt, 0 if no input format was found, | 
| 289 |  |  | * and -1 if the input format does not match or there is an | 
| 290 | greg | 1.4 | * error reading the header.  If fmt is empty, then -1 is returned | 
| 291 | greg | 1.3 | * if any input format is found (or there is an error), and 0 otherwise. | 
| 292 |  |  | * If fmt contains any '*' or '?' characters, then checkheader | 
| 293 |  |  | * does wildcard expansion and copies a matching result into fmt. | 
| 294 | greg | 2.7 | * Be sure that fmt is big enough to hold the match in such cases, | 
| 295 |  |  | * and that it is not a static, read-only string! | 
| 296 | greg | 1.3 | * The input header (minus any format lines) is copied to fout | 
| 297 |  |  | * if fout is not NULL. | 
| 298 |  |  | */ | 
| 299 |  |  |  | 
| 300 | greg | 2.5 | int | 
| 301 | greg | 1.3 | checkheader(fin, fmt, fout) | 
| 302 | greg | 1.2 | FILE  *fin; | 
| 303 |  |  | char  *fmt; | 
| 304 |  |  | FILE  *fout; | 
| 305 | greg | 1.1 | { | 
| 306 | greg | 1.2 | struct check    cdat; | 
| 307 | greg | 2.7 | register char   *cp; | 
| 308 | greg | 1.2 |  | 
| 309 |  |  | cdat.fp = fout; | 
| 310 |  |  | cdat.fs[0] = '\0'; | 
| 311 | greg | 2.16 | if (getheader(fin, mycheck, (char *)&cdat) < 0) | 
| 312 | greg | 1.3 | return(-1); | 
| 313 | greg | 2.6 | if (!cdat.fs[0]) | 
| 314 |  |  | return(0); | 
| 315 | greg | 2.7 | for (cp = fmt; *cp; cp++)               /* check for globbing */ | 
| 316 |  |  | if (*cp == '?' | *cp == '*') | 
| 317 |  |  | if (globmatch(fmt, cdat.fs)) { | 
| 318 |  |  | strcpy(fmt, cdat.fs); | 
| 319 |  |  | return(1); | 
| 320 |  |  | } else | 
| 321 |  |  | return(-1); | 
| 322 |  |  | return(strcmp(fmt, cdat.fs) ? -1 : 1);  /* literal match */ | 
| 323 | greg | 1.1 | } |