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

Annotation of src/usr.bin/tip/acu.c, Revision 1.8

1.8     ! hugh        1: /*     $OpenBSD: acu.c,v 1.7 2001/10/24 18:38:58 millert Exp $ */
1.3       millert     2: /*     $NetBSD: acu.c,v 1.4 1996/12/29 10:34:03 cgd 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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)acu.c      8.1 (Berkeley) 6/6/93";
                     40: #endif
1.8     ! hugh       41: static const char rcsid[] = "$OpenBSD: acu.c,v 1.7 2001/10/24 18:38:58 millert Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #include "tip.h"
                     45:
                     46: static acu_t *acu = NOACU;
                     47: static int conflag;
                     48: static void acuabort();
                     49: static acu_t *acutype();
                     50: static jmp_buf jmpbuf;
                     51: /*
                     52:  * Establish connection for tip
                     53:  *
                     54:  * If DU is true, we should dial an ACU whose type is AT.
                     55:  * The phone numbers are in PN, and the call unit is in CU.
                     56:  *
                     57:  * If the PN is an '@', then we consult the PHONES file for
                     58:  *   the phone numbers.  This file is /etc/phones, unless overriden
                     59:  *   by an exported shell variable.
                     60:  *
                     61:  * The data base files must be in the format:
                     62:  *     host-name[ \t]*phone-number
                     63:  *   with the possibility of multiple phone numbers
                     64:  *   for a single host acting as a rotary (in the order
                     65:  *   found in the file).
                     66:  */
                     67: char *
                     68: connect()
                     69: {
1.7       millert    70:        char *cp = PN;
1.1       deraadt    71:        char *phnum, string[256];
                     72:        FILE *fd;
1.4       deraadt    73:        volatile int tried = 0;
1.1       deraadt    74:
                     75:        if (!DU) {              /* regular connect message */
                     76:                if (CM != NOSTR)
1.5       deraadt    77:                        parwrite(FD, CM, size(CM));
1.1       deraadt    78:                logent(value(HOST), "", DV, "call completed");
                     79:                return (NOSTR);
                     80:        }
                     81:        /*
                     82:         * @ =>'s use data base in PHONES environment variable
                     83:         *        otherwise, use /etc/phones
                     84:         */
                     85:        signal(SIGINT, acuabort);
                     86:        signal(SIGQUIT, acuabort);
                     87:        if (setjmp(jmpbuf)) {
                     88:                signal(SIGINT, SIG_IGN);
                     89:                signal(SIGQUIT, SIG_IGN);
                     90:                printf("\ncall aborted\n");
                     91:                logent(value(HOST), "", "", "call aborted");
                     92:                if (acu != NOACU) {
1.3       millert    93:                        setboolean(value(VERBOSE), FALSE);
1.1       deraadt    94:                        if (conflag)
                     95:                                disconnect(NOSTR);
                     96:                        else
                     97:                                (*acu->acu_abort)();
                     98:                }
                     99:                return ("interrupt");
                    100:        }
                    101:        if ((acu = acutype(AT)) == NOACU)
                    102:                return ("unknown ACU type");
                    103:        if (*cp != '@') {
                    104:                while (*cp) {
1.6       krw       105:                        phnum = cp;
                    106:                        cp = strpbrk(cp, ",");
                    107:                        if (*cp != '\0')
1.1       deraadt   108:                                *cp++ = '\0';
1.6       krw       109:
                    110:                        if (strlen(phnum) == 0)
                    111:                                continue;
                    112:
                    113:                        conflag = (*acu->acu_dialer)(phnum, CU);
                    114:                        if (conflag)
                    115:                                break;
                    116:
                    117:                        logent(value(HOST), phnum, acu->acu_name, "call failed");
1.1       deraadt   118:                        tried++;
                    119:                }
                    120:        } else {
                    121:                if ((fd = fopen(PH, "r")) == NOFILE) {
                    122:                        printf("%s: ", PH);
                    123:                        return ("can't open phone number file");
                    124:                }
                    125:                while (fgets(string, sizeof(string), fd) != NOSTR) {
1.6       krw       126:                        cp = &string[strcspn(string, " \t\n")];
                    127:                        if (*cp != '\0')
                    128:                                *cp++ = '\0';
                    129:
                    130:                        if (strcmp(string, value(HOST)) != 0)
1.1       deraadt   131:                                continue;
1.6       krw       132:
                    133:                        cp += strspn(cp, " \t\n");
                    134:                        phnum = cp;
                    135:                        *(cp + strcspn(cp, ",\n")) = '\0';
                    136:
                    137:                        if (strlen(phnum) == 0)
                    138:                                continue;
                    139:
                    140:                        conflag = (*acu->acu_dialer)(phnum, CU);
                    141:                        if (conflag)
                    142:                                break;
1.1       deraadt   143:
1.6       krw       144:                        logent(value(HOST), phnum, acu->acu_name, "call failed");
1.1       deraadt   145:                        tried++;
                    146:                }
                    147:                fclose(fd);
                    148:        }
1.6       krw       149:        if (conflag) {
                    150:                if (CM != NOSTR)
                    151:                        parwrite(FD, CM, size(CM));
                    152:                logent(value(HOST), phnum, acu->acu_name, "call completed");
                    153:                return (NOSTR);
                    154:        } else if (!tried) {
1.1       deraadt   155:                logent(value(HOST), "", acu->acu_name, "missing phone number");
1.6       krw       156:                return ("missing phone number");
                    157:        } else {
1.1       deraadt   158:                (*acu->acu_abort)();
1.6       krw       159:                return ("call failed");
                    160:        }
1.1       deraadt   161: }
                    162:
1.4       deraadt   163: void
1.1       deraadt   164: disconnect(reason)
                    165:        char *reason;
                    166: {
                    167:        if (!conflag) {
                    168:                logent(value(HOST), "", DV, "call terminated");
                    169:                return;
                    170:        }
                    171:        if (reason == NOSTR) {
                    172:                logent(value(HOST), "", acu->acu_name, "call terminated");
                    173:                if (boolean(value(VERBOSE)))
                    174:                        printf("\r\ndisconnecting...");
                    175:        } else
                    176:                logent(value(HOST), "", acu->acu_name, reason);
                    177:        (*acu->acu_disconnect)();
                    178: }
                    179:
                    180: static void
                    181: acuabort(s)
                    182: {
                    183:        signal(s, SIG_IGN);
                    184:        longjmp(jmpbuf, 1);
                    185: }
                    186:
                    187: static acu_t *
                    188: acutype(s)
1.7       millert   189:        char *s;
1.1       deraadt   190: {
1.7       millert   191:        acu_t *p;
1.1       deraadt   192:        extern acu_t acutable[];
                    193:
                    194:        for (p = acutable; p->acu_name != '\0'; p++)
                    195:                if (!strcmp(s, p->acu_name))
                    196:                        return (p);
                    197:        return (NOACU);
                    198: }