Replace done with exit at uip
[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         setlocale(LC_ALL, "");
45         invo_name = mhbasename(argv[0]);
46
47         /* read user profile/context */
48         context_read();
49
50         arguments = getarguments(invo_name, argc, argv, 1);
51         argp = arguments;
52
53         /*
54         ** Parse arguments
55         */
56         while ((cp = *argp++)) {
57                 if (*cp == '-') {
58                         switch (smatch(++cp, switches)) {
59                         case AMBIGSW:
60                                 ambigsw(cp, switches);
61                                 exit(1);
62                         case UNKWNSW:
63                                 adios(NULL, "-%s unknown", cp);
64
65                         case HELPSW:
66                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
67                                 print_help(buf, switches, 1);
68                                 exit(0);
69                         case VERSIONSW:
70                                 print_version(invo_name);
71                                 exit(0);
72
73                         case FORMSW:
74                                 if (!(form = *argp++) || *form == '-')
75                                         adios(NULL, "missing argument to %s",
76                                                         argp[-2]);
77                                 continue;
78
79                         case WIDTHSW:
80                                 if (!(cp = *argp++) || *cp == '-')
81                                         adios(NULL, "missing argument to %s",
82                                                         argp[-2]);
83                                 width = atoi(cp);
84                                 continue;
85
86                         case FILESW:
87                                 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
88                                         adios(NULL, "missing argument to %s",
89                                                         argp[-2]);
90                                 if (strcmp(file = cp, "-")!=0)
91                                         file = getcpy(expanddir(cp));
92                                 continue;
93                         }
94                 }
95                 if (*cp == '+' || *cp == '@') {
96                         if (folder)
97                                 adios(NULL, "only one folder at a time!");
98                         else
99                                 folder = getcpy(expandfol(cp));
100                 } else
101                         app_msgarg(&msgs, cp);
102         }
103
104         /*
105         ** Get new format string.  Must be before chdir().
106         */
107         fmtstr = new_fs(form, FORMAT);
108
109         /*
110         ** We are scanning a maildrop file
111         */
112         if (file) {
113                 if (msgs.size)
114                         adios(NULL, "\"msgs\" not allowed with -file");
115                 if (folder)
116                         adios(NULL, "\"+folder\" not allowed with -file");
117
118                 /* check if "file" is really stdin */
119                 if (strcmp(file, "-") == 0) {
120                         in = stdin;
121                         file = "stdin";
122                 } else if (!(in = fopen(file, "r"))) {
123                         adios(file, "unable to open");
124                 }
125
126                 thisisanmbox(in);
127                 for (msgnum = 1; ; ++msgnum) {
128                         state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
129                         if (state != SCNMSG)
130                                 break;
131                 }
132                 fclose(in);
133                 exit(0);
134         }
135
136         /*
137         ** We are scanning a folder
138         */
139
140         if (!msgs.size)
141                 app_msgarg(&msgs, seq_all);
142         if (!folder)
143                 folder = getcurfol();
144         maildir = toabsdir(folder);
145
146         if (chdir(maildir) == NOTOK)
147                 adios(maildir, "unable to change directory to");
148
149         /* read folder and create message structure */
150         if (!(mp = folder_read(folder)))
151                 adios(NULL, "unable to read folder %s", folder);
152
153         /* check for empty folder */
154         if (mp->nummsg == 0)
155                 adios(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(1);
161         seq_setprev(mp);  /* set the Previous-Sequence */
162
163         context_replace(curfolder, folder);  /* update current folder */
164         seq_save(mp);  /* synchronize message sequences */
165         context_save();  /* save the context file */
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 = getcpy(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                         free(dp);
184                 }
185         }
186
187         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
188                 if (is_selected(mp, msgnum)) {
189                         if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
190                                 admonish(cp, "unable to open message");
191                                 continue;
192                         }
193
194                         /*
195                         ** Check if message is in any sequence given
196                         ** by Unseen-Sequence profile entry.
197                         */
198                         unseen = 0;
199                         for (i = 0; i < num_unseen_seq; i++) {
200                                 if (in_sequence(mp, seqnum[i], msgnum)) {
201                                         unseen = 1;
202                                         break;
203                                 }
204                         }
205
206                         switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
207                                         width, msgnum==mp->curmsg, unseen)) {
208                         case SCNMSG:
209                         case SCNERR:
210                                 break;
211
212                         default:
213                                 adios(NULL, "scan() botch(%d)", state);
214
215                         case SCNEOF:
216                                 advise(NULL, "message %d: empty", msgnum);
217                                 break;
218                         }
219                         fclose(in);
220                 }
221         }
222
223         folder_free(mp);  /* free folder/message structure */
224
225         return 0;
226 }