Convert inc and scan from m_getfld() to m_getfld2()
[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                 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                 exit(EX_OK);
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(EX_OSERR, maildir, "unable to change directory to");
150
151         if (!(mp = folder_read(folder)))
152                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
153
154         /* check for empty folder */
155         if (mp->nummsg == 0)
156                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
157
158         /* parse all the message ranges/sequences and set SELECTED */
159         for (msgnum = 0; msgnum < msgs.size; msgnum++)
160                 if (!m_convert(mp, msgs.msgs[msgnum]))
161                         exit(EX_USAGE);
162         seq_setprev(mp);
163
164         context_replace(curfolder, folder);
165         seq_save(mp);
166         context_save();
167
168         /*
169         ** Get the sequence number for each `unseen' sequence
170         */
171         if (!(cp = context_find(usequence))) {
172                 cp = seq_unseen;  /* use default, if not set */
173         }
174         if (*cp) {
175                 char **ap, *dp;
176
177                 dp = getcpy(cp);
178                 ap = brkstring(dp, " ", "\n");
179                 for (i = 0; ap && *ap; i++, ap++) {
180                         seqnum[i] = seq_getnum(mp, *ap);
181                 }
182                 num_unseen_seq = i;
183                 if (dp) {
184                         free(dp);
185                 }
186         }
187
188         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
189                 if (!is_selected(mp, msgnum)) {
190                         continue;
191                 }
192
193                 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
194                         admonish(cp, "unable to open message");
195                         continue;
196                 }
197
198                 /*
199                 ** Check if message is in any sequence given
200                 ** by Unseen-Sequence profile entry.
201                 */
202                 unseen = 0;
203                 for (i = 0; i < num_unseen_seq; i++) {
204                         if (in_sequence(mp, seqnum[i], msgnum)) {
205                                 unseen = 1;
206                                 break;
207                         }
208                 }
209
210                 switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
211                                 width, msgnum==mp->curmsg, unseen)) {
212                 case SCNMSG:
213                 case SCNERR:
214                         break;
215
216                 default:
217                         adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
218
219                 case SCNEOF:
220                         advise(NULL, "message %d: empty", msgnum);
221                         break;
222                 }
223                 fclose(in);
224         }
225         folder_free(mp);
226
227         return 0;
228 }