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