]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/template.c
2 * "$Id: template.c 4921 2006-01-12 21:26:26Z mike $"
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"
43 static void cgi_copy(FILE *out
, FILE *in
, int element
, char term
);
44 static void cgi_puts(const char *s
, FILE *out
);
48 * 'cgiCopyTemplateFile()' - Copy a template file and replace all the
49 * '{variable}' strings with the variable value.
53 cgiCopyTemplateFile(FILE *out
, /* I - Output file */
54 const char *tmpl
) /* I - Template file to read */
56 FILE *in
; /* Input file */
60 * Open the template file...
63 if ((in
= fopen(tmpl
, "r")) == NULL
)
67 * Parse the file to the end...
70 cgi_copy(out
, in
, 0, 0);
73 * Close the template file and return...
81 * 'cgiCopyTemplateLang()' - Copy a template file using a language...
85 cgiCopyTemplateLang(const char *tmpl
) /* I - Base filename */
87 int i
; /* Looping var */
88 char filename
[1024], /* Filename */
89 locale
[16]; /* Locale name */
90 const char *directory
, /* Directory for templates */
92 FILE *in
; /* Input file */
96 * Convert the language to a locale name...
99 if ((lang
= getenv("LANG")) != NULL
)
101 for (i
= 0; lang
[i
] && i
< 15; i
++)
102 if (isalnum(lang
[i
] & 255))
103 locale
[i
] = tolower(lang
[i
]);
113 * See if we have a template file for this language...
116 directory
= cgiGetTemplateDir();
118 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
119 if (access(filename
, 0))
123 snprintf(filename
, sizeof(filename
), "%s/%s/%s", directory
, locale
, tmpl
);
124 if (access(filename
, 0))
125 snprintf(filename
, sizeof(filename
), "%s/%s", directory
, tmpl
);
129 * Open the template file...
132 if ((in
= fopen(filename
, "r")) == NULL
)
136 * Parse the file to the end...
139 cgi_copy(stdout
, in
, 0, 0);
142 * Close the template file and return...
150 * 'cgiGetTemplateDir()' - Get the templates directory...
153 char * /* O - Template directory */
154 cgiGetTemplateDir(void)
156 const char *datadir
; /* CUPS_DATADIR env var */
157 static char templates
[1024] = ""; /* Template directory */
163 * Build the template directory pathname...
166 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
167 datadir
= CUPS_DATADIR
;
169 snprintf(templates
, sizeof(templates
), "%s/templates", datadir
);
177 * 'cgiSetServerVersion()' - Set the server name and CUPS version...
181 cgiSetServerVersion(void)
183 cgiSetVariable("SERVER_NAME", getenv("SERVER_NAME"));
184 cgiSetVariable("REMOTE_USER", getenv("REMOTE_USER"));
185 cgiSetVariable("CUPS_VERSION", CUPS_SVERSION
);
188 setlocale(LC_TIME
, "");
194 * 'cgi_copy()' - Copy the template file, substituting as needed...
198 cgi_copy(FILE *out
, /* I - Output file */
199 FILE *in
, /* I - Input file */
200 int element
, /* I - Element number (0 to N) */
201 char term
) /* I - Terminating character */
203 int ch
; /* Character from file */
204 char op
; /* Operation */
205 char name
[255], /* Name of variable */
206 *nameptr
, /* Pointer into name */
207 innername
[255], /* Inner comparison name */
208 *innerptr
, /* Pointer into inner name */
209 *s
; /* String pointer */
210 const char *value
; /* Value of variable */
211 const char *innerval
; /* Inner value */
212 const char *outptr
; /* Output string pointer */
213 char outval
[1024], /* Formatted output string */
214 compare
[1024]; /* Comparison string */
215 int result
; /* Result of comparison */
219 * Parse the file to the end...
222 while ((ch
= getc(in
)) != EOF
)
228 * Get a variable name...
231 for (s
= name
; (ch
= getc(in
)) != EOF
;)
232 if (strchr("}]<>=! \t\n", ch
))
234 else if (s
> name
&& ch
== '?')
236 else if (s
< (name
+ sizeof(name
) - 1))
241 if (s
== name
&& isspace(ch
& 255))
253 * See if it has a value...
259 * Insert value only if it exists...
262 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
266 if ((value
= cgiGetArray(name
+ 1, atoi(nameptr
) - 1)) != NULL
)
274 else if ((value
= cgiGetArray(name
+ 1, element
)) != NULL
)
282 else if (name
[0] == '#')
289 sprintf(outval
, "%d", cgiGetSize(name
+ 1));
291 sprintf(outval
, "%d", element
+ 1);
295 else if (name
[0] == '[')
298 * Loop for # of elements...
301 int i
; /* Looping var */
302 long pos
; /* File position */
303 int count
; /* Number of elements */
306 if (isdigit(name
[1] & 255))
307 count
= atoi(name
+ 1);
309 count
= cgiGetSize(name
+ 1);
315 for (i
= 0; i
< count
; i
++)
317 fseek(in
, pos
, SEEK_SET
);
318 cgi_copy(out
, in
, i
, '}');
322 cgi_copy(NULL
, in
, 0, '}');
329 * Insert variable or variable name (if element is NULL)...
332 if ((nameptr
= strrchr(name
, '-')) != NULL
&& isdigit(nameptr
[1] & 255))
335 if ((value
= cgiGetArray(name
, atoi(nameptr
) - 1)) == NULL
)
337 snprintf(outval
, sizeof(outval
), "{%s}", name
);
343 else if ((value
= cgiGetArray(name
, element
)) == NULL
)
345 snprintf(outval
, sizeof(outval
), "{%s}", name
);
353 * See if the terminating character requires another test...
359 * End of substitution...
363 cgi_puts(outptr
, out
);
369 * OK, process one of the following checks:
371 * {name?exist:not-exist} Exists?
372 * {name=value?true:false} Equal
373 * {name<value?true:false} Less than
374 * {name>value?true:false} Greater than
375 * {name!value?true:false} Not equal
381 * Test for existance...
384 result
= cgiGetArray(name
, element
) != NULL
&& outptr
[0];
389 * Compare to a string...
394 for (s
= compare
; (ch
= getc(in
)) != EOF
;)
397 else if (s
>= (compare
+ sizeof(compare
) - 1))
401 sprintf(s
, "%d", element
+ 1);
407 * Grab the value of a variable...
410 innerptr
= innername
;
411 while ((ch
= getc(in
)) != EOF
&& ch
!= '}')
412 if (innerptr
< (innername
+ sizeof(innername
) - 1))
416 if (innername
[0] == '#')
417 sprintf(s
, "%d", cgiGetSize(innername
+ 1));
418 else if ((innerptr
= strrchr(innername
, '-')) != NULL
&&
419 isdigit(innerptr
[1] & 255))
422 if ((innerval
= cgiGetArray(innername
, atoi(innerptr
) - 1)) == NULL
)
425 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
427 else if (innername
[0] == '?')
429 if ((innerval
= cgiGetArray(innername
+ 1, element
)) == NULL
)
432 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
434 else if ((innerval
= cgiGetArray(innername
, element
)) == NULL
)
435 snprintf(s
, sizeof(compare
) - (s
- compare
), "{%s}", innername
);
437 strlcpy(s
, innerval
, sizeof(compare
) - (s
- compare
));
452 * Do the comparison...
458 result
= strcasecmp(outptr
, compare
) < 0;
461 result
= strcasecmp(outptr
, compare
) > 0;
464 result
= strcasecmp(outptr
, compare
) == 0;
467 result
= strcasecmp(outptr
, compare
) != 0;
478 * Comparison true; output first part and ignore second...
481 cgi_copy(out
, in
, element
, ':');
482 cgi_copy(NULL
, in
, element
, '}');
487 * Comparison false; ignore first part and output second...
490 cgi_copy(NULL
, in
, element
, ':');
491 cgi_copy(out
, in
, element
, '}');
494 else if (ch
== '\\') /* Quoted char */
505 * Flush any pending output...
514 * 'cgi_puts()' - Put a string to the output file, quoting as needed...
518 cgi_puts(const char *s
, /* I - String to output */
519 FILE *out
) /* I - Output file */
526 * Pass <A HREF="url"> and </A>, otherwise quote it...
529 if (!strncasecmp(s
, "<A HREF=\"", 9))
531 fputs("<A HREF=\"", out
);
534 while (*s
&& *s
!= '\"')
549 else if (!strncasecmp(s
, "</A>", 4))
560 fputs(""", out
);
572 * End of "$Id: template.c 4921 2006-01-12 21:26:26Z mike $".