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

📄 vmh.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
		    break;		}		if (c == WERASC) {		    if (bp <= buffer)			continue;		    do {			bp--, w -> _curx--;		    } while (isspace (*bp) && bp > buffer);		    if (bp > buffer) {			do {			    bp--, w -> _curx--;			} while (!isspace (*bp) && bp > buffer);			if (isspace (*bp))			    bp++, w -> _curx++;		    }		    wclrtoeol (w);		    break;		}				if (c >= ' ' && c < '\177')		    (void) waddch (w, *bp++ = c);		break;	}	wrefresh (w);    }}/*  */static int  WINwritev (w, iov, n)register WINDOW *w;register struct iovec   *iov;register int     n;{    register int    i;    werase (w);    wmove (w, 0, 0);    for (i = 0; i < n; i++, iov++)	wprintw (w, "%*.*s", iov -> iov_len, iov -> iov_len, iov -> iov_base);    wrefresh (w);    sleep (PAUSE);    return OK;}/*  */static struct {    char   *h_msg;    int    *h_val;}               hlpmsg[] = {                    "		forward		backwards", NULL,                    "		-------		---------", NULL,                    "next screen	SPACE", NULL,                    "next %d line%s	RETURN		y", &smallmove,                    "next %d line%s	EOT		u", &largemove,                    "go		g		G", NULL,                    "", NULL,                    "refresh		CTRL-L", NULL,                    "quit		q", NULL,                    NULL, NULL};/*  */static int  WINless (w, fin)register WINDOW *w;int	fin;{    register int    c,                    i,                    n;    int     nfresh,#ifdef	notdef	    nlatch,#endif	/* notdef */            nwait;    char   *cp;    register struct line   *lbottom;    did_less++;    cp = NULL;#ifdef	notdef    if (fin)	ltop = NULL;#endif	/* notdef */    lbottom = NULL;    nfresh = 1;    nwait = 0;    wrefresh (w);    for (;;) {	if (nfresh || nwait) {	    nfresh = 0;#ifdef	notdef	    nlatch = 1;once_only: ;#endif	/* notdef */	    werase (w);	    wmove (w, 0, 0);	    if (ltop == NULL)		if (fin) {		    (void) lgo (ltail -> l_no - w -> _maxy + 1);		    if (ltop == NULL)			ltop = lhead;		}		else		    ltop = lbottom && lbottom -> l_prev ? lbottom -> l_prev			    : lbottom;	    for (lbottom = ltop; lbottom; lbottom = lbottom -> l_next)		if (waddstr (w, lbottom -> l_buf) == ERR			|| waddch (w, '\n') == ERR)		    break;	    if (lbottom == NULL)		if (fin) {#ifdef	notdef		    if (nlatch && (ltail -> l_no >= w -> _maxy)) {			(void) lgo (ltail -> l_no - w -> _maxy + 1);			nlatch = 0;			goto once_only;		    }#endif	/* notdef */		    lbottom = ltail;		    while (waddstr (w, "~\n") != ERR)			continue;		}		else {		    wrefresh (w);		    return 0;		}	    if (!nwait)		wrefresh (w);	}	wmove (Command, 0, 0);	if (cp) {	    wstandout (Command);	    wprintw (Command, "%s", cp);	    wstandend (Command);	    cp = NULL;	}	else	    wprintw (Command, fin ? "top:%d bot:%d end:%d" : "top:%d bot:%d",		    ltop -> l_no, lbottom -> l_no, ltail -> l_no);	wprintw (Command, ">> ");	wclrtoeol (Command);	wrefresh (Command);	c = toascii (wgetch (Command));	werase (Command);	wrefresh (Command);	if (nwait) {	    nwait = 0;	    wrefresh (w);	}	n = 0;again: 	;	switch (c) {	    case ' ': 		ltop = lbottom -> l_next;		nfresh++;		break;	    case '\r': 	    case '\n': 	    case 'e': 	    case 'j': 		if (n)		    smallmove = n;		if (ladvance (smallmove))		    nfresh++;		break;	    case 'y': 	    case 'k': 		if (n)		    smallmove = n;		if (lretreat (smallmove))		    nfresh++;		break;	    case 'd': 	eof: 	;		if (n)		    largemove = n;		if (ladvance (largemove))		    nfresh++;		break;	    case 'u': 		if (n)		    largemove = n;		if (lretreat (largemove))		    nfresh++;		break;	    case 'g': 		if (lgo (n ? n : 1))		    nfresh++;		break;	    case 'G': 		if (lgo (n ? n : ltail -> l_no - w -> _maxy + 1))		    nfresh++;		break;	    case '\f': 	    case 'r': 		wrefresh (curscr);		break;	    case 'h': 	    case '?': 		werase (w);		wmove (w, 0, 0);		for (i = 0; hlpmsg[i].h_msg; i++) {		    if (hlpmsg[i].h_val)			wprintw (w, hlpmsg[i].h_msg, *hlpmsg[i].h_val,				*hlpmsg[i].h_val != 1 ? "s" : "");		    else			(void) waddstr (w, hlpmsg[i].h_msg);		    (void) waddch (w, '\n');		}		wrefresh (w);		nwait++;		break;	    case 'q': 		return 1;	    default: 		if (c == EOFC)		    goto eof;		if (isdigit (c)) {		    wmove (Command, 0, 0);		    i = 0;		    while (isdigit (c)) {			wprintw (Command, "%c", c);			wrefresh (Command);			i = i * 10 + c - '0';			c = toascii (wgetch (Command));		    }		    werase (Command);		    wrefresh (Command);		    if (i > 0) {			n = i;			goto again;		    }		    cp = "bad number";		}		else		    cp = "not understood";		break;	}    }}/*  */static int  WINputc (w, c)register WINDOW *w;register char c;{    register int    x,                    y;    switch (c) {	default: 	    if (!isascii (c)) {		if (WINputc (w, 'M') == ERR || WINputc (w, '-') == ERR)		    return ERR;		c = toascii (c);	    }	    else		if (c < ' ' || c == '\177') {		    if (WINputc (w, '^') == ERR)			return ERR;		    c ^= 0100;		}	    break;	case '\t': 	case '\n': 	    break;    }    if (w != Scan)	return waddch (w, c);    if ((x = w -> _curx) < 0 || x >= w -> _maxx	    || (y = w -> _cury) < 0 || y >= w -> _maxy)	return DONE;    switch (c) {	case '\t': 	    for (x = 8 - (x & 0x07); x > 0; x--)		if (WINputc (w, ' ') == ERR)		    return ERR;	    break;	case '\n': 	    if (++y < w -> _maxy) 		(void) waddch (w, c);	    else		wclrtoeol (w);	    break;	default: 	    if (++x < w -> _maxx) 		(void) waddch (w, c);	    break;    }    return DONE;}/*    LINES */static  lreset () {    register struct line   *lp,                           *mp;    for (lp = lhead; lp; lp = mp) {	mp = lp -> l_next;	free (lp -> l_buf);	free ((char *) lp);    }    lhead = ltop = ltail = NULL;}static	linsert (w)WINDOW *w;{    register char  *cp;    register struct line   *lp;    if ((lp = (struct line  *) calloc ((unsigned) 1, sizeof *lp)) == NULL)	adios (NULLCP, "unable to allocate line storage");    lp -> l_no = (ltail ? ltail -> l_no : 0) + 1;#ifndef	BSD44    lp -> l_buf = getcpy (w -> _y[w -> _cury]);#else    lp -> l_buf = getcpy (w -> lines[w -> _cury]->line);#endif    for (cp = lp -> l_buf + strlen (lp -> l_buf) - 1; cp >= lp -> l_buf; cp--)	if (isspace (*cp))	    *cp = 0;	else	    break;    if (lhead == NULL)	lhead = lp;    if (ltop == NULL)	ltop = lp;    if (ltail)	ltail -> l_next = lp;    lp -> l_prev = ltail;    ltail = lp;}/*  */static int  ladvance (n)int	n;{    register int    i;    register struct line   *lp;    for (i = 0, lp = ltop; i < n && lp; i++, lp = lp -> l_next)	continue;    if (ltop == lp)	return 0;    ltop = lp;    return 1;}static int  lretreat (n)int	n;{    register int    i;    register struct line   *lp;    for (i = 0, lp = ltop; i < n && lp; i++, lp = lp -> l_prev)	if (!lp -> l_prev)	    break;    if (ltop == lp)	return 0;    ltop = lp;    return 1;}/*  */static int  lgo (n)int	n;{    register int    i,                    j;    register struct line   *lp;    if ((i = n - (lp = lhead) -> l_no)	    > (j = abs (n - (ltop ? ltop : ltail) -> l_no)))	i = j, lp = ltop ? ltop : ltail;    if (i > (j = abs (ltail -> l_no - n)))	i = j, lp = ltail;    if (n >= lp -> l_no) {	for (; lp; lp = lp -> l_next)	    if (lp -> l_no == n)		break;    }    else {	for (; lp; lp = lp -> l_prev)	    if (lp -> l_no == n)		break;	if (!lp)	    lp = lhead;    }    if (ltop == lp)	return 0;    ltop = lp;    return 1;}/*    TTYS */static int  TTYinit (nprog) {    if (!isatty (fileno (stdin)) || !isatty (fileno (stdout)))	if (nprog)	    return NOTOK;	else	    adios (NULLCP, "not a tty");    foreground ();#ifndef	SYS5    if (ioctl (fileno (stdin), TIOCGETP, (char *) &sg) == NOTOK)	adios ("failed", "ioctl TIOCGETP");    if (ioctl (fileno (stdin), TIOCGETC, (char *) &tc) == NOTOK)	adios ("failed", "ioctl TIOCGETC");#else#ifdef	TCGETATTR    if( tcgetattr( fileno(stdin), &sg) == NOTOK)	adios( "failed", "tcgetattr");#else	/* SYS5 */    if (ioctl (fileno (stdin), TCGETA, &sg) == NOTOK)	adios ("failed", "ioctl TCGETA");#endif#endif#ifdef	TIOCGLTC    if (ioctl (fileno (stdin), TIOCGLTC, (char *) &ltc) == NOTOK)	adios ("failed", "ioctl TIOCGLTC");#endif	/* TIOCGLTC */    intrc = INTR;    sideground ();    tty_ready = OK;    (void) signal (SIGPIPE, PIPEser);    return OK;}/*  */static	TTYon () {    if (tty_ready == DONE)	return;    INTR = NOTOK;#ifndef	SYS5    (void) ioctl (fileno (stdin), TIOCSETC, (char *) &tc);#else	/* SYS5 */    (void) ioctl (fileno (stdin), TCSETA, &sg);#endif	/* SYS5 */    (void) crmode ();    (void) noecho ();    (void) nonl ();    scrollok (curscr, FALSE);    discard (stdin);    tty_ready = DONE;    (void) signal (SIGHUP, SIGser);    (void) signal (SIGINT, SIGser);    (void) signal (SIGQUIT, SIGser);#ifdef	SIGTSTP    (void) signal (SIGTSTP, TSTPser);#endif	/* SIGTSTP */}/*  */static	TTYoff () {    if (tty_ready == NOTOK)	return;    INTR = intrc;#ifndef	SYS5    (void) ioctl (fileno (stdin), TIOCSETC, (char *) &tc);#else	/* SYS5 */    (void) ioctl (fileno (stdin), TCSETA, &sg);#endif	/* SYS5 */    leaveok (curscr, TRUE);    mvcur (0, COLS - 1, LINES - 1, 0);    endwin ();    if (tty_ready == DONE) {#ifndef	TERMINFO	if (CE)	    tputs (CE, 0, _putchar);	else#else	/* TERMINFO */	putp(clr_eol);#endif	/* TERMINFO */	    fprintf (stdout, "\r\n");    }    (void) fflush (stdout);    tty_ready = NOTOK;    (void) signal (SIGHUP, SIG_DFL);    (void) signal (SIGINT, SIG_DFL);    (void) signal (SIGQUIT, SIG_DFL);#ifdef	SIGTSTP    (void) signal (SIGTSTP, SIG_DFL);#endif	/* SIGTSTP */}/*  */static  foreground () {#ifdef	TIOCGPGRP    int     pgrp,            tpgrp;    TYPESIG     (*tstat) ();    if ((pgrp = getpgrp (0)) == NOTOK)	adios ("process group", "unable to determine");    for (;;) {	if (ioctl (fileno (stdin), TIOCGPGRP, (char *) &tpgrp) == NOTOK)	    adios ("tty's process group", "unable to determine");	if (pgrp == tpgrp)	    break;	tstat = signal (SIGTTIN, SIG_DFL);	(void) kill (0, SIGTTIN);	(void) signal (SIGTTIN, tstat);    }        (void) signal (SIGTTIN, SIG_IGN);    (void) signal (SIGTTOU, SIG_IGN);    (void) signal (SIGTSTP, SIG_IGN);#endif	/* TIOCGPGRP */}sideground () {#ifdef	TIOCGPGRP    (void) signal (SIGTTIN, SIG_DFL);    (void) signal (SIGTTOU, SIG_DFL);    (void) signal (SIGTSTP, SIG_DFL);#endif	/* TIOCGPGRP */}/*    SIGNALS *//* ARGSUSED */static TYPESIG  ALRMser (sig)int     sig;{     longjmp (PEERctx, DONE);}#ifdef	BSD42/* ARGSUSED */#endif	/* BSD42 */static TYPESIG  PIPEser (sig)int	sig;{#ifndef	BSD42    (void) signal (sig, SIG_IGN);#endif	/* BSD42 */    adios (NULLCP, "lost peer");}#ifdef	BSD42/* ARGSUSED */#endif	/* BSD42 */static TYPESIG  SIGser (sig)int     sig;{#ifndef	BSD42    (void) signal (sig, SIG_IGN);#endif	/* BSD42 */    done (1);}#ifdef	SIGTSTPstatic TYPESIG  TSTPser (sig)int     sig;{#ifndef	TERMINFO    tputs (tgoto (CM, 0, LINES - 1), 0, _putchar);#else	/* TERMINFO */    move(LINES - 1, 0);	/* to lower left corner */    clrtoeol();		/* clear bottom line */    wrefresh(curscr);	/* flush out everything */#endif	/* TERMINFO */    (void) fflush (stdout);    TTYoff ();#ifdef	BSD42    (void) sigsetmask (sigblock (0) & ~sigmask (SIGTSTP));#endif	/* BSD42 */    (void) kill (getpid (), sig);#ifdef	BSD42    (void) sigblock (sigmask (SIGTSTP));#endif	/* BSD42 */    TTYon ();    wrefresh (curscr);}#endif	/* SIGTSTP *//*    MISCELLANY */void	done (status)int	status;{    TTYoff ();    (void) pFIN ();    exit (status);}/*  *//* VARARGS2 */static void  adorn (what, fmt, a, b, c, d, e, f)char   *what,       *fmt,       *a,       *b,       *c,       *d,       *e,       *f;{    char   *cp = invo_name;    invo_name = NULL;    advise (what, fmt, a, b, c, d, e, f);    invo_name = cp;}/*  *//* VARARGS3 */void advertise (what, tail, fmt, a, b, c, d, e, f)char   *what,       *tail,       *fmt,       *a,       *b,       *c,       *d,       *e,       *f;{    int	    eindex = errno;    char    buffer[BUFSIZ],            err[BUFSIZ];    struct iovec    iob[20];    register struct iovec  *iov = iob;    (void) fflush (stdout);    (void) fflush (stderr);    if (invo_name) {	iov -> iov_len = strlen (iov -> iov_base = invo_name);	iov++;	iov -> iov_len = strlen (iov -> iov_base = ": ");	iov++;    }        (void) sprintf (buffer, fmt, a, b, c, d, e, f);    iov -> iov_len = strlen (iov -> iov_base = buffer);    iov++;    if (what) {	if (*what) {	    iov -> iov_len = strlen (iov -> iov_base = " ");	    iov++;	    iov -> iov_len = strlen (iov -> iov_base = what);	    iov++;	    iov -> iov_len = strlen (iov -> iov_base = ": ");	    iov++;	}	if (eindex > 0 && eindex < sys_nerr)	    iov -> iov_len = strlen (iov -> iov_base = sys_errlist[eindex]);	else {	    (void) sprintf (err, "Error %d", eindex);	    iov -> iov_len = strlen (iov -> iov_base = err);	}	iov++;    }    if (tail && *tail) {	iov -> iov_len = strlen (iov -> iov_base = ", ");	iov++;	iov -> iov_len = strlen (iov -> iov_base = tail);	iov++;    }    iov -> iov_len = strlen (iov -> iov_base = "\n");    iov++;    if (tty_ready == DONE)	(void) WINwritev (Display, iob, iov - iob);    else	(void) writev (fileno (stderr), iob, iov - iob);}/*  */#ifndef	BSD42static int     writev (fd, iov, n)register int     fd;register struct iovec   *iov;register int     n;{    register int    i,                    j;    for (i = j = 0; i < n; i++, iov++)	if (write (fd, iov -> iov_base, iov -> iov_len) != iov -> iov_len)	    break;	else	    j += iov -> iov_len;    return j;}#endif	/* BSD42 */

⌨️ 快捷键说明

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