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.5  1997/01/24 13:13:12  adam
45  * Implemnted egw_source and added a "raw" option to the URL.
46  * Fixed a bug in the buffering system of wproto; the macro wo_putc could
47  * override memory if it was the first HTML generating function called.
48  *
49  * Revision 1.4  1996/05/23 15:53:12  adam
50  * Bug fix: egw_enc failed on 8-bit chars.
51  * New command: egw_parms.
52  *
53  * Revision 1.3  1996/03/14  11:48:39  adam
54  * New function egw_prog that returns name of shell.
55  *
56  * Revision 1.2  1996/03/08  16:47:06  adam
57  * Logs of changes.
58  *
59  * Revision 1.1  1996/02/29  15:36:34  adam
60  * Main shell loop implemented as a separate function called wshmain.
61  *
62  */
63
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <assert.h>
68 #include <unistd.h>
69 #include <ctype.h>
70
71 #include <sys/resource.h>
72 #include <sys/time.h>
73
74 #include <gw-db.h>
75 #include <gw-res.h>
76 #include "winterp.h"
77
78 WCLIENT wcl;
79 static char *mod = "w";
80
81 void wshmain (int argc, char **argv, W_Interp_Type w_interp_type)
82 {
83     char *script, *parms, parms_buf[512];
84     int timeout;
85     W_Interp w_interp;
86     GW_DB gw_db;
87     GwRes shRes;
88 #if 1
89     struct rlimit rlim;
90
91     rlim.rlim_cur = RLIM_INFINITY;
92     rlim.rlim_max = RLIM_INFINITY;
93     setrlimit (RLIMIT_CORE, &rlim);
94 #endif
95
96     gw_log_init (*argv);
97     gw_log_file (GW_LOG_ALL, "egwsh_log");
98 #if 0
99     gw_log_level (GW_LOG_ALL);
100 #endif
101     gw_log (GW_LOG_STAT, *argv, "Europagate www shell");
102     shRes = gw_res_init ();
103     gw_res_merge (shRes, "egw.res");
104     gw_res_merge (shRes, "shell.res");
105
106 #if 1
107     gw_log_level (gw_log_mask_str (
108                   gw_res_get (shRes, "log.level", "default")));
109 #endif 
110     if (!(wcl = wproto_init (gw_res_get (shRes, "fifo.dir", "/tmp/egw"),
111                              *argv)))
112     {
113         gw_log (GW_LOG_FATAL, mod, "init");
114         exit(1);
115     }
116     gw_log_session (wcl->id);
117
118     w_interp = w_interp_create (w_interp_type, wcl, NULL);
119
120     w_interp_load_state (w_interp, NULL);
121     timeout = atoi (gw_res_get (shRes, "timeout", "120"));
122     if (timeout < 1)
123         timeout = 1;
124     while (1)
125     {
126         char *p;
127         int r;
128         int raw_mode = 0;
129
130         r = wproto_process (wcl, timeout);
131         if (r <= 0)
132         {
133             gw_db = gw_db_open ("www.db", 0, 0);
134             if (gw_db)
135                 break;
136             gw_log (GW_LOG_DEBUG, mod, "Cannot terminate - new request");
137             timeout = 10;
138             continue;
139         }
140         timeout = atoi (gw_res_get (shRes, "timeout", "120"));
141         if (timeout < 1)
142             timeout = 1;
143         wo_clear (wcl, NULL);
144         strcpy (parms_buf, wcl->wf_parms);
145         parms = parms_buf;
146
147         script = wcl->wf_script;
148
149         if (*script)
150         {
151 #if 0
152             p = script + strlen(script);
153             while (*--p && p != script)
154                 switch (*p)
155                 {
156                 case '+':
157                     wproto_cache (wcl, wcl->cache_level+1);
158                     break;
159                 case '-':
160                     if (p[1])
161                         wproto_cache (wcl, wcl->cache_level-atoi(p+1));
162                     else
163                         wproto_cache (wcl, wcl->cache_level-1);
164                     *p = '\0';
165                     break;
166                 case '=':
167                     if (isdigit(p[1]))
168                         wproto_cache (wcl, atoi(p+1));
169                     *p = '\0';
170                     break;
171                 case ';':
172                     wo_printf (wcl, "Expires: %s\n", p[1] ? p+1 : "0");
173                     gw_log (GW_LOG_DEBUG, mod, "Expires: %s", p[1] ?p+1:"0");
174                     *p = '\0';
175                     break;
176                 }
177 #else
178             p = script;
179             while (*p)
180             {
181                 if (*p == ';') /* ";" or ";<date>" */
182                 {
183                     wo_printf (wcl, "Expires: %s\n", p[1] ? p+1 : "0");
184                     gw_log (GW_LOG_DEBUG, mod, "Expires: %s", p[1] ?p+1:"0");
185                     *p = '\0';
186                     break;
187                 }
188                 else if (*p == '+') /* "+<name>=<value>" or "+<name>" */
189                 {
190                     const char *name1 = NULL;
191                     const char *name2 = NULL;
192                     const char *value1 = NULL;
193                     const char *value2 = NULL;
194                     int len;
195                     *p = '\0';
196                     name1 = ++p;
197                     while (*p && *p != ';' && *p != '+')
198                     {
199                         if (*p == '=')
200                         {
201                             name2 = p++;
202                             value1 = p;
203                             while (*p && *p != ';' && *p != '+')
204                                 p++;
205                             value2 = p;
206                             break;
207                         }
208                         p++;
209                     }
210                     if (!name2)
211                         name2 = p;
212                     len = name2 - name1;
213                     if (len == 3 && !memcmp (name1, "raw", len))
214                         raw_mode = 1;
215                 }
216                 else
217                     p++;
218             }
219             *p = '\0';
220 #endif
221         }
222         if (!raw_mode)
223             wo_printf (wcl, "Content-type: text/html\n\n");
224         for (p = parms; *p; p++)
225             if (*p == '+')
226                 *p = ' ';
227         gw_log (GW_LOG_STAT, mod, "script: %s", script);
228         gw_log (GW_LOG_STAT, mod, "parms: %s", parms);
229         if (w_interp_exec (w_interp, script, parms))
230         {
231             if (raw_mode)
232                 wo_printf (wcl, "Content-type: text/html\n\n");
233             wo_printf (wcl, "<html><head><title>wsh error</title></head>\n");
234             wo_printf (wcl, "<body>Couldn't execute script %s</body></html>",
235                        script);
236         }
237         wo_finish(wcl);
238     }
239     gw_log (GW_LOG_DEBUG, mod, "save state..");
240     w_interp_save_state (w_interp, NULL);
241     wproto_terminate(wcl);
242 }
243
244