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 wprintf("%s", serv_info.serv_nodename);
155 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
156 wprintf("%s", serv_info.serv_humannode);
159 else if (!strcasecmp(keyname, "SERV_FQDN")) {
160 wprintf("%s", serv_info.serv_fqdn);
163 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
164 wprintf("%s", 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 wprintf("%s", serv_info.serv_bbs_city);
178 /* Page-local variables */
179 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
180 if (!strcasecmp(ptr->wcs_key, keyname)) {
181 if (ptr->wcs_type == WCS_STRING) {
182 wprintf("%s", ptr->wcs_value);
184 else if (ptr->wcs_type == WCS_SERVCMD) {
185 pvo_do_cmd(ptr->wcs_value);
187 else if (ptr->wcs_type == WCS_FUNCTION) {
188 (*ptr->wcs_function) ();
197 * Display a variable-substituted template
199 void do_template(void *templatename) {
200 char filename[PATH_MAX];
203 char outbuf[sizeof inbuf];
204 char key[sizeof inbuf];
207 strcpy(filename, "static/");
208 strcat(filename, templatename);
210 strcat(filename, ".wml");
212 strcat(filename, ".html");
214 fp = fopen(filename, "r");
216 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
217 wprintf("'%s' - %s<BR>\n",
218 templatename, strerror(errno));
224 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
227 while (strlen(inbuf) > 0) {
229 for (i=strlen(inbuf); i>=0; --i) {
230 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
233 wprintf("%s", inbuf);
237 strncpy(outbuf, inbuf, pos);
239 wprintf("%s", outbuf);
240 strcpy(inbuf, &inbuf[pos]);
242 for (i=strlen(inbuf); i>=0; --i) {
243 if (inbuf[i]=='>') pos = i;
245 strncpy(key, &inbuf[2], pos-2);
248 strcpy(inbuf, &inbuf[pos+1]);