exit strfcpy, only use m_strcpy.
[apps/madmutt.git] / status.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
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
17
18 #include "mutt.h"
19 #include "mutt_menu.h"
20 #include "mutt_curses.h"
21 #include "sort.h"
22 #include "mapping.h"
23 #include "mx.h"
24 #include "buffy.h"
25
26
27 #include <string.h>
28 #include <ctype.h>
29 #include <unistd.h>
30
31 #define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
32
33 static char *get_sort_str (char *buf, size_t buflen, int method)
34 {
35   snprintf (buf, buflen, "%s%s%s",
36             (method & SORT_REVERSE) ? "reverse-" : "",
37             (method & SORT_LAST) ? "last-" : "",
38             mutt_getnamebyvalue (method & SORT_MASK, SortMethods));
39   return buf;
40 }
41
42 /* %b = number of incoming folders with unread messages [option]
43  * %B = short mailbox path
44  * %d = number of deleted messages [option]
45  * %f = full mailbox path
46  * %F = number of flagged messages [option]
47  * %h = hostname
48  * %l = length of mailbox (in bytes) [option]
49  * %m = total number of messages [option]
50  * %M = number of messages shown (virutal message count when limiting) [option]
51  * %n = number of new messages [option]
52  * %p = number of postponed messages [option]
53  * %P = percent of way through index
54  * %r = readonly/wontwrite/changed flag
55  * %s = current sorting method ($sort)
56  * %S = current aux sorting method ($sort_aux)
57  * %t = # of tagged messages [option]
58  * %v = Mutt-ng version 
59  * %V = currently active limit pattern [option] */
60 static const char *status_format_str (char *buf, size_t buflen, char op,
61                                       const char *src, const char *prefix,
62                                       const char *ifstring,
63                                       const char *elsestring,
64                                       unsigned long data, format_flag flags)
65 {
66   char fmt[SHORT_STRING], tmp[SHORT_STRING];
67   const char *cp, *p;
68   int count, optional = (flags & M_FORMAT_OPTIONAL);
69   MUTTMENU *menu = (MUTTMENU *) data;
70
71   *buf = 0;
72   switch (op) {
73   case 'b':
74     if (!optional) {
75       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
76       snprintf (buf, buflen, fmt, buffy_check (0));
77     }
78     else if (!buffy_check (0))
79       optional = 0;
80     break;
81
82   case 'B':
83     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
84 #ifdef USE_COMPRESSED
85     if (Context && Context->compressinfo && Context->realpath) {
86       if ((p = strrchr (Context->realpath, '/')))
87         m_strcpy(tmp, sizeof(tmp), p + 1);
88       else
89         m_strcpy(tmp, sizeof(tmp), Context->realpath);
90     }
91     else
92 #endif
93     if (Context && Context->path) {
94       if ((p = strrchr (Context->path, '/')))
95         m_strcpy(tmp, sizeof(tmp), p + 1);
96       else
97         m_strcpy(tmp, sizeof(tmp), Context->path);
98     }
99     else
100       m_strcpy(tmp, sizeof(tmp), _("no mailbox"));
101     snprintf (buf, buflen, fmt, tmp);
102     break;
103
104   case 'd':
105     if (!optional) {
106       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
107       snprintf (buf, buflen, fmt, Context ? Context->deleted : 0);
108     }
109     else if (!Context || !Context->deleted)
110       optional = 0;
111     break;
112
113   case 'h':
114     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
115     snprintf (buf, buflen, fmt, NONULL (Hostname));
116     break;
117
118   case 'f':
119     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
120 #ifdef USE_COMPRESSED
121     if (Context && Context->compressinfo && Context->realpath) {
122       m_strcpy(tmp, sizeof(tmp), Context->realpath);
123       mutt_pretty_mailbox (tmp);
124     }
125     else
126 #endif
127     if (Context && Context->path) {
128       m_strcpy(tmp, sizeof(tmp), Context->path);
129       mutt_pretty_mailbox (tmp);
130     }
131     else
132       m_strcpy(tmp, sizeof(tmp), _("(no mailbox)"));
133     snprintf (buf, buflen, fmt, tmp);
134     break;
135
136   case 'F':
137     if (!optional) {
138       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
139       snprintf (buf, buflen, fmt, Context ? Context->flagged : 0);
140     }
141     else if (!Context || !Context->flagged)
142       optional = 0;
143     break;
144
145   case 'l':
146     if (!optional) {
147       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
148       mutt_pretty_size (tmp, sizeof (tmp), Context ? Context->size : 0);
149       snprintf (buf, buflen, fmt, tmp);
150     }
151     else if (!Context || !Context->size)
152       optional = 0;
153     break;
154
155   case 'L':
156     if (!optional) {
157       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
158       mutt_pretty_size (tmp, sizeof (tmp), Context ? Context->vsize : 0);
159       snprintf (buf, buflen, fmt, tmp);
160     }
161     else if (!Context || !Context->pattern)
162       optional = 0;
163     break;
164
165   case 'm':
166     if (!optional) {
167       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
168       snprintf (buf, buflen, fmt, Context ? Context->msgcount : 0);
169     }
170     else if (!Context || !Context->msgcount)
171       optional = 0;
172     break;
173
174   case 'M':
175     if (!optional) {
176       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
177       snprintf (buf, buflen, fmt, Context ? Context->vcount : 0);
178     }
179     else if (!Context || !Context->pattern)
180       optional = 0;
181     break;
182
183   case 'n':
184     if (!optional) {
185       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
186       snprintf (buf, buflen, fmt, Context ? Context->new : 0);
187     }
188     else if (!Context || Context->new <= 0)
189       optional = 0;
190     break;
191
192   case 'o':
193     if (!optional) {
194       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
195       snprintf (buf, buflen, fmt,
196                 Context ? Context->unread - Context->new : 0);
197     }
198     else if (!Context || !(Context->unread - Context->new))
199       optional = 0;
200     break;
201
202   case 'p':
203     count = mutt_num_postponed (0);
204     if (!optional) {
205       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
206       snprintf (buf, buflen, fmt, count);
207     }
208     else if (!count)
209       optional = 0;
210     break;
211
212   case 'P':
213     if (menu->top + menu->pagelen >= menu->max)
214       cp = menu->top ? "end" : "all";
215     else {
216       count = (100 * (menu->top + menu->pagelen)) / menu->max;
217       snprintf (tmp, sizeof (tmp), "%d%%", count);
218       cp = tmp;
219     }
220     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
221     snprintf (buf, buflen, fmt, cp);
222     break;
223
224   case 'r':
225     {
226       int i = 0;
227
228       if (Context) {
229         i = option (OPTATTACHMSG) ? 3 : ((Context->readonly ||
230                                           Context->dontwrite) ? 2 : (Context->
231                                                                      changed
232                                                                      || (
233 #ifdef USE_IMAP
234                                                                              /* deleted doesn't necessarily mean changed in IMAP */
235                                                                              Context->
236                                                                              magic
237                                                                              !=
238                                                                              M_IMAP
239                                                                              &&
240 #endif
241                                                                              Context->
242                                                                              deleted))
243                                          ? 1 : 0);
244       }
245
246       if (!StChars)
247         buf[0] = 0;
248       else if (i >= m_strlen(StChars))
249         buf[0] = StChars[0];
250       else
251         buf[0] = StChars[i];
252
253       buf[1] = 0;
254       break;
255     }
256
257   case 's':
258     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
259     snprintf (buf, buflen, fmt, get_sort_str (tmp, sizeof (tmp), Sort));
260     break;
261
262   case 'S':
263     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
264     snprintf (buf, buflen, fmt, get_sort_str (tmp, sizeof (tmp), SortAux));
265     break;
266
267   case 't':
268     if (!optional) {
269       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
270       snprintf (buf, buflen, fmt, Context ? Context->tagged : 0);
271     }
272     else if (!Context || !Context->tagged)
273       optional = 0;
274     break;
275
276   case 'u':
277     if (!optional) {
278       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
279       snprintf (buf, buflen, fmt, Context ? Context->unread : 0);
280     }
281     else if (!Context || !Context->unread)
282       optional = 0;
283     break;
284
285   case 'v':
286     m_strcpy(buf, buflen, mutt_make_version(0));
287     break;
288
289   case 'V':
290     if (!optional) {
291       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
292       snprintf (buf, buflen, fmt,
293                 (Context && Context->pattern) ? Context->pattern : "");
294     }
295     else if (!Context || !Context->pattern)
296       optional = 0;
297     break;
298
299   case 0:
300     *buf = 0;
301     return (src);
302
303   default:
304     snprintf (buf, buflen, "%%%s%c", prefix, op);
305     break;
306   }
307
308   if (optional)
309     menu_status_line (buf, buflen, menu, ifstring);
310   else if (flags & M_FORMAT_OPTIONAL)
311     menu_status_line (buf, buflen, menu, elsestring);
312
313   return (src);
314 }
315
316 void menu_status_line (char* buf, size_t len, MUTTMENU* menu, const char* p) {
317   /*
318    * if we have enough space for buffer, format lines to $COLS-$SidebarWidth
319    * only to not wrap past end of screen
320    */
321   int width = COLS - SW;
322   mutt_FormatString (buf, (width >= len ? len : (width + 1)),
323                      p, status_format_str,
324                      (unsigned long) menu, 0);
325 }