]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/var.c
2 * "$Id: var.c,v 1.5 1997/05/13 16:38:07 mike Exp $"
4 * CGI form variable functions.
6 * Copyright 1997 by Easy Software Products, All Rights Reserved.
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.
24 * Revision 1.5 1997/05/13 16:38:07 mike
25 * Fixed a bug in the cgi_initialize_string() code - was not sorting vars!
27 * Revision 1.4 1997/05/13 15:16:30 mike
28 * Updated cgiCheckVariables() to see if the variable is blank.
30 * Revision 1.3 1997/05/13 14:56:37 mike
31 * Added cgiCheckVariables() function to check for required variables.
33 * Revision 1.2 1997/05/08 20:14:19 mike
34 * Renamed CGI_Name functions to cgiName functions.
35 * Updated documentation.
37 * Revision 1.1 1997/05/08 19:55:53 mike
46 * Data structure to hold all the CGI form variables...
51 char *name
, /* Name of variable */
52 *value
; /* Value of variable */
60 static int form_count
= 0; /* Form variable count */
61 static var_t
*form_vars
= NULL
; /* Form variables */
68 static void cgi_sort_variables(void);
69 static int cgi_compare_variables(var_t
*v1
, var_t
*v2
);
70 static void cgi_add_variable(char *name
, char *value
);
71 static void cgi_initialize_string(char *data
);
72 static int cgi_initialize_get(int need_content
);
73 static int cgi_initialize_post(int need_content
);
77 * 'cgiInitialize()' - Initialize the CGI variable "database"...
81 cgiInitialize(int need_content
) /* I - True if input is required */
83 char *method
; /* Form posting method */
88 puts("Content-type: text/plain\n");
91 method
= getenv("REQUEST_METHOD");
94 return (!need_content
);
96 if (strcasecmp(method
, "GET") == 0)
97 return (cgi_initialize_get(need_content
));
98 else if (strcasecmp(method
, "POST") == 0)
99 return (cgi_initialize_post(need_content
));
101 return (!need_content
);
106 * 'cgiCheckVariables()' - Check for the presence of "required" variables.
108 * Returns 1 if all variables are present, 0 otherwise. Name may be separated
109 * by spaces and/or commas.
113 cgiCheckVariables(char *names
) /* I - Variables to look for */
115 char name
[255], /* Current variable name */
116 *s
, /* Pointer in string */
117 *val
; /* Value of variable */
123 while (*names
!= '\0')
125 while (*names
== ' ' || *names
== ',')
128 for (s
= name
; *names
!= '\0' && *names
!= ' ' && *names
!= ','; s
++, names
++)
135 if ((val
= cgiGetVariable(name
)) == NULL
)
139 return (0); /* Can't be blank, either! */
147 * 'cgiGetVariable()' - Get a CGI variable from the database...
149 * Returns NULL if the variable doesn't exist...
153 cgiGetVariable(char *name
) /* I - Name of variable */
155 var_t key
, /* Search key */
156 *var
; /* Returned variable */
164 var
= bsearch(&key
, form_vars
, form_count
, sizeof(var_t
),
165 (int (*)(const void *, const void *))cgi_compare_variables
);
169 printf("cgiGetVariable(\"%s\") is returning NULL...\n", name
);
171 printf("cgiGetVariable(\"%s\") is returning \"%s\"...\n", name
, var
->value
);
174 return ((var
== NULL
) ? NULL
: var
->value
);
179 * 'cgiSetVariable()' - Set a CGI variable in the database...
183 cgiSetVariable(char *name
, /* I - Name of variable */
184 char *value
) /* I - Value of variable */
186 var_t key
, /* Search key */
187 *var
; /* Returned variable */
194 var
= bsearch(&key
, form_vars
, form_count
, sizeof(var_t
),
195 (int (*)(const void *, const void *))cgi_compare_variables
);
202 cgi_add_variable(name
, value
);
203 cgi_sort_variables();
208 var
->value
= strdup(value
);
214 * 'cgi_sort_variables()' - Sort all form variables for faster lookup.
218 cgi_sort_variables(void)
224 puts("Sorting variables...");
230 qsort(form_vars
, form_count
, sizeof(var_t
),
231 (int (*)(const void *, const void *))cgi_compare_variables
);
234 puts("New variable list is:");
235 for (i
= 0; i
< form_count
; i
++)
236 printf("%s = %s\n", form_vars
[i
].name
, form_vars
[i
].value
);
242 * 'cgi_compare_variables()' - Compare two variables.
246 cgi_compare_variables(var_t
*v1
, /* I - First variable */
247 var_t
*v2
) /* I - Second variable */
249 return (strcasecmp(v1
->name
, v2
->name
));
254 * 'cgi_add_variable()' - Add a form variable.
258 cgi_add_variable(char *name
, /* I - Variable name */
259 char *value
) /* I - Variable value */
265 printf("Adding variable \'%s\' with value \'%s\'...\n", name
, value
);
269 form_vars
= malloc(sizeof(var_t
));
271 form_vars
= realloc(form_vars
, (form_count
+ 1) * sizeof(var_t
));
273 var
= form_vars
+ form_count
;
274 var
->name
= strdup(name
);
275 var
->value
= strdup(value
);
281 * 'cgi_initialize_string()' - Initialize form variables from a string.
285 cgi_initialize_string(char *data
) /* I - Form data string */
287 int done
; /* True if we're done reading a form variable */
288 char *s
, /* Pointer to current form string */
289 ch
, /* Temporary character */
290 name
[255], /* Name of form variable */
291 value
[65536]; /* Variable value... */
302 * Loop until we've read all the form data...
305 while (*data
!= '\0')
308 * Get the variable name...
311 for (s
= name
; *data
!= '\0'; data
++, s
++)
322 * Read the variable value...
325 for (s
= value
, done
= 0; !done
&& *data
!= '\0'; data
++, s
++)
328 case '&' : /* End of data... */
333 case '+' : /* Escaped space character */
337 case '%' : /* Escaped control character */
339 * Read the hex code from stdin...
355 default : /* Other characters come straight through */
360 *s
= '\0'; /* nul terminate the string */
363 * Add the string to the variable "database"...
366 cgi_add_variable(name
, value
);
369 cgi_sort_variables();
374 * 'cgi_initialize_get()' - Initialize form variables using the GET method.
378 cgi_initialize_get(int need_content
) /* I - True if input is required */
380 char *data
; /* Pointer to form data string */
384 puts("Initializing variables using GET method...");
388 * Check to see if there is anything for us to read...
391 data
= getenv("QUERY_STRING");
393 return (!need_content
);
399 cgi_initialize_string(data
);
410 * 'cgi_initialize_post()' - Initialize variables using the POST method.
414 cgi_initialize_post(int need_content
) /* I - True if input is required */
416 char *content_length
, /* Length of input data (string) */
417 *data
; /* Pointer to form data string */
418 int length
, /* Length of input data */
419 nbytes
, /* Number of bytes read this read() */
420 tbytes
; /* Total number of bytes read */
424 puts("Initializing variables using POST method...");
428 * Check to see if there is anything for us to read...
431 content_length
= getenv("CONTENT_LENGTH");
432 if (content_length
== NULL
)
433 return (!need_content
);
436 * Get the length of the input stream and allocate a buffer for it...
439 length
= atoi(content_length
);
440 data
= malloc(length
+ 1);
443 * Read the data into the buffer...
446 for (tbytes
= 0; tbytes
< length
; tbytes
+= nbytes
)
447 if ((nbytes
= read(0, data
+ tbytes
, length
- tbytes
)) < 0)
450 return (!need_content
);
459 cgi_initialize_string(data
);
462 * Free the data and return...
472 * End of "$Id: var.c,v 1.5 1997/05/13 16:38:07 mike Exp $".