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