]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
4 * CGI template function.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
16 #include "cgi-private.h"
25 static void cgi_copy(FILE *out
, FILE *in
, int element
, char term
,
27 static void cgi_puts(const char *s
, FILE *out
);
28 static void cgi_puturi(const char *s
, FILE *out
);
32 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
33 * '{variable}' strings with the variable value.
37 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
38 const char *tmpl
) /* I - Template file to read */
40 FILE *in
; /* Input file */
43 fprintf(stderr
, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out
,
44 tmpl
? tmpl
: "(null)");
47 * Range check input...
54 * Open the template file...
57 if ((in
= fopen(tmpl
, "r")) == NULL
)
59 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
60 tmpl
? tmpl
: "(null)", strerror(errno
));
65 * Parse the file to the end...
68 cgi_copy(out
, in
, 0, 0, 0);
71 * Close the template file and return...
79 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
83 cgiCopyTemplateLang(const char *tmpl
) /* I - Base filename */
85 char filename
[1024], /* Filename */
86 locale
[16], /* Locale name */
87 *locptr
; /* Pointer into locale name */
88 const char *directory
, /* Directory for templates */
90 FILE *in
; /* Input file */
93 fprintf(stderr
, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n",
94 tmpl
? tmpl
: "(null)");
97 * Convert the language to a locale name...
102 if ((lang
= getenv("LANG")) != NULL
)
105 strlcpy(locale
+ 1, lang
, sizeof(locale
) - 1);
107 if ((locptr
= strchr(locale
, '.')) != NULL
)
108 *locptr
= '\0'; /* Strip charset */
111 fprintf(stderr
, "DEBUG2: lang=\"%s\", locale=\"%s\"...\n",
112 lang
? lang
: "(null)", locale
);
115 * See if we have a template file for this language...
118 directory
= cgiGetTemplateDir();
120 snprintf(filename
, sizeof(filename
), "%s%s/%s", directory
, locale
, tmpl
);
121 if ((in
= fopen(filename
, "r")) == NULL
)
125 snprintf(filename
, sizeof(filename
), "%s%s/%s", directory
, locale
, tmpl
);
126 if ((in
= fopen(filename
, "r")) == NULL
)
128 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
129 in
= fopen(filename
, "r");
133 fprintf(stderr
, "DEBUG2: Template file is \"%s\"...\n", filename
);
136 * Open the template file...
141 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
142 filename
, strerror(errno
));
147 * Parse the file to the end...
150 cgi_copy(stdout
, in
, 0, 0, 0);
153 * Close the template file and return...
161 * 'cgiGetTemplateDir()' - Get the templates directory...
164 char * /* O - Template directory */
165 cgiGetTemplateDir(void)
167 const char *datadir
; /* CUPS_DATADIR env var */
168 static char templates
[1024] = ""; /* Template directory */
174 * Build the template directory pathname...
177 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
178 datadir
= CUPS_DATADIR
;
180 snprintf(templates
, sizeof(templates
), "%s/templates", datadir
);
188 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
192 cgiSetServerVersion(void)
194 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
195 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
196 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION
);
199 setlocale(LC_TIME
, "");
205 * 'cgi_copy()' - Copy the template file, substituting as needed...
209 cgi_copy(FILE *out
, /* I - Output file */
210 FILE *in
, /* I - Input file */
211 int element
, /* I - Element number (0 to N) */
212 char term
, /* I - Terminating character */
213 int indent
) /* I - Debug info indentation */
215 int ch
; /* Character from file */
216 char op
; /* Operation */
217 char name
[255], /* Name of variable */
218 *nameptr
, /* Pointer into name */
219 innername
[255], /* Inner comparison name */
220 *innerptr
, /* Pointer into inner name */
221 *s
; /* String pointer */
222 const char *value
; /* Value of variable */
223 const char *innerval
; /* Inner value */
224 const char *outptr
; /* Output string pointer */
225 char outval
[1024], /* Formatted output string */
226 compare
[1024]; /* Comparison string */
227 int result
; /* Result of comparison */
228 int uriencode
; /* Encode as URI */
229 regex_t re
; /* Regular expression to match */
232 fprintf(stderr
, "DEBUG2: %*sStarting at file position %ld...\n", indent
, "",
236 * Parse the file to the end...
239 while ((ch
= getc(in
)) != EOF
)
245 * Get a variable name...
250 for (s
= name
; (ch
= getc(in
)) != EOF
;)
251 if (strchr("}]<>=!~ \t\n", ch
))
253 else if (s
== name
&& ch
== '%')
255 else if (s
> name
&& ch
== '?')
257 else if (s
< (name
+ sizeof(name
) - 1))
262 if (s
== name
&& isspace(ch
& 255))
264 fprintf(stderr
, "DEBUG2: %*sLone { at %ld...\n", indent
, "", ftell(in
));
276 fprintf(stderr
, "DEBUG2: %*s\"{%s}\" at %ld...\n", indent
, "", name
,
280 * See if it has a value...
286 * Insert value only if it exists...
289 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
293 if ((value
= cgiGetArray(name
+ 1, atoi(nameptr
) - 1)) != NULL
)
301 else if ((value
= cgiGetArray(name
+ 1, element
)) != NULL
)
309 else if (name
[0] == '#')
316 sprintf(outval
, "%d", cgiGetSize(name
+ 1));
318 sprintf(outval
, "%d", element
+ 1);
322 else if (name
[0] == '[')
325 * Loop for # of elements...
328 int i
; /* Looping var */
329 long pos
; /* File position */
330 int count
; /* Number of elements */
333 if (isdigit(name
[1] & 255))
334 count
= atoi(name
+ 1);
336 count
= cgiGetSize(name
+ 1);
340 fprintf(stderr
, "DEBUG2: %*sLooping on \"%s\" at %ld, count=%d...\n",
341 indent
, "", name
+ 1, pos
, count
);
345 for (i
= 0; i
< count
; i
++)
348 fseek(in
, pos
, SEEK_SET
);
350 cgi_copy(out
, in
, i
, '}', indent
+ 2);
354 cgi_copy(NULL
, in
, 0, '}', indent
+ 2);
356 fprintf(stderr
, "DEBUG2: %*sFinished looping on \"%s\"...\n", indent
,
361 else if (name
[0] == '$')
364 * Insert cookie value or nothing if not defined.
367 if ((value
= cgiGetCookie(name
+ 1)) != NULL
)
378 * Insert variable or variable name (if element is NULL)...
381 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
384 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
386 snprintf(outval
, sizeof(outval
), "{%s}", name
);
392 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
394 snprintf(outval
, sizeof(outval
), "{%s}", name
);
402 * See if the terminating character requires another test...
408 * End of substitution...
414 cgi_puturi(outptr
, out
);
415 else if (!_cups_strcasecmp(name
, "?cupsdconf_default"))
416 fputs(outptr
, stdout
);
418 cgi_puts(outptr
, out
);
425 * OK, process one of the following checks:
427 * {name?exist:not-exist} Exists?
428 * {name=value?true:false} Equal
429 * {name<value?true:false} Less than
430 * {name>value?true:false} Greater than
431 * {name!value?true:false} Not equal
432 * {name~refex?true:false} Regex match
440 * Test for existance...
444 result
= cgiGetArray(name
+ 1, element
) != NULL
;
445 else if (name
[0] == '#')
446 result
= cgiGetVariable(name
+ 1) != NULL
;
448 result
= cgiGetArray(name
, element
) != NULL
;
450 result
= result
&& outptr
[0];
456 * Compare to a string...
459 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
462 else if (s
>= (compare
+ sizeof(compare
) - 1))
466 sprintf(s
, "%d", element
+ 1);
472 * Grab the value of a variable...
475 innerptr
= innername
;
476 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
477 if (innerptr
< (innername
+ sizeof(innername
) - 1))
478 *innerptr
++ = (char)ch
;
481 if (innername
[0] == '#')
482 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
483 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
484 isdigit(innerptr
[1] & 255))
487 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
490 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
492 else if (innername
[0] == '?')
494 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
497 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
499 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
500 snprintf(s
, sizeof(compare
) - (size_t)(s
- compare
), "{%s}", innername
);
502 strlcpy(s
, innerval
, sizeof(compare
) - (size_t)(s
- compare
));
507 *s
++ = (char)getc(in
);
516 "DEBUG2: %*sBad terminator '%c' at file position %ld...\n",
517 indent
, "", ch
, ftell(in
));
522 * Do the comparison...
528 result
= _cups_strcasecmp(outptr
, compare
) < 0;
531 result
= _cups_strcasecmp(outptr
, compare
) > 0;
534 result
= _cups_strcasecmp(outptr
, compare
) == 0;
537 result
= _cups_strcasecmp(outptr
, compare
) != 0;
540 fprintf(stderr
, "DEBUG: Regular expression \"%s\"\n", compare
);
542 if (regcomp(&re
, compare
, REG_EXTENDED
| REG_ICASE
))
545 "ERROR: Unable to compile regular expression \"%s\"!\n",
551 regmatch_t matches
[10];
555 if (!regexec(&re
, outptr
, 10, matches
, 0))
558 for (i
= 0; i
< 10; i
++)
560 fprintf(stderr
, "DEBUG: matches[%d].rm_so=%d\n", i
,
561 (int)matches
[i
].rm_so
);
562 if (matches
[i
].rm_so
< 0)
579 "DEBUG2: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
580 indent
, "", name
, op
, compare
, ftell(in
), result
);
585 * Comparison true; output first part and ignore second...
588 fprintf(stderr
, "DEBUG2: %*sOutput first part...\n", indent
, "");
589 cgi_copy(out
, in
, element
, ':', indent
+ 2);
591 fprintf(stderr
, "DEBUG2: %*sSkip second part...\n", indent
, "");
592 cgi_copy(NULL
, in
, element
, '}', indent
+ 2);
597 * Comparison false; ignore first part and output second...
600 fprintf(stderr
, "DEBUG2: %*sSkip first part...\n", indent
, "");
601 cgi_copy(NULL
, in
, element
, ':', indent
+ 2);
603 fprintf(stderr
, "DEBUG2: %*sOutput second part...\n", indent
, "");
604 cgi_copy(out
, in
, element
, '}', indent
+ 2);
607 fprintf(stderr
, "DEBUG2: %*sFinished \"{%s%c%s\", out=%p...\n", indent
, "",
608 name
, op
, compare
, out
);
610 else if (ch
== '\\') /* Quoted char */
621 fprintf(stderr
, "DEBUG2: %*sReturning at file position %ld on EOF...\n",
622 indent
, "", ftell(in
));
625 "DEBUG2: %*sReturning at file position %ld on character '%c'...\n",
626 indent
, "", ftell(in
), ch
);
628 if (ch
== EOF
&& term
)
629 fprintf(stderr
, "ERROR: %*sSaw EOF, expected '%c'!\n", indent
, "", term
);
632 * Flush any pending output...
641 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
645 cgi_puts(const char *s
, /* I - String to output */
646 FILE *out
) /* I - Output file */
655 fputs(""", out
);
669 * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
673 cgi_puturi(const char *s
, /* I - String to output */
674 FILE *out
) /* I - Output file */
678 if (strchr("%@&+ <>#=", *s
) || *s
< ' ' || *s
& 128)
679 fprintf(out
, "%%%02X", *s
& 255);