/* * findtty: find a tty we can call a system with. */ #include #include #include "cico.h" #include "ttyio.h" char callee[16]; /* system to call */ int calltype; /* DIR or ACU call? */ long callspeed; /* what speed to call him at */ char callphone[40]; /* phone # to call */ char callseq[200]; /* msg-expect to use after conn */ char callwhen[20]; /* 'any' | 'none' | MoTuWeThFrSaSu... */ char tty[16]; /* terminal we're talking at */ char ttyprefix[40]; /* dial prefix */ char ttysuffix[40]; /* dial suffix */ char ttyinit[80]; /* init string */ long ttyispeed; /* init speed */ long ttycspeed; /* speed it's at */ int ttyflow; /* flow control we use on the tty */ char ttybusy[200]; /* codes returned by modem if it's busy */ char ttyfail[200]; /* codes returned by the modem if the line */ /* is bad or the number is bad */ extern int yes(), carrier(); /* carrier-detect functions (ick) */ int (*cdf)() = carrier; /* our appropriate carrier-detect function */ #include "cico.h" char systyp[20]; /* * speedok() is a local procedure for speedmatch */ static speedok(p, speed) register char *p; long speed; { register char *dash; if (dash=strchr(p,'-')) { *dash++ = 0; if (speed >= atol(p) && speed <= atol(dash)) { ttycspeed = speed; return 1; } } else if (speed == atol(p)) { ttycspeed = speed; return 1; } return 0; } /* * speedmatch() checks a tty speed against a list of valid speeds */ static speedmatch(speed, validstring) long speed; char *validstring; { register char *p, *q; for (p = validstring; q=strchr(p, ','); p=q) { *q++ = 0; if (speedok(p, speed)) return 1; } return speedok(p, speed); } /* speedmatch */ /* * parsestr() parse a string for findtty */ static parsestr(dst, src) register char *dst, *src; { register tmp; while (*src) if (*src == '\\' && src[1] != 0) { switch(*++src) { case 'r': *dst++ = '\r'; break; case 'n': *dst++ = '\n'; break; case 'f': *dst++ = '\f'; break; case 'b': *dst++ = '\b'; break; case 'P': if (isdigit(src[1])) { /* pause for a while... */ *dst++ = ('P'|0200); for (tmp=0; isdigit(*++src); ) tmp = (tmp*10) + (*src - '0'); if (tmp) *dst++ = tmp; else --dst; if (*src == '*') ++src; continue; } default: *dst++ = *src; break; } ++src; } else *dst++ = *src++; *dst = 0; } /* parsestr */ /* * oksys() checks if a system name is in the SYSTEMS file, then fills in * all the appropriate global variables so we can call the system. */ oksys(sys) char *sys; { int cnt; /* to check argcount on acu systems */ char *line; /* ... returned from findsys() with important bits */ extern char *findsys(); if (line=findsys(sys)) { cnt = sscanf(line, "%s %s %s %ld %s %[^\n]", callee, callwhen, systyp, &callspeed, callphone, callseq); if (stricmp(systyp, "acu") == 0) calltype = ACU; else if (stricmp(systyp, "dir") == 0) calltype = DIR; else calltype = DIALER; if (calltype != ACU || cnt == 6) return 1; } return 0; } /* oksys */ /* * yes() simply returns 1- used for 'carrier detect' on direct lines */ yes() { return 1; } /* * findtty() finds a device that we can use to call the system in question. * when findtty is called, we've already called oksys() or have filled in * the appropriate fields by hand. If findtty() finds a tty that we can * use, it fills in the tty[] string with the tty name and returns 1, * otherwise it returns 0. */ findtty() { FILE *cfg; char arg[300]; int cnt; char teltyp[9]; char telspeed[40]; char telopt[200]; register char *p; register char *q; char *busyp, *failp; if (calltype != DIALER) { /* don't look for dialers yet.. */ cdf = (calltype == DIR) ? yes : carrier; if (cfg=fopen(makepath(arg, NETDIR, DEVICES), "r")) { while (rdline(cfg, arg, sizeof arg)) { cnt = sscanf(arg, "%s %s %s %[^\n]", tty, teltyp, telspeed, telopt); if (cnt < 2) continue; if (stricmp(teltyp, systyp) != 0 || !speedmatch(callspeed, telspeed)) continue; ttyispeed = ttycspeed; ttyflow = TTYF_RTSCTS; if (cnt == 4) busyp = ttybusy; /* set up for building busy */ failp = ttyfail; /* and fail code strings */ for (p=strtok(telopt, ","); p; p=strtok(NULL, ",")) if (q = strchr(p, '=')) { while (isspace(*p)) ++p; *q++ = 0; if (stricmp(p, "initspeed") == 0) ttyispeed = atol(q); else if (stricmp(p, "initstring") == 0) parsestr(ttyinit, q); else if (stricmp(p, "prefix") == 0) parsestr(ttyprefix, q); else if (stricmp(p, "suffix") == 0) parsestr(ttysuffix, q); else if (stricmp(p, "flow") == 0) { if (stricmp(q, "none") == 0) ttyflow = TTYF_NONE; else if (stricmp(q, "rtscts") == 0) ttyflow = TTYF_RTSCTS; } else if (stricmp(p, "busy") == 0) { parsestr(busyp, q); *(busyp = 1+EOS(busyp)) = 0; } else if (stricmp(p, "fail") == 0) { parsestr(failp, q); *(failp = 1+EOS(failp)) = 0; } } if (lock(tty)) { fclose(cfg); return 1; /* configuration is done */ } } } } return 0; } /* findtty */