19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1980, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 350c3a8314SMike Heffner #if 0 36856f23edSMike Heffner static char sccsid[] = "@(#)lex.c 8.2 (Berkeley) 4/20/95"; 370c3a8314SMike Heffner #endif 389b50d902SRodney W. Grimes #endif /* not lint */ 39e026a48cSDavid E. O'Brien #include <sys/cdefs.h> 40e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$"); 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes #include "rcv.h" 439b50d902SRodney W. Grimes #include <errno.h> 449b50d902SRodney W. Grimes #include <fcntl.h> 459b50d902SRodney W. Grimes #include "extern.h" 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes /* 489b50d902SRodney W. Grimes * Mail -- a mail program 499b50d902SRodney W. Grimes * 509b50d902SRodney W. Grimes * Lexical processing of commands. 519b50d902SRodney W. Grimes */ 529b50d902SRodney W. Grimes 539ce73e90SMike Heffner const char *prompt = "& "; 549ce73e90SMike Heffner 559ce73e90SMike Heffner extern const struct cmd cmdtab[]; 569ce73e90SMike Heffner extern const char *version; 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes /* 599b50d902SRodney W. Grimes * Set up editing on the given file name. 609b50d902SRodney W. Grimes * If the first character of name is %, we are considered to be 619b50d902SRodney W. Grimes * editing the file, otherwise we are reading our mail which has 629b50d902SRodney W. Grimes * signficance for mbox and so forth. 63c92dfc23SMike Heffner * 64c92dfc23SMike Heffner * If the -e option is being passed to mail, this function has a 65c92dfc23SMike Heffner * tri-state return code: -1 on error, 0 on no mail, 1 if there is 66c92dfc23SMike Heffner * mail. 679b50d902SRodney W. Grimes */ 689b50d902SRodney W. Grimes int 699b50d902SRodney W. Grimes setfile(name) 709b50d902SRodney W. Grimes char *name; 719b50d902SRodney W. Grimes { 729b50d902SRodney W. Grimes FILE *ibuf; 73c92dfc23SMike Heffner int checkmode, i, fd; 749b50d902SRodney W. Grimes struct stat stb; 75856f23edSMike Heffner char isedit = *name != '%' || getuserid(myname) != getuid(); 769b50d902SRodney W. Grimes char *who = name[1] ? name + 1 : myname; 770c3a8314SMike Heffner char tempname[PATHSIZE]; 789b50d902SRodney W. Grimes static int shudclob; 799b50d902SRodney W. Grimes 809ce73e90SMike Heffner if ((name = expand(name)) == NULL) 819ce73e90SMike Heffner return (-1); 829b50d902SRodney W. Grimes 839b50d902SRodney W. Grimes if ((ibuf = Fopen(name, "r")) == NULL) { 849b50d902SRodney W. Grimes if (!isedit && errno == ENOENT) 859b50d902SRodney W. Grimes goto nomail; 860c3a8314SMike Heffner warn("%s", name); 879b50d902SRodney W. Grimes return (-1); 889b50d902SRodney W. Grimes } 899b50d902SRodney W. Grimes 909b50d902SRodney W. Grimes if (fstat(fileno(ibuf), &stb) < 0) { 910c3a8314SMike Heffner warn("fstat"); 929ce73e90SMike Heffner (void)Fclose(ibuf); 939b50d902SRodney W. Grimes return (-1); 949b50d902SRodney W. Grimes } 959b50d902SRodney W. Grimes 960c3a8314SMike Heffner if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) { 979ce73e90SMike Heffner (void)Fclose(ibuf); 980c3a8314SMike Heffner errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL; 990c3a8314SMike Heffner warn("%s", name); 1009b50d902SRodney W. Grimes return (-1); 1019b50d902SRodney W. Grimes } 1029b50d902SRodney W. Grimes 1039b50d902SRodney W. Grimes /* 1049b50d902SRodney W. Grimes * Looks like all will be well. We must now relinquish our 1059b50d902SRodney W. Grimes * hold on the current set of stuff. Must hold signals 1069b50d902SRodney W. Grimes * while we are reading the new file, else we will ruin 1079b50d902SRodney W. Grimes * the message[] data structure. 1089b50d902SRodney W. Grimes */ 1099b50d902SRodney W. Grimes 1109b50d902SRodney W. Grimes holdsigs(); 1119b50d902SRodney W. Grimes if (shudclob) 1129b50d902SRodney W. Grimes quit(); 1139b50d902SRodney W. Grimes 1149b50d902SRodney W. Grimes /* 1159b50d902SRodney W. Grimes * Copy the messages into /tmp 1169b50d902SRodney W. Grimes * and set pointers. 1179b50d902SRodney W. Grimes */ 1189b50d902SRodney W. Grimes 1199b50d902SRodney W. Grimes readonly = 0; 1209b50d902SRodney W. Grimes if ((i = open(name, 1)) < 0) 1219b50d902SRodney W. Grimes readonly++; 1229b50d902SRodney W. Grimes else 1239ce73e90SMike Heffner (void)close(i); 1249b50d902SRodney W. Grimes if (shudclob) { 1259ce73e90SMike Heffner (void)fclose(itf); 1269ce73e90SMike Heffner (void)fclose(otf); 1279b50d902SRodney W. Grimes } 1289b50d902SRodney W. Grimes shudclob = 1; 1299b50d902SRodney W. Grimes edit = isedit; 1300c3a8314SMike Heffner strlcpy(prevfile, mailname, sizeof(prevfile)); 1319b50d902SRodney W. Grimes if (name != mailname) 1320c3a8314SMike Heffner strlcpy(mailname, name, sizeof(mailname)); 1339b50d902SRodney W. Grimes mailsize = fsize(ibuf); 1349ce73e90SMike Heffner (void)snprintf(tempname, sizeof(tempname), 1359ce73e90SMike Heffner "%s/mail.RxXXXXXXXXXX", tmpdir); 1360c3a8314SMike Heffner if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL) 1370c3a8314SMike Heffner err(1, "%s", tempname); 1389b50d902SRodney W. Grimes (void)fcntl(fileno(otf), F_SETFD, 1); 1390c3a8314SMike Heffner if ((itf = fopen(tempname, "r")) == NULL) 1400c3a8314SMike Heffner err(1, "%s", tempname); 1419b50d902SRodney W. Grimes (void)fcntl(fileno(itf), F_SETFD, 1); 1429ce73e90SMike Heffner (void)rm(tempname); 143856f23edSMike Heffner setptr(ibuf, 0); 1449b50d902SRodney W. Grimes setmsize(msgCount); 145856f23edSMike Heffner /* 146856f23edSMike Heffner * New mail may have arrived while we were reading 147856f23edSMike Heffner * the mail file, so reset mailsize to be where 148856f23edSMike Heffner * we really are in the file... 149856f23edSMike Heffner */ 1506d48fa43SAndrey A. Chernov mailsize = ftello(ibuf); 1519ce73e90SMike Heffner (void)Fclose(ibuf); 1529b50d902SRodney W. Grimes relsesigs(); 1539b50d902SRodney W. Grimes sawcom = 0; 154c92dfc23SMike Heffner checkmode = value("checkmode") != NULL; 155c92dfc23SMike Heffner 156c92dfc23SMike Heffner if ((checkmode || !edit) && msgCount == 0) { 1579b50d902SRodney W. Grimes nomail: 158c92dfc23SMike Heffner if (!checkmode) { 1599b50d902SRodney W. Grimes fprintf(stderr, "No mail for %s\n", who); 1609ce73e90SMike Heffner return (-1); 161c92dfc23SMike Heffner } else 1629b50d902SRodney W. Grimes return (0); 1639b50d902SRodney W. Grimes } 164c92dfc23SMike Heffner return (checkmode ? 1 : 0); 165c92dfc23SMike Heffner } 1669b50d902SRodney W. Grimes 167856f23edSMike Heffner /* 168856f23edSMike Heffner * Incorporate any new mail that has arrived since we first 169856f23edSMike Heffner * started reading mail. 170856f23edSMike Heffner */ 171856f23edSMike Heffner int 172856f23edSMike Heffner incfile() 173856f23edSMike Heffner { 1746d48fa43SAndrey A. Chernov off_t newsize; 175856f23edSMike Heffner int omsgCount = msgCount; 176856f23edSMike Heffner FILE *ibuf; 177856f23edSMike Heffner 178856f23edSMike Heffner ibuf = Fopen(mailname, "r"); 179856f23edSMike Heffner if (ibuf == NULL) 180856f23edSMike Heffner return (-1); 181856f23edSMike Heffner holdsigs(); 182856f23edSMike Heffner newsize = fsize(ibuf); 183856f23edSMike Heffner if (newsize == 0) 184856f23edSMike Heffner return (-1); /* mail box is now empty??? */ 185856f23edSMike Heffner if (newsize < mailsize) 186856f23edSMike Heffner return (-1); /* mail box has shrunk??? */ 187856f23edSMike Heffner if (newsize == mailsize) 188856f23edSMike Heffner return (0); /* no new mail */ 189856f23edSMike Heffner setptr(ibuf, mailsize); 190856f23edSMike Heffner setmsize(msgCount); 1916d48fa43SAndrey A. Chernov mailsize = ftello(ibuf); 192856f23edSMike Heffner (void)Fclose(ibuf); 193856f23edSMike Heffner relsesigs(); 194856f23edSMike Heffner return (msgCount - omsgCount); 195856f23edSMike Heffner } 196856f23edSMike Heffner 1979b50d902SRodney W. Grimes int *msgvec; 1989b50d902SRodney W. Grimes int reset_on_stop; /* do a reset() if stopped */ 1999b50d902SRodney W. Grimes 2009b50d902SRodney W. Grimes /* 2019b50d902SRodney W. Grimes * Interpret user commands one by one. If standard input is not a tty, 2029b50d902SRodney W. Grimes * print no prompt. 2039b50d902SRodney W. Grimes */ 2049b50d902SRodney W. Grimes void 2059b50d902SRodney W. Grimes commands() 2069b50d902SRodney W. Grimes { 2079ce73e90SMike Heffner int n, eofloop = 0; 2089b50d902SRodney W. Grimes char linebuf[LINESIZE]; 2099b50d902SRodney W. Grimes 2109b50d902SRodney W. Grimes if (!sourcing) { 2119b50d902SRodney W. Grimes if (signal(SIGINT, SIG_IGN) != SIG_IGN) 2129ce73e90SMike Heffner (void)signal(SIGINT, intr); 2139b50d902SRodney W. Grimes if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 2149ce73e90SMike Heffner (void)signal(SIGHUP, hangup); 2159ce73e90SMike Heffner (void)signal(SIGTSTP, stop); 2169ce73e90SMike Heffner (void)signal(SIGTTOU, stop); 2179ce73e90SMike Heffner (void)signal(SIGTTIN, stop); 2189b50d902SRodney W. Grimes } 2199b50d902SRodney W. Grimes setexit(); 2209b50d902SRodney W. Grimes for (;;) { 2219b50d902SRodney W. Grimes /* 2229b50d902SRodney W. Grimes * Print the prompt, if needed. Clear out 2239b50d902SRodney W. Grimes * string space, and flush the output. 2249b50d902SRodney W. Grimes */ 2259ce73e90SMike Heffner if (!sourcing && value("interactive") != NULL) { 226856f23edSMike Heffner if ((value("autoinc") != NULL) && (incfile() > 0)) 227856f23edSMike Heffner printf("New mail has arrived.\n"); 2289b50d902SRodney W. Grimes reset_on_stop = 1; 229c858db96SKris Kennaway printf("%s", prompt); 2309b50d902SRodney W. Grimes } 2319ce73e90SMike Heffner (void)fflush(stdout); 2329b50d902SRodney W. Grimes sreset(); 2339b50d902SRodney W. Grimes /* 2349b50d902SRodney W. Grimes * Read a line of commands from the current input 2359b50d902SRodney W. Grimes * and handle end of file specially. 2369b50d902SRodney W. Grimes */ 2379b50d902SRodney W. Grimes n = 0; 2389b50d902SRodney W. Grimes for (;;) { 2399b50d902SRodney W. Grimes if (readline(input, &linebuf[n], LINESIZE - n) < 0) { 2409b50d902SRodney W. Grimes if (n == 0) 2419b50d902SRodney W. Grimes n = -1; 2429b50d902SRodney W. Grimes break; 2439b50d902SRodney W. Grimes } 2449b50d902SRodney W. Grimes if ((n = strlen(linebuf)) == 0) 2459b50d902SRodney W. Grimes break; 2469b50d902SRodney W. Grimes n--; 2479b50d902SRodney W. Grimes if (linebuf[n] != '\\') 2489b50d902SRodney W. Grimes break; 2499b50d902SRodney W. Grimes linebuf[n++] = ' '; 2509b50d902SRodney W. Grimes } 2519b50d902SRodney W. Grimes reset_on_stop = 0; 2529b50d902SRodney W. Grimes if (n < 0) { 2539b50d902SRodney W. Grimes /* eof */ 2549b50d902SRodney W. Grimes if (loading) 2559b50d902SRodney W. Grimes break; 2569b50d902SRodney W. Grimes if (sourcing) { 2579b50d902SRodney W. Grimes unstack(); 2589b50d902SRodney W. Grimes continue; 2599b50d902SRodney W. Grimes } 2609ce73e90SMike Heffner if (value("interactive") != NULL && 2619ce73e90SMike Heffner value("ignoreeof") != NULL && 2629b50d902SRodney W. Grimes ++eofloop < 25) { 2639b50d902SRodney W. Grimes printf("Use \"quit\" to quit.\n"); 2649b50d902SRodney W. Grimes continue; 2659b50d902SRodney W. Grimes } 2669b50d902SRodney W. Grimes break; 2679b50d902SRodney W. Grimes } 2689b50d902SRodney W. Grimes eofloop = 0; 2699b50d902SRodney W. Grimes if (execute(linebuf, 0)) 2709b50d902SRodney W. Grimes break; 2719b50d902SRodney W. Grimes } 2729b50d902SRodney W. Grimes } 2739b50d902SRodney W. Grimes 2749b50d902SRodney W. Grimes /* 2759b50d902SRodney W. Grimes * Execute a single command. 2769b50d902SRodney W. Grimes * Command functions return 0 for success, 1 for error, and -1 2779b50d902SRodney W. Grimes * for abort. A 1 or -1 aborts a load or source. A -1 aborts 2789b50d902SRodney W. Grimes * the interactive command loop. 2799b50d902SRodney W. Grimes * Contxt is non-zero if called while composing mail. 2809b50d902SRodney W. Grimes */ 2819b50d902SRodney W. Grimes int 2829b50d902SRodney W. Grimes execute(linebuf, contxt) 2839b50d902SRodney W. Grimes char linebuf[]; 2849b50d902SRodney W. Grimes int contxt; 2859b50d902SRodney W. Grimes { 2869b50d902SRodney W. Grimes char word[LINESIZE]; 2879b50d902SRodney W. Grimes char *arglist[MAXARGC]; 2889ce73e90SMike Heffner const struct cmd *com; 2899ce73e90SMike Heffner char *cp, *cp2; 2909ce73e90SMike Heffner int c, muvec[2]; 2919b50d902SRodney W. Grimes int e = 1; 2929b50d902SRodney W. Grimes 2939b50d902SRodney W. Grimes /* 2949b50d902SRodney W. Grimes * Strip the white space away from the beginning 2959b50d902SRodney W. Grimes * of the command, then scan out a word, which 2969b50d902SRodney W. Grimes * consists of anything except digits and white space. 2979b50d902SRodney W. Grimes * 2989b50d902SRodney W. Grimes * Handle ! escapes differently to get the correct 2999b50d902SRodney W. Grimes * lexical conventions. 3009b50d902SRodney W. Grimes */ 3019b50d902SRodney W. Grimes 3026d48fa43SAndrey A. Chernov for (cp = linebuf; isspace((unsigned char)*cp); cp++) 3039b50d902SRodney W. Grimes ; 3049b50d902SRodney W. Grimes if (*cp == '!') { 3059b50d902SRodney W. Grimes if (sourcing) { 3069b50d902SRodney W. Grimes printf("Can't \"!\" while sourcing\n"); 3079b50d902SRodney W. Grimes goto out; 3089b50d902SRodney W. Grimes } 3099b50d902SRodney W. Grimes shell(cp+1); 3109b50d902SRodney W. Grimes return (0); 3119b50d902SRodney W. Grimes } 3129b50d902SRodney W. Grimes cp2 = word; 3139ce73e90SMike Heffner while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL) 3149b50d902SRodney W. Grimes *cp2++ = *cp++; 3159b50d902SRodney W. Grimes *cp2 = '\0'; 3169b50d902SRodney W. Grimes 3179b50d902SRodney W. Grimes /* 3189b50d902SRodney W. Grimes * Look up the command; if not found, bitch. 3199b50d902SRodney W. Grimes * Normally, a blank command would map to the 3209b50d902SRodney W. Grimes * first command in the table; while sourcing, 3219b50d902SRodney W. Grimes * however, we ignore blank lines to eliminate 3229b50d902SRodney W. Grimes * confusion. 3239b50d902SRodney W. Grimes */ 3249b50d902SRodney W. Grimes 3259b50d902SRodney W. Grimes if (sourcing && *word == '\0') 3269b50d902SRodney W. Grimes return (0); 3279b50d902SRodney W. Grimes com = lex(word); 3289ce73e90SMike Heffner if (com == NULL) { 3299b50d902SRodney W. Grimes printf("Unknown command: \"%s\"\n", word); 3309b50d902SRodney W. Grimes goto out; 3319b50d902SRodney W. Grimes } 3329b50d902SRodney W. Grimes 3339b50d902SRodney W. Grimes /* 3349b50d902SRodney W. Grimes * See if we should execute the command -- if a conditional 3359b50d902SRodney W. Grimes * we always execute it, otherwise, check the state of cond. 3369b50d902SRodney W. Grimes */ 3379b50d902SRodney W. Grimes 3389b50d902SRodney W. Grimes if ((com->c_argtype & F) == 0) 3399ce73e90SMike Heffner if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode)) 3409b50d902SRodney W. Grimes return (0); 3419b50d902SRodney W. Grimes 3429b50d902SRodney W. Grimes /* 3439b50d902SRodney W. Grimes * Process the arguments to the command, depending 3449b50d902SRodney W. Grimes * on the type he expects. Default to an error. 3459b50d902SRodney W. Grimes * If we are sourcing an interactive command, it's 3469b50d902SRodney W. Grimes * an error. 3479b50d902SRodney W. Grimes */ 3489b50d902SRodney W. Grimes 3499b50d902SRodney W. Grimes if (!rcvmode && (com->c_argtype & M) == 0) { 3509b50d902SRodney W. Grimes printf("May not execute \"%s\" while sending\n", 3519b50d902SRodney W. Grimes com->c_name); 3529b50d902SRodney W. Grimes goto out; 3539b50d902SRodney W. Grimes } 3549b50d902SRodney W. Grimes if (sourcing && com->c_argtype & I) { 3559b50d902SRodney W. Grimes printf("May not execute \"%s\" while sourcing\n", 3569b50d902SRodney W. Grimes com->c_name); 3579b50d902SRodney W. Grimes goto out; 3589b50d902SRodney W. Grimes } 3599b50d902SRodney W. Grimes if (readonly && com->c_argtype & W) { 3609b50d902SRodney W. Grimes printf("May not execute \"%s\" -- message file is read only\n", 3619b50d902SRodney W. Grimes com->c_name); 3629b50d902SRodney W. Grimes goto out; 3639b50d902SRodney W. Grimes } 3649b50d902SRodney W. Grimes if (contxt && com->c_argtype & R) { 3659b50d902SRodney W. Grimes printf("Cannot recursively invoke \"%s\"\n", com->c_name); 3669b50d902SRodney W. Grimes goto out; 3679b50d902SRodney W. Grimes } 3689b50d902SRodney W. Grimes switch (com->c_argtype & ~(F|P|I|M|T|W|R)) { 3699b50d902SRodney W. Grimes case MSGLIST: 3709b50d902SRodney W. Grimes /* 3719b50d902SRodney W. Grimes * A message list defaulting to nearest forward 3729b50d902SRodney W. Grimes * legal message. 3739b50d902SRodney W. Grimes */ 3749b50d902SRodney W. Grimes if (msgvec == 0) { 3759b50d902SRodney W. Grimes printf("Illegal use of \"message list\"\n"); 3769b50d902SRodney W. Grimes break; 3779b50d902SRodney W. Grimes } 3789b50d902SRodney W. Grimes if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0) 3799b50d902SRodney W. Grimes break; 3809b50d902SRodney W. Grimes if (c == 0) { 3819ce73e90SMike Heffner *msgvec = first(com->c_msgflag, com->c_msgmask); 382d030d2d2SPoul-Henning Kamp msgvec[1] = 0; 3839b50d902SRodney W. Grimes } 384d030d2d2SPoul-Henning Kamp if (*msgvec == 0) { 3859b50d902SRodney W. Grimes printf("No applicable messages\n"); 3869b50d902SRodney W. Grimes break; 3879b50d902SRodney W. Grimes } 3889b50d902SRodney W. Grimes e = (*com->c_func)(msgvec); 3899b50d902SRodney W. Grimes break; 3909b50d902SRodney W. Grimes 3919b50d902SRodney W. Grimes case NDMLIST: 3929b50d902SRodney W. Grimes /* 3939b50d902SRodney W. Grimes * A message list with no defaults, but no error 3949b50d902SRodney W. Grimes * if none exist. 3959b50d902SRodney W. Grimes */ 3969b50d902SRodney W. Grimes if (msgvec == 0) { 3979b50d902SRodney W. Grimes printf("Illegal use of \"message list\"\n"); 3989b50d902SRodney W. Grimes break; 3999b50d902SRodney W. Grimes } 4009b50d902SRodney W. Grimes if (getmsglist(cp, msgvec, com->c_msgflag) < 0) 4019b50d902SRodney W. Grimes break; 4029b50d902SRodney W. Grimes e = (*com->c_func)(msgvec); 4039b50d902SRodney W. Grimes break; 4049b50d902SRodney W. Grimes 4059b50d902SRodney W. Grimes case STRLIST: 4069b50d902SRodney W. Grimes /* 4079b50d902SRodney W. Grimes * Just the straight string, with 4089b50d902SRodney W. Grimes * leading blanks removed. 4099b50d902SRodney W. Grimes */ 4106d48fa43SAndrey A. Chernov while (isspace((unsigned char)*cp)) 4119b50d902SRodney W. Grimes cp++; 4129b50d902SRodney W. Grimes e = (*com->c_func)(cp); 4139b50d902SRodney W. Grimes break; 4149b50d902SRodney W. Grimes 4159b50d902SRodney W. Grimes case RAWLIST: 4169b50d902SRodney W. Grimes /* 4179b50d902SRodney W. Grimes * A vector of strings, in shell style. 4189b50d902SRodney W. Grimes */ 4199b50d902SRodney W. Grimes if ((c = getrawlist(cp, arglist, 4209ce73e90SMike Heffner sizeof(arglist) / sizeof(*arglist))) < 0) 4219b50d902SRodney W. Grimes break; 4229b50d902SRodney W. Grimes if (c < com->c_minargs) { 4239b50d902SRodney W. Grimes printf("%s requires at least %d arg(s)\n", 4249b50d902SRodney W. Grimes com->c_name, com->c_minargs); 4259b50d902SRodney W. Grimes break; 4269b50d902SRodney W. Grimes } 4279b50d902SRodney W. Grimes if (c > com->c_maxargs) { 4289b50d902SRodney W. Grimes printf("%s takes no more than %d arg(s)\n", 4299b50d902SRodney W. Grimes com->c_name, com->c_maxargs); 4309b50d902SRodney W. Grimes break; 4319b50d902SRodney W. Grimes } 4329b50d902SRodney W. Grimes e = (*com->c_func)(arglist); 4339b50d902SRodney W. Grimes break; 4349b50d902SRodney W. Grimes 4359b50d902SRodney W. Grimes case NOLIST: 4369b50d902SRodney W. Grimes /* 4379b50d902SRodney W. Grimes * Just the constant zero, for exiting, 4389b50d902SRodney W. Grimes * eg. 4399b50d902SRodney W. Grimes */ 4409b50d902SRodney W. Grimes e = (*com->c_func)(0); 4419b50d902SRodney W. Grimes break; 4429b50d902SRodney W. Grimes 4439b50d902SRodney W. Grimes default: 4440c3a8314SMike Heffner errx(1, "Unknown argtype"); 4459b50d902SRodney W. Grimes } 4469b50d902SRodney W. Grimes 4479b50d902SRodney W. Grimes out: 4489b50d902SRodney W. Grimes /* 4499b50d902SRodney W. Grimes * Exit the current source file on 4509b50d902SRodney W. Grimes * error. 4519b50d902SRodney W. Grimes */ 4529b50d902SRodney W. Grimes if (e) { 4539b50d902SRodney W. Grimes if (e < 0) 4549ce73e90SMike Heffner return (1); 4559b50d902SRodney W. Grimes if (loading) 4569ce73e90SMike Heffner return (1); 4579b50d902SRodney W. Grimes if (sourcing) 4589b50d902SRodney W. Grimes unstack(); 4599ce73e90SMike Heffner return (0); 4609b50d902SRodney W. Grimes } 461856f23edSMike Heffner if (com == NULL) 462856f23edSMike Heffner return (0); 4639ce73e90SMike Heffner if (value("autoprint") != NULL && com->c_argtype & P) 4649b50d902SRodney W. Grimes if ((dot->m_flag & MDELETED) == 0) { 4659b50d902SRodney W. Grimes muvec[0] = dot - &message[0] + 1; 4669b50d902SRodney W. Grimes muvec[1] = 0; 4679b50d902SRodney W. Grimes type(muvec); 4689b50d902SRodney W. Grimes } 4699b50d902SRodney W. Grimes if (!sourcing && (com->c_argtype & T) == 0) 4709b50d902SRodney W. Grimes sawcom = 1; 4719b50d902SRodney W. Grimes return (0); 4729b50d902SRodney W. Grimes } 4739b50d902SRodney W. Grimes 4749b50d902SRodney W. Grimes /* 4759b50d902SRodney W. Grimes * Set the size of the message vector used to construct argument 4769b50d902SRodney W. Grimes * lists to message list functions. 4779b50d902SRodney W. Grimes */ 4789b50d902SRodney W. Grimes void 4799b50d902SRodney W. Grimes setmsize(sz) 4809b50d902SRodney W. Grimes int sz; 4819b50d902SRodney W. Grimes { 4829b50d902SRodney W. Grimes 483856f23edSMike Heffner if (msgvec != NULL) 4849ce73e90SMike Heffner (void)free(msgvec); 4859ce73e90SMike Heffner msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec)); 4869b50d902SRodney W. Grimes } 4879b50d902SRodney W. Grimes 4889b50d902SRodney W. Grimes /* 4899b50d902SRodney W. Grimes * Find the correct command in the command table corresponding 4909b50d902SRodney W. Grimes * to the passed command "word" 4919b50d902SRodney W. Grimes */ 4929b50d902SRodney W. Grimes 4939ce73e90SMike Heffner __const struct cmd * 4949b50d902SRodney W. Grimes lex(word) 4959b50d902SRodney W. Grimes char word[]; 4969b50d902SRodney W. Grimes { 4979ce73e90SMike Heffner const struct cmd *cp; 4989b50d902SRodney W. Grimes 499b5cfa4b2SJoerg Wunsch /* 500b5cfa4b2SJoerg Wunsch * ignore trailing chars after `#' 501b5cfa4b2SJoerg Wunsch * 502b5cfa4b2SJoerg Wunsch * lines with beginning `#' are comments 5030c3a8314SMike Heffner * spaces before `#' are ignored in execute() 504b5cfa4b2SJoerg Wunsch */ 505b5cfa4b2SJoerg Wunsch 506b5cfa4b2SJoerg Wunsch if (*word == '#') 507b5cfa4b2SJoerg Wunsch *(word+1) = '\0'; 508b5cfa4b2SJoerg Wunsch 509b5cfa4b2SJoerg Wunsch 5109ce73e90SMike Heffner for (cp = &cmdtab[0]; cp->c_name != NULL; cp++) 5119b50d902SRodney W. Grimes if (isprefix(word, cp->c_name)) 5129b50d902SRodney W. Grimes return (cp); 5139ce73e90SMike Heffner return (NULL); 5149b50d902SRodney W. Grimes } 5159b50d902SRodney W. Grimes 5169b50d902SRodney W. Grimes /* 5179b50d902SRodney W. Grimes * Determine if as1 is a valid prefix of as2. 5189b50d902SRodney W. Grimes * Return true if yep. 5199b50d902SRodney W. Grimes */ 5209b50d902SRodney W. Grimes int 5219b50d902SRodney W. Grimes isprefix(as1, as2) 5229ce73e90SMike Heffner const char *as1, *as2; 5239b50d902SRodney W. Grimes { 5249ce73e90SMike Heffner const char *s1, *s2; 5259b50d902SRodney W. Grimes 5269b50d902SRodney W. Grimes s1 = as1; 5279b50d902SRodney W. Grimes s2 = as2; 5289b50d902SRodney W. Grimes while (*s1++ == *s2) 5299b50d902SRodney W. Grimes if (*s2++ == '\0') 5309b50d902SRodney W. Grimes return (1); 5319b50d902SRodney W. Grimes return (*--s1 == '\0'); 5329b50d902SRodney W. Grimes } 5339b50d902SRodney W. Grimes 5349b50d902SRodney W. Grimes /* 5359b50d902SRodney W. Grimes * The following gets called on receipt of an interrupt. This is 5369b50d902SRodney W. Grimes * to abort printout of a command, mainly. 5379b50d902SRodney W. Grimes * Dispatching here when command() is inactive crashes rcv. 5389b50d902SRodney W. Grimes * Close all open files except 0, 1, 2, and the temporary. 5399b50d902SRodney W. Grimes * Also, unstack all source files. 5409b50d902SRodney W. Grimes */ 5419b50d902SRodney W. Grimes 5429b50d902SRodney W. Grimes int inithdr; /* am printing startup headers */ 5439b50d902SRodney W. Grimes 5449b50d902SRodney W. Grimes /*ARGSUSED*/ 5459b50d902SRodney W. Grimes void 5469b50d902SRodney W. Grimes intr(s) 5479b50d902SRodney W. Grimes int s; 5489b50d902SRodney W. Grimes { 5499b50d902SRodney W. Grimes 5509b50d902SRodney W. Grimes noreset = 0; 5519b50d902SRodney W. Grimes if (!inithdr) 5529b50d902SRodney W. Grimes sawcom++; 5539b50d902SRodney W. Grimes inithdr = 0; 5549b50d902SRodney W. Grimes while (sourcing) 5559b50d902SRodney W. Grimes unstack(); 5569b50d902SRodney W. Grimes 5579b50d902SRodney W. Grimes close_all_files(); 5589b50d902SRodney W. Grimes 5599b50d902SRodney W. Grimes if (image >= 0) { 5609ce73e90SMike Heffner (void)close(image); 5619b50d902SRodney W. Grimes image = -1; 5629b50d902SRodney W. Grimes } 5639b50d902SRodney W. Grimes fprintf(stderr, "Interrupt\n"); 5649b50d902SRodney W. Grimes reset(0); 5659b50d902SRodney W. Grimes } 5669b50d902SRodney W. Grimes 5679b50d902SRodney W. Grimes /* 5689b50d902SRodney W. Grimes * When we wake up after ^Z, reprint the prompt. 5699b50d902SRodney W. Grimes */ 5709b50d902SRodney W. Grimes void 5719b50d902SRodney W. Grimes stop(s) 5729b50d902SRodney W. Grimes int s; 5739b50d902SRodney W. Grimes { 5749b50d902SRodney W. Grimes sig_t old_action = signal(s, SIG_DFL); 575856f23edSMike Heffner sigset_t nset; 5769b50d902SRodney W. Grimes 577856f23edSMike Heffner (void)sigemptyset(&nset); 578856f23edSMike Heffner (void)sigaddset(&nset, s); 579856f23edSMike Heffner (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 5809ce73e90SMike Heffner (void)kill(0, s); 581856f23edSMike Heffner (void)sigprocmask(SIG_BLOCK, &nset, NULL); 5829ce73e90SMike Heffner (void)signal(s, old_action); 5839b50d902SRodney W. Grimes if (reset_on_stop) { 5849b50d902SRodney W. Grimes reset_on_stop = 0; 5859b50d902SRodney W. Grimes reset(0); 5869b50d902SRodney W. Grimes } 5879b50d902SRodney W. Grimes } 5889b50d902SRodney W. Grimes 5899b50d902SRodney W. Grimes /* 5909b50d902SRodney W. Grimes * Branch here on hangup signal and simulate "exit". 5919b50d902SRodney W. Grimes */ 5929b50d902SRodney W. Grimes /*ARGSUSED*/ 5939b50d902SRodney W. Grimes void 5949b50d902SRodney W. Grimes hangup(s) 5959b50d902SRodney W. Grimes int s; 5969b50d902SRodney W. Grimes { 5979b50d902SRodney W. Grimes 5989b50d902SRodney W. Grimes /* nothing to do? */ 5999b50d902SRodney W. Grimes exit(1); 6009b50d902SRodney W. Grimes } 6019b50d902SRodney W. Grimes 6029b50d902SRodney W. Grimes /* 6039b50d902SRodney W. Grimes * Announce the presence of the current Mail version, 6049b50d902SRodney W. Grimes * give the message count, and print a header listing. 6059b50d902SRodney W. Grimes */ 6069b50d902SRodney W. Grimes void 6079b50d902SRodney W. Grimes announce() 6089b50d902SRodney W. Grimes { 6099b50d902SRodney W. Grimes int vec[2], mdot; 6109b50d902SRodney W. Grimes 611856f23edSMike Heffner mdot = newfileinfo(0); 6129b50d902SRodney W. Grimes vec[0] = mdot; 6139b50d902SRodney W. Grimes vec[1] = 0; 6149b50d902SRodney W. Grimes dot = &message[mdot - 1]; 6159ce73e90SMike Heffner if (msgCount > 0 && value("noheader") == NULL) { 6169b50d902SRodney W. Grimes inithdr++; 6179b50d902SRodney W. Grimes headers(vec); 6189b50d902SRodney W. Grimes inithdr = 0; 6199b50d902SRodney W. Grimes } 6209b50d902SRodney W. Grimes } 6219b50d902SRodney W. Grimes 6229b50d902SRodney W. Grimes /* 6239b50d902SRodney W. Grimes * Announce information about the file we are editing. 6249b50d902SRodney W. Grimes * Return a likely place to set dot. 6259b50d902SRodney W. Grimes */ 6269b50d902SRodney W. Grimes int 627856f23edSMike Heffner newfileinfo(omsgCount) 628856f23edSMike Heffner int omsgCount; 6299b50d902SRodney W. Grimes { 6309ce73e90SMike Heffner struct message *mp; 6319ce73e90SMike Heffner int u, n, mdot, d, s; 6320c3a8314SMike Heffner char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename; 6339b50d902SRodney W. Grimes 634856f23edSMike Heffner for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++) 6359b50d902SRodney W. Grimes if (mp->m_flag & MNEW) 6369b50d902SRodney W. Grimes break; 6379b50d902SRodney W. Grimes if (mp >= &message[msgCount]) 638856f23edSMike Heffner for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++) 6399b50d902SRodney W. Grimes if ((mp->m_flag & MREAD) == 0) 6409b50d902SRodney W. Grimes break; 6419b50d902SRodney W. Grimes if (mp < &message[msgCount]) 6429b50d902SRodney W. Grimes mdot = mp - &message[0] + 1; 6439b50d902SRodney W. Grimes else 644856f23edSMike Heffner mdot = omsgCount + 1; 6459b50d902SRodney W. Grimes s = d = 0; 6469b50d902SRodney W. Grimes for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) { 6479b50d902SRodney W. Grimes if (mp->m_flag & MNEW) 6489b50d902SRodney W. Grimes n++; 6499b50d902SRodney W. Grimes if ((mp->m_flag & MREAD) == 0) 6509b50d902SRodney W. Grimes u++; 6519b50d902SRodney W. Grimes if (mp->m_flag & MDELETED) 6529b50d902SRodney W. Grimes d++; 6539b50d902SRodney W. Grimes if (mp->m_flag & MSAVED) 6549b50d902SRodney W. Grimes s++; 6559b50d902SRodney W. Grimes } 6569b50d902SRodney W. Grimes ename = mailname; 6570c3a8314SMike Heffner if (getfold(fname, sizeof(fname) - 1) >= 0) { 6589b50d902SRodney W. Grimes strcat(fname, "/"); 6599b50d902SRodney W. Grimes if (strncmp(fname, mailname, strlen(fname)) == 0) { 6609ce73e90SMike Heffner (void)snprintf(zname, sizeof(zname), "+%s", 6619ce73e90SMike Heffner mailname + strlen(fname)); 6629b50d902SRodney W. Grimes ename = zname; 6639b50d902SRodney W. Grimes } 6649b50d902SRodney W. Grimes } 6659b50d902SRodney W. Grimes printf("\"%s\": ", ename); 6669b50d902SRodney W. Grimes if (msgCount == 1) 6679b50d902SRodney W. Grimes printf("1 message"); 6689b50d902SRodney W. Grimes else 6699b50d902SRodney W. Grimes printf("%d messages", msgCount); 6709b50d902SRodney W. Grimes if (n > 0) 6719b50d902SRodney W. Grimes printf(" %d new", n); 6729b50d902SRodney W. Grimes if (u-n > 0) 6739b50d902SRodney W. Grimes printf(" %d unread", u); 6749b50d902SRodney W. Grimes if (d > 0) 6759b50d902SRodney W. Grimes printf(" %d deleted", d); 6769b50d902SRodney W. Grimes if (s > 0) 6779b50d902SRodney W. Grimes printf(" %d saved", s); 6789b50d902SRodney W. Grimes if (readonly) 6799b50d902SRodney W. Grimes printf(" [Read only]"); 6809b50d902SRodney W. Grimes printf("\n"); 6819b50d902SRodney W. Grimes return (mdot); 6829b50d902SRodney W. Grimes } 6839b50d902SRodney W. Grimes 6849b50d902SRodney W. Grimes /* 6859b50d902SRodney W. Grimes * Print the current version number. 6869b50d902SRodney W. Grimes */ 6879b50d902SRodney W. Grimes 6889b50d902SRodney W. Grimes /*ARGSUSED*/ 6899b50d902SRodney W. Grimes int 6909b50d902SRodney W. Grimes pversion(e) 6919b50d902SRodney W. Grimes int e; 6929b50d902SRodney W. Grimes { 6939b50d902SRodney W. Grimes 6949b50d902SRodney W. Grimes printf("Version %s\n", version); 6959b50d902SRodney W. Grimes return (0); 6969b50d902SRodney W. Grimes } 6979b50d902SRodney W. Grimes 6989b50d902SRodney W. Grimes /* 6999b50d902SRodney W. Grimes * Load a file of user definitions. 7009b50d902SRodney W. Grimes */ 7019b50d902SRodney W. Grimes void 7029b50d902SRodney W. Grimes load(name) 7039b50d902SRodney W. Grimes char *name; 7049b50d902SRodney W. Grimes { 7059ce73e90SMike Heffner FILE *in, *oldin; 7069b50d902SRodney W. Grimes 7079b50d902SRodney W. Grimes if ((in = Fopen(name, "r")) == NULL) 7089b50d902SRodney W. Grimes return; 7099b50d902SRodney W. Grimes oldin = input; 7109b50d902SRodney W. Grimes input = in; 7119b50d902SRodney W. Grimes loading = 1; 7129b50d902SRodney W. Grimes sourcing = 1; 7139b50d902SRodney W. Grimes commands(); 7149b50d902SRodney W. Grimes loading = 0; 7159b50d902SRodney W. Grimes sourcing = 0; 7169b50d902SRodney W. Grimes input = oldin; 7179ce73e90SMike Heffner (void)Fclose(in); 7189b50d902SRodney W. Grimes } 719