Installer import into contrib (real import this time)
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / frontends / cgi / cgictest.c
1 /* Change this if the SERVER_NAME environment variable does not report
2         the true name of your web server. */
3 #if 1
4 #define SERVER_NAME cgiServerName
5 #endif
6 #if 0
7 #define SERVER_NAME "www.boutell.com"
8 #endif
9
10 /* You may need to change this, particularly under Windows;
11         it is a reasonable guess as to an acceptable place to
12         store a saved environment in order to test that feature. 
13         If that feature is not important to you, you needn't
14         concern yourself with this. */
15
16 #ifdef WIN32
17 #define SAVED_ENVIRONMENT "c:\\cgicsave.env"
18 #else
19 #define SAVED_ENVIRONMENT "/tmp/cgicsave.env"
20 #endif /* WIN32 */
21
22 #include <stdio.h>
23 #include "cgic.h"
24 #include <string.h>
25 #include <stdlib.h>
26
27 void HandleSubmit();
28 void ShowForm();
29 void CookieSet();
30 void Name();
31 void Address();
32 void Hungry();
33 void Temperature();
34 void Frogs();
35 void Color();
36 void Flavors();
37 void NonExButtons();
38 void RadioButtons();
39 void File();
40 void Entries();
41 void Cookies();
42 void LoadEnvironment();
43 void SaveEnvironment();
44
45 int cgiMain() {
46 #ifdef DEBUG
47         LoadEnvironment();
48 #endif /* DEBUG */
49         /* Load a previously saved CGI scenario if that button
50                 has been pressed. */
51         if (cgiFormSubmitClicked("loadenvironment") == cgiFormSuccess) {
52                 LoadEnvironment();
53         }
54         /* Set any new cookie requested. Must be done *before*
55                 outputting the content type. */
56         CookieSet();
57         /* Send the content type, letting the browser know this is HTML */
58         cgiHeaderContentType("text/html");
59         /* Top of the page */
60         fprintf(cgiOut, "<HTML><HEAD>\n");
61         fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n");
62         fprintf(cgiOut, "<BODY><H1>cgic test</H1>\n");
63         /* If a submit button has already been clicked, act on the 
64                 submission of the form. */
65         if ((cgiFormSubmitClicked("testcgic") == cgiFormSuccess) ||
66                 cgiFormSubmitClicked("saveenvironment") == cgiFormSuccess)
67         {
68                 HandleSubmit();
69                 fprintf(cgiOut, "<hr>\n");
70         }
71         /* Now show the form */
72         ShowForm();
73         /* Finish up the page */
74         fprintf(cgiOut, "</BODY></HTML>\n");
75         return 0;
76 }
77
78 void HandleSubmit()
79 {
80         Name();
81         Address();
82         Hungry();
83         Temperature();
84         Frogs();
85         Color();
86         Flavors();
87         NonExButtons();
88         RadioButtons();
89         File();
90         Entries();
91         Cookies();
92         /* The saveenvironment button, in addition to submitting the form,
93                 also saves the resulting CGI scenario to disk for later
94                 replay with the 'load saved environment' button. */
95         if (cgiFormSubmitClicked("saveenvironment") == cgiFormSuccess) {
96                 SaveEnvironment();
97         }
98 }
99
100 void Name() {
101         char name[81];
102         cgiFormStringNoNewlines("name", name, 81);
103         fprintf(cgiOut, "Name: ");
104         cgiHtmlEscape(name);
105         fprintf(cgiOut, "<BR>\n");
106 }
107         
108 void Address() {
109         char address[241];
110         cgiFormString("address", address, 241);
111         fprintf(cgiOut, "Address: <PRE>\n");
112         cgiHtmlEscape(address);
113         fprintf(cgiOut, "</PRE>\n");
114 }
115
116 void Hungry() {
117         if (cgiFormCheckboxSingle("hungry") == cgiFormSuccess) {
118                 fprintf(cgiOut, "I'm Hungry!<BR>\n");
119         } else {
120                 fprintf(cgiOut, "I'm Not Hungry!<BR>\n");
121         }
122 }
123         
124 void Temperature() {
125         double temperature;
126         cgiFormDoubleBounded("temperature", &temperature, 80.0, 120.0, 98.6);
127         fprintf(cgiOut, "My temperature is %f.<BR>\n", temperature);
128 }
129         
130 void Frogs() {
131         int frogsEaten;
132         cgiFormInteger("frogs", &frogsEaten, 0);
133         fprintf(cgiOut, "I have eaten %d frogs.<BR>\n", frogsEaten);
134 }
135
136 char *colors[] = {
137         "Red",
138         "Green",
139         "Blue"
140 };
141
142 void Color() {
143         int colorChoice;
144         cgiFormSelectSingle("colors", colors, 3, &colorChoice, 0);
145         fprintf(cgiOut, "I am: %s<BR>\n", colors[colorChoice]);
146 }        
147
148 char *flavors[] = {
149         "pistachio",
150         "walnut",
151         "creme"
152 };
153
154 void Flavors() {
155         int flavorChoices[3];
156         int i;
157         int result;     
158         int invalid;
159         result = cgiFormSelectMultiple("flavors", flavors, 3, 
160                 flavorChoices, &invalid);
161         if (result == cgiFormNotFound) {
162                 fprintf(cgiOut, "I hate ice cream.<p>\n");
163         } else {        
164                 fprintf(cgiOut, "My favorite ice cream flavors are:\n");
165                 fprintf(cgiOut, "<ul>\n");
166                 for (i=0; (i < 3); i++) {
167                         if (flavorChoices[i]) {
168                                 fprintf(cgiOut, "<li>%s\n", flavors[i]);
169                         }
170                 }
171                 fprintf(cgiOut, "</ul>\n");
172         }
173 }
174
175 char *ages[] = {
176         "1",
177         "2",
178         "3",
179         "4"
180 };
181
182 void RadioButtons() {
183         int ageChoice;
184         char ageText[10];
185         /* Approach #1: check for one of several valid responses. 
186                 Good if there are a short list of possible button values and
187                 you wish to enumerate them. */
188         cgiFormRadio("age", ages, 4, &ageChoice, 0);
189
190         fprintf(cgiOut, "Age of Truck: %s (method #1)<BR>\n", 
191                 ages[ageChoice]);
192
193         /* Approach #2: just get the string. Good
194                 if the information is not critical or if you wish
195                 to verify it in some other way. Note that if
196                 the information is numeric, cgiFormInteger,
197                 cgiFormDouble, and related functions may be
198                 used instead of cgiFormString. */       
199         cgiFormString("age", ageText, 10);
200
201         fprintf(cgiOut, "Age of Truck: %s (method #2)<BR>\n", ageText);
202 }
203
204 char *votes[] = {
205         "A",
206         "B",
207         "C",
208         "D"
209 };
210
211 void NonExButtons() {
212         int voteChoices[4];
213         int i;
214         int result;     
215         int invalid;
216
217         char **responses;
218
219         /* Method #1: check for valid votes. This is a good idea,
220                 since votes for nonexistent candidates should probably
221                 be discounted... */
222         fprintf(cgiOut, "Votes (method 1):<BR>\n");
223         result = cgiFormCheckboxMultiple("vote", votes, 4, 
224                 voteChoices, &invalid);
225         if (result == cgiFormNotFound) {
226                 fprintf(cgiOut, "I hate them all!<p>\n");
227         } else {        
228                 fprintf(cgiOut, "My preferred candidates are:\n");
229                 fprintf(cgiOut, "<ul>\n");
230                 for (i=0; (i < 4); i++) {
231                         if (voteChoices[i]) {
232                                 fprintf(cgiOut, "<li>%s\n", votes[i]);
233                         }
234                 }
235                 fprintf(cgiOut, "</ul>\n");
236         }
237
238         /* Method #2: get all the names voted for and trust them.
239                 This is good if the form will change more often
240                 than the code and invented responses are not a danger
241                 or can be checked in some other way. */
242         fprintf(cgiOut, "Votes (method 2):<BR>\n");
243         result = cgiFormStringMultiple("vote", &responses);
244         if (result == cgiFormNotFound) {        
245                 fprintf(cgiOut, "I hate them all!<p>\n");
246         } else {
247                 int i = 0;
248                 fprintf(cgiOut, "My preferred candidates are:\n");
249                 fprintf(cgiOut, "<ul>\n");
250                 while (responses[i]) {
251                         fprintf(cgiOut, "<li>%s\n", responses[i]);
252                         i++;
253                 }
254                 fprintf(cgiOut, "</ul>\n");
255         }
256         /* We must be sure to free the string array or a memory
257                 leak will occur. Simply calling free() would free
258                 the array but not the individual strings. The
259                 function cgiStringArrayFree() does the job completely. */       
260         cgiStringArrayFree(responses);
261 }
262
263 void Entries()
264 {
265         char **array, **arrayStep;
266         fprintf(cgiOut, "List of All Submitted Form Field Names:<p>\n");
267         if (cgiFormEntries(&array) != cgiFormSuccess) {
268                 return;
269         }
270         arrayStep = array;
271         fprintf(cgiOut, "<ul>\n");
272         while (*arrayStep) {
273                 fprintf(cgiOut, "<li>");
274                 cgiHtmlEscape(*arrayStep);
275                 fprintf(cgiOut, "\n");
276                 arrayStep++;
277         }
278         fprintf(cgiOut, "</ul>\n");
279         cgiStringArrayFree(array);
280 }
281
282 void Cookies()
283 {
284         char **array, **arrayStep;
285         char cname[1024], cvalue[1024];
286         fprintf(cgiOut, "Cookies Submitted On This Call, With Values (Many Browsers NEVER Submit Cookies):<p>\n");
287         if (cgiCookies(&array) != cgiFormSuccess) {
288                 return;
289         }
290         arrayStep = array;
291         fprintf(cgiOut, "<table border=1>\n");
292         fprintf(cgiOut, "<tr><th>Cookie<th>Value</tr>\n");
293         while (*arrayStep) {
294                 char value[1024];
295                 fprintf(cgiOut, "<tr>");
296                 fprintf(cgiOut, "<td>");
297                 cgiHtmlEscape(*arrayStep);
298                 fprintf(cgiOut, "<td>");
299                 cgiCookieString(*arrayStep, value, sizeof(value));
300                 cgiHtmlEscape(value);
301                 fprintf(cgiOut, "\n");
302                 arrayStep++;
303         }
304         fprintf(cgiOut, "</table>\n");
305         cgiFormString("cname", cname, sizeof(cname));   
306         cgiFormString("cvalue", cvalue, sizeof(cvalue));        
307         if (strlen(cname)) {
308                 fprintf(cgiOut, "New Cookie Set On This Call:<p>\n");
309                 fprintf(cgiOut, "Name: ");      
310                 cgiHtmlEscape(cname);
311                 fprintf(cgiOut, "Value: ");     
312                 cgiHtmlEscape(cvalue);
313                 fprintf(cgiOut, "<p>\n");
314                 fprintf(cgiOut, "If your browser accepts cookies (many do not), this new cookie should appear in the above list the next time the form is submitted.<p>\n"); 
315         }
316         cgiStringArrayFree(array);
317 }
318         
319 void File()
320 {
321         cgiFilePtr file;
322         char name[1024];
323         char contentType[1024];
324         char buffer[1024];
325         int size;
326         int got;
327         if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) {
328                 printf("<p>No file was uploaded.<p>\n");
329                 return;
330         } 
331         fprintf(cgiOut, "The filename submitted was: ");
332         cgiHtmlEscape(name);
333         fprintf(cgiOut, "<p>\n");
334         cgiFormFileSize("file", &size);
335         fprintf(cgiOut, "The file size was: %d bytes<p>\n", size);
336         cgiFormFileContentType("file", contentType, sizeof(contentType));
337         fprintf(cgiOut, "The alleged content type of the file was: ");
338         cgiHtmlEscape(contentType);
339         fprintf(cgiOut, "<p>\n");
340         fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.<p>\n");
341         fprintf(cgiOut, "The file's contents are shown here:<p>\n");
342         if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
343                 fprintf(cgiOut, "Could not open the file.<p>\n");
344                 return;
345         }
346         fprintf(cgiOut, "<pre>\n");
347         while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
348                 cgiFormSuccess)
349         {
350                 cgiHtmlEscapeData(buffer, got);
351         }
352         fprintf(cgiOut, "</pre>\n");
353         cgiFormFileClose(file);
354 }
355
356 void ShowForm()
357 {
358         fprintf(cgiOut, "<!-- 2.0: multipart/form-data is required for file uploads. -->");
359         fprintf(cgiOut, "<form method=\"POST\" enctype=\"multipart/form-data\" ");
360         fprintf(cgiOut, "       action=\"");
361         cgiValueEscape(cgiScriptName);
362         fprintf(cgiOut, "\">\n");
363         fprintf(cgiOut, "<p>\n");
364         fprintf(cgiOut, "Text Field containing Plaintext\n");
365         fprintf(cgiOut, "<p>\n");
366         fprintf(cgiOut, "<input type=\"text\" name=\"name\">Your Name\n");
367         fprintf(cgiOut, "<p>\n");
368         fprintf(cgiOut, "Multiple-Line Text Field\n");
369         fprintf(cgiOut, "<p>\n");
370         fprintf(cgiOut, "<textarea NAME=\"address\" ROWS=4 COLS=40>\n");
371         fprintf(cgiOut, "Default contents go here. \n");
372         fprintf(cgiOut, "</textarea>\n");
373         fprintf(cgiOut, "<p>\n");
374         fprintf(cgiOut, "Checkbox\n");
375         fprintf(cgiOut, "<p>\n");
376         fprintf(cgiOut, "<input type=\"checkbox\" name=\"hungry\" checked>Hungry\n");
377         fprintf(cgiOut, "<p>\n");
378         fprintf(cgiOut, "Text Field containing a Numeric Value\n");
379         fprintf(cgiOut, "<p>\n");
380         fprintf(cgiOut, "<input type=\"text\" name=\"temperature\" value=\"98.6\">\n");
381         fprintf(cgiOut, "Blood Temperature (80.0-120.0)\n");
382         fprintf(cgiOut, "<p>\n");
383         fprintf(cgiOut, "Text Field containing an Integer Value\n");
384         fprintf(cgiOut, "<p>\n");
385         fprintf(cgiOut, "<input type=\"text\" name=\"frogs\" value=\"1\">\n");
386         fprintf(cgiOut, "Frogs Eaten\n");
387         fprintf(cgiOut, "<p>\n");
388         fprintf(cgiOut, "Single-SELECT\n");
389         fprintf(cgiOut, "<br>\n");
390         fprintf(cgiOut, "<select name=\"colors\">\n");
391         fprintf(cgiOut, "<option value=\"Red\">Red\n");
392         fprintf(cgiOut, "<option value=\"Green\">Green\n");
393         fprintf(cgiOut, "<option value=\"Blue\">Blue\n");
394         fprintf(cgiOut, "</select>\n");
395         fprintf(cgiOut, "<br>\n");
396         fprintf(cgiOut, "Multiple-SELECT\n");
397         fprintf(cgiOut, "<br>\n");
398         fprintf(cgiOut, "<select name=\"flavors\" multiple>\n");
399         fprintf(cgiOut, "<option value=\"pistachio\">Pistachio\n");
400         fprintf(cgiOut, "<option value=\"walnut\">Walnut\n");
401         fprintf(cgiOut, "<option value=\"creme\">Creme\n");
402         fprintf(cgiOut, "</select>\n");
403         fprintf(cgiOut, "<p>Exclusive Radio Button Group: Age of Truck in Years\n");
404         fprintf(cgiOut, "<input type=\"radio\" name=\"age\" value=\"1\">1\n");
405         fprintf(cgiOut, "<input type=\"radio\" name=\"age\" value=\"2\">2\n");
406         fprintf(cgiOut, "<input type=\"radio\" name=\"age\" value=\"3\" checked>3\n");
407         fprintf(cgiOut, "<input type=\"radio\" name=\"age\" value=\"4\">4\n");
408         fprintf(cgiOut, "<p>Nonexclusive Checkbox Group: Voting for Zero through Four Candidates\n");
409         fprintf(cgiOut, "<input type=\"checkbox\" name=\"vote\" value=\"A\">A\n");
410         fprintf(cgiOut, "<input type=\"checkbox\" name=\"vote\" value=\"B\">B\n");
411         fprintf(cgiOut, "<input type=\"checkbox\" name=\"vote\" value=\"C\">C\n");
412         fprintf(cgiOut, "<input type=\"checkbox\" name=\"vote\" value=\"D\">D\n");
413         fprintf(cgiOut, "<p>File Upload:\n");
414         fprintf(cgiOut, "<input type=\"file\" name=\"file\" value=\"\"> (Select A Local File)\n");
415         fprintf(cgiOut, "<p>\n");
416         fprintf(cgiOut, "<p>Set a Cookie<p>\n");
417         fprintf(cgiOut, "<input name=\"cname\" value=\"\"> Cookie Name\n");
418         fprintf(cgiOut, "<input name=\"cvalue\" value=\"\"> Cookie Value<p>\n");
419         fprintf(cgiOut, "<input type=\"submit\" name=\"testcgic\" value=\"Submit Request\">\n");
420         fprintf(cgiOut, "<input type=\"reset\" value=\"Reset Request\">\n");
421         fprintf(cgiOut, "<p>Save the CGI Environment<p>\n");
422         fprintf(cgiOut, "Pressing this button will submit the form, then save the CGI environment so that it can be replayed later by calling cgiReadEnvironment (in a debugger, for instance).<p>\n");
423         fprintf(cgiOut, "<input type=\"submit\" name=\"saveenvironment\" value=\"Save Environment\">\n");
424         fprintf(cgiOut, "</form>\n");
425 }
426
427 void CookieSet()
428 {
429         char cname[1024];
430         char cvalue[1024];
431         /* Must set cookies BEFORE calling cgiHeaderContentType */
432         cgiFormString("cname", cname, sizeof(cname));   
433         cgiFormString("cvalue", cvalue, sizeof(cvalue));        
434         if (strlen(cname)) {
435                 /* Cookie lives for one day (or until browser chooses
436                         to get rid of it, which may be immediately),
437                         and applies only to this script on this site. */        
438                 cgiHeaderCookieSetString(cname, cvalue,
439                         86400, cgiScriptName, SERVER_NAME);
440         }
441 }
442
443 void LoadEnvironment()
444 {
445         if (cgiReadEnvironment(SAVED_ENVIRONMENT) != 
446                 cgiEnvironmentSuccess) 
447         {
448                 cgiHeaderContentType("text/html");
449                 fprintf(cgiOut, "<head>Error</head>\n");
450                 fprintf(cgiOut, "<body><h1>Error</h1>\n");
451                 fprintf(cgiOut, "cgiReadEnvironment failed. Most "
452                         "likely you have not saved an environment "
453                         "yet.\n");
454                 exit(0);
455         }
456         /* OK, return now and show the results of the saved environment */
457 }
458
459 void SaveEnvironment()
460 {
461         if (cgiWriteEnvironment(SAVED_ENVIRONMENT) != 
462                 cgiEnvironmentSuccess) 
463         {
464                 fprintf(cgiOut, "<p>cgiWriteEnvironment failed. Most "
465                         "likely %s is not a valid path or is not "
466                         "writable by the user that the CGI program "
467                         "is running as.<p>\n", SAVED_ENVIRONMENT);
468         } else {
469                 fprintf(cgiOut, "<p>Environment saved. Click this button "
470                         "to restore it, playing back exactly the same "
471                         "scenario: "
472                         "<form method=POST action=\"");
473                 cgiValueEscape(cgiScriptName);
474                 fprintf(cgiOut, "\">" 
475                         "<input type=\"submit\" "
476                         "value=\"Load Environment\" "
477                         "name=\"loadenvironment\"></form><p>\n");
478         }
479 }
480