]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
2 * CGI template function.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
14 #include "cgi-private.h"
23 static void cgi_copy(FILE *out
, FILE *in
, int element
, char term
,
25 static void cgi_puts(const char *s
, FILE *out
);
26 static void cgi_puturi(const char *s
, FILE *out
);
30 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
31 * '{variable}' strings with the variable value.
35 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
36 const char *tmpl
) /* I - Template file to read */
38 FILE *in
; /* Input file */
41 fprintf(stderr
, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out
,
42 tmpl
? tmpl
: "(null)");
45 * Range check input...
52 * Open the template file...
55 if ((in
= fopen(tmpl
, "r")) == NULL
)
57 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
58 tmpl
? tmpl
: "(null)", strerror(errno
));
63 * Parse the file to the end...
66 cgi_copy(out
, in
, 0, 0, 0);
69 * Close the template file and return...
77 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
81 cgiCopyTemplateLang(const char *tmpl
) /* I - Base filename */
83 char filename
[1024], /* Filename */
84 locale
[16], /* Locale name */
85 *locptr
; /* Pointer into locale name */
86 const char *directory
, /* Directory for templates */
88 FILE *in
; /* Input file */
91 fprintf(stderr
, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n",
92 tmpl
? tmpl
: "(null)");
95 * Convert the language to a locale name...
100 if ((lang
= getenv("LANG")) != NULL
)
103 strlcpy(locale
+ 1, lang
, sizeof(locale
) - 1);
105 if ((locptr
= strchr(locale
, '.')) != NULL
)
106 *locptr
= '\0'; /* Strip charset */
109 fprintf(stderr
, "DEBUG2: lang=\"%s\", locale=\"%s\"...\n",
110 lang
? lang
: "(null)", locale
);
113 * See if we have a template file for this language...
116 directory
= cgiGetTemplateDir();
118 snprintf(filename
, sizeof(filename
), "%s%s/%s", directory
, locale
, tmpl
);
119 if ((in
= fopen(filename
, "r")) == NULL
)
123 snprintf(filename
, sizeof(filename
), "%s%s/%s", directory
, locale
, tmpl
);
124 if ((in
= fopen(filename
, "r")) == NULL
)
126 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
127 in
= fopen(filename
, "r");
131 fprintf(stderr
, "DEBUG2: Template file is \"%s\"...\n", filename
);
134 * Open the template file...
139 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
140 filename
, strerror(errno
));
145 * Parse the file to the end...
148 cgi_copy(stdout
, in
, 0, 0, 0);
151 * Close the template file and return...
159 * 'cgiGetTemplateDir()' - Get the templates directory...
162 char * /* O - Template directory */
163 cgiGetTemplateDir(void)
165 const char *datadir
; /* CUPS_DATADIR env var */
166 static char templates
[1024] = ""; /* Template directory */
172 * Build the template directory pathname...
175 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
176 datadir
= CUPS_DATADIR
;
178 snprintf(templates
, sizeof(templates
), "%s/templates", datadir
);
186 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
190 cgiSetServerVersion(void)
192 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
193 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
194 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION
);
197 setlocale(LC_TIME
, "");
203 * 'cgi_copy()' - Copy the template file, substituting as needed...
207 cgi_copy(FILE *out
, /* I - Output file */
208 FILE *in
, /* I - Input file */
209 int element
, /* I - Element number (0 to N) */
210 char term
, /* I - Terminating character */
211 int indent
) /* I - Debug info indentation */
213 int ch
; /* Character from file */
214 char op
; /* Operation */
215 char name
[255], /* Name of variable */
216 *nameptr
, /* Pointer into name */
217 innername
[255], /* Inner comparison name */
218 *innerptr
, /* Pointer into inner name */
219 *s
; /* String pointer */
220 const char *value
; /* Value of variable */
221 const char *innerval
; /* Inner value */
222 const char *outptr
; /* Output string pointer */
223 char outval
[1024], /* Formatted output string */
224 compare
[1024]; /* Comparison string */
225 int result
; /* Result of comparison */
226 int uriencode
; /* Encode as URI */
227 regex_t re
; /* Regular expression to match */
230 fprintf(stderr
, "DEBUG2: %*sStarting at file position %ld...\n", indent
, "",
234 * Parse the file to the end...
237 while ((ch
= getc(in
)) != EOF
)
243 * Get a variable name...
248 for (s
= name
; (ch
= getc(in
)) != EOF
;)
249 if (strchr("}]<>=!~ \t\n", ch
))
251 else if (s
== name
&& ch
== '%')
253 else if (s
> name
&& ch
== '?')
255 else if (s
< (name
+ sizeof(name
) - 1))
260 if (s
== name
&& isspace(ch
& 255))
262 fprintf(stderr
, "DEBUG2: %*sLone { at %ld...\n", indent
, "", ftell(in
));
274 fprintf(stderr
, "DEBUG2: %*s\"{%s}\" at %ld...\n", indent
, "", name
,
278 * See if it has a value...
284 * Insert value only if it exists...
287 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
291 if ((value
= cgiGetArray(name
+ 1, atoi(nameptr
) - 1)) != NULL
)
299 else if ((value
= cgiGetArray(name
+ 1, element
)) != NULL
)
307 else if (name
[0] == '#')
314 sprintf(outval
, "%d", cgiGetSize(name
+ 1));
316 sprintf(outval
, "%d", element
+ 1);
320 else if (name
[0] == '[')
323 * Loop for # of elements...
326 int i
; /* Looping var */
327 long pos
; /* File position */
328 int count
; /* Number of elements */
331 if (isdigit(name
[1] & 255))
332 count
= atoi(name
+ 1);
334 count
= cgiGetSize(name
+ 1);
338 fprintf(stderr
, "DEBUG2: %*sLooping on \"%s\" at %ld, count=%d...\n",
339 indent
, "", name
+ 1, pos
, count
);
343 for (i
= 0; i
< count
; i
++)
346 fseek(in
, pos
, SEEK_SET
);
348 cgi_copy(out
, in
, i
, '}', indent
+ 2);
352 cgi_copy(NULL
, in
, 0, '}', indent
+ 2);
354 fprintf(stderr
, "DEBUG2: %*sFinished looping on \"%s\"...\n", indent
,
359 else if (name
[0] == '$')
362 * Insert cookie value or nothing if not defined.
365 if ((value
= cgiGetCookie(name
+ 1)) != NULL
)
376 * Insert variable or variable name (if element is NULL)...
379 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
382 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
384 snprintf(outval
, sizeof(outval
), "{%s}", name
);
390 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
392 snprintf(outval
, sizeof(outval
), "{%s}", name
);
400 * See if the terminating character requires another test...
406 * End of substitution...
412 cgi_puturi(outptr
, out
);
413 else if (!_cups_strcasecmp(name
, "?cupsdconf_default"))
414 fputs(outptr
, stdout
);
416 cgi_puts(outptr
, out
);
423 * OK, process one of the following checks:
425 * {name?exist:not-exist} Exists?
426 * {name=value?true:false} Equal
427 * {name<value?true:false} Less than
428 * {name>value?true:false} Greater than
429 * {name!value?true:false} Not equal
430 * {name~refex?true:false} Regex match
438 * Test for existance...
442 result
= cgiGetArray(name
+ 1, element
) != NULL
;
443 else if (name
[0] == '#')
444 result
= cgiGetVariable(name
+ 1) != NULL
;
446 result
= cgiGetArray(name
, element
) != NULL
;
448 result
= result
&& outptr
[0];
454 * Compare to a string...
457 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
460 else if (s
>= (compare
+ sizeof(compare
) - 1))
464 sprintf(s
, "%d", element
+ 1);
470 * Grab the value of a variable...
473 innerptr
= innername
;
474 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
475 if (innerptr
< (innername
+ sizeof(innername
) - 1))
476 *innerptr
++ = (char)ch
;
479 if (innername
[0] == '#')
480 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
481 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
482 isdigit(innerptr
[1] & 255))
485 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
488 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
490 else if (innername
[0] == '?')
492 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
495 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
497 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
498 snprintf(s
, sizeof(compare
) - (size_t)(s
- compare
), "{%s}", innername
);
500 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
505 *s
++ = (char)getc(in
);
514 "DEBUG2: %*sBad terminator '%c' at file position %ld...\n",
515 indent
, "", ch
, ftell(in
));
520 * Do the comparison...
526 result
= _cups_strcasecmp(outptr
, compare
) < 0;
529 result
= _cups_strcasecmp(outptr
, compare
) > 0;
532 result
= _cups_strcasecmp(outptr
, compare
) == 0;
535 result
= _cups_strcasecmp(outptr
, compare
) != 0;
538 fprintf(stderr
, "DEBUG: Regular expression \"%s\"\n", compare
);
540 if (regcomp(&re
, compare
, REG_EXTENDED
| REG_ICASE
))
543 "ERROR: Unable to compile regular expression \"%s\"!\n",
549 regmatch_t matches
[10];
553 if (!regexec(&re
, outptr
, 10, matches
, 0))
556 for (i
= 0; i
< 10; i
++)
558 fprintf(stderr
, "DEBUG: matches[%d].rm_so=%d\n", i
,
559 (int)matches
[i
].rm_so
);
560 if (matches
[i
].rm_so
< 0)
577 "DEBUG2: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
578 indent
, "", name
, op
, compare
, ftell(in
), result
);
583 * Comparison true; output first part and ignore second...
586 fprintf(stderr
, "DEBUG2: %*sOutput first part...\n", indent
, "");
587 cgi_copy(out
, in
, element
, ':', indent
+ 2);
589 fprintf(stderr
, "DEBUG2: %*sSkip second part...\n", indent
, "");
590 cgi_copy(NULL
, in
, element
, '}', indent
+ 2);
595 * Comparison false; ignore first part and output second...
598 fprintf(stderr
, "DEBUG2: %*sSkip first part...\n", indent
, "");
599 cgi_copy(NULL
, in
, element
, ':', indent
+ 2);
601 fprintf(stderr
, "DEBUG2: %*sOutput second part...\n", indent
, "");
602 cgi_copy(out
, in
, element
, '}', indent
+ 2);
605 fprintf(stderr
, "DEBUG2: %*sFinished \"{%s%c%s\", out=%p...\n", indent
, "",
606 name
, op
, compare
, out
);
608 else if (ch
== '\\') /* Quoted char */
619 fprintf(stderr
, "DEBUG2: %*sReturning at file position %ld on EOF...\n",
620 indent
, "", ftell(in
));
623 "DEBUG2: %*sReturning at file position %ld on character '%c'...\n",
624 indent
, "", ftell(in
), ch
);
626 if (ch
== EOF
&& term
)
627 fprintf(stderr
, "ERROR: %*sSaw EOF, expected '%c'!\n", indent
, "", term
);
630 * Flush any pending output...
639 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
643 cgi_puts(const char *s
, /* I - String to output */
644 FILE *out
) /* I - Output file */
653 fputs(""", out
);
667 * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
671 cgi_puturi(const char *s
, /* I - String to output */
672 FILE *out
) /* I - Output file */
676 if (strchr("%@&+ <>#=", *s
) || *s
< ' ' || *s
& 128)
677 fprintf(out
, "%%%02X", *s
& 255);