cmd1.c revision 1.23 1 1.23 christos /* $NetBSD: cmd1.c,v 1.23 2005/07/19 23:07:10 christos Exp $ */
2 1.5 christos
3 1.1 cgd /*-
4 1.3 deraadt * Copyright (c) 1980, 1993
5 1.3 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.21 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.10 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.5 christos #if 0
35 1.6 tls static char sccsid[] = "@(#)cmd1.c 8.2 (Berkeley) 4/20/95";
36 1.5 christos #else
37 1.23 christos __RCSID("$NetBSD: cmd1.c,v 1.23 2005/07/19 23:07:10 christos Exp $");
38 1.5 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include "rcv.h"
42 1.3 deraadt #include "extern.h"
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * Mail -- a mail program
46 1.1 cgd *
47 1.1 cgd * User commands.
48 1.1 cgd */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * Print the current active headings.
52 1.1 cgd * Don't change dot if invoker didn't give an argument.
53 1.1 cgd */
54 1.1 cgd
55 1.1 cgd static int screen;
56 1.1 cgd
57 1.3 deraadt int
58 1.15 wiz headers(void *v)
59 1.1 cgd {
60 1.5 christos int *msgvec = v;
61 1.10 lukem int n, mesg, flag;
62 1.10 lukem struct message *mp;
63 1.1 cgd int size;
64 1.1 cgd
65 1.1 cgd size = screensize();
66 1.1 cgd n = msgvec[0];
67 1.1 cgd if (n != 0)
68 1.1 cgd screen = (n-1)/size;
69 1.1 cgd if (screen < 0)
70 1.1 cgd screen = 0;
71 1.1 cgd mp = &message[screen * size];
72 1.1 cgd if (mp >= &message[msgCount])
73 1.1 cgd mp = &message[msgCount - size];
74 1.1 cgd if (mp < &message[0])
75 1.1 cgd mp = &message[0];
76 1.1 cgd flag = 0;
77 1.1 cgd mesg = mp - &message[0];
78 1.1 cgd if (dot != &message[n-1])
79 1.1 cgd dot = mp;
80 1.1 cgd for (; mp < &message[msgCount]; mp++) {
81 1.1 cgd mesg++;
82 1.1 cgd if (mp->m_flag & MDELETED)
83 1.1 cgd continue;
84 1.1 cgd if (flag++ >= size)
85 1.1 cgd break;
86 1.1 cgd printhead(mesg);
87 1.1 cgd }
88 1.1 cgd if (flag == 0) {
89 1.23 christos (void)printf("No more mail.\n");
90 1.1 cgd return(1);
91 1.1 cgd }
92 1.1 cgd return(0);
93 1.1 cgd }
94 1.1 cgd
95 1.1 cgd /*
96 1.1 cgd * Scroll to the next/previous screen
97 1.1 cgd */
98 1.3 deraadt int
99 1.15 wiz scroll(void *v)
100 1.1 cgd {
101 1.5 christos char *arg = v;
102 1.10 lukem int s, size;
103 1.1 cgd int cur[1];
104 1.1 cgd
105 1.1 cgd cur[0] = 0;
106 1.1 cgd size = screensize();
107 1.1 cgd s = screen;
108 1.1 cgd switch (*arg) {
109 1.1 cgd case 0:
110 1.1 cgd case '+':
111 1.1 cgd s++;
112 1.12 dean if (s * size >= msgCount) {
113 1.23 christos (void)printf("On last screenful of messages\n");
114 1.1 cgd return(0);
115 1.1 cgd }
116 1.1 cgd screen = s;
117 1.1 cgd break;
118 1.1 cgd
119 1.1 cgd case '-':
120 1.1 cgd if (--s < 0) {
121 1.23 christos (void)printf("On first screenful of messages\n");
122 1.1 cgd return(0);
123 1.1 cgd }
124 1.1 cgd screen = s;
125 1.1 cgd break;
126 1.1 cgd
127 1.1 cgd default:
128 1.23 christos (void)printf("Unrecognized scrolling command \"%s\"\n", arg);
129 1.1 cgd return(1);
130 1.1 cgd }
131 1.1 cgd return(headers(cur));
132 1.1 cgd }
133 1.1 cgd
134 1.1 cgd /*
135 1.1 cgd * Compute screen size.
136 1.1 cgd */
137 1.3 deraadt int
138 1.15 wiz screensize(void)
139 1.1 cgd {
140 1.1 cgd int s;
141 1.1 cgd char *cp;
142 1.1 cgd
143 1.16 wiz if ((cp = value("screen")) != NULL && (s = atoi(cp)) > 0)
144 1.1 cgd return s;
145 1.1 cgd return screenheight - 4;
146 1.1 cgd }
147 1.1 cgd
148 1.1 cgd /*
149 1.1 cgd * Print out the headlines for each message
150 1.1 cgd * in the passed message list.
151 1.1 cgd */
152 1.3 deraadt int
153 1.15 wiz from(void *v)
154 1.1 cgd {
155 1.5 christos int *msgvec = v;
156 1.10 lukem int *ip;
157 1.1 cgd
158 1.8 pk for (ip = msgvec; *ip != 0; ip++)
159 1.1 cgd printhead(*ip);
160 1.1 cgd if (--ip >= msgvec)
161 1.1 cgd dot = &message[*ip - 1];
162 1.1 cgd return(0);
163 1.1 cgd }
164 1.1 cgd
165 1.1 cgd /*
166 1.1 cgd * Print out the header of a specific message.
167 1.1 cgd * This is a slight improvement to the standard one.
168 1.1 cgd */
169 1.3 deraadt void
170 1.15 wiz printhead(int mesg)
171 1.1 cgd {
172 1.1 cgd struct message *mp;
173 1.1 cgd char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
174 1.1 cgd char pbuf[BUFSIZ];
175 1.1 cgd struct headline hl;
176 1.1 cgd int subjlen;
177 1.1 cgd char *name;
178 1.1 cgd
179 1.1 cgd mp = &message[mesg-1];
180 1.17 wiz (void)readline(setinput(mp), headline, LINESIZE);
181 1.16 wiz if ((subjline = hfield("subject", mp)) == NULL)
182 1.1 cgd subjline = hfield("subj", mp);
183 1.1 cgd /*
184 1.1 cgd * Bletch!
185 1.1 cgd */
186 1.1 cgd curind = dot == mp ? '>' : ' ';
187 1.1 cgd dispc = ' ';
188 1.1 cgd if (mp->m_flag & MSAVED)
189 1.1 cgd dispc = '*';
190 1.1 cgd if (mp->m_flag & MPRESERVE)
191 1.1 cgd dispc = 'P';
192 1.1 cgd if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
193 1.1 cgd dispc = 'N';
194 1.1 cgd if ((mp->m_flag & (MREAD|MNEW)) == 0)
195 1.1 cgd dispc = 'U';
196 1.1 cgd if (mp->m_flag & MBOX)
197 1.1 cgd dispc = 'M';
198 1.1 cgd parse(headline, &hl, pbuf);
199 1.23 christos (void)snprintf(wcount, LINESIZE, "%3ld/%-5llu", mp->m_blines,
200 1.23 christos /*LINTED*/
201 1.23 christos (unsigned long long)mp->m_size);
202 1.1 cgd subjlen = screenwidth - 50 - strlen(wcount);
203 1.16 wiz name = value("show-rcpt") != NULL ?
204 1.1 cgd skin(hfield("to", mp)) : nameof(mp, 0);
205 1.16 wiz if (subjline == NULL || subjlen < 0) /* pretty pathetic */
206 1.23 christos (void)printf("%c%c%3d %-20.20s %16.16s %s\n",
207 1.1 cgd curind, dispc, mesg, name, hl.l_date, wcount);
208 1.1 cgd else
209 1.23 christos (void)printf("%c%c%3d %-20.20s %16.16s %s \"%.*s\"\n",
210 1.1 cgd curind, dispc, mesg, name, hl.l_date, wcount,
211 1.1 cgd subjlen, subjline);
212 1.1 cgd }
213 1.1 cgd
214 1.1 cgd /*
215 1.1 cgd * Print out the value of dot.
216 1.1 cgd */
217 1.3 deraadt int
218 1.23 christos /*ARGSUSED*/
219 1.15 wiz pdot(void *v)
220 1.1 cgd {
221 1.23 christos (void)printf("%d\n", (int)(dot - &message[0] + 1));
222 1.1 cgd return(0);
223 1.1 cgd }
224 1.1 cgd
225 1.1 cgd /*
226 1.1 cgd * Print out all the possible commands.
227 1.1 cgd */
228 1.3 deraadt int
229 1.23 christos /*ARGSUSED*/
230 1.15 wiz pcmdlist(void *v)
231 1.1 cgd {
232 1.10 lukem const struct cmd *cp;
233 1.10 lukem int cc;
234 1.1 cgd
235 1.23 christos (void)printf("Commands are:\n");
236 1.1 cgd for (cc = 0, cp = cmdtab; cp->c_name != NULL; cp++) {
237 1.1 cgd cc += strlen(cp->c_name) + 2;
238 1.1 cgd if (cc > 72) {
239 1.23 christos (void)printf("\n");
240 1.1 cgd cc = strlen(cp->c_name) + 2;
241 1.1 cgd }
242 1.16 wiz if ((cp+1)->c_name != NULL)
243 1.23 christos (void)printf("%s, ", cp->c_name);
244 1.1 cgd else
245 1.23 christos (void)printf("%s\n", cp->c_name);
246 1.1 cgd }
247 1.1 cgd return(0);
248 1.1 cgd }
249 1.1 cgd
250 1.1 cgd /*
251 1.1 cgd * Paginate messages, honor ignored fields.
252 1.1 cgd */
253 1.3 deraadt int
254 1.15 wiz more(void *v)
255 1.1 cgd {
256 1.5 christos int *msgvec = v;
257 1.1 cgd return (type1(msgvec, 1, 1));
258 1.1 cgd }
259 1.1 cgd
260 1.1 cgd /*
261 1.1 cgd * Paginate messages, even printing ignored fields.
262 1.1 cgd */
263 1.3 deraadt int
264 1.15 wiz More(void *v)
265 1.1 cgd {
266 1.5 christos int *msgvec = v;
267 1.1 cgd
268 1.1 cgd return (type1(msgvec, 0, 1));
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd /*
272 1.1 cgd * Type out messages, honor ignored fields.
273 1.1 cgd */
274 1.3 deraadt int
275 1.15 wiz type(void *v)
276 1.1 cgd {
277 1.5 christos int *msgvec = v;
278 1.1 cgd
279 1.1 cgd return(type1(msgvec, 1, 0));
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd /*
283 1.1 cgd * Type out messages, even printing ignored fields.
284 1.1 cgd */
285 1.3 deraadt int
286 1.15 wiz Type(void *v)
287 1.1 cgd {
288 1.5 christos int *msgvec = v;
289 1.1 cgd
290 1.1 cgd return(type1(msgvec, 0, 0));
291 1.1 cgd }
292 1.1 cgd
293 1.1 cgd /*
294 1.1 cgd * Type out the messages requested.
295 1.1 cgd */
296 1.1 cgd jmp_buf pipestop;
297 1.3 deraadt int
298 1.15 wiz type1(int *msgvec, int doign, int page)
299 1.1 cgd {
300 1.10 lukem int *ip;
301 1.5 christos struct message *mp;
302 1.22 christos const char *cp;
303 1.1 cgd int nlines;
304 1.1 cgd FILE *obuf;
305 1.5 christos #if __GNUC__
306 1.5 christos /* Avoid longjmp clobbering */
307 1.17 wiz (void)&cp;
308 1.17 wiz (void)&obuf;
309 1.5 christos #endif
310 1.1 cgd
311 1.1 cgd obuf = stdout;
312 1.1 cgd if (setjmp(pipestop))
313 1.1 cgd goto close_pipe;
314 1.16 wiz if (value("interactive") != NULL &&
315 1.16 wiz (page || (cp = value("crt")) != NULL)) {
316 1.1 cgd nlines = 0;
317 1.1 cgd if (!page) {
318 1.1 cgd for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
319 1.19 ross nlines += message[*ip - 1].m_blines;
320 1.1 cgd }
321 1.1 cgd if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
322 1.1 cgd cp = value("PAGER");
323 1.1 cgd if (cp == NULL || *cp == '\0')
324 1.1 cgd cp = _PATH_MORE;
325 1.1 cgd obuf = Popen(cp, "w");
326 1.1 cgd if (obuf == NULL) {
327 1.18 wiz warn("%s", cp);
328 1.1 cgd obuf = stdout;
329 1.1 cgd } else
330 1.23 christos (void)signal(SIGPIPE, brokpipe);
331 1.1 cgd }
332 1.1 cgd }
333 1.1 cgd for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
334 1.1 cgd mp = &message[*ip - 1];
335 1.1 cgd touch(mp);
336 1.1 cgd dot = mp;
337 1.16 wiz if (value("quiet") == NULL)
338 1.23 christos (void)fprintf(obuf, "Message %d:\n", *ip);
339 1.17 wiz (void)sendmessage(mp, obuf, doign ? ignore : 0, NULL);
340 1.1 cgd }
341 1.1 cgd close_pipe:
342 1.1 cgd if (obuf != stdout) {
343 1.1 cgd /*
344 1.1 cgd * Ignore SIGPIPE so it can't cause a duplicate close.
345 1.1 cgd */
346 1.23 christos (void)signal(SIGPIPE, SIG_IGN);
347 1.23 christos (void)Pclose(obuf);
348 1.23 christos (void)signal(SIGPIPE, SIG_DFL);
349 1.1 cgd }
350 1.1 cgd return(0);
351 1.1 cgd }
352 1.1 cgd
353 1.1 cgd /*
354 1.1 cgd * Respond to a broken pipe signal --
355 1.1 cgd * probably caused by quitting more.
356 1.1 cgd */
357 1.1 cgd void
358 1.23 christos /*ARGSUSED*/
359 1.15 wiz brokpipe(int signo)
360 1.1 cgd {
361 1.1 cgd longjmp(pipestop, 1);
362 1.1 cgd }
363 1.1 cgd
364 1.1 cgd /*
365 1.1 cgd * Print the top so many lines of each desired message.
366 1.1 cgd * The number of lines is taken from the variable "toplines"
367 1.1 cgd * and defaults to 5.
368 1.1 cgd */
369 1.3 deraadt int
370 1.15 wiz top(void *v)
371 1.1 cgd {
372 1.5 christos int *msgvec = v;
373 1.10 lukem int *ip;
374 1.10 lukem struct message *mp;
375 1.1 cgd int c, topl, lines, lineb;
376 1.1 cgd char *valtop, linebuf[LINESIZE];
377 1.1 cgd FILE *ibuf;
378 1.1 cgd
379 1.1 cgd topl = 5;
380 1.1 cgd valtop = value("toplines");
381 1.16 wiz if (valtop != NULL) {
382 1.1 cgd topl = atoi(valtop);
383 1.1 cgd if (topl < 0 || topl > 10000)
384 1.1 cgd topl = 5;
385 1.1 cgd }
386 1.1 cgd lineb = 1;
387 1.1 cgd for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
388 1.1 cgd mp = &message[*ip - 1];
389 1.1 cgd touch(mp);
390 1.1 cgd dot = mp;
391 1.16 wiz if (value("quiet") == NULL)
392 1.23 christos (void)printf("Message %d:\n", *ip);
393 1.1 cgd ibuf = setinput(mp);
394 1.1 cgd c = mp->m_lines;
395 1.1 cgd if (!lineb)
396 1.23 christos (void)printf("\n");
397 1.1 cgd for (lines = 0; lines < c && lines <= topl; lines++) {
398 1.1 cgd if (readline(ibuf, linebuf, LINESIZE) < 0)
399 1.1 cgd break;
400 1.23 christos (void)puts(linebuf);
401 1.1 cgd lineb = blankline(linebuf);
402 1.1 cgd }
403 1.1 cgd }
404 1.1 cgd return(0);
405 1.1 cgd }
406 1.1 cgd
407 1.1 cgd /*
408 1.1 cgd * Touch all the given messages so that they will
409 1.1 cgd * get mboxed.
410 1.1 cgd */
411 1.3 deraadt int
412 1.15 wiz stouch(void *v)
413 1.1 cgd {
414 1.5 christos int *msgvec = v;
415 1.10 lukem int *ip;
416 1.1 cgd
417 1.1 cgd for (ip = msgvec; *ip != 0; ip++) {
418 1.1 cgd dot = &message[*ip-1];
419 1.1 cgd dot->m_flag |= MTOUCH;
420 1.1 cgd dot->m_flag &= ~MPRESERVE;
421 1.1 cgd }
422 1.1 cgd return(0);
423 1.1 cgd }
424 1.1 cgd
425 1.1 cgd /*
426 1.1 cgd * Make sure all passed messages get mboxed.
427 1.1 cgd */
428 1.3 deraadt int
429 1.15 wiz mboxit(void *v)
430 1.1 cgd {
431 1.5 christos int *msgvec = v;
432 1.10 lukem int *ip;
433 1.1 cgd
434 1.1 cgd for (ip = msgvec; *ip != 0; ip++) {
435 1.1 cgd dot = &message[*ip-1];
436 1.1 cgd dot->m_flag |= MTOUCH|MBOX;
437 1.1 cgd dot->m_flag &= ~MPRESERVE;
438 1.1 cgd }
439 1.1 cgd return(0);
440 1.1 cgd }
441 1.1 cgd
442 1.1 cgd /*
443 1.1 cgd * List the folders the user currently has.
444 1.1 cgd */
445 1.3 deraadt int
446 1.23 christos /*ARGSUSED*/
447 1.15 wiz folders(void *v)
448 1.1 cgd {
449 1.7 mikel char dirname[PATHSIZE];
450 1.22 christos const char *cmd;
451 1.1 cgd
452 1.1 cgd if (getfold(dirname) < 0) {
453 1.23 christos (void)printf("No value set for \"folder\"\n");
454 1.1 cgd return 1;
455 1.1 cgd }
456 1.16 wiz if ((cmd = value("LISTER")) == NULL)
457 1.1 cgd cmd = "ls";
458 1.20 christos (void)run_command(cmd, 0, -1, -1, dirname, NULL);
459 1.6 tls return 0;
460 1.6 tls }
461 1.6 tls
462 1.6 tls /*
463 1.6 tls * Update the mail file with any new messages that have
464 1.6 tls * come in since we started reading mail.
465 1.6 tls */
466 1.6 tls int
467 1.23 christos /*ARGSUSED*/
468 1.15 wiz inc(void *v)
469 1.6 tls {
470 1.6 tls int nmsg, mdot;
471 1.6 tls
472 1.6 tls nmsg = incfile();
473 1.6 tls
474 1.6 tls if (nmsg == 0) {
475 1.23 christos (void)printf("No new mail.\n");
476 1.6 tls } else if (nmsg > 0) {
477 1.6 tls mdot = newfileinfo(msgCount - nmsg);
478 1.6 tls dot = &message[mdot - 1];
479 1.6 tls } else {
480 1.23 christos (void)printf("\"inc\" command failed...\n");
481 1.6 tls }
482 1.6 tls
483 1.1 cgd return 0;
484 1.1 cgd }
485