5 * \defgroup Subst Variable substitution type stuff
6 * \ingroup CitadelConfig
15 * \brief Clear out the list of substitution variables local to this session
17 void clear_local_substs(void) {
20 while (WC->vars != NULL) {
23 if ((WC->vars->wcs_type == WCS_STRING)
24 || (WC->vars->wcs_type == WCS_SERVCMD)) {
25 free(WC->vars->wcs_value);
37 * \brief Add a substitution variable (local to this session)
38 * \param keyname the replacementstring to substitute
39 * \param keytype the kind of the key
40 * \param format the format string ala printf
41 * \param ... the arguments to substitute in the formatstring
43 void svprintf(char *keyname, int keytype, const char *format,...)
47 struct wcsubst *ptr = NULL;
51 * First scan through to see if we're doing a replacement of
54 for (scan=WC->vars; scan!=NULL; scan=scan->next) {
55 if (!strcasecmp(scan->wcs_key, keyname)) {
61 /** Otherwise allocate a new one */
63 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
65 safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
69 /** Format the string and save it */
71 va_start(arg_ptr, format);
72 vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
75 ptr->wcs_type = keytype;
76 ptr->wcs_value = strdup(wbuf);
80 * \brief Add a substitution variable (local to this session) that does a callback
81 * \param keyname the keystring to substitute
82 * \param fcn_ptr the function callback to give the substitution string
84 void svcallback(char *keyname, void (*fcn_ptr)() )
88 ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
90 ptr->wcs_type = WCS_FUNCTION;
91 strcpy(ptr->wcs_key, keyname);
92 ptr->wcs_function = fcn_ptr;
99 * \brief back end for print_value_of() ... does a server command
100 * \param servcmd server command to execute on the citadel server
102 void pvo_do_cmd(char *servcmd) {
106 serv_getln(buf, sizeof buf);
112 wprintf("%s\n", &buf[4]);
118 wprintf("%s\n", &buf[4]);
127 * \brief Print the value of a variable
128 * \param keyname get a key to print
130 void print_value_of(char *keyname) {
134 if (keyname[0] == '=') {
135 do_template(&keyname[1]);
138 if (!strcasecmp(keyname, "SERV_PID")) {
139 wprintf("%d", WC->ctdl_pid);
142 else if (!strcasecmp(keyname, "SERV_NODENAME")) {
143 escputs(serv_info.serv_nodename);
146 else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
147 escputs(serv_info.serv_humannode);
150 else if (!strcasecmp(keyname, "SERV_FQDN")) {
151 escputs(serv_info.serv_fqdn);
154 else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
155 escputs(serv_info.serv_software);
158 else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
160 serv_info.serv_rev_level / 100,
161 serv_info.serv_rev_level % 100
165 else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
166 escputs(serv_info.serv_bbs_city);
169 else if (!strcasecmp(keyname, "CURRENT_USER")) {
170 escputs(WC->wc_fullname);
173 else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
174 escputs(WC->wc_roomname);
177 /** Page-local variables */
178 else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
179 if (!strcasecmp(ptr->wcs_key, keyname)) {
180 if (ptr->wcs_type == WCS_STRING) {
181 wprintf("%s", ptr->wcs_value);
183 else if (ptr->wcs_type == WCS_SERVCMD) {
184 pvo_do_cmd(ptr->wcs_value);
186 else if (ptr->wcs_type == WCS_FUNCTION) {
187 (*ptr->wcs_function) ();
196 * \brief Display a variable-substituted template
197 * \param templatename template file to load
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(_("ERROR: 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]);