Skip to content
Snippets Groups Projects
term.c 37.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
    
    
    /* term_change_size():
     *	Change the size of the terminal
     */
    protected int
    term_change_size(EditLine *el, int lins, int cols)
    {
    	/*
             * Just in case
             */
    	Val(T_co) = (cols < 2) ? 80 : cols;
    	Val(T_li) = (lins < 1) ? 24 : lins;
    
    	/* re-make display buffers */
    	if (term_rebuffer_display(el) == -1)
    		return (-1);
    	re_clear_display(el);
    	return (0);
    }
    
    
    /* term_init_arrow():
     *	Initialize the arrow key bindings from termcap
     */
    private void
    term_init_arrow(EditLine *el)
    {
    	fkey_t *arrow = el->el_term.t_fkey;
    
    	arrow[A_K_DN].name = "down";
    	arrow[A_K_DN].key = T_kd;
    	arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
    	arrow[A_K_DN].type = XK_CMD;
    
    	arrow[A_K_UP].name = "up";
    	arrow[A_K_UP].key = T_ku;
    	arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
    	arrow[A_K_UP].type = XK_CMD;
    
    	arrow[A_K_LT].name = "left";
    	arrow[A_K_LT].key = T_kl;
    	arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
    	arrow[A_K_LT].type = XK_CMD;
    
    	arrow[A_K_RT].name = "right";
    	arrow[A_K_RT].key = T_kr;
    	arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
    	arrow[A_K_RT].type = XK_CMD;
    
    	arrow[A_K_HO].name = "home";
    	arrow[A_K_HO].key = T_kh;
    	arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
    	arrow[A_K_HO].type = XK_CMD;
    
    	arrow[A_K_EN].name = "end";
    	arrow[A_K_EN].key = T_at7;
    	arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
    	arrow[A_K_EN].type = XK_CMD;
    }
    
    
    /* term_reset_arrow():
     *	Reset arrow key bindings
     */
    private void
    term_reset_arrow(EditLine *el)
    {
    	fkey_t *arrow = el->el_term.t_fkey;
    	static const char strA[] = {033, '[', 'A', '\0'};
    	static const char strB[] = {033, '[', 'B', '\0'};
    	static const char strC[] = {033, '[', 'C', '\0'};
    	static const char strD[] = {033, '[', 'D', '\0'};
    	static const char strH[] = {033, '[', 'H', '\0'};
    	static const char strF[] = {033, '[', 'F', '\0'};
    	static const char stOA[] = {033, 'O', 'A', '\0'};
    	static const char stOB[] = {033, 'O', 'B', '\0'};
    	static const char stOC[] = {033, 'O', 'C', '\0'};
    	static const char stOD[] = {033, 'O', 'D', '\0'};
    	static const char stOH[] = {033, 'O', 'H', '\0'};
    	static const char stOF[] = {033, 'O', 'F', '\0'};
    
    	key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
    	key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
    	key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
    	key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
    	key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
    	key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
    	key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
    	key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
    	key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
    	key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
    	key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
    	key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
    
    	if (el->el_map.type == MAP_VI) {
    		key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
    		key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
    		key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
    		key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
    		key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
    		key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
    		key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
    		key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
    		key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
    		key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
    		key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
    		key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
    	}
    }
    
    
    /* term_set_arrow():
     *	Set an arrow key binding
     */
    protected int
    term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
    {
    	fkey_t *arrow = el->el_term.t_fkey;
    	int i;
    
    	for (i = 0; i < A_K_NKEYS; i++)
    		if (strcmp(name, arrow[i].name) == 0) {
    			arrow[i].fun = *fun;
    			arrow[i].type = type;
    			return (0);
    		}
    	return (-1);
    }
    
    
    /* term_clear_arrow():
     *	Clear an arrow key binding
     */
    protected int
    term_clear_arrow(EditLine *el, const char *name)
    {
    	fkey_t *arrow = el->el_term.t_fkey;
    	int i;
    
    	for (i = 0; i < A_K_NKEYS; i++)
    		if (strcmp(name, arrow[i].name) == 0) {
    			arrow[i].type = XK_NOD;
    			return (0);
    		}
    	return (-1);
    }
    
    
    /* term_print_arrow():
     *	Print the arrow key bindings
     */
    protected void
    term_print_arrow(EditLine *el, const char *name)
    {
    	int i;
    	fkey_t *arrow = el->el_term.t_fkey;
    
    	for (i = 0; i < A_K_NKEYS; i++)
    		if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
    			if (arrow[i].type != XK_NOD)
    				key_kprint(el, arrow[i].name, &arrow[i].fun,
    				    arrow[i].type);
    }
    
    
    /* term_bind_arrow():
     *	Bind the arrow keys
     */
    protected void
    term_bind_arrow(EditLine *el)
    {
    	el_action_t *map;
    	const el_action_t *dmap;
    	int i, j;
    	char *p;
    	fkey_t *arrow = el->el_term.t_fkey;
    
    	/* Check if the components needed are initialized */
    	if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
    		return;
    
    	map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
    	dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
    
    	term_reset_arrow(el);
    
    	for (i = 0; i < A_K_NKEYS; i++) {
    		p = el->el_term.t_str[arrow[i].key];
    		if (p && *p) {
    			j = (unsigned char) *p;
    			/*
    		         * Assign the arrow keys only if:
    		         *
    		         * 1. They are multi-character arrow keys and the user
    		         *    has not re-assigned the leading character, or
    		         *    has re-assigned the leading character to be
    		         *	  ED_SEQUENCE_LEAD_IN
    		         * 2. They are single arrow keys pointing to an
    			 *    unassigned key.
    		         */
    			if (arrow[i].type == XK_NOD)
    				key_clear(el, map, p);
    			else {
    				if (p[1] && (dmap[j] == map[j] ||
    					map[j] == ED_SEQUENCE_LEAD_IN)) {
    					key_add(el, p, &arrow[i].fun,
    					    arrow[i].type);
    					map[j] = ED_SEQUENCE_LEAD_IN;
    				} else if (map[j] == ED_UNASSIGNED) {
    					key_clear(el, map, p);
    					if (arrow[i].type == XK_CMD)
    						map[j] = arrow[i].fun.cmd;
    					else
    						key_add(el, p, &arrow[i].fun,
    						    arrow[i].type);
    				}
    			}
    		}
    	}
    }
    
    
    /* term__putc():
     *	Add a character
     */
    protected int
    term__putc(int c)
    {
    
    	return (fputc(c, term_outfile));
    }
    
    
    /* term__flush():
     *	Flush output
     */
    protected void
    term__flush(void)
    {
    
    	(void) fflush(term_outfile);
    }
    
    
    /* term_telltc():
     *	Print the current termcap characteristics
     */
    protected int
    /*ARGSUSED*/
    term_telltc(EditLine *el, int argc, const char **argv)
    {
    	const struct termcapstr *t;
    	char **ts;
    	char upbuf[EL_BUFSIZ];
    
    	(void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
    	(void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
    	(void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
    	    Val(T_co), Val(T_li));
    	(void) fprintf(el->el_outfile,
    	    "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
    	(void) fprintf(el->el_outfile,
    	    "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
    	(void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
    	    EL_HAS_AUTO_MARGINS ? "has" : "does not have");
    	if (EL_HAS_AUTO_MARGINS)
    		(void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
    		    EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
    
    	for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
    		(void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
    		    t->long_name,
    		    t->name, *ts && **ts ?
    		    key__decode_str(*ts, upbuf, "") : "(empty)");
    	(void) fputc('\n', el->el_outfile);
    	return (0);
    }
    
    
    /* term_settc():
     *	Change the current terminal characteristics
     */
    protected int
    /*ARGSUSED*/
    term_settc(EditLine *el, int argc, const char **argv)
    {
    	const struct termcapstr *ts;
    	const struct termcapval *tv;
    	const char *what, *how;
    
    	if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
    		return (-1);
    
    	what = argv[1];
    	how = argv[2];
    
    	/*
             * Do the strings first
             */
    	for (ts = tstr; ts->name != NULL; ts++)
    		if (strcmp(ts->name, what) == 0)
    			break;
    
    	if (ts->name != NULL) {
    		term_alloc(el, ts, how);
    		term_setflags(el);
    		return (0);
    	}
    	/*
             * Do the numeric ones second
             */
    	for (tv = tval; tv->name != NULL; tv++)
    		if (strcmp(tv->name, what) == 0)
    			break;
    
    	if (tv->name != NULL) {
    		if (tv == &tval[T_pt] || tv == &tval[T_km] ||
    		    tv == &tval[T_am] || tv == &tval[T_xn]) {
    			if (strcmp(how, "yes") == 0)
    				el->el_term.t_val[tv - tval] = 1;
    			else if (strcmp(how, "no") == 0)
    				el->el_term.t_val[tv - tval] = 0;
    			else {
    				(void) fprintf(el->el_errfile,
    				    "settc: Bad value `%s'.\n", how);
    				return (-1);
    			}
    			term_setflags(el);
    			if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
    				return (-1);
    			return (0);
    		} else {
    			long i;
    			char *ep;
    
    			i = strtol(how, &ep, 10);
    			if (*ep != '\0') {
    				(void) fprintf(el->el_errfile,
    				    "settc: Bad value `%s'.\n", how);
    				return (-1);
    			}
    			el->el_term.t_val[tv - tval] = (int) i;
    			el->el_term.t_size.v = Val(T_co);
    			el->el_term.t_size.h = Val(T_li);
    			if (tv == &tval[T_co] || tv == &tval[T_li])
    				if (term_change_size(el, Val(T_li), Val(T_co))
    				    == -1)
    					return (-1);
    			return (0);
    		}
    	}
    	return (-1);
    }
    
    
    /* term_echotc():
     *	Print the termcap string out with variable substitution
     */
    protected int
    /*ARGSUSED*/
    term_echotc(EditLine *el, int argc, const char **argv)
    {
    	char *cap, *scap, *ep;
    	int arg_need, arg_cols, arg_rows;
    	int verbose = 0, silent = 0;
    	char *area;
    	static const char fmts[] = "%s\n", fmtd[] = "%d\n";
    	const struct termcapstr *t;
    	char buf[TC_BUFSIZE];
    	long i;
    
    	area = buf;
    
    	if (argv == NULL || argv[1] == NULL)
    		return (-1);
    	argv++;
    
    	if (argv[0][0] == '-') {
    		switch (argv[0][1]) {
    		case 'v':
    			verbose = 1;
    			break;
    		case 's':
    			silent = 1;
    			break;
    		default:
    			/* stderror(ERR_NAME | ERR_TCUSAGE); */
    			break;
    		}
    		argv++;
    	}
    	if (!*argv || *argv[0] == '\0')
    		return (0);
    	if (strcmp(*argv, "tabs") == 0) {
    		(void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
    		return (0);
    	} else if (strcmp(*argv, "meta") == 0) {
    		(void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
    		return (0);
    	} else if (strcmp(*argv, "xn") == 0) {
    		(void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
    		    "yes" : "no");
    		return (0);
    	} else if (strcmp(*argv, "am") == 0) {
    		(void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
    		    "yes" : "no");
    		return (0);
    	} else if (strcmp(*argv, "baud") == 0) {
    #ifdef notdef
    		int i;
    
    		for (i = 0; baud_rate[i].b_name != NULL; i++)
    			if (el->el_tty.t_speed == baud_rate[i].b_rate) {
    				(void) fprintf(el->el_outfile, fmts,
    				    baud_rate[i].b_name);
    				return (0);
    			}
    		(void) fprintf(el->el_outfile, fmtd, 0);
    #else
    
    		(void) fprintf(el->el_outfile, fmtd, (int) el->el_tty.t_speed);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    		return (0);
    	} else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
    		(void) fprintf(el->el_outfile, fmtd, Val(T_li));
    		return (0);
    	} else if (strcmp(*argv, "cols") == 0) {
    		(void) fprintf(el->el_outfile, fmtd, Val(T_co));
    		return (0);
    	}
    	/*
             * Try to use our local definition first
             */
    	scap = NULL;
    	for (t = tstr; t->name != NULL; t++)
    		if (strcmp(t->name, *argv) == 0) {
    			scap = el->el_term.t_str[t - tstr];
    			break;
    		}
    	if (t->name == NULL)
    
    Mark Spencer's avatar
    Mark Spencer committed
    		scap = tgetstr((char *)argv, &area);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!scap || scap[0] == '\0') {
    		if (!silent)
    			(void) fprintf(el->el_errfile,
    			    "echotc: Termcap parameter `%s' not found.\n",
    			    *argv);
    		return (-1);
    	}
    	/*
             * Count home many values we need for this capability.
             */
    	for (cap = scap, arg_need = 0; *cap; cap++)
    		if (*cap == '%')
    			switch (*++cap) {
    			case 'd':
    			case '2':
    			case '3':
    			case '.':
    			case '+':
    				arg_need++;
    				break;
    			case '%':
    			case '>':
    			case 'i':
    			case 'r':
    			case 'n':
    			case 'B':
    			case 'D':
    				break;
    			default:
    				/*
    				 * hpux has lot's of them...
    				 */
    				if (verbose)
    					(void) fprintf(el->el_errfile,
    				"echotc: Warning: unknown termcap %% `%c'.\n",
    					    *cap);
    				/* This is bad, but I won't complain */
    				break;
    			}
    
    	switch (arg_need) {
    	case 0:
    		argv++;
    		if (*argv && *argv[0]) {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Warning: Extra argument `%s'.\n",
    				    *argv);
    			return (-1);
    		}
    		(void) tputs(scap, 1, term__putc);
    		break;
    	case 1:
    		argv++;
    		if (!*argv || *argv[0] == '\0') {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Warning: Missing argument.\n");
    			return (-1);
    		}
    		arg_cols = 0;
    		i = strtol(*argv, &ep, 10);
    		if (*ep != '\0' || i < 0) {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Bad value `%s' for rows.\n",
    				    *argv);
    			return (-1);
    		}
    		arg_rows = (int) i;
    		argv++;
    		if (*argv && *argv[0]) {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Warning: Extra argument `%s'.\n",
    				    *argv);
    			return (-1);
    		}
    		(void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
    		break;
    	default:
    		/* This is wrong, but I will ignore it... */
    		if (verbose)
    			(void) fprintf(el->el_errfile,
    			 "echotc: Warning: Too many required arguments (%d).\n",
    			    arg_need);
    		/* FALLTHROUGH */
    	case 2:
    		argv++;
    		if (!*argv || *argv[0] == '\0') {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Warning: Missing argument.\n");
    			return (-1);
    		}
    		i = strtol(*argv, &ep, 10);
    		if (*ep != '\0' || i < 0) {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Bad value `%s' for cols.\n",
    				    *argv);
    			return (-1);
    		}
    		arg_cols = (int) i;
    		argv++;
    		if (!*argv || *argv[0] == '\0') {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Warning: Missing argument.\n");
    			return (-1);
    		}
    		i = strtol(*argv, &ep, 10);
    		if (*ep != '\0' || i < 0) {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Bad value `%s' for rows.\n",
    				    *argv);
    			return (-1);
    		}
    		arg_rows = (int) i;
    		if (*ep != '\0') {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Bad value `%s'.\n", *argv);
    			return (-1);
    		}
    		argv++;
    		if (*argv && *argv[0]) {
    			if (!silent)
    				(void) fprintf(el->el_errfile,
    				    "echotc: Warning: Extra argument `%s'.\n",
    				    *argv);
    			return (-1);
    		}
    		(void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,
    		    term__putc);
    		break;
    	}
    	return (0);
    }