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

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