and less size_t's
[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 <lib-lib/debug.h>
17
18 #include "mutt.h"
19 #include "state.h"
20 #include "rfc3676.h"
21
22 static void state_prefix_put (const char *d, ssize_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, ssize_t * l, STATE * s)
32 {
33   char bufo[BUFO_SIZE];
34   const char *ib;
35   char *ob;
36   ssize_t ibl, obl;
37
38   if (!bufi) {
39     if (cd != (iconv_t) (-1)) {
40       ob = bufo, obl = sizeof (bufo);
41       my_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 = m_strlen(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       }
94       else if (!option (OPTTEXTFLOWED) && option (OPTQUOTEQUOTED) && offset) {
95         for (i = 0; i < offset; i++)
96           if (Quotebuf[i] != ' ')
97             j = i;
98         strncpy (buf, Quotebuf, j + 1);
99         strcpy (buf + j + 1, Quotebuf + j);
100       }
101       else
102         snprintf (buf, sizeof (buf), "%s%s", NONULL (s->prefix), Quotebuf);
103
104 #ifdef DEBUG
105       if (m_strlen(buf) >= 2) {
106         save = buf[m_strlen(buf) - 1];
107         buf[m_strlen(buf) - 1] = '\0';
108         debug_print (2, ("buf = '%s'\n", buf));
109         buf[m_strlen(buf)] = save;
110       }
111 #endif
112
113       state_puts (buf, s);
114     }
115   }
116   else
117     state_putc (c, s);
118
119   if (c == '\n') {
120     state_set_prefix (s);
121     Quotebuf[0] = '\0';
122   }
123 }
124
125 int state_printf (STATE * s, const char *fmt, ...)
126 {
127   int rv;
128   va_list ap;
129
130   va_start (ap, fmt);
131   rv = vfprintf (s->fpout, fmt, ap);
132   va_end (ap);
133
134   return rv;
135 }
136
137 void state_mark_attach (STATE * s)
138 {
139   if ((s->flags & M_DISPLAY) && !m_strcmp(Pager, "builtin"))
140     state_puts (AttachmentMarker, s);
141 }
142
143 void state_attach_puts (const char *t, STATE * s)
144 {
145   if (*t != '\n')
146     state_mark_attach (s);
147   while (*t) {
148     state_putc (*t, s);
149     if (*t++ == '\n' && *t)
150       if (*t != '\n')
151         state_mark_attach (s);
152   }
153 }