]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
3f9e14a2640bc2c3cae57562196c8dcbb9b0ef0f
2 * "$Id: template.c,v 1.22.2.5 2003/07/20 15:41:18 mike Exp $"
4 * CGI template function.
6 * Copyright 1997-2003 by Easy Software Products.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * cgiCopyTemplateFile() - Copy a template file and replace all the
25 * '{variable}' strings with the variable value.
26 * cgiCopyTemplateLang() - Copy a template file using a language...
27 * cgi_copy() - Copy the template file, substituting as needed...
28 * cgi_puts() - Put a string to the output file, quoting as
39 static void cgi_copy(FILE *out
, FILE *in
, int element
, char term
);
40 static void cgi_puts(const char *s
, FILE *out
);
44 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
45 * '{variable}' strings with the variable value.
49 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
50 const char *tmpl
) /* I - Template file to read */
52 FILE *in
; /* Input file */
56 * Open the template file...
59 if ((in
= fopen(tmpl
, "r")) == NULL
)
63 * Parse the file to the end...
66 cgi_copy(out
, in
, 0, 0);
69 * Close the template file and return...
77 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
81 cgiCopyTemplateLang(FILE *out
, /* I - Output file */
82 const char *directory
, /* I - Directory */
83 const char *tmpl
, /* I - Base filename */
84 const char *lang
) /* I - Language */
86 int i
; /* Looping var */
87 char filename
[1024], /* Filename */
88 locale
[16]; /* Locale name */
89 FILE *in
; /* Input file */
93 * Convert the language to a locale name...
98 for (i
= 0; lang
[i
] && i
< 15; i
++)
100 locale
[i
] = tolower(lang
[i
]);
110 * See if we have a template file for this language...
113 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
114 if (access(filename
, 0))
118 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
119 if (access(filename
, 0))
120 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
124 * Open the template file...
127 if ((in
= fopen(filename
, "r")) == NULL
)
131 * Parse the file to the end...
134 cgi_copy(out
, in
, 0, 0);
137 * Close the template file and return...
145 * 'cgi_copy()' - Copy the template file, substituting as needed...
149 cgi_copy(FILE *out
, /* I - Output file */
150 FILE *in
, /* I - Input file */
151 int element
, /* I - Element number (0 to N) */
152 char term
) /* I - Terminating character */
154 int ch
; /* Character from file */
155 char op
; /* Operation */
156 char name
[255], /* Name of variable */
157 *nameptr
, /* Pointer into name */
158 innername
[255], /* Inner comparison name */
159 *innerptr
, /* Pointer into inner name */
160 *s
; /* String pointer */
161 const char *value
; /* Value of variable */
162 const char *innerval
; /* Inner value */
163 const char *outptr
; /* Output string pointer */
164 char outval
[1024], /* Formatted output string */
165 compare
[1024]; /* Comparison string */
166 int result
; /* Result of comparison */
170 * Parse the file to the end...
173 while ((ch
= getc(in
)) != EOF
)
179 * Get a variable name...
182 for (s
= name
; (ch
= getc(in
)) != EOF
;)
183 if (strchr("}]<>=! \t\n", ch
))
185 else if (s
> name
&& ch
== '?')
187 else if (s
< (name
+ sizeof(name
) - 1))
192 if (s
== name
&& isspace(ch
))
204 * See if it has a value...
210 * Insert value only if it exists...
213 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1]))
217 if ((value
= cgiGetArray(name
+ 1, atoi(nameptr
) - 1)) != NULL
)
225 else if ((value
= cgiGetArray(name
+ 1, element
)) != NULL
)
233 else if (name
[0] == '#')
240 sprintf(outval
, "%d", cgiGetSize(name
+ 1));
242 sprintf(outval
, "%d", element
+ 1);
246 else if (name
[0] == '[')
249 * Loop for # of elements...
252 int i
; /* Looping var */
253 long pos
; /* File position */
254 int count
; /* Number of elements */
257 if (isdigit(name
[1]))
258 count
= atoi(name
+ 1);
260 count
= cgiGetSize(name
+ 1);
266 for (i
= 0; i
< count
; i
++)
268 fseek(in
, pos
, SEEK_SET
);
269 cgi_copy(out
, in
, i
, '}');
273 cgi_copy(NULL
, in
, 0, '}');
280 * Insert variable or variable name (if element is NULL)...
283 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1]))
286 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
288 snprintf(outval
, sizeof(outval
), "{%s}", name
);
294 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
296 snprintf(outval
, sizeof(outval
), "{%s}", name
);
304 * See if the terminating character requires another test...
310 * End of substitution...
314 cgi_puts(outptr
, out
);
320 * OK, process one of the following checks:
322 * {name?exist:not-exist} Exists?
323 * {name=value?true:false} Equal
324 * {name<value?true:false} Less than
325 * {name>value?true:false} Greater than
326 * {name!value?true:false} Not equal
332 * Test for existance...
335 result
= cgiGetArray(name
, element
) != NULL
&& outptr
[0];
340 * Compare to a string...
345 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
348 else if (s
>= (compare
+ sizeof(compare
) - 1))
352 sprintf(s
, "%d", element
+ 1);
358 * Grab the value of a variable...
361 innerptr
= innername
;
362 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
363 if (innerptr
< (innername
+ sizeof(innername
) - 1))
367 if (innername
[0] == '#')
368 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
369 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
370 isdigit(innerptr
[1]))
373 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
376 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
378 else if (innername
[0] == '?')
380 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
383 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
385 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
386 snprintf(s
, sizeof(s
), "{%s}", innername
);
388 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
403 * Do the comparison...
409 result
= strcasecmp(outptr
, compare
) < 0;
412 result
= strcasecmp(outptr
, compare
) > 0;
415 result
= strcasecmp(outptr
, compare
) == 0;
418 result
= strcasecmp(outptr
, compare
) != 0;
429 * Comparison true; output first part and ignore second...
432 cgi_copy(out
, in
, element
, ':');
433 cgi_copy(NULL
, in
, element
, '}');
438 * Comparison false; ignore first part and output second...
441 cgi_copy(NULL
, in
, element
, ':');
442 cgi_copy(out
, in
, element
, '}');
445 else if (ch
== '\\') /* Quoted char */
456 * Flush any pending output...
465 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
469 cgi_puts(const char *s
,
476 else if (s
[0] == '>')
479 fputs(""", out
);
480 else if (s
[0] == '&')
491 * End of "$Id: template.c,v 1.22.2.5 2003/07/20 15:41:18 mike Exp $".