]> code.citadel.org Git - citadel.git/blobdiff - citadel/html.c
* Removed the built-in memory leak checker. It wasn't threadsafe and
[citadel.git] / citadel / html.c
index 323fa0523023bee62b25cc303058b6bc1e43264e..4b910252859367a9c4f43bf647261d1ad6e713dc 100644 (file)
@@ -2,22 +2,39 @@
  * $Id$
  *
  * Functions which handle translation between HTML and plain text
+ * Copyright (c) 2000-2001 by Art Cancro and others.   This program is
+ * released under the terms of the GNU General Public License.
  */
 
+#ifdef DLL_EXPORT
+#define IN_LIBCIT
+#endif
+
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <signal.h>
-#include <time.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
 #include <limits.h>
-#include <syslog.h>
 #include "citadel.h"
 #include "server.h"
+#include "serv_extensions.h"
 #include "control.h"
 #include "sysdep_decls.h"
 #include "support.h"
@@ -42,7 +59,8 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
        int done_reading = 0;
        char *inptr;
        char *outptr;
-       size_t outlen;
+       size_t outptr_buffer_size;
+       size_t output_len = 0;
        int i, j, ch, did_out, rb;
        int nest = 0;           /* Bracket nesting level */
 
@@ -50,17 +68,18 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
        strcpy(inbuf, "");
        strcpy(outbuf, "");
 
-       outptr = mallok(strlen(inptr) + SIZ);
+       outptr_buffer_size = strlen(inptr) + SIZ;
+       outptr = malloc(outptr_buffer_size);
        if (outptr == NULL) return NULL;
        strcpy(outptr, "");
-       outlen = 0;
+       output_len = 0;
 
        do {
                /* Fill the input buffer */
-               if ( (done_reading == 0) && (strlen(inbuf) < 128) ) {
+               if ( (done_reading == 0) && (strlen(inbuf) < (SIZ-128)) ) {
 
-                       ch = *inputmsg++;
-                       if (ch > 0) {
+                       ch = *inptr++;
+                       if (ch != 0) {
                                inbuf[strlen(inbuf)+1] = 0;
                                inbuf[strlen(inbuf)] = ch;
                        } 
@@ -78,8 +97,12 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                        if (inbuf[i]==10) inbuf[i]=32;
                        if (inbuf[i]==13) inbuf[i]=32;
                        if (inbuf[i]==9) inbuf[i]=32;
-                       if ((inbuf[i]<32) || (inbuf[i]>126))
-                               strcpy(&inbuf[i], &inbuf[i+1]);
+                       if ((inbuf[i]<32) || (inbuf[i]>126)) {
+                               inbuf[i] = '?';
+                               /* strcpy(&inbuf[i], &inbuf[i+1]); */
+                       }
+                   }
+                   for (i=0; i<strlen(inbuf); ++i) {
                        while ((inbuf[i]==32)&&(inbuf[i+1]==32))
                                strcpy(&inbuf[i], &inbuf[i+1]);
                    }
@@ -214,9 +237,10 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                }
 
                /* Make sure the output buffer is big enough */
-               if ((strlen(outptr) + strlen(outbuf) + 128) > outlen) {
-                       outlen += 128;
-                       outptr = realloc(outptr, outlen);
+               if ((output_len + strlen(outbuf) + SIZ)
+                  > outptr_buffer_size) {
+                       outptr_buffer_size += SIZ;
+                       outptr = realloc(outptr, outptr_buffer_size);
                }
 
                /* Output any lines terminated with hard line breaks */
@@ -225,10 +249,17 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                        if (strlen(outbuf)>0)
                            for (i = 0; i<strlen(outbuf); ++i) {
                                if ( (i<(screenwidth-2)) && (outbuf[i]=='\n')) {
-                                       strncat(outptr, outbuf, i+1);
-                                       strcat(outptr, "\n");
-                                       if (do_citaformat)
-                                               strcat(outptr, " ");
+
+                                       strncpy(&outptr[output_len],
+                                               outbuf, i+1);
+                                       output_len += (i+1);
+
+                                       if (do_citaformat) {
+                                               strcpy(&outptr[output_len],
+                                                       " ");
+                                               ++output_len;
+                                       }
+
                                        strcpy(outbuf, &outbuf[i+1]);
                                        i = 0;
                                        did_out = 1;
@@ -243,27 +274,51 @@ char *html_to_ascii(char *inputmsg, int screenwidth, int do_citaformat) {
                                if (outbuf[i]==32) rb = i;
                        }
                        if (rb>=0) {
-                               strncat(outptr, outbuf, rb);
-                               strcat(outptr, "\n");
-                               if (do_citaformat)
-                                       strcat(outptr, " ");
+                               strncpy(&outptr[output_len], outbuf, rb);
+                               output_len += rb;
+                               strcpy(&outptr[output_len], "\n");
+                               output_len += 1;
+                               if (do_citaformat) {
+                                       strcpy(&outptr[output_len], " ");
+                                       ++output_len;
+                               }
                                strcpy(outbuf, &outbuf[rb+1]);
                        } else {
-
-                               strncat(outptr, outbuf, screenwidth-2);
-                               strcat(outptr, "\n");
-                               if (do_citaformat)
-                                       strcat(outptr, " ");
+                               strncpy(&outptr[output_len], outbuf,
+                                       screenwidth-2);
+                               output_len += (screenwidth-2);
+                               strcpy(&outptr[output_len], "\n");
+                               output_len += 1;
+                               if (do_citaformat) {
+                                       strcpy(&outptr[output_len], " ");
+                                       ++output_len;
+                               }
                                strcpy(outbuf, &outbuf[screenwidth-2]);
                        }
                }
 
        } while (done_reading == 0);
 
-       strcat(outptr, outbuf);
-       strcat(outptr, "\n");
+       strcpy(&outptr[output_len], outbuf);
+       output_len += strlen(outbuf);
+
+       /* Strip leading/trailing whitespace.  We can't do this with
+        * striplt() because it uses too many strlen()'s
+        */
+       while ((output_len > 0) && (isspace(outptr[0]))) {
+               strcpy(outptr, &outptr[1]);
+               --output_len;
+       }
+       while ((output_len > 0) && (isspace(outptr[output_len-1]))) {
+               outptr[output_len-1] = 0;
+               --output_len;
+       }
+
+       if (outptr[output_len-1] != '\n') {
+               strcat(outptr, "\n");
+               ++output_len;
+       }
 
        return outptr;
 
 }
-