1 const char cgi_rcs[] = "$Id: cgi.c,v 1.29 2001/09/20 15:47:44 steudten Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgi.c,v $
6 * Purpose : Declares functions to intercept request, generate
7 * html or gif answers, and to compose HTTP resonses.
8 * This only contains the framework functions, the
9 * actual handler functions are declared elsewhere.
11 * Functions declared include:
14 * Copyright : Written by and Copyright (C) 2001 the SourceForge
15 * IJBSWA team. http://ijbswa.sourceforge.net
17 * Based on the Internet Junkbuster originally written
18 * by and Copyright (C) 1997 Anonymous Coders and
19 * Junkbusters Corporation. http://www.junkbusters.com
21 * This program is free software; you can redistribute it
22 * and/or modify it under the terms of the GNU General
23 * Public License as published by the Free Software
24 * Foundation; either version 2 of the License, or (at
25 * your option) any later version.
27 * This program is distributed in the hope that it will
28 * be useful, but WITHOUT ANY WARRANTY; without even the
29 * implied warranty of MERCHANTABILITY or FITNESS FOR A
30 * PARTICULAR PURPOSE. See the GNU General Public
31 * License for more details.
33 * The GNU General Public License should be included with
34 * this file. If not, you can view it at
35 * http://www.gnu.org/copyleft/gpl.html
36 * or write to the Free Software Foundation, Inc., 59
37 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 * Revision 1.29 2001/09/20 15:47:44 steudten
43 * Fix BUG: Modify int size to size_t size in fill_template()
44 * - removes big trouble on machines where sizeof(int) != sizeof(size_t).
46 * Revision 1.28 2001/09/19 18:00:37 oes
47 * - Deletef time() FIXME (Can't fail under Linux either, if
48 * the argument is guaranteed to be in out address space,
51 * - Pointer notation cosmetics
52 * - Fixed a minor bug in template_fill(): Failiure of
53 * pcrs_execute() now secure.
55 * Revision 1.27 2001/09/16 17:08:54 jongfoster
56 * Moving simple CGI functions from cgi.c to new file cgisimple.c
58 * Revision 1.26 2001/09/16 15:47:37 jongfoster
59 * First version of CGI-based edit interface. This is very much a
60 * work-in-progress, and you can't actually use it to edit anything
61 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
64 * Revision 1.25 2001/09/16 15:02:35 jongfoster
65 * Adding i.j.b/robots.txt.
66 * Inlining add_stats() since it's only ever called from one place.
68 * Revision 1.24 2001/09/16 11:38:01 jongfoster
69 * Splitting fill_template() into 2 functions:
70 * template_load() loads the file
71 * template_fill() performs the PCRS regexps.
72 * This is because the CGI edit interface has a "table row"
73 * template which is used many times in the page - this
74 * change means it's only loaded from disk once.
76 * Revision 1.23 2001/09/16 11:16:05 jongfoster
77 * Better error handling in dispatch_cgi() and parse_cgi_parameters()
79 * Revision 1.22 2001/09/16 11:00:10 jongfoster
80 * New function alloc_http_response, for symmetry with free_http_response
82 * Revision 1.21 2001/09/13 23:53:03 jongfoster
83 * Support for both static and dynamically generated CGI pages.
84 * Correctly setting Last-Modified: and Expires: HTTP headers.
86 * Revision 1.20 2001/09/13 23:40:36 jongfoster
87 * (Cosmetic only) Indentation correction
89 * Revision 1.19 2001/09/13 23:31:25 jongfoster
90 * Moving image data to cgi.c rather than cgi.h.
92 * Revision 1.18 2001/08/05 16:06:20 jongfoster
93 * Modifiying "struct map" so that there are now separate header and
94 * "map_entry" structures. This means that functions which modify a
95 * map no longer need to return a pointer to the modified map.
96 * Also, it no longer reverses the order of the entries (which may be
97 * important with some advanced template substitutions).
99 * Revision 1.17 2001/08/05 15:57:38 oes
100 * Adapted finish_http_response to new list_to_text
102 * Revision 1.16 2001/08/01 21:33:18 jongfoster
103 * Changes to fill_template() that reduce memory usage without having
104 * an impact on performance. I also renamed some variables so as not
105 * to clash with the C++ keywords "new" and "template".
107 * Revision 1.15 2001/08/01 21:19:22 jongfoster
108 * Moving file version information to a separate CGI page.
110 * Revision 1.14 2001/08/01 00:19:03 jongfoster
111 * New function: map_conditional() for an if-then-else syntax.
112 * Changing to use new version of show_defines()
114 * Revision 1.13 2001/07/30 22:08:36 jongfoster
115 * Tidying up #defines:
116 * - All feature #defines are now of the form FEATURE_xxx
117 * - Permanently turned off WIN_GUI_EDIT
118 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
120 * Revision 1.12 2001/07/29 18:47:05 jongfoster
121 * Adding missing #include "loadcfg.h"
123 * Revision 1.11 2001/07/18 17:24:37 oes
124 * Changed to conform to new pcrs interface
126 * Revision 1.10 2001/07/13 13:53:13 oes
127 * Removed all #ifdef PCRS and related code
129 * Revision 1.9 2001/06/29 21:45:41 oes
130 * Indentation, CRLF->LF, Tab-> Space
132 * Revision 1.8 2001/06/29 13:21:46 oes
133 * - Cosmetics: renamed and reordered functions, variables,
134 * texts, improved comments etc
136 * - Removed ij_untrusted_url() The relevant
137 * info is now part of the "untrusted" page,
138 * which is generated by filters.c:trust_url()
140 * - Generators of content now call finish_http_response()
141 * themselves, making jcc.c:chat() a little less
144 * - Removed obsolete "Pragma: no-cache" from our headers
146 * - http_responses now know their head length
148 * - fill_template now uses the new interface to pcrs, so that
149 * - long jobs (like whole files) no longer have to be assembled
150 * in a fixed size buffer
151 * - the new T (trivial) option is used, and the replacement may
152 * contain Perl syntax backrefs without confusing pcrs
154 * - Introduced default_exports() which generates a set of exports
155 * common to all CGIs and other content generators
157 * - Introduced convenience function map_block_killer()
159 * - Introduced convenience function make_menu()
161 * - Introduced CGI-like function error_response() which generates
162 * the "No such domain" and "Connect failed" messages using the
165 * - cgi_show_url_info:
166 * - adapted to new CGI features
167 * - form and answers now generated from same template
168 * - http:// prefix in URL now OK
171 * - adapted to new CGI features
172 * - no longer uses csp->init_proxy_args
175 * - moved menu generation to make_menu()
177 * - add_stats now writes single export map entries instead
180 * - Moved redirect_url() to filters.c
182 * - Fixed mem leak in free_http_response(), map_block_killer(),
184 * - Removed logentry from cancelled commit
186 * Revision 1.7 2001/06/09 10:51:58 jongfoster
187 * Changing "show URL info" handler to new style.
188 * Changing BUFSIZ ==> BUFFER_SIZE
190 * Revision 1.6 2001/06/07 23:05:19 jongfoster
191 * Removing code related to old forward and ACL files.
193 * Revision 1.5 2001/06/05 19:59:16 jongfoster
194 * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
196 * Revision 1.4 2001/06/04 10:41:52 swa
197 * show version string of cgi.h and cgi.c
199 * Revision 1.3 2001/06/03 19:12:16 oes
200 * introduced new cgi handling
202 * No revisions before 1.3
204 **********************************************************************/
210 #include <sys/types.h>
217 #define snprintf _snprintf
218 #endif /* def _WIN32 */
226 #include "miscutil.h"
227 #include "cgisimple.h"
228 #ifdef FEATURE_CGI_EDIT_ACTIONS
230 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
232 const char cgi_h_rcs[] = CGI_H_VERSION;
234 const struct cgi_dispatcher cgi_dispatcher[] = {
237 "HIDE Sends a robots.txt file to tell robots to go away." },
240 "Show information about the current configuration" },
242 13, cgi_show_url_info,
243 "Show which actions apply to a URL and why" },
245 12, cgi_show_version,
246 "Show the source code version numbers" },
249 "HIDE Send the transparent or \"Junkbuster\" gif" },
250 #ifdef FEATURE_CGI_EDIT_ACTIONS
251 { "edit-actions-list",
252 17, cgi_edit_actions_list,
253 "Edit the actions list" },
254 { "edit-actions-submit",
255 19, cgi_edit_actions_submit,
256 "HIDE Change the actions for (a) specified URL(s)" },
258 12, cgi_edit_actions,
259 "HIDE Edit the actions for (a) specified URL(s)" },
260 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
262 12, cgi_show_request,
263 "Show the client's request headers." },
266 "Junkbuster main page" },
267 { NULL, 0, NULL, NULL }
274 * Hint: You can encode your own GIFs like that:
275 * perl -e 'while (read STDIN, $c, 1) { printf("\\%.3o,", unpack("C", $c)); }'
278 const char image_junkbuster_gif_data[] =
279 "GIF89aD\000\013\000\360\000\000\000\000\000\377\377\377!"
280 "\371\004\001\000\000\001\000,\000\000\000\000D\000\013\000"
281 "\000\002a\214\217\251\313\355\277\000\200G&K\025\316hC\037"
282 "\200\234\230Y\2309\235S\230\266\206\372J\253<\3131\253\271"
283 "\270\215\342\254\013\203\371\202\264\334P\207\332\020o\266"
284 "N\215I\332=\211\312\3513\266:\026AK)\364\370\365aobr\305"
285 "\372\003S\275\274k2\354\254z\347?\335\274x\306^9\374\276"
288 const int image_junkbuster_gif_length = sizeof(image_junkbuster_gif_data) - 1;
291 const char image_blank_gif_data[] =
292 "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000"
293 "\000!\371\004\001\000\000\000\000,\000\000\000\000\001"
294 "\000\001\000\000\002\002D\001\000;";
296 const int image_blank_gif_length = sizeof(image_blank_gif_data) - 1;
299 /*********************************************************************
301 * Function : dispatch_cgi
303 * Description : Checks if a request URL has either the magical hostname
304 * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
305 * the (rest of the) path as a cgi name plus query string,
306 * prepares a map that maps CGI parameter names to their values,
307 * initializes the http_response struct, and calls the
308 * relevant CGI handler function.
311 * 1 : csp = Current client state (buffers, headers, etc...)
313 * Returns : http_response if match, NULL if nonmatch or handler fail
315 *********************************************************************/
316 struct http_response *dispatch_cgi(struct client_state *csp)
318 char *argstring = NULL;
319 const struct cgi_dispatcher *d;
320 struct map *param_list;
321 struct http_response *rsp;
324 * Should we intercept ?
327 /* Either the host matches CGI_PREFIX_HOST ..*/
328 if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
330 /* ..then the path will all be for us */
331 argstring = csp->http->path;
333 /* Or it's the host part HOME_PAGE_URL, and the path /config ? */
334 else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
335 && (0 == strncmpic(csp->http->path,"/config", 7))
336 && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
338 /* then it's everything following "/config" */
339 argstring = csp->http->path + 7;
347 * This is a CGI call.
350 /* Get mem for response or fail*/
351 if (NULL == (rsp = alloc_http_response()))
357 /* Remove leading slash */
358 if (*argstring == '/')
363 log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
364 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
365 csp->ip_addr_str, csp->http->cmd);
367 /* Find and start the right CGI function*/
368 for (d = cgi_dispatcher; d->handler; d++)
370 if (strncmp(argstring, d->name, d->name_length) == 0)
372 if (NULL == (param_list =
373 parse_cgi_parameters(argstring + d->name_length)))
375 free_map(param_list);
376 free_http_response(rsp);
379 if ((d->handler)(csp, rsp, param_list))
381 free_map(param_list);
382 free_http_response(rsp);
386 free_map(param_list);
387 return(finish_http_response(rsp));
391 /* Can't get here, since cgi_default will match all requests */
392 free_http_response(rsp);
398 /*********************************************************************
400 * Function : parse_cgi_parameters
402 * Description : Parse a URL-encoded argument string into name/value
403 * pairs and store them in a struct map list.
406 * 1 : string = string to be parsed
408 * Returns : pointer to param list, or NULL if out of memory.
410 *********************************************************************/
411 struct map *parse_cgi_parameters(char *argstring)
414 char *vector[BUFFER_SIZE];
416 struct map *cgi_params;
418 if (NULL == (cgi_params = new_map()))
423 if(*argstring == '?')
427 if (NULL == (tmp = strdup(argstring)))
429 free_map(cgi_params);
433 pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
435 for (i = 0; i < pairs; i++)
437 if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
440 map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
450 /*********************************************************************
452 * Function : error_response
454 * Description : returns an http_response that explains the reason
455 * why a request failed.
458 * 1 : csp = Current client state (buffers, headers, etc...)
459 * 2 : templatename = Which template should be used for the answer
460 * 3 : errno = system error number
462 * Returns : NULL if no memory, else http_response
464 *********************************************************************/
465 struct http_response *error_response(struct client_state *csp, const char *templatename, int err)
467 struct http_response *rsp;
468 struct map * exports = default_exports(csp, NULL);
470 if (NULL == (rsp = alloc_http_response()))
475 map(exports, "host-html", 1, html_encode(csp->http->host), 0);
476 map(exports, "hostport", 1, csp->http->hostport, 1);
477 map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
478 map(exports, "path", 1, csp->http->path, 1);
479 map(exports, "path-html", 1, html_encode(csp->http->path), 0);
480 map(exports, "error", 1, safe_strerror(err), 0);
481 map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
483 rsp->body = template_load(csp, templatename);
484 template_fill(&rsp->body, exports);
487 if (!strcmp(templatename, "no-such-domain"))
489 rsp->status = strdup("404 No such domain");
491 else if (!strcmp(templatename, "connect-failed"))
493 rsp->status = strdup("503 Connect failed");
496 return(finish_http_response(rsp));
501 /*********************************************************************
503 * Function : get_http_time
505 * Description : Get the time in a format suitable for use in a
506 * HTTP header - e.g.:
507 * "Sun, 06 Nov 1994 08:49:37 GMT"
510 * 1 : time_offset = Time returned will be current time
511 * plus this number of seconds.
512 * 2 : buf = Destination for result. Must be long enough
513 * to hold 29 characters plus a trailing zero.
517 *********************************************************************/
518 void get_http_time(int time_offset, char *buf)
520 static const char day_names[7][4] =
521 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
522 static const char month_names[12][4] =
523 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
524 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
531 time(¤t_time); /* get current time */
533 current_time += time_offset;
535 /* get and save the gmt */
536 t = gmtime(¤t_time);
538 /* Format: "Sun, 06 Nov 1994 08:49:37 GMT" */
540 "%s, %02d %s %4d %02d:%02d:%02d GMT",
541 day_names[t->tm_wday],
543 month_names[t->tm_mon],
554 /*********************************************************************
556 * Function : finish_http_response
558 * Description : Fill in the missing headers in an http response,
559 * and flatten the headers to an http head.
562 * 1 : rsp = pointer to http_response to be processed
564 * Returns : http_response, or NULL on failiure
566 *********************************************************************/
567 struct http_response *finish_http_response(struct http_response *rsp)
569 char buf[BUFFER_SIZE];
572 * Fill in the HTTP Status
574 sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
575 enlist_first(rsp->headers, buf);
578 * Set the Content-Length
580 if (rsp->content_length == 0)
582 rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
584 sprintf(buf, "Content-Length: %d", rsp->content_length);
585 enlist(rsp->headers, buf);
588 * Fill in the default headers:
590 * Content-Type: default to text/html if not already specified.
591 * Date: set to current date/time.
592 * Last-Modified: set to date/time the page was last changed.
593 * Expires: set to date/time page next needs reloading.
594 * Cache-Control: set to "no-cache" if applicable.
596 * See http://www.w3.org/Protocols/rfc2068/rfc2068
598 enlist_unique(rsp->headers, "Content-Type: text/html", 13);
603 * Set Expires to about 10 min into the future so it'll get reloaded
604 * occasionally, e.g. if IJB gets upgraded.
607 get_http_time(0, buf);
608 enlist_unique_header(rsp->headers, "Date", buf);
610 /* Some date in the past. */
611 enlist_unique_header(rsp->headers, "Last-Modified", "Sat, 17 Jun 2000 12:00:00 GMT");
613 get_http_time(10 * 60, buf); /* 10 * 60sec = 10 minutes */
614 enlist_unique_header(rsp->headers, "Expires", buf);
619 * Compliant browsers should not cache this due to the "Cache-Control"
620 * setting. However, to be certain, we also set both "Last-Modified"
621 * and "Expires" to the current time.
623 enlist_unique_header(rsp->headers, "Cache-Control", "no-cache");
624 get_http_time(0, buf);
625 enlist_unique_header(rsp->headers, "Date", buf);
626 enlist_unique_header(rsp->headers, "Last-Modified", buf);
627 enlist_unique_header(rsp->headers, "Expires", buf);
634 if (NULL == (rsp->head = list_to_text(rsp->headers)))
636 free_http_response(rsp);
639 rsp->head_length = strlen(rsp->head);
646 /*********************************************************************
648 * Function : alloc_http_response
650 * Description : Allocates a new http_response structure.
654 * Returns : pointer to a new http_response, or NULL.
656 *********************************************************************/
657 struct http_response *alloc_http_response(void)
659 return (struct http_response *) zalloc(sizeof(struct http_response));
664 /*********************************************************************
666 * Function : free_http_response
668 * Description : Free the memory occupied by an http_response
669 * and its depandant structures.
672 * 1 : rsp = pointer to http_response to be freed
676 *********************************************************************/
677 void free_http_response(struct http_response *rsp)
684 destroy_list(rsp->headers);
691 /*********************************************************************
693 * Function : fill_template
695 * Description : CGI support function that loads a given HTML
696 * template from the confdir, ignoring comment
700 * 1 : csp = Current client state (buffers, headers, etc...)
701 * 3 : template = name of the HTML template to be used
703 * Returns : char * with loaded template, or NULL if failure
705 *********************************************************************/
706 char *template_load(struct client_state *csp, const char *templatename)
708 char buf[BUFFER_SIZE];
709 char *file_buffer = NULL;
713 * Open template file or fail
715 snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, templatename);
717 if(NULL == (fp = fopen(buf, "r")))
719 log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
725 * Read the file, ignoring comments
727 while (fgets(buf, BUFFER_SIZE, fp))
729 /* skip lines starting with '#' */
735 file_buffer = strsav(file_buffer, buf);
744 /*********************************************************************
746 * Function : fill_template
748 * Description : CGI support function that fills in a pre-loaded
749 * HTML template by replacing @name@ with value using
750 * pcrs, for each item in the output map.
752 * Note that a leading '$' charachter in the export map's
753 * values will be stripped and toggle on backreference
757 * 1 : template_ptr = IN: Template to be filled out.
759 * OUT: Filled out template.
760 * Caller must free().
761 * 2 : exports = map with fill in symbol -> name pairs
765 *********************************************************************/
766 void template_fill(char **template_ptr, struct map *exports)
770 char buf[BUFFER_SIZE];
771 char *tmp_out_buffer;
777 assert(template_ptr);
778 assert(*template_ptr);
781 file_buffer = *template_ptr;
782 size = strlen(file_buffer) + 1;
785 * Assemble pcrs joblist from exports map
787 for (m = exports->first; m != NULL; m = m->next)
792 * First character of name is '$', so remove this flag
793 * character and allow backreferences ($1 etc) in the
794 * "replace with" text.
796 snprintf(buf, BUFFER_SIZE, "%s", m->name + 1);
802 * Treat the "replace with" text as a literal string -
803 * no quoting needed, no backreferences allowed.
804 * ("Trivial" ['T'] flag).
808 /* Enclose name in @@ */
809 snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
813 log_error(LOG_LEVEL_CGI, "Substituting: s/%s/%s/%s", buf, m->value, flags);
815 /* Make and run job. */
816 job = pcrs_compile(buf, m->value, flags, &error);
819 log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
823 pcrs_execute(job, file_buffer, size, &tmp_out_buffer, &size);
824 if (NULL != tmp_out_buffer)
827 file_buffer = tmp_out_buffer;
836 *template_ptr = file_buffer;
841 /*********************************************************************
843 * Function : default_exports
845 * Description : returns a struct map list that contains exports
846 * which are common to all CGI functions.
849 * 1 : exports = Structure to write output to. This
850 * structure should be newly allocated and will be
852 * 1 : csp = Current client state (buffers, headers, etc...)
853 * 2 : caller = name of CGI who calls us and which should
854 * be excluded from the generated menu.
855 * Returns : NULL if no memory, else map
857 *********************************************************************/
858 struct map *default_exports(const struct client_state *csp, const char *caller)
861 struct map * exports = new_map();
863 map(exports, "version", 1, VERSION, 1);
864 map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
865 map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
866 map(exports, "admin-address", 1, csp->config->admin_address ? csp->config->admin_address : "fill@me.in.please", 1);
867 map(exports, "homepage", 1, HOME_PAGE_URL, 1);
868 map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
869 map(exports, "menu", 1, make_menu(caller), 0);
870 map(exports, "code-status", 1, CODE_STATUS, 1);
872 snprintf(buf, 20, "%d", csp->config->hport);
873 map(exports, "my-port", 1, buf, 1);
875 if(!strcmp(CODE_STATUS, "stable"))
877 map_block_killer(exports, "unstable");
880 if(csp->config->proxy_info_url != NULL)
882 map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
886 map_block_killer(exports, "have-proxy-info");
894 /*********************************************************************
896 * Function : map_block_killer
898 * Description : Convenience function.
899 * Adds a "killer" for the conditional HTML-template
900 * block <name>, i.e. a substitution of the regex
901 * "if-<name>-start.*if-<name>-end" to the given
905 * 1 : exports = map to extend
906 * 2 : name = name of conditional block
908 * Returns : extended map
910 *********************************************************************/
911 void map_block_killer(struct map *exports, const char *name)
913 char buf[1000]; /* Will do, since the names are hardwired */
915 snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
916 map(exports, buf, 1, "", 1);
921 /*********************************************************************
923 * Function : map_conditional
925 * Description : Convenience function.
926 * Adds an "if-then-else" for the conditional HTML-template
927 * block <name>, i.e. a substitution of the form:
934 * The control structure and one of the alternatives
938 * 1 : exports = map to extend
939 * 2 : name = name of conditional block
940 * 3 : choose_first = nonzero for first, zero for second.
942 * Returns : extended map
944 *********************************************************************/
945 void map_conditional(struct map *exports, const char *name, int choose_first)
947 char buf[1000]; /* Will do, since the names are hardwired */
949 snprintf(buf, 1000, (choose_first
950 ? "else-not-%s@.*@endif-%s"
951 : "if-%s-then@.*@else-not-%s"),
953 map(exports, buf, 1, "", 1);
955 snprintf(buf, 1000, (choose_first ? "if-%s-then" : "endif-%s"), name);
956 map(exports, buf, 1, "", 1);
961 /*********************************************************************
963 * Function : make_menu
965 * Description : Returns an HTML-formatted menu of the available
966 * unhidden CGIs, excluding the one given in <self>.
968 * Parameters : self = name of CGI to leave out, can be NULL
970 * Returns : menu string
972 *********************************************************************/
973 char *make_menu(const char *self)
975 const struct cgi_dispatcher *d;
976 char buf[BUFFER_SIZE];
981 self = "NO-SUCH-CGI!";
984 /* List available unhidden CGI's and export as "other-cgis" */
985 for (d = cgi_dispatcher; d->handler; d++)
987 if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
989 snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
990 HOME_PAGE_URL, d->name, d->description);
991 result = strsav(result, buf);
999 /*********************************************************************
1001 * Function : dump_map
1003 * Description : HTML-dump a map for debugging
1006 * 1 : the_map = map to dump
1008 * Returns : string with HTML
1010 *********************************************************************/
1011 char *dump_map(const struct map *the_map)
1013 struct map_entry *cur_entry = the_map->first;
1016 ret = strsav(ret, "<table>\n");
1020 ret = strsav(ret, "<tr><td><b>");
1021 ret = strsav(ret, cur_entry->name);
1022 ret = strsav(ret, "</b></td><td>");
1023 ret = strsav(ret, cur_entry->value);
1024 ret = strsav(ret, "</td></tr>\n");
1025 cur_entry = cur_entry->next;
1028 ret = strsav(ret, "</table>\n");