c318a2278b579cf36328bb12d9f70029abe75f0d
[mmh] / uip / anno.c
1
2 /*
3  * anno.c -- annotate messages
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9
10 /*
11  * We allocate space for messages (msgs array)
12  * this number of elements at a time.
13  */
14 #define MAXMSGS  256
15
16
17 static struct swit switches[] = {
18 #define COMPSW  0
19     { "component field", 0 },
20 #define INPLSW  1
21     { "inplace", 0 },
22 #define NINPLSW 2
23     { "noinplace", 0 },
24 #define DATESW  3
25     { "date", 0 },
26 #define NDATESW 4
27     { "nodate", 0 },
28 #define TEXTSW  5
29     { "text body", 0 },
30 #define VERSIONSW 6
31     { "version", 0 },
32 #define HELPSW  7
33     { "help", 4 },
34     { NULL, 0 }
35 };
36
37 /*
38  * static prototypes
39  */
40 static void make_comp (char **);
41
42
43 int
44 main (int argc, char **argv)
45 {
46     int inplace = 1, datesw = 1;
47     int nummsgs, maxmsgs, msgnum;
48     char *cp, *maildir, *comp = NULL;
49     char *text = NULL, *folder = NULL, buf[BUFSIZ];
50     char **argp, **arguments, **msgs;
51     struct msgs *mp;
52
53 #ifdef LOCALE
54     setlocale(LC_ALL, "");
55 #endif
56     invo_name = r1bindex (argv[0], '/');
57
58     /* read user profile/context */
59     context_read();
60
61     arguments = getarguments (invo_name, argc, argv, 1);
62     argp = arguments;
63
64     /*
65      * Allocate the initial space to record message
66      * names and ranges.
67      */
68     nummsgs = 0;
69     maxmsgs = MAXMSGS;
70     if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
71         adios (NULL, "unable to allocate storage");
72
73     while ((cp = *argp++)) {
74         if (*cp == '-') {
75             switch (smatch (++cp, switches)) {
76                 case AMBIGSW: 
77                     ambigsw (cp, switches);
78                     done (1);
79                 case UNKWNSW: 
80                     adios (NULL, "-%s unknown", cp);
81
82                 case HELPSW: 
83                     snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
84                         invo_name);
85                     print_help (buf, switches, 1);
86                     done (1);
87                 case VERSIONSW:
88                     print_version(invo_name);
89                     done (1);
90
91                 case COMPSW: 
92                     if (comp)
93                         adios (NULL, "only one component at a time!");
94                     if (!(comp = *argp++) || *comp == '-')
95                         adios (NULL, "missing argument to %s", argp[-2]);
96                     continue;
97
98                 case DATESW: 
99                     datesw++;
100                     continue;
101                 case NDATESW: 
102                     datesw = 0;
103                     continue;
104
105                 case INPLSW: 
106                     inplace++;
107                     continue;
108                 case NINPLSW: 
109                     inplace = 0;
110                     continue;
111
112                 case TEXTSW: 
113                     if (text)
114                         adios (NULL, "only one body at a time!");
115                     if (!(text = *argp++) || *text == '-')
116                         adios (NULL, "missing argument to %s", argp[-2]);
117                     continue;
118             }
119         }
120         if (*cp == '+' || *cp == '@') {
121             if (folder)
122                 adios (NULL, "only one folder at a time!");
123             else
124                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
125         } else {
126             /*
127              * Check if we need to allocate more space
128              * for message name/ranges.
129              */
130             if (nummsgs >= maxmsgs) {
131                 maxmsgs += MAXMSGS;
132                 if (!(msgs = (char **) realloc (msgs,
133                         (size_t) (maxmsgs * sizeof(*msgs)))))
134                     adios (NULL, "unable to reallocate msgs storage");
135             }
136             msgs[nummsgs++] = cp;
137         }
138     }
139
140 #ifdef UCI
141     if (strcmp(invo_name, "fanno") == 0)        /* ugh! */
142         datesw = 0;
143 #endif  /* UCI */
144
145     if (!context_find ("path"))
146         free (path ("./", TFOLDER));
147     if (!nummsgs)
148         msgs[nummsgs++] = "cur";
149     if (!folder)
150         folder = getfolder (1);
151     maildir = m_maildir (folder);
152
153     if (chdir (maildir) == NOTOK)
154         adios (maildir, "unable to change directory to");
155
156     /* read folder and create message structure */
157     if (!(mp = folder_read (folder)))
158         adios (NULL, "unable to read folder %s", folder);
159
160     /* check for empty folder */
161     if (mp->nummsg == 0)
162         adios (NULL, "no messages in %s", folder);
163
164     /* parse all the message ranges/sequences and set SELECTED */
165     for (msgnum = 0; msgnum < nummsgs; msgnum++)
166         if (!m_convert (mp, msgs[msgnum]))
167             done (1);
168
169     make_comp (&comp);
170
171     /* annotate all the SELECTED messages */
172     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
173         if (is_selected(mp, msgnum))
174             annotate (m_name (msgnum), comp, text, inplace, datesw);
175
176     context_replace (pfolder, folder);  /* update current folder  */
177     seq_setcur (mp, mp->lowsel);        /* update current message */
178     seq_save (mp);      /* synchronize message sequences */
179     folder_free (mp);   /* free folder/message structure */
180     context_save ();    /* save the context file         */
181     done (0);
182 }
183
184
185 static void
186 make_comp (char **ap)
187 {
188     register char *cp;
189     char buffer[BUFSIZ];
190
191     if (*ap == NULL) {
192         printf ("Enter component name: ");
193         fflush (stdout);
194
195         if (fgets (buffer, sizeof buffer, stdin) == NULL)
196             done (1);
197         *ap = trimcpy (buffer);
198     }
199
200     if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
201         *cp = 0;
202     if (strlen (*ap) == 0)
203         adios (NULL, "null component name");
204     if (**ap == '-')
205         adios (NULL, "invalid component name %s", *ap);
206     if (strlen (*ap) >= NAMESZ)
207         adios (NULL, "too large component name %s", *ap);
208
209     for (cp = *ap; *cp; cp++)
210         if (!isalnum (*cp) && *cp != '-')
211             adios (NULL, "invalid component name %s", *ap);
212 }
213