📄 dial.c
字号:
/* * This file is part of the Minicom Communications Program, * written by Miquel van Smoorenburg 1991/1992/1993. */#include <sys/types.h>#if defined (_POSIX_SOURCE) || defined(_BSD43)# include <unistd.h># include <stdlib.h># ifdef _MINIX# undef NULL# endif#endif#if defined(_BSD43) || defined (_SYSV)# define NOSTREAMS# include <sys/file.h>#endif#include <string.h>#ifdef _MINIX# undef NULL#endif#include <stdio.h>#include <sys/stat.h>#ifdef linux# include <sys/ioctl.h># include <sys/kd.h># include <sys/time.h>#endif#include "keyboard.h"#include "window.h"#include "minicom.h"#include "vt100.h"#include "configsym.h"/* Dialing directory. */struct dialent { char name[32]; char number[16]; char script[16]; char username[32]; char password[32]; char term; char baud[8]; char parity[2]; char dialtype; char localecho; char bits[2]; struct dialent *next;};/* Version info. */#define DIALMAGIC 0x55AAstruct dver { short magic; short version; short size; short res1; short res2; short res3; short res4;};#define dialentno(di, no) ((struct dialent *)((char *)(di) + ((no) * sizeof(struct dialent)))) static struct dialent *dialents;static int nrents = 1;static int newtype;/* Access to ".daildir" denied? */static int dendd = 0;/* * Functions to talk to the modem. */ /* * Send a string to the modem. */void mputs(s)char *s;{ char c; while(*s) { if (*s == '^' && (*(s + 1))) { s++; if (*s == '^') c = *s; else c = (*s) & 31; } else c = *s; if (c == '~') sleep(1); else write(portfd, &c, 1); s++; }} /* * Initialize the modem. */ void modeminit(){ WIN *w; if (P_MINIT[0] == '\0') return; w = tell("Initializing Modem"); m_dtrtoggle(portfd); mputs(P_MINIT); wclose(w, 1);}/* * Reset the modem. */void modemreset(){ WIN *w; if (P_MRESET[0] == '\0') return; w = tell("Resetting Modem"); mputs(P_MRESET); sleep(1); wclose(w, 1);}/* * Hang the line up. */void hangup(){ WIN *w; w = tell("Hanging up"); if (P_MDROPDTR[0] == 'Y') { m_dtrtoggle(portfd); /* Sometimes the minix tty driver does not see * that DCD has dropped. * (This is a kludge!) */ m_setdcd(portfd, 0); } else { mputs(P_MHANGUP); sleep(1); } /* If we don't have DCD support fake DCD dropped */ bogus_dcd = 0; wclose(w, 1);}/* * This seemed to fit best in this file * Send a break signal. */void sendbreak(){ WIN *w; w = tell("Sending BREAK"); wcursor(w, CNONE); m_break(portfd); wclose(w, 1);} WIN *dialwin;int dialtime;#if defined (__linux__) && defined(_SELECT)/* * Play music until key is pressed. */void music(){ int x, i, k; int consolefd = 0; /* If we're in X, we have to explicitly use the console */ if (strncmp(getenv("TERM"), "xterm", 5) == 0 && strcmp(getenv("DISPLAY"), ":0.0") == 0) { consolefd = open("/dev/console", O_WRONLY); if (consolefd < 0) consolefd = 0; } /* Tell keyboard handler what we want. */ keyboard(KSIGIO, 0); /* And loop forever :-) */ for(i = 0; i < 9; i++) { k = 2000 - 200 * (i % 3); (void) ioctl(consolefd, KIOCSOUND, k); /* Check keypress with timeout 160 ms */ x = check_io(-1, 0, 160, NULL, NULL); if (x & 2) break; } (void) ioctl(consolefd, KIOCSOUND, 0); /* Wait for keypress and absorb it */ while((x & 2) == 0) x = check_io(-1, 0, 10000, NULL, NULL); (void) keyboard(KGETKEY, 0);}#endif/* * The dial has failed. Tell user. * Count down until retrytime and return. */static int dialfailed(s, rtime)char *s;int rtime;{ int f, x; wlocate(dialwin, 1, 5); wprintf(dialwin, " No connection: %s.\n", s); if (rtime < 0) { wprintf(dialwin, " Press any key to continue.. "); if (check_io(-1, 0, 10000, NULL, NULL) & 2) (void) keyboard(KGETKEY, 0); return(0); } wprintf(dialwin, " Retry in %2d seconds ", rtime); for(f = rtime - 1; f >= 0; f--) { x = check_io(-1, 0, 1000, NULL, NULL); if (x & 2) { /* Key pressed - absorb it. */ (void) keyboard(KGETKEY, 0); return(-1); } wlocate(dialwin, 14, 6); wprintf(dialwin, "%2d ", f); } sleep(1); /* Allow modem time to hangup if redial time == 0 */ wlocate(dialwin, 1, 5); wprintf(dialwin, " \n"); wprintf(dialwin, " "); return(0);}/* * Dial a number, and display the name. */int dial(name, num, keypress, dialtype)char *name;char *num;int keypress;int dialtype;{ char *s, *t; int f, x, nb, retst = -1; int modidx, retries = 0; int maxretries = 1, rdelay = 45; char *reason = "Max retries"; time_t now, last; char buf[128]; char modbuf[128]; dialwin = wopen(18, 9, 62, 15, BSINGLE, stdattr, MFG, MBG, 0, 0, 1); wtitle(dialwin, TMID, "Autodial"); wcursor(dialwin, CNONE); wputs(dialwin, "\n"); wprintf(dialwin, " Dialing : %s\n", name); wprintf(dialwin, " At : %s\n", num); wredraw(dialwin, 1); /* Tell keyboard routines we need them. */ keyboard(KSIGIO, 0); maxretries = atoi(P_MRETRIES); if (maxretries <= 0) maxretries = 1; rdelay = atoi(P_MRDELAY); if (rdelay < 0) rdelay = 0; /* Main retry loop of dial() */MainLoop: while(++retries <= maxretries) { /* Calculate dial time */ dialtime = atoi(P_MDIALTIME); if (dialtime == 0) dialtime = 45; time(&now); last = now; /* Show used time */ wlocate(dialwin, 0, 3); wprintf(dialwin, " Time : %-3d", dialtime); if (maxretries > 1) wprintf(dialwin, " Attempt #%d", retries); wputs(dialwin, "\n\n\n Press any key to cancel dialing"); /* Start the dial */ m_flush(portfd); switch(dialtype) { case 0: mputs(P_MDIALPRE); mputs(num); mputs(P_MDIALSUF); break; case 1: mputs(P_MDIALPRE2); mputs(num); mputs(P_MDIALSUF2); break; case 2: mputs(P_MDIALPRE3); mputs(num); mputs(P_MDIALSUF3); break; } /* Wait 'till the modem says something */ modbuf[0] = 0; modidx = 0; s = buf; buf[0] = 0; while(dialtime > 0) { if (*s == 0) { x = check_io(portfd, 0, 1000, buf, NULL); s = buf; } if (x & 2) { /* Key was pressed - cancelled. */ mputs(P_MDIALCAN); (void) keyboard(KGETKEY, 0); (void) keyboard(KSTOP, 0); wclose(dialwin, 1); return(retst); } if (x & 1) { /* Data available from the modem. Put in buffer. */ if (*s == '\r' || *s == '\n') { /* We look for [\r\n]STRING[\r\n] */ modbuf[modidx] = 0; modidx = 0; } else if (modidx < 127) { /* Normal character. Add. */ modbuf[modidx++] = *s; modbuf[modidx] = 0; } /* Skip to next received char */ if (*s) s++; /* Only look when we got a whole line. */ if (modidx == 0 && !strncmp(modbuf, P_MCONNECT, strlen(P_MCONNECT))) { retst = 0; /* Try to do auto-bauding */ if (sscanf(s + strlen(P_MCONNECT), "%d", &nb) == 1) retst = nb; /* Try to figure out if this system supports DCD */ f = m_getdcd(portfd); bogus_dcd = 1; wlocate(dialwin, 1, 6); if (keypress) { wputs(dialwin, "Connected. Press any key to continue");#if defined (__linux__) && defined(_SELECT) music();#else x = check_io(-1, 0, 0, NULL, NULL); if ((x & 2) == 2) (void) keyboard(KGETKEY, 0);#endif } keyboard(KSTOP, 0); wclose(dialwin, 1); dialwin = NIL_WIN; return(retst); } for(f = 0; f < 3; f++) { if (f == 0) t = P_MNOCON1; if (f == 1) t = P_MNOCON2; if (f == 2) t = P_MNOCON3; if (f == 3) t = P_MNOCON4; if ((*t) && (!strncmp(modbuf, t, strlen(t)))) { if (retries < maxretries) { x = dialfailed(t, rdelay); if (x < 0) { keyboard(KSTOP, 0); wclose(dialwin, 1); return(retst); } } if (maxretries == 1) reason = t; goto MainLoop; } } } /* Do timer routines here. */ time(&now); if (last != now) { dialtime -= (now - last); if (dialtime < 0) dialtime = 0; wlocate(dialwin, 11, 3); wprintf(dialwin, "%-3d ", dialtime); if (dialtime <= 0) { reason = "Timeout"; retst = -1; if (retries < maxretries) { x = dialfailed(reason, rdelay); if (x < 0) { keyboard(KSTOP, 0); wclose(dialwin, 1); return(retst); } } } } last = now; } } /* End of main while cq MainLoop */ dialfailed(reason, -1); keyboard(KSTOP, 0); wclose(dialwin, 1); return(retst);}/* * Create an empty entry. */static struct dialent *mkstdent(){ struct dialent *d; d = (struct dialent *)malloc(sizeof (struct dialent)); if (d == (struct dialent *)0) return(d); d->name[0] = 0; d->number[0] = 0; d->script[0] = 0; d->username[0] = 0; d->password[0] = 0; d->term = 1; d->dialtype = 0; d->localecho = 0; strcpy(d->baud, DEF_BAUD); strcpy(d->bits, "8"); strcpy(d->parity, "N"); d->next = (struct dialent *)0; return(d);}/* * Read in the dialing directory from $HOME/.dialdir */int readdialdir(){ long size; FILE *fp; char dfile[256]; static int didread = 0; int f; struct dialent *d, *prev = (struct dialent *)0; struct stat stt; struct dver dial_ver; if (didread) return(0); didread = 1; nrents = 1; /* Construct path */ sprintf(dfile, "%s/.dialdir", homedir); /* Check if it's readable IF it exists */ if (stat(dfile, &stt) >= 0 && access(dfile, R_OK) < 0) { werror("Cannot open ~/.dialdir: permission denied"); dialents = mkstdent(); dendd = 1; return(0); } if ((fp = fopen(dfile, "r")) == (FILE *)NULL) { dialents = mkstdent(); return(0); } /* Get size of the file */ fseek(fp, 0L, SEEK_END); size = ftell(fp); if (size == 0) { dialents = mkstdent(); fclose(fp); return(0); } /* Get version of the dialing directory */ fseek(fp, 0L, SEEK_SET); fread(&dial_ver, sizeof(dial_ver), 1, fp); if (dial_ver.magic != DIALMAGIC) { /* First version without version info. */ dial_ver.version = 0; fseek(fp, 0L, SEEK_SET);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -