File: fcgi_hello.c

package info (click to toggle)
clearsilver 0.10.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: ansic: 24,586; python: 4,233; sh: 2,502; cs: 1,429; ruby: 819; java: 735; makefile: 589; perl: 120; lisp: 34; sql: 21
file content (59 lines) | stat: -rw-r--r-- 1,481 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * Copyright 2006 Mike Tsao. All rights reserved.
 *
 * Hello World using FastCGI and ClearSilver.
 */

#include "ClearSilver.h"
#include <string>
#include <fcgi_stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <syslog.h>

static bool quit = false;

static int cs_printf(void *ctx, const char *s, va_list args) {
  return printf(s, args);
}

static int cs_write(void *ctx, const char *s, int n) {
  return fwrite(const_cast<char *>(s), n, 1, FCGI_stdout);
}

int main(int argc, char **argv, char **envp) {
  openlog(argv[0], 0, LOG_USER);
  syslog(LOG_INFO, "%s started.", argv[0]); 

  int hits = 0;
  while (FCGI_Accept() >= 0) {
    HDF *hdf = NULL;
    CGI *cgi = NULL;

    /* Note that we aren't doing any error handling here, we really should. */
    hdf_init(&hdf);

    // Takes ownership of HDF.
    cgi_init(&cgi, hdf);

    hits++;

    /* Initialize the standard cgiwrap environment.  FastCGI already wraps some
     * of the standard calls that cgiwrap wraps.  */
    cgiwrap_init_std(argc, argv, environ);

    /* Then, we install our own wrappers for some cgiwrap calls that aren't
     * already wrapped in the standard wrappers. */
    cgiwrap_init_emu(NULL, NULL, cs_printf, cs_write, NULL, NULL, NULL);

    hdf_read_file(cgi->hdf, "common.hdf");
    hdf_read_file(cgi->hdf, "hello_world.hdf");

    cgi_display(cgi, "hello_world.cs");

    // This destroys HDF.
    cgi_destroy(&cgi);
  }
  syslog(LOG_INFO, "%s ending.", argv[0]); 
  return 0;
}