5 * \defgroup Subst Variable substitution type stuff
6 * \ingroup CitadelConfig
12 #include <sys/types.h>
19 * \brief Clear out the list of substitution variables local to this session
21 void clear_substs(struct wcsession *wc) {
24 while (wc->vars != NULL) {
27 if ((wc->vars->wcs_type == WCS_STRING)
28 || (wc->vars->wcs_type == WCS_SERVCMD)) {
29 free(wc->vars->wcs_value);
40 * \brief Clear out the list of substitution variables local to this session
42 void clear_local_substs(void) {
48 * \brief Add a substitution variable (local to this session)
49 * \param keyname the replacementstring to substitute
50 * \param keytype the kind of the key
51 * \param format the format string ala printf
52 * \param ... the arguments to substitute in the formatstring
54 void svprintf(char *keyname, int keytype, const char *format,...)
58 struct wcsubst *ptr = NULL;
62 * First scan through to see if we're doing a replacement of
65 for (scan=WC->vars; scan!=NULL; scan=scan->next) {
66 if (!strcasecmp(scan->wcs_key, keyname)) {
68 if (ptr->wcs_value != NULL)
73 /** Otherwise allocate a new one */
75 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
76 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
81 /** Format the string and save it */
83 va_start(arg_ptr, format);
84 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
87 ptr->wcs_function = NULL;
88 ptr->wcs_type = keytype;
89 ptr->wcs_value = strdup(wbuf);
93 * \brief Add a substitution variable (local to this session) that does a callback
94 * \param keyname the keystring to substitute
95 * \param fcn_ptr the function callback to give the substitution string
97 void svcallback(char *keyname, void (*fcn_ptr)() )
103 * First scan through to see if we're doing a replacement of
107 for (scan=WC->vars; scan!=NULL; scan=scan->next) {
108 if (!strcasecmp(scan->wcs_key, keyname)) {
110 if (ptr->wcs_value != NULL)
111 free(ptr->wcs_value);
115 /** Otherwise allocate a new one */
117 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
118 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
119 ptr->next = WC->vars;
122 ptr->wcs_value = NULL;
123 ptr->wcs_type = WCS_FUNCTION;
124 ptr->wcs_function = fcn_ptr;
130 * \brief back end for print_value_of() ... does a server command
131 * \param servcmd server command to execute on the citadel server
133 void pvo_do_cmd(char *servcmd) {
137 serv_getln(buf, sizeof buf);
143 wprintf("%s\n", &buf[4]);
149 wprintf("%s\n", &buf[4]);
158 * \brief Print the value of a variable
159 * \param keyname get a key to print
161 void print_value_of(char *keyname) {
165 if (keyname[0] == '=') {
166 do_template(&keyname[1]);
169 if (!strcasecmp(keyname, "SERV_PID")) {
170 wprintf("%d", WC->ctdl_pid);
173 else if (!strcasecmp(keyname, "SERV_NODENAME")) {
174 escputs(serv_info.serv_nodename);
177 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
178 escputs(serv_info.serv_humannode);
181 else if (!strcasecmp(keyname, "SERV_FQDN")) {
182 escputs(serv_info.serv_fqdn);
185 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
186 escputs(serv_info.serv_software);
189 else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
191 serv_info.serv_rev_level / 100,
192 serv_info.serv_rev_level % 100
196 else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
197 escputs(serv_info.serv_bbs_city);
200 else if (!strcasecmp(keyname, "CURRENT_USER")) {
201 escputs(WC->wc_fullname);
204 else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
205 escputs(WC->wc_roomname);
208 /** Page-local variables */
209 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
210 if (!strcasecmp(ptr->wcs_key, keyname)) {
211 if (ptr->wcs_type == WCS_STRING) {
212 wprintf("%s", ptr->wcs_value);
214 else if (ptr->wcs_type == WCS_SERVCMD) {
215 pvo_do_cmd(ptr->wcs_value);
217 else if (ptr->wcs_type == WCS_FUNCTION) {
218 (*ptr->wcs_function) ();
224 extern char *static_dirs[PATH_MAX]; /**< Disk representation */
227 * \brief Display a variable-substituted template
228 * \param templatename template file to load
230 void do_template(void *templatename) {
231 char flat_filename[PATH_MAX];
232 char filename[PATH_MAX];
235 char outbuf[sizeof inbuf];
236 char key[sizeof inbuf];
240 strcpy(flat_filename, templatename);
242 strcat(flat_filename, ".wml");
244 strcat(flat_filename, ".html");
246 strcpy(filename, static_dirs[1]);
247 strcat(filename, flat_filename);
248 if (stat(filename, &mystat) == -1)
250 strcpy(filename, static_dirs[0]);
251 strcat(filename, flat_filename);
254 fp = fopen(filename, "r");
256 wprintf(_("ERROR: could not open template "));
257 wprintf("'%s' - %s<br />\n",
258 templatename, strerror(errno));
264 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
271 for (i=len; i>=0; --i) {
272 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
275 wprintf("%s", inbuf);
280 strncpy(outbuf, inbuf, pos);
282 wprintf("%s", outbuf);
283 memmove(inbuf, &inbuf[pos], len - pos +1);
286 for (i=len; i>=0; --i) {
287 if (inbuf[i]=='>') pos = i;
289 strncpy(key, &inbuf[2], pos-2);
293 memmove(inbuf, &inbuf[pos], len - pos + 1);