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