1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
** Copyright 2007 Double Precision, Inc.
** See COPYING for distribution information.
*/
/*
*/
#include "cgi.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
static void do_cgi_select(const char *name,
const char *optvalues,
const char *optlabels,
const char *default_value,
size_t list_size,
const char *flags,
void (*output_func)(const char *, size_t, void *),
void *output_arg)
{
(*output_func)("<select name='", 0, output_arg);
(*output_func)(name, 0, output_arg);
(*output_func)("'", 0, output_arg);
if (strchr(flags, 'm'))
(*output_func)(" multiple='multiple'", 0, output_arg);
if (strchr(flags, 'd'))
(*output_func)(" disabled='disabled'", 0, output_arg);
(*output_func)("'>", 0, output_arg);
if (!optvalues)
optvalues="";
while (*optlabels)
{
const char *label_start=optlabels;
const char *value_start=optvalues;
while (*optlabels)
{
if (*optlabels == '\n')
break;
++optlabels;
}
while (*optvalues)
{
if (*optvalues == '\n')
break;
++optvalues;
}
(*output_func)("<option", 0, output_arg);
if (*value_start)
{
if (default_value &&
optvalues - value_start == strlen(default_value) &&
strncmp(value_start, default_value,
optvalues-value_start) == 0)
{
(*output_func)(" selected='selected'", 0,
output_arg);
}
(*output_func)(" value='", 0, output_arg);
(*output_func)(value_start, optvalues-value_start,
output_arg);
(*output_func)("'", 0, output_arg);
}
(*output_func)(">", 0, output_arg);
(*output_func)(label_start, optlabels-label_start, output_arg);
(*output_func)("</option>", 0, output_arg);
if (*optlabels)
++optlabels;
if (*optvalues)
++optvalues;
}
(*output_func)("</select>", 0, output_arg);
}
static void cnt_bytes(const char *str, size_t cnt, void *arg)
{
if (!cnt)
cnt=strlen(str);
*(size_t *)arg += cnt;
}
static void save_bytes(const char *str, size_t cnt, void *arg)
{
char **p=(char **)arg;
if (!cnt)
cnt=strlen(str);
memcpy(*p, str, cnt);
*p += cnt;
}
char *cgi_select(const char *name,
const char *optvalues,
const char *optlabels,
const char *default_value,
size_t list_size,
const char *flags)
{
size_t cnt=1;
char *buf;
char *ptr;
if (!flags)
flags="";
do_cgi_select(name, optvalues, optlabels, default_value,
list_size, flags, cnt_bytes, &cnt);
buf=malloc(cnt);
if (!buf)
return NULL;
ptr=buf;
do_cgi_select(name, optvalues, optlabels, default_value,
list_size, flags, save_bytes, &ptr);
*ptr=0;
return buf;
}
|