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]);
128 wprintf("%s\n", &buf[4]);
137 * Print the value of a variable
139 void print_value_of(char *keyname) {
143 if (!strcasecmp(keyname, "SERV_PID")) {
144 wprintf("%d", serv_info.serv_pid);
147 else if (!strcasecmp(keyname, "SERV_NODENAME")) {
148 wprintf("%s", serv_info.serv_nodename);
151 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
152 wprintf("%s", serv_info.serv_humannode);
155 else if (!strcasecmp(keyname, "SERV_FQDN")) {
156 wprintf("%s", serv_info.serv_fqdn);
159 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
160 wprintf("%s", serv_info.serv_software);
163 else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
165 serv_info.serv_rev_level / 100,
166 serv_info.serv_rev_level % 100
170 else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
171 wprintf("%s", serv_info.serv_bbs_city);
174 /* Page-local variables */
175 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
176 if (!strcasecmp(ptr->wcs_key, keyname)) {
177 if (ptr->wcs_type == WCS_STRING) {
178 wprintf("%s", ptr->wcs_value);
180 else if (ptr->wcs_type == WCS_SERVCMD) {
181 pvo_do_cmd(ptr->wcs_value);
183 else if (ptr->wcs_type == WCS_FUNCTION) {
184 (*ptr->wcs_function) ();
193 * Display a variable-substituted template
195 void do_template(void *templatename) {
196 char filename[PATH_MAX];
199 char outbuf[sizeof inbuf];
200 char key[sizeof inbuf];
203 strcpy(filename, "static/");
204 strcat(filename, templatename);
206 strcat(filename, ".wml");
208 strcat(filename, ".html");
210 fp = fopen(filename, "r");
212 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
213 wprintf("'%s' - %s<BR>\n",
214 templatename, strerror(errno));
220 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
223 while (strlen(inbuf) > 0) {
225 for (i=strlen(inbuf); i>=0; --i) {
226 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
229 wprintf("%s", inbuf);
233 strncpy(outbuf, inbuf, pos);
235 wprintf("%s", outbuf);
236 strcpy(inbuf, &inbuf[pos]);
238 for (i=strlen(inbuf); i>=0; --i) {
239 if (inbuf[i]=='>') pos = i;
241 strncpy(key, &inbuf[2], pos-2);
244 strcpy(inbuf, &inbuf[pos+1]);