]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
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
);
47 static void cgi_puturi(const char *s
, FILE *out
);
51 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
52 * '{variable}' strings with the variable value.
56 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
57 const char *tmpl
) /* I - Template file to read */
59 FILE *in
; /* Input file */
62 fprintf(stderr
, "DEBUG: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out
,
63 tmpl
? tmpl
: "(null)");
66 * Open the template file...
69 if ((in
= fopen(tmpl
, "r")) == NULL
)
71 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
72 tmpl
? tmpl
: "(null)", strerror(errno
));
77 * Parse the file to the end...
80 cgi_copy(out
, in
, 0, 0, 0);
83 * Close the template file and return...
91 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
95 cgiCopyTemplateLang(const char *tmpl
) /* I - Base filename */
97 int i
; /* Looping var */
98 char filename
[1024], /* Filename */
99 locale
[16]; /* Locale name */
100 const char *directory
, /* Directory for templates */
101 *lang
; /* Language */
102 FILE *in
; /* Input file */
105 fprintf(stderr
, "DEBUG: cgiCopyTemplateLang(tmpl=\"%s\")\n",
106 tmpl
? tmpl
: "(null)");
109 * Convert the language to a locale name...
112 if ((lang
= getenv("LANG")) != NULL
)
114 for (i
= 0; lang
[i
] && i
< 15; i
++)
115 if (isalnum(lang
[i
] & 255) || lang
[i
] == '_')
116 locale
[i
] = tolower(lang
[i
]);
117 else if (lang
[i
] == '-')
127 fprintf(stderr
, "DEBUG: locale=\"%s\"...\n", locale
);
130 * See if we have a template file for this language...
133 directory
= cgiGetTemplateDir();
135 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
136 if (access(filename
, 0))
140 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
141 if (access(filename
, 0))
142 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
145 fprintf(stderr
, "DEBUG: Template file is \"%s\"...\n", filename
);
148 * Open the template file...
151 if ((in
= fopen(filename
, "r")) == NULL
)
153 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
154 filename
, strerror(errno
));
159 * Parse the file to the end...
162 cgi_copy(stdout
, in
, 0, 0, 0);
165 * Close the template file and return...
173 * 'cgiGetTemplateDir()' - Get the templates directory...
176 char * /* O - Template directory */
177 cgiGetTemplateDir(void)
179 const char *datadir
; /* CUPS_DATADIR env var */
180 static char templates
[1024] = ""; /* Template directory */
186 * Build the template directory pathname...
189 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
190 datadir
= CUPS_DATADIR
;
192 snprintf(templates
, sizeof(templates
), "%s/templates", datadir
);
200 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
204 cgiSetServerVersion(void)
206 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
207 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
208 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION
);
211 setlocale(LC_TIME
, "");
217 * 'cgi_copy()' - Copy the template file, substituting as needed...
221 cgi_copy(FILE *out
, /* I - Output file */
222 FILE *in
, /* I - Input file */
223 int element
, /* I - Element number (0 to N) */
224 char term
, /* I - Terminating character */
225 int indent
) /* I - Debug info indentation */
227 int ch
; /* Character from file */
228 char op
; /* Operation */
229 char name
[255], /* Name of variable */
230 *nameptr
, /* Pointer into name */
231 innername
[255], /* Inner comparison name */
232 *innerptr
, /* Pointer into inner name */
233 *s
; /* String pointer */
234 const char *value
; /* Value of variable */
235 const char *innerval
; /* Inner value */
236 const char *outptr
; /* Output string pointer */
237 char outval
[1024], /* Formatted output string */
238 compare
[1024]; /* Comparison string */
239 int result
; /* Result of comparison */
240 int uriencode
; /* Encode as URI */
243 fprintf(stderr
, "DEBUG: %*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
, "DEBUG: %*sLone { at %ld...\n", indent
, "", ftell(in
));
287 fprintf(stderr
, "DEBUG: %*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
, "DEBUG: %*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
, "DEBUG: %*sFinished looping on \"%s\"...\n", indent
,
375 * Insert variable or variable name (if element is NULL)...
378 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
381 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
383 snprintf(outval
, sizeof(outval
), "{%s}", name
);
389 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
391 snprintf(outval
, sizeof(outval
), "{%s}", name
);
399 * See if the terminating character requires another test...
405 * End of substitution...
411 cgi_puturi(outptr
, out
);
413 cgi_puts(outptr
, out
);
420 * OK, process one of the following checks:
422 * {name?exist:not-exist} Exists?
423 * {name=value?true:false} Equal
424 * {name<value?true:false} Less than
425 * {name>value?true:false} Greater than
426 * {name!value?true:false} Not equal
434 * Test for existance...
437 result
= cgiGetArray(name
, element
) != NULL
&& outptr
[0];
443 * Compare to a string...
446 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
449 else if (s
>= (compare
+ sizeof(compare
) - 1))
453 sprintf(s
, "%d", element
+ 1);
459 * Grab the value of a variable...
462 innerptr
= innername
;
463 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
464 if (innerptr
< (innername
+ sizeof(innername
) - 1))
468 if (innername
[0] == '#')
469 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
470 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
471 isdigit(innerptr
[1] & 255))
474 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
477 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
479 else if (innername
[0] == '?')
481 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
484 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
486 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
487 snprintf(s
, sizeof(compare
) - (s
- compare
), "{%s}", innername
);
489 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
503 "DEBUG: %*sBad terminator '%c' at file position %ld...\n",
504 indent
, "", ch
, ftell(in
));
509 * Do the comparison...
515 result
= strcasecmp(outptr
, compare
) < 0;
518 result
= strcasecmp(outptr
, compare
) > 0;
521 result
= strcasecmp(outptr
, compare
) == 0;
524 result
= strcasecmp(outptr
, compare
) != 0;
533 "DEBUG: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
534 indent
, "", name
, op
, compare
, ftell(in
), result
);
539 * Comparison true; output first part and ignore second...
542 fprintf(stderr
, "DEBUG: %*sOutput first part...\n", indent
, "");
543 cgi_copy(out
, in
, element
, ':', indent
+ 2);
545 fprintf(stderr
, "DEBUG: %*sSkip second part...\n", indent
, "");
546 cgi_copy(NULL
, in
, element
, '}', indent
+ 2);
551 * Comparison false; ignore first part and output second...
554 fprintf(stderr
, "DEBUG: %*sSkip first part...\n", indent
, "");
555 cgi_copy(NULL
, in
, element
, ':', indent
+ 2);
557 fprintf(stderr
, "DEBUG: %*sOutput second part...\n", indent
, "");
558 cgi_copy(out
, in
, element
, '}', indent
+ 2);
561 fprintf(stderr
, "DEBUG: %*sFinished \"{%s%c%s\", out=%p...\n", indent
, "",
562 name
, op
, compare
, out
);
564 else if (ch
== '\\') /* Quoted char */
575 fprintf(stderr
, "DEBUG: %*sReturning at file position %ld on EOF...\n",
576 indent
, "", ftell(in
));
579 "DEBUG: %*sReturning at file position %ld on character '%c'...\n",
580 indent
, "", ftell(in
), ch
);
582 if (ch
== EOF
&& term
)
583 fprintf(stderr
, "ERROR: %*sSaw EOF, expected '%c'!\n", indent
, "", term
);
586 * Flush any pending output...
595 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
599 cgi_puts(const char *s
, /* I - String to output */
600 FILE *out
) /* I - Output file */
607 * Pass <A HREF="url"> and </A>, otherwise quote it...
610 if (!strncasecmp(s
, "<A HREF=\"", 9))
612 fputs("<A HREF=\"", out
);
615 while (*s
&& *s
!= '\"')
630 else if (!strncasecmp(s
, "</A>", 4))
641 fputs(""", out
);
653 * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
657 cgi_puturi(const char *s
, /* I - String to output */
658 FILE *out
) /* I - Output file */
662 if (strchr("%&+ <>#=", *s
) || *s
& 128)
663 fprintf(out
, "%%%02X", *s
& 255);