]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2e4ff8af | 2 | * "$Id: template.c 6986 2007-09-25 15:34:52Z mike $" |
ef416fc2 | 3 | * |
4 | * CGI template function. | |
5 | * | |
91c84a35 | 6 | * Copyright 2007-2008 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 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" | |
bd7854cb | 28 | #include <errno.h> |
2e4ff8af | 29 | #include <regex.h> |
ef416fc2 | 30 | |
31 | ||
32 | /* | |
33 | * Local functions... | |
34 | */ | |
35 | ||
bd7854cb | 36 | static void cgi_copy(FILE *out, FILE *in, int element, char term, |
37 | int indent); | |
ef416fc2 | 38 | static void cgi_puts(const char *s, FILE *out); |
a74454a7 | 39 | static void cgi_puturi(const char *s, FILE *out); |
ef416fc2 | 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 | ||
355e94dc | 54 | fprintf(stderr, "DEBUG2: cgiCopyTemplateFile(out=%p, tmpl=\"%s\")\n", out, |
f301802f | 55 | tmpl ? tmpl : "(null)"); |
bd7854cb | 56 | |
91c84a35 MS |
57 | /* |
58 | * Range check input... | |
59 | */ | |
60 | ||
61 | if (!tmpl || !out) | |
62 | return; | |
63 | ||
ef416fc2 | 64 | /* |
65 | * Open the template file... | |
66 | */ | |
67 | ||
68 | if ((in = fopen(tmpl, "r")) == NULL) | |
bd7854cb | 69 | { |
70 | fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n", | |
f301802f | 71 | tmpl ? tmpl : "(null)", strerror(errno)); |
ef416fc2 | 72 | return; |
bd7854cb | 73 | } |
ef416fc2 | 74 | |
75 | /* | |
76 | * Parse the file to the end... | |
77 | */ | |
78 | ||
bd7854cb | 79 | cgi_copy(out, in, 0, 0, 0); |
ef416fc2 | 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 | { | |
ef416fc2 | 96 | char filename[1024], /* Filename */ |
db1f069b MS |
97 | locale[16], /* Locale name */ |
98 | *locptr; /* Pointer into locale name */ | |
ef416fc2 | 99 | const char *directory, /* Directory for templates */ |
100 | *lang; /* Language */ | |
101 | FILE *in; /* Input file */ | |
102 | ||
103 | ||
355e94dc | 104 | fprintf(stderr, "DEBUG2: cgiCopyTemplateLang(tmpl=\"%s\")\n", |
f301802f | 105 | tmpl ? tmpl : "(null)"); |
bd7854cb | 106 | |
ef416fc2 | 107 | /* |
108 | * Convert the language to a locale name... | |
109 | */ | |
110 | ||
db1f069b MS |
111 | locale[0] = '\0'; |
112 | ||
ef416fc2 | 113 | if ((lang = getenv("LANG")) != NULL) |
114 | { | |
db1f069b MS |
115 | locale[0] = '/'; |
116 | strlcpy(locale + 1, lang, sizeof(locale) - 1); | |
ef416fc2 | 117 | |
db1f069b MS |
118 | if ((locptr = strchr(locale, '.')) != NULL) |
119 | *locptr = '\0'; /* Strip charset */ | |
ef416fc2 | 120 | } |
ef416fc2 | 121 | |
db1f069b MS |
122 | fprintf(stderr, "DEBUG: lang=\"%s\", locale=\"%s\"...\n", |
123 | lang ? lang : "(null)", locale); | |
bd7854cb | 124 | |
ef416fc2 | 125 | /* |
126 | * See if we have a template file for this language... | |
127 | */ | |
128 | ||
129 | directory = cgiGetTemplateDir(); | |
130 | ||
db1f069b MS |
131 | snprintf(filename, sizeof(filename), "%s%s/%s", directory, locale, tmpl); |
132 | if ((in = fopen(filename, "r")) == NULL) | |
ef416fc2 | 133 | { |
db1f069b | 134 | locale[3] = '\0'; |
ef416fc2 | 135 | |
db1f069b MS |
136 | snprintf(filename, sizeof(filename), "%s%s/%s", directory, locale, tmpl); |
137 | if ((in = fopen(filename, "r")) == NULL) | |
138 | { | |
ef416fc2 | 139 | snprintf(filename, sizeof(filename), "%s/%s", directory, tmpl); |
db1f069b MS |
140 | in = fopen(filename, "r"); |
141 | } | |
ef416fc2 | 142 | } |
143 | ||
355e94dc | 144 | fprintf(stderr, "DEBUG2: Template file is \"%s\"...\n", filename); |
bd7854cb | 145 | |
ef416fc2 | 146 | /* |
147 | * Open the template file... | |
148 | */ | |
149 | ||
db1f069b | 150 | if (!in) |
bd7854cb | 151 | { |
152 | fprintf(stderr, "ERROR: Unable to open template file \"%s\" - %s\n", | |
153 | filename, strerror(errno)); | |
ef416fc2 | 154 | return; |
bd7854cb | 155 | } |
ef416fc2 | 156 | |
157 | /* | |
158 | * Parse the file to the end... | |
159 | */ | |
160 | ||
bd7854cb | 161 | cgi_copy(stdout, in, 0, 0, 0); |
ef416fc2 | 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 | |
bd7854cb | 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 */ | |
ef416fc2 | 225 | { |
bd7854cb | 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 */ | |
a74454a7 | 239 | int uriencode; /* Encode as URI */ |
2e4ff8af | 240 | regex_t re; /* Regular expression to match */ |
bd7854cb | 241 | |
242 | ||
355e94dc | 243 | fprintf(stderr, "DEBUG2: %*sStarting at file position %ld...\n", indent, "", |
bd7854cb | 244 | ftell(in)); |
ef416fc2 | 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 | ||
a74454a7 | 259 | uriencode = 0; |
260 | ||
ef416fc2 | 261 | for (s = name; (ch = getc(in)) != EOF;) |
2e4ff8af | 262 | if (strchr("}]<>=!~ \t\n", ch)) |
ef416fc2 | 263 | break; |
a74454a7 | 264 | else if (s == name && ch == '%') |
265 | uriencode = 1; | |
ef416fc2 | 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 | { | |
355e94dc | 275 | fprintf(stderr, "DEBUG2: %*sLone { at %ld...\n", indent, "", ftell(in)); |
bd7854cb | 276 | |
ef416fc2 | 277 | if (out) |
278 | { | |
279 | putc('{', out); | |
280 | putc(ch, out); | |
281 | } | |
282 | ||
283 | continue; | |
284 | } | |
285 | ||
bd7854cb | 286 | if (ch == '}') |
355e94dc | 287 | fprintf(stderr, "DEBUG2: %*s\"{%s}\" at %ld...\n", indent, "", name, |
bd7854cb | 288 | ftell(in)); |
289 | ||
ef416fc2 | 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 | ||
355e94dc | 351 | fprintf(stderr, "DEBUG2: %*sLooping on \"%s\" at %ld, count=%d...\n", |
bd7854cb | 352 | indent, "", name + 1, pos, count); |
353 | ||
ef416fc2 | 354 | if (count > 0) |
355 | { | |
356 | for (i = 0; i < count; i ++) | |
357 | { | |
bd7854cb | 358 | if (i) |
359 | fseek(in, pos, SEEK_SET); | |
360 | ||
361 | cgi_copy(out, in, i, '}', indent + 2); | |
ef416fc2 | 362 | } |
363 | } | |
364 | else | |
bd7854cb | 365 | cgi_copy(NULL, in, 0, '}', indent + 2); |
366 | ||
355e94dc | 367 | fprintf(stderr, "DEBUG2: %*sFinished looping on \"%s\"...\n", indent, |
bd7854cb | 368 | "", name + 1); |
ef416fc2 | 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) | |
a74454a7 | 409 | { |
410 | if (uriencode) | |
411 | cgi_puturi(outptr, out); | |
355e94dc MS |
412 | else if (!strcasecmp(name, "?cupsdconf_default")) |
413 | fputs(outptr, stdout); | |
a74454a7 | 414 | else |
415 | cgi_puts(outptr, out); | |
416 | } | |
ef416fc2 | 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 | |
2e4ff8af | 429 | * {name~refex?true:false} Regex match |
ef416fc2 | 430 | */ |
431 | ||
bd7854cb | 432 | op = ch; |
433 | ||
ef416fc2 | 434 | if (ch == '?') |
435 | { | |
436 | /* | |
437 | * Test for existance... | |
438 | */ | |
439 | ||
bd7854cb | 440 | result = cgiGetArray(name, element) != NULL && outptr[0]; |
441 | compare[0] = '\0'; | |
ef416fc2 | 442 | } |
443 | else | |
444 | { | |
445 | /* | |
446 | * Compare to a string... | |
447 | */ | |
448 | ||
ef416fc2 | 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 != '?') | |
bd7854cb | 504 | { |
505 | fprintf(stderr, | |
355e94dc | 506 | "DEBUG2: %*sBad terminator '%c' at file position %ld...\n", |
bd7854cb | 507 | indent, "", ch, ftell(in)); |
ef416fc2 | 508 | return; |
bd7854cb | 509 | } |
ef416fc2 | 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; | |
2e4ff8af MS |
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; | |
ef416fc2 | 562 | default : |
563 | result = 1; | |
564 | break; | |
565 | } | |
566 | } | |
567 | ||
bd7854cb | 568 | fprintf(stderr, |
355e94dc | 569 | "DEBUG2: %*sStarting \"{%s%c%s\" at %ld, result=%d...\n", |
bd7854cb | 570 | indent, "", name, op, compare, ftell(in), result); |
571 | ||
ef416fc2 | 572 | if (result) |
573 | { | |
574 | /* | |
575 | * Comparison true; output first part and ignore second... | |
576 | */ | |
577 | ||
355e94dc | 578 | fprintf(stderr, "DEBUG2: %*sOutput first part...\n", indent, ""); |
bd7854cb | 579 | cgi_copy(out, in, element, ':', indent + 2); |
580 | ||
355e94dc | 581 | fprintf(stderr, "DEBUG2: %*sSkip second part...\n", indent, ""); |
bd7854cb | 582 | cgi_copy(NULL, in, element, '}', indent + 2); |
ef416fc2 | 583 | } |
584 | else | |
585 | { | |
586 | /* | |
587 | * Comparison false; ignore first part and output second... | |
588 | */ | |
589 | ||
355e94dc | 590 | fprintf(stderr, "DEBUG2: %*sSkip first part...\n", indent, ""); |
bd7854cb | 591 | cgi_copy(NULL, in, element, ':', indent + 2); |
592 | ||
355e94dc | 593 | fprintf(stderr, "DEBUG2: %*sOutput second part...\n", indent, ""); |
bd7854cb | 594 | cgi_copy(out, in, element, '}', indent + 2); |
ef416fc2 | 595 | } |
bd7854cb | 596 | |
355e94dc | 597 | fprintf(stderr, "DEBUG2: %*sFinished \"{%s%c%s\", out=%p...\n", indent, "", |
bd7854cb | 598 | name, op, compare, out); |
ef416fc2 | 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 | ||
bd7854cb | 610 | if (ch == EOF) |
355e94dc | 611 | fprintf(stderr, "DEBUG2: %*sReturning at file position %ld on EOF...\n", |
bd7854cb | 612 | indent, "", ftell(in)); |
613 | else | |
614 | fprintf(stderr, | |
355e94dc | 615 | "DEBUG2: %*sReturning at file position %ld on character '%c'...\n", |
bd7854cb | 616 | indent, "", ftell(in), ch); |
617 | ||
618 | if (ch == EOF && term) | |
619 | fprintf(stderr, "ERROR: %*sSaw EOF, expected '%c'!\n", indent, "", term); | |
620 | ||
ef416fc2 | 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("&", 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("<", out); | |
673 | } | |
674 | else if (*s == '>') | |
675 | fputs(">", out); | |
676 | else if (*s == '\"') | |
677 | fputs(""", out); | |
678 | else if (*s == '&') | |
679 | fputs("&", out); | |
680 | else | |
681 | putc(*s, out); | |
682 | ||
683 | s ++; | |
684 | } | |
685 | } | |
686 | ||
687 | ||
688 | /* | |
a74454a7 | 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 | /* | |
2e4ff8af | 709 | * End of "$Id: template.c 6986 2007-09-25 15:34:52Z mike $". |
ef416fc2 | 710 | */ |