Installer import into contrib (real import this time)
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / frontends / cgi / example.c
1 #include <stdio.h>
2
3 #include <string.h>
4 #include "dfui/dfui.h"
5
6 /*
7  *
8  *
9  * Example for how to communicate with a DFUI backend from within a CGI.
10  * Not currently working (missing the bit where we send back a response,)
11  * but it shows the outline.
12  *
13  * Only seems to work with thttpd (where user= the user that the DFUI
14  * backend is running as;) had no luck with Apache.
15  *
16  *
17  */
18
19 static struct dfui_response *
20 create_response_from_posted_data(void)
21 {
22         struct dfui_response *r;
23                 
24         r = dfui_response_new("the_form", "the_button_was_clicked");
25         /* add the contents of all fields to r's dataset(s) */
26
27         return(r);
28 }
29
30 static void
31 display_HTML_form_based_on(struct dfui_form *f __unused)
32 {
33         printf("<p>foo.</p>");
34 }
35
36 static int
37 request_method_is(const char *method)
38 {
39         /* XXX */
40         if (!strcasecmp(method, "plugh"))
41                 return(1);
42         else
43                 return(0);
44 }
45
46 int
47 main(int argc __unused, char **argv __unused)
48 {
49         struct dfui_connection *c;
50         struct dfui_form *f;
51         struct dfui_response *r;
52         char msgtype;
53         void *payload;
54
55         printf("Content-type: text/html\r\n\r\n");
56         fflush(stdout);
57
58         c = dfui_connection_new(DFUI_TRANSPORT_CAPS, "test");
59         dfui_fe_connect(c);
60
61         printf("<html><h1>Hi!</h1>");
62         fflush(stdout);
63
64         if (request_method_is("POST")) {
65                 r = create_response_from_posted_data();
66                 dfui_fe_submit(c, r);
67                 dfui_response_free(r);
68         }
69
70         dfui_fe_receive(c, &msgtype, &payload);
71         switch (msgtype) {
72         case DFUI_BE_MSG_PRESENT:
73                 f = (struct dfui_form *)payload;
74                 display_HTML_form_based_on(f);
75                 dfui_form_free(f);
76         }
77
78         dfui_fe_disconnect(c);
79         return(0);
80 }