[1] | 1 | |
---|
| 2 | /* |
---|
| 3 | $Id: cgictest.c,v 1.2 2004/04/07 17:09:27 fox Exp $ |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | #include <stdio.h> |
---|
| 7 | #include "cgic.h" |
---|
| 8 | |
---|
| 9 | void Name(); |
---|
| 10 | void Address(); |
---|
| 11 | void Hungry(); |
---|
| 12 | void Temperature(); |
---|
| 13 | void Frogs(); |
---|
| 14 | void Color(); |
---|
| 15 | void Flavors(); |
---|
| 16 | void NonExButtons(); |
---|
| 17 | void RadioButtons(); |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | int cgiMain() { |
---|
| 21 | #if DEBUG |
---|
| 22 | /* Load a saved CGI scenario if we're debugging */ |
---|
| 23 | cgiReadEnvironment("/home/boutell/public_html/capcgi.dat"); |
---|
| 24 | #endif |
---|
[6] | 25 | dup2(cgiOut,stdout); |
---|
| 26 | printf("Content-Type: text/html; charset=utf-8\r\nStatus: 200 OK\r\n\r\n"); |
---|
| 27 | //cgiHeaderContentType("text/html"); |
---|
| 28 | printf( "<HTML><HEAD>\n"); |
---|
| 29 | printf( "<TITLE>cgic test</TITLE></HEAD>\n"); |
---|
| 30 | printf( "<BODY><H1>cgic test</H1>\n"); |
---|
[1] | 31 | Name(); |
---|
| 32 | Address(); |
---|
| 33 | Hungry(); |
---|
| 34 | Temperature(); |
---|
| 35 | Frogs(); |
---|
| 36 | Color(); |
---|
| 37 | Flavors(); |
---|
| 38 | NonExButtons(); |
---|
| 39 | RadioButtons(); |
---|
[6] | 40 | printf( "</BODY></HTML>\n"); |
---|
[1] | 41 | return 0; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void Name() { |
---|
| 45 | char name[81]; |
---|
| 46 | int result = cgiFormStringNoNewlines("name", name, 81); |
---|
| 47 | switch (result) { |
---|
| 48 | case cgiFormSuccess: |
---|
[6] | 49 | printf( "Name fetched, result code: cgiFormSuccess<br>\n"); |
---|
[1] | 50 | break; |
---|
| 51 | case cgiFormTruncated: |
---|
[6] | 52 | printf( "Name fetched, result code: cgiFormTruncated<br>\n"); |
---|
[1] | 53 | break; |
---|
| 54 | case cgiFormEmpty: |
---|
[6] | 55 | printf( "Name fetched, result code: cgiFormEmpty<br>\n"); |
---|
[1] | 56 | break; |
---|
| 57 | case cgiFormNotFound: |
---|
[6] | 58 | printf( "Name fetched, result code: cgiFormNotFound<br>\n"); |
---|
[1] | 59 | break; |
---|
| 60 | case cgiFormMemory: |
---|
[6] | 61 | printf( "Name fetched, result code: cgiFormMemory<br>\n"); |
---|
[1] | 62 | break; |
---|
| 63 | default: |
---|
[6] | 64 | printf( "Name fetched, unexpected result code: %d\n", result); |
---|
[1] | 65 | break; |
---|
| 66 | } |
---|
[6] | 67 | printf( "Name: %s<BR>\n", name); |
---|
[1] | 68 | } |
---|
| 69 | |
---|
| 70 | void Address() { |
---|
| 71 | char address[241]; |
---|
| 72 | cgiFormString("address", address, 241); |
---|
[6] | 73 | printf( "Address: <PRE>\n%s</PRE>\n", address); |
---|
[1] | 74 | } |
---|
| 75 | |
---|
| 76 | void Hungry() { |
---|
| 77 | if (cgiFormCheckboxSingle("hungry") == cgiFormSuccess) { |
---|
[6] | 78 | printf( "I'm Hungry!<BR>\n"); |
---|
[1] | 79 | } else { |
---|
[6] | 80 | printf( "I'm Not Hungry!<BR>\n"); |
---|
[1] | 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void Temperature() { |
---|
| 85 | double temperature; |
---|
| 86 | cgiFormDoubleBounded("temperature", &temperature, 80.0, 120.0, 98.6); |
---|
[6] | 87 | printf( "My temperature is %f.<BR>\n", temperature); |
---|
[1] | 88 | } |
---|
| 89 | |
---|
| 90 | void Frogs() { |
---|
| 91 | int frogsEaten; |
---|
| 92 | cgiFormInteger("frogs", &frogsEaten, 0); |
---|
[6] | 93 | printf( "I have eaten %d frogs.<BR>\n", frogsEaten); |
---|
[1] | 94 | } |
---|
| 95 | |
---|
| 96 | char *colors[] = { |
---|
| 97 | "Red", |
---|
| 98 | "Green", |
---|
| 99 | "Blue" |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | void Color() { |
---|
| 103 | int colorChoice; |
---|
| 104 | cgiFormSelectSingle("colors", colors, 3, &colorChoice, 0); |
---|
[6] | 105 | printf( "I am: %s<BR>\n", colors[colorChoice]); |
---|
[1] | 106 | } |
---|
| 107 | |
---|
| 108 | char *flavors[] = { |
---|
| 109 | "pistachio", |
---|
| 110 | "walnut", |
---|
| 111 | "creme" |
---|
| 112 | }; |
---|
| 113 | |
---|
| 114 | void Flavors() { |
---|
| 115 | int flavorChoices[3]; |
---|
| 116 | int i; |
---|
| 117 | int result; |
---|
| 118 | int invalid; |
---|
| 119 | result = cgiFormSelectMultiple("flavors", flavors, 3, |
---|
| 120 | flavorChoices, &invalid); |
---|
| 121 | if (result == cgiFormNotFound) { |
---|
[6] | 122 | printf( "I hate ice cream.<p>\n"); |
---|
[1] | 123 | } else { |
---|
[6] | 124 | printf( "My favorite ice cream flavors are:\n"); |
---|
| 125 | printf( "<ul>\n"); |
---|
[1] | 126 | for (i=0; (i < 3); i++) { |
---|
| 127 | if (flavorChoices[i]) { |
---|
[6] | 128 | printf( "<li>%s\n", flavors[i]); |
---|
[1] | 129 | } |
---|
| 130 | } |
---|
[6] | 131 | printf( "</ul>\n"); |
---|
[1] | 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | char *ages[] = { |
---|
| 136 | "1", |
---|
| 137 | "2", |
---|
| 138 | "3", |
---|
| 139 | "4" |
---|
| 140 | }; |
---|
| 141 | |
---|
| 142 | void RadioButtons() { |
---|
| 143 | int ageChoice; |
---|
| 144 | char ageText[10]; |
---|
| 145 | /* Approach #1: check for one of several valid responses. |
---|
| 146 | Good if there are a short list of possible button values and |
---|
| 147 | you wish to enumerate them. */ |
---|
| 148 | cgiFormRadio("age", ages, 4, &ageChoice, 0); |
---|
| 149 | |
---|
[6] | 150 | printf( "Age of Truck: %s (method #1)<BR>\n", |
---|
[1] | 151 | ages[ageChoice]); |
---|
| 152 | |
---|
| 153 | /* Approach #2: just get the string. Good |
---|
| 154 | if the information is not critical or if you wish |
---|
| 155 | to verify it in some other way. Note that if |
---|
| 156 | the information is numeric, cgiFormInteger, |
---|
| 157 | cgiFormDouble, and related functions may be |
---|
| 158 | used instead of cgiFormString. */ |
---|
| 159 | cgiFormString("age", ageText, 10); |
---|
| 160 | |
---|
[6] | 161 | printf( "Age of Truck: %s (method #2)<BR>\n", ageText); |
---|
[1] | 162 | } |
---|
| 163 | |
---|
| 164 | char *votes[] = { |
---|
| 165 | "A", |
---|
| 166 | "B", |
---|
| 167 | "C", |
---|
| 168 | "D" |
---|
| 169 | }; |
---|
| 170 | |
---|
| 171 | void NonExButtons() { |
---|
| 172 | int voteChoices[4]; |
---|
| 173 | int i; |
---|
| 174 | int result; |
---|
| 175 | int invalid; |
---|
| 176 | |
---|
| 177 | char **responses; |
---|
| 178 | |
---|
| 179 | /* Method #1: check for valid votes. This is a good idea, |
---|
| 180 | since votes for nonexistent candidates should probably |
---|
| 181 | be discounted... */ |
---|
[6] | 182 | printf( "Votes (method 1):<BR>\n"); |
---|
[1] | 183 | result = cgiFormCheckboxMultiple("vote", votes, 4, |
---|
| 184 | voteChoices, &invalid); |
---|
| 185 | if (result == cgiFormNotFound) { |
---|
[6] | 186 | printf( "I hate them all!<p>\n"); |
---|
[1] | 187 | } else { |
---|
[6] | 188 | printf( "My preferred candidates are:\n"); |
---|
| 189 | printf( "<ul>\n"); |
---|
[1] | 190 | for (i=0; (i < 4); i++) { |
---|
| 191 | if (voteChoices[i]) { |
---|
[6] | 192 | printf( "<li>%s\n", votes[i]); |
---|
[1] | 193 | } |
---|
| 194 | } |
---|
[6] | 195 | printf( "</ul>\n"); |
---|
[1] | 196 | } |
---|
| 197 | |
---|
| 198 | /* Method #2: get all the names voted for and trust them. |
---|
| 199 | This is good if the form will change more often |
---|
| 200 | than the code and invented responses are not a danger |
---|
| 201 | or can be checked in some other way. */ |
---|
[6] | 202 | printf( "Votes (method 2):<BR>\n"); |
---|
[1] | 203 | result = cgiFormStringMultiple("vote", &responses); |
---|
| 204 | if (result == cgiFormNotFound) { |
---|
[6] | 205 | printf( "I hate them all!<p>\n"); |
---|
[1] | 206 | } else { |
---|
| 207 | int i = 0; |
---|
[6] | 208 | printf( "My preferred candidates are:\n"); |
---|
| 209 | printf( "<ul>\n"); |
---|
[1] | 210 | while (responses[i]) { |
---|
[6] | 211 | printf( "<li>%s\n", responses[i]); |
---|
[1] | 212 | i++; |
---|
| 213 | } |
---|
[6] | 214 | printf( "</ul>\n"); |
---|
[1] | 215 | } |
---|
| 216 | /* We must be sure to free the string array or a memory |
---|
| 217 | leak will occur. Simply calling free() would free |
---|
| 218 | the array but not the individual strings. The |
---|
| 219 | function cgiStringArrayFree() does the job completely. */ |
---|
| 220 | cgiStringArrayFree(responses); |
---|
| 221 | } |
---|
| 222 | |
---|