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_local_substs(void) {
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);
41 * \brief Add a substitution variable (local to this session)
42 * \param keyname the replacementstring to substitute
43 * \param keytype the kind of the key
44 * \param format the format string ala printf
45 * \param ... the arguments to substitute in the formatstring
47 void svprintf(char *keyname, int keytype, const char *format,...)
51 struct wcsubst *ptr = NULL;
55 * First scan through to see if we're doing a replacement of
58 for (scan=WC->vars; scan!=NULL; scan=scan->next) {
59 if (!strcasecmp(scan->wcs_key, keyname)) {
65 /** Otherwise allocate a new one */
67 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
69 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
73 /** Format the string and save it */
75 va_start(arg_ptr, format);
76 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
79 ptr->wcs_type = keytype;
80 ptr->wcs_value = strdup(wbuf);
84 * \brief Add a substitution variable (local to this session) that does a callback
85 * \param keyname the keystring to substitute
86 * \param fcn_ptr the function callback to give the substitution string
88 void svcallback(char *keyname, void (*fcn_ptr)() )
92 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
94 ptr->wcs_type = WCS_FUNCTION;
95 strcpy(ptr->wcs_key, keyname);
96 ptr->wcs_function = fcn_ptr;
103 * \brief back end for print_value_of() ... does a server command
104 * \param servcmd server command to execute on the citadel server
106 void pvo_do_cmd(char *servcmd) {
110 serv_getln(buf, sizeof buf);
116 wprintf("%s\n", &buf[4]);
122 wprintf("%s\n", &buf[4]);
131 * \brief Print the value of a variable
132 * \param keyname get a key to print
134 void print_value_of(char *keyname) {
138 if (keyname[0] == '=') {
139 do_template(&keyname[1]);
142 if (!strcasecmp(keyname, "SERV_PID")) {
143 wprintf("%d", WC->ctdl_pid);
146 else if (!strcasecmp(keyname, "SERV_NODENAME")) {
147 escputs(serv_info.serv_nodename);
150 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
151 escputs(serv_info.serv_humannode);
154 else if (!strcasecmp(keyname, "SERV_FQDN")) {
155 escputs(serv_info.serv_fqdn);
158 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
159 escputs(serv_info.serv_software);
162 else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
164 serv_info.serv_rev_level / 100,
165 serv_info.serv_rev_level % 100
169 else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
170 escputs(serv_info.serv_bbs_city);
173 else if (!strcasecmp(keyname, "CURRENT_USER")) {
174 escputs(WC->wc_fullname);
177 else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
178 escputs(WC->wc_roomname);
181 /** Page-local variables */
182 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
183 if (!strcasecmp(ptr->wcs_key, keyname)) {
184 if (ptr->wcs_type == WCS_STRING) {
185 wprintf("%s", ptr->wcs_value);
187 else if (ptr->wcs_type == WCS_SERVCMD) {
188 pvo_do_cmd(ptr->wcs_value);
190 else if (ptr->wcs_type == WCS_FUNCTION) {
191 (*ptr->wcs_function) ();
197 extern char *static_dirs[PATH_MAX]; /**< Disk representation */
200 * \brief Display a variable-substituted template
201 * \param templatename template file to load
203 void do_template(void *templatename) {
204 char flat_filename[PATH_MAX];
205 char filename[PATH_MAX];
208 char outbuf[sizeof inbuf];
209 char key[sizeof inbuf];
213 strcpy(flat_filename, templatename);
215 strcat(flat_filename, ".wml");
217 strcat(flat_filename, ".html");
219 strcpy(filename, static_dirs[1]);
220 strcat(filename, flat_filename);
221 if (stat(filename, &mystat) == -1)
223 strcpy(filename, static_dirs[0]);
224 strcat(filename, flat_filename);
227 fp = fopen(filename, "r");
229 wprintf(_("ERROR: could not open template "));
230 wprintf("'%s' - %s<br />\n",
231 templatename, strerror(errno));
237 while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
244 for (i=len; i>=0; --i) {
245 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
248 wprintf("%s", inbuf);
253 strncpy(outbuf, inbuf, pos);
255 wprintf("%s", outbuf);
256 memmove(inbuf, &inbuf[pos], len - pos +1);
259 for (i=len; i>=0; --i) {
260 if (inbuf[i]=='>') pos = i;
262 strncpy(key, &inbuf[2], pos-2);
266 memmove(inbuf, &inbuf[pos], len - pos + 1);