Home | History | Annotate | Line # | Download | only in aculib
      1  1.13  dholland /*	$NetBSD: v831.c,v 1.13 2016/06/30 05:56:46 dholland Exp $	*/
      2   1.3       jtc 
      3   1.1       cgd /*
      4   1.3       jtc  * Copyright (c) 1983, 1993
      5   1.3       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.9       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.6     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.3       jtc #if 0
     35   1.3       jtc static char sccsid[] = "@(#)v831.c	8.1 (Berkeley) 6/6/93";
     36   1.3       jtc #endif
     37  1.13  dholland __RCSID("$NetBSD: v831.c,v 1.13 2016/06/30 05:56:46 dholland Exp $");
     38   1.1       cgd #endif /* not lint */
     39   1.1       cgd 
     40   1.1       cgd /*
     41   1.1       cgd  * Routines for dialing up on Vadic 831
     42   1.1       cgd  */
     43   1.1       cgd #include "tip.h"
     44   1.1       cgd 
     45   1.1       cgd static jmp_buf jmpbuf;
     46   1.1       cgd static int child = -1;
     47   1.1       cgd 
     48  1.11     perry static	void	alarmtr(int);
     49  1.11     perry static	int	dialit(char *, char *);
     50  1.11     perry static	char   *sanitize(char *);
     51   1.6     lukem 
     52   1.6     lukem int
     53  1.11     perry v831_dialer(char *num, char *acu)
     54   1.1       cgd {
     55  1.10  christos         int status, mypid;
     56   1.6     lukem         int timelim;
     57   1.1       cgd 
     58   1.1       cgd         if (boolean(value(VERBOSE)))
     59  1.12  christos                 (void)printf("\nstarting call...");
     60   1.1       cgd #ifdef DEBUG
     61  1.12  christos         (void)printf("(acu=%s)\n", acu);
     62   1.1       cgd #endif
     63   1.1       cgd         if ((AC = open(acu, O_RDWR)) < 0) {
     64   1.1       cgd                 if (errno == EBUSY)
     65  1.12  christos                         (void)printf("line busy...");
     66   1.1       cgd                 else
     67  1.12  christos                         (void)printf("acu open error...");
     68   1.1       cgd                 return (0);
     69   1.1       cgd         }
     70   1.1       cgd         if (setjmp(jmpbuf)) {
     71  1.12  christos 		(void)kill(child, SIGKILL);
     72  1.12  christos 		(void)close(AC);
     73   1.1       cgd                 return (0);
     74   1.1       cgd         }
     75  1.12  christos         (void)signal(SIGALRM, alarmtr);
     76   1.1       cgd         timelim = 5 * strlen(num);
     77  1.12  christos         (void)alarm((unsigned)(timelim < 30 ? 30 : timelim));
     78   1.1       cgd         if ((child = fork()) == 0) {
     79   1.1       cgd                 /*
     80   1.1       cgd                  * ignore this stuff for aborts
     81   1.1       cgd                  */
     82  1.12  christos                 (void)signal(SIGALRM, SIG_IGN);
     83  1.12  christos 		(void)signal(SIGINT, SIG_IGN);
     84  1.12  christos                 (void)signal(SIGQUIT, SIG_IGN);
     85  1.12  christos                 (void)sleep(2);
     86   1.1       cgd                 exit(dialit(num, acu) != 'A');
     87   1.1       cgd         }
     88   1.1       cgd         /*
     89   1.1       cgd          * open line - will return on carrier
     90   1.1       cgd          */
     91   1.1       cgd         if ((FD = open(DV, O_RDWR)) < 0) {
     92   1.1       cgd #ifdef DEBUG
     93  1.12  christos                 (void)printf("(after open, errno=%d)\n", errno);
     94   1.1       cgd #endif
     95   1.1       cgd                 if (errno == EIO)
     96  1.12  christos                         (void)printf("lost carrier...");
     97   1.1       cgd                 else
     98  1.12  christos                         (void)printf("dialup line open failed...");
     99  1.12  christos                 (void)alarm(0);
    100  1.12  christos                 (void)kill(child, SIGKILL);
    101  1.12  christos                 (void)close(AC);
    102   1.1       cgd                 return (0);
    103   1.1       cgd         }
    104  1.12  christos         (void)alarm(0);
    105  1.12  christos         (void)signal(SIGALRM, SIG_DFL);
    106  1.10  christos         while ((mypid = wait(&status)) != child && mypid != -1)
    107   1.1       cgd                 ;
    108   1.1       cgd         if (status) {
    109  1.12  christos                 (void)close(AC);
    110   1.1       cgd                 return (0);
    111   1.1       cgd         }
    112   1.1       cgd         return (1);
    113   1.1       cgd }
    114   1.1       cgd 
    115   1.1       cgd static void
    116  1.12  christos /*ARGSUSED*/
    117  1.12  christos alarmtr(int dummy __unused)
    118   1.1       cgd {
    119   1.7       mrg 
    120  1.12  christos         (void)alarm(0);
    121   1.1       cgd         longjmp(jmpbuf, 1);
    122   1.1       cgd }
    123   1.1       cgd 
    124   1.1       cgd /*
    125   1.1       cgd  * Insurance, for some reason we don't seem to be
    126   1.1       cgd  *  hanging up...
    127   1.1       cgd  */
    128   1.6     lukem void
    129  1.11     perry v831_disconnect(void)
    130   1.1       cgd {
    131   1.4        pk 	struct termios	cntrl;
    132   1.1       cgd 
    133  1.12  christos         (void)sleep(2);
    134   1.1       cgd #ifdef DEBUG
    135  1.12  christos         (void)printf("[disconnect: FD=%d]\n", FD);
    136   1.1       cgd #endif
    137   1.1       cgd         if (FD > 0) {
    138  1.12  christos                 (void)ioctl(FD, TIOCCDTR, 0);
    139  1.12  christos 		(void)tcgetattr(FD, &cntrl);
    140  1.12  christos 		(void)cfsetospeed(&cntrl, 0);
    141  1.12  christos 		(void)cfsetispeed(&cntrl, 0);
    142  1.12  christos 		(void)tcsetattr(FD, TCSAFLUSH, &cntrl);
    143  1.12  christos                 (void)ioctl(FD, TIOCNXCL, NULL);
    144   1.1       cgd         }
    145  1.12  christos         (void)close(FD);
    146   1.1       cgd }
    147   1.1       cgd 
    148   1.6     lukem void
    149  1.11     perry v831_abort(void)
    150   1.1       cgd {
    151   1.1       cgd 
    152   1.1       cgd #ifdef DEBUG
    153  1.12  christos         (void)printf("[abort: AC=%d]\n", AC);
    154   1.1       cgd #endif
    155  1.12  christos         (void)sleep(2);
    156   1.1       cgd         if (child > 0)
    157  1.12  christos                 (void)kill(child, SIGKILL);
    158   1.1       cgd         if (AC > 0)
    159  1.12  christos                 (void)ioctl(FD, TIOCNXCL, NULL);
    160  1.12  christos                 (void)close(AC);
    161   1.1       cgd         if (FD > 0)
    162  1.12  christos                 (void)ioctl(FD, TIOCCDTR, 0);
    163  1.12  christos         (void)close(FD);
    164   1.1       cgd }
    165   1.1       cgd 
    166   1.1       cgd /*
    167   1.1       cgd  * Sigh, this probably must be changed at each site.
    168   1.1       cgd  */
    169   1.1       cgd struct vaconfig {
    170  1.10  christos 	const char	*vc_name;
    171   1.1       cgd 	char	vc_rack;
    172   1.1       cgd 	char	vc_modem;
    173   1.1       cgd } vaconfig[] = {
    174   1.1       cgd 	{ "/dev/cua0",'4','0' },
    175   1.1       cgd 	{ "/dev/cua1",'4','1' },
    176  1.12  christos 	{ 0, '\0', '\0' }
    177   1.1       cgd };
    178   1.1       cgd 
    179  1.12  christos #define pc(x)	(void)(c = x, write(AC,&c,1))
    180   1.1       cgd #define ABORT	01
    181   1.1       cgd #define SI	017
    182   1.1       cgd #define STX	02
    183   1.1       cgd #define ETX	03
    184   1.1       cgd 
    185   1.1       cgd static int
    186  1.11     perry dialit(char *phonenum, char *acu)
    187   1.1       cgd {
    188   1.6     lukem         struct vaconfig *vp;
    189   1.4        pk 	struct termios cntrl;
    190   1.1       cgd         char c;
    191   1.6     lukem         int i;
    192   1.1       cgd 
    193   1.1       cgd         phonenum = sanitize(phonenum);
    194   1.1       cgd #ifdef DEBUG
    195  1.12  christos         (void)printf("(dial phonenum=%s)\n", phonenum);
    196   1.1       cgd #endif
    197   1.1       cgd         if (*phonenum == '<' && phonenum[1] == 0)
    198   1.1       cgd                 return ('Z');
    199   1.1       cgd 	for (vp = vaconfig; vp->vc_name; vp++)
    200   1.1       cgd 		if (strcmp(vp->vc_name, acu) == 0)
    201   1.1       cgd 			break;
    202   1.1       cgd 	if (vp->vc_name == 0) {
    203  1.12  christos 		(void)printf("Unable to locate dialer (%s)\n", acu);
    204   1.1       cgd 		return ('K');
    205   1.1       cgd 	}
    206  1.12  christos 	(void)tcgetattr(AC, &cntrl);
    207  1.12  christos 	(void)cfsetospeed(&cntrl, B2400);
    208  1.12  christos 	(void)cfsetispeed(&cntrl, B2400);
    209   1.4        pk 	cntrl.c_cflag |= PARODD | PARENB;
    210   1.4        pk 	cntrl.c_lflag &= ~(ISIG | ICANON);
    211  1.12  christos 	(void)tcsetattr(AC, TCSANOW, &cntrl);
    212  1.12  christos 	(void)tcflush(AC, TCIOFLUSH);
    213   1.1       cgd         pc(STX);
    214   1.1       cgd 	pc(vp->vc_rack);
    215   1.1       cgd 	pc(vp->vc_modem);
    216   1.1       cgd 	while (*phonenum && *phonenum != '<')
    217   1.1       cgd 		pc(*phonenum++);
    218   1.1       cgd         pc(SI);
    219   1.1       cgd 	pc(ETX);
    220  1.12  christos         (void)sleep(1);
    221   1.1       cgd         i = read(AC, &c, 1);
    222   1.1       cgd #ifdef DEBUG
    223  1.12  christos         (void)printf("read %d chars, char=%c, errno %d\n", i, c, errno);
    224   1.1       cgd #endif
    225   1.1       cgd         if (i != 1)
    226   1.1       cgd 		c = 'M';
    227   1.1       cgd         if (c == 'B' || c == 'G') {
    228   1.1       cgd                 char cc, oc = c;
    229   1.1       cgd 
    230   1.1       cgd                 pc(ABORT);
    231  1.12  christos                 (void)read(AC, &cc, 1);
    232   1.1       cgd #ifdef DEBUG
    233  1.12  christos                 (void)printf("abort response=%c\n", cc);
    234   1.1       cgd #endif
    235   1.1       cgd                 c = oc;
    236   1.1       cgd                 v831_disconnect();
    237   1.1       cgd         }
    238  1.12  christos         (void)close(AC);
    239   1.1       cgd #ifdef DEBUG
    240  1.12  christos         (void)printf("dialit: returns %c\n", c);
    241   1.1       cgd #endif
    242   1.1       cgd         return (c);
    243   1.1       cgd }
    244   1.1       cgd 
    245   1.1       cgd static char *
    246  1.11     perry sanitize(char *s)
    247   1.1       cgd {
    248   1.1       cgd         static char buf[128];
    249   1.6     lukem         char *cp;
    250   1.1       cgd 
    251   1.7       mrg         for (cp = buf; *s && buf + sizeof buf - cp > 1; s++) {
    252  1.13  dholland 		if (!isdigit((unsigned char)*s) && *s != '<' && *s != '_')
    253   1.1       cgd 			continue;
    254   1.1       cgd 		if (*s == '_')
    255   1.1       cgd 			*s = '=';
    256   1.1       cgd 		*cp++ = *s;
    257   1.1       cgd 	}
    258   1.1       cgd         *cp++ = 0;
    259   1.1       cgd         return (buf);
    260   1.1       cgd }
    261