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

📄 main.c

📁 minicom源碼,用於Linux下Terminal的重要程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * main.c	main loop of emulator. * *		This file is part of the minicom communications package, *		Copyright 1991-1995 Miquel van Smoorenburg. * *		This program is free software; you can redistribute it and/or *		modify it under the terms of the GNU General Public License *		as published by the Free Software Foundation; either version *		2 of the License, or (at your option) any later version. * * fmg 1/11/94 color mods * jl  22.06.97 log it when DCD drops * jl  02.06.98 added call duration to the "Gone offline" log line * jl  14.06.99 moved lockfile creation to before serial port opening * */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include "rcsid.h"RCSID("$Id: main.c,v 1.7 2003/05/10 18:10:37 al-guest Exp $")#include "port.h"#include "minicom.h"#include "intl.h"#ifdef HAVE_ERRNO_H#include <errno.h>#endif#ifdef SVR4_LOCKS#include <sys/types.h>#include <sys/stat.h>#include <sys/mkdev.h>#endif /* SVR4_LOCKS */static jmp_buf albuf;/* Compile SCCS ID into executable. */char *Version = VERSION;void curs_status();/* * Find out name to use for lockfile when locking tty. */char *mbasename(char *s, char *res, int reslen){  char *p;    if (strncmp(s, "/dev/", 5) == 0) {	/* In /dev */	strncpy(res, s + 5, reslen - 1);	res[reslen-1] = 0;	for (p = res; *p; p++)		if (*p == '/') *p = '_';  } else {	/* Outside of /dev. Do something sensible. */	if ((p = strrchr(s, '/')) == NULL)  		p = s;	else  		p++;	strncpy(res, p, reslen - 1);	res[reslen-1] = 0;  }  return res;}/* * Leave. */void leave(s)char *s;{  if (stdwin) wclose(stdwin, 1);  if (portfd > 0) {	m_restorestate(portfd);	close(portfd);  }  set_privs();  if (lockfile[0]) unlink(lockfile);  if (P_CALLIN[0]) (void) fastsystem(P_CALLIN, CNULL, CNULL, CNULL);  if (real_uid) chown(dial_tty, (uid_t)portuid, (gid_t)portgid);  fprintf(stderr, "%s", s);  exit(1);}/* * Return text describing command-key. */char *esc_key(){  static char buf[16];  if (!alt_override && P_ESCAPE[0] == '^' && P_ESCAPE[1] != '[') {	sprintf(buf, "CTRL-%c ", P_ESCAPE[1]);	return(buf);  }#if defined(_COHERENT) || defined(i386) || defined(__i386__)  sprintf(buf, "ESC,");#else  sprintf(buf, "Meta-");#endif  return(buf);}/*ARGSUSED*/static void get_alrm(dummy)int dummy;{  (void)dummy;  longjmp(albuf, 1);}#ifdef USE_SOCKET/* * If portfd is a socket, we try to (re)connect */void term_socket_connect(){  if (!portfd_is_socket || portfd_is_connected)    return;  if ((portfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)    return;  if (connect(portfd, (struct sockaddr *)&portfd_sock_addr,              sizeof(portfd_sock_addr)) == -1)    term_socket_close();  else    portfd_is_connected = 1;}/* * Close the connection if the socket delivers "eof" (read returns 0) */void term_socket_close(){  close(portfd);  portfd_is_connected = 0;  portfd = -1;}#endif /* USE_SOCKET *//* * Open the terminal. */int open_term(doinit)int doinit;{  struct stat stt;  char buf[128];  int fd, n = 0;  int pid;  int mask;#ifdef HAVE_ERRNO_H  int s_errno;#endif  /* Upgrade our status. */  set_privs();  /* First see if the lock file directory is present. */  if (P_LOCK[0] && stat(P_LOCK, &stt) == 0) {#ifdef SVR4_LOCKS    stat(dial_tty, &stt);    sprintf(lockfile, "%s/LK.%03d.%03d.%03d", P_LOCK, major(stt.st_dev), major(stt.st_rdev), minor(stt.st_rdev));#else /* SVR4_LOCKS */    snprintf(lockfile, sizeof(lockfile), "%s/LCK..%s", P_LOCK, mbasename(dial_tty, buf, sizeof(buf)));#endif /* SVR4_LOCKS */  }  else    lockfile[0] = 0;  if (doinit > 0 && lockfile[0] && (fd = open(lockfile, O_RDONLY)) >= 0) {	n = read(fd, buf, 127);	close(fd);	if (n > 0) {		pid = -1;		if (n == 4)			/* Kermit-style lockfile. */			pid = *(int *)buf;		else {			/* Ascii lockfile. */			buf[n] = 0;			sscanf(buf, "%d", &pid);		}		if (pid > 0 && kill((pid_t)pid, 0) < 0 &&			errno == ESRCH) {		    fprintf(stderr, _("Lockfile is stale. Overriding it..\n"));		    sleep(1);		    unlink(lockfile);		} else		    n = 0;	}	if (n == 0) {  		if (stdwin != NIL_WIN) wclose(stdwin, 1);  		fprintf(stderr, _("Device %s is locked.\n"), dial_tty);		drop_privs();		return(-1);	}  }  if (doinit > 0 && lockfile[0]) {  	/* Create lockfile compatible with UUCP-1.2 */	mask = umask(022);#ifdef _COH3	if ((fd = creat(lockfile, 0666)) < 0) {#else  	if ((fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0) {#endif  		if (stdwin != NIL_WIN) wclose(stdwin, 1);  		fprintf(stderr, _("Cannot create lockfile. Sorry.\n"));		drop_privs();		return(-1);  	}	(void) umask(mask);	(void) chown(lockfile, (uid_t)real_uid, (gid_t)real_gid);  	snprintf(buf, sizeof(buf), "%10ld minicom %.20s\n", (long)getpid(), username);  	write(fd, buf, strlen(buf));  	close(fd);  }  /* Run a special program to disable callin if needed. */  if (doinit > 0 && P_CALLOUT[0]) {	if(fastsystem(P_CALLOUT, CNULL, CNULL, CNULL) < 0) {  		if (stdwin != NIL_WIN) wclose(stdwin, 1);  		fprintf(stderr, _("Could not setup for dial out.\n"));  		if (lockfile[0]) unlink(lockfile);		drop_privs();		return(-1);  	}  }  /* Now open the tty device. */  if (setjmp(albuf) == 0) {	portfd = -1;	signal(SIGALRM, get_alrm);	alarm(4);#if USE_SOCKET#define SOCKET_PREFIX "unix#"	portfd_is_socket = portfd_is_connected = 0;	if (strncmp(dial_tty, SOCKET_PREFIX, strlen(SOCKET_PREFIX)) == 0) {		portfd_is_socket = 1;		portfd_sock_addr.sun_family = AF_UNIX;		strncpy(portfd_sock_addr.sun_path,		        dial_tty + strlen(SOCKET_PREFIX),			sizeof(portfd_sock_addr.sun_path)-1);		portfd_sock_addr.sun_path[sizeof(portfd_sock_addr.sun_path)-1] = 0;		term_socket_connect();	}#endif /* USE_SOCKET */	if (!portfd_is_socket) {#if defined(O_NDELAY) && defined(F_SETFL)		portfd = open(dial_tty, O_RDWR|O_NDELAY|O_NOCTTY);		if (portfd >= 0) {			/* Cancel the O_NDELAY flag. */			n = fcntl(portfd, F_GETFL, 0);			(void) fcntl(portfd, F_SETFL, n & ~O_NDELAY);		}#else		if (portfd < 0)			portfd = open(dial_tty, O_RDWR|O_NOCTTY);#endif	}	if (portfd >= 0) {		if (doinit > 0) m_savestate(portfd);		port_init();	}  }#ifdef HAVE_ERRNO_H  s_errno = errno;#endif  alarm(0);  signal(SIGALRM, SIG_IGN);  if (portfd < 0 && !portfd_is_socket) {	if (doinit > 0) {  		if (stdwin != NIL_WIN) wclose(stdwin, 1);#ifdef HAVE_ERRNO_H		fprintf(stderr, _("minicom: cannot open %s: %s\n"),			dial_tty, strerror(s_errno));#else  		fprintf(stderr, _("minicom: cannot open %s. Sorry.\n"), dial_tty);#endif		if (lockfile[0]) unlink(lockfile);		drop_privs();		return(-1);	}	if (lockfile[0]) unlink(lockfile);	werror(_("Cannot open %s!"), dial_tty);	drop_privs();	return(-1);  }  /* Remember owner of port */  stat(dial_tty, &stt);  portuid = stt.st_uid;  portgid = stt.st_gid;  /* Give it to us! */  if (real_uid != 0) chown(dial_tty, (uid_t)real_uid, (gid_t)real_gid);  /* Set CLOCAL mode */  m_nohang(portfd);  /* Set Hangup on Close if program crashes. (Hehe) */  m_hupcl(portfd, 1);  if (doinit > 0) m_flush(portfd);  drop_privs();  return(0);}/* Function to write output. */static void do_output(s, len)char *s;int len;{  char buf[256];  int f;  if (len == 0) len = strlen(s);  if (P_PARITY[0] == 'M') {	for(f = 0; f < len && f < 256; f++)		buf[f] = *s++ | 0x80;	write(portfd, buf, f);  } else	write(portfd, s, len);}/* Function to handle keypad mode switches. */static void kb_handler(a, b)int a,b;{  cursormode = b;  keypadmode = a;  if (st) curs_status();}/* * Initialize screen and status line. */void init_emul(type, do_init)int type;int do_init;{  int x = -1, y = -1;  char attr = 0;  int maxy;  int ypos;  extern int use_status;  if (st != NIL_WIN) {  	wclose(st, 1);  	tempst = 0;  	st = NIL_WIN;  }  if (us != NIL_WIN) {	x = us->curx; y = us->cury;	attr = us->attr;  	wclose(us, 0);  }	  /* See if we have space for a fixed status line */  maxy = LINES - 1;  if ((use_status || LINES > 24)		&& P_STATLINE[0] == 'e') {	if (use_status) {		ypos = LINES;		maxy = LINES - 1;	} else {		ypos = LINES - 1;

⌨️ 快捷键说明

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