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