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