[bug #4302] errno is not always an extern int
[mmh] / uip / anno.c
1
2 /*
3  * anno.c -- annotate messages
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  *
11  *      Four new options have been added: delete, list, number, and draft.
12  *      Message header fields are used by the new MIME attachment code in
13  *      the send command.  Adding features to generalize the anno command
14  *      seemed to be a better approach than the creation of a new command
15  *      whose features would overlap with those of the anno command.
16  *
17  *      The -draft option causes anno to operate on the current draft file
18  *      instead of on a message sequence.
19  *
20  *      The -delete option deletes header elements that match the -component
21  *      field name.  If -delete is used without the -text option, the first
22  *      header field whose field name matches the component name is deleted.
23  *      If the -delete is used with the -text option, and the -text argument
24  *      begins with a /, the first header field whose field name matches the
25  *      component name and whose field body matches the text is deleted.  If
26  *      the -text argument does not begin with a /, then the text is assumed
27  *      to be the last component of a path name, and the first header field
28  *      whose field name matches the component name and a field body whose
29  *      last path name component matches the text is deleted.  If the -delete
30  *      option is used with the new -number option described below, the nth
31  *      header field whose field name matches the component name is deleted.
32  *      No header fields are deleted if none of the above conditions are met.
33  *
34  *      The -list option outputs the field bodies from each header field whose
35  *      field name matches the component name, one per line.  If no -text
36  *      option is specified, only the last path name component of each field
37  *      body is output.  The entire field body is output if the -text option
38  *      is used; the contents of the -text argument are ignored.  If the -list
39  *      option is used in conjuction with the new -number option described
40  *      below, each line is numbered starting with 1.  A tab separates the
41  *      number from the field body.
42  *
43  *      The -number option works with both the -delete and -list options as
44  *      described above.  The -number option takes an optional argument.  A
45  *      value of 1 is assumed if this argument is absent.
46  */
47
48 #include <h/mh.h>
49
50 /*
51  * We allocate space for messages (msgs array)
52  * this number of elements at a time.
53  */
54 #define MAXMSGS  256
55
56
57 static struct swit switches[] = {
58 #define COMPSW  0
59     { "component field", 0 },
60 #define INPLSW  1
61     { "inplace", 0 },
62 #define NINPLSW 2
63     { "noinplace", 0 },
64 #define DATESW  3
65     { "date", 0 },
66 #define NDATESW 4
67     { "nodate", 0 },
68 #define TEXTSW  5
69     { "text body", 0 },
70 #define VERSIONSW 6
71     { "version", 0 },
72 #define HELPSW  7
73     { "help", 0 },
74 #define DRFTSW                   8
75     { "draft", 2 },
76 #define LISTSW                   9
77     { "list", 1 },
78 #define DELETESW                10
79     { "delete", 2 },
80 #define NUMBERSW                11
81     { "number", 2 },
82 #define APPENDSW                12
83     { "append", 1 },
84     { NULL, 0 }
85 };
86
87 /*
88  * static prototypes
89  */
90 static void make_comp (char **);
91
92
93 int
94 main (int argc, char **argv)
95 {
96     int inplace = 1, datesw = 1;
97     int nummsgs, maxmsgs, msgnum;
98     char *cp, *maildir, *comp = NULL;
99     char *text = NULL, *folder = NULL, buf[BUFSIZ];
100     char **argp, **arguments, **msgs;
101     struct msgs *mp;
102     int         append = 0;             /* append annotations instead of default prepend */
103     int         delete = -1;            /* delete header element if set */
104     char        *draft = (char *)0;     /* draft file name */
105     int         isdf = 0;               /* return needed for m_draft() */
106     int         list = 0;               /* list header elements if set */
107     int         number = 0;             /* delete specific number of like elements if set */
108
109 #ifdef LOCALE
110     setlocale(LC_ALL, "");
111 #endif
112     invo_name = r1bindex (argv[0], '/');
113
114     /* read user profile/context */
115     context_read();
116
117     arguments = getarguments (invo_name, argc, argv, 1);
118     argp = arguments;
119
120     /*
121      * Allocate the initial space to record message
122      * names and ranges.
123      */
124     nummsgs = 0;
125     maxmsgs = MAXMSGS;
126     if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
127         adios (NULL, "unable to allocate storage");
128
129     while ((cp = *argp++)) {
130         if (*cp == '-') {
131             switch (smatch (++cp, switches)) {
132                 case AMBIGSW:
133                     ambigsw (cp, switches);
134                     done (1);
135                 case UNKWNSW:
136                     adios (NULL, "-%s unknown", cp);
137
138                 case HELPSW:
139                     snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
140                         invo_name);
141                     print_help (buf, switches, 1);
142                     done (1);
143                 case VERSIONSW:
144                     print_version(invo_name);
145                     done (1);
146
147                 case COMPSW:
148                     if (comp)
149                         adios (NULL, "only one component at a time!");
150                     if (!(comp = *argp++) || *comp == '-')
151                         adios (NULL, "missing argument to %s", argp[-2]);
152                     continue;
153
154                 case DATESW:
155                     datesw++;
156                     continue;
157                 case NDATESW:
158                     datesw = 0;
159                     continue;
160
161                 case INPLSW:
162                     inplace++;
163                     continue;
164                 case NINPLSW:
165                     inplace = 0;
166                     continue;
167
168                 case TEXTSW:
169                     if (text)
170                         adios (NULL, "only one body at a time!");
171                     if (!(text = *argp++) || *text == '-')
172                         adios (NULL, "missing argument to %s", argp[-2]);
173                     continue;
174
175                 case DELETESW:          /* delete annotations */
176                     delete = 0;
177                     continue;
178
179                 case DRFTSW:            /* draft message specified */
180                     draft = "";
181                     continue;
182
183                 case LISTSW:            /* produce a listing */
184                     list = 1;
185                     continue;
186
187                 case NUMBERSW:          /* number listing or delete by number */
188                     if (number != 0)
189                         adios (NULL, "only one number at a time!");
190
191                     if (argp - arguments == argc - 1 || **argp == '-')
192                         number = 1;
193
194                     else if (!(number = atoi(*argp++)))
195                         adios (NULL, "missing argument to %s", argp[-2]);
196
197                     delete = number;
198                     continue;
199
200                 case APPENDSW:          /* append annotations instead of default prepend */
201                     append = 1;
202                     continue;
203             }
204         }
205         if (*cp == '+' || *cp == '@') {
206             if (folder)
207                 adios (NULL, "only one folder at a time!");
208             else
209                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
210         } else {
211             /*
212              * Check if we need to allocate more space
213              * for message name/ranges.
214              */
215             if (nummsgs >= maxmsgs) {
216                 maxmsgs += MAXMSGS;
217                 if (!(msgs = (char **) realloc (msgs,
218                         (size_t) (maxmsgs * sizeof(*msgs)))))
219                     adios (NULL, "unable to reallocate msgs storage");
220             }
221             msgs[nummsgs++] = cp;
222         }
223     }
224
225     /*
226      *  We're dealing with the draft message instead of message numbers.
227      *  Get the name of the draft and deal with it just as we do with
228      *  message numbers below.
229      */
230
231     if (draft != (char *)0) {
232         if (nummsgs != 0)
233             adios(NULL, "can only have message numbers or -draft.");
234
235         draft = getcpy(m_draft(folder, (char *)0, 1, &isdf));
236
237         make_comp(&comp);
238
239         if (list)
240             annolist(draft, comp, text, number);
241         else
242             annotate (draft, comp, text, inplace, datesw, delete, append);
243
244         return (done(0));
245     }
246
247 #ifdef UCI
248     if (strcmp(invo_name, "fanno") == 0)        /* ugh! */
249         datesw = 0;
250 #endif  /* UCI */
251
252     if (!context_find ("path"))
253         free (path ("./", TFOLDER));
254     if (!nummsgs)
255         msgs[nummsgs++] = "cur";
256     if (!folder)
257         folder = getfolder (1);
258     maildir = m_maildir (folder);
259
260     if (chdir (maildir) == NOTOK)
261         adios (maildir, "unable to change directory to");
262
263     /* read folder and create message structure */
264     if (!(mp = folder_read (folder)))
265         adios (NULL, "unable to read folder %s", folder);
266
267     /* check for empty folder */
268     if (mp->nummsg == 0)
269         adios (NULL, "no messages in %s", folder);
270
271     /* parse all the message ranges/sequences and set SELECTED */
272     for (msgnum = 0; msgnum < nummsgs; msgnum++)
273         if (!m_convert (mp, msgs[msgnum]))
274             done (1);
275
276     make_comp (&comp);
277
278     /* annotate all the SELECTED messages */
279     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
280         if (is_selected(mp, msgnum)) {
281             if (list)
282                 annolist(m_name(msgnum), comp, text, number);
283             else
284                 annotate (m_name (msgnum), comp, text, inplace, datesw, delete, append);
285         }
286     }
287
288     context_replace (pfolder, folder);  /* update current folder  */
289     seq_setcur (mp, mp->lowsel);        /* update current message */
290     seq_save (mp);      /* synchronize message sequences */
291     folder_free (mp);   /* free folder/message structure */
292     context_save ();    /* save the context file         */
293     return done (0);
294 }
295
296 static void
297 make_comp (char **ap)
298 {
299     register char *cp;
300     char buffer[BUFSIZ];
301
302     if (*ap == NULL) {
303         printf ("Enter component name: ");
304         fflush (stdout);
305
306         if (fgets (buffer, sizeof buffer, stdin) == NULL)
307             done (1);
308         *ap = trimcpy (buffer);
309     }
310
311     if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
312         *cp = 0;
313     if (strlen (*ap) == 0)
314         adios (NULL, "null component name");
315     if (**ap == '-')
316         adios (NULL, "invalid component name %s", *ap);
317     if (strlen (*ap) >= NAMESZ)
318         adios (NULL, "too large component name %s", *ap);
319
320     for (cp = *ap; *cp; cp++)
321         if (!isalnum (*cp) && *cp != '-')
322             adios (NULL, "invalid component name %s", *ap);
323 }