lex.c revision 1.28 1 1.28 christos /* $NetBSD: lex.c,v 1.28 2006/10/21 21:37:20 christos Exp $ */
2 1.7 christos
3 1.1 cgd /*
4 1.4 deraadt * Copyright (c) 1980, 1993
5 1.4 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.23 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.11 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.7 christos #if 0
35 1.8 tls static char sccsid[] = "@(#)lex.c 8.2 (Berkeley) 4/20/95";
36 1.7 christos #else
37 1.28 christos __RCSID("$NetBSD: lex.c,v 1.28 2006/10/21 21:37:20 christos Exp $");
38 1.7 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include "rcv.h"
42 1.28 christos #include <util.h>
43 1.4 deraadt #include "extern.h"
44 1.1 cgd
45 1.28 christos #ifdef USE_EDITLINE
46 1.26 christos #include "complete.h"
47 1.26 christos #endif
48 1.26 christos
49 1.1 cgd /*
50 1.1 cgd * Mail -- a mail program
51 1.1 cgd *
52 1.1 cgd * Lexical processing of commands.
53 1.1 cgd */
54 1.1 cgd
55 1.24 christos const char *prompt = "& ";
56 1.1 cgd
57 1.1 cgd /*
58 1.1 cgd * Set up editing on the given file name.
59 1.1 cgd * If the first character of name is %, we are considered to be
60 1.1 cgd * editing the file, otherwise we are reading our mail which has
61 1.1 cgd * signficance for mbox and so forth.
62 1.1 cgd */
63 1.4 deraadt int
64 1.24 christos setfile(const char *name)
65 1.1 cgd {
66 1.1 cgd FILE *ibuf;
67 1.20 wiz int i, fd;
68 1.1 cgd struct stat stb;
69 1.13 dean char isedit = *name != '%' || getuserid(myname) != getuid();
70 1.24 christos const char *who = name[1] ? name + 1 : myname;
71 1.1 cgd static int shudclob;
72 1.20 wiz char tempname[PATHSIZE];
73 1.1 cgd
74 1.18 wiz if ((name = expand(name)) == NULL)
75 1.1 cgd return -1;
76 1.1 cgd
77 1.1 cgd if ((ibuf = Fopen(name, "r")) == NULL) {
78 1.1 cgd if (!isedit && errno == ENOENT)
79 1.1 cgd goto nomail;
80 1.22 wiz warn("%s", name);
81 1.1 cgd return(-1);
82 1.1 cgd }
83 1.1 cgd
84 1.1 cgd if (fstat(fileno(ibuf), &stb) < 0) {
85 1.22 wiz warn("fstat");
86 1.25 christos (void)Fclose(ibuf);
87 1.1 cgd return (-1);
88 1.1 cgd }
89 1.1 cgd
90 1.1 cgd switch (stb.st_mode & S_IFMT) {
91 1.1 cgd case S_IFDIR:
92 1.25 christos (void)Fclose(ibuf);
93 1.1 cgd errno = EISDIR;
94 1.22 wiz warn("%s", name);
95 1.1 cgd return (-1);
96 1.1 cgd
97 1.1 cgd case S_IFREG:
98 1.1 cgd break;
99 1.1 cgd
100 1.1 cgd default:
101 1.25 christos (void)Fclose(ibuf);
102 1.1 cgd errno = EINVAL;
103 1.22 wiz warn("%s", name);
104 1.1 cgd return (-1);
105 1.1 cgd }
106 1.1 cgd
107 1.1 cgd /*
108 1.1 cgd * Looks like all will be well. We must now relinquish our
109 1.1 cgd * hold on the current set of stuff. Must hold signals
110 1.1 cgd * while we are reading the new file, else we will ruin
111 1.1 cgd * the message[] data structure.
112 1.1 cgd */
113 1.1 cgd
114 1.1 cgd holdsigs();
115 1.1 cgd if (shudclob)
116 1.1 cgd quit();
117 1.1 cgd
118 1.1 cgd /*
119 1.1 cgd * Copy the messages into /tmp
120 1.1 cgd * and set pointers.
121 1.1 cgd */
122 1.1 cgd
123 1.1 cgd readonly = 0;
124 1.1 cgd if ((i = open(name, 1)) < 0)
125 1.1 cgd readonly++;
126 1.1 cgd else
127 1.25 christos (void)close(i);
128 1.1 cgd if (shudclob) {
129 1.25 christos (void)fclose(itf);
130 1.25 christos (void)fclose(otf);
131 1.1 cgd }
132 1.1 cgd shudclob = 1;
133 1.1 cgd edit = isedit;
134 1.25 christos (void)strcpy(prevfile, mailname);
135 1.1 cgd if (name != mailname)
136 1.25 christos (void)strcpy(mailname, name);
137 1.1 cgd mailsize = fsize(ibuf);
138 1.20 wiz (void)snprintf(tempname, sizeof(tempname),
139 1.20 wiz "%s/mail.RxXXXXXXXXXX", tmpdir);
140 1.20 wiz if ((fd = mkstemp(tempname)) == -1 ||
141 1.20 wiz (otf = fdopen(fd, "w")) == NULL)
142 1.20 wiz err(1, "%s", tempname);
143 1.28 christos (void)fcntl(fileno(otf), F_SETFD, FD_CLOEXEC);
144 1.20 wiz if ((itf = fopen(tempname, "r")) == NULL)
145 1.20 wiz err(1, "%s", tempname);
146 1.28 christos (void)fcntl(fileno(itf), F_SETFD, FD_CLOEXEC);
147 1.25 christos (void)rm(tempname);
148 1.25 christos setptr(ibuf, (off_t)0);
149 1.1 cgd setmsize(msgCount);
150 1.8 tls /*
151 1.9 mikel * New mail may have arrived while we were reading
152 1.9 mikel * the mail file, so reset mailsize to be where
153 1.8 tls * we really are in the file...
154 1.8 tls */
155 1.8 tls mailsize = ftell(ibuf);
156 1.25 christos (void)Fclose(ibuf);
157 1.1 cgd relsesigs();
158 1.1 cgd sawcom = 0;
159 1.1 cgd if (!edit && msgCount == 0) {
160 1.1 cgd nomail:
161 1.25 christos (void)fprintf(stderr, "No mail for %s\n", who);
162 1.1 cgd return -1;
163 1.1 cgd }
164 1.1 cgd return(0);
165 1.1 cgd }
166 1.1 cgd
167 1.8 tls /*
168 1.8 tls * Incorporate any new mail that has arrived since we first
169 1.8 tls * started reading mail.
170 1.8 tls */
171 1.8 tls int
172 1.17 wiz incfile(void)
173 1.8 tls {
174 1.25 christos off_t newsize;
175 1.8 tls int omsgCount = msgCount;
176 1.8 tls FILE *ibuf;
177 1.8 tls
178 1.8 tls ibuf = Fopen(mailname, "r");
179 1.8 tls if (ibuf == NULL)
180 1.8 tls return -1;
181 1.8 tls holdsigs();
182 1.8 tls newsize = fsize(ibuf);
183 1.8 tls if (newsize == 0)
184 1.9 mikel return -1; /* mail box is now empty??? */
185 1.8 tls if (newsize < mailsize)
186 1.8 tls return -1; /* mail box has shrunk??? */
187 1.8 tls if (newsize == mailsize)
188 1.8 tls return 0; /* no new mail */
189 1.8 tls setptr(ibuf, mailsize);
190 1.8 tls setmsize(msgCount);
191 1.8 tls mailsize = ftell(ibuf);
192 1.25 christos (void)Fclose(ibuf);
193 1.8 tls relsesigs();
194 1.8 tls return(msgCount - omsgCount);
195 1.8 tls }
196 1.8 tls
197 1.1 cgd int *msgvec;
198 1.1 cgd int reset_on_stop; /* do a reset() if stopped */
199 1.1 cgd
200 1.1 cgd /*
201 1.1 cgd * Interpret user commands one by one. If standard input is not a tty,
202 1.1 cgd * print no prompt.
203 1.1 cgd */
204 1.4 deraadt void
205 1.17 wiz commands(void)
206 1.1 cgd {
207 1.11 lukem int n;
208 1.1 cgd char linebuf[LINESIZE];
209 1.28 christos int eofloop;
210 1.1 cgd
211 1.1 cgd if (!sourcing) {
212 1.1 cgd if (signal(SIGINT, SIG_IGN) != SIG_IGN)
213 1.25 christos (void)signal(SIGINT, intr);
214 1.1 cgd if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
215 1.25 christos (void)signal(SIGHUP, hangup);
216 1.25 christos (void)signal(SIGTSTP, stop);
217 1.25 christos (void)signal(SIGTTOU, stop);
218 1.25 christos (void)signal(SIGTTIN, stop);
219 1.1 cgd }
220 1.27 christos setexit(); /* defined as (void)setjmp(srbuf) in def.h */
221 1.28 christos eofloop = 0; /* initialize this after a possible longjmp */
222 1.1 cgd for (;;) {
223 1.1 cgd /*
224 1.1 cgd * Print the prompt, if needed. Clear out
225 1.1 cgd * string space, and flush the output.
226 1.1 cgd */
227 1.18 wiz if (!sourcing && value("interactive") != NULL) {
228 1.18 wiz if ((value("autoinc") != NULL) && (incfile() > 0))
229 1.25 christos (void)printf("New mail has arrived.\n");
230 1.1 cgd reset_on_stop = 1;
231 1.28 christos #ifndef USE_EDITLINE
232 1.25 christos (void)printf("%s", prompt);
233 1.26 christos #endif
234 1.1 cgd }
235 1.25 christos (void)fflush(stdout);
236 1.1 cgd sreset();
237 1.1 cgd /*
238 1.1 cgd * Read a line of commands from the current input
239 1.1 cgd * and handle end of file specially.
240 1.1 cgd */
241 1.1 cgd n = 0;
242 1.1 cgd for (;;) {
243 1.28 christos #ifdef USE_EDITLINE
244 1.26 christos if (!sourcing) {
245 1.26 christos char *line;
246 1.26 christos
247 1.28 christos if ((line = my_gets(&elm.command, prompt, NULL)) == NULL) {
248 1.26 christos if (n == 0)
249 1.26 christos n = -1;
250 1.26 christos break;
251 1.26 christos }
252 1.28 christos (void)strncpy(linebuf, line, LINESIZE);
253 1.26 christos }
254 1.26 christos else {
255 1.28 christos if (mail_readline(input, &linebuf[n], LINESIZE - n) < 0) {
256 1.26 christos if (n == 0)
257 1.26 christos n = -1;
258 1.26 christos break;
259 1.26 christos }
260 1.26 christos }
261 1.28 christos #else /* USE_EDITLINE */
262 1.28 christos if (mail_readline(input, &linebuf[n], LINESIZE - n) < 0) {
263 1.1 cgd if (n == 0)
264 1.1 cgd n = -1;
265 1.1 cgd break;
266 1.1 cgd }
267 1.28 christos #endif /* USE_EDITLINE */
268 1.26 christos
269 1.26 christos if (sourcing) { /* allow comments in source files */
270 1.26 christos char *ptr = strchr(linebuf, '#');
271 1.26 christos if (ptr)
272 1.26 christos *ptr = '\0';
273 1.26 christos }
274 1.1 cgd if ((n = strlen(linebuf)) == 0)
275 1.1 cgd break;
276 1.1 cgd n--;
277 1.1 cgd if (linebuf[n] != '\\')
278 1.1 cgd break;
279 1.1 cgd linebuf[n++] = ' ';
280 1.1 cgd }
281 1.1 cgd reset_on_stop = 0;
282 1.1 cgd if (n < 0) {
283 1.1 cgd /* eof */
284 1.1 cgd if (loading)
285 1.1 cgd break;
286 1.1 cgd if (sourcing) {
287 1.25 christos (void)unstack();
288 1.1 cgd continue;
289 1.1 cgd }
290 1.28 christos #ifdef USE_EDITLINE
291 1.26 christos {
292 1.26 christos char *p;
293 1.26 christos if (value("interactive") != NULL &&
294 1.28 christos (p = value("ignoreeof")) != NULL &&
295 1.28 christos ++eofloop < (*p == '\0' ? 25 : atoi(p))) {
296 1.26 christos (void)printf("Use \"quit\" to quit.\n");
297 1.26 christos continue;
298 1.26 christos }
299 1.26 christos }
300 1.26 christos #else
301 1.18 wiz if (value("interactive") != NULL &&
302 1.18 wiz value("ignoreeof") != NULL &&
303 1.1 cgd ++eofloop < 25) {
304 1.25 christos (void)printf("Use \"quit\" to quit.\n");
305 1.1 cgd continue;
306 1.1 cgd }
307 1.26 christos #endif
308 1.1 cgd break;
309 1.1 cgd }
310 1.1 cgd eofloop = 0;
311 1.1 cgd if (execute(linebuf, 0))
312 1.1 cgd break;
313 1.1 cgd }
314 1.1 cgd }
315 1.1 cgd
316 1.1 cgd /*
317 1.1 cgd * Execute a single command.
318 1.1 cgd * Command functions return 0 for success, 1 for error, and -1
319 1.1 cgd * for abort. A 1 or -1 aborts a load or source. A -1 aborts
320 1.1 cgd * the interactive command loop.
321 1.1 cgd * Contxt is non-zero if called while composing mail.
322 1.1 cgd */
323 1.4 deraadt int
324 1.17 wiz execute(char linebuf[], int contxt)
325 1.1 cgd {
326 1.1 cgd char word[LINESIZE];
327 1.1 cgd char *arglist[MAXARGC];
328 1.7 christos const struct cmd *com = NULL;
329 1.11 lukem char *cp, *cp2;
330 1.11 lukem int c;
331 1.1 cgd int muvec[2];
332 1.1 cgd int e = 1;
333 1.1 cgd
334 1.1 cgd /*
335 1.1 cgd * Strip the white space away from the beginning
336 1.1 cgd * of the command, then scan out a word, which
337 1.1 cgd * consists of anything except digits and white space.
338 1.1 cgd *
339 1.1 cgd * Handle ! escapes differently to get the correct
340 1.1 cgd * lexical conventions.
341 1.1 cgd */
342 1.1 cgd
343 1.12 christos for (cp = linebuf; isspace((unsigned char)*cp); cp++)
344 1.1 cgd ;
345 1.1 cgd if (*cp == '!') {
346 1.1 cgd if (sourcing) {
347 1.25 christos (void)printf("Can't \"!\" while sourcing\n");
348 1.1 cgd goto out;
349 1.1 cgd }
350 1.28 christos (void)shell(cp + 1);
351 1.1 cgd return(0);
352 1.1 cgd }
353 1.1 cgd cp2 = word;
354 1.18 wiz while (*cp && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
355 1.1 cgd *cp2++ = *cp++;
356 1.1 cgd *cp2 = '\0';
357 1.1 cgd
358 1.1 cgd /*
359 1.1 cgd * Look up the command; if not found, bitch.
360 1.1 cgd * Normally, a blank command would map to the
361 1.1 cgd * first command in the table; while sourcing,
362 1.1 cgd * however, we ignore blank lines to eliminate
363 1.1 cgd * confusion.
364 1.1 cgd */
365 1.1 cgd
366 1.1 cgd if (sourcing && *word == '\0')
367 1.1 cgd return(0);
368 1.1 cgd com = lex(word);
369 1.19 wiz if (com == NULL) {
370 1.25 christos (void)printf("Unknown command: \"%s\"\n", word);
371 1.1 cgd goto out;
372 1.1 cgd }
373 1.1 cgd
374 1.1 cgd /*
375 1.1 cgd * See if we should execute the command -- if a conditional
376 1.1 cgd * we always execute it, otherwise, check the state of cond.
377 1.1 cgd */
378 1.1 cgd
379 1.1 cgd if ((com->c_argtype & F) == 0)
380 1.7 christos if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
381 1.1 cgd return(0);
382 1.1 cgd
383 1.1 cgd /*
384 1.1 cgd * Process the arguments to the command, depending
385 1.1 cgd * on the type he expects. Default to an error.
386 1.1 cgd * If we are sourcing an interactive command, it's
387 1.1 cgd * an error.
388 1.1 cgd */
389 1.1 cgd
390 1.1 cgd if (!rcvmode && (com->c_argtype & M) == 0) {
391 1.25 christos (void)printf("May not execute \"%s\" while sending\n",
392 1.1 cgd com->c_name);
393 1.1 cgd goto out;
394 1.1 cgd }
395 1.1 cgd if (sourcing && com->c_argtype & I) {
396 1.25 christos (void)printf("May not execute \"%s\" while sourcing\n",
397 1.1 cgd com->c_name);
398 1.1 cgd goto out;
399 1.1 cgd }
400 1.1 cgd if (readonly && com->c_argtype & W) {
401 1.25 christos (void)printf("May not execute \"%s\" -- message file is read only\n",
402 1.1 cgd com->c_name);
403 1.1 cgd goto out;
404 1.1 cgd }
405 1.1 cgd if (contxt && com->c_argtype & R) {
406 1.25 christos (void)printf("Cannot recursively invoke \"%s\"\n", com->c_name);
407 1.1 cgd goto out;
408 1.1 cgd }
409 1.1 cgd switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
410 1.1 cgd case MSGLIST:
411 1.1 cgd /*
412 1.1 cgd * A message list defaulting to nearest forward
413 1.1 cgd * legal message.
414 1.1 cgd */
415 1.1 cgd if (msgvec == 0) {
416 1.25 christos (void)printf("Illegal use of \"message list\"\n");
417 1.1 cgd break;
418 1.1 cgd }
419 1.1 cgd if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
420 1.1 cgd break;
421 1.1 cgd if (c == 0) {
422 1.1 cgd *msgvec = first(com->c_msgflag,
423 1.1 cgd com->c_msgmask);
424 1.10 pk msgvec[1] = 0;
425 1.1 cgd }
426 1.10 pk if (*msgvec == 0) {
427 1.25 christos (void)printf("No applicable messages\n");
428 1.1 cgd break;
429 1.1 cgd }
430 1.1 cgd e = (*com->c_func)(msgvec);
431 1.1 cgd break;
432 1.1 cgd
433 1.1 cgd case NDMLIST:
434 1.1 cgd /*
435 1.1 cgd * A message list with no defaults, but no error
436 1.1 cgd * if none exist.
437 1.1 cgd */
438 1.1 cgd if (msgvec == 0) {
439 1.25 christos (void)printf("Illegal use of \"message list\"\n");
440 1.1 cgd break;
441 1.1 cgd }
442 1.1 cgd if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
443 1.1 cgd break;
444 1.1 cgd e = (*com->c_func)(msgvec);
445 1.1 cgd break;
446 1.1 cgd
447 1.1 cgd case STRLIST:
448 1.1 cgd /*
449 1.1 cgd * Just the straight string, with
450 1.1 cgd * leading blanks removed.
451 1.1 cgd */
452 1.12 christos while (isspace((unsigned char)*cp))
453 1.1 cgd cp++;
454 1.1 cgd e = (*com->c_func)(cp);
455 1.1 cgd break;
456 1.1 cgd
457 1.1 cgd case RAWLIST:
458 1.1 cgd /*
459 1.1 cgd * A vector of strings, in shell style.
460 1.1 cgd */
461 1.1 cgd if ((c = getrawlist(cp, arglist,
462 1.1 cgd sizeof arglist / sizeof *arglist)) < 0)
463 1.1 cgd break;
464 1.1 cgd if (c < com->c_minargs) {
465 1.25 christos (void)printf("%s requires at least %d arg(s)\n",
466 1.1 cgd com->c_name, com->c_minargs);
467 1.1 cgd break;
468 1.1 cgd }
469 1.1 cgd if (c > com->c_maxargs) {
470 1.25 christos (void)printf("%s takes no more than %d arg(s)\n",
471 1.1 cgd com->c_name, com->c_maxargs);
472 1.1 cgd break;
473 1.1 cgd }
474 1.1 cgd e = (*com->c_func)(arglist);
475 1.1 cgd break;
476 1.1 cgd
477 1.1 cgd case NOLIST:
478 1.1 cgd /*
479 1.1 cgd * Just the constant zero, for exiting,
480 1.1 cgd * eg.
481 1.1 cgd */
482 1.1 cgd e = (*com->c_func)(0);
483 1.1 cgd break;
484 1.1 cgd
485 1.1 cgd default:
486 1.11 lukem errx(1, "Unknown argtype");
487 1.1 cgd }
488 1.1 cgd
489 1.1 cgd out:
490 1.1 cgd /*
491 1.1 cgd * Exit the current source file on
492 1.1 cgd * error.
493 1.1 cgd */
494 1.1 cgd if (e) {
495 1.1 cgd if (e < 0)
496 1.1 cgd return 1;
497 1.1 cgd if (loading)
498 1.1 cgd return 1;
499 1.1 cgd if (sourcing)
500 1.25 christos (void)unstack();
501 1.1 cgd return 0;
502 1.1 cgd }
503 1.7 christos if (com == NULL)
504 1.7 christos return(0);
505 1.18 wiz if (value("autoprint") != NULL && com->c_argtype & P)
506 1.1 cgd if ((dot->m_flag & MDELETED) == 0) {
507 1.1 cgd muvec[0] = dot - &message[0] + 1;
508 1.1 cgd muvec[1] = 0;
509 1.25 christos (void)type(muvec);
510 1.1 cgd }
511 1.1 cgd if (!sourcing && (com->c_argtype & T) == 0)
512 1.1 cgd sawcom = 1;
513 1.1 cgd return(0);
514 1.1 cgd }
515 1.1 cgd
516 1.1 cgd /*
517 1.1 cgd * Set the size of the message vector used to construct argument
518 1.1 cgd * lists to message list functions.
519 1.1 cgd */
520 1.4 deraadt void
521 1.17 wiz setmsize(int sz)
522 1.1 cgd {
523 1.1 cgd
524 1.1 cgd if (msgvec != 0)
525 1.28 christos free(msgvec);
526 1.28 christos msgvec = ecalloc((size_t) (sz + 1), sizeof *msgvec);
527 1.1 cgd }
528 1.1 cgd
529 1.1 cgd /*
530 1.1 cgd * Find the correct command in the command table corresponding
531 1.1 cgd * to the passed command "word"
532 1.1 cgd */
533 1.1 cgd
534 1.6 jtc const struct cmd *
535 1.17 wiz lex(char word[])
536 1.1 cgd {
537 1.11 lukem const struct cmd *cp;
538 1.1 cgd
539 1.18 wiz for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
540 1.1 cgd if (isprefix(word, cp->c_name))
541 1.1 cgd return(cp);
542 1.19 wiz return(NULL);
543 1.1 cgd }
544 1.1 cgd
545 1.1 cgd /*
546 1.1 cgd * Determine if as1 is a valid prefix of as2.
547 1.1 cgd * Return true if yep.
548 1.1 cgd */
549 1.4 deraadt int
550 1.24 christos isprefix(char *as1, const char *as2)
551 1.1 cgd {
552 1.24 christos char *s1;
553 1.24 christos const char *s2;
554 1.1 cgd
555 1.1 cgd s1 = as1;
556 1.1 cgd s2 = as2;
557 1.1 cgd while (*s1++ == *s2)
558 1.1 cgd if (*s2++ == '\0')
559 1.1 cgd return(1);
560 1.1 cgd return(*--s1 == '\0');
561 1.1 cgd }
562 1.1 cgd
563 1.1 cgd /*
564 1.1 cgd * The following gets called on receipt of an interrupt. This is
565 1.1 cgd * to abort printout of a command, mainly.
566 1.1 cgd * Dispatching here when command() is inactive crashes rcv.
567 1.1 cgd * Close all open files except 0, 1, 2, and the temporary.
568 1.1 cgd * Also, unstack all source files.
569 1.1 cgd */
570 1.1 cgd
571 1.1 cgd int inithdr; /* am printing startup headers */
572 1.1 cgd
573 1.1 cgd /*ARGSUSED*/
574 1.1 cgd void
575 1.28 christos intr(int s __unused)
576 1.1 cgd {
577 1.1 cgd
578 1.1 cgd noreset = 0;
579 1.1 cgd if (!inithdr)
580 1.1 cgd sawcom++;
581 1.1 cgd inithdr = 0;
582 1.1 cgd while (sourcing)
583 1.25 christos (void)unstack();
584 1.1 cgd
585 1.1 cgd close_all_files();
586 1.1 cgd
587 1.1 cgd if (image >= 0) {
588 1.25 christos (void)close(image);
589 1.1 cgd image = -1;
590 1.1 cgd }
591 1.25 christos (void)fprintf(stderr, "Interrupt\n");
592 1.1 cgd reset(0);
593 1.1 cgd }
594 1.1 cgd
595 1.1 cgd /*
596 1.1 cgd * When we wake up after ^Z, reprint the prompt.
597 1.1 cgd */
598 1.1 cgd void
599 1.17 wiz stop(int s)
600 1.1 cgd {
601 1.1 cgd sig_t old_action = signal(s, SIG_DFL);
602 1.7 christos sigset_t nset;
603 1.1 cgd
604 1.25 christos (void)sigemptyset(&nset);
605 1.25 christos (void)sigaddset(&nset, s);
606 1.25 christos (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
607 1.25 christos (void)kill(0, s);
608 1.25 christos (void)sigprocmask(SIG_BLOCK, &nset, NULL);
609 1.25 christos (void)signal(s, old_action);
610 1.1 cgd if (reset_on_stop) {
611 1.1 cgd reset_on_stop = 0;
612 1.1 cgd reset(0);
613 1.1 cgd }
614 1.1 cgd }
615 1.1 cgd
616 1.1 cgd /*
617 1.1 cgd * Branch here on hangup signal and simulate "exit".
618 1.1 cgd */
619 1.1 cgd /*ARGSUSED*/
620 1.1 cgd void
621 1.28 christos hangup(int s __unused)
622 1.1 cgd {
623 1.1 cgd
624 1.1 cgd /* nothing to do? */
625 1.1 cgd exit(1);
626 1.1 cgd }
627 1.1 cgd
628 1.1 cgd /*
629 1.1 cgd * Announce the presence of the current Mail version,
630 1.1 cgd * give the message count, and print a header listing.
631 1.1 cgd */
632 1.4 deraadt void
633 1.17 wiz announce(void)
634 1.1 cgd {
635 1.1 cgd int vec[2], mdot;
636 1.1 cgd
637 1.8 tls mdot = newfileinfo(0);
638 1.1 cgd vec[0] = mdot;
639 1.1 cgd vec[1] = 0;
640 1.1 cgd dot = &message[mdot - 1];
641 1.18 wiz if (msgCount > 0 && value("noheader") == NULL) {
642 1.1 cgd inithdr++;
643 1.25 christos (void)headers(vec);
644 1.1 cgd inithdr = 0;
645 1.1 cgd }
646 1.1 cgd }
647 1.1 cgd
648 1.1 cgd /*
649 1.1 cgd * Announce information about the file we are editing.
650 1.1 cgd * Return a likely place to set dot.
651 1.1 cgd */
652 1.4 deraadt int
653 1.17 wiz newfileinfo(int omsgCount)
654 1.1 cgd {
655 1.11 lukem struct message *mp;
656 1.25 christos int u, n, mdot, d, s;
657 1.25 christos size_t l;
658 1.9 mikel char fname[PATHSIZE], zname[PATHSIZE], *ename;
659 1.1 cgd
660 1.8 tls for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
661 1.1 cgd if (mp->m_flag & MNEW)
662 1.1 cgd break;
663 1.1 cgd if (mp >= &message[msgCount])
664 1.8 tls for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
665 1.1 cgd if ((mp->m_flag & MREAD) == 0)
666 1.1 cgd break;
667 1.1 cgd if (mp < &message[msgCount])
668 1.1 cgd mdot = mp - &message[0] + 1;
669 1.1 cgd else
670 1.8 tls mdot = omsgCount + 1;
671 1.1 cgd s = d = 0;
672 1.1 cgd for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
673 1.1 cgd if (mp->m_flag & MNEW)
674 1.1 cgd n++;
675 1.1 cgd if ((mp->m_flag & MREAD) == 0)
676 1.1 cgd u++;
677 1.1 cgd if (mp->m_flag & MDELETED)
678 1.1 cgd d++;
679 1.1 cgd if (mp->m_flag & MSAVED)
680 1.1 cgd s++;
681 1.1 cgd }
682 1.1 cgd ename = mailname;
683 1.1 cgd if (getfold(fname) >= 0) {
684 1.9 mikel l = strlen(fname);
685 1.9 mikel if (l < PATHSIZE - 1)
686 1.9 mikel fname[l++] = '/';
687 1.9 mikel if (strncmp(fname, mailname, l) == 0) {
688 1.25 christos (void)snprintf(zname, PATHSIZE, "+%s",
689 1.9 mikel mailname + l);
690 1.1 cgd ename = zname;
691 1.1 cgd }
692 1.1 cgd }
693 1.25 christos (void)printf("\"%s\": ", ename);
694 1.1 cgd if (msgCount == 1)
695 1.25 christos (void)printf("1 message");
696 1.1 cgd else
697 1.25 christos (void)printf("%d messages", msgCount);
698 1.1 cgd if (n > 0)
699 1.25 christos (void)printf(" %d new", n);
700 1.1 cgd if (u-n > 0)
701 1.25 christos (void)printf(" %d unread", u);
702 1.1 cgd if (d > 0)
703 1.25 christos (void)printf(" %d deleted", d);
704 1.1 cgd if (s > 0)
705 1.25 christos (void)printf(" %d saved", s);
706 1.1 cgd if (readonly)
707 1.25 christos (void)printf(" [Read only]");
708 1.25 christos (void)printf("\n");
709 1.1 cgd return(mdot);
710 1.1 cgd }
711 1.1 cgd
712 1.1 cgd /*
713 1.1 cgd * Print the current version number.
714 1.1 cgd */
715 1.1 cgd
716 1.1 cgd /*ARGSUSED*/
717 1.4 deraadt int
718 1.28 christos pversion(void *v __unused)
719 1.1 cgd {
720 1.25 christos (void)printf("Version %s\n", version);
721 1.1 cgd return(0);
722 1.1 cgd }
723 1.1 cgd
724 1.1 cgd /*
725 1.1 cgd * Load a file of user definitions.
726 1.1 cgd */
727 1.4 deraadt void
728 1.24 christos load(const char *name)
729 1.1 cgd {
730 1.11 lukem FILE *in, *oldin;
731 1.1 cgd
732 1.1 cgd if ((in = Fopen(name, "r")) == NULL)
733 1.1 cgd return;
734 1.1 cgd oldin = input;
735 1.1 cgd input = in;
736 1.1 cgd loading = 1;
737 1.1 cgd sourcing = 1;
738 1.1 cgd commands();
739 1.1 cgd loading = 0;
740 1.1 cgd sourcing = 0;
741 1.1 cgd input = oldin;
742 1.25 christos (void)Fclose(in);
743 1.1 cgd }
744