Rocco Rutte:
[apps/madmutt.git] / state.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9 #if HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdlib.h>
14 #include <stdio.h>
15
16 #include "mutt.h"
17 #include "state.h"
18 #include "rfc3676.h"
19
20 #include "lib/debug.h"
21
22 static void state_prefix_put (const char *d, size_t dlen, STATE * s)
23 {
24   if (s->prefix)
25     while (dlen--)
26       state_prefix_putc (*d++, s);
27   else
28     fwrite (d, dlen, 1, s->fpout);
29 }
30
31 void mutt_convert_to_state (iconv_t cd, char *bufi, size_t * l, STATE * s)
32 {
33   char bufo[BUFO_SIZE];
34   ICONV_CONST char *ib;
35   char *ob;
36   size_t ibl, obl;
37
38   if (!bufi) {
39     if (cd != (iconv_t) (-1)) {
40       ob = bufo, obl = sizeof (bufo);
41       iconv (cd, 0, 0, &ob, &obl);
42       if (ob != bufo)
43         state_prefix_put (bufo, ob - bufo, s);
44     }
45     if (Quotebuf[0] != '\0')
46       state_prefix_putc ('\n', s);
47     return;
48   }
49
50   if (cd == (iconv_t) (-1)) {
51     state_prefix_put (bufi, *l, s);
52     *l = 0;
53     return;
54   }
55
56   ib = bufi, ibl = *l;
57   for (;;) {
58     ob = bufo, obl = sizeof (bufo);
59     mutt_iconv (cd, &ib, &ibl, &ob, &obl, 0, "?");
60     if (ob == bufo)
61       break;
62     state_prefix_put (bufo, ob - bufo, s);
63   }
64   memmove (bufi, ib, ibl);
65   *l = ibl;
66 }
67
68 void state_prefix_putc (char c, STATE * s)
69 {
70   if (s->flags & M_PENDINGPREFIX) {
71     int i;
72
73     i = str_len (Quotebuf);
74     Quotebuf[i++] = c;
75     Quotebuf[i] = '\0';
76     if (i == sizeof (Quotebuf) - 1 || c == '\n') {
77       char buf[2 * SHORT_STRING];
78       int j = 0, offset = 0;
79       regmatch_t pmatch[1];
80 #ifdef DEBUG
81       unsigned char save = '\0';
82 #endif
83
84       state_reset_prefix (s);
85       while (regexec
86              ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch,
87               0) == 0)
88         offset += pmatch->rm_eo;
89
90       if (!option (OPTQUOTEEMPTY) && Quotebuf[offset] == '\n') {
91         buf[0] = '\n';
92         buf[1] = '\0';
93       } else if (option (OPTTEXTFLOWED))
94         rfc3676_quote_line (s, buf, sizeof (buf), Quotebuf);
95       else if (option (OPTQUOTEQUOTED) && offset) {
96         for (i = 0; i < offset; i++)
97           if (Quotebuf[i] != ' ')
98             j = i;
99         strncpy (buf, Quotebuf, j + 1);
100         strcpy (buf + j + 1, Quotebuf + j);
101       }
102       else
103         snprintf (buf, sizeof (buf), "%s%s", NONULL (s->prefix), Quotebuf);
104
105 #ifdef DEBUG
106       if (str_len (buf) >= 2) {
107         save = buf[str_len (buf) - 1];
108         buf[str_len (buf) - 1] = '\0';
109         debug_print (2, ("buf = '%s'\n", buf));
110         buf[str_len (buf)] = save;
111       }
112 #endif
113
114       state_puts (buf, s);
115     }
116   }
117   else
118     state_putc (c, s);
119
120   if (c == '\n') {
121     state_set_prefix (s);
122     Quotebuf[0] = '\0';
123   }
124 }
125
126 int state_printf (STATE * s, const char *fmt, ...)
127 {
128   int rv;
129   va_list ap;
130
131   va_start (ap, fmt);
132   rv = vfprintf (s->fpout, fmt, ap);
133   va_end (ap);
134
135   return rv;
136 }
137
138 void state_mark_attach (STATE * s)
139 {
140   if ((s->flags & M_DISPLAY) && !str_cmp (Pager, "builtin"))
141     state_puts (AttachmentMarker, s);
142 }
143
144 void state_attach_puts (const char *t, STATE * s)
145 {
146   if (*t != '\n')
147     state_mark_attach (s);
148   while (*t) {
149     state_putc (*t, s);
150     if (*t++ == '\n' && *t)
151       if (*t != '\n')
152         state_mark_attach (s);
153   }
154 }