. Added space to manpage synopsis
[infodrom/cgilib] / cgitest.c
1 /*
2     cgitest.c - Testprogram for cgi.o
3     Copyright (c) 1998,9 by Martin Schulze <joey@infodrom.north.de>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
18  */
19
20 /*
21  * Compile with: cc -o cgitest cgitest.c -lcgi
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <cgi.h>
27
28 s_cgi **cgi;
29
30 #define URL "http://www.infodrom.north.de/cgilib/"
31
32 void print_form()
33 {
34     printf ("<h1>Test-Form</h1>\n");
35     printf ("<form action=\"/cgi-bin/cgitest/insertdata\" method=post>\n");
36     printf ("Input: <input name=string size=50>\n<br>");
37     printf ("<select name=select multiple>\n<option>Nr. 1\n<option>Nr. 2\n<option>Nr. 3\n<option>Nr. 4\n</select>\n");
38     printf ("Text: <textarea name=text cols=50>\n</textarea>\n");
39     printf ("<center><input type=submit value=Submit> ");
40     printf ("<input type=reset value=Reset></center>\n");
41     printf ("</form>\n");
42     printf ("<hr width=50%%><form action=\"/cgi-bin/cgitest/listall\" method=post>\n");
43     printf ("Input: <input name=string size=50>\n<br>");
44     printf ("<select name=select multiple>\n<option>Nr. 1\n<option>Nr. 2\n<option>Nr. 3\n<option>Nr. 4\n</select>\n");
45     printf ("Text: <textarea name=text cols=50>\n</textarea>\n");
46     printf ("<center><input type=submit value=Show> ");
47     printf ("<input type=reset value=Reset></center>\n");
48     printf ("</form>\n");
49     printf ("<p><br><p><br><a href=\"/cgi-bin/cgitest/redirect\">Redirect</a><p>\n");
50 }
51
52 void eval_cgi()
53 {
54     printf ("<h1>Results</h1>\n\n");
55     printf ("<b>string</b>: %s<p>\n", cgiGetValue(cgi, "string"));
56     printf ("<b>text</b>: %s<p>\n", cgiGetValue(cgi, "text"));
57     printf ("<b>select</b>: %s<p>\n", cgiGetValue(cgi, "select"));
58 }
59
60 void listall (char **env)
61 {
62   char **vars;
63   char *val;
64   int i;
65
66   printf ("<h3>Environment Variables</h3>\n<pre>\n");
67   for (i=0; env[i]; i++)
68     printf ("%s\n", env[i]);
69   
70   printf ("</pre>\n<h3>CGI Variables</h3>\n<pre>\n");
71
72   vars = cgiGetVariables (cgi);
73   if (!vars)
74       return;
75
76   for (i=0; vars[i] != NULL; i++) {
77       val = cgiGetValue (cgi, vars[i]);
78       printf ("%s=%s\n", vars[i], val?val:"");
79   }
80   printf ("</pre>\n");
81 }
82
83 int main (int argc, char **argv, char **env)
84 {
85     char *path_info = NULL;
86
87     cgiDebug(0, 0);
88     cgi = cgiInit();
89
90     path_info = getenv("PATH_INFO");
91     if (path_info) {
92         if (!strcmp(path_info, "/redirect")) {
93             cgiRedirect("http://www.infodrom.north.de/");
94             exit (0);
95         } else if (!strcmp(path_info, "/listall")) {
96             cgiHeader();
97             printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
98             printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
99             listall (env);
100         } else {
101             cgiHeader();
102             printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
103             printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
104             if (strlen (path_info))
105                 printf ("path_info: %s<br>\n", path_info);
106             if (!strcmp(path_info, "/insertdata")) {
107                 eval_cgi();
108             } else
109                 print_form();
110         }
111     } else {
112         cgiHeader();
113         printf ("<html>\n<head><title>cgilib</title></title>\n\n<body bgcolor=\"#ffffff\">\n");
114         printf ("<h1><a href=\"%s\">cgilib</a></h1>\n", URL);
115         print_form();
116     }
117
118     printf ("\n<hr>\n</body>\n</html>\n");
119     return 0;
120 }
121
122 /*
123  * Local variables:
124  *  c-indent-level: 4
125  *  c-basic-offset: 4
126  *  tab-width: 8
127  * End:
128  */