705d7e76e709c88ee551423e9653c1f677b6967d
[citadel.git] / webcit / html2html.c
1 /*
2  * $Id$
3  *
4  * Output an HTML message, modifying it slightly to make sure it plays nice
5  * with the rest of our web framework.
6  *
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <limits.h>
19 #include <netinet/in.h>
20 #include <netdb.h>
21 #include <string.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include "webcit.h"
28 #include "vcard.h"
29 #include "webserver.h"
30
31
32 /*
33  */
34 void output_html(void) {
35         char buf[SIZ];
36         char *msg;
37         char *ptr;
38         char *msgstart;
39         char *msgend;
40         int total_length = 1;
41         int line_length = 0;
42
43         msg = strdup("");
44         msgstart = msg;
45         msgend = msg;
46
47         while (serv_gets(buf), strcmp(buf, "000")) {
48                 line_length = strlen(buf);
49                 total_length = total_length + line_length + 1;
50                 msg = realloc(msg, total_length);
51                 strcpy(msgend, buf);
52                 strcat(msgend, "\n");
53                 msgend = &msgend[line_length + 1];
54         }
55
56         ptr = msg;
57         msgstart = msg;
58         msgend = &msg[total_length];
59
60         fprintf(stderr, "msg looks like this:\n%s\n", ptr);
61
62         while (ptr < msgend) {
63
64                 /* Advance to next tag */
65                 ptr = strchr(ptr, '<');
66                 ++ptr;
67
68                 /* Any of these tags cause everything up to and including
69                  * the tag to be removed.
70                  */     
71                 if ( (!strncasecmp(ptr, "HTML", 4))
72                    ||(!strncasecmp(ptr, "HEAD", 4))
73                    ||(!strncasecmp(ptr, "/HEAD", 5))
74                    ||(!strncasecmp(ptr, "BODY", 4)) ) {
75                         ptr = strchr(ptr, '>');
76                         ++ptr;
77                         msgstart = ptr;
78                 }
79
80                 /* Any of these tags cause everything including and following
81                  * the tag to be removed.
82                  */
83                 if ( (!strncasecmp(ptr, "/HTML", 5))
84                    ||(!strncasecmp(ptr, "/BODY", 5)) ) {
85                         --ptr;
86                         msgend = ptr;
87                         strcpy(ptr, "");
88                         
89                 }
90
91                 ++ptr;
92         }
93
94         write(WC->http_sock, msgstart, strlen(msgstart));
95
96         /* Close a bunch of tags that might have been opened 
97         wprintf("</I></B></FONT></TD></TR></TABLE></TT></PRE></A><BR>\n");
98          */
99
100         /* Now give back the memory */
101         free(msg);
102 }
103