]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
2 * "$Id: template.c 5113 2006-02-16 12:02:44Z mike $"
4 * CGI template function.
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cgiCopyTemplateFile() - Copy a template file and replace all the
27 * '{variable}' strings with the variable value.
28 * cgiCopyTemplateLang() - Copy a template file using a language...
29 * cgiGetTemplateDir() - Get the templates directory...
30 * cgiSetServerVersion() - Set the server name and CUPS version...
31 * cgi_copy() - Copy the template file, substituting as needed...
32 * cgi_puts() - Put a string to the output file, quoting as
36 #include "cgi-private.h"
44 static void cgi_copy(FILE *out
, FILE *in
, int element
, char term
,
46 static void cgi_puts(const char *s
, FILE *out
);
50 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
51 * '{variable}' strings with the variable value.
55 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
56 const char *tmpl
) /* I - Template file to read */
58 FILE *in
; /* Input file */
61 fprintf(stderr
, "DEBUG: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out
,
65 * Open the template file...
68 if ((in
= fopen(tmpl
, "r")) == NULL
)
70 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
71 tmpl
, strerror(errno
));
76 * Parse the file to the end...
79 cgi_copy(out
, in
, 0, 0, 0);
82 * Close the template file and return...
90 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
94 cgiCopyTemplateLang(const char *tmpl
) /* I - Base filename */
96 int i
; /* Looping var */
97 char filename
[1024], /* Filename */
98 locale
[16]; /* Locale name */
99 const char *directory
, /* Directory for templates */
100 *lang
; /* Language */
101 FILE *in
; /* Input file */
104 fprintf(stderr
, "DEBUG: cgiCopyTemplateLang(tmpl=\"%s\")\n", tmpl
);
107 * Convert the language to a locale name...
110 if ((lang
= getenv("LANG")) != NULL
)
112 for (i
= 0; lang
[i
] && i
< 15; i
++)
113 if (isalnum(lang
[i
] & 255))
114 locale
[i
] = tolower(lang
[i
]);
115 else if (lang
[i
] == '-')
125 fprintf(stderr
, "DEBUG: locale=\"%s\"...\n", locale
);
128 * See if we have a template file for this language...
131 directory
= cgiGetTemplateDir();
133 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
134 if (access(filename
, 0))
138 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
139 if (access(filename
, 0))
140 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
143 fprintf(stderr
, "DEBUG: Template file is \"%s\"...\n", filename
);
146 * Open the template file...
149 if ((in
= fopen(filename
, "r")) == NULL
)
151 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
152 filename
, strerror(errno
));
157 * Parse the file to the end...
160 cgi_copy(stdout
, in
, 0, 0, 0);
163 * Close the template file and return...
171 * 'cgiGetTemplateDir()' - Get the templates directory...
174 char * /* O - Template directory */
175 cgiGetTemplateDir(void)
177 const char *datadir
; /* CUPS_DATADIR env var */
178 static char templates
[1024] = ""; /* Template directory */
184 * Build the template directory pathname...
187 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
188 datadir
= CUPS_DATADIR
;
190 snprintf(templates
, sizeof(templates
), "%s/templates", datadir
);
198 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
202 cgiSetServerVersion(void)
204 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
205 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
206 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION
);
209 setlocale(LC_TIME
, "");
215 * 'cgi_copy()' - Copy the template file, substituting as needed...
219 cgi_copy(FILE *out
, /* I - Output file */
220 FILE *in
, /* I - Input file */
221 int element
, /* I - Element number (0 to N) */
222 char term
, /* I - Terminating character */
223 int indent
) /* I - Debug info indentation */
225 int ch
; /* Character from file */
226 char op
; /* Operation */
227 char name
[255], /* Name of variable */
228 *nameptr
, /* Pointer into name */
229 innername
[255], /* Inner comparison name */
230 *innerptr
, /* Pointer into inner name */
231 *s
; /* String pointer */
232 const char *value
; /* Value of variable */
233 const char *innerval
; /* Inner value */
234 const char *outptr
; /* Output string pointer */
235 char outval
[1024], /* Formatted output string */
236 compare
[1024]; /* Comparison string */
237 int result
; /* Result of comparison */
240 fprintf(stderr
, "DEBUG: %*sStarting at file position %ld...\n", indent
, "",
244 * Parse the file to the end...
247 while ((ch
= getc(in
)) != EOF
)
253 * Get a variable name...
256 for (s
= name
; (ch
= getc(in
)) != EOF
;)
257 if (strchr("}]<>=! \t\n", ch
))
259 else if (s
> name
&& ch
== '?')
261 else if (s
< (name
+ sizeof(name
) - 1))
266 if (s
== name
&& isspace(ch
& 255))
268 fprintf(stderr
, "DEBUG: %*sLone { at %ld...\n", indent
, "", ftell(in
));
280 fprintf(stderr
, "DEBUG: %*s\"{%s}\" at %ld...\n", indent
, "", name
,
284 * See if it has a value...
290 * Insert value only if it exists...
293 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
297 if ((value
= cgiGetArray(name
+ 1, atoi(nameptr
) - 1)) != NULL
)
305 else if ((value
= cgiGetArray(name
+ 1, element
)) != NULL
)
313 else if (name
[0] == '#')
320 sprintf(outval
, "%d", cgiGetSize(name
+ 1));
322 sprintf(outval
, "%d", element
+ 1);
326 else if (name
[0] == '[')
329 * Loop for # of elements...
332 int i
; /* Looping var */
333 long pos
; /* File position */
334 int count
; /* Number of elements */
337 if (isdigit(name
[1] & 255))
338 count
= atoi(name
+ 1);
340 count
= cgiGetSize(name
+ 1);
344 fprintf(stderr
, "DEBUG: %*sLooping on \"%s\" at %ld, count=%d...\n",
345 indent
, "", name
+ 1, pos
, count
);
349 for (i
= 0; i
< count
; i
++)
352 fseek(in
, pos
, SEEK_SET
);
354 cgi_copy(out
, in
, i
, '}', indent
+ 2);
358 cgi_copy(NULL
, in
, 0, '}', indent
+ 2);
360 fprintf(stderr
, "DEBUG: %*sFinished looping on \"%s\"...\n", indent
,
368 * Insert variable or variable name (if element is NULL)...
371 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
374 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
376 snprintf(outval
, sizeof(outval
), "{%s}", name
);
382 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
384 snprintf(outval
, sizeof(outval
), "{%s}", name
);
392 * See if the terminating character requires another test...
398 * End of substitution...
402 cgi_puts(outptr
, out
);
408 * OK, process one of the following checks:
410 * {name?exist:not-exist} Exists?
411 * {name=value?true:false} Equal
412 * {name<value?true:false} Less than
413 * {name>value?true:false} Greater than
414 * {name!value?true:false} Not equal
422 * Test for existance...
425 result
= cgiGetArray(name
, element
) != NULL
&& outptr
[0];
431 * Compare to a string...
434 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
437 else if (s
>= (compare
+ sizeof(compare
) - 1))
441 sprintf(s
, "%d", element
+ 1);
447 * Grab the value of a variable...
450 innerptr
= innername
;
451 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
452 if (innerptr
< (innername
+ sizeof(innername
) - 1))
456 if (innername
[0] == '#')
457 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
458 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
459 isdigit(innerptr
[1] & 255))
462 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
465 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
467 else if (innername
[0] == '?')
469 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
472 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
474 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
475 snprintf(s
, sizeof(compare
) - (s
- compare
), "{%s}", innername
);
477 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
491 "DEBUG: %*sBad terminator '%c' at file position %ld...\n",
492 indent
, "", ch
, ftell(in
));
497 * Do the comparison...
503 result
= strcasecmp(outptr
, compare
) < 0;
506 result
= strcasecmp(outptr
, compare
) > 0;
509 result
= strcasecmp(outptr
, compare
) == 0;
512 result
= strcasecmp(outptr
, compare
) != 0;
521 "DEBUG: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
522 indent
, "", name
, op
, compare
, ftell(in
), result
);
527 * Comparison true; output first part and ignore second...
530 fprintf(stderr
, "DEBUG: %*sOutput first part...\n", indent
, "");
531 cgi_copy(out
, in
, element
, ':', indent
+ 2);
533 fprintf(stderr
, "DEBUG: %*sSkip second part...\n", indent
, "");
534 cgi_copy(NULL
, in
, element
, '}', indent
+ 2);
539 * Comparison false; ignore first part and output second...
542 fprintf(stderr
, "DEBUG: %*sSkip first part...\n", indent
, "");
543 cgi_copy(NULL
, in
, element
, ':', indent
+ 2);
545 fprintf(stderr
, "DEBUG: %*sOutput second part...\n", indent
, "");
546 cgi_copy(out
, in
, element
, '}', indent
+ 2);
549 fprintf(stderr
, "DEBUG: %*sFinished \"{%s%c%s\", out=%p...\n", indent
, "",
550 name
, op
, compare
, out
);
552 else if (ch
== '\\') /* Quoted char */
563 fprintf(stderr
, "DEBUG: %*sReturning at file position %ld on EOF...\n",
564 indent
, "", ftell(in
));
567 "DEBUG: %*sReturning at file position %ld on character '%c'...\n",
568 indent
, "", ftell(in
), ch
);
570 if (ch
== EOF
&& term
)
571 fprintf(stderr
, "ERROR: %*sSaw EOF, expected '%c'!\n", indent
, "", term
);
574 * Flush any pending output...
583 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
587 cgi_puts(const char *s
, /* I - String to output */
588 FILE *out
) /* I - Output file */
595 * Pass <A HREF="url"> and </A>, otherwise quote it...
598 if (!strncasecmp(s
, "<A HREF=\"", 9))
600 fputs("<A HREF=\"", out
);
603 while (*s
&& *s
!= '\"')
618 else if (!strncasecmp(s
, "</A>", 4))
629 fputs(""", out
);
641 * End of "$Id: template.c 5113 2006-02-16 12:02:44Z mike $".