[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.27

1.27    ! deraadt     1: /*     $OpenBSD: main.c,v 1.26 2014/12/16 18:37:17 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;
                     52:        char *subject;
                     53:        char *ef;
                     54:        char nosrc = 0;
1.5       millert    55:        char *rc;
1.17      millert    56:        extern const char version[];
1.1       deraadt    57:
                     58:        /*
                     59:         * Set up a reasonable environment.
                     60:         * Figure out whether we are being run interactively,
                     61:         * start the SIGCHLD catcher, and so forth.
                     62:         */
1.6       millert    63:        (void)signal(SIGCHLD, sigchild);
1.15      millert    64:        (void)signal(SIGPIPE, SIG_IGN);
1.1       deraadt    65:        if (isatty(0))
                     66:                assign("interactive", "");
                     67:        image = -1;
                     68:        /*
                     69:         * Now, determine how we are being used.
                     70:         * We successively pick off - flags.
                     71:         * If there is anything left, it is the base of the list
                     72:         * of users to mail to.  Argp will be set to point to the
                     73:         * first of these users.
                     74:         */
1.7       millert    75:        ef = NULL;
1.16      millert    76:        to = NULL;
                     77:        cc = NULL;
                     78:        bcc = NULL;
                     79:        smopts = NULL;
1.7       millert    80:        subject = NULL;
1.24      millert    81:        while ((i = getopt(argc, argv, "EIN:b:c:dfins:u:v")) != -1) {
1.1       deraadt    82:                switch (i) {
                     83:                case 'u':
                     84:                        /*
                     85:                         * Next argument is person to pretend to be.
                     86:                         */
1.27    ! deraadt    87:                        if (strlen(optarg) >= LOGIN_NAME_MAX)
1.14      millert    88:                                errx(1, "username `%s' too long", optarg);
1.3       deraadt    89:                        unsetenv("MAIL");
1.1       deraadt    90:                        myname = optarg;
1.11      millert    91:                        uflag = 1;
1.1       deraadt    92:                        break;
                     93:                case 'i':
                     94:                        /*
                     95:                         * User wants to ignore interrupts.
                     96:                         * Set the variable "ignore"
                     97:                         */
                     98:                        assign("ignore", "");
                     99:                        break;
                    100:                case 'd':
                    101:                        debug++;
                    102:                        break;
                    103:                case 's':
                    104:                        /*
                    105:                         * Give a subject field for sending from
                    106:                         * non terminal
                    107:                         */
                    108:                        subject = optarg;
                    109:                        break;
                    110:                case 'f':
                    111:                        /*
                    112:                         * User is specifying file to "edit" with Mail,
                    113:                         * as opposed to reading system mailbox.
1.25      millert   114:                         * We read his mbox file unless another file
                    115:                         * is specified after the arguments.
                    116:                         */
                    117:                        ef = "&";
1.1       deraadt   118:                        break;
                    119:                case 'n':
                    120:                        /*
                    121:                         * User doesn't want to source /usr/lib/Mail.rc
                    122:                         */
                    123:                        nosrc++;
                    124:                        break;
                    125:                case 'N':
                    126:                        /*
                    127:                         * Avoid initial header printing.
                    128:                         */
                    129:                        assign("noheader", "");
                    130:                        break;
                    131:                case 'v':
                    132:                        /*
                    133:                         * Send mailer verbose flag
                    134:                         */
                    135:                        assign("verbose", "");
                    136:                        break;
                    137:                case 'I':
                    138:                        /*
                    139:                         * We're interactive
                    140:                         */
                    141:                        assign("interactive", "");
                    142:                        break;
                    143:                case 'c':
                    144:                        /*
                    145:                         * Get Carbon Copy Recipient list
                    146:                         */
                    147:                        cc = cat(cc, nalloc(optarg, GCC));
                    148:                        break;
                    149:                case 'b':
                    150:                        /*
                    151:                         * Get Blind Carbon Copy Recipient list
                    152:                         */
                    153:                        bcc = cat(bcc, nalloc(optarg, GBCC));
                    154:                        break;
1.21      martynas  155:                case 'E':
                    156:                        /*
                    157:                         * Don't send messages with an empty body.
                    158:                         */
                    159:                        assign("skipempty", "");
                    160:                        break;
1.15      millert   161:                default:
                    162:                        usage();
                    163:                        /*NOTREACHED*/
1.1       deraadt   164:                }
                    165:        }
1.25      millert   166:        if (ef != NULL) {
                    167:                /* Check for optional mailbox file name. */
                    168:                if (optind < argc) {
                    169:                        ef = argv[optind++];
                    170:                        if (optind < argc)
                    171:                            errx(1, "Cannot give -f and people to send to");
                    172:                }
                    173:        } else {
1.26      millert   174:                for (i = optind; argv[i]; i++)
1.25      millert   175:                        to = cat(to, nalloc(argv[i], GTO));
                    176:        }
1.1       deraadt   177:        /*
                    178:         * Check for inconsistent arguments.
                    179:         */
1.16      millert   180:        if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
1.5       millert   181:                errx(1, "You must specify direct recipients with -s, -c, or -b");
1.15      millert   182:        /*
                    183:         * Block SIGINT except where we install an explicit handler for it.
                    184:         */
                    185:        sigemptyset(&intset);
                    186:        sigaddset(&intset, SIGINT);
                    187:        (void)sigprocmask(SIG_BLOCK, &intset, NULL);
                    188:        /*
                    189:         * Initialization.
                    190:         */
1.1       deraadt   191:        tinit();
                    192:        setscreensize();
                    193:        input = stdin;
                    194:        rcvmode = !to;
                    195:        spreserve();
                    196:        if (!nosrc)
                    197:                load(_PATH_MASTER_RC);
                    198:        /*
                    199:         * Expand returns a savestr, but load only uses the file name
                    200:         * for fopen, so it's safe to do this.
                    201:         */
1.5       millert   202:        if ((rc = getenv("MAILRC")) == 0)
                    203:                rc = "~/.mailrc";
                    204:        load(expand(rc));
1.1       deraadt   205:        if (!rcvmode) {
                    206:                mail(to, cc, bcc, smopts, subject);
                    207:                /*
                    208:                 * why wait?
                    209:                 */
                    210:                exit(senderr);
                    211:        }
                    212:        /*
                    213:         * Ok, we are reading mail.
                    214:         * Decide whether we are editing a mailbox or reading
                    215:         * the system mailbox, and open up the right stuff.
                    216:         */
1.7       millert   217:        if (ef == NULL)
1.1       deraadt   218:                ef = "%";
                    219:        if (setfile(ef) < 0)
                    220:                exit(1);                /* error already reported */
                    221:
1.15      millert   222:        if (value("quiet") == NULL)
                    223:                (void)printf("Mail version %s.  Type ? for help.\n",
                    224:                        version);
                    225:        announce();
                    226:        (void)fflush(stdout);
1.1       deraadt   227:        commands();
1.15      millert   228:        (void)ignoresig(SIGHUP, NULL, NULL);
                    229:        (void)ignoresig(SIGINT, NULL, NULL);
                    230:        (void)ignoresig(SIGQUIT, NULL, NULL);
1.1       deraadt   231:        quit();
                    232:        exit(0);
                    233: }
                    234:
                    235: /*
                    236:  * Compute what the screen size for printing headers should be.
                    237:  * We use the following algorithm for the height:
                    238:  *     If baud rate < 1200, use  9
                    239:  *     If baud rate = 1200, use 14
                    240:  *     If baud rate > 1200, use 24 or ws_row
                    241:  * Width is either 80 or ws_col;
                    242:  */
                    243: void
