4 * Variable substitution type stuff
16 #include <sys/types.h>
18 #include <sys/socket.h>
22 #include <netinet/in.h>
34 * Clear out the list of substitution variables local to this session
36 void clear_local_substs(void) {
39 while (WC->vars != NULL) {
42 if ((WC->vars->wcs_type == WCS_STRING)
43 || (WC->vars->wcs_type == WCS_SERVCMD)) {
44 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 /* First scan through to see if we're doing a replacement of
68 for (scan=WC->vars; scan!=NULL; scan=scan->next) {
69 if (!strcasecmp(scan->wcs_key, keyname)) {
75 /* Otherwise allocate a new one */
77 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
79 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
83 /* Format the string and save it */
85 va_start(arg_ptr, format);
86 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
89 ptr->wcs_type = keytype;
90 ptr->wcs_value = strdup(wbuf);
94 * Add a substitution variable (local to this session) that does a callback
96 void svcallback(char *keyname, void (*fcn_ptr)() )
100 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
101 ptr->next = WC->vars;
102 ptr->wcs_type = WCS_FUNCTION;
103 strcpy(ptr->wcs_key, keyname);
104 ptr->wcs_function = fcn_ptr;
111 * back end for print_value_of() ... does a server command
113 void pvo_do_cmd(char *servcmd) {
117 serv_getln(buf, sizeof buf);
123 wprintf("%s\n", &buf[4]);
126 fmout(NULL, "CENTER");
129 wprintf("%s\n", &buf[4]);
138 * Print the value of a variable
140 void print_value_of(char *keyname) {
144 if (keyname[0] == '=') {
145 do_template(&keyname[1]);
148 if (!strcasecmp(keyname, "SERV_PID")) {
149 wprintf("%d", WC->ctdl_pid);
152 else if (!strcasecmp(keyname, "SERV_NODENAME")) {
153 escputs(serv_info.serv_nodename);
156 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
157 escputs(serv_info.serv_humannode);
160 else if (!strcasecmp(keyname, "SERV_FQDN")) {
161 escputs(serv_info.serv_fqdn);
164 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
165 escputs(serv_info.serv_software);
168 else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
170 serv_info.serv_rev_level / 100,
171 serv_info.serv_rev_level % 100
175 else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
176 escputs(serv_info.serv_bbs_city);
179 else if (!strcasecmp(keyname, "CURRENT_USER")) {
180 escputs(WC->wc_username);
183 else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
184 escputs(WC->wc_roomname);
187 /* Page-local variables */
188 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
189 if (!strcasecmp(ptr->wcs_key, keyname)) {
190 if (ptr->wcs_type == WCS_STRING) {
191 wprintf("%s", ptr->wcs_value);
193 else if (ptr->wcs_type == WCS_SERVCMD) {
194 pvo_do_cmd(ptr->wcs_value);
196 else if (ptr->wcs_type == WCS_FUNCTION) {
197 (*ptr->wcs_function) ();
206 * Display a variable-substituted template
208 void do_template(void *templatename) {
209 char filename[PATH_MAX];
212 char outbuf[sizeof inbuf];
213 char key[sizeof inbuf];
216 strcpy(filename, "static/");
217 strcat(filename, templatename);
219 strcat(filename, ".wml");
221 strcat(filename, ".html");
223 fp = fopen(filename, "r");
225 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
226 wprintf("'%s' - %s<br />\n",
227 templatename, strerror(errno));
233 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
236 while (strlen(inbuf) > 0) {
238 for (i=strlen(inbuf); i>=0; --i) {
239 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
242 wprintf("%s", inbuf);
246 strncpy(outbuf, inbuf, pos);
248 wprintf("%s", outbuf);
249 strcpy(inbuf, &inbuf[pos]);
251 for (i=strlen(inbuf); i>=0; --i) {
252 if (inbuf[i]=='>') pos = i;
254 strncpy(key, &inbuf[2], pos-2);
257 strcpy(inbuf, &inbuf[pos+1]);