11 #include "utils_cgi.h"
15 #include <fcgi_stdio.h>
22 typedef struct parameter_s parameter_t;
26 parameter_t *parameters;
27 size_t parameters_num;
30 static param_list_t *pl_global = NULL;
32 static char *uri_unescape_copy (char *dest, const char *src, size_t n) /* {{{ */
37 if ((dest == NULL) || (src == NULL) || (n < 1))
54 else if ((src_ptr[0] == '%')
55 && isxdigit ((int) src_ptr[1]) && isxdigit ((int) src_ptr[2]))
61 tmpstr[0] = src_ptr[1];
62 tmpstr[1] = src_ptr[2];
67 value = strtol (tmpstr, &endptr, /* base = */ 16);
68 if ((endptr == tmpstr) || (errno != 0))
74 *dest_ptr = (char) value;
87 } /* while (*src_ptr != 0) */
89 assert (*dest_ptr == 0);
91 } /* }}} char *uri_unescape */
93 static char *uri_unescape (const char *string) /* {{{ */
100 uri_unescape_copy (buffer, string, sizeof (buffer));
102 return (strdup (buffer));
103 } /* }}} char *uri_unescape */
105 static int param_parse_keyval (param_list_t *pl, char *keyval) /* {{{ */
113 value_raw = strchr (key_raw, '=');
114 if (value_raw == NULL)
119 key = uri_unescape (key_raw);
123 value = uri_unescape (value_raw);
130 param_set (pl, key, value);
136 } /* }}} int param_parse_keyval */
138 static int parse_query_string (param_list_t *pl, /* {{{ */
144 if ((pl == NULL) || (query_string == NULL))
147 dummy = query_string;
148 while ((keyval = strtok (dummy, ";&")) != NULL)
151 param_parse_keyval (pl, keyval);
155 } /* }}} int parse_query_string */
157 int param_init (void) /* {{{ */
159 if (pl_global != NULL)
162 pl_global = param_create (/* query string = */ NULL);
163 if (pl_global == NULL)
167 } /* }}} int param_init */
169 void param_finish (void) /* {{{ */
171 param_destroy (pl_global);
173 } /* }}} void param_finish */
175 const char *param (const char *key) /* {{{ */
179 return (param_get (pl_global, key));
180 } /* }}} const char *param */
182 param_list_t *param_create (const char *query_string) /* {{{ */
187 if (query_string == NULL)
188 query_string = getenv ("QUERY_STRING");
190 if (query_string == NULL)
193 tmp = strdup (query_string);
197 pl = malloc (sizeof (*pl));
203 memset (pl, 0, sizeof (*pl));
205 parse_query_string (pl, tmp);
209 } /* }}} param_list_t *param_create */
211 param_list_t *param_clone (__attribute__((unused)) param_list_t *pl) /* {{{ */
213 /* FIXME: To be implemented. */
216 } /* }}} param_list_t *param_clone */
218 void param_destroy (param_list_t *pl) /* {{{ */
225 for (i = 0; i < pl->parameters_num; i++)
227 free (pl->parameters[i].key);
228 free (pl->parameters[i].value);
230 free (pl->parameters);
232 } /* }}} void param_destroy */
234 const char *param_get (param_list_t *pl, const char *name) /* {{{ */
238 if ((pl == NULL) || (name == NULL))
241 for (i = 0; i < pl->parameters_num; i++)
243 if ((name == NULL) && (pl->parameters[i].key == NULL))
244 return (pl->parameters[i].value);
245 else if ((name != NULL) && (pl->parameters[i].key != NULL)
246 && (strcmp (name, pl->parameters[i].key) == 0))
247 return (pl->parameters[i].value);
251 } /* }}} char *param_get */
253 static int param_add (param_list_t *pl, /* {{{ */
254 const char *key, const char *value)
258 tmp = realloc (pl->parameters,
259 sizeof (*pl->parameters) * (pl->parameters_num + 1));
262 pl->parameters = tmp;
263 tmp = pl->parameters + pl->parameters_num;
265 memset (tmp, 0, sizeof (*tmp));
266 tmp->key = strdup (key);
267 if (tmp->key == NULL)
270 tmp->value = strdup (value);
271 if (tmp->value == NULL)
277 pl->parameters_num++;
280 } /* }}} int param_add */
282 static int param_delete (param_list_t *pl, /* {{{ */
287 if ((pl == NULL) || (name == NULL))
290 for (i = 0; i < pl->parameters_num; i++)
291 if (strcasecmp (pl->parameters[i].key, name) == 0)
294 if (i >= pl->parameters_num)
297 if (i < (pl->parameters_num - 1))
301 p = pl->parameters[i];
302 pl->parameters[i] = pl->parameters[pl->parameters_num - 1];
303 pl->parameters[pl->parameters_num - 1] = p;
306 pl->parameters_num--;
307 free (pl->parameters[pl->parameters_num].key);
308 free (pl->parameters[pl->parameters_num].value);
311 } /* }}} int param_delete */
313 int param_set (param_list_t *pl, const char *name, /* {{{ */
320 if ((pl == NULL) || (name == NULL))
324 return (param_delete (pl, name));
327 for (i = 0; i < pl->parameters_num; i++)
329 if (strcasecmp (pl->parameters[i].key, name) == 0)
331 p = pl->parameters + i;
337 return (param_add (pl, name, value));
339 value_copy = strdup (value);
340 if (value_copy == NULL)
344 p->value = value_copy;
347 } /* }}} int param_set */
349 char *param_as_string (param_list_t *pl) /* {{{ */
360 for (i = 0; i < pl->parameters_num; i++)
362 uri_escape_copy (key, pl->parameters[i].key, sizeof (key));
363 uri_escape_copy (value, pl->parameters[i].value, sizeof (value));
366 strlcat (buffer, ";", sizeof (buffer));
367 strlcat (buffer, key, sizeof (buffer));
368 strlcat (buffer, "=", sizeof (buffer));
369 strlcat (buffer, value, sizeof (buffer));
372 return (strdup (buffer));
373 } /* }}} char *param_as_string */
375 int param_print_hidden (param_list_t *pl) /* {{{ */
384 for (i = 0; i < pl->parameters_num; i++)
386 html_escape_copy (key, pl->parameters[i].key, sizeof (key));
387 html_escape_copy (value, pl->parameters[i].value, sizeof (value));
389 printf (" <input type=\"hidden\" name=\"%s\" value=\"%s\" />\n",
394 } /* }}} int param_print_hidden */
396 char *uri_escape_copy (char *dest, const char *src, size_t n) /* {{{ */
410 else if ((((unsigned char) src[in]) < 32)
415 || (((unsigned char) src[in]) >= 128))
422 snprintf (esc, sizeof (esc), "%%%02x", (unsigned int) src[in]);
424 dest[out+1] = esc[1];
425 dest[out+2] = esc[2];
439 } /* }}} char *uri_escape_copy */
441 char *uri_escape (const char *string) /* {{{ */
448 uri_escape_copy (buffer, string, sizeof (buffer));
450 return (strdup (buffer));
451 } /* }}} char *uri_escape */
453 const char *script_name (void) /* {{{ */
457 ret = getenv ("SCRIPT_NAME");
459 ret = "collection4.fcgi";
462 } /* }}} char *script_name */
464 int time_to_rfc1123 (time_t t, char *buffer, size_t buffer_size) /* {{{ */
469 /* RFC 1123 *requires* the time to be GMT and the "GMT" timezone string.
470 * Apache will ignore the timezone if "localtime_r" and "%z" is used,
471 * resulting in weird behavior. */
472 if (gmtime_r (&t, &tm_tmp) == NULL)
475 status = strftime (buffer, buffer_size, "%a, %d %b %Y %T GMT", &tm_tmp);
480 } /* }}} int time_to_rfc1123 */
482 #define COPY_ENTITY(e) do { \
483 size_t len = strlen (e); \
484 if (dest_size < (len + 1)) \
486 strcpy (dest_ptr, (e)); \
491 char *html_escape_copy (char *dest, const char *src, size_t n) /* {{{ */
500 for (pos = 0; src[pos] != 0; pos++)
503 COPY_ENTITY (""");
504 else if (src[pos] == '<')
505 COPY_ENTITY ("<");
506 else if (src[pos] == '>')
507 COPY_ENTITY (">");
508 else if (src[pos] == '&')
509 COPY_ENTITY ("&");
512 *dest_ptr = src[pos];
523 } /* }}} char *html_escape_copy */
527 char *html_escape_buffer (char *buffer, size_t buffer_size) /* {{{ */
529 char tmp[buffer_size];
531 html_escape_copy (tmp, buffer, sizeof (tmp));
532 memcpy (buffer, tmp, buffer_size);
535 } /* }}} char *html_escape_buffer */
537 char *html_escape (const char *string) /* {{{ */
544 html_escape_copy (buffer, string, sizeof (buffer));
546 return (strdup (buffer));
547 } /* }}} char *html_escape */
549 int html_print_page (const char *title, /* {{{ */
550 const page_callbacks_t *cb, void *user_data)
554 printf ("Content-Type: text/html\n"
555 "X-Generator: "PACKAGE_STRING"\n"
559 title = "c4: collection4 graph interface";
561 title_html = html_escape (title);
565 " <title>%s</title>\n"
566 " <link rel=\"stylesheet\" type=\"text/css\" href=\"../share/style.css\" />\n"
567 " <script type=\"text/javascript\" src=\"../share/jquery-1.4.2.min.js\">\n"
569 " <script type=\"text/javascript\" src=\"../share/collection.js\">\n"
575 " <table id=\"layout-table\">\n"
576 " <tr id=\"layout-top\">\n"
577 " <td id=\"layout-top-left\">");
578 if (cb->top_left != NULL)
579 (*cb->top_left) (user_data);
581 html_print_logo (NULL);
583 " <td id=\"layout-top-center\">");
584 if (cb->top_center != NULL)
585 (*cb->top_center) (user_data);
587 printf ("<h1>%s</h1>", title_html);
589 " <td id=\"layout-top-right\">");
590 if (cb->top_right != NULL)
591 (*cb->top_right) (user_data);
594 " <tr id=\"layout-middle\">\n"
595 " <td id=\"layout-middle-left\">");
596 if (cb->middle_left != NULL)
597 (*cb->middle_left) (user_data);
599 " <td id=\"layout-middle-center\">");
600 if (cb->middle_center != NULL)
601 (*cb->middle_center) (user_data);
603 " <td id=\"layout-middle-right\">");
604 if (cb->middle_right != NULL)
605 (*cb->middle_right) (user_data);
608 " <tr id=\"layout-bottom\">\n"
609 " <td id=\"layout-bottom-left\">");
610 if (cb->bottom_left != NULL)
611 (*cb->bottom_left) (user_data);
613 " <td id=\"layout-bottom-center\">");
614 if (cb->bottom_center != NULL)
615 (*cb->bottom_center) (user_data);
617 " <td id=\"layout-bottom-right\">");
618 if (cb->bottom_right != NULL)
619 (*cb->bottom_right) (user_data);
628 } /* }}} int html_print_page */
630 int html_print_logo (__attribute__((unused)) void *user_data) /* {{{ */
632 printf ("<a href=\"%s?action=list_graphs\" id=\"logo-canvas\">\n"
633 " <h1>c<sup>4</sup></h1>\n"
634 " <div id=\"logo-subscript\">collection 4</div>\n"
638 } /* }}} int html_print_search_box */
640 int html_print_search_box (__attribute__((unused)) void *user_data) /* {{{ */
644 term_html = html_escape (param ("q"));
646 printf ("<form action=\"%s\" method=\"get\" id=\"search-form\">\n"
647 " <input type=\"hidden\" name=\"action\" value=\"list_graphs\" />\n"
648 " <input type=\"text\" name=\"q\" value=\"%s\" id=\"search-input\" />\n"
649 " <input type=\"submit\" name=\"button\" value=\"Search\" />\n"
652 (term_html != NULL) ? term_html : "");
657 } /* }}} int html_print_search_box */
659 /* vim: set sw=2 sts=2 et fdm=marker : */