⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dial.c

📁 小型通讯程序的C语言源程序的C语言源程序库
💻 C
📖 第 1 页 / 共 2 页
字号:
  } else	size -= sizeof(dial_ver);  if (size % sizeof(struct dialent) != 0) {  	werror("Phonelist garbled");  	fclose(fp);  	unlink(dfile);  	dialents = mkstdent();  	return(-1);  }  /* Read in the dialing entries */  nrents = size / sizeof(struct dialent);  if (nrents == 0) {	dialents = mkstdent();	nrents = 1;	fclose(fp);	return(0);  }  for(f = 1; f <= nrents; f++) {  	if ((d = (struct dialent *)malloc(sizeof (struct dialent))) ==  		(struct dialent *)0) {  			if(f == 1)				dialents = mkstdent();			else				prev->next = (struct dialent *)0;  			werror("Out of memory while reading dialing directory");			fclose(fp);  			return(-1);  	}  	fread((char *)d, sizeof(struct dialent), (size_t)1, fp);  	if (prev != (struct dialent *)0)  		prev->next = d;  	else  		dialents = d;  	prev = d;	/* Adjust for version. */	if (dial_ver.version == 0) {		d->dialtype = 0;		d->localecho = 0;	}  }  d->next = (struct dialent *)0;  fclose(fp);  return(0);}/* * Write the new $HOME/.dialdir */static void writedialdir(){  struct dialent *d;  char dfile[256];  FILE *fp;  int ret;  struct dver dial_ver;  /* Make no sense if access denied */  if (dendd) return;  sprintf(dfile, "%s/.dialdir", homedir);  if ((ret = waccess(dfile)) < 0 || (fp = fopen(dfile, "w")) == (FILE *)0) {  	werror("Can't write to ~/.dialdir");	dendd = 1;  	return;  }  if (ret == A_OK_NOTEXIST) {#ifndef DEBUG  	/* FIXME: should use fchown and fchmod */#ifndef HAS_FCHOWN	chown(dfile, real_uid, real_gid);	chmod(dfile, 0600);#else	fchown(fileno(fp), real_uid, real_gid);	fchmod(fileno(fp), 0600);#endif#endif  }  d = dialents;  /* Set up version info. */  dial_ver.magic = DIALMAGIC;  dial_ver.version = 1;  fwrite(&dial_ver, sizeof(dial_ver), 1, fp);  /* Write dialing directory */  while(d) {  	if (fwrite(d, sizeof(struct dialent), (size_t)1, fp) != 1) {  		werror("Error writing ~/.dialdir!");  		fclose(fp);  		return;  	}  	d = d->next;  }  fclose(fp);}/* * Get entry "no" in list. */static struct dialent *getno(no)int no;{  struct dialent *d;    d = dialents;    if (no >= nrents) return((struct dialent *)NULL);  while(no--) d = d->next;  return(d);}static char *te[] = { "VT100", "MINIX", "ANSI " };/* * Edit an entry. */static void dedit(d)struct dialent *d;{  WIN *w;  int c;    w = wopen(5, 5, 75, 15, BDOUBLE, stdattr, MFG, MBG, 0, 0, 1);  wprintf(w, " A -   Name               : %s\n", d->name);  wprintf(w, " B -   Number             : %s\n", d->number);  wprintf(w, " C -   Dial string #      : %d\n", d->dialtype + 1);  wprintf(w, " D -   Local echo         : %s\n", yesno(d->localecho));  wprintf(w, " E -   Script             : %s\n", d->script);  wprintf(w, " F -   Username           : %s\n", d->username);  wprintf(w, " G -   Password           : %s\n", d->password);  wprintf(w, " H -   Terminal Emulation : %s\n", te[d->term - 1]);  wprintf(w, " I -   Line Settings      : %s %s%s1",  	d->baud, d->bits, d->parity);  wlocate(w, 4, 11);  wputs(w, "Change which setting? ");  wredraw(w, 1);  while(1) {      wlocate(w, 26, 11);      c = getch();      if (c >= 'a') c -= 32;      switch(c) {        case '\033':        case '\r':  	case '\n':  		wclose(w, 1);  		return;  	case 'A':  		wlocate(w, 28, 0);  		(void) wgets(w, d->name, 31, 32);  		break;  	case 'B':  		wlocate(w, 28, 1);  		(void) wgets(w, d->number, 15, 16);  		break;	case 'C':		d->dialtype = (d->dialtype + 1) % 3;		wlocate(w, 28, 2);		wprintf(w, "%d", d->dialtype + 1);		wflush();		break;	case 'D':		d->localecho = !d->localecho;		wlocate(w, 28, 3);		wprintf(w, "%s", yesno(d->localecho));		wflush();		break;  	case 'E':  		wlocate(w, 28, 4);  		(void) wgets(w, d->script, 15, 16);	  		break;  	case 'F':  		wlocate(w, 28, 5);  		(void) wgets(w, d->username, 31, 32);  		break;  	case 'G':  		wlocate(w, 28, 6);  		(void) wgets(w, d->password, 31, 32);  		break;	  	case 'H':  		d->term = (d->term % 3) + 1;  		wlocate(w, 28, 7);  		wputs(w, te[d->term - 1]);	  		break;  	case 'I':  		get_bbp(d->baud, d->bits, d->parity);  		wlocate(w, 28, 8);  		wprintf(w, "%s %s%s1  ", d->baud, d->bits, d->parity);  		break;  	default:  		break;      }  }}static WIN *dsub;static char *what =  "  Dial    Add     Edit   Remove ";static int dprev;/* * Highlight a choice in the horizontal menu. */static void dhili(k)int k;{  if (k == dprev) return;  if (dprev >= 0) {  	wlocate(dsub, 22 + 8*dprev, 0);	if (!useattr) {		wputs(dsub, " ");	} else {  		wsetattr(dsub, A_REVERSE | stdattr);  		wprintf(dsub, "%8.8s", what + 8*dprev);	}  }  dprev = k;  wlocate(dsub, 22 + 8*k, 0);  if (!useattr) {	wputs(dsub, ">");  } else {	wsetattr(dsub, stdattr);	wprintf(dsub, "%8.8s", what + 8*k);  }}  static char *fmt = "\r %2d  %-16.16s %-16.16s%5s %s%s1    %-6.6s %-15.15s\n";/* * Print the dialing directory. Only draw from "cur" to bottom. */static void prdir(dialw, top, cur)WIN *dialw;int top, cur;{  int f, start;  struct dialent *d;  start = cur - top;  dirflush = 0;  wlocate(dialw, 0, start + 1);  for(f = start; f < 16; f++) {  	d = getno(f + top);  	if (d == (struct dialent *)0) break;  	wprintf(dialw, fmt, f+1+top, d->name, d->number,  		d->baud, d->bits, d->parity, te[d->term - 1], d->script);  }  dirflush = 1;  wflush();}/* Little menu. */static char *d_yesno[] = { "   Yes  ", "   No   ", CNULL };/* * Ask for a number and if <enter> is given go to the * dialing directory */void dialdir(){  WIN *w;  struct dialent *d, *d1, *d2;  static int cur = 0;  static int ocur = 0;  int subm = 0;  int quit = 0;  static int top = 0;  int c, nb;  char buf[32];  int pgud = 0;  int first = 1;  dprev = -1;  w = wopen(0, LINES - 1, COLS - 1, LINES - 1, BNONE, A_REVERSE, WHITE, BLACK, 1, 0, 1);  wputs(w, " Number to dial: (enter for dialing directory) ");  buf[0] = 0;  if (wgets(w, buf, 19, 32) < 0) {  	wclose(w, 1);  	return;  }  wclose(w, 1);  if (buf[0] != 0) {  	(void) dial("Manually entered number", buf, 1, 0);  	return;  }    /* Allright, draw the dialing directory! */    dirflush = 0;  dsub = wopen(2, 22, 77, 22, BNONE, A_REVERSE | stdattr, MFG, MBG, 0, 0, 1);  w = wopen(3, 2, 76, 19, BSINGLE, stdattr, MFG, MBG, 0, 0, 1);  wcursor(w, CNONE);  wtitle(w, TMID, "Dialing Directory");  wputs(w, "     Name               Number       Line Format Terminal Script\n");  wlocate(dsub, 22, 0);  wputs(dsub, what);  wsetregion(w, 1, w->ys - 1);  w->doscroll = 0;  prdir(w, top, top);    wlocate(w, 14, 17);  wputs(w, "( Press Escape to exit.. )");  dhili(subm);  dirflush = 1;  wredraw(dsub, 1);again:    wcurbar(w, cur + 1 - top, A_REVERSE | stdattr);  if (first) {  	wredraw(w, 1);  	first = 0;  }  while(!quit) {  	d = getno(cur);  	switch(c = getch()) {  		case K_UP:  		case 'k':  			cur -= (cur > 0);  			break;  		case K_DN:  		case 'j':  			cur += (cur < nrents - 1);  			break;  		case K_LT:  		case 'h':			subm--;			if (subm < 0) subm = 3;  			break;  		case K_RT:  		case 'l':  			subm = (subm + 1) % 4;  			break;  		case K_PGUP:  		case '\002': /* Control-B */  			pgud = 1;  			quit = 1;  			break;	  		case K_PGDN:  		case '\006': /* Control-F */  			pgud = 2;  			quit = 1;  			break;	  		case '\033':  		case '\r':  		case '\n':  			quit = 1;  			break;  		default:  			break;  	}  	/* Decide if we have to delete the cursor bar */  	if (cur != ocur || quit) wcurbar(w, ocur + 1 - top, A_NORMAL | stdattr);  		  	if (cur < top) {  		top--;  		prdir(w, top, top);		}	if (cur - top > 15) {		top++;  		prdir(w, top, top);		}  	if (cur != ocur) wcurbar(w, cur + 1 - top, A_REVERSE | stdattr);  	ocur = cur;  	dhili(subm);  }  quit = 0;  /* ESC means quit */  if (c == '\033') {  	writedialdir();	wclose(w, 1);	wclose(dsub, 1);	return;  }  /* Page up or down ? */  if (pgud == 1) { /* Page up */  	ocur = top;  	top -= 16;  	if (top < 0) top = 0;  	cur = top;  	pgud = 0;  	if (ocur != top) prdir(w, top, cur);  	ocur = cur;	goto again;  }  if (pgud == 2) { /* Page down */  	ocur = top;  	if (top < nrents - 16) {		top += 16;		if (top > nrents - 16) top = nrents - 16;		cur = top;	} else		cur = nrents - 1;		pgud = 0;	if (ocur != top) prdir(w, top, cur);	ocur = cur;	goto again;  }    /* Dial an entry */  if (subm == 0) {  	wclose(w, 1);  	wclose(dsub, 1);  	writedialdir();  	strcpy(P_BAUDRATE, d->baud);  	strcpy(P_PARITY, d->parity);  	strcpy(P_BITS, d->bits);	m_setparms(portfd, P_BAUDRATE, P_PARITY, P_BITS);  	mode_status();  	newtype = d->term;	vt_set(-1, -1, NULL, -1, -1, d->localecho, -1);	local_echo = d->localecho;  	if ((nb = dial(d->name, d->number, d->script[0] ? 0 : 1,			d->dialtype)) < 0) return;  	/* Did we detect a baudrate , and can we set it? */  	if (P_MAUTOBAUD[0] == 'Y' && nb) {  		sprintf(P_BAUDRATE, "%d", nb);		m_setparms(portfd, P_BAUDRATE, P_PARITY, P_BITS);  	} else  		nb = 0;  	if (newtype != terminal)  		init_emul(newtype);  	else if (nb)  		mode_status();  	if (d->script[0]) runscript(0, d->script, d->username, d->password);	/* Remember _what_ we dialed.. */	dial_name = d->name;	dial_number = d->number;  	return;  }  /* Add / insert an entry */  if (subm == 1) {  	d1 = mkstdent();  	if (d1 == (struct dialent *)0) {  		wbell();  		goto again;  	}  	cur++;  	ocur = cur;  	d2 = d->next;  	d->next = d1;  	d1->next = d2;  	  	nrents++;  	if (cur - top > 15) {  		top++;  		prdir(w, top, top);  	} else {  		prdir(w, top, cur);	}  }  /* Edit an entry */  if (subm == 2) {  	dedit(d);  	wlocate(w, 0, cur + 1 - top);  	wprintf(w, fmt, cur+1, d->name, d->number,  		d->baud, d->bits, d->parity, te[d->term - 1], d->script);  }    /* Delete an entry from the list */  if (subm == 3 && ask("Remove entry?", d_yesno) == 0) {  	if (nrents == 1) {  		free((char *)d);  		d = dialents = mkstdent();  		prdir(w, top, top);  		goto again;  	}  	if (cur == 0)  		dialents = d->next;  	else  		getno(cur - 1)->next = d->next;  	free((char *)d);  	nrents--;  	if (cur - top == 0 && top == nrents) {  		top--;  		cur--;  		prdir(w, top, top);  	} else {  		if (cur == nrents) cur--;		prdir(w, top, cur);  	}	if (nrents - top <= 15) {		wlocate(w, 0, nrents - top + 1);		wclreol(w);	}  	ocur = cur;  }  goto again;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -