Implemented a new command, egw_clear, to clear http output cache.
[egate.git] / www / wshmain.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  * $Log: wshmain.c,v $
44  * Revision 1.6  1997/01/27 11:27:16  adam
45  * Implemented a new command, egw_clear, to clear http output cache.
46  * Changed prototype for function wo_clear.
47  *
48  * Revision 1.5  1997/01/24 13:13:12  adam
49  * Implemnted egw_source and added a "raw" option to the URL.
50  * Fixed a bug in the buffering system of wproto; the macro wo_putc could
51  * override memory if it was the first HTML generating function called.
52  *
53  * Revision 1.4  1996/05/23 15:53:12  adam
54  * Bug fix: egw_enc failed on 8-bit chars.
55  * New command: egw_parms.
56  *
57  * Revision 1.3  1996/03/14  11:48:39  adam
58  * New function egw_prog that returns name of shell.
59  *
60  * Revision 1.2  1996/03/08  16:47:06  adam
61  * Logs of changes.
62  *
63  * Revision 1.1  1996/02/29  15:36:34  adam
64  * Main shell loop implemented as a separate function called wshmain.
65  *
66  */
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <assert.h>
72 #include <unistd.h>
73 #include <ctype.h>
74
75 #include <sys/resource.h>
76 #include <sys/time.h>
77
78 #include <gw-db.h>
79 #include <gw-res.h>
80 #include "winterp.h"
81
82 WCLIENT wcl;
83 static char *mod = "w";
84
85 void wshmain (int argc, char **argv, W_Interp_Type w_interp_type)
86 {
87     char *script, *parms, parms_buf[512];
88     int timeout;
89     W_Interp w_interp;
90     GW_DB gw_db;
91     GwRes shRes;
92 #if 1
93     struct rlimit rlim;
94
95     rlim.rlim_cur = RLIM_INFINITY;
96     rlim.rlim_max = RLIM_INFINITY;
97     setrlimit (RLIMIT_CORE, &rlim);
98 #endif
99
100     gw_log_init (*argv);
101     gw_log_file (GW_LOG_ALL, "egwsh_log");
102 #if 0
103     gw_log_level (GW_LOG_ALL);
104 #endif
105     gw_log (GW_LOG_STAT, *argv, "Europagate www shell");
106     shRes = gw_res_init ();
107     gw_res_merge (shRes, "egw.res");
108     gw_res_merge (shRes, "shell.res");
109
110 #if 1
111     gw_log_level (gw_log_mask_str (
112                   gw_res_get (shRes, "log.level", "default")));
113 #endif 
114     if (!(wcl = wproto_init (gw_res_get (shRes, "fifo.dir", "/tmp/egw"),
115                              *argv)))
116     {
117         gw_log (GW_LOG_FATAL, mod, "init");
118         exit(1);
119     }
120     gw_log_session (wcl->id);
121
122     w_interp = w_interp_create (w_interp_type, wcl, NULL);
123
124     w_interp_load_state (w_interp, NULL);
125     timeout = atoi (gw_res_get (shRes, "timeout", "120"));
126     if (timeout < 1)
127         timeout = 1;
128     while (1)
129     {
130         char *p;
131         int r;
132         int raw_mode = 0;
133
134         r = wproto_process (wcl, timeout);
135         if (r <= 0)
136         {
137             gw_db = gw_db_open ("www.db", 0, 0);
138             if (gw_db)
139                 break;
140             gw_log (GW_LOG_DEBUG, mod, "Cannot terminate - new request");
141             timeout = 10;
142             continue;
143         }
144         timeout = atoi (gw_res_get (shRes, "timeout", "120"));
145         if (timeout < 1)
146             timeout = 1;
147         wo_clear (wcl);
148         strcpy (parms_buf, wcl->wf_parms);
149         parms = parms_buf;
150
151         script = wcl->wf_script;
152
153         if (*script)
154         {
155 #if 0
156             p = script + strlen(script);
157             while (*--p && p != script)
158                 switch (*p)
159                 {
160                 case '+':
161                     wproto_cache (wcl, wcl->cache_level+1);
162                     break;
163                 case '-':
164                     if (p[1])
165                         wproto_cache (wcl, wcl->cache_level-atoi(p+1));
166                     else
167                         wproto_cache (wcl, wcl->cache_level-1);
168                     *p = '\0';
169                     break;
170                 case '=':
171                     if (isdigit(p[1]))
172                         wproto_cache (wcl, atoi(p+1));
173                     *p = '\0';
174                     break;
175                 case ';':
176                     wo_printf (wcl, "Expires: %s\n", p[1] ? p+1 : "0");
177                     gw_log (GW_LOG_DEBUG, mod, "Expires: %s", p[1] ?p+1:"0");
178                     *p = '\0';
179                     break;
180                 }
181 #else
182             p = script;
183             while (*p)
184             {
185                 if (*p == ';') /* ";" or ";<date>" */
186                 {
187                     wo_printf (wcl, "Expires: %s\n", p[1] ? p+1 : "0");
188                     gw_log (GW_LOG_DEBUG, mod, "Expires: %s", p[1] ?p+1:"0");
189                     *p = '\0';
190                     break;
191                 }
192                 else if (*p == '+') /* "+<name>=<value>" or "+<name>" */
193                 {
194                     const char *name1 = NULL;
195                     const char *name2 = NULL;
196                     const char *value1 = NULL;
197                     const char *value2 = NULL;
198                     int len;
199                     *p = '\0';
200                     name1 = ++p;
201                     while (*p && *p != ';' && *p != '+')
202                     {
203                         if (*p == '=')
204                         {
205                             name2 = p++;
206                             value1 = p;
207                             while (*p && *p != ';' && *p != '+')
208                                 p++;
209                             value2 = p;
210                             break;
211                         }
212                         p++;
213                     }
214                     if (!name2)
215                         name2 = p;
216                     len = name2 - name1;
217                     if (len == 3 && !memcmp (name1, "raw", len))
218                         raw_mode = 1;
219                 }
220                 else
221                     p++;
222             }
223             *p = '\0';
224 #endif
225         }
226         if (!raw_mode)
227             wo_printf (wcl, "Content-type: text/html\n\n");
228         for (p = parms; *p; p++)
229             if (*p == '+')
230                 *p = ' ';
231         gw_log (GW_LOG_STAT, mod, "script: %s", script);
232         gw_log (GW_LOG_STAT, mod, "parms: %s", parms);
233         if (w_interp_exec (w_interp, script, parms))
234         {
235             if (raw_mode)
236                 wo_printf (wcl, "Content-type: text/html\n\n");
237             wo_printf (wcl, "<html><head><title>wsh error</title></head>\n");
238             wo_printf (wcl, "<body>Couldn't execute script %s</body></html>",
239                        script);
240         }
241         wo_finish(wcl);
242     }
243     gw_log (GW_LOG_DEBUG, mod, "save state..");
244     w_interp_save_state (w_interp, NULL);
245     wproto_terminate(wcl);
246 }
247
248