Use continue instead of one long if for the whole loop body
[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 #include <unistd.h>
16 #include <locale.h>
17 #include <sysexits.h>
18
19 static struct swit switches[] = {
20 #define FORMSW  0
21         { "form formatfile", 0 },
22 #define WIDTHSW  1
23         { "width columns", 0 },
24 #define FILESW  2
25         { "file file", 0 },
26 #define VERSIONSW 3
27         { "Version", 0 },
28 #define HELPSW  4
29         { "help", 0 },
30         { NULL, 0 }
31 };
32
33
34 int
35 main(int argc, char **argv)
36 {
37         int width = 0;
38         int i, state, msgnum;
39         int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
40         char *cp, *maildir, *file = NULL, *folder = NULL;
41         char *form = NULL, buf[BUFSIZ];
42         char **argp, *fmtstr, **arguments;
43         struct msgs_array msgs = { 0, 0, NULL };
44         struct msgs *mp;
45         FILE *in;
46
47         setlocale(LC_ALL, "");
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                                 exit(EX_USAGE);
65                         case UNKWNSW:
66                                 adios(EX_USAGE, 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                                 exit(argc == 2 ? EX_OK : EX_USAGE);
72                         case VERSIONSW:
73                                 print_version(invo_name);
74                                 exit(argc == 2 ? EX_OK : EX_USAGE);
75
76                         case FORMSW:
77                                 if (!(form = *argp++) || *form == '-')
78                                         adios(EX_USAGE, NULL, "missing argument to %s",
79                                                         argp[-2]);
80                                 continue;
81
82                         case WIDTHSW:
83                                 if (!(cp = *argp++) || *cp == '-')
84                                         adios(EX_USAGE, 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(EX_USAGE, 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(EX_USAGE, 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         fmtstr = new_fs(form, FORMAT);
111
112         /*
113         ** We are scanning a maildrop file
114         */
115         if (file) {
116                 if (msgs.size)
117                         adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
118                 if (folder)
119                         adios(EX_USAGE, 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(EX_IOERR, file, "unable to open");
127                 }
128
129                 thisisanmbox(in);
130                 for (msgnum = 1; ; ++msgnum) {
131                         state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
132                         if (state != SCNMSG)
133                                 break;
134                 }
135                 fclose(in);
136                 exit(EX_OK);
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(EX_OSERR, maildir, "unable to change directory to");
151
152         if (!(mp = folder_read(folder)))
153                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
154
155         /* check for empty folder */
156         if (mp->nummsg == 0)
157                 adios(EX_DATAERR, 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                         exit(EX_USAGE);
163         seq_setprev(mp);
164
165         context_replace(curfolder, folder);
166         seq_save(mp);
167         context_save();
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                         continue;
192                 }
193
194                 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
195                         admonish(cp, "unable to open message");
196                         continue;
197                 }
198
199                 /*
200                 ** Check if message is in any sequence given
201                 ** by Unseen-Sequence profile entry.
202                 */
203                 unseen = 0;
204                 for (i = 0; i < num_unseen_seq; i++) {
205                         if (in_sequence(mp, seqnum[i], msgnum)) {
206                                 unseen = 1;
207                                 break;
208                         }
209                 }
210
211                 switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
212                                 width, msgnum==mp->curmsg, unseen)) {
213                 case SCNMSG:
214                 case SCNERR:
215                         break;
216
217                 default:
218                         adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
219
220                 case SCNEOF:
221                         advise(NULL, "message %d: empty", msgnum);
222                         break;
223                 }
224                 fclose(in);
225         }
226         folder_free(mp);
227
228         return 0;
229 }