]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
2 * "$Id: template.c 6717 2007-07-24 23:47:12Z mike $"
4 * CGI template function.
6 * Copyright 2007 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"
35 static void cgi_copy(FILE *out
, FILE *in
, int element
, char term
,
37 static void cgi_puts(const char *s
, FILE *out
);
38 static void cgi_puturi(const char *s
, FILE *out
);
42 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
43 * '{variable}' strings with the variable value.
47 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
48 const char *tmpl
) /* I - Template file to read */
50 FILE *in
; /* Input file */
53 fprintf(stderr
, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out
,
54 tmpl
? tmpl
: "(null)");
57 * Open the template file...
60 if ((in
= fopen(tmpl
, "r")) == NULL
)
62 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
63 tmpl
? tmpl
: "(null)", strerror(errno
));
68 * Parse the file to the end...
71 cgi_copy(out
, in
, 0, 0, 0);
74 * Close the template file and return...
82 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
86 cgiCopyTemplateLang(const char *tmpl
) /* I - Base filename */
88 int i
; /* Looping var */
89 char filename
[1024], /* Filename */
90 locale
[16]; /* Locale name */
91 const char *directory
, /* Directory for templates */
93 FILE *in
; /* Input file */
96 fprintf(stderr
, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n",
97 tmpl
? tmpl
: "(null)");
100 * Convert the language to a locale name...
103 if ((lang
= getenv("LANG")) != NULL
)
105 for (i
= 0; lang
[i
] && i
< 15; i
++)
106 if (isalnum(lang
[i
] & 255) || lang
[i
] == '_')
107 locale
[i
] = tolower(lang
[i
]);
108 else if (lang
[i
] == '-')
118 fprintf(stderr
, "DEBUG2: locale=\"%s\"...\n", locale
);
121 * See if we have a template file for this language...
124 directory
= cgiGetTemplateDir();
126 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
127 if (access(filename
, 0))
131 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
132 if (access(filename
, 0))
133 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
136 fprintf(stderr
, "DEBUG2: Template file is \"%s\"...\n", filename
);
139 * Open the template file...
142 if ((in
= fopen(filename
, "r")) == NULL
)
144 fprintf(stderr
, "ERROR: Unable to open template file \"%s\" - %s\n",
145 filename
, strerror(errno
));
150 * Parse the file to the end...
153 cgi_copy(stdout
, in
, 0, 0, 0);
156 * Close the template file and return...
164 * 'cgiGetTemplateDir()' - Get the templates directory...
167 char * /* O - Template directory */
168 cgiGetTemplateDir(void)
170 const char *datadir
; /* CUPS_DATADIR env var */
171 static char templates
[1024] = ""; /* Template directory */
177 * Build the template directory pathname...
180 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
181 datadir
= CUPS_DATADIR
;
183 snprintf(templates
, sizeof(templates
), "%s/templates", datadir
);
191 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
195 cgiSetServerVersion(void)
197 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
198 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
199 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION
);
202 setlocale(LC_TIME
, "");
208 * 'cgi_copy()' - Copy the template file, substituting as needed...
212 cgi_copy(FILE *out
, /* I - Output file */
213 FILE *in
, /* I - Input file */
214 int element
, /* I - Element number (0 to N) */
215 char term
, /* I - Terminating character */
216 int indent
) /* I - Debug info indentation */
218 int ch
; /* Character from file */
219 char op
; /* Operation */
220 char name
[255], /* Name of variable */
221 *nameptr
, /* Pointer into name */
222 innername
[255], /* Inner comparison name */
223 *innerptr
, /* Pointer into inner name */
224 *s
; /* String pointer */
225 const char *value
; /* Value of variable */
226 const char *innerval
; /* Inner value */
227 const char *outptr
; /* Output string pointer */
228 char outval
[1024], /* Formatted output string */
229 compare
[1024]; /* Comparison string */
230 int result
; /* Result of comparison */
231 int uriencode
; /* Encode as URI */
234 fprintf(stderr
, "DEBUG2: %*sStarting at file position %ld...\n", indent
, "",
238 * Parse the file to the end...
241 while ((ch
= getc(in
)) != EOF
)
247 * Get a variable name...
252 for (s
= name
; (ch
= getc(in
)) != EOF
;)
253 if (strchr("}]<>=! \t\n", ch
))
255 else if (s
== name
&& ch
== '%')
257 else if (s
> name
&& ch
== '?')
259 else if (s
< (name
+ sizeof(name
) - 1))
264 if (s
== name
&& isspace(ch
& 255))
266 fprintf(stderr
, "DEBUG2: %*sLone { at %ld...\n", indent
, "", ftell(in
));
278 fprintf(stderr
, "DEBUG2: %*s\"{%s}\" at %ld...\n", indent
, "", name
,
282 * See if it has a value...
288 * Insert value only if it exists...
291 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
295 if ((value
= cgiGetArray(name
+ 1, atoi(nameptr
) - 1)) != NULL
)
303 else if ((value
= cgiGetArray(name
+ 1, element
)) != NULL
)
311 else if (name
[0] == '#')
318 sprintf(outval
, "%d", cgiGetSize(name
+ 1));
320 sprintf(outval
, "%d", element
+ 1);
324 else if (name
[0] == '[')
327 * Loop for # of elements...
330 int i
; /* Looping var */
331 long pos
; /* File position */
332 int count
; /* Number of elements */
335 if (isdigit(name
[1] & 255))
336 count
= atoi(name
+ 1);
338 count
= cgiGetSize(name
+ 1);
342 fprintf(stderr
, "DEBUG2: %*sLooping on \"%s\" at %ld, count=%d...\n",
343 indent
, "", name
+ 1, pos
, count
);
347 for (i
= 0; i
< count
; i
++)
350 fseek(in
, pos
, SEEK_SET
);
352 cgi_copy(out
, in
, i
, '}', indent
+ 2);
356 cgi_copy(NULL
, in
, 0, '}', indent
+ 2);
358 fprintf(stderr
, "DEBUG2: %*sFinished looping on \"%s\"...\n", indent
,
366 * Insert variable or variable name (if element is NULL)...
369 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
372 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
374 snprintf(outval
, sizeof(outval
), "{%s}", name
);
380 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
382 snprintf(outval
, sizeof(outval
), "{%s}", name
);
390 * See if the terminating character requires another test...
396 * End of substitution...
402 cgi_puturi(outptr
, out
);
403 else if (!strcasecmp(name
, "?cupsdconf_default"))
404 fputs(outptr
, stdout
);
406 cgi_puts(outptr
, out
);
413 * OK, process one of the following checks:
415 * {name?exist:not-exist} Exists?
416 * {name=value?true:false} Equal
417 * {name<value?true:false} Less than
418 * {name>value?true:false} Greater than
419 * {name!value?true:false} Not equal
427 * Test for existance...
430 result
= cgiGetArray(name
, element
) != NULL
&& outptr
[0];
436 * Compare to a string...
439 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
442 else if (s
>= (compare
+ sizeof(compare
) - 1))
446 sprintf(s
, "%d", element
+ 1);
452 * Grab the value of a variable...
455 innerptr
= innername
;
456 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
457 if (innerptr
< (innername
+ sizeof(innername
) - 1))
461 if (innername
[0] == '#')
462 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
463 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
464 isdigit(innerptr
[1] & 255))
467 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
470 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
472 else if (innername
[0] == '?')
474 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
477 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
479 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
480 snprintf(s
, sizeof(compare
) - (s
- compare
), "{%s}", innername
);
482 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
496 "DEBUG2: %*sBad terminator '%c' at file position %ld...\n",
497 indent
, "", ch
, ftell(in
));
502 * Do the comparison...
508 result
= strcasecmp(outptr
, compare
) < 0;
511 result
= strcasecmp(outptr
, compare
) > 0;
514 result
= strcasecmp(outptr
, compare
) == 0;
517 result
= strcasecmp(outptr
, compare
) != 0;
526 "DEBUG2: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
527 indent
, "", name
, op
, compare
, ftell(in
), result
);
532 * Comparison true; output first part and ignore second...
535 fprintf(stderr
, "DEBUG2: %*sOutput first part...\n", indent
, "");
536 cgi_copy(out
, in
, element
, ':', indent
+ 2);
538 fprintf(stderr
, "DEBUG2: %*sSkip second part...\n", indent
, "");
539 cgi_copy(NULL
, in
, element
, '}', indent
+ 2);
544 * Comparison false; ignore first part and output second...
547 fprintf(stderr
, "DEBUG2: %*sSkip first part...\n", indent
, "");
548 cgi_copy(NULL
, in
, element
, ':', indent
+ 2);
550 fprintf(stderr
, "DEBUG2: %*sOutput second part...\n", indent
, "");
551 cgi_copy(out
, in
, element
, '}', indent
+ 2);
554 fprintf(stderr
, "DEBUG2: %*sFinished \"{%s%c%s\", out=%p...\n", indent
, "",
555 name
, op
, compare
, out
);
557 else if (ch
== '\\') /* Quoted char */
568 fprintf(stderr
, "DEBUG2: %*sReturning at file position %ld on EOF...\n",
569 indent
, "", ftell(in
));
572 "DEBUG2: %*sReturning at file position %ld on character '%c'...\n",
573 indent
, "", ftell(in
), ch
);
575 if (ch
== EOF
&& term
)
576 fprintf(stderr
, "ERROR: %*sSaw EOF, expected '%c'!\n", indent
, "", term
);
579 * Flush any pending output...
588 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
592 cgi_puts(const char *s
, /* I - String to output */
593 FILE *out
) /* I - Output file */
600 * Pass <A HREF="url"> and </A>, otherwise quote it...
603 if (!strncasecmp(s
, "<A HREF=\"", 9))
605 fputs("<A HREF=\"", out
);
608 while (*s
&& *s
!= '\"')
623 else if (!strncasecmp(s
, "</A>", 4))
634 fputs(""", out
);
646 * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
650 cgi_puturi(const char *s
, /* I - String to output */
651 FILE *out
) /* I - Output file */
655 if (strchr("%&+ <>#=", *s
) || *s
& 128)
656 fprintf(out
, "%%%02X", *s
& 255);
666 * End of "$Id: template.c 6717 2007-07-24 23:47:12Z mike $".