]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / cgi-bin / template.c
1 /*
2 * "$Id: template.c 6986 2007-09-25 15:34:52Z mike $"
3 *
4 * CGI template function.
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
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/".
14 *
15 * Contents:
16 *
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
24 * needed...
25 */
26
27 #include "cgi-private.h"
28 #include <errno.h>
29 #include <regex.h>
30
31
32 /*
33 * Local functions...
34 */
35
36 static void cgi_copy(FILE *out, FILE *in, int element, char term,
37 int indent);
38 static void cgi_puts(const char *s, FILE *out);
39 static void cgi_puturi(const char *s, FILE *out);
40
41
42 /*
43 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
44 * '{variable}' strings with the variable value.
45 */
46
47 void
48 cgiCopyTemplateFile(FILE *out, /* I - Output file */
49 const char *tmpl) /* I - Template file to read */
50 {
51 FILE *in; /* Input file */
52
53
54 fprintf(stderr, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out,
55 tmpl ? tmpl : "(null)");
56
57 /*
58 * Range check input...
59 */
60
61 if (!tmpl || !out)
62 return;
63
64 /*
65 * Open the template file...
66 */
67
68 if ((in = fopen(tmpl, "r")) == NULL)
69 {
70 fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n",
71 tmpl ? tmpl : "(null)", strerror(errno));
72 return;
73 }
74
75 /*
76 * Parse the file to the end...
77 */
78
79 cgi_copy(out, in, 0, 0, 0);
80
81 /*
82 * Close the template file and return...
83 */
84
85 fclose(in);
86 }
87
88
89 /*
90 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
91 */
92
93 void
94 cgiCopyTemplateLang(const char *tmpl) /* I - Base filename */
95 {
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 */
102
103
104 fprintf(stderr, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n",
105 tmpl ? tmpl : "(null)");
106
107 /*
108 * Convert the language to a locale name...
109 */
110
111 locale[0] = '\0';
112
113 if ((lang = getenv("LANG")) != NULL)
114 {
115 locale[0] = '/';
116 strlcpy(locale + 1, lang, sizeof(locale) - 1);
117
118 if ((locptr = strchr(locale, '.')) != NULL)
119 *locptr = '\0'; /* Strip charset */
120 }
121
122 fprintf(stderr, "DEBUG: lang=\"%s\", locale=\"%s\"...\n",
123 lang ? lang : "(null)", locale);
124
125 /*
126 * See if we have a template file for this language...
127 */
128
129 directory = cgiGetTemplateDir();
130
131 snprintf(filename, sizeof(filename), "%s%s/%s", directory, locale, tmpl);
132 if ((in = fopen(filename, "r")) == NULL)
133 {
134 locale[3] = '\0';
135
136 snprintf(filename, sizeof(filename), "%s%s/%s", directory, locale, tmpl);
137 if ((in = fopen(filename, "r")) == NULL)
138 {
139 snprintf(filename, sizeof(filename), "%s/%s", directory, tmpl);
140 in = fopen(filename, "r");
141 }
142 }
143
144 fprintf(stderr, "DEBUG2: Template file is \"%s\"...\n", filename);
145
146 /*
147 * Open the template file...
148 */
149
150 if (!in)
151 {
152 fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n",
153 filename, strerror(errno));
154 return;
155 }
156
157 /*
158 * Parse the file to the end...
159 */
160
161 cgi_copy(stdout, in, 0, 0, 0);
162
163 /*
164 * Close the template file and return...
165 */
166
167 fclose(in);
168 }
169
170
171 /*
172 * 'cgiGetTemplateDir()' - Get the templates directory...
173 */
174
175 char * /* O - Template directory */
176 cgiGetTemplateDir(void)
177 {
178 const char *datadir; /* CUPS_DATADIR env var */
179 static char templates[1024] = ""; /* Template directory */
180
181
182 if (!templates[0])
183 {
184 /*
185 * Build the template directory pathname...
186 */
187
188 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
189 datadir = CUPS_DATADIR;
190
191 snprintf(templates, sizeof(templates), "%s/templates", datadir);
192 }
193
194 return (templates);
195 }
196
197
198 /*
199 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
200 */
201
202 void
203 cgiSetServerVersion(void)
204 {
205 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
206 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
207 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION);
208
209 #ifdef LC_TIME
210 setlocale(LC_TIME, "");
211 #endif /* LC_TIME */
212 }
213
214
215 /*
216 * 'cgi_copy()' - Copy the template file, substituting as needed...
217 */
218
219 static void
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 */
225 {
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 */
241
242
243 fprintf(stderr, "DEBUG2: %*sStarting at file position %ld...\n", indent, "",
244 ftell(in));
245
246 /*
247 * Parse the file to the end...
248 */
249
250 while ((ch = getc(in)) != EOF)
251 if (ch == term)
252 break;
253 else if (ch == '{')
254 {
255 /*
256 * Get a variable name...
257 */
258
259 uriencode = 0;
260
261 for (s = name; (ch = getc(in)) != EOF;)
262 if (strchr("}]<>=!~ \t\n", ch))
263 break;
264 else if (s == name && ch == '%')
265 uriencode = 1;
266 else if (s > name && ch == '?')
267 break;
268 else if (s < (name + sizeof(name) - 1))
269 *s++ = ch;
270
271 *s = '\0';
272
273 if (s == name && isspace(ch & 255))
274 {
275 fprintf(stderr, "DEBUG2: %*sLone { at %ld...\n", indent, "", ftell(in));
276
277 if (out)
278 {
279 putc('{', out);
280 putc(ch, out);
281 }
282
283 continue;
284 }
285
286 if (ch == '}')
287 fprintf(stderr, "DEBUG2: %*s\"{%s}\" at %ld...\n", indent, "", name,
288 ftell(in));
289
290 /*
291 * See if it has a value...
292 */
293
294 if (name[0] == '?')
295 {
296 /*
297 * Insert value only if it exists...
298 */
299
300 if ((nameptr = strrchr(name, '-')) != NULL && isdigit(nameptr[1] & 255))
301 {
302 *nameptr++ = '\0';
303
304 if ((value = cgiGetArray(name + 1, atoi(nameptr) - 1)) != NULL)
305 outptr = value;
306 else
307 {
308 outval[0] = '\0';
309 outptr = outval;
310 }
311 }
312 else if ((value = cgiGetArray(name + 1, element)) != NULL)
313 outptr = value;
314 else
315 {
316 outval[0] = '\0';
317 outptr = outval;
318 }
319 }
320 else if (name[0] == '#')
321 {
322 /*
323 * Insert count...
324 */
325
326 if (name[1])
327 sprintf(outval, "%d", cgiGetSize(name + 1));
328 else
329 sprintf(outval, "%d", element + 1);
330
331 outptr = outval;
332 }
333 else if (name[0] == '[')
334 {
335 /*
336 * Loop for # of elements...
337 */
338
339 int i; /* Looping var */
340 long pos; /* File position */
341 int count; /* Number of elements */
342
343
344 if (isdigit(name[1] & 255))
345 count = atoi(name + 1);
346 else
347 count = cgiGetSize(name + 1);
348
349 pos = ftell(in);
350
351 fprintf(stderr, "DEBUG2: %*sLooping on \"%s\" at %ld, count=%d...\n",
352 indent, "", name + 1, pos, count);
353
354 if (count > 0)
355 {
356 for (i = 0; i < count; i ++)
357 {
358 if (i)
359 fseek(in, pos, SEEK_SET);
360
361 cgi_copy(out, in, i, '}', indent + 2);
362 }
363 }
364 else
365 cgi_copy(NULL, in, 0, '}', indent + 2);
366
367 fprintf(stderr, "DEBUG2: %*sFinished looping on \"%s\"...\n", indent,
368 "", name + 1);
369
370 continue;
371 }
372 else
373 {
374 /*
375 * Insert variable or variable name (if element is NULL)...
376 */
377
378 if ((nameptr = strrchr(name, '-')) != NULL && isdigit(nameptr[1] & 255))
379 {
380 *nameptr++ = '\0';
381 if ((value = cgiGetArray(name, atoi(nameptr) - 1)) == NULL)
382 {
383 snprintf(outval, sizeof(outval), "{%s}", name);
384 outptr = outval;
385 }
386 else
387 outptr = value;
388 }
389 else if ((value = cgiGetArray(name, element)) == NULL)
390 {
391 snprintf(outval, sizeof(outval), "{%s}", name);
392 outptr = outval;
393 }
394 else
395 outptr = value;
396 }
397
398 /*
399 * See if the terminating character requires another test...
400 */
401
402 if (ch == '}')
403 {
404 /*
405 * End of substitution...
406 */
407
408 if (out)
409 {
410 if (uriencode)
411 cgi_puturi(outptr, out);
412 else if (!strcasecmp(name, "?cupsdconf_default"))
413 fputs(outptr, stdout);
414 else
415 cgi_puts(outptr, out);
416 }
417
418 continue;
419 }
420
421 /*
422 * OK, process one of the following checks:
423 *
424 * {name?exist:not-exist} Exists?
425 * {name=value?true:false} Equal
426 * {name<value?true:false} Less than
427 * {name>value?true:false} Greater than
428 * {name!value?true:false} Not equal
429 * {name~refex?true:false} Regex match
430 */
431
432 op = ch;
433
434 if (ch == '?')
435 {
436 /*
437 * Test for existance...
438 */
439
440 result = cgiGetArray(name, element) != NULL && outptr[0];
441 compare[0] = '\0';
442 }
443 else
444 {
445 /*
446 * Compare to a string...
447 */
448
449 for (s = compare; (ch = getc(in)) != EOF;)
450 if (ch == '?')
451 break;
452 else if (s >= (compare + sizeof(compare) - 1))
453 continue;
454 else if (ch == '#')
455 {
456 sprintf(s, "%d", element + 1);
457 s += strlen(s);
458 }
459 else if (ch == '{')
460 {
461 /*
462 * Grab the value of a variable...
463 */
464
465 innerptr = innername;
466 while ((ch = getc(in)) != EOF && ch != '}')
467 if (innerptr < (innername + sizeof(innername) - 1))
468 *innerptr++ = ch;
469 *innerptr = '\0';
470
471 if (innername[0] == '#')
472 sprintf(s, "%d", cgiGetSize(innername + 1));
473 else if ((innerptr = strrchr(innername, '-')) != NULL &&
474 isdigit(innerptr[1] & 255))
475 {
476 *innerptr++ = '\0';
477 if ((innerval = cgiGetArray(innername, atoi(innerptr) - 1)) == NULL)
478 *s = '\0';
479 else
480 strlcpy(s, innerval, sizeof(compare) - (s - compare));
481 }
482 else if (innername[0] == '?')
483 {
484 if ((innerval = cgiGetArray(innername + 1, element)) == NULL)
485 *s = '\0';
486 else
487 strlcpy(s, innerval, sizeof(compare) - (s - compare));
488 }
489 else if ((innerval = cgiGetArray(innername, element)) == NULL)
490 snprintf(s, sizeof(compare) - (s - compare), "{%s}", innername);
491 else
492 strlcpy(s, innerval, sizeof(compare) - (s - compare));
493
494 s += strlen(s);
495 }
496 else if (ch == '\\')
497 *s++ = getc(in);
498 else
499 *s++ = ch;
500
501 *s = '\0';
502
503 if (ch != '?')
504 {
505 fprintf(stderr,
506 "DEBUG2: %*sBad terminator '%c' at file position %ld...\n",
507 indent, "", ch, ftell(in));
508 return;
509 }
510
511 /*
512 * Do the comparison...
513 */
514
515 switch (op)
516 {
517 case '<' :
518 result = strcasecmp(outptr, compare) < 0;
519 break;
520 case '>' :
521 result = strcasecmp(outptr, compare) > 0;
522 break;
523 case '=' :
524 result = strcasecmp(outptr, compare) == 0;
525 break;
526 case '!' :
527 result = strcasecmp(outptr, compare) != 0;
528 break;
529 case '~' :
530 fprintf(stderr, "DEBUG: Regular expression \"%s\"\n", compare);
531
532 if (regcomp(&re, compare, REG_EXTENDED | REG_ICASE))
533 {
534 fprintf(stderr,
535 "ERROR: Unable to compile regular expresion \"%s\"!\n",
536 compare);
537 result = 0;
538 }
539 else
540 {
541 regmatch_t matches[10];
542
543 result = 0;
544
545 if (!regexec(&re, outptr, 10, matches, 0))
546 {
547 int i;
548 for (i = 0; i < 10; i ++)
549 {
550 fprintf(stderr, "DEBUG: matches[%d].rm_so=%d\n", i,
551 (int)matches[i].rm_so);
552 if (matches[i].rm_so < 0)
553 break;
554
555 result ++;
556 }
557 }
558
559 regfree(&re);
560 }
561 break;
562 default :
563 result = 1;
564 break;
565 }
566 }
567
568 fprintf(stderr,
569 "DEBUG2: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
570 indent, "", name, op, compare, ftell(in), result);
571
572 if (result)
573 {
574 /*
575 * Comparison true; output first part and ignore second...
576 */
577
578 fprintf(stderr, "DEBUG2: %*sOutput first part...\n", indent, "");
579 cgi_copy(out, in, element, ':', indent + 2);
580
581 fprintf(stderr, "DEBUG2: %*sSkip second part...\n", indent, "");
582 cgi_copy(NULL, in, element, '}', indent + 2);
583 }
584 else
585 {
586 /*
587 * Comparison false; ignore first part and output second...
588 */
589
590 fprintf(stderr, "DEBUG2: %*sSkip first part...\n", indent, "");
591 cgi_copy(NULL, in, element, ':', indent + 2);
592
593 fprintf(stderr, "DEBUG2: %*sOutput second part...\n", indent, "");
594 cgi_copy(out, in, element, '}', indent + 2);
595 }
596
597 fprintf(stderr, "DEBUG2: %*sFinished \"{%s%c%s\", out=%p...\n", indent, "",
598 name, op, compare, out);
599 }
600 else if (ch == '\\') /* Quoted char */
601 {
602 if (out)
603 putc(getc(in), out);
604 else
605 getc(in);
606 }
607 else if (out)
608 putc(ch, out);
609
610 if (ch == EOF)
611 fprintf(stderr, "DEBUG2: %*sReturning at file position %ld on EOF...\n",
612 indent, "", ftell(in));
613 else
614 fprintf(stderr,
615 "DEBUG2: %*sReturning at file position %ld on character '%c'...\n",
616 indent, "", ftell(in), ch);
617
618 if (ch == EOF && term)
619 fprintf(stderr, "ERROR: %*sSaw EOF, expected '%c'!\n", indent, "", term);
620
621 /*
622 * Flush any pending output...
623 */
624
625 if (out)
626 fflush(out);
627 }
628
629
630 /*
631 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
632 */
633
634 static void
635 cgi_puts(const char *s, /* I - String to output */
636 FILE *out) /* I - Output file */
637 {
638 while (*s)
639 {
640 if (*s == '<')
641 {
642 /*
643 * Pass <A HREF="url"> and </A>, otherwise quote it...
644 */
645
646 if (!strncasecmp(s, "<A HREF=\"", 9))
647 {
648 fputs("<A HREF=\"", out);
649 s += 9;
650
651 while (*s && *s != '\"')
652 {
653 if (*s == '&')
654 fputs("&amp;", out);
655 else
656 putc(*s, out);
657
658 s ++;
659 }
660
661 if (*s)
662 s ++;
663
664 fputs("\">", out);
665 }
666 else if (!strncasecmp(s, "</A>", 4))
667 {
668 fputs("</A>", out);
669 s += 3;
670 }
671 else
672 fputs("&lt;", out);
673 }
674 else if (*s == '>')
675 fputs("&gt;", out);
676 else if (*s == '\"')
677 fputs("&quot;", out);
678 else if (*s == '&')
679 fputs("&amp;", out);
680 else
681 putc(*s, out);
682
683 s ++;
684 }
685 }
686
687
688 /*
689 * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
690 */
691
692 static void
693 cgi_puturi(const char *s, /* I - String to output */
694 FILE *out) /* I - Output file */
695 {
696 while (*s)
697 {
698 if (strchr("%&+ <>#=", *s) || *s & 128)
699 fprintf(out, "%%%02X", *s & 255);
700 else
701 putc(*s, out);
702
703 s ++;
704 }
705 }
706
707
708 /*
709 * End of "$Id: template.c 6986 2007-09-25 15:34:52Z mike $".
710 */