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