00001 /***************************************************************************** 00002 * Copyright (C) 2000 Jim Kent. This source code may be freely used * 00003 * for personal, academic, and non-profit purposes. Commercial use * 00004 * permitted only by explicit agreement with Jim Kent (jim_kent@pacbell.net) * 00005 *****************************************************************************/ 00006 /* Cheapcgi.h - turns variables passed from the web form into 00007 * something that C understands. */ 00008 00009 #ifndef CHEAPCGI_H 00010 #define CHEAPCGI_H 00011 00012 #ifndef DYSTRING_H 00013 #include "dystring.h" 00014 #endif 00015 00016 #ifndef HASH_H 00017 #include "hash.h" 00018 #endif 00019 00020 struct cgiVar 00021 /* Info on one cgi variable. */ 00022 { 00023 struct cgiVar *next; /* Next in list. */ 00024 char *name; /* Name - allocated in hash. */ 00025 char *val; /* Value - also not allocated here. */ 00026 boolean saved; /* True if saved. */ 00027 }; 00028 00029 /* return the list of cgiVar's */ 00030 struct cgiVar* cgiVarList(); 00031 00032 /* Get the string associated with varName from the cookie string. */ 00033 char *findCookieData(char *varName); 00034 00035 void dumpCookieList(); 00036 /* Print out the cookie list. */ 00037 00038 /* Return TRUE if looks like we're being run as a CGI. */ 00039 boolean cgiIsOnWeb(); 00040 00041 /* Return name of script so libs can do context-sensitive stuff. */ 00042 char *cgiScriptName(); 00043 00044 /* Return name of server */ 00045 char *cgiServerName(); 00046 00047 /* These routines abort the html output if the input isn't 00048 * there or is misformatted. */ 00049 char *cgiString(char *varName); 00050 int cgiInt(char *varName); 00051 double cgiDouble(char *varName); 00052 00053 boolean cgiBoolean(char *varName); 00054 /* The cgiBoolean is a little problematic. If the variable 00055 * is TRUE it exists, but if it is false it is simply not 00056 * defined. cgiBoolean() thus returns FALSE if the CGI 00057 * variable doesn't exist or if it is set to FALSE. To 00058 * work around this when need be use cgiBooleanDefined(), 00059 * which relies on the fact that when we define a boolean 00060 * variable we also define a hidden variable. */ 00061 00062 boolean cgiBooleanDefined(char *name); 00063 /* Return TRUE if boolean variable is defined (by 00064 * checking for shadow). */ 00065 00066 char *cgiBooleanShadowPrefix(); 00067 /* Prefix for shadow variable set with boolean variables. */ 00068 00069 void cgiMakeHiddenBoolean(char *name, boolean on); 00070 /* Make hidden boolean variable. Also make a shadow hidden variable so we 00071 * can distinguish between variable not present and 00072 * variable set to false. */ 00073 00074 int cgiIntExp(char *varName); 00075 /* Evaluate an integer expression in varName and 00076 * return value. */ 00077 00078 char *cgiOptionalString(char *varName); 00079 /* Return value of string if it exists in cgi environment, else NULL */ 00080 00081 char *cgiUsualString(char *varName, char *usual); 00082 /* Return value of string if it exists in cgi environment. 00083 * Otherwiser return 'usual' */ 00084 00085 struct slName *cgiStringList(char *varName); 00086 /* Find list of cgi variables with given name. This 00087 * may be empty. Free result with slFreeList(). */ 00088 00089 int cgiOptionalInt(char *varName, int defaultVal); 00090 /* This returns value of varName if it exists in cgi environment, 00091 * otherwise it returns defaultVal. */ 00092 00093 double cgiOptionalDouble(char *varName, double defaultVal); 00094 /* Returns double value. */ 00095 00096 #define cgiUsualInt cgiOptionalInt 00097 #define cgiUsualDouble cgiOptionalDouble 00098 00099 struct cgiChoice 00100 /* Choice table */ 00101 { 00102 char *name; 00103 int value; 00104 }; 00105 00106 int cgiOneChoice(char *varName, struct cgiChoice *choices, int choiceSize); 00107 /* Returns value associated with string variable in choice table. */ 00108 00109 boolean cgiVarExists(char *varName); 00110 /* Returns TRUE if the variable was passed in. */ 00111 00112 void cgiBadVar(char *varName); 00113 /* Complain about a variable that's not there. */ 00114 00115 void cgiDecode(char *in, char *out, int inLength); 00116 /* Decode from cgi pluses-for-spaces format to normal. 00117 * Out will be a little shorter than in typically. */ 00118 00119 char *cgiEncode(char *inString); 00120 /* Return a cgi-encoded version of inString. 00121 * Alphanumerics kept as is, space translated to plus, 00122 * and all other characters translated to %hexVal. 00123 * You can free return value with freeMem(). */ 00124 00125 char *cgiEncodeFull(char *inString); 00126 /* Return a cgi-encoded version of inString (no + for space!). 00127 * Alphanumerics/./_ kept as is and all other characters translated to 00128 * %hexVal. */ 00129 00130 void cgiMakeButtonWithMsg(char *name, char *value, char *msg); 00131 /* Make 'submit' type button. Display msg on mouseover, if present*/ 00132 00133 void cgiMakeButton(char *name, char *value); 00134 /* Make 'submit' type button. */ 00135 00136 void cgiMakeOnClickButton(char *command, char *value); 00137 /* Make 'push' type button with client side onClick (java)script. */ 00138 00139 void cgiMakeOptionalButton(char *name, char *value, boolean disabled); 00140 /* Make 'submit' type button that can be disabled. */ 00141 00142 void cgiMakeRadioButton(char *name, char *value, boolean checked); 00143 /* Make radio type button. A group of radio buttons should have the 00144 * same name but different values. The default selection should be 00145 * sent with checked on. */ 00146 00147 void cgiMakeOnClickRadioButton(char *name, char *value, boolean checked, 00148 char *command); 00149 /* Make radio type button with onClick command. 00150 * A group of radio buttons should have the 00151 * same name but different values. The default selection should be 00152 * sent with checked on. */ 00153 00154 void cgiMakeCheckBoxWithMsg(char *name, boolean checked, char *msg); 00155 /* Make check box. Also make a shadow hidden variable so we 00156 * can distinguish between variable not present and 00157 * variable set to false. Use msg as mousever if present */ 00158 00159 void cgiMakeCheckBox(char *name, boolean checked); 00160 /* Make check box. */ 00161 00162 void cgiMakeTextArea(char *varName, char *initialVal, int rowCount, int columnCount); 00163 /* Make a text area with area rowCount X columnCount and with text: intialVal. */ 00164 00165 void cgiMakeTextAreaDisableable(char *varName, char *initialVal, int rowCount, int columnCount, boolean disabled); 00166 /* Make a text area that can be disabled. The rea has rowCount X 00167 * columnCount and with text: intialVal */ 00168 00169 void cgiMakeTextVar(char *varName, char *initialVal, int charSize); 00170 /* Make a text control filled with initial value. If charSize 00171 * is zero it's calculated from initialVal size. */ 00172 00173 void cgiMakeIntVar(char *varName, int initialVal, int maxDigits); 00174 /* Make a text control filled with initial integer value. */ 00175 00176 void cgiMakeDoubleVar(char *varName, double initialVal, int maxDigits); 00177 /* Make a text control filled with initial floating-point value. */ 00178 00179 void cgiMakeDropListClass(char *name, char *menu[], int menuSize, char *checked, char *class); 00180 /* Make a drop-down list with names and style sheet class. */ 00181 00182 void cgiMakeDropList(char *name, char *menu[], int menuSize, char *checked); 00183 /* Make a drop-down list with names. 00184 * uses style "normalText" */ 00185 00186 void cgiMakeDropListWithVals(char *name, char *menu[], char *values[], 00187 int menuSize, char *checked); 00188 /* Make a drop-down list with names and values. In this case checked 00189 * corresponds to a value, not a menu. */ 00190 00191 void cgiMakeDropListFull(char *name, char *menu[], char *values[], int menuSize, char *checked, char *extraAttribs); 00192 /* Make a drop-down list with names and values. */ 00193 00194 void cgiMakeMultList(char *name, char *menu[], int menuSize, char *checked, int length); 00195 /* Make a list of names which can have multiple selections. 00196 * Same as drop-down list except "multiple" is added to select tag */ 00197 00198 void cgiMakeHiddenVar(char *varName, char *string); 00199 /* Store string in hidden input for next time around. */ 00200 00201 void cgiContinueHiddenVar(char *varName); 00202 /* Write CGI var back to hidden input for next time around. 00203 * (if it exists). */ 00204 00205 void cgiContinueAllVars(); 00206 /* Write back all CGI vars as hidden input for next time around. */ 00207 00208 void cgiVarExclude(char *varName); 00209 /* If varName exists, remove it. */ 00210 00211 void cgiVarExcludeExcept(char **varNames); 00212 /* Exclude all variables except for those in NULL 00213 * terminated array varNames. varNames may be NULL 00214 * in which case nothing is excluded. */ 00215 00216 void cgiVarSet(char *varName, char *val); 00217 /* Set a cgi variable to a particular value. */ 00218 00219 struct dyString *cgiUrlString(); 00220 /* Get URL-formatted that expresses current CGI variable state. */ 00221 00222 boolean cgiSpoof(int *pArgc, char *argv[]); 00223 /* Use the command line to set up things as if we were a CGI program. 00224 * User types in command line (assuming your program called cgiScript) 00225 * like: 00226 * cgiScript nonCgiArg1 var1=value1 var2=value2 var3=value3 nonCgiArg2 00227 * or like 00228 * cgiScript nonCgiArg1 var1=value1&var2=value2&var3=value3 nonCgiArg2 00229 * (The non-cgi arguments can occur anywhere. The cgi arguments (all containing 00230 * the character '=') are erased from argc/argv. Normally you call this 00231 * cgiSpoof(&argc, argv); 00232 */ 00233 00234 boolean cgiFromCommandLine(int *pArgc, char *argv[], boolean preferWeb); 00235 /* Use the command line to set up things as if we were a CGI program. 00236 * If preferWeb is TRUE will choose real CGI variables over command 00237 * line ones. */ 00238 00239 void useTempFile(); 00240 /* tell cheapcgi to use temp files */ 00241 00242 boolean cgiFromFile(char *fileName); 00243 /* Set up a cgi environment using parameters stored in a file. 00244 * Takes file with arguments in the form: 00245 * argument1=someVal 00246 * # This is a comment 00247 * argument2=someOtherVal 00248 * ... 00249 * and puts them into the cgi environment so that the usual 00250 * cgiGetVar() commands can be used. Useful when a program 00251 * has a lot of possible parameters. 00252 */ 00253 00254 boolean cgiParseInput(char *input, struct hash **retHash, 00255 struct cgiVar **retList); 00256 /* Parse cgi-style input into a hash table and list. This will alter 00257 * the input data. The hash table will contain references back 00258 * into input, so please don't free input until you're done with 00259 * the hash. Prints message and returns FALSE if there's an error.*/ 00260 00261 void cgiSimpleTableStart(); 00262 /* start HTML table -- no customization. Leaves room 00263 * for a fancier implementation */ 00264 00265 void cgiTableEnd(); 00266 /* end HTML table */ 00267 00268 void cgiMakeSubmitButton(); 00269 /* Make 'submit' type button. */ 00270 00271 void cgiMakeResetButton(); 00272 /* Make 'reset' type button. */ 00273 00274 void cgiMakeClearButton(char *form, char *field); 00275 /* Make button to clear a text field. */ 00276 00277 void cgiMakeFileEntry(char *name); 00278 /* Make file entry box/browser */ 00279 00280 void cgiSimpleTableRowStart(); 00281 /* Start table row */ 00282 00283 void cgiTableRowEnd(); 00284 /* End table row */ 00285 00286 void cgiSimpleTableFieldStart(); 00287 /* Start table field */ 00288 00289 void cgiTableFieldEnd(); 00290 /* End table field */ 00291 00292 void cgiTableField(char *text); 00293 /* Make table field entry */ 00294 00295 void cgiTableFieldWithMsg(char *text, char *msg); 00296 /* Make table field entry with mouseover */ 00297 00298 void cgiParagraph(char *text); 00299 /* Make text paragraph */ 00300 00301 00302 #endif /* CHEAPCGI_H */
1.5.2