6b3b9e79889ad33fd78f7087ea17eb1a7271e763
[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   // query to cache-line
143
144   // check if in cache
145   
146   // generate page
147   
148   //Html page = ctgit_action_dispatch (&query);
149
150   page = html(
151               html_httpheader(
152                               html_httpfield(
153                                              "Content-type",
154                                              "text/html",
155                                              html_attr("charset", "UTF-8")
156                                              )//,
157                               //html_httpfield("Last-Modified", cgit_page.modified),
158                               //html_httpfield("Expires", cgit_page.expires)
159                               ),
160               html_document(
161                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
162                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
163                             html_content(
164                                          /*head*/
165                                          html(
166                                               html(html_tag("title"), "ctgit"),
167                                               html_nl(),
168                                               html_meta("generator", "ctgit")//,
169                                               //html_link_rel("stylesheet", "text/css", cgit_page.stylesheet)
170                                               ),
171                                          /*body*/
172                                          html (
173                                                html (
174                                                      html_tag("div"),
175                                                      ctgit_action_dispatch (&query)
176                                                      ),
177                                                html(
178                                                     html_tag("div"),
179                                                     error_log
180                                                     )
181                                          ),
182                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
183                                          html_attr("xml:lang", "en"),
184                                          html_attr("lang", "en")
185                                          )
186                             )
187               );
188
189   timestat = clock() - timestat;
190   html_list_append (error_log, html (
191                                      html_fmt ("processing took %g secs\n", timestat/(float)CLOCKS_PER_SEC),
192                                      html_tag("br")
193                                      )
194                     );
195
196   html_fprint (stdout, page);
197
198
199   html_free (page);
200
201   ctgit_query_destroy (&query);
202
203   return 0;
204
205  error:
206   page = html(
207               html_httpheader(
208                               html_httpfield(
209                                              "Content-type",
210                                              "text/html",
211                                              html_attr("charset", "UTF-8")
212                                              )                              ),
213               html_document(
214                             "-//W3C//DTD XHTML 1.0 Transitional//EN",
215                             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd",
216                             html_content(
217                                          /*head*/
218                                          html(
219                                               html(html_tag("title"), "ERROR: ctgit"),
220                                               html_nl(),
221                                               html_meta("generator", "ctgit")
222                                               ),
223                                          /*body*/
224                                          html(
225                                               html_tag("div"),
226                                               error_log
227                                               ),
228                                          html_attr("xmlns", "http://www.w3.org/1999/xhtml"),
229                                          html_attr("xml:lang", "en"),
230                                          html_attr("lang", "en")
231                                          )
232                             )
233               );
234
235   html_fprint (stdout, page);
236   html_free (page);
237   return 10;
238 }