scan: Don't fflush manually. Let stdio care for this.
[mmh] / uip / scan.c
1 /*
2 ** scan.c -- display a one-line "scan" listing of folder or messages
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/fmt_scan.h>
11 #include <h/scansbr.h>
12 #include <h/tws.h>
13 #include <h/utils.h>
14 #include <errno.h>
15
16 static struct swit switches[] = {
17 #define FORMSW  0
18         { "form formatfile", 0 },
19 #define WIDTHSW  1
20         { "width columns", 0 },
21 #define FILESW  2
22         { "file file", 4 },
23 #define VERSIONSW 3
24         { "version", 0 },
25 #define HELPSW  4
26         { "help", 0 },
27         { NULL, 0 }
28 };
29
30
31 int
32 main(int argc, char **argv)
33 {
34         int width = 0;
35         int i, state, msgnum;
36         int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
37         char *cp, *maildir, *file = NULL, *folder = NULL;
38         char *form = NULL, buf[BUFSIZ];
39         char **argp, *nfs, **arguments;
40         struct msgs_array msgs = { 0, 0, NULL };
41         struct msgs *mp;
42         FILE *in;
43
44 #ifdef LOCALE
45         setlocale(LC_ALL, "");
46 #endif
47         invo_name = mhbasename(argv[0]);
48
49         /* read user profile/context */
50         context_read();
51
52         arguments = getarguments(invo_name, argc, argv, 1);
53         argp = arguments;
54
55         /*
56         ** Parse arguments
57         */
58         while ((cp = *argp++)) {
59                 if (*cp == '-') {
60                         switch (smatch(++cp, switches)) {
61                         case AMBIGSW:
62                                 ambigsw(cp, switches);
63                                 done(1);
64                         case UNKWNSW:
65                                 adios(NULL, "-%s unknown", cp);
66
67                         case HELPSW:
68                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
69                                 print_help(buf, switches, 1);
70                                 done(1);
71                         case VERSIONSW:
72                                 print_version(invo_name);
73                                 done(1);
74
75                         case FORMSW:
76                                 if (!(form = *argp++) || *form == '-')
77                                         adios(NULL, "missing argument to %s",
78                                                         argp[-2]);
79                                 continue;
80
81                         case WIDTHSW:
82                                 if (!(cp = *argp++) || *cp == '-')
83                                         adios(NULL, "missing argument to %s",
84                                                         argp[-2]);
85                                 width = atoi(cp);
86                                 continue;
87
88                         case FILESW:
89                                 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
90                                         adios(NULL, "missing argument to %s",
91                                                         argp[-2]);
92                                 if (strcmp(file = cp, "-")!=0)
93                                         file = getcpy(expanddir(cp));
94                                 continue;
95                         }
96                 }
97                 if (*cp == '+' || *cp == '@') {
98                         if (folder)
99                                 adios(NULL, "only one folder at a time!");
100                         else
101                                 folder = getcpy(expandfol(cp));
102                 } else
103                         app_msgarg(&msgs, cp);
104         }
105
106         /*
107         ** Get new format string.  Must be before chdir().
108         */
109         nfs = new_fs(form, FORMAT);
110
111         /*
112         ** We are scanning a maildrop file
113         */
114         if (file) {
115                 if (msgs.size)
116                         adios(NULL, "\"msgs\" not allowed with -file");
117                 if (folder)
118                         adios(NULL, "\"+folder\" not allowed with -file");
119
120                 /* check if "file" is really stdin */
121                 if (strcmp(file, "-") == 0) {
122                         in = stdin;
123                         file = "stdin";
124                 } else if (!(in = fopen(file, "r"))) {
125                         adios(file, "unable to open");
126                 }
127
128                 m_unknown(in);
129                 for (msgnum = 1; ; ++msgnum) {
130                         state = scan(in, msgnum, -1, nfs, width, 0, 0,
131                                         NULL, 0L, 1);
132                         if (state != SCNMSG && state != SCNENC)
133                                 break;
134                 }
135                 fclose(in);
136                 done(0);
137         }
138
139         /*
140         ** We are scanning a folder
141         */
142
143         if (!msgs.size)
144                 app_msgarg(&msgs, seq_all);
145         if (!folder)
146                 folder = getcurfol();
147         maildir = toabsdir(folder);
148
149         if (chdir(maildir) == NOTOK)
150                 adios(maildir, "unable to change directory to");
151
152         /* read folder and create message structure */
153         if (!(mp = folder_read(folder)))
154                 adios(NULL, "unable to read folder %s", folder);
155
156         /* check for empty folder */
157         if (mp->nummsg == 0)
158                 adios(NULL, "no messages in %s", folder);
159
160         /* parse all the message ranges/sequences and set SELECTED */
161         for (msgnum = 0; msgnum < msgs.size; msgnum++)
162                 if (!m_convert(mp, msgs.msgs[msgnum]))
163                         done(1);
164         seq_setprev(mp);  /* set the Previous-Sequence */
165
166         context_replace(curfolder, folder);  /* update current folder */
167         seq_save(mp);  /* synchronize message sequences */
168         context_save();  /* save the context file */
169
170         /*
171         ** Get the sequence number for each `unseen' sequence
172         */
173         if (!(cp = context_find(usequence))) {
174                 cp = seq_unseen;  /* use default, if not set */
175         }
176         if (*cp) {
177                 char **ap, *dp;
178
179                 dp = getcpy(cp);
180                 ap = brkstring(dp, " ", "\n");
181                 for (i = 0; ap && *ap; i++, ap++) {
182                         seqnum[i] = seq_getnum(mp, *ap);
183                 }
184                 num_unseen_seq = i;
185                 if (dp) {
186                         free(dp);
187                 }
188         }
189
190         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
191                 if (is_selected(mp, msgnum)) {
192                         if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
193                                 admonish(cp, "unable to open message");
194                                 continue;
195                         }
196
197                         /*
198                         ** Check if message is in any sequence given
199                         ** by Unseen-Sequence profile entry.
200                         */
201                         unseen = 0;
202                         for (i = 0; i < num_unseen_seq; i++) {
203                                 if (in_sequence(mp, seqnum[i], msgnum)) {
204                                         unseen = 1;
205                                         break;
206                                 }
207                         }
208
209                         switch (state = scan(in, msgnum, 0, nfs, width,
210                                                 msgnum == mp->curmsg, unseen,
211                                                 folder, 0L, 1)) {
212                         case SCNMSG:
213                         case SCNENC:
214                         case SCNERR:
215                                 break;
216
217                         default:
218                                 adios(NULL, "scan() botch(%d)", state);
219
220                         case SCNEOF:
221                                 advise(NULL, "message %d: empty", msgnum);
222                                 break;
223                         }
224                         fclose(in);
225                 }
226         }
227
228         folder_free(mp);  /* free folder/message structure */
229
230         done(0);
231         return 1;
232 }