1.16      millert   244: setscreensize(void)
1.1       deraadt   245: {
                    246:        struct termios tbuf;
                    247:        struct winsize ws;
                    248:        speed_t ospeed;
                    249:
                    250:        if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
                    251:                ws.ws_col = ws.ws_row = 0;
                    252:        if (tcgetattr(1, &tbuf) < 0)
                    253:                ospeed = 9600;
                    254:        else
                    255:                ospeed = cfgetospeed(&tbuf);
1.8       millert   256:        if (ospeed < B1200)
1.1       deraadt   257:                screenheight = 9;
1.8       millert   258:        else if (ospeed == B1200)
1.1       deraadt   259:                screenheight = 14;
                    260:        else if (ws.ws_row != 0)
                    261:                screenheight = ws.ws_row;
                    262:        else
                    263:                screenheight = 24;
                    264:        if ((realscreenheight = ws.ws_row) == 0)
                    265:                realscreenheight = 24;
                    266:        if ((screenwidth = ws.ws_col) == 0)
                    267:                screenwidth = 80;
1.15      millert   268: }
                    269:
                    270: __dead void
1.16      millert   271: usage(void)
1.15      millert   272: {
                    273:
1.22      jmc       274:        fprintf(stderr, "usage: %s [-dEIinv] [-b list] [-c list] "
1.20      sobrado   275:            "[-s subject] to-addr ...\n", __progname);
1.22      jmc       276:        fprintf(stderr, "       %s [-dEIiNnv] -f [file]\n", __progname);
                    277:        fprintf(stderr, "       %s [-dEIiNnv] [-u user]\n", __progname);
1.15      millert   278:        exit(1);
1.1       deraadt   279: }