]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
2 * "$Id: template.c 7219 2008-01-14 22:00:02Z mike $"
4 * CGI template function.
6 * Copyright 2007-2011 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/".
17 * cgiCopyTemplateFile() - Copy a template file and replace all the
18 * '{variable}' strings with the variable value.
19 * cgiCopyTemplateLang() - Copy a template file using a language...
20 * cgiGetTemplateDir() - Get the templates directory...
21 * cgiSetServerVersion() - Set the server name and CUPS version...
22 * cgi_copy() - Copy the template file, substituting as needed...
23 * cgi_puts() - Put a string to the output file, quoting as
27 #include "cgi-private.h"
36 static void cgi_copy(FILE *out
, FILE *in
, int element
, char term
,
38 static void cgi_puts(const char *s
, FILE *out
);
39 static void cgi_puturi(const char *s
, FILE *out
);
43 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
44 * '{variable}' strings with the variable value.
48 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
49 const char *tmpl
) /* I - Template file to read */
51 FILE *in
; /* Input file */
54 fprintf(stderr
, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out
,
55 tmpl
? tmpl
: "(null)");
58 * Range check input...
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
? tmpl
: "(null)", 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 char filename
[1024], /* Filename */
97 locale
[16], /* Locale name */
98 *locptr
; /* Pointer into locale name */
99 const char *directory
, /* Directory for templates */
100 *lang
; /* Language */
101 FILE *in
; /* Input file */
104 fprintf(stderr
, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n",
105 tmpl
? tmpl
: "(null)");
108 * Convert the language to a locale name...
113 if ((lang
= getenv("LANG")) != NULL
)
116 strlcpy(locale
+ 1, lang
, sizeof(locale
) - 1);
118 if ((locptr
= strchr(locale
, '.')) != NULL
)
119 *locptr
= '\0'; /* Strip charset */
122 fprintf(stderr
, "DEBUG2: lang=\"%s\", locale=\"%s\"...\n",
123 lang
? lang
: "(null)", locale
);
126 * See if we have a template file for this language...
129 directory
= cgiGetTemplateDir();
131 snprintf(filename
, sizeof(filename
), "%s%s/%s", directory
, locale
, tmpl
);
132 if ((in
= fopen(filename
, "r")) == NULL
)
136 snprintf(filename
, sizeof(filename
), "%s%s/%s", directory
, locale
, tmpl
);
137 if ((in
= fopen(filename
, "r")) == NULL
)
139 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
140 in
= fopen(filename
, "r");
144 fprintf(stderr
, "DEBUG2: Template file is \"%s\"...\n", filename
);
147 * Open the template file...
152 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
153 filename
, strerror(errno
));
158 * Parse the file to the end...
161 cgi_copy(stdout
, in
, 0, 0, 0);
164 * Close the template file and return...
172 * 'cgiGetTemplateDir()' - Get the templates directory...
175 char * /* O - Template directory */
176 cgiGetTemplateDir(void)
178 const char *datadir
; /* CUPS_DATADIR env var */
179 static char templates
[1024] = ""; /* Template directory */
185 * Build the template directory pathname...
188 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
189 datadir
= CUPS_DATADIR
;
191 snprintf(templates
, sizeof(templates
), "%s/templates", datadir
);
199 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
203 cgiSetServerVersion(void)
205 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
206 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
207 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION
);
210 setlocale(LC_TIME
, "");
216 * 'cgi_copy()' - Copy the template file, substituting as needed...
220 cgi_copy(FILE *out
, /* I - Output file */
221 FILE *in
, /* I - Input file */
222 int element
, /* I - Element number (0 to N) */
223 char term
, /* I - Terminating character */
224 int indent
) /* I - Debug info indentation */
226 int ch
; /* Character from file */
227 char op
; /* Operation */
228 char name
[255], /* Name of variable */
229 *nameptr
, /* Pointer into name */
230 innername
[255], /* Inner comparison name */
231 *innerptr
, /* Pointer into inner name */
232 *s
; /* String pointer */
233 const char *value
; /* Value of variable */
234 const char *innerval
; /* Inner value */
235 const char *outptr
; /* Output string pointer */
236 char outval
[1024], /* Formatted output string */
237 compare
[1024]; /* Comparison string */
238 int result
; /* Result of comparison */
239 int uriencode
; /* Encode as URI */
240 regex_t re
; /* Regular expression to match */
243 fprintf(stderr
, "DEBUG2: %*sStarting at file position %ld...\n", indent
, "",
247 * Parse the file to the end...
250 while ((ch
= getc(in
)) != EOF
)
256 * Get a variable name...
261 for (s
= name
; (ch
= getc(in
)) != EOF
;)
262 if (strchr("}]<>=!~ \t\n", ch
))
264 else if (s
== name
&& ch
== '%')
266 else if (s
> name
&& ch
== '?')
268 else if (s
< (name
+ sizeof(name
) - 1))
273 if (s
== name
&& isspace(ch
& 255))
275 fprintf(stderr
, "DEBUG2: %*sLone { at %ld...\n", indent
, "", ftell(in
));
287 fprintf(stderr
, "DEBUG2: %*s\"{%s}\" at %ld...\n", indent
, "", name
,
291 * See if it has a value...
297 * Insert value only if it exists...
300 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
304 if ((value
= cgiGetArray(name
+ 1, atoi(nameptr
) - 1)) != NULL
)
312 else if ((value
= cgiGetArray(name
+ 1, element
)) != NULL
)
320 else if (name
[0] == '#')
327 sprintf(outval
, "%d", cgiGetSize(name
+ 1));
329 sprintf(outval
, "%d", element
+ 1);
333 else if (name
[0] == '[')
336 * Loop for # of elements...
339 int i
; /* Looping var */
340 long pos
; /* File position */
341 int count
; /* Number of elements */
344 if (isdigit(name
[1] & 255))
345 count
= atoi(name
+ 1);
347 count
= cgiGetSize(name
+ 1);
351 fprintf(stderr
, "DEBUG2: %*sLooping on \"%s\" at %ld, count=%d...\n",
352 indent
, "", name
+ 1, pos
, count
);
356 for (i
= 0; i
< count
; i
++)
359 fseek(in
, pos
, SEEK_SET
);
361 cgi_copy(out
, in
, i
, '}', indent
+ 2);
365 cgi_copy(NULL
, in
, 0, '}', indent
+ 2);
367 fprintf(stderr
, "DEBUG2: %*sFinished looping on \"%s\"...\n", indent
,
372 else if (name
[0] == '$')
375 * Insert cookie value or nothing if not defined.
378 if ((value
= cgiGetCookie(name
+ 1)) != NULL
)
389 * Insert variable or variable name (if element is NULL)...
392 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
395 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
397 snprintf(outval
, sizeof(outval
), "{%s}", name
);
403 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
405 snprintf(outval
, sizeof(outval
), "{%s}", name
);
413 * See if the terminating character requires another test...
419 * End of substitution...
425 cgi_puturi(outptr
, out
);
426 else if (!_cups_strcasecmp(name
, "?cupsdconf_default"))
427 fputs(outptr
, stdout
);
429 cgi_puts(outptr
, out
);
436 * OK, process one of the following checks:
438 * {name?exist:not-exist} Exists?
439 * {name=value?true:false} Equal
440 * {name<value?true:false} Less than
441 * {name>value?true:false} Greater than
442 * {name!value?true:false} Not equal
443 * {name~refex?true:false} Regex match
451 * Test for existance...
455 result
= cgiGetArray(name
+ 1, element
) != NULL
;
456 else if (name
[0] == '#')
457 result
= cgiGetVariable(name
+ 1) != NULL
;
459 result
= cgiGetArray(name
, element
) != NULL
;
461 result
= result
&& outptr
[0];
467 * Compare to a string...
470 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
473 else if (s
>= (compare
+ sizeof(compare
) - 1))
477 sprintf(s
, "%d", element
+ 1);
483 * Grab the value of a variable...
486 innerptr
= innername
;
487 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
488 if (innerptr
< (innername
+ sizeof(innername
) - 1))
492 if (innername
[0] == '#')
493 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
494 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
495 isdigit(innerptr
[1] & 255))
498 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
501 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
503 else if (innername
[0] == '?')
505 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
508 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
510 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
511 snprintf(s
, sizeof(compare
) - (s
- compare
), "{%s}", innername
);
513 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
527 "DEBUG2: %*sBad terminator '%c' at file position %ld...\n",
528 indent
, "", ch
, ftell(in
));
533 * Do the comparison...
539 result
= _cups_strcasecmp(outptr
, compare
) < 0;
542 result
= _cups_strcasecmp(outptr
, compare
) > 0;
545 result
= _cups_strcasecmp(outptr
, compare
) == 0;
548 result
= _cups_strcasecmp(outptr
, compare
) != 0;
551 fprintf(stderr
, "DEBUG: Regular expression \"%s\"\n", compare
);
553 if (regcomp(&re
, compare
, REG_EXTENDED
| REG_ICASE
))
556 "ERROR: Unable to compile regular expresion \"%s\"!\n",
562 regmatch_t matches
[10];
566 if (!regexec(&re
, outptr
, 10, matches
, 0))
569 for (i
= 0; i
< 10; i
++)
571 fprintf(stderr
, "DEBUG: matches[%d].rm_so=%d\n", i
,
572 (int)matches
[i
].rm_so
);
573 if (matches
[i
].rm_so
< 0)
590 "DEBUG2: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
591 indent
, "", name
, op
, compare
, ftell(in
), result
);
596 * Comparison true; output first part and ignore second...
599 fprintf(stderr
, "DEBUG2: %*sOutput first part...\n", indent
, "");
600 cgi_copy(out
, in
, element
, ':', indent
+ 2);
602 fprintf(stderr
, "DEBUG2: %*sSkip second part...\n", indent
, "");
603 cgi_copy(NULL
, in
, element
, '}', indent
+ 2);
608 * Comparison false; ignore first part and output second...
611 fprintf(stderr
, "DEBUG2: %*sSkip first part...\n", indent
, "");
612 cgi_copy(NULL
, in
, element
, ':', indent
+ 2);
614 fprintf(stderr
, "DEBUG2: %*sOutput second part...\n", indent
, "");
615 cgi_copy(out
, in
, element
, '}', indent
+ 2);
618 fprintf(stderr
, "DEBUG2: %*sFinished \"{%s%c%s\", out=%p...\n", indent
, "",
619 name
, op
, compare
, out
);
621 else if (ch
== '\\') /* Quoted char */
632 fprintf(stderr
, "DEBUG2: %*sReturning at file position %ld on EOF...\n",
633 indent
, "", ftell(in
));
636 "DEBUG2: %*sReturning at file position %ld on character '%c'...\n",
637 indent
, "", ftell(in
), ch
);
639 if (ch
== EOF
&& term
)
640 fprintf(stderr
, "ERROR: %*sSaw EOF, expected '%c'!\n", indent
, "", term
);
643 * Flush any pending output...
652 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
656 cgi_puts(const char *s
, /* I - String to output */
657 FILE *out
) /* I - Output file */
664 * Pass <A HREF="url"> and </A>, otherwise quote it...
667 if (!_cups_strncasecmp(s
, "<A HREF=\"", 9))
669 fputs("<A HREF=\"", out
);
672 while (*s
&& *s
!= '\"')
687 else if (!_cups_strncasecmp(s
, "</A>", 4))
698 fputs(""", out
);
712 * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
716 cgi_puturi(const char *s
, /* I - String to output */
717 FILE *out
) /* I - Output file */
721 if (strchr("%@&+ <>#=", *s
) || *s
< ' ' || *s
& 128)
722 fprintf(out
, "%%%02X", *s
& 255);
732 * End of "$Id: template.c 7219 2008-01-14 22:00:02Z mike $".