]> code.citadel.org Git - citadel.git/blob - webcit/subst.c
* Started working on a variable-substitution thing
[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                         free(WC->vars->wcs_value);
46                 }
47
48                 free(WC->vars);
49                 WC->vars = ptr;
50         }
51 }
52
53
54 /*
55  * Add a substitution variable (local to this session)
56  */
57 void svprintf(char *keyname, const char *format,...)
58 {
59         va_list arg_ptr;
60         char wbuf[1024];
61         struct wcsubst *ptr;
62
63         va_start(arg_ptr, format);
64         vsprintf(wbuf, format, arg_ptr);
65         va_end(arg_ptr);
66
67         ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
68         ptr->next = WC->vars;
69         ptr->wcs_type = WCS_STRING;
70         strcpy(ptr->wcs_key, keyname);
71         ptr->wcs_value = malloc(strlen(wbuf)+1);
72         strcpy(ptr->wcs_value, wbuf);
73         WC->vars = ptr;
74 }
75
76
77
78 /*
79  * Print the value of a variable
80  */
81 void print_value_of(char *keyname) {
82         struct wbsubst *ptr;
83
84         for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
85                 if (!strcasecmp(ptr->wcs_key, keyname)) {
86                         if (ptr->wcs_type == WCS_STRING) {
87                                 wprintf("%s", ptr->wcs_value);
88                         }
89                 }
90         }
91 }
92
93
94
95 /*
96  * Display a variable-substituted template
97  */
98 void do_template(void *templatename) {
99         char filename[PATH_MAX];
100         FILE *fp;
101         char inbuf[1024];
102         char outbuf[sizeof inbuf];
103         char key[sizeof inbuf];
104         int i, j, pos;
105         int olen;
106
107         strcpy(filename, "static/");
108         strcat(filename, templatename);
109         
110         fp = fopen(filename, "r");
111         if (fp == NULL) {
112                 wprintf("<BLINK>ERROR</BLINK> - could not open template ");
113                 wprintf("'%s' - %s<BR>\n",
114                         templatename, strerror(errno));
115                 return;
116         }
117
118         strcpy(inbuf, "");
119
120         while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
121                 strcpy(outbuf, "");
122                 olen = 0;
123
124                 for (i=0; i<strlen(inbuf); ++i) {
125                         if (strncmp(&inbuf[i], "<?", 2)) {
126                                 outbuf[olen] = inbuf[i];
127                                 outbuf[++olen] = 0;
128                         }
129                         else {
130                                 pos = (-1);
131                                 for (j=strlen(inbuf); j>=i;  --j)  {
132                                         if (inbuf[j]=='>') pos = j;
133                                 }
134                                 if (pos > 0) {
135                                         strncpy(key, &inbuf[i+2], pos-i-2);
136                                         print_value_of(key);
137                                         olen = strlen(outbuf);
138                                         i = pos;
139                                 }
140                                 else {
141                                         i = i + 2;
142                                 }
143                         }
144                 }
145                 wprintf("%s", outbuf);
146         }
147
148         fclose(fp);
149 }