]> diplodocus.org Git - nmh/blob - uip/mark.c
Fixed three LC_ALL settings:
[nmh] / uip / mark.c
1
2 /*
3 * mark.c -- add message(s) to sequences in given folder
4 * -- delete messages (s) from sequences in given folder
5 * -- list sequences in given folder
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 #include <h/utils.h>
14
15 #define MARK_SWITCHES \
16 X("add", 0, ADDSW) \
17 X("delete", 0, DELSW) \
18 X("list", 0, LSTSW) \
19 X("sequence name", 0, SEQSW) \
20 X("public", 0, PUBLSW) \
21 X("nopublic", 0, NPUBLSW) \
22 X("zero", 0, ZEROSW) \
23 X("nozero", 0, NZEROSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
26 X("debug", -5, DEBUGSW) \
27
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(MARK);
30 #undef X
31
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(MARK, switches);
34 #undef X
35
36 /*
37 * static prototypes
38 */
39 static void print_debug (struct msgs *);
40 static void seq_printdebug (struct msgs *);
41
42
43 int
44 main (int argc, char **argv)
45 {
46 int addsw = 0, deletesw = 0, debugsw = 0;
47 int listsw = 0, publicsw = -1, zerosw = 0, msgnum;
48 unsigned int seqp = 0;
49 char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
50 char **argp, **arguments;
51 svector_t seqs = svector_create (0);
52 struct msgs_array msgs = { 0, 0, NULL };
53 struct msgs *mp;
54
55 setlocale(LC_ALL, "");
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 * Parse arguments
66 */
67 while ((cp = *argp++)) {
68 if (*cp == '-') {
69 switch (smatch (++cp, switches)) {
70 case AMBIGSW:
71 ambigsw (cp, switches);
72 done (1);
73 case UNKWNSW:
74 adios (NULL, "-%s unknown\n", cp);
75
76 case HELPSW:
77 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
78 invo_name);
79 print_help (buf, switches, 1);
80 done (0);
81 case VERSIONSW:
82 print_version(invo_name);
83 done (0);
84
85 case ADDSW:
86 addsw++;
87 deletesw = listsw = 0;
88 continue;
89 case DELSW:
90 deletesw++;
91 addsw = listsw = 0;
92 continue;
93 case LSTSW:
94 listsw++;
95 addsw = deletesw = 0;
96 continue;
97
98 case SEQSW:
99 if (!(cp = *argp++) || *cp == '-')
100 adios (NULL, "missing argument to %s", argp[-2]);
101
102 svector_push_back (seqs, cp);
103 seqp++;
104 continue;
105
106 case PUBLSW:
107 publicsw = 1;
108 continue;
109 case NPUBLSW:
110 publicsw = 0;
111 continue;
112
113 case DEBUGSW:
114 debugsw++;
115 continue;
116
117 case ZEROSW:
118 zerosw++;
119 continue;
120 case NZEROSW:
121 zerosw = 0;
122 continue;
123 }
124 }
125 if (*cp == '+' || *cp == '@') {
126 if (folder)
127 adios (NULL, "only one folder at a time!");
128 else
129 folder = pluspath (cp);
130 } else
131 app_msgarg(&msgs, cp);
132 }
133
134 /*
135 * If we haven't specified -add, -delete, or -list,
136 * then use -add if a sequence was specified, else
137 * use -list.
138 */
139 if (!addsw && !deletesw && !listsw) {
140 if (seqp)
141 addsw++;
142 else
143 listsw++;
144 }
145
146 if (!context_find ("path"))
147 free (path ("./", TFOLDER));
148 if (!msgs.size)
149 app_msgarg(&msgs, listsw ? "all" :"cur");
150 if (!folder)
151 folder = getfolder (1);
152 maildir = m_maildir (folder);
153
154 if (chdir (maildir) == NOTOK)
155 adios (maildir, "unable to change directory to");
156
157 /* read folder and create message structure */
158 if (!(mp = folder_read (folder, 1)))
159 adios (NULL, "unable to read folder %s", folder);
160
161 /* print some general debugging info */
162 if (debugsw)
163 print_debug(mp);
164
165 /* check for empty folder */
166 if (mp->nummsg == 0)
167 adios (NULL, "no messages in %s", folder);
168
169 /* parse all the message ranges/sequences and set SELECTED */
170 for (msgnum = 0; msgnum < msgs.size; msgnum++)
171 if (!m_convert (mp, msgs.msgs[msgnum]))
172 done (1);
173
174 if (publicsw == 1 && is_readonly(mp))
175 adios (NULL, "folder %s is read-only, so -public not allowed", folder);
176
177 /*
178 * Make sure at least one sequence has been
179 * specified if we are adding or deleting.
180 */
181 if (seqp == 0 && (addsw || deletesw))
182 adios (NULL, "-%s requires at least one -sequence argument",
183 addsw ? "add" : "delete");
184
185 /* Adding messages to sequences */
186 if (addsw) {
187 for (seqp = 0; seqp < svector_size (seqs); seqp++)
188 if (!seq_addsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
189 done (1);
190 }
191
192 /* Deleting messages from sequences */
193 if (deletesw) {
194 for (seqp = 0; seqp < svector_size (seqs); seqp++)
195 if (!seq_delsel (mp, svector_at (seqs, seqp), publicsw, zerosw))
196 done (1);
197 }
198
199 /* Listing messages in sequences */
200 if (listsw) {
201 if (seqp) {
202 /* print the sequences given */
203 for (seqp = 0; seqp < svector_size (seqs); seqp++)
204 seq_print (mp, svector_at (seqs, seqp));
205 } else {
206 /* else print them all */
207 seq_printall (mp);
208 }
209
210 /* print debugging info about SELECTED messages */
211 if (debugsw)
212 seq_printdebug (mp);
213 }
214
215 svector_free (seqs);
216 seq_save (mp); /* synchronize message sequences */
217 context_replace (pfolder, folder); /* update current folder */
218 context_save (); /* save the context file */
219 folder_free (mp); /* free folder/message structure */
220 done (0);
221 return 1;
222 }
223
224
225 /*
226 * Print general debugging info
227 */
228 static void
229 print_debug (struct msgs *mp)
230 {
231 char buf[100];
232
233 printf ("invo_name = %s\n", invo_name);
234 printf ("mypath = %s\n", mypath);
235 printf ("defpath = %s\n", defpath);
236 printf ("ctxpath = %s\n", ctxpath);
237 printf ("context flags = %s\n", snprintb (buf, sizeof(buf),
238 (unsigned) ctxflags, DBITS));
239 printf ("foldpath = %s\n", mp->foldpath);
240 printf ("folder flags = %s\n\n", snprintb(buf, sizeof(buf),
241 (unsigned) mp->msgflags, FBITS));
242 printf ("lowmsg=%d hghmsg=%d nummsg=%d curmsg=%d\n",
243 mp->lowmsg, mp->hghmsg, mp->nummsg, mp->curmsg);
244 printf ("lowsel=%d hghsel=%d numsel=%d\n",
245 mp->lowsel, mp->hghsel, mp->numsel);
246 printf ("lowoff=%d hghoff=%d\n\n", mp->lowoff, mp->hghoff);
247 }
248
249
250 /*
251 * Print debugging info about all the SELECTED
252 * messages and the sequences they are in.
253 * Due limitattions of snprintb(), only a limited
254 * number of sequences will be printed. See the
255 * comments in sbr/seq_bits.c.
256 */
257 static void
258 seq_printdebug (struct msgs *mp)
259 {
260 int msgnum;
261 char buf[BUFSIZ];
262
263 printf ("\n");
264 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
265 if (is_selected (mp, msgnum))
266 printf ("%*d: %s\n", DMAXFOLDER, msgnum,
267 snprintb (buf, sizeof buf,
268 (unsigned) *bvector_bits (msgstat (mp, msgnum)),
269 seq_bits (mp)));
270 }
271 }