4 * Variable substitution type stuff
14 * Clear out the list of substitution variables local to this session
16 void clear_local_substs(void) {
19 while (WC->vars != NULL) {
22 if ((WC->vars->wcs_type == WCS_STRING)
23 || (WC->vars->wcs_type == WCS_SERVCMD)) {
24 free(WC->vars->wcs_value);
36 * Add a substitution variable (local to this session)
38 void svprintf(char *keyname, int keytype, const char *format,...)
42 struct wcsubst *ptr = NULL;
45 /* First scan through to see if we're doing a replacement of
48 for (scan=WC->vars; scan!=NULL; scan=scan->next) {
49 if (!strcasecmp(scan->wcs_key, keyname)) {
55 /* Otherwise allocate a new one */
57 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
59 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
63 /* Format the string and save it */
65 va_start(arg_ptr, format);
66 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
69 ptr->wcs_type = keytype;
70 ptr->wcs_value = strdup(wbuf);
74 * Add a substitution variable (local to this session) that does a callback
76 void svcallback(char *keyname, void (*fcn_ptr)() )
80 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
82 ptr->wcs_type = WCS_FUNCTION;
83 strcpy(ptr->wcs_key, keyname);
84 ptr->wcs_function = fcn_ptr;
91 * back end for print_value_of() ... does a server command
93 void pvo_do_cmd(char *servcmd) {
97 serv_getln(buf, sizeof buf);
103 wprintf("%s\n", &buf[4]);
109 wprintf("%s\n", &buf[4]);
118 * Print the value of a variable
120 void print_value_of(char *keyname) {
124 if (keyname[0] == '=') {
125 do_template(&keyname[1]);
128 if (!strcasecmp(keyname, "SERV_PID")) {
129 wprintf("%d", WC->ctdl_pid);
132 else if (!strcasecmp(keyname, "SERV_NODENAME")) {
133 escputs(serv_info.serv_nodename);
136 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
137 escputs(serv_info.serv_humannode);
140 else if (!strcasecmp(keyname, "SERV_FQDN")) {
141 escputs(serv_info.serv_fqdn);
144 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
145 escputs(serv_info.serv_software);
148 else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
150 serv_info.serv_rev_level / 100,
151 serv_info.serv_rev_level % 100
155 else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
156 escputs(serv_info.serv_bbs_city);
159 else if (!strcasecmp(keyname, "CURRENT_USER")) {
160 escputs(WC->wc_username);
163 else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
164 escputs(WC->wc_roomname);
167 /* Page-local variables */
168 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
169 if (!strcasecmp(ptr->wcs_key, keyname)) {
170 if (ptr->wcs_type == WCS_STRING) {
171 wprintf("%s", ptr->wcs_value);
173 else if (ptr->wcs_type == WCS_SERVCMD) {
174 pvo_do_cmd(ptr->wcs_value);
176 else if (ptr->wcs_type == WCS_FUNCTION) {
177 (*ptr->wcs_function) ();
186 * Display a variable-substituted template
188 void do_template(void *templatename) {
189 char filename[PATH_MAX];
192 char outbuf[sizeof inbuf];
193 char key[sizeof inbuf];
196 strcpy(filename, "static/");
197 strcat(filename, templatename);
199 strcat(filename, ".wml");
201 strcat(filename, ".html");
203 fp = fopen(filename, "r");
205 wprintf(_("ERROR: could not open template "));
206 wprintf("'%s' - %s<br />\n",
207 templatename, strerror(errno));
213 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
216 while (strlen(inbuf) > 0) {
218 for (i=strlen(inbuf); i>=0; --i) {
219 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
222 wprintf("%s", inbuf);
226 strncpy(outbuf, inbuf, pos);
228 wprintf("%s", outbuf);
229 strcpy(inbuf, &inbuf[pos]);
231 for (i=strlen(inbuf); i>=0; --i) {
232 if (inbuf[i]=='>') pos = i;
234 strncpy(key, &inbuf[2], pos-2);
237 strcpy(inbuf, &inbuf[pos+1]);