]> code.citadel.org Git - citadel.git/blobdiff - webcit/html2html.c
Cleaned up some commented-out stuff that was left in various files
[citadel.git] / webcit / html2html.c
index 00a2adcbb9301260c93462cac4515b6ea11dd3b2..8799aaa2a3d6a2d85a61b511ebc7b7927338abc0 100644 (file)
@@ -77,6 +77,7 @@ void output_html(char *supplied_charset, int treat_as_wiki) {
        char *msgstart;
        char *msgend;
        char *converted_msg;
+       size_t converted_alloc = 0;
        int buffer_length = 1;
        int line_length = 0;
        int content_length = 0;
@@ -103,15 +104,20 @@ void output_html(char *supplied_charset, int treat_as_wiki) {
        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                line_length = strlen(buf);
                buffer_length = content_length + line_length + 2;
-               msg = realloc(msg, buffer_length);
-               if (msg == NULL) {
+               ptr = realloc(msg, buffer_length);
+               if (ptr == NULL) {
                        wprintf("<b>");
                        wprintf(_("realloc() error! couldn't get %d bytes: %s"),
                                buffer_length + 1,
                                strerror(errno));
                        wprintf("</b><br /><br />\n");
+                       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                               /** flush */
+                       }
+                       free(msg);
                        return;
                }
+               msg = ptr;
                strcpy(&msg[content_length], buf);
                content_length += line_length;
                strcpy(&msg[content_length], "\n");
@@ -216,7 +222,7 @@ void output_html(char *supplied_charset, int treat_as_wiki) {
           && (strcasecmp(charset, ""))
        ) {
                lprintf(9, "Converting %s to UTF-8\n", charset);
-               ic = iconv_open("UTF-8", charset);
+               ic = ctdl_iconv_open("UTF-8", charset);
                if (ic == (iconv_t)(-1) ) {
                        lprintf(5, "%s:%d iconv_open() failed: %s\n",
                                __FILE__, __LINE__, strerror(errno));
@@ -237,15 +243,27 @@ void output_html(char *supplied_charset, int treat_as_wiki) {
        }
 #endif
 
-       /** FIXME At this point, shigerugo's messages are still clean.
-        *        Figure out what is mangling them below.
+       /**
+        *      At this point, the message has been stripped down to
+        *      only the content inside the <BODY></BODY> tags, and has
+        *      been converted to UTF-8 if it was originally in a foreign
+        *      character set.  The text is also guaranteed to be null
+        *      terminated now.
         */
 
        /** Now go through the message, parsing tags as necessary. */
-       converted_msg = malloc(content_length);
+       converted_alloc = content_length + 8192;
+       converted_msg = malloc(converted_alloc);
+       if (converted_msg == NULL) {
+               wprintf("Error %d: %s<br />%s:%s", errno, strerror(errno), __FILE__, __LINE__);
+               goto BAIL;
+       }
+
        strcpy(converted_msg, "");
-       ptr = msgstart;
+       ptr = msg;
+       msgend = strchr(msg, 0);
        while (ptr < msgend) {
+
                /**
                 * Change mailto: links to WebCit mail, by replacing the
                 * link with one that points back to our mail room.  Due to
@@ -254,7 +272,13 @@ void output_html(char *supplied_charset, int treat_as_wiki) {
                 */
                if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
                        content_length += 64;
-                       converted_msg = realloc(converted_msg, content_length);
+                       if (content_length >= converted_alloc) {
+                               converted_alloc += 8192;
+                               converted_msg = realloc(converted_msg, converted_alloc);
+                               if (converted_msg == NULL) {
+                                       abort();
+                               }
+                       }
                        sprintf(&converted_msg[output_length],
                                "<a href=\"display_enter"
                                "?force_room=_MAIL_&recp=");
@@ -263,30 +287,41 @@ void output_html(char *supplied_charset, int treat_as_wiki) {
                        ++alevel;
                }
                /** Make external links open in a separate window */
-               else if (!strncasecmp(ptr, "<a href=", 8)) {
+               else if (!strncasecmp(ptr, "<a href=\"", 9)) {
                        ++alevel;
                        if ( ((strchr(ptr, ':') < strchr(ptr, '/')))
                             &&  ((strchr(ptr, '/') < strchr(ptr, '>'))) 
                             ) {
                                /* open external links to new window */
                                content_length += 64;
-                               converted_msg = realloc(converted_msg, content_length);
+                               if (content_length >= converted_alloc) {
+                                       converted_alloc += 8192;
+                                       converted_msg = realloc(converted_msg, converted_alloc);
+                                       if (converted_msg == NULL) {
+                                               abort();
+                                       }
+                               }
                                sprintf(&converted_msg[output_length], new_window);
                                output_length += strlen(new_window);
                                ptr = &ptr[8];
                        }
                        else if ( (treat_as_wiki) && (strncasecmp(ptr, "<a href=\"wiki?", 14)) ) {
-                               lprintf(9, "converting wiki link\n");
                                content_length += 64;
-                               converted_msg = realloc(converted_msg, content_length);
+                               if (content_length >= converted_alloc) {
+                                       converted_alloc += 8192;
+                                       converted_msg = realloc(converted_msg, converted_alloc);
+                                       if (converted_msg == NULL) {
+                                               abort();
+                                       }
+                               }
                                sprintf(&converted_msg[output_length], "<a href=\"wiki?page=");
                                output_length += 19;
                                ptr = &ptr[9];
                        }
                        else {
-                               sprintf(&converted_msg[output_length], "<a href=");
-                               output_length += 8;
-                               ptr = &ptr[8];
+                               sprintf(&converted_msg[output_length], "<a href=\"");
+                               output_length += 9;
+                               ptr = &ptr[9];
                        }
                }
                /**
@@ -313,7 +348,13 @@ void output_html(char *supplied_charset, int treat_as_wiki) {
                                }
                                if (linklen > 0) {
                                        content_length += (32 + linklen);
-                                       converted_msg = realloc(converted_msg, content_length);
+                                       if (content_length >= converted_alloc) {
+                                               converted_alloc += 8192;
+                                               converted_msg = realloc(converted_msg, converted_alloc);
+                                               if (converted_msg == NULL) {
+                                                       abort();
+                                               }
+                                       }
                                        sprintf(&converted_msg[output_length], new_window);
                                        output_length += strlen(new_window);
                                        converted_msg[output_length] = '\"';
@@ -346,15 +387,19 @@ void output_html(char *supplied_charset, int treat_as_wiki) {
                }
        }
 
+       /**     uncomment these two lines to override conversion        */
+       /**     memcpy(converted_msg, msg, content_length);             */
+       /**     output_length = content_length;                         */
+
        /** Output our big pile of markup */
        client_write(converted_msg, output_length);
 
-       /** A little trailing vertical whitespace... */
+BAIL:  /** A little trailing vertical whitespace... */
        wprintf("<br /><br />\n");
 
        /** Now give back the memory */
-       free(converted_msg);
-       free(msg);
+       if (converted_msg != NULL) free(converted_msg);
+       if (msg != NULL) free(msg);
 }
 
 /*@}*/