Fix: set 'main' action after the query dispatch
[webgit] / src / ctgit.c
1 /*
2     cehtehs git web frontend
3
4   Copyright (C)
5     2007,               Christian Thaeter <ct@pipapo.org>
6
7   This program is free software; you can redistribute it and/or
8   modify it under the terms of the GNU General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22
23 #include "ctgit.h"
24
25 #ifndef CTGIT_CONFIG
26 #define CTGIT_CONFIG "/etc/ctgit.conf"
27 #endif
28
29 #include "actions.h"
30 #include "query.h"
31 #include "options.h"
32
33 #include "git/git-compat-util.h"
34
35 #include <cwa.h>
36
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <time.h>
41 #include <setjmp.h>
42
43 Html error_log;
44 jmp_buf err_jmp;
45
46 void
47 ctgit_err_vargs (const char *err, va_list params)
48 {
49   html_list_append (error_log, html (html_fmt_vargs (err, params), html_tag ("br")));
50   longjmp (err_jmp, 0);
51 }
52
53 void
54 ctgit_warn_vargs (const char *err, va_list params)
55 {
56   html_list_append (error_log, html (html_fmt_vargs (err, params), html_tag("br")));
57 }
58
59
60 static void
61 ctgit_setup (void)
62 {
63   set_die_routine (ctgit_err_vargs);
64   set_error_routine (ctgit_err_vargs);
65   set_warn_routine (ctgit_warn_vargs);
66 }
67
68
69 FILE*
70 ctgit_git_open_vargs (const char* fmt, va_list args)
71 {
72   /* note: its dangerous to call this function with user supplied data, always use validated data */
73   char buf[256] = "git ";
74   char cmd[1024];
75
76   strncat (buf, fmt, 255);
77   if (vsnprintf (cmd, 1024, buf, args) >= 1024)
78     return NULL;
79
80   return popen (cmd, "r");
81 }
82
83
84 FILE*
85 ctgit_git_open (const char* fmt, ...)
86 {
87   FILE* ret = NULL;
88   va_list args;
89
90   va_start (args, fmt);
91   ret = ctgit_git_open_vargs (fmt, args);
92   va_end (args);
93
94   return ret;
95 }
96
97 void
98 ctgit_git_close (FILE* handle)
99 {
100   if (handle)
101     pclose (handle);
102 }
103
104
105 int
106 main (int argc, char**argv)
107 {
108   Html page;
109   struct ctgit_query query;
110
111   ctgit_setup();
112
113   error_log = html_list();
114
115   clock_t timestat = clock();
116
117   ctgit_query_init (&query);
118
119   /* set some defaults if not already set */
120   setenv ("REQUEST_METHOD", "GET", 0);
121   setenv ("SCRIPT_NAME", argv[0], 0);
122   setenv ("CTGIT_CONFIG", CTGIT_CONFIG, 0);
123
124   /* we can only longjmp out of libgit's error handling */
125   if (setjmp (err_jmp))
126     goto error;
127
128   if (ctgit_commandline_dispatch (argc, argv, &query))
129     goto error;
130
131   // read config
132   if (ctgit_configfile_dispatch (getenv("CTGIT_CONFIG"), &query))
133     goto error;
134
135
136   // parse request/query What to show?
137
138   query.request = cgi_new ();
139
140   cgi_run_query (query.request, ctgit_param_dispatch, &query);
141
142   /* default action is to show the main page with the list of repos */
143   if (!query.action)
144     query.action = cwa_strndup ("main", SIZE_MAX);
145
146   // query to cache-line
147
148   // check if in cache
149   
150   // generate page
151   
152   //Html page = ctgit_action_dispatch (&query);
153
154   page = html(
155               html_httpheader(
156                               html_httpfield(
157                                              "Content-type",
158                                              "text/html",
159                                              html_attr("charset", "UTF-8")
160                                              )//,
161                               //html_httpfield("Last-Modified", cgit_page.modified),
162                               //html_httpfield("Expires", cgit_page.expires)
163                               ),
164               html_document(
165                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
166                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
167                             html_content(
168                                          /*head*/
169                                          html(
170                                               html(html_tag("title"), "ctgit"),
171                                               html_nl(),
172                                               html_meta("generator", "ctgit")//,
173                                               //html_link_rel("stylesheet", "text/css", cgit_page.stylesheet)
174                                               ),
175                                          /*body*/
176                                          html (
177                                                html (
178                                                      html_tag("div"),
179                                                      ctgit_action_dispatch (&query)
180                                                      ),
181                                                html(
182                                                     html_tag("div"),
183                                                     error_log
184                                                     )
185                                          ),
186                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
187                                          html_attr("xml:lang", "en"),
188                                          html_attr("lang", "en")
189                                          )
190                             )
191               );
192
193   timestat = clock() - timestat;
194   html_list_append (error_log, html (
195                                      html_fmt ("processing took %g secs\n", timestat/(float)CLOCKS_PER_SEC),
196                                      html_tag("br")
197                                      )
198                     );
199
200   html_fprint (stdout, page);
201
202
203   html_free (page);
204
205   ctgit_query_destroy (&query);
206
207   return 0;
208
209  error:
210   page = html(
211               html_httpheader(
212                               html_httpfield(
213                                              "Content-type",
214                                              "text/html",
215                                              html_attr("charset", "UTF-8")
216                                              )                              ),
217               html_document(
218                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
219                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
220                             html_content(
221                                          /*head*/
222                                          html(
223                                               html(html_tag("title"), "ERROR: ctgit"),
224                                               html_nl(),
225                                               html_meta("generator", "ctgit")
226                                               ),
227                                          /*body*/
228                                          html(
229                                               html_tag("div"),
230                                               error_log
231                                               ),
232                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
233                                          html_attr("xml:lang", "en"),
234                                          html_attr("lang", "en")
235                                          )
236                             )
237               );
238
239   html_fprint (stdout, page);
240   html_free (page);
241   return 10;
242 }