Don't require a length for switch prefixes without good reason.
[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", 0 },
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, *fmtstr, **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         fmtstr = 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                 thisisanmbox(in);
129                 for (msgnum = 1; ; ++msgnum) {
130                         state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
131                         if (state != SCNMSG)
132                                 break;
133                 }
134                 fclose(in);
135                 done(0);
136         }
137
138         /*
139         ** We are scanning a folder
140         */
141
142         if (!msgs.size)
143                 app_msgarg(&msgs, seq_all);
144         if (!folder)
145                 folder = getcurfol();
146         maildir = toabsdir(folder);
147
148         if (chdir(maildir) == NOTOK)
149                 adios(maildir, "unable to change directory to");
150
151         /* read folder and create message structure */
152         if (!(mp = folder_read(folder)))
153                 adios(NULL, "unable to read folder %s", folder);
154
155         /* check for empty folder */
156         if (mp->nummsg == 0)
157                 adios(NULL, "no messages in %s", folder);
158
159         /* parse all the message ranges/sequences and set SELECTED */
160         for (msgnum = 0; msgnum < msgs.size; msgnum++)
161                 if (!m_convert(mp, msgs.msgs[msgnum]))
162                         done(1);
163         seq_setprev(mp);  /* set the Previous-Sequence */
164
165         context_replace(curfolder, folder);  /* update current folder */
166         seq_save(mp);  /* synchronize message sequences */
167         context_save();  /* save the context file */
168
169         /*
170         ** Get the sequence number for each `unseen' sequence
171         */
172         if (!(cp = context_find(usequence))) {
173                 cp = seq_unseen;  /* use default, if not set */
174         }
175         if (*cp) {
176                 char **ap, *dp;
177
178                 dp = getcpy(cp);
179                 ap = brkstring(dp, " ", "\n");
180                 for (i = 0; ap && *ap; i++, ap++) {
181                         seqnum[i] = seq_getnum(mp, *ap);
182                 }
183                 num_unseen_seq = i;
184                 if (dp) {
185                         free(dp);
186                 }
187         }
188
189         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
190                 if (is_selected(mp, msgnum)) {
191                         if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
192                                 admonish(cp, "unable to open message");
193                                 continue;
194                         }
195
196                         /*
197                         ** Check if message is in any sequence given
198                         ** by Unseen-Sequence profile entry.
199                         */
200                         unseen = 0;
201                         for (i = 0; i < num_unseen_seq; i++) {
202                                 if (in_sequence(mp, seqnum[i], msgnum)) {
203                                         unseen = 1;
204                                         break;
205                                 }
206                         }
207
208                         switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
209                                         width, msgnum==mp->curmsg, unseen)) {
210                         case SCNMSG:
211                         case SCNERR:
212                                 break;
213
214                         default:
215                                 adios(NULL, "scan() botch(%d)", state);
216
217                         case SCNEOF:
218                                 advise(NULL, "message %d: empty", msgnum);
219                                 break;
220                         }
221                         fclose(in);
222                 }
223         }
224
225         folder_free(mp);  /* free folder/message structure */
226
227         done(0);
228         return 1;
229 }