[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mail

Annotation of src/usr.bin/mail/main.c, Revision 1.29

1.29    ! millert     1: /*     $OpenBSD: main.c,v 1.28 2015/01/20 16:59:07 millert Exp $       */
1.5       millert     2: /*     $NetBSD: main.c,v 1.7 1997/05/13 06:15:57 mikel Exp $   */
1.2       deraadt     3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.18      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include "rcv.h"
                     34: #include <fcntl.h>
1.2       deraadt    35: #include <sys/ioctl.h>
1.1       deraadt    36: #include "extern.h"
                     37:
1.16      millert    38: __dead void    usage(void);
                     39:        int     main(int, char **);
1.10      millert    40:
1.1       deraadt    41: /*
                     42:  * Mail -- a mail program
                     43:  *
                     44:  * Startup -- interface with user.
                     45:  */
                     46:
                     47: int
1.16      millert    48: main(int argc, char **argv)
1.1       deraadt    49: {
1.10      millert    50:        int i;
1.1       deraadt    51:        struct name *to, *cc, *bcc, *smopts;
1.28      millert    52:        char *fromaddr;
1.1       deraadt    53:        char *subject;
                     54:        char *ef;
                     55:        char nosrc = 0;
1.5       millert    56:        char *rc;
1.17      millert    57:        extern const char version[];
1.1       deraadt    58:
                     59:        /*
                     60:         * Set up a reasonable environment.
                     61:         * Figure out whether we are being run interactively,
                     62:         * start the SIGCHLD catcher, and so forth.
                     63:         */
1.6       millert    64:        (void)signal(SIGCHLD, sigchild);
1.15      millert    65:        (void)signal(SIGPIPE, SIG_IGN);
1.1       deraadt    66:        if (isatty(0))
                     67:                assign("interactive", "");
                     68:        image = -1;
                     69:        /*
                     70:         * Now, determine how we are being used.
                     71:         * We successively pick off - flags.
                     72:         * If there is anything left, it is the base of the list
                     73:         * of users to mail to.  Argp will be set to point to the
                     74:         * first of these users.
                     75:         */
1.7       millert    76:        ef = NULL;
1.16      millert    77:        to = NULL;
                     78:        cc = NULL;
                     79:        bcc = NULL;
                     80:        smopts = NULL;
1.28      millert    81:        fromaddr = NULL;
1.7       millert    82:        subject = NULL;
1.28      millert    83:        while ((i = getopt(argc, argv, "EIN:b:c:dfinr:s:u:v")) != -1) {
1.1       deraadt    84:                switch (i) {
                     85:                case 'u':
                     86:                        /*
                     87:                         * Next argument is person to pretend to be.
                     88:                         */
1.27      deraadt    89:                        if (strlen(optarg) >= LOGIN_NAME_MAX)
1.14      millert    90:                                errx(1, "username `%s' too long", optarg);
1.3       deraadt    91:                        unsetenv("MAIL");
1.1       deraadt    92:                        myname = optarg;
1.11      millert    93:                        uflag = 1;
1.1       deraadt    94:                        break;
                     95:                case 'i':
                     96:                        /*
                     97:                         * User wants to ignore interrupts.
                     98:                         * Set the variable "ignore"
                     99:                         */
                    100:                        assign("ignore", "");
                    101:                        break;
                    102:                case 'd':
                    103:                        debug++;
                    104:                        break;
1.28      millert   105:                case 'r':
                    106:                        /*
                    107:                         * Set From: address
                    108:                         */
                    109:                        fromaddr = optarg;
                    110:                        break;
1.1       deraadt   111:                case 's':
                    112:                        /*
                    113:                         * Give a subject field for sending from
                    114:                         * non terminal
                    115:                         */
                    116:                        subject = optarg;
                    117:                        break;
                    118:                case 'f':
                    119:                        /*
                    120:                         * User is specifying file to "edit" with Mail,
                    121:                         * as opposed to reading system mailbox.
1.25      millert   122:                         * We read his mbox file unless another file
                    123:                         * is specified after the arguments.
                    124:                         */
                    125:                        ef = "&";
1.1       deraadt   126:                        break;
                    127:                case 'n':
                    128:                        /*
                    129:                         * User doesn't want to source /usr/lib/Mail.rc
                    130:                         */
                    131:                        nosrc++;
                    132:                        break;
                    133:                case 'N':
                    134:                        /*
                    135:                         * Avoid initial header printing.
                    136:                         */
                    137:                        assign("noheader", "");
                    138:                        break;
                    139:                case 'v':
                    140:                        /*
                    141:                         * Send mailer verbose flag
                    142:                         */
                    143:                        assign("verbose", "");
                    144:                        break;
                    145:                case 'I':
                    146:                        /*
                    147:                         * We're interactive
                    148:                         */
                    149:                        assign("interactive", "");
                    150:                        break;
                    151:                case 'c':
                    152:                        /*
                    153:                         * Get Carbon Copy Recipient list
                    154:                         */
                    155:                        cc = cat(cc, nalloc(optarg, GCC));
                    156:                        break;
                    157:                case 'b':
                    158:                        /*
                    159:                         * Get Blind Carbon Copy Recipient list
                    160:                         */
                    161:                        bcc = cat(bcc, nalloc(optarg, GBCC));
                    162:                        break;
1.21      martynas  163:                case 'E':
                    164:                        /*
                    165:                         * Don't send messages with an empty body.
                    166:                         */
                    167:                        assign("skipempty", "");
                    168:                        break;
1.15      millert   169:                default:
                    170:                        usage();
                    171:                        /*NOTREACHED*/
1.1       deraadt   172:                }
                    173:        }
1.25      millert   174:        if (ef != NULL) {
                    175:                /* Check for optional mailbox file name. */
                    176:                if (optind < argc) {
                    177:                        ef = argv[optind++];
                    178:                        if (optind < argc)
                    179:                            errx(1, "Cannot give -f and people to send to");
                    180:                }
                    181:        } else {
1.26      millert   182:                for (i = optind; argv[i]; i++)
1.25      millert   183:                        to = cat(to, nalloc(argv[i], GTO));
                    184:        }
1.1       deraadt   185:        /*
                    186:         * Check for inconsistent arguments.
                    187:         */
1.29    ! millert   188:        if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL ||
        !           189:            fromaddr != NULL))
        !           190:                errx(1, "You must specify direct recipients with -s, -c, -b, "
        !           191:                    "or -r");
