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