Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / anno.c
1 /* anno.c - annotate messages */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: anno.c,v 2.6 1992/12/15 00:20:22 jromine Exp $";
4 #endif  /* lint */
5
6 #include "../h/mh.h"
7 #include <ctype.h>
8 #include <stdio.h>
9 #ifdef LOCALE
10 #include        <locale.h>
11 #endif
12
13 static make_comp();
14 /* \f */
15
16 static struct swit switches[] = {
17 #define COMPSW  0
18     "component field", 0,
19
20 #define INPLSW  1
21     "inplace", 0,
22 #define NINPLSW 2
23     "noinplace", 0,
24
25 #define DATESW  3
26     "date", 0,
27 #define NDATESW 4
28     "nodate", 0,
29
30 #define TEXTSW  5
31     "text body", 0,
32
33 #define HELPSW  6
34     "help", 4,
35
36     NULL, 0
37 };
38
39 /* \f */
40
41 /* ARGSUSED */
42
43 main (argc, argv)
44 int     argc;
45 char  **argv;
46 {
47     int     inplace = 0,
48             datesw = 1,
49             msgp = 0,
50             msgnum;
51     char   *cp,
52            *maildir,
53            *comp = NULL,
54            *text = NULL,
55            *folder = NULL,
56             buf[100],
57           **ap,
58           **argp,
59            *arguments[MAXARGS],
60            *msgs[MAXARGS];
61     struct msgs *mp;
62
63 #ifdef LOCALE
64         setlocale(LC_ALL, "");
65 #endif
66     invo_name = r1bindex (argv[0], '/');
67     if ((cp = m_find (invo_name)) != NULL) {
68         ap = brkstring (cp = getcpy (cp), " ", "\n");
69         ap = copyip (ap, arguments);
70     }
71     else
72         ap = arguments;
73     (void) copyip (argv + 1, ap);
74     argp = arguments;
75
76 /* \f */
77
78     while (cp = *argp++) {
79         if (*cp == '-')
80             switch (smatch (++cp, switches)) {
81                 case AMBIGSW: 
82                     ambigsw (cp, switches);
83                     done (1);
84                 case UNKWNSW: 
85                     adios (NULLCP, "-%s unknown", cp);
86                 case HELPSW: 
87                     (void) sprintf (buf, "%s [+folder] [msgs] [switches]",
88                             invo_name);
89                     help (buf, switches);
90                     done (1);
91
92                 case COMPSW: 
93                     if (comp)
94                         adios (NULLCP, "only one component at a time!");
95                     if (!(comp = *argp++) || *comp == '-')
96                         adios (NULLCP, "missing argument to %s", argp[-2]);
97                     continue;
98
99                 case DATESW: 
100                     datesw++;
101                     continue;
102                 case NDATESW: 
103                     datesw = 0;
104                     continue;
105
106                 case INPLSW: 
107                     inplace++;
108                     continue;
109                 case NINPLSW: 
110                     inplace = 0;
111                     continue;
112
113                 case TEXTSW: 
114                     if (text)
115                         adios (NULLCP, "only one body at a time!");
116                     if (!(text = *argp++) || *text == '-')
117                         adios (NULLCP, "missing argument to %s", argp[-2]);
118                     continue;
119             }
120         if (*cp == '+' || *cp == '@') {
121             if (folder)
122                 adios (NULLCP, "only one folder at a time!");
123             else
124                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
125         }
126         else
127             msgs[msgp++] = cp;
128     }
129 #ifdef UCI
130     if (strcmp(invo_name, "fanno") == 0)        /* ugh! */
131         datesw = 0;
132 #endif  /* UCI */
133
134 /* \f */
135
136     if (!m_find ("path"))
137         free (path ("./", TFOLDER));
138     if (!msgp)
139         msgs[msgp++] = "cur";
140     if (!folder)
141         folder = m_getfolder ();
142     maildir = m_maildir (folder);
143
144     if (chdir (maildir) == NOTOK)
145         adios (maildir, "unable to change directory to");
146     if (!(mp = m_gmsg (folder)))
147         adios (NULLCP, "unable to read folder %s", folder);
148     if (mp -> hghmsg == 0)
149         adios (NULLCP, "no messages in %s", folder);
150
151     for (msgnum = 0; msgnum < msgp; msgnum++)
152         if (!m_convert (mp, msgs[msgnum]))
153             done (1);
154
155     make_comp (&comp);
156
157     for (msgnum = mp -> lowsel; msgnum <= mp -> hghsel; msgnum++)
158         if (mp -> msgstats[msgnum] & SELECTED)
159             (void) annotate (m_name (msgnum), comp, text, inplace, datesw);
160
161     m_replace (pfolder, folder);
162     if (mp -> lowsel != mp -> curmsg)
163         m_setcur (mp, mp -> lowsel);
164     m_sync (mp);
165     m_update ();
166
167     done (0);
168 }
169
170 /* \f */
171
172 static make_comp (ap)
173 register char **ap;
174 {
175     register char  *cp;
176     char    buffer[BUFSIZ];
177
178     if (*ap == NULL) {
179         printf ("Enter component name: ");
180         (void) fflush (stdout);
181
182         if (fgets (buffer, sizeof buffer, stdin) == NULL)
183             done (1);
184         *ap = trimcpy (buffer);
185     }
186
187     if ((cp = *ap + strlen (*ap) - 1) > *ap && *cp == ':')
188         *cp = 0;
189     if (strlen (*ap) == 0)
190         adios (NULLCP, "null component name");
191     if (**ap == '-')
192         adios (NULLCP, "invalid component name %s", *ap);
193     if (strlen (*ap) >= NAMESZ)
194         adios (NULLCP, "too large component name %s", *ap);
195
196     for (cp = *ap; *cp; cp++)
197         if (!isalnum (*cp) && *cp != '-')
198             adios (NULLCP, "invalid component name %s", *ap);
199 }