continue the include dance
[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 #include <stdarg.h>
16
17 #include <lib-lib/debug.h>
18
19 #include "mutt.h"
20 #include "state.h"
21 #include "rfc3676.h"
22
23 static void state_prefix_put (const char *d, ssize_t dlen, STATE * s)
24 {
25   if (s->prefix)
26     while (dlen--)
27       state_prefix_putc (*d++, s);
28   else
29     fwrite (d, dlen, 1, s->fpout);
30 }
31
32 void mutt_convert_to_state (iconv_t cd, char *bufi, ssize_t * l, STATE * s)
33 {
34   char bufo[BUFO_SIZE];
35   const char *ib;
36   char *ob;
37   ssize_t ibl, obl;
38
39   if (!bufi) {
40     if (cd != (iconv_t) (-1)) {
41       ob = bufo, obl = sizeof (bufo);
42       my_iconv(cd, 0, 0, &ob, &obl);
43       if (ob != bufo)
44         state_prefix_put (bufo, ob - bufo, s);
45     }
46     if (Quotebuf[0] != '\0')
47       state_prefix_putc ('\n', s);
48     return;
49   }
50
51   if (cd == (iconv_t) (-1)) {
52     state_prefix_put (bufi, *l, s);
53     *l = 0;
54     return;
55   }
56
57   ib = bufi, ibl = *l;
58   for (;;) {
59     ob = bufo, obl = sizeof (bufo);
60     mutt_iconv (cd, &ib, &ibl, &ob, &obl, 0, "?");
61     if (ob == bufo)
62       break;
63     state_prefix_put (bufo, ob - bufo, s);
64   }
65   memmove (bufi, ib, ibl);
66   *l = ibl;
67 }
68
69 void state_prefix_putc (char c, STATE * s)
70 {
71   if (s->flags & M_PENDINGPREFIX) {
72     int i;
73
74     i = m_strlen(Quotebuf);
75     Quotebuf[i++] = c;
76     Quotebuf[i] = '\0';
77     if (i == sizeof (Quotebuf) - 1 || c == '\n') {
78       char buf[2 * SHORT_STRING];
79       int j = 0, offset = 0;
80       regmatch_t pmatch[1];
81 #ifdef DEBUG
82       unsigned char save = '\0';
83 #endif
84
85       state_reset_prefix (s);
86       while (regexec
87              ((regex_t *) QuoteRegexp.rx, &Quotebuf[offset], 1, pmatch,
88               0) == 0)
89         offset += pmatch->rm_eo;
90
91       if (!option (OPTQUOTEEMPTY) && Quotebuf[offset] == '\n') {
92         buf[0] = '\n';
93         buf[1] = '\0';
94       }
95       else if (!option (OPTTEXTFLOWED) && 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 (m_strlen(buf) >= 2) {
107         save = buf[m_strlen(buf) - 1];
108         buf[m_strlen(buf) - 1] = '\0';
109         debug_print (2, ("buf = '%s'\n", buf));
110         buf[m_strlen(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) && !m_strcmp(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 }