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