Add to git.
[monolith.git] / apps / msp.c
1 /* Monolith server-parsed pages (.msp's).
2  * - by Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: msp.c,v 1.8 2003/02/22 15:34:24 rich Exp $
19  */
20
21 #include <pool.h>
22 #include <pstring.h>
23
24 #include <pthr_cgi.h>
25
26 #include "monolith.h"
27 #include "ml_window.h"
28 #include "ml_msp.h"
29
30 /*----- The following standard boilerplate code must appear -----*/
31
32 /* Main entry point to the app. */
33 static void app_main (ml_session);
34
35 int
36 handle_request (rws_request rq)
37 {
38   return ml_entry_point (rq, app_main);
39 }
40
41 /*----- End of standard boilerplate code -----*/
42
43 static void
44 app_main (ml_session session)
45 {
46   pool pool = ml_session_pool (session);
47   ml_window w;
48   ml_msp msp;
49   const char *conninfo, *rootdir, *filename, *canonical_path;
50   cgi args;
51
52   canonical_path = ml_session_canonical_path (session);
53
54   /* Create the top-level window. */
55   w = new_ml_window (session, pool);
56
57   /* Disable the window headers. We can assume that the .msp file will
58    * supply <html>...</html>.
59    */
60   ml_window_set_headers_flag (w, 0);
61
62   /* Nominate this window as the main window for this application. */
63   ml_session_set_main_window (session, w);
64
65   /* Get the document root. */
66   rootdir = ml_cfg_get_string (session, "msp root", 0);
67   if (rootdir == 0)
68     {
69       fprintf (stderr, "%s: no \"msp root\" directive in conf file.\n",
70                canonical_path);
71       return;
72     }
73
74   /* Get the database connection name. */
75   conninfo = ml_cfg_get_string (session, "msp database", 0);
76
77   /* Get the filename. */
78   args = ml_session_args (session);
79   if (!args || (filename = cgi_param (args, "page")) == 0)
80     {
81       fprintf (stderr, "%s: missing page parameter.\n", canonical_path);
82       return;
83     }
84
85   /* Create the msp widget. */
86   msp = new_ml_msp (pool, session, conninfo, rootdir, filename);
87   if (!msp)
88     {
89       const char *msg =
90         psprintf (pool,
91                   "%s: failed to create msp widget (bad filename "
92                   "or msp file contains errors?)",
93                   canonical_path);
94
95       /* Send some extra debug to the log file. */
96       fprintf (stderr,
97                "%s\n"
98                "%s: rootdir = %s, filename = %s\n",
99                msg, canonical_path, rootdir, filename);
100
101       /* Die with an error back to the browser. */
102       pth_die (msg);
103     }
104
105   /* Pack the msp widget into the window. */
106   ml_window_pack (w, msp);
107 }