]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/var.c
2 * "$Id: var.c,v 1.4 1997/05/13 15:16:30 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.4 1997/05/13 15:16:30 mike
25 * Updated cgiCheckVariables() to see if the variable is blank.
27 * Revision 1.3 1997/05/13 14:56:37 mike
28 * Added cgiCheckVariables() function to check for required variables.
30 * Revision 1.2 1997/05/08 20:14:19 mike
31 * Renamed CGI_Name functions to cgiName functions.
32 * Updated documentation.
34 * Revision 1.1 1997/05/08 19:55:53 mike
42 * Data structure to hold all the CGI form variables...
47 char *name
, /* Name of variable */
48 *value
; /* Value of variable */
56 static int form_count
= 0; /* Form variable count */
57 static var_t
*form_vars
= NULL
; /* Form variables */
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
);
73 * 'cgiInitialize()' - Initialize the CGI variable "database"...
77 cgiInitialize(int need_content
) /* I - True if input is required */
79 char *method
; /* Form posting method */
82 method
= getenv("REQUEST_METHOD");
85 return (!need_content
);
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
));
92 return (!need_content
);
97 * 'cgiCheckVariables()' - Check for the presence of "required" variables.
99 * Returns 1 if all variables are present, 0 otherwise. Name may be separated
100 * by spaces and/or commas.
104 cgiCheckVariables(char *names
) /* I - Variables to look for */
106 char name
[255], /* Current variable name */
107 *s
, /* Pointer in string */
108 *val
; /* Value of variable */
114 while (*names
!= '\0')
116 while (*names
== ' ' || *names
== ',')
119 for (s
= name
; *names
!= '\0' && *names
!= ' ' && *names
!= ','; s
++, names
++)
126 if ((val
= cgiGetVariable(name
)) == NULL
)
130 return (0); /* Can't be blank, either! */
138 * 'cgiGetVariable()' - Get a CGI variable from the database...
140 * Returns NULL if the variable doesn't exist...
144 cgiGetVariable(char *name
) /* I - Name of variable */
146 var_t key
, /* Search key */
147 *var
; /* Returned variable */
155 var
= bsearch(&key
, form_vars
, form_count
, sizeof(var_t
),
156 (int (*)(const void *, const void *))cgi_compare_variables
);
158 return ((var
== NULL
) ? NULL
: var
->value
);
163 * 'cgiSetVariable()' - Set a CGI variable in the database...
167 cgiSetVariable(char *name
, /* I - Name of variable */
168 char *value
) /* I - Value of variable */
170 var_t key
, /* Search key */
171 *var
; /* Returned variable */
178 var
= bsearch(&key
, form_vars
, form_count
, sizeof(var_t
),
179 (int (*)(const void *, const void *))cgi_compare_variables
);
186 cgi_add_variable(name
, value
);
187 cgi_sort_variables();
192 var
->value
= strdup(value
);
198 * 'cgi_sort_variables()' - Sort all form variables for faster lookup.
202 cgi_sort_variables(void)
207 qsort(form_vars
, form_count
, sizeof(var_t
),
208 (int (*)(const void *, const void *))cgi_compare_variables
);
213 * 'cgi_compare_variables()' - Compare two variables.
217 cgi_compare_variables(var_t
*v1
, /* I - First variable */
218 var_t
*v2
) /* I - Second variable */
220 return (strcasecmp(v1
->name
, v2
->name
));
225 * 'cgi_add_variable()' - Add a form variable.
229 cgi_add_variable(char *name
, /* I - Variable name */
230 char *value
) /* I - Variable value */
236 form_vars
= malloc(sizeof(var_t
));
238 form_vars
= realloc(form_vars
, (form_count
+ 1) * sizeof(var_t
));
240 var
= form_vars
+ form_count
;
241 var
->name
= strdup(name
);
242 var
->value
= strdup(value
);
248 * 'cgi_initialize_string()' - Initialize form variables from a string.
252 cgi_initialize_string(char *data
) /* I - Form data string */
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... */
269 * Loop until we've read all the form data...
272 while (*data
!= '\0')
275 * Get the variable name...
278 for (s
= name
; *data
!= '\0'; data
++, s
++)
289 * Read the variable value...
292 for (s
= value
, done
= 0; !done
&& *data
!= '\0'; data
++, s
++)
295 case '&' : /* End of data... */
300 case '+' : /* Escaped space character */
304 case '%' : /* Escaped control character */
306 * Read the hex code from stdin...
322 default : /* Other characters come straight through */
327 *s
= '\0'; /* nul terminate the string */
330 * Add the string to the variable "database"...
333 cgi_add_variable(name
, value
);
339 * 'cgi_initialize_get()' - Initialize form variables using the GET method.
343 cgi_initialize_get(int need_content
) /* I - True if input is required */
345 char *data
; /* Pointer to form data string */
349 * Check to see if there is anything for us to read...
352 data
= getenv("QUERY_STRING");
354 return (!need_content
);
360 cgi_initialize_string(data
);
371 * 'cgi_initialize_post()' - Initialize variables using the POST method.
375 cgi_initialize_post(int need_content
) /* I - True if input is required */
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 */
385 * Check to see if there is anything for us to read...
388 content_length
= getenv("CONTENT_LENGTH");
389 if (content_length
== NULL
)
390 return (!need_content
);
393 * Get the length of the input stream and allocate a buffer for it...
396 length
= atoi(content_length
);
397 data
= malloc(length
+ 1);
400 * Read the data into the buffer...
403 for (tbytes
= 0; tbytes
< length
; tbytes
+= nbytes
)
404 if ((nbytes
= read(0, data
+ tbytes
, length
- tbytes
)) < 0)
407 return (!need_content
);
416 cgi_initialize_string(data
);
419 * Free the data and return...
429 * End of "$Id: var.c,v 1.4 1997/05/13 15:16:30 mike Exp $".