]> code.citadel.org Git - citadel.git/blob - webcit/subst.c
* Brought over the newer string tokenizer from Citadel
[citadel.git] / webcit / subst.c
1 /*
2  * $Id$
3  *
4  * Variable substitution type stuff
5  *
6  */
7
8
9
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/socket.h>
19 #include <sys/time.h>
20 #include <sys/stat.h>
21 #include <limits.h>
22 #include <netinet/in.h>
23 #include <netdb.h>
24 #include <string.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include "webcit.h"
31
32 struct wcsubst *global_subst = NULL;
33
34
35 /*
36  * Clear out the list of substitution variables local to this session
37  */
38 void clear_local_substs(void) {
39         struct wcsubst *ptr;
40
41         while (WC->vars != NULL) {
42                 ptr = WC->vars->next;
43
44                 if ((WC->vars->wcs_type == WCS_STRING)
45                    || (WC->vars->wcs_type == WCS_SERVCMD)) {
46                         free(WC->vars->wcs_value);
47                 }
48
49                 free(WC->vars);
50                 WC->vars = ptr;
51         }
52 }
53
54
55 /*
56  * Add a substitution variable (local to this session)
57  */
58 void svprintf(char *keyname, int keytype, const char *format,...)
59 {
60         va_list arg_ptr;
61         char wbuf[1024];
62         struct wcsubst *ptr;
63
64         va_start(arg_ptr, format);
65         vsprintf(wbuf, format, arg_ptr);
66         va_end(arg_ptr);
67
68         ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
69         ptr->next = WC->vars;
70         ptr->wcs_type = keytype;
71         strcpy(ptr->wcs_key, keyname);
72         ptr->wcs_value = malloc(strlen(wbuf)+1);
73         strcpy(ptr->wcs_value, wbuf);
74         WC->vars = ptr;
75 }
76
77 /*
78  * Add a substitution variable (local to this session) that does a callback
79  */
80 void svcallback(char *keyname, void (*fcn_ptr)() )
81 {
82         struct wcsubst *ptr;
83
84         ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
85         ptr->next = WC->vars;
86         ptr->wcs_type = WCS_FUNCTION;
87         strcpy(ptr->wcs_key, keyname);
88         ptr->wcs_function = fcn_ptr;
89         WC->vars = ptr;
90 }
91
92
93
94 /*
95  * back end for print_value_of() ... does a server command
96  */
97 void pvo_do_cmd(char *servcmd) {
98         char buf[SIZ];
99
100         serv_puts(servcmd);
101         serv_gets(buf);
102
103         switch(buf[0]) {
104                 case '2':
105                 case '3':
106                 case '5':
107                         wprintf("%s\n", &buf[4]);
108                         break;
109                 case '1':
110                         fmout(NULL);
111                         break;
112                 case '4':
113                         wprintf("%s\n", &buf[4]);
114                         serv_puts("000");
115                         break;
116         }
117 }
118
119
120
121 /*
122  * Print the value of a variable
123  */
124 void print_value_of(char *keyname) {
125         struct wcsubst *ptr;
126         void *fcn();
127
128         for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
129                 if (!strcasecmp(ptr->wcs_key, keyname)) {
130                         if (ptr->wcs_type == WCS_STRING) {
131                                 wprintf("%s", ptr->wcs_value);
132                         }
133                         else if (ptr->wcs_type == WCS_SERVCMD) {
134                                 pvo_do_cmd(ptr->wcs_value);
135                         }
136                         else if (ptr->wcs_type == WCS_FUNCTION) {
137                                 (*ptr->wcs_function) ();
138                         }
139                 }
140         }
141 }
142
143
144
145 /*
146  * Display a variable-substituted template
147  */
148 void do_template(void *templatename) {
149         char filename[PATH_MAX];
150         FILE *fp;
151         char inbuf[1024];
152         char outbuf[sizeof inbuf];
153         char key[sizeof inbuf];
154         int i, pos;
155
156         strcpy(filename, "static/");
157         strcat(filename, templatename);
158         
159         fp = fopen(filename, "r");
160         if (fp == NULL) {
161                 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
162                 wprintf("'%s' - %s<BR>\n",
163                         templatename, strerror(errno));
164                 return;
165         }
166
167         strcpy(inbuf, "");
168
169         while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
170                 strcpy(outbuf, "");
171
172                 while (strlen(inbuf) > 0) {
173                         pos = (-1);
174                         for (i=strlen(inbuf); i>=0; --i) {
175                                 if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
176                         }
177                         if (pos < 0) {
178                                 wprintf("%s", inbuf);
179                                 strcpy(inbuf, "");
180                         }
181                         else {
182                                 strncpy(outbuf, inbuf, pos);
183                                 outbuf[pos] = 0;
184                                 wprintf("%s", outbuf);
185                                 strcpy(inbuf, &inbuf[pos]);
186                                 pos = 1;
187                                 for (i=strlen(inbuf); i>=0; --i) {
188                                         if (inbuf[i]=='>') pos = i;
189                                 }
190                                 strncpy(key, &inbuf[2], pos-2);
191                                 key[pos-2] = 0;
192                                 print_value_of(key);
193                                 strcpy(inbuf, &inbuf[pos+1]);
194                         }
195                 }
196         }
197
198         fclose(fp);
199 }