deae91fe4775f20b36b0992a0f6e9021578eb2b1
[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 void
85 webgit_enter_repository (struct webgit_query* query)
86 {
87   LLIST_FOREACH (&query->repos, node)
88     {
89       struct webgit_repo_info* ri = (struct webgit_repo_info*) node;
90       if (!strcmp (ri->name, query->repo))
91         {
92           if (chdir (ri->path))
93             return die ("error: wrong path %s", ri->path);
94           break;
95         }
96     }
97
98   setup_git_directory ();
99 }
100
101
102 int
103 main (int argc, char**argv)
104 {
105   Html page;
106   struct webgit_query query;
107
108   webgit_setup();
109
110   error_log = html_list();
111
112   clock_t timestat = clock();
113
114   webgit_query_init (&query);
115
116   /* set some defaults if not already set */
117   setenv ("REQUEST_METHOD", "GET", 0);
118   setenv ("SCRIPT_NAME", argv[0], 0);
119   setenv ("WEBGIT_CONFIG", WEBGIT_CONFIG, 0);
120
121   /* we can only longjmp out of libgit's error handling */
122   if (setjmp (err_jmp))
123     goto error;
124
125   if (webgit_commandline_dispatch (argc, argv, &query))
126     goto error;
127
128   // read config
129   if (webgit_configfile_dispatch (getenv("WEBGIT_CONFIG"), &query))
130     goto error;
131
132
133   // parse request/query What to show?
134
135   query.request = cgi_new ();
136
137   cgi_run_query (query.request, webgit_param_dispatch, &query);
138
139   /* default action is to show the main page with the list of repos */
140   if (!query.action)
141     query.action = cwa_strndup ("main", SIZE_MAX);
142
143   // query to cache-line
144
145   // check if in cache
146   
147   // generate page
148   
149   //Html page = webgit_action_dispatch (&query);
150
151   page = html(
152               html_httpheader(
153                               html_httpfield(
154                                              "Content-type",
155                                              query.content_type,
156                                              html_attr("charset", "UTF-8")
157                                              )//,
158                               //html_httpfield("Last-Modified", cgit_page.modified),
159                               //html_httpfield("Expires", cgit_page.expires)
160                               ),
161               html_document(
162                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
163                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
164                             html_content(
165                                          /*head*/
166                                          html(
167                                               html(html_tag("title"), "webgit"),
168                                               html_nl(),
169                                               html_meta("generator", "webgit")//,
170                                               //html_link_rel("stylesheet", "text/css", cgit_page.stylesheet)
171                                               ),
172                                          /*body*/
173                                          html (
174                                                html (
175                                                      html_tag("div"),
176                                                      webgit_action_dispatch (&query)
177                                                      ),
178                                                html(
179                                                     html_tag("div"),
180                                                     error_log
181                                                     )
182                                          ),
183                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
184                                          html_attr("xml:lang", "en"),
185                                          html_attr("lang", "en")
186                                          )
187                             )
188               );
189
190   timestat = clock() - timestat;
191   html_list_append (error_log, html (
192                                      html_fmt ("processing took %g secs\n", timestat/(float)CLOCKS_PER_SEC),
193                                      html_tag("br")
194                                      )
195                     );
196
197   html_fprint (stdout, page);
198
199
200   html_free (page);
201
202   webgit_query_destroy (&query);
203
204   return 0;
205
206  error:
207   page = html(
208               html_httpheader(
209                               html_httpfield(
210                                              "Content-type",
211                                              "text/html",
212                                              html_attr("charset", "UTF-8")
213                                              )                              ),
214               html_document(
215                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
216                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
217                             html_content(
218                                          /*head*/
219                                          html(
220                                               html(html_tag("title"), "ERROR: webgit"),
221                                               html_nl(),
222                                               html_meta("generator", "webgit")
223                                               ),
224                                          /*body*/
225                                          html(
226                                               html_tag("div"),
227                                               error_log
228                                               ),
229                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
230                                          html_attr("xml:lang", "en"),
231                                          html_attr("lang", "en")
232                                          )
233                             )
234               );
235
236   html_fprint (stdout, page);
237   html_free (page);
238   return 10;
239 }