]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
2 * "$Id: template.c 6649 2007-07-11 21:46:42Z 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
, "DEBUG: 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
, "DEBUG: 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
, "DEBUG: 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
, "DEBUG: 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
, "DEBUG: %*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
, "DEBUG: %*sLone { at %ld...\n", indent
, "", ftell(in
));
278 fprintf(stderr
, "DEBUG: %*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
, "DEBUG: %*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
, "DEBUG: %*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
);
404 cgi_puts(outptr
, out
);
411 * OK, process one of the following checks:
413 * {name?exist:not-exist} Exists?
414 * {name=value?true:false} Equal
415 * {name<value?true:false} Less than
416 * {name>value?true:false} Greater than
417 * {name!value?true:false} Not equal
425 * Test for existance...
428 result
= cgiGetArray(name
, element
) != NULL
&& outptr
[0];
434 * Compare to a string...
437 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
440 else if (s
>= (compare
+ sizeof(compare
) - 1))
444 sprintf(s
, "%d", element
+ 1);
450 * Grab the value of a variable...
453 innerptr
= innername
;
454 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
455 if (innerptr
< (innername
+ sizeof(innername
) - 1))
459 if (innername
[0] == '#')
460 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
461 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
462 isdigit(innerptr
[1] & 255))
465 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
468 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
470 else if (innername
[0] == '?')
472 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
475 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
477 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
478 snprintf(s
, sizeof(compare
) - (s
- compare
), "{%s}", innername
);
480 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
494 "DEBUG: %*sBad terminator '%c' at file position %ld...\n",
495 indent
, "", ch
, ftell(in
));
500 * Do the comparison...
506 result
= strcasecmp(outptr
, compare
) < 0;
509 result
= strcasecmp(outptr
, compare
) > 0;
512 result
= strcasecmp(outptr
, compare
) == 0;
515 result
= strcasecmp(outptr
, compare
) != 0;
524 "DEBUG: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n",
525 indent
, "", name
, op
, compare
, ftell(in
), result
);
530 * Comparison true; output first part and ignore second...
533 fprintf(stderr
, "DEBUG: %*sOutput first part...\n", indent
, "");
534 cgi_copy(out
, in
, element
, ':', indent
+ 2);
536 fprintf(stderr
, "DEBUG: %*sSkip second part...\n", indent
, "");
537 cgi_copy(NULL
, in
, element
, '}', indent
+ 2);
542 * Comparison false; ignore first part and output second...
545 fprintf(stderr
, "DEBUG: %*sSkip first part...\n", indent
, "");
546 cgi_copy(NULL
, in
, element
, ':', indent
+ 2);
548 fprintf(stderr
, "DEBUG: %*sOutput second part...\n", indent
, "");
549 cgi_copy(out
, in
, element
, '}', indent
+ 2);
552 fprintf(stderr
, "DEBUG: %*sFinished \"{%s%c%s\", out=%p...\n", indent
, "",
553 name
, op
, compare
, out
);
555 else if (ch
== '\\') /* Quoted char */
566 fprintf(stderr
, "DEBUG: %*sReturning at file position %ld on EOF...\n",
567 indent
, "", ftell(in
));
570 "DEBUG: %*sReturning at file position %ld on character '%c'...\n",
571 indent
, "", ftell(in
), ch
);
573 if (ch
== EOF
&& term
)
574 fprintf(stderr
, "ERROR: %*sSaw EOF, expected '%c'!\n", indent
, "", term
);
577 * Flush any pending output...
586 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
590 cgi_puts(const char *s
, /* I - String to output */
591 FILE *out
) /* I - Output file */
598 * Pass <A HREF="url"> and </A>, otherwise quote it...
601 if (!strncasecmp(s
, "<A HREF=\"", 9))
603 fputs("<A HREF=\"", out
);
606 while (*s
&& *s
!= '\"')
621 else if (!strncasecmp(s
, "</A>", 4))
632 fputs(""", out
);
644 * 'cgi_puturi()' - Put a URI string to the output file, quoting as needed...
648 cgi_puturi(const char *s
, /* I - String to output */
649 FILE *out
) /* I - Output file */
653 if (strchr("%&+ <>#=", *s
) || *s
& 128)
654 fprintf(out
, "%%%02X", *s
& 255);
664 * End of "$Id: template.c 6649 2007-07-11 21:46:42Z mike $".