ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
Revision: 2.2
Committed: Thu Feb 20 11:40:36 1992 UTC (32 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +19 -2 lines
Log Message:
added quoting of arguments containing spaces to printargs()

File Contents

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