1.15      millert   192:        /*
                    193:         * Block SIGINT except where we install an explicit handler for it.
                    194:         */
                    195:        sigemptyset(&intset);
                    196:        sigaddset(&intset, SIGINT);
                    197:        (void)sigprocmask(SIG_BLOCK, &intset, NULL);
                    198:        /*
                    199:         * Initialization.
                    200:         */
1.1       deraadt   201:        tinit();
                    202:        setscreensize();
                    203:        input = stdin;
                    204:        rcvmode = !to;
                    205:        spreserve();
                    206:        if (!nosrc)
                    207:                load(_PATH_MASTER_RC);
                    208:        /*
                    209:         * Expand returns a savestr, but load only uses the file name
                    210:         * for fopen, so it's safe to do this.
                    211:         */
1.5       millert   212:        if ((rc = getenv("MAILRC")) == 0)
                    213:                rc = "~/.mailrc";
                    214:        load(expand(rc));
1.1       deraadt   215:        if (!rcvmode) {
1.28      millert   216:                mail(to, cc, bcc, smopts, fromaddr, subject);
1.1       deraadt   217:                /*
                    218:                 * why wait?
                    219:                 */
                    220:                exit(senderr);
                    221:        }
                    222:        /*
                    223:         * Ok, we are reading mail.
                    224:         * Decide whether we are editing a mailbox or reading
                    225:         * the system mailbox, and open up the right stuff.
                    226:         */
1.7       millert   227:        if (ef == NULL)
1.1       deraadt   228:                ef = "%";
                    229:        if (setfile(ef) < 0)
                    230:                exit(1);                /* error already reported */
                    231:
1.15      millert   232:        if (value("quiet") == NULL)
                    233:                (void)printf("Mail version %s.  Type ? for help.\n",
                    234:                        version);
                    235:        announce();
                    236:        (void)fflush(stdout);
1.1       deraadt   237:        commands();
1.15      millert   238:        (void)ignoresig(SIGHUP, NULL, NULL);
                    239:        (void)ignoresig(SIGINT, NULL, NULL);
                    240:        (void)ignoresig(SIGQUIT, NULL, NULL);
1.1       deraadt   241:        quit();
                    242:        exit(0);
                    243: }
                    244:
                    245: /*
                    246:  * Compute what the screen size for printing headers should be.
                    247:  * We use the following algorithm for the height:
                    248:  *     If baud rate < 1200, use  9
                    249:  *     If baud rate = 1200, use 14
                    250:  *     If baud rate > 1200, use 24 or ws_row
                    251:  * Width is either 80 or ws_col;
                    252:  */
                    253: void
1.16      millert   254: setscreensize(void)
1.1       deraadt   255: {
                    256:        struct termios tbuf;
                    257:        struct winsize ws;
                    258:        speed_t ospeed;
                    259:
                    260:        if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
                    261:                ws.ws_col = ws.ws_row = 0;
                    262:        if (tcgetattr(1, &tbuf) < 0)
                    263:                ospeed = 9600;
                    264:        else
                    265:                ospeed = cfgetospeed(&tbuf);
1.8       millert   266:        if (ospeed < B1200)
1.1       deraadt   267:                screenheight = 9;
1.8       millert   268:        else if (ospeed == B1200)
1.1       deraadt   269:                screenheight = 14;
                    270:        else if (ws.ws_row != 0)
                    271:                screenheight = ws.ws_row;
                    272:        else
                    273:                screenheight = 24;
                    274:        if ((realscreenheight = ws.ws_row) == 0)
                    275:                realscreenheight = 24;
                    276:        if ((screenwidth = ws.ws_col) == 0)
                    277:                screenwidth = 80;
1.15      millert   278: }
                    279:
                    280: __dead void
1.16      millert   281: usage(void)
1.15      millert   282: {
                    283:
1.22      jmc       284:        fprintf(stderr, "usage: %s [-dEIinv] [-b list] [-c list] "
1.28      millert   285:            "[-r from-addr] [-s subject] to-addr ...\n", __progname);
1.22      jmc       286:        fprintf(stderr, "       %s [-dEIiNnv] -f [file]\n", __progname);
                    287:        fprintf(stderr, "       %s [-dEIiNnv] [-u user]\n", __progname);
1.15      millert   288:        exit(1);
1.1       deraadt   289: }