webgit_tree_action() dereferences an treeish to the tree object
[webgit] / src / webgit.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 "webgit.h"
24
25 #define WEBGIT_CONFIG "./webgit.conf"
26
27 #ifndef WEBGIT_CONFIG
28 #define WEBGIT_CONFIG "/etc/webgit.conf"
29 #endif
30
31 #include "actions.h"
32 #include "query.h"
33 #include "options.h"
34
35 #include "git/git-compat-util.h"
36
37 #include <cwa.h>
38
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include <time.h>
43 #include <setjmp.h>
44
45 Html error_log;
46 jmp_buf err_jmp;
47
48 void
49 webgit_err_vargs (const char *err, va_list params)
50 {
51   html_list_append (error_log, html (html_fmt_vargs (err, params), html_tag ("br")));
52   longjmp (err_jmp, 0);
53 }
54
55 void
56 webgit_warn_vargs (const char *err, va_list params)
57 {
58   html_list_append (error_log, html (html_fmt_vargs (err, params), html_tag("br")));
59 }
60
61
62 static void
63 webgit_setup (void)
64 {
65   set_die_routine (webgit_err_vargs);
66   set_error_routine (webgit_err_vargs);
67   set_warn_routine (webgit_warn_vargs);
68 }
69
70
71 Html
72 webgit_email_link (Html name, Html email)
73 {
74   return html (
75                html_tag("a",
76                         html_attr ("href",
77                                    html ("mailto:", email))
78                         ),
79                name
80                );
81 }
82
83
84
85 int
86 main (int argc, char**argv)
87 {
88   Html page;
89   struct webgit_query query;
90
91   webgit_setup();
92
93   error_log = html_list();
94
95   clock_t timestat = clock();
96
97   webgit_query_init (&query);
98
99   /* set some defaults if not already set */
100   setenv ("REQUEST_METHOD", "GET", 0);
101   setenv ("SCRIPT_NAME", argv[0], 0);
102   setenv ("WEBGIT_CONFIG", WEBGIT_CONFIG, 0);
103
104   /* we can only longjmp out of libgit's error handling */
105   if (setjmp (err_jmp))
106     goto error;
107
108   if (webgit_commandline_dispatch (argc, argv, &query))
109     goto error;
110
111   // read config
112   if (webgit_configfile_dispatch (getenv("WEBGIT_CONFIG"), &query))
113     goto error;
114
115
116   // parse request/query What to show?
117
118   query.request = cgi_new ();
119
120   cgi_run_query (query.request, webgit_param_dispatch, &query);
121
122   /* default action is to show the main page with the list of repos */
123   if (!query.action)
124     query.action = cwa_strndup ("main", SIZE_MAX);
125
126   // query to cache-line
127
128   // check if in cache
129   
130   // generate page
131   
132   //Html page = webgit_action_dispatch (&query);
133
134   page = html(
135               html_httpheader(
136                               html_httpfield(
137                                              "Content-type",
138                                              query.content_type,
139                                              html_attr("charset", "UTF-8")
140                                              )//,
141                               //html_httpfield("Last-Modified", cgit_page.modified),
142                               //html_httpfield("Expires", cgit_page.expires)
143                               ),
144               html_document(
145                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
146                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
147                             html_content(
148                                          /*head*/
149                                          html(
150                                               html(html_tag("title"), "webgit"),
151                                               html_nl(),
152                                               html_meta("generator", "webgit")//,
153                                               //html_link_rel("stylesheet", "text/css", cgit_page.stylesheet)
154                                               ),
155                                          /*body*/
156                                          html (
157                                                html (
158                                                      html_tag("div"),
159                                                      webgit_action_dispatch (&query)
160                                                      ),
161                                                html(
162                                                     html_tag("div"),
163                                                     error_log
164                                                     )
165                                          ),
166                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
167                                          html_attr("xml:lang", "en"),
168                                          html_attr("lang", "en")
169                                          )
170                             )
171               );
172
173   timestat = clock() - timestat;
174   html_list_append (error_log, html (
175                                      html_fmt ("processing took %g secs\n", timestat/(float)CLOCKS_PER_SEC),
176                                      html_tag("br")
177                                      )
178                     );
179
180   html_fprint (stdout, page);
181
182
183   html_free (page);
184
185   webgit_query_destroy (&query);
186
187   return 0;
188
189  error:
190   page = html(
191               html_httpheader(
192                               html_httpfield(
193                                              "Content-type",
194                                              "text/html",
195                                              html_attr("charset", "UTF-8")
196                                              )                              ),
197               html_document(
198                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
199                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
200                             html_content(
201                                          /*head*/
202                                          html(
203                                               html(html_tag("title"), "ERROR: webgit"),
204                                               html_nl(),
205                                               html_meta("generator", "webgit")
206                                               ),
207                                          /*body*/
208                                          html(
209                                               html_tag("div"),
210                                               error_log
211                                               ),
212                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
213                                          html_attr("xml:lang", "en"),
214                                          html_attr("lang", "en")
215                                          )
216                             )
217               );
218
219   html_fprint (stdout, page);
220   html_free (page);
221   return 10;
222 }