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

📄 term.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
/*- * Copyright (c) 1992, 1993 *	The Regents of the University of California.  All rights reserved. * * This code is derived from software contributed to Berkeley by * Christos Zoulas of Cornell University. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#if !defined(lint) && !defined(SCCSID)static char sccsid[] = "@(#)term.c	8.1 (Berkeley) 6/4/93";#endif /* not lint && not SCCSID *//* * term.c: Editor/termcap-curses interface *	   We have to declare a static variable here, since the *	   termcap putchar routine does not take an argument! */#include "sys.h"#include <stdio.h>#include <signal.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include "termcap.h"	/* XXX: should be <termcap.h> */#include <sys/types.h>#include "el.h"/* * IMPORTANT NOTE: these routines are allowed to look at the current screen * and the current possition assuming that it is correct.  If this is not * true, then the update will be WRONG!  This is (should be) a valid * assumption... */#define TC_BUFSIZE 2048#define GoodStr(a) (el->el_term.t_str[a] != NULL && \		    el->el_term.t_str[a][0] != '\0')#define Str(a) el->el_term.t_str[a]#define Val(a) el->el_term.t_val[a]private struct {    char   *b_name;    int     b_rate;} baud_rate[] = {#ifdef B0    { "0", B0 },#endif#ifdef B50    { "50", B50 },#endif#ifdef B75    { "75", B75 },#endif#ifdef B110    { "110", B110 },#endif#ifdef B134    { "134", B134 },#endif#ifdef B150    { "150", B150 },#endif#ifdef B200    { "200", B200 },#endif#ifdef B300    { "300", B300 },#endif#ifdef B600    { "600", B600 },#endif#ifdef B900    { "900", B900 },#endif#ifdef B1200    { "1200", B1200 },#endif#ifdef B1800    { "1800", B1800 },#endif#ifdef B2400    { "2400", B2400 },#endif#ifdef B3600    { "3600", B3600 },#endif#ifdef B4800    { "4800", B4800 },#endif#ifdef B7200    { "7200", B7200 },#endif#ifdef B9600    { "9600", B9600 },#endif#ifdef EXTA    { "19200", EXTA },#endif#ifdef B19200    { "19200", B19200 },#endif#ifdef EXTB    { "38400", EXTB },#endif#ifdef B38400    { "38400", B38400 },#endif    { NULL, 0 }};private struct termcapstr {    char   *name;    char   *long_name;} tstr[] = {#define T_al	0    {	"al",	"add new blank line"		},#define T_bl	1    {	"bl",	"audible bell"			},#define T_cd	2    {	"cd",	"clear to bottom"		},#define T_ce	3    {	"ce",	"clear to end of line"		},#define T_ch	4    {	"ch",	"cursor to horiz pos"		},#define T_cl	5    {	"cl",	"clear screen"			},#define	T_dc	6    {	"dc",	"delete a character"		},#define	T_dl	7    {	"dl",	"delete a line"		 	},#define	T_dm	8    {	"dm",	"start delete mode"		},#define	T_ed	9    {	"ed",	"end delete mode"		},#define	T_ei	10    {	"ei",	"end insert mode"		},#define	T_fs	11    {	"fs",	"cursor from status line"	},#define	T_ho	12    {	"ho",	"home cursor"			},#define	T_ic	13    {	"ic",	"insert character"		},#define	T_im	14     {	"im",	"start insert mode"		},#define	T_ip	15    {	"ip",	"insert padding"		},#define	T_kd	16    {	"kd",	"sends cursor down"		},#define	T_kl	17    {	"kl",	"sends cursor left"		},#define T_kr	18    {	"kr",	"sends cursor right"		},#define T_ku	19    {	"ku",	"sends cursor up"		},#define T_md	20    {	"md",	"begin bold"			},#define T_me	21    {	"me",	"end attributes"		},#define T_nd	22    {	"nd",	"non destructive space"	 	},#define T_se	23    {	"se",	"end standout"			},#define T_so	24    {	"so",	"begin standout"		},#define T_ts	25    {	"ts",	"cursor to status line"	 	},#define T_up	26    {	"up",	"cursor up one"		 	},#define T_us	27    {	"us",	"begin underline"		},#define T_ue	28    {	"ue",	"end underline"		 	},#define T_vb	29    {	"vb",	"visible bell"			},#define T_DC	30    {	"DC",	"delete multiple chars"	 	},#define T_DO	31    {	"DO",	"cursor down multiple"		},#define T_IC	32    {	"IC",	"insert multiple chars"	 	},#define T_LE	33    {	"LE",	"cursor left multiple"		},#define T_RI	34    {	"RI",	"cursor right multiple"	 	},#define T_UP	35    {	"UP",	"cursor up multiple"		},#define T_str	36    {	NULL,   NULL			 	}};private struct termcapval {    char   *name;    char   *long_name;} tval[] = {#define T_pt	0    {	"pt",	"has physical tabs"	},#define T_li	1    {	"li",	"Number of lines"	},#define T_co	2    {	"co",	"Number of columns"	},#define T_km	3    {	"km",	"Has meta key"		},#define T_xt	4    {	"xt",	"Tab chars destructive" },#define T_MT	5    {	"MT",	"Has meta key"		},	/* XXX? */#define T_val	6    {	NULL, 	NULL,			}};/* do two or more of the attributes use me */private	void	term_rebuffer_display	__P((EditLine *));private	void	term_free_display	__P((EditLine *));private	void	term_alloc_display	__P((EditLine *));private	void	term_alloc		__P((EditLine *,					     struct termcapstr *, char *)); private void	term_init_arrow		__P((EditLine *));private void	term_reset_arrow	__P((EditLine *));private FILE *term_outfile = NULL;	/* XXX: How do we fix that? *//* term_setflags(): *	Set the terminal capability flags */private voidterm_setflags(el)    EditLine *el;{    EL_FLAGS = 0;    if (el->el_tty.t_tabs) 	EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;    EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;    EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;    EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;    EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?		 TERM_CAN_INSERT : 0;    EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP))  ? TERM_CAN_UP : 0;    if (GoodStr(T_me) && GoodStr(T_ue))	EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ? TERM_CAN_ME : 0;    else	EL_FLAGS &= ~TERM_CAN_ME;    if (GoodStr(T_me) && GoodStr(T_se))	EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ? TERM_CAN_ME : 0;#ifdef DEBUG_SCREEN    if (!EL_CAN_UP) {	(void) fprintf(el->el_errfile, "WARNING: Your terminal cannot move up.\n");	(void) fprintf(el->el_errfile, "Editing may be odd for long lines.\n");    }    if (!EL_CAN_CEOL)	(void) fprintf(el->el_errfile, "no clear EOL capability.\n");    if (!EL_CAN_DELETE)	(void) fprintf(el->el_errfile, "no delete char capability.\n");    if (!EL_CAN_INSERT)	(void) fprintf(el->el_errfile, "no insert char capability.\n");#endif /* DEBUG_SCREEN */}/* term_init(): *	Initialize the terminal stuff */protected intterm_init(el)    EditLine *el;{    el->el_term.t_buf = (char *)  el_malloc(TC_BUFSIZE);    el->el_term.t_cap = (char *)  el_malloc(TC_BUFSIZE);    el->el_term.t_fkey = (fkey_t *) el_malloc(4 * sizeof(fkey_t));    el->el_term.t_loc = 0;    el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char*));    (void) memset(el->el_term.t_str, 0, T_str * sizeof(char*));    el->el_term.t_val = (int *)   el_malloc(T_val * sizeof(int));    (void) memset(el->el_term.t_val, 0, T_val * sizeof(char*));    term_outfile = el->el_outfile;    (void) term_set(el, NULL);    term_init_arrow(el);    return 0;}/* term_end(): *	Clean up the terminal stuff */protected voidterm_end(el)    EditLine *el;{    el_free((ptr_t) el->el_term.t_buf);    el->el_term.t_buf = NULL;    el_free((ptr_t) el->el_term.t_cap);    el->el_term.t_cap = NULL;    el->el_term.t_loc = 0;    el_free((ptr_t) el->el_term.t_str);    el->el_term.t_str = NULL;    el_free((ptr_t) el->el_term.t_val);    el->el_term.t_val = NULL;    term_free_display(el);}/* term_alloc(): *	Maintain a string pool for termcap strings */private voidterm_alloc(el, t, cap)    EditLine *el;    struct termcapstr *t;    char   *cap;{    char    termbuf[TC_BUFSIZE];    int     tlen, clen;    char    **tlist = el->el_term.t_str;    char    **tmp, **str = &tlist[t - tstr];    if (cap == NULL || *cap == '\0') {	*str = NULL;	return;    }    else	clen = strlen(cap);    tlen  = *str == NULL ? 0 : strlen(*str);    /*     * New string is shorter; no need to allocate space     */    if (clen <= tlen) {	(void) strcpy(*str, cap);	return;    }    /*     * New string is longer; see if we have enough space to append     */    if (el->el_term.t_loc + 3 < TC_BUFSIZE) {	(void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);	el->el_term.t_loc += clen + 1;	/* one for \0 */	return;    }    /*     * Compact our buffer; no need to check compaction, cause we know it     * fits...     */    tlen = 0;    for (tmp = tlist; tmp < &tlist[T_str]; tmp++)	if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {	    char   *ptr;	    for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)		continue;	    termbuf[tlen++] = '\0';	}    memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);    el->el_term.t_loc = tlen;    if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {	(void) fprintf(el->el_errfile, "Out of termcap string space.\n");	return;    }    (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);    el->el_term.t_loc += clen + 1;		/* one for \0 */    return;} /* end term_alloc *//* term_rebuffer_display(): *	Rebuffer the display after the screen changed size */private voidterm_rebuffer_display(el)    EditLine *el;{    coord_t *c = &el->el_term.t_size;    term_free_display(el);    /* make this public, -1 to avoid wraps */    c->h = Val(T_co) - 1;    c->v = (EL_BUFSIZ * 4) / c->h + 1;    term_alloc_display(el);} /* end term_rebuffer_display *//* term_alloc_display(): *	Allocate a new display. */private voidterm_alloc_display(el)    EditLine *el;{    int i;    char  **b;    coord_t *c = &el->el_term.t_size;    b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));    for (i = 0; i < c->v; i++)	b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));    b[c->v] = NULL;    el->el_display = b;    b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));    for (i = 0; i < c->v; i++)	b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));    b[c->v] = NULL;    el->el_vdisplay = b;} /* end term_alloc_display *//* term_free_display(): *	Free the display buffers */private voidterm_free_display(el)    EditLine *el;{    char  **b;    char  **bufp;    b = el->el_display;    el->el_display = NULL;    if (b != NULL) {	for (bufp = b; *bufp != NULL; bufp++)	    el_free((ptr_t) *bufp);	el_free((ptr_t) b);    }    b = el->el_vdisplay;    el->el_vdisplay = NULL;    if (b != NULL) {	for (bufp = b; *bufp != NULL; bufp++)	    el_free((ptr_t) * bufp);	el_free((ptr_t) b);    }} /* end term_free_display *//* term_move_to_line(): *	move to line <where> (first line == 0) * 	as efficiently as possible */protected voidterm_move_to_line(el, where)    EditLine *el;    int     where;		{

⌨️ 快捷键说明

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