]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/var.c
Updated cgiCheckVariables() to see if the variable is blank.
[thirdparty/cups.git] / cgi-bin / var.c
1 /*
2 * "$Id: var.c,v 1.4 1997/05/13 15:16:30 mike Exp $"
3 *
4 * CGI form variable functions.
5 *
6 * Copyright 1997 by Easy Software Products, All Rights Reserved.
7 *
8 * Contents:
9 *
10 * cgiInitialize() - Initialize the CGI variable "database"...
11 * cgiCheckVariables() - Check for the presence of "required" variables.
12 * cgiGetVariable() - Get a CGI variable from the database...
13 * cgiSetVariable() - Set a CGI variable in the database...
14 * cgi_sort_variables() - Sort all form variables for faster lookup.
15 * cgi_compare_variables() - Compare two variables.
16 * cgi_add_variable() - Add a form variable.
17 * cgi_initialize_string() - Initialize form variables from a string.
18 * cgi_initialize_get() - Initialize form variables using the GET method.
19 * cgi_initialize_post() - Initialize variables using the POST method.
20 *
21 * Revision History:
22 *
23 * $Log: var.c,v $
24 * Revision 1.4 1997/05/13 15:16:30 mike
25 * Updated cgiCheckVariables() to see if the variable is blank.
26 *
27 * Revision 1.3 1997/05/13 14:56:37 mike
28 * Added cgiCheckVariables() function to check for required variables.
29 *
30 * Revision 1.2 1997/05/08 20:14:19 mike
31 * Renamed CGI_Name functions to cgiName functions.
32 * Updated documentation.
33 *
34 * Revision 1.1 1997/05/08 19:55:53 mike
35 * Initial revision
36 */
37
38 #include "cgi.h"
39
40
41 /*
42 * Data structure to hold all the CGI form variables...
43 */
44
45 typedef struct
46 {
47 char *name, /* Name of variable */
48 *value; /* Value of variable */
49 } var_t;
50
51
52 /*
53 * Local globals...
54 */
55
56 static int form_count = 0; /* Form variable count */
57 static var_t *form_vars = NULL; /* Form variables */
58
59
60 /*
61 * Local functions...
62 */
63
64 static void cgi_sort_variables(void);
65 static int cgi_compare_variables(var_t *v1, var_t *v2);
66 static void cgi_add_variable(char *name, char *value);
67 static void cgi_initialize_string(char *data);
68 static int cgi_initialize_get(int need_content);
69 static int cgi_initialize_post(int need_content);
70
71
72 /*
73 * 'cgiInitialize()' - Initialize the CGI variable "database"...
74 */
75
76 int
77 cgiInitialize(int need_content) /* I - True if input is required */
78 {
79 char *method; /* Form posting method */
80
81
82 method = getenv("REQUEST_METHOD");
83
84 if (method == NULL)
85 return (!need_content);
86
87 if (strcasecmp(method, "GET") == 0)
88 return (cgi_initialize_get(need_content));
89 else if (strcasecmp(method, "POST") == 0)
90 return (cgi_initialize_post(need_content));
91 else
92 return (!need_content);
93 }
94
95
96 /*
97 * 'cgiCheckVariables()' - Check for the presence of "required" variables.
98 *
99 * Returns 1 if all variables are present, 0 otherwise. Name may be separated
100 * by spaces and/or commas.
101 */
102
103 int
104 cgiCheckVariables(char *names) /* I - Variables to look for */
105 {
106 char name[255], /* Current variable name */
107 *s, /* Pointer in string */
108 *val; /* Value of variable */
109
110
111 if (names == NULL)
112 return (1);
113
114 while (*names != '\0')
115 {
116 while (*names == ' ' || *names == ',')
117 names ++;
118
119 for (s = name; *names != '\0' && *names != ' ' && *names != ','; s ++, names ++)
120 *s = *names;
121
122 *s = 0;
123 if (name[0] == '\0')
124 break;
125
126 if ((val = cgiGetVariable(name)) == NULL)
127 return (0);
128
129 if (*val == '\0')
130 return (0); /* Can't be blank, either! */
131 };
132
133 return (1);
134 }
135
136
137 /*
138 * 'cgiGetVariable()' - Get a CGI variable from the database...
139 *
140 * Returns NULL if the variable doesn't exist...
141 */
142
143 char *
144 cgiGetVariable(char *name) /* I - Name of variable */
145 {
146 var_t key, /* Search key */
147 *var; /* Returned variable */
148
149
150 if (form_count < 1)
151 return (NULL);
152
153 key.name = name;
154
155 var = bsearch(&key, form_vars, form_count, sizeof(var_t),
156 (int (*)(const void *, const void *))cgi_compare_variables);
157
158 return ((var == NULL) ? NULL : var->value);
159 }
160
161
162 /*
163 * 'cgiSetVariable()' - Set a CGI variable in the database...
164 */
165
166 void
167 cgiSetVariable(char *name, /* I - Name of variable */
168 char *value) /* I - Value of variable */
169 {
170 var_t key, /* Search key */
171 *var; /* Returned variable */
172
173
174 if (form_count > 0)
175 {
176 key.name = name;
177
178 var = bsearch(&key, form_vars, form_count, sizeof(var_t),
179 (int (*)(const void *, const void *))cgi_compare_variables);
180 }
181 else
182 var = NULL;
183
184 if (var == NULL)
185 {
186 cgi_add_variable(name, value);
187 cgi_sort_variables();
188 }
189 else
190 {
191 free(var->value);
192 var->value = strdup(value);
193 };
194 }
195
196
197 /*
198 * 'cgi_sort_variables()' - Sort all form variables for faster lookup.
199 */
200
201 static void
202 cgi_sort_variables(void)
203 {
204 if (form_count < 2)
205 return;
206
207 qsort(form_vars, form_count, sizeof(var_t),
208 (int (*)(const void *, const void *))cgi_compare_variables);
209 }
210
211
212 /*
213 * 'cgi_compare_variables()' - Compare two variables.
214 */
215
216 static int
217 cgi_compare_variables(var_t *v1, /* I - First variable */
218 var_t *v2) /* I - Second variable */
219 {
220 return (strcasecmp(v1->name, v2->name));
221 }
222
223
224 /*
225 * 'cgi_add_variable()' - Add a form variable.
226 */
227
228 static void
229 cgi_add_variable(char *name, /* I - Variable name */
230 char *value) /* I - Variable value */
231 {
232 var_t *var;
233
234
235 if (form_count == 0)
236 form_vars = malloc(sizeof(var_t));
237 else
238 form_vars = realloc(form_vars, (form_count + 1) * sizeof(var_t));
239
240 var = form_vars + form_count;
241 var->name = strdup(name);
242 var->value = strdup(value);
243 form_count ++;
244 }
245
246
247 /*
248 * 'cgi_initialize_string()' - Initialize form variables from a string.
249 */
250
251 static void
252 cgi_initialize_string(char *data) /* I - Form data string */
253 {
254 int done; /* True if we're done reading a form variable */
255 char *s, /* Pointer to current form string */
256 ch, /* Temporary character */
257 name[255], /* Name of form variable */
258 value[65536]; /* Variable value... */
259
260
261 /*
262 * Check input...
263 */
264
265 if (data == NULL)
266 return;
267
268 /*
269 * Loop until we've read all the form data...
270 */
271
272 while (*data != '\0')
273 {
274 /*
275 * Get the variable name...
276 */
277
278 for (s = name; *data != '\0'; data ++, s ++)
279 if (*data == '=')
280 break;
281 else
282 *s = *data;
283
284 *s = '\0';
285 if (*data == '=')
286 data ++;
287
288 /*
289 * Read the variable value...
290 */
291
292 for (s = value, done = 0; !done && *data != '\0'; data ++, s ++)
293 switch (*data)
294 {
295 case '&' : /* End of data... */
296 done = 1;
297 s --;
298 break;
299
300 case '+' : /* Escaped space character */
301 *s = ' ';
302 break;
303
304 case '%' : /* Escaped control character */
305 /*
306 * Read the hex code from stdin...
307 */
308
309 data ++;
310 ch = *data - '0';
311 if (ch > 9)
312 ch -= 7;
313 *s = ch << 4;
314
315 data ++;
316 ch = *data - '0';
317 if (ch > 9)
318 ch -= 7;
319 *s |= ch;
320 break;
321
322 default : /* Other characters come straight through */
323 *s = *data;
324 break;
325 };
326
327 *s = '\0'; /* nul terminate the string */
328
329 /*
330 * Add the string to the variable "database"...
331 */
332
333 cgi_add_variable(name, value);
334 };
335 }
336
337
338 /*
339 * 'cgi_initialize_get()' - Initialize form variables using the GET method.
340 */
341
342 static int
343 cgi_initialize_get(int need_content) /* I - True if input is required */
344 {
345 char *data; /* Pointer to form data string */
346
347
348 /*
349 * Check to see if there is anything for us to read...
350 */
351
352 data = getenv("QUERY_STRING");
353 if (data == NULL)
354 return (!need_content);
355
356 /*
357 * Parse it out...
358 */
359
360 cgi_initialize_string(data);
361
362 /*
363 * Return...
364 */
365
366 return (1);
367 }
368
369
370 /*
371 * 'cgi_initialize_post()' - Initialize variables using the POST method.
372 */
373
374 static int
375 cgi_initialize_post(int need_content) /* I - True if input is required */
376 {
377 char *content_length, /* Length of input data (string) */
378 *data; /* Pointer to form data string */
379 int length, /* Length of input data */
380 nbytes, /* Number of bytes read this read() */
381 tbytes; /* Total number of bytes read */
382
383
384 /*
385 * Check to see if there is anything for us to read...
386 */
387
388 content_length = getenv("CONTENT_LENGTH");
389 if (content_length == NULL)
390 return (!need_content);
391
392 /*
393 * Get the length of the input stream and allocate a buffer for it...
394 */
395
396 length = atoi(content_length);
397 data = malloc(length + 1);
398
399 /*
400 * Read the data into the buffer...
401 */
402
403 for (tbytes = 0; tbytes < length; tbytes += nbytes)
404 if ((nbytes = read(0, data + tbytes, length - tbytes)) < 0)
405 {
406 free(data);
407 return (!need_content);
408 };
409
410 data[length] = '\0';
411
412 /*
413 * Parse it out...
414 */
415
416 cgi_initialize_string(data);
417
418 /*
419 * Free the data and return...
420 */
421
422 free(data);
423
424 return (1);
425 }
426
427
428 /*
429 * End of "$Id: var.c,v 1.4 1997/05/13 15:16:30 mike Exp $".
430 */