Replace getcpy() and strdup() with mh_xstrdup()
[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         /*
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         /* read folder and create message structure */
153         if (!(mp = folder_read(folder)))
154                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
155
156         /* check for empty folder */
157         if (mp->nummsg == 0)
158                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
159
160         /* parse all the message ranges/sequences and set SELECTED */
161         for (msgnum = 0; msgnum < msgs.size; msgnum++)
162                 if (!m_convert(mp, msgs.msgs[msgnum]))
163                         exit(EX_USAGE);
164         seq_setprev(mp);  /* set the Previous-Sequence */
165
166         context_replace(curfolder, folder);  /* update current folder */
167         seq_save(mp);  /* synchronize message sequences */
168         context_save();  /* save the context file */
169
170         /*
171         ** Get the sequence number for each `unseen' sequence
172         */
173         if (!(cp = context_find(usequence))) {
174                 cp = seq_unseen;  /* use default, if not set */
175         }
176         if (*cp) {
177                 char **ap, *dp;
178
179                 dp = mh_xstrdup(cp);
180                 ap = brkstring(dp, " ", "\n");
181                 for (i = 0; ap && *ap; i++, ap++) {
182                         seqnum[i] = seq_getnum(mp, *ap);
183                 }
184                 num_unseen_seq = i;
185                 if (dp) {
186                         mh_free0(&dp);
187                 }
188         }
189
190         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
191                 if (is_selected(mp, msgnum)) {
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         }
225
226         folder_free(mp);  /* free folder/message structure */
227
228         return 0;
229 }