]> code.citadel.org Git - citadel.git/blob - webcit/subst.c
* Completed the initial hack of the variable substitution template thingy.
[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
79 /*
80  * back end for print_value_of() ... does a server command
81  */
82 void pvo_do_cmd(char *servcmd) {
83         char buf[256];
84
85         serv_puts(servcmd);
86         serv_gets(buf);
87
88         switch(buf[0]) {
89                 case '2':
90                 case '3':
91                 case '5':
92                         wprintf("%s\n", &buf[4]);
93                         break;
94                 case '1':
95                         fmout(NULL);
96                         break;
97                 case '4':
98                         wprintf("%s\n", &buf[4]);
99                         serv_puts("000");
100                         break;
101         }
102 }
103
104
105
106 /*
107  * Print the value of a variable
108  */
109 void print_value_of(char *keyname) {
110         struct wcsubst *ptr;
111
112         for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
113                 if (!strcasecmp(ptr->wcs_key, keyname)) {
114                         if (ptr->wcs_type == WCS_STRING) {
115                                 wprintf("%s", ptr->wcs_value);
116                         }
117                         else if (ptr->wcs_type == WCS_SERVCMD) {
118                                 pvo_do_cmd(ptr->wcs_value);
119                         }
120                 }
121         }
122 }
123
124
125
126 /*
127  * Display a variable-substituted template
128  */
129 void do_template(void *templatename) {
130         char filename[PATH_MAX];
131         FILE *fp;
132         char inbuf[1024];
133         char outbuf[sizeof inbuf];
134         char key[sizeof inbuf];
135         int i, j, pos;
136         int olen;
137
138         strcpy(filename, "static/");
139         strcat(filename, templatename);
140         
141         fp = fopen(filename, "r");
142         if (fp == NULL) {
143                 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
144                 wprintf("'%s' - %s<BR>\n",
145                         templatename, strerror(errno));
146                 return;
147         }
148
149         strcpy(inbuf, "");
150
151         while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
152                 strcpy(outbuf, "");
153                 olen = 0;
154
155                 for (i=0; i<strlen(inbuf); ++i) {
156                         if (strncmp(&inbuf[i], "<?", 2)) {
157                                 outbuf[olen] = inbuf[i];
158                                 outbuf[++olen] = 0;
159                         }
160                         else {
161                                 pos = (-1);
162                                 for (j=strlen(inbuf); j>=i;  --j)  {
163                                         if (inbuf[j]=='>') pos = j;
164                                 }
165                                 if (pos > 0) {
166                                         wprintf("%s", outbuf);
167                                         strcpy(outbuf, "");
168                                         olen = 0;
169                                         strncpy(key, &inbuf[i+2], pos-i-2);
170                                         print_value_of(key);
171                                         i = pos;
172                                 }
173                                 else {
174                                         i = i + 2;
175                                 }
176                         }
177                 }
178                 wprintf("%s", outbuf);
179         }
180
181         fclose(fp);
182 }