]> diplodocus.org Git - nmh/commitdiff
Break out the unquote functionality to a separate function.
authorKen Hornstein <kenh@pobox.com>
Thu, 14 Nov 2013 16:43:33 +0000 (11:43 -0500)
committerKen Hornstein <kenh@pobox.com>
Thu, 14 Nov 2013 16:43:33 +0000 (11:43 -0500)
Makefile.am
h/prototypes.h
sbr/fmt_scan.c
sbr/unquote.c [new file with mode: 0644]

index 1f87ac26850ed0d402805acd2b2d4b36335547f8..439ce1520b71d6c7583922b5f880cf7224941ddb 100644 (file)
@@ -547,7 +547,8 @@ sbr_libmh_a_SOURCES = sbr/addrsbr.c sbr/ambigsw.c sbr/atooi.c sbr/arglist.c \
                      sbr/seq_setcur.c sbr/seq_setprev.c sbr/seq_setunseen.c \
                      sbr/showfile.c sbr/signals.c sbr/smatch.c \
                      sbr/snprintb.c sbr/ssequal.c \
-                     sbr/strindex.c sbr/trimcpy.c sbr/uprf.c sbr/vfgets.c \
+                     sbr/strindex.c sbr/trimcpy.c sbr/unquote.c \
+                     sbr/uprf.c sbr/vfgets.c \
                      sbr/fmt_def.c sbr/mf.c sbr/utils.c sbr/ctype-checked.c \
                      sbr/m_mktemp.c sbr/getansreadline.c sbr/vector.c \
                      config/config.c config/version.c
index 57e51d7b47128b74673f91ba00f6922cf9f71bc6..6db999584ac21bce99b56ce5654979f0e87c9b6a 100644 (file)
@@ -212,6 +212,23 @@ int ssequal (char *, char *);
 int stringdex (char *, char *);
 char *trimcpy (char *);
 int unputenv (char *);
+
+/*
+ * Remove quotes and quoted-pair sequences from RFC-5322 atoms.
+ *
+ * Currently the actual algorithm is simpler than it technically should
+ * be: any quotes are simply eaten, unless they're preceded by the escape
+ * character (\).  This seems to be sufficient for our needs for now.
+ *
+ * Arguments:
+ *
+ * input       - The input string
+ * output      - The output string; is assumed to have at least as much
+ *               room as the input string.  At worst the output string will
+ *               be the same size as the input string; it might be smaller.
+ *
+ */
+void unquote_string(const char *input, char *output);
 int uprf (char *, char *);
 int vfgets (FILE *, char **);
 char *write_charset_8bit (void);
index d0bb16dfc1053eb235c44d5648c8d50d1b6a139d..408b1093a86786beea9e309077867d4b82b2078f 100644 (file)
@@ -868,32 +868,10 @@ fmt_scan (struct format *format, char *scanl, size_t max, int width, int *dat,
                /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
        case FT_LS_UNQUOTE:
            if (str) {          
-               int m;
                strncpy(buffer, str, sizeof(buffer));
                /* strncpy doesn't NUL-terminate if it fills the buffer */
                buffer[sizeof(buffer)-1] = '\0';
-               str = buffer;
-       
-               /* we will parse from buffer to buffer2 */
-               n = 0; /* n is the input position in str */
-               m = 0; /* m is the ouput position in buffer2 */
-
-               while ( str[n] != '\0') {
-                   switch ( str[n] ) {
-                       case '\\':
-                           n++;
-                           if ( str[n] != '\0')
-                               buffer2[m++] = str[n++];
-                           break;
-                       case '"':
-                           n++;
-                           break;
-                       default:
-                           buffer2[m++] = str[n++];
-                           break;
-                       }
-               }
-               buffer2[m] = '\0';
+               unquote_string(buffer, buffer2);
                str = buffer2;
             }
            break;
diff --git a/sbr/unquote.c b/sbr/unquote.c
new file mode 100644 (file)
index 0000000..f9fa911
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * unquote.c: Handle quote removal and quoted-pair strings on
+ * RFC 2822-5322 atoms.
+ *
+ * This code is Copyright (c) 2013, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
+ */
+
+#include <h/mh.h>
+
+/*
+ * Remove quotes (and handle escape strings) from RFC 5322 quoted-strings.
+ *
+ * Since we never add characters to the string, the output buffer is assumed
+ * to have at least as many characters as the input string.
+ *
+ */
+
+void
+unquote_string(const char *input, char *output)
+{
+    int n = 0; /* n is the position in the input buffer */
+    int m = 0; /* m is the position in the output buffer */
+
+    while ( input[n] != '\0') {
+       switch ( input[n] ) {
+       case '\\':
+           n++;
+           if ( input[n] != '\0')
+               output[m++] = input[n++];
+           break;
+       case '"':
+           n++;
+           break;
+       default:
+           output[m++] = input[n++];
+           break;
+       }
+    }
+
+    output[m] = '\0';
+
+    return;
+}