annotate dpi/hello.c @ 0:6ee11bf9e3ea

Initial revision
author jcid
date Sun, 07 Oct 2007 00:36:34 +0200
parents
children d9e7b35430de
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * Dpi for "Hello World".
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
4 * This server is an example. Play with it and modify to your taste.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * Copyright 2003 Jorge Arellano Cid <jcid@dillo.org>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #include "../dpip/dpip.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 #define MSG(...) printf("[hello dpi]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 /*---------------------------------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 int main(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 FILE *in_stream;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 SockHandler *sh;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 char *dpip_tag, *cmd = NULL, *url = NULL, *child_cmd = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 char *esc_tag, *d_cmd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 size_t n;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 int ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 char buf[4096];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 char *choice[] = {"Window was closed", "Yes", "No",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 "Could be", "It's OK", "Cancel"};
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 /* "Could>be", ">It's OK", "Can'>cel"}; --for testing */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 int choice_num;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 MSG("starting...\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 /* Initialize the SockHandler */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 sh = sock_handler_new(STDIN_FILENO, STDOUT_FILENO, 2*1024);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 /* Read the dpi command from STDIN */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 dpip_tag = sock_handler_read(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 MSG("tag = [%s]\n", dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 cmd = a_Dpip_get_attr(dpip_tag, strlen(dpip_tag), "cmd");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 url = a_Dpip_get_attr(dpip_tag, strlen(dpip_tag), "url");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 /*-- Dialog part */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 char *dpip_tag2, *dialog_msg;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 /* Let's confirm the request */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 /* NOTE: you can send less alternatives (e.g. only alt1 and alt2) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 d_cmd = a_Dpip_build_cmd(
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 "cmd=%s msg=%s alt1=%s alt2=%s alt3=%s alt4=%s alt5=%s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 "dialog", "Do you want to see the hello page?",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 choice[1], choice[2], choice[3], choice[4], choice[5]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 sock_handler_write_str(sh, 1, d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 /* Get the answer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 dpip_tag2 = sock_handler_read(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 MSG("tag = [%s]\n", dpip_tag2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 /* Get "msg" value */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 dialog_msg = a_Dpip_get_attr(dpip_tag2, strlen(dpip_tag2), "msg");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 choice_num = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 if (dialog_msg)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 choice_num = *dialog_msg - '0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 dFree(dialog_msg);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 dFree(dpip_tag2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 /*-- EOD part */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 /* Start sending our answer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 sock_handler_write_str(sh, 0, d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 sock_handler_printf(sh, 0,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 "Content-type: text/html\n\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 "<html>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 "<head><title>Simple dpi test page (hello.dpi)</title></head>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 "<body><hr><h1>Hello world!</h1><hr>\n<br><br>\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 /* Show the choice received with the dialog */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 sock_handler_printf(sh, 0,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 "<hr>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 "<table width='100%%' border='1' bgcolor='burlywood'><tr><td>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 "<big><em>Dialog question:</em> Do you want to see the hello page?<br>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 "<em>Answer received:</em> <b>%s</b></big> </table>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 "<hr>\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 choice[choice_num]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 /* Show the dpip tag we received */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 esc_tag = Escape_html_str(dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 sock_handler_printf(sh, 0,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 "<h3>dpip tag received:</h3>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 "<pre>\n%s</pre>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 "<br><small>(<b>dpip:</b> dpi protocol)</small><br><br><br>\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 esc_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 dFree(esc_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 /* Now something more interesting,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 * fork a command and show its feedback */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 if (cmd && url) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 child_cmd = dStrdup("date -R");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 MSG("[%s]\n", child_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 /* Fork, exec command, get its output and answer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 if ((in_stream = popen(child_cmd, "r")) == NULL) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 perror("popen");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 return EXIT_FAILURE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 sock_handler_printf(sh, 0, "<h3>date:</h3>\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 sock_handler_printf(sh, 0, "<pre>\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 /* Read/Write */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 while ((n = fread (buf, 1, 4096, in_stream)) > 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 sock_handler_write(sh, 0, buf, n);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 sock_handler_printf(sh, 0, "</pre>\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 if ((ret = pclose(in_stream)) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 MSG("popen: [%d]\n", ret);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 dFree(child_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 sock_handler_printf(sh, 1, "</body></html>\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 dFree(cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 dFree(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 dFree(dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 /* Finish the SockHandler */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 sock_handler_close(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 sock_handler_free(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161