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

Annotation of src/usr.bin/tip/cu.c, Revision 1.37

1.37    ! nicm        1: /*     $OpenBSD: cu.c,v 1.36 2010/07/02 07:40:03 nicm Exp $    */
1.3       millert     2: /*     $NetBSD: cu.c,v 1.5 1997/02/11 09:24:05 mrg Exp $       */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1983, 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.14      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:
1.21      ray        33: #include <err.h>
                     34: #include <paths.h>
1.32      chl        35: #include <util.h>
1.21      ray        36:
1.1       deraadt    37: #include "tip.h"
                     38:
1.18      moritz     39: static void    cuusage(void);
1.1       deraadt    40:
                     41: /*
                     42:  * Botch the interface to look like cu's
                     43:  */
1.4       deraadt    44: void
1.17      deraadt    45: cumain(int argc, char *argv[])
1.1       deraadt    46: {
1.29      nicm       47:        int ch, i, parity, baudrate;
1.20      millert    48:        const char *errstr;
1.1       deraadt    49:        static char sbuf[12];
1.33      nicm       50:        char *device;
1.1       deraadt    51:
1.6       millert    52:        if (argc < 2)
                     53:                cuusage();
1.33      nicm       54:        vsetnum(BAUDRATE, DEFBR);
1.11      miod       55:        parity = 0;     /* none */
1.20      millert    56:
                     57:        /*
                     58:         * Convert obsolecent -### speed to modern -s### syntax which
                     59:         * getopt() can handle.
                     60:         */
                     61:        for (i = 1; i < argc; i++) {
                     62:                if (argv[i][0] == '-') {
                     63:                        switch (argv[i][1]) {
                     64:                        case '0': case '1': case '2': case '3': case '4':
                     65:                        case '5': case '6': case '7': case '8': case '9':
                     66:                                ch = snprintf(sbuf, sizeof(sbuf), "-s%s",
                     67:                                    &argv[i][1]);
                     68:                                if (ch <= 0 || ch >= sizeof(sbuf)) {
                     69:                                        errx(3, "invalid speed: %s",
                     70:                                            &argv[i][1]);
                     71:                                }
                     72:                                argv[i] = sbuf;
                     73:                                break;
                     74:                        case '-':
                     75:                                /* if we get "--" stop processing args */
                     76:                                if (argv[i][2] == '\0')
                     77:                                        goto getopt;
                     78:                                break;
                     79:                        }
                     80:                }
                     81:        }
                     82:
                     83: getopt:
1.26      nicm       84:        while ((ch = getopt(argc, argv, "l:s:htoe")) != -1) {
1.17      deraadt    85:                switch (ch) {
1.6       millert    86:                case 'l':
1.33      nicm       87:                        if (vgetstr(DEVICE) != NULL) {
1.8       deraadt    88:                                fprintf(stderr,
1.23      martynas   89:                                    "%s: cannot specify multiple -l options\n",
1.8       deraadt    90:                                    __progname);
                     91:                                exit(3);
                     92:                        }
                     93:                        if (strchr(optarg, '/'))
1.33      nicm       94:                                vsetstr(DEVICE, optarg);
1.31      nicm       95:                        else {
1.33      nicm       96:                                if (asprintf(&device,
1.31      nicm       97:                                    "%s%s", _PATH_DEV, optarg) == -1)
1.21      ray        98:                                        err(3, "asprintf");
1.33      nicm       99:                                vsetstr(DEVICE, device);
1.31      nicm      100:                        }
1.1       deraadt   101:                        break;
                    102:                case 's':
1.29      nicm      103:                        baudrate = (int)strtonum(optarg, 0, INT_MAX, &errstr);
1.20      millert   104:                        if (errstr)
                    105:                                errx(3, "speed is %s: %s", errstr, optarg);
1.33      nicm      106:                        vsetnum(BAUDRATE, baudrate);
1.6       millert   107:                        break;
                    108:                case 'h':
1.33      nicm      109:                        vsetnum(LECHO, 1);
                    110:                        vsetnum(HALFDUPLEX, 1);
1.6       millert   111:                        break;
                    112:                case 't':
1.27      nicm      113:                        /* Was for a hardwired dial-up connection. */
1.1       deraadt   114:                        break;
1.6       millert   115:                case 'o':
1.11      miod      116:                        if (parity != 0)
                    117:                                parity = 0;     /* -e -o */
                    118:                        else
                    119:                                parity = 1;     /* odd */
1.6       millert   120:                        break;
                    121:                case 'e':
1.11      miod      122:                        if (parity != 0)
                    123:                                parity = 0;     /* -o -e */
                    124:                        else
                    125:                                parity = -1;    /* even */
1.1       deraadt   126:                        break;
                    127:                default:
1.10      pvalchev  128:                        cuusage();
1.1       deraadt   129:                        break;
                    130:                }
                    131:        }
1.6       millert   132:        argc -= optind;
                    133:        argv += optind;
                    134:
                    135:        switch (argc) {
                    136:        case 1:
1.30      nicm      137:                /* Was phone number but now ignored. */
1.6       millert   138:        case 0:
                    139:                break;
                    140:        default:
                    141:                cuusage();
                    142:                break;
                    143:        }
                    144:
1.1       deraadt   145:        signal(SIGINT, cleanup);
                    146:        signal(SIGQUIT, cleanup);
                    147:        signal(SIGHUP, cleanup);
                    148:        signal(SIGTERM, cleanup);
1.16      otto      149:        signal(SIGCHLD, SIG_DFL);
1.36      nicm      150:
1.1       deraadt   151:        /*
                    152:         * The "cu" host name is used to define the
                    153:         * attributes of the generic dialer.
                    154:         */
1.36      nicm      155:        snprintf(sbuf, sizeof(sbuf), "cu%d", vgetnum(BAUDRATE));
1.35      nicm      156:        FD = hunt(sbuf);
1.1       deraadt   157:        setbuf(stdout, NULL);
1.36      nicm      158:
1.1       deraadt   159:        loginit();
1.36      nicm      160:
1.11      miod      161:        switch (parity) {
                    162:        case -1:
                    163:                setparity("even");
                    164:                break;
                    165:        case 1:
                    166:                setparity("odd");
                    167:                break;
                    168:        default:
                    169:                setparity("none");
                    170:                break;
                    171:        }
1.33      nicm      172:        vsetnum(VERBOSE, 0);
                    173:        if (ttysetup(vgetnum(BAUDRATE))) {
1.34      nicm      174:                fprintf(stderr, "%s: unsupported speed %d\n",
1.33      nicm      175:                    __progname, vgetnum(BAUDRATE));
1.13      millert   176:                (void)uu_unlock(uucplock);
                    177:                exit(3);
                    178:        }
1.26      nicm      179:        con();
1.6       millert   180: }
                    181:
1.18      moritz    182: static void
1.17      deraadt   183: cuusage(void)
1.6       millert   184: {
1.30      nicm      185:        fprintf(stderr, "usage: cu [-eho] [-l line] [-s speed | -speed]\n");
1.6       millert   186:        exit(8);
1.1       deraadt   187: }