Add %(unmailto) format function for List-Post headers
[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 = mh_xstrdup(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 = mh_xstrdup(expandfol(cp));
103                 } else
104                         app_msgarg(&msgs, cp);
105         }
106
107         /* Set format string.  Must be before chdir(). */
108         fmtstr = new_fs(form, scanformat);
109
110         /*
111         ** We are scanning a maildrop file
112         */
113         if (file) {
114                 if (msgs.size)
115                         adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
116                 if (folder)
117                         adios(EX_USAGE, NULL, "\"+folder\" not allowed with -file");
118
119                 /* check if "file" is really stdin */
120                 if (strcmp(file, "-") == 0) {
121                         in = stdin;
122                         file = "stdin";
123                 } else if (!(in = fopen(file, "r"))) {
124                         adios(EX_IOERR, file, "unable to open");
125                 }
126
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(EX_OK);
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(EX_OSERR, maildir, "unable to change directory to");
148
149         if (!(mp = folder_read(folder)))
150                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
151
152         /* check for empty folder */
153         if (mp->nummsg == 0)
154                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
155
156         /* parse all the message ranges/sequences and set SELECTED */
157         for (msgnum = 0; msgnum < msgs.size; msgnum++)
158                 if (!m_convert(mp, msgs.msgs[msgnum]))
159                         exit(EX_USAGE);
160         seq_setprev(mp);
161
162         context_replace(curfolder, folder);
163         seq_save(mp);
164         context_save();
165
166         /*
167         ** Get the sequence number for each `unseen' sequence
168         */
169         if (!(cp = context_find(usequence))) {
170                 cp = seq_unseen;  /* use default, if not set */
171         }
172         if (*cp) {
173                 char **ap, *dp;
174
175                 dp = mh_xstrdup(cp);
176                 ap = brkstring(dp, " ", "\n");
177                 for (i = 0; ap && *ap; i++, ap++) {
178                         seqnum[i] = seq_getnum(mp, *ap);
179                 }
180                 num_unseen_seq = i;
181                 if (dp) {
182                         mh_free0(&dp);
183                 }
184         }
185
186         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
187                 if (!is_selected(mp, msgnum)) {
188                         continue;
189                 }
190
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(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
216
217                 case SCNEOF:
218                         advise(NULL, "message %d: empty", msgnum);
219                         break;
220                 }
221                 fclose(in);
222         }
223         folder_free(mp);
224
225         return 0;
226 }