1 /**
2 * Copyright 2006 Mike Tsao. All rights reserved.
3 *
4 * Hello World using FastCGI and ClearSilver.
5 */
6
7 #include "ClearSilver.h"
8 #include <string>
9 #include <fcgi_stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <syslog.h>
13
14 static bool quit = false;
15
cs_printf(void * ctx,const char * s,va_list args)16 static int cs_printf(void *ctx, const char *s, va_list args) {
17 return printf(s, args);
18 }
19
cs_write(void * ctx,const char * s,int n)20 static int cs_write(void *ctx, const char *s, int n) {
21 return fwrite(const_cast<char *>(s), n, 1, FCGI_stdout);
22 }
23
main(int argc,char ** argv,char ** envp)24 int main(int argc, char **argv, char **envp) {
25 openlog(argv[0], 0, LOG_USER);
26 syslog(LOG_INFO, "%s started.", argv[0]);
27
28 int hits = 0;
29 while (FCGI_Accept() >= 0) {
30 HDF *hdf = NULL;
31 CGI *cgi = NULL;
32
33 /* Note that we aren't doing any error handling here, we really should. */
34 hdf_init(&hdf);
35
36 // Takes ownership of HDF.
37 cgi_init(&cgi, hdf);
38
39 hits++;
40
41 /* Initialize the standard cgiwrap environment. FastCGI already wraps some
42 * of the standard calls that cgiwrap wraps. */
43 cgiwrap_init_std(argc, argv, environ);
44
45 /* Then, we install our own wrappers for some cgiwrap calls that aren't
46 * already wrapped in the standard wrappers. */
47 cgiwrap_init_emu(NULL, NULL, cs_printf, cs_write, NULL, NULL, NULL);
48
49 hdf_read_file(cgi->hdf, "common.hdf");
50 hdf_read_file(cgi->hdf, "hello_world.hdf");
51
52 cgi_display(cgi, "hello_world.cs");
53
54 // This destroys HDF.
55 cgi_destroy(&cgi);
56 }
57 syslog(LOG_INFO, "%s ending.", argv[0]);
58 return 0;
59 }
60