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,...)
62 struct wcsubst *ptr = NULL;
65 va_start(arg_ptr, format);
66 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
69 /* First scan through to see if we're doing a replacement of
72 for (scan=WC->vars; scan!=NULL; scan=scan->next) {
73 if (!strcasecmp(scan->wcs_key, keyname)) {
79 /* Otherwise allocate a new one */
81 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
83 strcpy(ptr->wcs_key, keyname);
87 ptr->wcs_type = keytype;
88 ptr->wcs_value = strdup(wbuf);
92 * Add a substitution variable (local to this session) that does a callback
94 void svcallback(char *keyname, void (*fcn_ptr)() )
98 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
100 ptr->wcs_type = WCS_FUNCTION;
101 strcpy(ptr->wcs_key, keyname);
102 ptr->wcs_function = fcn_ptr;
109 * back end for print_value_of() ... does a server command
111 void pvo_do_cmd(char *servcmd) {
121 wprintf("%s\n", &buf[4]);
124 fmout(NULL, "CENTER");
127 wprintf("%s\n", &buf[4]);
136 * Print the value of a variable
138 void print_value_of(char *keyname) {
142 if (keyname[0] == '=') {
143 do_template(&keyname[1]);
146 if (!strcasecmp(keyname, "SERV_PID")) {
147 wprintf("%d", serv_info.serv_pid);
150 else if (!strcasecmp(keyname, "SERV_NODENAME")) {
151 escputs(serv_info.serv_nodename);
154 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
155 escputs(serv_info.serv_humannode);
158 else if (!strcasecmp(keyname, "SERV_FQDN")) {
159 escputs(serv_info.serv_fqdn);
162 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
163 escputs(serv_info.serv_software);
166 else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
168 serv_info.serv_rev_level / 100,
169 serv_info.serv_rev_level % 100
173 else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
174 escputs(serv_info.serv_bbs_city);
177 else if (!strcasecmp(keyname, "CURRENT_USER")) {
178 escputs(WC->wc_username);
181 else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
182 escputs(WC->wc_roomname);
185 /* Page-local variables */
186 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
187 if (!strcasecmp(ptr->wcs_key, keyname)) {
188 if (ptr->wcs_type == WCS_STRING) {
189 wprintf("%s", ptr->wcs_value);
191 else if (ptr->wcs_type == WCS_SERVCMD) {
192 pvo_do_cmd(ptr->wcs_value);
194 else if (ptr->wcs_type == WCS_FUNCTION) {
195 (*ptr->wcs_function) ();
204 * Display a variable-substituted template
206 void do_template(void *templatename) {
207 char filename[PATH_MAX];
210 char outbuf[sizeof inbuf];
211 char key[sizeof inbuf];
214 strcpy(filename, "static/");
215 strcat(filename, templatename);
217 strcat(filename, ".wml");
219 strcat(filename, ".html");
221 fp = fopen(filename, "r");
223 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
224 wprintf("'%s' - %s<BR>\n",
225 templatename, strerror(errno));
231 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
234 while (strlen(inbuf) > 0) {
236 for (i=strlen(inbuf); i>=0; --i) {
237 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
240 wprintf("%s", inbuf);
244 strncpy(outbuf, inbuf, pos);
246 wprintf("%s", outbuf);
247 strcpy(inbuf, &inbuf[pos]);
249 for (i=strlen(inbuf); i>=0; --i) {
250 if (inbuf[i]=='>') pos = i;
252 strncpy(key, &inbuf[2], pos-2);
255 strcpy(inbuf, &inbuf[pos+1]);