4 * Variable substitution type stuff
16 #include <sys/types.h>
18 #include <sys/socket.h>
22 #include <netinet/in.h>
32 struct wcsubst *global_subst = NULL;
36 * Clear out the list of substitution variables local to this session
38 void clear_local_substs(void) {
41 while (WC->vars != NULL) {
44 if ((WC->vars->wcs_type == WCS_STRING)
45 || (WC->vars->wcs_type == WCS_SERVCMD)) {
46 free(WC->vars->wcs_value);
56 * Add a substitution variable (local to this session)
58 void svprintf(char *keyname, int keytype, const char *format,...)
64 va_start(arg_ptr, format);
65 vsprintf(wbuf, format, arg_ptr);
68 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
70 ptr->wcs_type = keytype;
71 strcpy(ptr->wcs_key, keyname);
72 ptr->wcs_value = malloc(strlen(wbuf)+1);
73 strcpy(ptr->wcs_value, wbuf);
78 * Add a substitution variable (local to this session) that does a callback
80 void svcallback(char *keyname, void (*fcn_ptr)() )
84 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
86 ptr->wcs_type = WCS_FUNCTION;
87 strcpy(ptr->wcs_key, keyname);
88 ptr->wcs_function = fcn_ptr;
95 * back end for print_value_of() ... does a server command
97 void pvo_do_cmd(char *servcmd) {
107 wprintf("%s\n", &buf[4]);
113 wprintf("%s\n", &buf[4]);
122 * Print the value of a variable
124 void print_value_of(char *keyname) {
128 for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
129 if (!strcasecmp(ptr->wcs_key, keyname)) {
130 if (ptr->wcs_type == WCS_STRING) {
131 wprintf("%s", ptr->wcs_value);
133 else if (ptr->wcs_type == WCS_SERVCMD) {
134 pvo_do_cmd(ptr->wcs_value);
136 else if (ptr->wcs_type == WCS_FUNCTION) {
137 (*ptr->wcs_function) ();
146 * Display a variable-substituted template
148 void do_template(void *templatename) {
149 char filename[PATH_MAX];
152 char outbuf[sizeof inbuf];
153 char key[sizeof inbuf];
156 strcpy(filename, "static/");
157 strcat(filename, templatename);
159 strcat(filename, ".wml");
161 strcat(filename, ".html");
163 fp = fopen(filename, "r");
165 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
166 wprintf("'%s' - %s<BR>\n",
167 templatename, strerror(errno));
173 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
176 while (strlen(inbuf) > 0) {
178 for (i=strlen(inbuf); i>=0; --i) {
179 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
182 wprintf("%s", inbuf);
186 strncpy(outbuf, inbuf, pos);
188 wprintf("%s", outbuf);
189 strcpy(inbuf, &inbuf[pos]);
191 for (i=strlen(inbuf); i>=0; --i) {
192 if (inbuf[i]=='>') pos = i;
194 strncpy(key, &inbuf[2], pos-2);
197 strcpy(inbuf, &inbuf[pos+1]);