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 vsprintf(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));
85 ptr->wcs_type = keytype;
86 strcpy(ptr->wcs_key, keyname);
87 ptr->wcs_value = malloc(strlen(wbuf)+1);
88 strcpy(ptr->wcs_value, wbuf);
93 * Add a substitution variable (local to this session) that does a callback
95 void svcallback(char *keyname, void (*fcn_ptr)() )
99 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
100 ptr->next = WC->vars;
101 ptr->wcs_type = WCS_FUNCTION;
102 strcpy(ptr->wcs_key, keyname);
103 ptr->wcs_function = fcn_ptr;
110 * back end for print_value_of() ... does a server command
112 void pvo_do_cmd(char *servcmd) {
122 wprintf("%s\n", &buf[4]);
125 fmout(NULL, "CENTER");
128 wprintf("%s\n", &buf[4]);
137 * Print the value of a variable
139 void print_value_of(char *keyname) {
143 if (keyname[0] == '=') {
144 do_template(&keyname[1]);
147 if (!strcasecmp(keyname, "SERV_PID")) {
148 wprintf("%d", serv_info.serv_pid);
151 else if (!strcasecmp(keyname, "SERV_NODENAME")) {
152 escputs(serv_info.serv_nodename);
155 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
156 escputs(serv_info.serv_humannode);
159 else if (!strcasecmp(keyname, "SERV_FQDN")) {
160 escputs(serv_info.serv_fqdn);
163 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
164 escputs(serv_info.serv_software);
167 else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
169 serv_info.serv_rev_level / 100,
170 serv_info.serv_rev_level % 100
174 else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
175 escputs(serv_info.serv_bbs_city);
178 else if (!strcasecmp(keyname, "CURRENT_USER")) {
179 escputs(WC->wc_username);
182 else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
183 escputs(WC->wc_roomname);
186 /* Page-local variables */
187 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
188 if (!strcasecmp(ptr->wcs_key, keyname)) {
189 if (ptr->wcs_type == WCS_STRING) {
190 wprintf("%s", ptr->wcs_value);
192 else if (ptr->wcs_type == WCS_SERVCMD) {
193 pvo_do_cmd(ptr->wcs_value);
195 else if (ptr->wcs_type == WCS_FUNCTION) {
196 (*ptr->wcs_function) ();
205 * Display a variable-substituted template
207 void do_template(void *templatename) {
208 char filename[PATH_MAX];
211 char outbuf[sizeof inbuf];
212 char key[sizeof inbuf];
215 strcpy(filename, "static/");
216 strcat(filename, templatename);
218 strcat(filename, ".wml");
220 strcat(filename, ".html");
222 fp = fopen(filename, "r");
224 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
225 wprintf("'%s' - %s<BR>\n",
226 templatename, strerror(errno));
232 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
235 while (strlen(inbuf) > 0) {
237 for (i=strlen(inbuf); i>=0; --i) {
238 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
241 wprintf("%s", inbuf);
245 strncpy(outbuf, inbuf, pos);
247 wprintf("%s", outbuf);
248 strcpy(inbuf, &inbuf[pos]);
250 for (i=strlen(inbuf); i>=0; --i) {
251 if (inbuf[i]=='>') pos = i;
253 strncpy(key, &inbuf[2], pos-2);
256 strcpy(inbuf, &inbuf[pos+1]);