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

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