term.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /* $NetBSD: term.c,v 1.35 2002/03/18 16:00:59 christos Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Christos Zoulas of Cornell University.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #include "config.h"
  38. #if !defined(lint) && !defined(SCCSID)
  39. #if 0
  40. static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
  41. #else
  42. __RCSID("$NetBSD: term.c,v 1.35 2002/03/18 16:00:59 christos Exp $");
  43. #endif
  44. #endif /* not lint && not SCCSID */
  45. /*
  46. * term.c: Editor/termcap-curses interface
  47. * We have to declare a static variable here, since the
  48. * termcap putchar routine does not take an argument!
  49. */
  50. #include <stdio.h>
  51. #include <signal.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54. #include <unistd.h>
  55. #ifdef HAVE_TERMCAP_H
  56. #include <termcap.h>
  57. #endif
  58. #ifdef HAVE_CURSES_H
  59. #include <curses.h>
  60. #endif
  61. #ifdef HAVE_NCURSES_H
  62. #include <ncurses.h>
  63. #endif
  64. /* Solaris's term.h does horrid things. */
  65. #if (defined(HAVE_TERM_H) && !defined(SUNOS))
  66. #include <term.h>
  67. #endif
  68. #include <sys/types.h>
  69. #include <sys/ioctl.h>
  70. #include "el.h"
  71. /*
  72. * IMPORTANT NOTE: these routines are allowed to look at the current screen
  73. * and the current possition assuming that it is correct. If this is not
  74. * true, then the update will be WRONG! This is (should be) a valid
  75. * assumption...
  76. */
  77. #define TC_BUFSIZE 2048
  78. #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
  79. el->el_term.t_str[a][0] != '\0')
  80. #define Str(a) el->el_term.t_str[a]
  81. #define Val(a) el->el_term.t_val[a]
  82. #ifdef notdef
  83. private const struct {
  84. const char *b_name;
  85. int b_rate;
  86. } baud_rate[] = {
  87. #ifdef B0
  88. { "0", B0 },
  89. #endif
  90. #ifdef B50
  91. { "50", B50 },
  92. #endif
  93. #ifdef B75
  94. { "75", B75 },
  95. #endif
  96. #ifdef B110
  97. { "110", B110 },
  98. #endif
  99. #ifdef B134
  100. { "134", B134 },
  101. #endif
  102. #ifdef B150
  103. { "150", B150 },
  104. #endif
  105. #ifdef B200
  106. { "200", B200 },
  107. #endif
  108. #ifdef B300
  109. { "300", B300 },
  110. #endif
  111. #ifdef B600
  112. { "600", B600 },
  113. #endif
  114. #ifdef B900
  115. { "900", B900 },
  116. #endif
  117. #ifdef B1200
  118. { "1200", B1200 },
  119. #endif
  120. #ifdef B1800
  121. { "1800", B1800 },
  122. #endif
  123. #ifdef B2400
  124. { "2400", B2400 },
  125. #endif
  126. #ifdef B3600
  127. { "3600", B3600 },
  128. #endif
  129. #ifdef B4800
  130. { "4800", B4800 },
  131. #endif
  132. #ifdef B7200
  133. { "7200", B7200 },
  134. #endif
  135. #ifdef B9600
  136. { "9600", B9600 },
  137. #endif
  138. #ifdef EXTA
  139. { "19200", EXTA },
  140. #endif
  141. #ifdef B19200
  142. { "19200", B19200 },
  143. #endif
  144. #ifdef EXTB
  145. { "38400", EXTB },
  146. #endif
  147. #ifdef B38400
  148. { "38400", B38400 },
  149. #endif
  150. { NULL, 0 }
  151. };
  152. #endif
  153. private const struct termcapstr {
  154. const char *name;
  155. const char *long_name;
  156. } tstr[] = {
  157. #define T_al 0
  158. { "al", "add new blank line" },
  159. #define T_bl 1
  160. { "bl", "audible bell" },
  161. #define T_cd 2
  162. { "cd", "clear to bottom" },
  163. #define T_ce 3
  164. { "ce", "clear to end of line" },
  165. #define T_ch 4
  166. { "ch", "cursor to horiz pos" },
  167. #define T_cl 5
  168. { "cl", "clear screen" },
  169. #define T_dc 6
  170. { "dc", "delete a character" },
  171. #define T_dl 7
  172. { "dl", "delete a line" },
  173. #define T_dm 8
  174. { "dm", "start delete mode" },
  175. #define T_ed 9
  176. { "ed", "end delete mode" },
  177. #define T_ei 10
  178. { "ei", "end insert mode" },
  179. #define T_fs 11
  180. { "fs", "cursor from status line" },
  181. #define T_ho 12
  182. { "ho", "home cursor" },
  183. #define T_ic 13
  184. { "ic", "insert character" },
  185. #define T_im 14
  186. { "im", "start insert mode" },
  187. #define T_ip 15
  188. { "ip", "insert padding" },
  189. #define T_kd 16
  190. { "kd", "sends cursor down" },
  191. #define T_kl 17
  192. { "kl", "sends cursor left" },
  193. #define T_kr 18
  194. { "kr", "sends cursor right" },
  195. #define T_ku 19
  196. { "ku", "sends cursor up" },
  197. #define T_md 20
  198. { "md", "begin bold" },
  199. #define T_me 21
  200. { "me", "end attributes" },
  201. #define T_nd 22
  202. { "nd", "non destructive space" },
  203. #define T_se 23
  204. { "se", "end standout" },
  205. #define T_so 24
  206. { "so", "begin standout" },
  207. #define T_ts 25
  208. { "ts", "cursor to status line" },
  209. #define T_up 26
  210. { "up", "cursor up one" },
  211. #define T_us 27
  212. { "us", "begin underline" },
  213. #define T_ue 28
  214. { "ue", "end underline" },
  215. #define T_vb 29
  216. { "vb", "visible bell" },
  217. #define T_DC 30
  218. { "DC", "delete multiple chars" },
  219. #define T_DO 31
  220. { "DO", "cursor down multiple" },
  221. #define T_IC 32
  222. { "IC", "insert multiple chars" },
  223. #define T_LE 33
  224. { "LE", "cursor left multiple" },
  225. #define T_RI 34
  226. { "RI", "cursor right multiple" },
  227. #define T_UP 35
  228. { "UP", "cursor up multiple" },
  229. #define T_kh 36
  230. { "kh", "send cursor home" },
  231. #define T_at7 37
  232. { "@7", "send cursor end" },
  233. #define T_str 38
  234. { NULL, NULL }
  235. };
  236. private const struct termcapval {
  237. const char *name;
  238. const char *long_name;
  239. } tval[] = {
  240. #define T_am 0
  241. { "am", "has automatic margins" },
  242. #define T_pt 1
  243. { "pt", "has physical tabs" },
  244. #define T_li 2
  245. { "li", "Number of lines" },
  246. #define T_co 3
  247. { "co", "Number of columns" },
  248. #define T_km 4
  249. { "km", "Has meta key" },
  250. #define T_xt 5
  251. { "xt", "Tab chars destructive" },
  252. #define T_xn 6
  253. { "xn", "newline ignored at right margin" },
  254. #define T_MT 7
  255. { "MT", "Has meta key" }, /* XXX? */
  256. #define T_val 8
  257. { NULL, NULL, }
  258. };
  259. /* do two or more of the attributes use me */
  260. private void term_setflags(EditLine *);
  261. private int term_rebuffer_display(EditLine *);
  262. private void term_free_display(EditLine *);
  263. private int term_alloc_display(EditLine *);
  264. private void term_alloc(EditLine *, const struct termcapstr *, const char *);
  265. private void term_init_arrow(EditLine *);
  266. private void term_reset_arrow(EditLine *);
  267. private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
  268. /* term_setflags():
  269. * Set the terminal capability flags
  270. */
  271. private void
  272. term_setflags(EditLine *el)
  273. {
  274. EL_FLAGS = 0;
  275. if (el->el_tty.t_tabs)
  276. EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
  277. EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
  278. EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
  279. EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
  280. EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
  281. TERM_CAN_INSERT : 0;
  282. EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
  283. EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
  284. EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
  285. if (GoodStr(T_me) && GoodStr(T_ue))
  286. EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
  287. TERM_CAN_ME : 0;
  288. else
  289. EL_FLAGS &= ~TERM_CAN_ME;
  290. if (GoodStr(T_me) && GoodStr(T_se))
  291. EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
  292. TERM_CAN_ME : 0;
  293. #ifdef DEBUG_SCREEN
  294. if (!EL_CAN_UP) {
  295. (void) fprintf(el->el_errfile,
  296. "WARNING: Your terminal cannot move up.\n");
  297. (void) fprintf(el->el_errfile,
  298. "Editing may be odd for long lines.\n");
  299. }
  300. if (!EL_CAN_CEOL)
  301. (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
  302. if (!EL_CAN_DELETE)
  303. (void) fprintf(el->el_errfile, "no delete char capability.\n");
  304. if (!EL_CAN_INSERT)
  305. (void) fprintf(el->el_errfile, "no insert char capability.\n");
  306. #endif /* DEBUG_SCREEN */
  307. }
  308. /* term_init():
  309. * Initialize the terminal stuff
  310. */
  311. protected int
  312. term_init(EditLine *el)
  313. {
  314. el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
  315. if (el->el_term.t_buf == NULL)
  316. return (-1);
  317. el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
  318. if (el->el_term.t_cap == NULL)
  319. return (-1);
  320. el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
  321. if (el->el_term.t_fkey == NULL)
  322. return (-1);
  323. el->el_term.t_loc = 0;
  324. el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
  325. if (el->el_term.t_str == NULL)
  326. return (-1);
  327. (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
  328. el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
  329. if (el->el_term.t_val == NULL)
  330. return (-1);
  331. (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
  332. term_outfile = el->el_outfile;
  333. (void) term_set(el, NULL);
  334. term_init_arrow(el);
  335. return (0);
  336. }
  337. /* term_end():
  338. * Clean up the terminal stuff
  339. */
  340. protected void
  341. term_end(EditLine *el)
  342. {
  343. el_free((ptr_t) el->el_term.t_buf);
  344. el->el_term.t_buf = NULL;
  345. el_free((ptr_t) el->el_term.t_cap);
  346. el->el_term.t_cap = NULL;
  347. el_free((ptr_t) el->el_term.t_fkey);
  348. el->el_term.t_fkey = NULL;
  349. el->el_term.t_loc = 0;
  350. el_free((ptr_t) el->el_term.t_str);
  351. el->el_term.t_str = NULL;
  352. el_free((ptr_t) el->el_term.t_val);
  353. el->el_term.t_val = NULL;
  354. term_free_display(el);
  355. }
  356. /* term_alloc():
  357. * Maintain a string pool for termcap strings
  358. */
  359. private void
  360. term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
  361. {
  362. char termbuf[TC_BUFSIZE];
  363. int tlen, clen;
  364. char **tlist = el->el_term.t_str;
  365. char **tmp, **str = &tlist[t - tstr];
  366. if (cap == NULL || *cap == '\0') {
  367. *str = NULL;
  368. return;
  369. } else
  370. clen = strlen(cap);
  371. tlen = *str == NULL ? 0 : strlen(*str);
  372. /*
  373. * New string is shorter; no need to allocate space
  374. */
  375. if (clen <= tlen) {
  376. (void) strcpy(*str, cap); /* XXX strcpy is safe */
  377. return;
  378. }
  379. /*
  380. * New string is longer; see if we have enough space to append
  381. */
  382. if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
  383. /* XXX strcpy is safe */
  384. (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
  385. cap);
  386. el->el_term.t_loc += clen + 1; /* one for \0 */
  387. return;
  388. }
  389. /*
  390. * Compact our buffer; no need to check compaction, cause we know it
  391. * fits...
  392. */
  393. tlen = 0;
  394. for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
  395. if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
  396. char *ptr;
  397. for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
  398. continue;
  399. termbuf[tlen++] = '\0';
  400. }
  401. memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
  402. el->el_term.t_loc = tlen;
  403. if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
  404. (void) fprintf(el->el_errfile,
  405. "Out of termcap string space.\n");
  406. return;
  407. }
  408. /* XXX strcpy is safe */
  409. (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
  410. el->el_term.t_loc += clen + 1; /* one for \0 */
  411. return;
  412. }
  413. /* term_rebuffer_display():
  414. * Rebuffer the display after the screen changed size
  415. */
  416. private int
  417. term_rebuffer_display(EditLine *el)
  418. {
  419. coord_t *c = &el->el_term.t_size;
  420. term_free_display(el);
  421. c->h = Val(T_co);
  422. c->v = Val(T_li);
  423. if (term_alloc_display(el) == -1)
  424. return (-1);
  425. return (0);
  426. }
  427. /* term_alloc_display():
  428. * Allocate a new display.
  429. */
  430. private int
  431. term_alloc_display(EditLine *el)
  432. {
  433. int i;
  434. char **b;
  435. coord_t *c = &el->el_term.t_size;
  436. b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
  437. if (b == NULL)
  438. return (-1);
  439. for (i = 0; i < c->v; i++) {
  440. b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
  441. if (b[i] == NULL)
  442. return (-1);
  443. }
  444. b[c->v] = NULL;
  445. el->el_display = b;
  446. b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
  447. if (b == NULL)
  448. return (-1);
  449. for (i = 0; i < c->v; i++) {
  450. b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
  451. if (b[i] == NULL)
  452. return (-1);
  453. }
  454. b[c->v] = NULL;
  455. el->el_vdisplay = b;
  456. return (0);
  457. }
  458. /* term_free_display():
  459. * Free the display buffers
  460. */
  461. private void
  462. term_free_display(EditLine *el)
  463. {
  464. char **b;
  465. char **bufp;
  466. b = el->el_display;
  467. el->el_display = NULL;
  468. if (b != NULL) {
  469. for (bufp = b; *bufp != NULL; bufp++)
  470. el_free((ptr_t) * bufp);
  471. el_free((ptr_t) b);
  472. }
  473. b = el->el_vdisplay;
  474. el->el_vdisplay = NULL;
  475. if (b != NULL) {
  476. for (bufp = b; *bufp != NULL; bufp++)
  477. el_free((ptr_t) * bufp);
  478. el_free((ptr_t) b);
  479. }
  480. }
  481. /* term_move_to_line():
  482. * move to line <where> (first line == 0)
  483. * as efficiently as possible
  484. */
  485. protected void
  486. term_move_to_line(EditLine *el, int where)
  487. {
  488. int del;
  489. if (where == el->el_cursor.v)
  490. return;
  491. if (where > el->el_term.t_size.v) {
  492. #ifdef DEBUG_SCREEN
  493. (void) fprintf(el->el_errfile,
  494. "term_move_to_line: where is ridiculous: %d\r\n", where);
  495. #endif /* DEBUG_SCREEN */
  496. return;
  497. }
  498. if ((del = where - el->el_cursor.v) > 0) {
  499. while (del > 0) {
  500. if (EL_HAS_AUTO_MARGINS &&
  501. el->el_display[el->el_cursor.v][0] != '\0') {
  502. /* move without newline */
  503. term_move_to_char(el, el->el_term.t_size.h - 1);
  504. term_overwrite(el,
  505. &el->el_display[el->el_cursor.v][el->el_cursor.h],
  506. 1);
  507. /* updates Cursor */
  508. del--;
  509. } else {
  510. if ((del > 1) && GoodStr(T_DO)) {
  511. (void) tputs(tgoto(Str(T_DO), del, del),
  512. del, term__putc);
  513. del = 0;
  514. } else {
  515. for (; del > 0; del--)
  516. term__putc('\n');
  517. /* because the \n will become \r\n */
  518. el->el_cursor.h = 0;
  519. }
  520. }
  521. }
  522. } else { /* del < 0 */
  523. if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
  524. (void) tputs(tgoto(Str(T_UP), -del, -del), -del,
  525. term__putc);
  526. else {
  527. if (GoodStr(T_up))
  528. for (; del < 0; del++)
  529. (void) tputs(Str(T_up), 1, term__putc);
  530. }
  531. }
  532. el->el_cursor.v = where;/* now where is here */
  533. }
  534. /* term_move_to_char():
  535. * Move to the character position specified
  536. */
  537. protected void
  538. term_move_to_char(EditLine *el, int where)
  539. {
  540. int del, i;
  541. mc_again:
  542. if (where == el->el_cursor.h)
  543. return;
  544. if (where > el->el_term.t_size.h) {
  545. #ifdef DEBUG_SCREEN
  546. (void) fprintf(el->el_errfile,
  547. "term_move_to_char: where is riduculous: %d\r\n", where);
  548. #endif /* DEBUG_SCREEN */
  549. return;
  550. }
  551. if (!where) { /* if where is first column */
  552. term__putc('\r'); /* do a CR */
  553. el->el_cursor.h = 0;
  554. return;
  555. }
  556. del = where - el->el_cursor.h;
  557. if ((del < -4 || del > 4) && GoodStr(T_ch))
  558. /* go there directly */
  559. (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
  560. else {
  561. if (del > 0) { /* moving forward */
  562. if ((del > 4) && GoodStr(T_RI))
  563. (void) tputs(tgoto(Str(T_RI), del, del),
  564. del, term__putc);
  565. else {
  566. /* if I can do tabs, use them */
  567. if (EL_CAN_TAB) {
  568. if ((el->el_cursor.h & 0370) !=
  569. (where & 0370)) {
  570. /* if not within tab stop */
  571. for (i =
  572. (el->el_cursor.h & 0370);
  573. i < (where & 0370);
  574. i += 8)
  575. term__putc('\t');
  576. /* then tab over */
  577. el->el_cursor.h = where & 0370;
  578. }
  579. }
  580. /*
  581. * it's usually cheaper to just write the
  582. * chars, so we do.
  583. */
  584. /*
  585. * NOTE THAT term_overwrite() WILL CHANGE
  586. * el->el_cursor.h!!!
  587. */
  588. term_overwrite(el,
  589. &el->el_display[el->el_cursor.v][el->el_cursor.h],
  590. where - el->el_cursor.h);
  591. }
  592. } else { /* del < 0 := moving backward */
  593. if ((-del > 4) && GoodStr(T_LE))
  594. (void) tputs(tgoto(Str(T_LE), -del, -del),
  595. -del, term__putc);
  596. else { /* can't go directly there */
  597. /*
  598. * if the "cost" is greater than the "cost"
  599. * from col 0
  600. */
  601. if (EL_CAN_TAB ?
  602. (-del > (((unsigned int) where >> 3) +
  603. (where & 07)))
  604. : (-del > where)) {
  605. term__putc('\r'); /* do a CR */
  606. el->el_cursor.h = 0;
  607. goto mc_again; /* and try again */
  608. }
  609. for (i = 0; i < -del; i++)
  610. term__putc('\b');
  611. }
  612. }
  613. }
  614. el->el_cursor.h = where; /* now where is here */
  615. }
  616. /* term_overwrite():
  617. * Overstrike num characters
  618. */
  619. protected void
  620. term_overwrite(EditLine *el, const char *cp, int n)
  621. {
  622. if (n <= 0)
  623. return; /* catch bugs */
  624. if (n > el->el_term.t_size.h) {
  625. #ifdef DEBUG_SCREEN
  626. (void) fprintf(el->el_errfile,
  627. "term_overwrite: n is riduculous: %d\r\n", n);
  628. #endif /* DEBUG_SCREEN */
  629. return;
  630. }
  631. do {
  632. term__putc(*cp++);
  633. el->el_cursor.h++;
  634. } while (--n);
  635. if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
  636. if (EL_HAS_AUTO_MARGINS) { /* yes */
  637. el->el_cursor.h = 0;
  638. el->el_cursor.v++;
  639. if (EL_HAS_MAGIC_MARGINS) {
  640. /* force the wrap to avoid the "magic"
  641. * situation */
  642. char c;
  643. if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
  644. != '\0')
  645. term_overwrite(el, &c, 1);
  646. else
  647. term__putc(' ');
  648. el->el_cursor.h = 1;
  649. }
  650. } else /* no wrap, but cursor stays on screen */
  651. el->el_cursor.h = el->el_term.t_size.h;
  652. }
  653. }
  654. /* term_deletechars():
  655. * Delete num characters
  656. */
  657. protected void
  658. term_deletechars(EditLine *el, int num)
  659. {
  660. if (num <= 0)
  661. return;
  662. if (!EL_CAN_DELETE) {
  663. #ifdef DEBUG_EDIT
  664. (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
  665. #endif /* DEBUG_EDIT */
  666. return;
  667. }
  668. if (num > el->el_term.t_size.h) {
  669. #ifdef DEBUG_SCREEN
  670. (void) fprintf(el->el_errfile,
  671. "term_deletechars: num is riduculous: %d\r\n", num);
  672. #endif /* DEBUG_SCREEN */
  673. return;
  674. }
  675. if (GoodStr(T_DC)) /* if I have multiple delete */
  676. if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
  677. * expen. */
  678. (void) tputs(tgoto(Str(T_DC), num, num),
  679. num, term__putc);
  680. return;
  681. }
  682. if (GoodStr(T_dm)) /* if I have delete mode */
  683. (void) tputs(Str(T_dm), 1, term__putc);
  684. if (GoodStr(T_dc)) /* else do one at a time */
  685. while (num--)
  686. (void) tputs(Str(T_dc), 1, term__putc);
  687. if (GoodStr(T_ed)) /* if I have delete mode */
  688. (void) tputs(Str(T_ed), 1, term__putc);
  689. }
  690. /* term_insertwrite():
  691. * Puts terminal in insert character mode or inserts num
  692. * characters in the line
  693. */
  694. protected void
  695. term_insertwrite(EditLine *el, char *cp, int num)
  696. {
  697. if (num <= 0)
  698. return;
  699. if (!EL_CAN_INSERT) {
  700. #ifdef DEBUG_EDIT
  701. (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
  702. #endif /* DEBUG_EDIT */
  703. return;
  704. }
  705. if (num > el->el_term.t_size.h) {
  706. #ifdef DEBUG_SCREEN
  707. (void) fprintf(el->el_errfile,
  708. "StartInsert: num is riduculous: %d\r\n", num);
  709. #endif /* DEBUG_SCREEN */
  710. return;
  711. }
  712. if (GoodStr(T_IC)) /* if I have multiple insert */
  713. if ((num > 1) || !GoodStr(T_ic)) {
  714. /* if ic would be more expensive */
  715. (void) tputs(tgoto(Str(T_IC), num, num),
  716. num, term__putc);
  717. term_overwrite(el, cp, num);
  718. /* this updates el_cursor.h */
  719. return;
  720. }
  721. if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
  722. (void) tputs(Str(T_im), 1, term__putc);
  723. el->el_cursor.h += num;
  724. do
  725. term__putc(*cp++);
  726. while (--num);
  727. if (GoodStr(T_ip)) /* have to make num chars insert */
  728. (void) tputs(Str(T_ip), 1, term__putc);
  729. (void) tputs(Str(T_ei), 1, term__putc);
  730. return;
  731. }
  732. do {
  733. if (GoodStr(T_ic)) /* have to make num chars insert */
  734. (void) tputs(Str(T_ic), 1, term__putc);
  735. /* insert a char */
  736. term__putc(*cp++);
  737. el->el_cursor.h++;
  738. if (GoodStr(T_ip)) /* have to make num chars insert */
  739. (void) tputs(Str(T_ip), 1, term__putc);
  740. /* pad the inserted char */
  741. } while (--num);
  742. }
  743. /* term_clear_EOL():
  744. * clear to end of line. There are num characters to clear
  745. */
  746. protected void
  747. term_clear_EOL(EditLine *el, int num)
  748. {
  749. int i;
  750. if (EL_CAN_CEOL && GoodStr(T_ce))
  751. (void) tputs(Str(T_ce), 1, term__putc);
  752. else {
  753. for (i = 0; i < num; i++)
  754. term__putc(' ');
  755. el->el_cursor.h += num; /* have written num spaces */
  756. }
  757. }
  758. /* term_clear_screen():
  759. * Clear the screen
  760. */
  761. protected void
  762. term_clear_screen(EditLine *el)
  763. { /* clear the whole screen and home */
  764. if (GoodStr(T_cl))
  765. /* send the clear screen code */
  766. (void) tputs(Str(T_cl), Val(T_li), term__putc);
  767. else if (GoodStr(T_ho) && GoodStr(T_cd)) {
  768. (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
  769. /* clear to bottom of screen */
  770. (void) tputs(Str(T_cd), Val(T_li), term__putc);
  771. } else {
  772. term__putc('\r');
  773. term__putc('\n');
  774. }
  775. }
  776. /* term_beep():
  777. * Beep the way the terminal wants us
  778. */
  779. protected void
  780. term_beep(EditLine *el)
  781. {
  782. if (GoodStr(T_bl))
  783. /* what termcap says we should use */
  784. (void) tputs(Str(T_bl), 1, term__putc);
  785. else
  786. term__putc('\007'); /* an ASCII bell; ^G */
  787. }
  788. #ifdef notdef
  789. /* term_clear_to_bottom():
  790. * Clear to the bottom of the screen
  791. */
  792. protected void
  793. term_clear_to_bottom(EditLine *el)
  794. {
  795. if (GoodStr(T_cd))
  796. (void) tputs(Str(T_cd), Val(T_li), term__putc);
  797. else if (GoodStr(T_ce))
  798. (void) tputs(Str(T_ce), Val(T_li), term__putc);
  799. }
  800. #endif
  801. /* term_set():
  802. * Read in the terminal capabilities from the requested terminal
  803. */
  804. protected int
  805. term_set(EditLine *el, const char *term)
  806. {
  807. int i;
  808. char buf[TC_BUFSIZE];
  809. char *area;
  810. const struct termcapstr *t;
  811. sigset_t oset, nset;
  812. int lins, cols;
  813. (void) sigemptyset(&nset);
  814. (void) sigaddset(&nset, SIGWINCH);
  815. (void) sigprocmask(SIG_BLOCK, &nset, &oset);
  816. area = buf;
  817. if (term == NULL)
  818. term = getenv("TERM");
  819. if (!term || !term[0])
  820. term = "dumb";
  821. if (strcmp(term, "emacs") == 0)
  822. el->el_flags |= EDIT_DISABLED;
  823. memset(el->el_term.t_cap, 0, TC_BUFSIZE);
  824. i = tgetent(el->el_term.t_cap, term);
  825. if (i <= 0) {
  826. if (i == -1)
  827. (void) fprintf(el->el_errfile,
  828. "Cannot read termcap database;\n");
  829. else if (i == 0)
  830. (void) fprintf(el->el_errfile,
  831. "No entry for terminal type \"%s\";\n", term);
  832. (void) fprintf(el->el_errfile,
  833. "using dumb terminal settings.\n");
  834. Val(T_co) = 80; /* do a dumb terminal */
  835. Val(T_pt) = Val(T_km) = Val(T_li) = 0;
  836. Val(T_xt) = Val(T_MT);
  837. for (t = tstr; t->name != NULL; t++)
  838. term_alloc(el, t, NULL);
  839. } else {
  840. /* auto/magic margins */
  841. Val(T_am) = tgetflag("am");
  842. Val(T_xn) = tgetflag("xn");
  843. /* Can we tab */
  844. Val(T_pt) = tgetflag("pt");
  845. Val(T_xt) = tgetflag("xt");
  846. /* do we have a meta? */
  847. Val(T_km) = tgetflag("km");
  848. Val(T_MT) = tgetflag("MT");
  849. /* Get the size */
  850. Val(T_co) = tgetnum("co");
  851. Val(T_li) = tgetnum("li");
  852. for (t = tstr; t->name != NULL; t++)
  853. term_alloc(el, t, tgetstr((char *)t->name, &area));
  854. }
  855. if (Val(T_co) < 2)
  856. Val(T_co) = 80; /* just in case */
  857. if (Val(T_li) < 1)
  858. Val(T_li) = 24;
  859. el->el_term.t_size.v = Val(T_co);
  860. el->el_term.t_size.h = Val(T_li);
  861. term_setflags(el);
  862. /* get the correct window size */
  863. (void) term_get_size(el, &lins, &cols);
  864. if (term_change_size(el, lins, cols) == -1)
  865. return (-1);
  866. (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  867. term_bind_arrow(el);
  868. return (i <= 0 ? -1 : 0);
  869. }
  870. /* term_get_size():
  871. * Return the new window size in lines and cols, and
  872. * true if the size was changed.
  873. */
  874. protected int
  875. term_get_size(EditLine *el, int *lins, int *cols)
  876. {
  877. *cols = Val(T_co);
  878. *lins = Val(T_li);
  879. #ifdef TIOCGWINSZ
  880. {
  881. struct winsize ws;
  882. if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
  883. if (ws.ws_col)
  884. *cols = ws.ws_col;
  885. if (ws.ws_row)
  886. *lins = ws.ws_row;
  887. }
  888. }
  889. #endif
  890. #ifdef TIOCGSIZE
  891. {
  892. struct ttysize ts;
  893. if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
  894. if (ts.ts_cols)
  895. *cols = ts.ts_cols;
  896. if (ts.ts_lines)
  897. *lins = ts.ts_lines;
  898. }
  899. }
  900. #endif
  901. return (Val(T_co) != *cols || Val(T_li) != *lins);
  902. }
  903. /* term_change_size():
  904. * Change the size of the terminal
  905. */
  906. protected int
  907. term_change_size(EditLine *el, int lins, int cols)
  908. {
  909. /*
  910. * Just in case
  911. */
  912. Val(T_co) = (cols < 2) ? 80 : cols;
  913. Val(T_li) = (lins < 1) ? 24 : lins;
  914. /* re-make display buffers */
  915. if (term_rebuffer_display(el) == -1)
  916. return (-1);
  917. re_clear_display(el);
  918. return (0);
  919. }
  920. /* term_init_arrow():
  921. * Initialize the arrow key bindings from termcap
  922. */
  923. private void
  924. term_init_arrow(EditLine *el)
  925. {
  926. fkey_t *arrow = el->el_term.t_fkey;
  927. arrow[A_K_DN].name = "down";
  928. arrow[A_K_DN].key = T_kd;
  929. arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
  930. arrow[A_K_DN].type = XK_CMD;
  931. arrow[A_K_UP].name = "up";
  932. arrow[A_K_UP].key = T_ku;
  933. arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
  934. arrow[A_K_UP].type = XK_CMD;
  935. arrow[A_K_LT].name = "left";
  936. arrow[A_K_LT].key = T_kl;
  937. arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
  938. arrow[A_K_LT].type = XK_CMD;
  939. arrow[A_K_RT].name = "right";
  940. arrow[A_K_RT].key = T_kr;
  941. arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
  942. arrow[A_K_RT].type = XK_CMD;
  943. arrow[A_K_HO].name = "home";
  944. arrow[A_K_HO].key = T_kh;
  945. arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
  946. arrow[A_K_HO].type = XK_CMD;
  947. arrow[A_K_EN].name = "end";
  948. arrow[A_K_EN].key = T_at7;
  949. arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
  950. arrow[A_K_EN].type = XK_CMD;
  951. }
  952. /* term_reset_arrow():
  953. * Reset arrow key bindings
  954. */
  955. private void
  956. term_reset_arrow(EditLine *el)
  957. {
  958. fkey_t *arrow = el->el_term.t_fkey;
  959. static const char strA[] = {033, '[', 'A', '\0'};
  960. static const char strB[] = {033, '[', 'B', '\0'};
  961. static const char strC[] = {033, '[', 'C', '\0'};
  962. static const char strD[] = {033, '[', 'D', '\0'};
  963. static const char strH[] = {033, '[', 'H', '\0'};
  964. static const char strF[] = {033, '[', 'F', '\0'};
  965. static const char stOA[] = {033, 'O', 'A', '\0'};
  966. static const char stOB[] = {033, 'O', 'B', '\0'};
  967. static const char stOC[] = {033, 'O', 'C', '\0'};
  968. static const char stOD[] = {033, 'O', 'D', '\0'};
  969. static const char stOH[] = {033, 'O', 'H', '\0'};
  970. static const char stOF[] = {033, 'O', 'F', '\0'};
  971. key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
  972. key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
  973. key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
  974. key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
  975. key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
  976. key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
  977. key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
  978. key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
  979. key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
  980. key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
  981. key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
  982. key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
  983. if (el->el_map.type == MAP_VI) {
  984. key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
  985. key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
  986. key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
  987. key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
  988. key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
  989. key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
  990. key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
  991. key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
  992. key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
  993. key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
  994. key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
  995. key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
  996. }
  997. }
  998. /* term_set_arrow():
  999. * Set an arrow key binding
  1000. */
  1001. protected int
  1002. term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
  1003. {
  1004. fkey_t *arrow = el->el_term.t_fkey;
  1005. int i;
  1006. for (i = 0; i < A_K_NKEYS; i++)
  1007. if (strcmp(name, arrow[i].name) == 0) {
  1008. arrow[i].fun = *fun;
  1009. arrow[i].type = type;
  1010. return (0);
  1011. }
  1012. return (-1);
  1013. }
  1014. /* term_clear_arrow():
  1015. * Clear an arrow key binding
  1016. */
  1017. protected int
  1018. term_clear_arrow(EditLine *el, const char *name)
  1019. {
  1020. fkey_t *arrow = el->el_term.t_fkey;
  1021. int i;
  1022. for (i = 0; i < A_K_NKEYS; i++)
  1023. if (strcmp(name, arrow[i].name) == 0) {
  1024. arrow[i].type = XK_NOD;
  1025. return (0);
  1026. }
  1027. return (-1);
  1028. }
  1029. /* term_print_arrow():
  1030. * Print the arrow key bindings
  1031. */
  1032. protected void
  1033. term_print_arrow(EditLine *el, const char *name)
  1034. {
  1035. int i;
  1036. fkey_t *arrow = el->el_term.t_fkey;
  1037. for (i = 0; i < A_K_NKEYS; i++)
  1038. if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
  1039. if (arrow[i].type != XK_NOD)
  1040. key_kprint(el, arrow[i].name, &arrow[i].fun,
  1041. arrow[i].type);
  1042. }
  1043. /* term_bind_arrow():
  1044. * Bind the arrow keys
  1045. */
  1046. protected void
  1047. term_bind_arrow(EditLine *el)
  1048. {
  1049. el_action_t *map;
  1050. const el_action_t *dmap;
  1051. int i, j;
  1052. char *p;
  1053. fkey_t *arrow = el->el_term.t_fkey;
  1054. /* Check if the components needed are initialized */
  1055. if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
  1056. return;
  1057. map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
  1058. dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
  1059. term_reset_arrow(el);
  1060. for (i = 0; i < A_K_NKEYS; i++) {
  1061. p = el->el_term.t_str[arrow[i].key];
  1062. if (p && *p) {
  1063. j = (unsigned char) *p;
  1064. /*
  1065. * Assign the arrow keys only if:
  1066. *
  1067. * 1. They are multi-character arrow keys and the user
  1068. * has not re-assigned the leading character, or
  1069. * has re-assigned the leading character to be
  1070. * ED_SEQUENCE_LEAD_IN
  1071. * 2. They are single arrow keys pointing to an
  1072. * unassigned key.
  1073. */
  1074. if (arrow[i].type == XK_NOD)
  1075. key_clear(el, map, p);
  1076. else {
  1077. if (p[1] && (dmap[j] == map[j] ||
  1078. map[j] == ED_SEQUENCE_LEAD_IN)) {
  1079. key_add(el, p, &arrow[i].fun,
  1080. arrow[i].type);
  1081. map[j] = ED_SEQUENCE_LEAD_IN;
  1082. } else if (map[j] == ED_UNASSIGNED) {
  1083. key_clear(el, map, p);
  1084. if (arrow[i].type == XK_CMD)
  1085. map[j] = arrow[i].fun.cmd;
  1086. else
  1087. key_add(el, p, &arrow[i].fun,
  1088. arrow[i].type);
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. /* term__putc():
  1095. * Add a character
  1096. */
  1097. protected int
  1098. term__putc(int c)
  1099. {
  1100. return (fputc(c, term_outfile));
  1101. }
  1102. /* term__flush():
  1103. * Flush output
  1104. */
  1105. protected void
  1106. term__flush(void)
  1107. {
  1108. (void) fflush(term_outfile);
  1109. }
  1110. /* term_telltc():
  1111. * Print the current termcap characteristics
  1112. */
  1113. protected int
  1114. /*ARGSUSED*/
  1115. term_telltc(EditLine *el, int argc, const char **argv)
  1116. {
  1117. const struct termcapstr *t;
  1118. char **ts;
  1119. char upbuf[EL_BUFSIZ];
  1120. (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
  1121. (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
  1122. (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
  1123. Val(T_co), Val(T_li));
  1124. (void) fprintf(el->el_outfile,
  1125. "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
  1126. (void) fprintf(el->el_outfile,
  1127. "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
  1128. (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
  1129. EL_HAS_AUTO_MARGINS ? "has" : "does not have");
  1130. if (EL_HAS_AUTO_MARGINS)
  1131. (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
  1132. EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
  1133. for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
  1134. (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
  1135. t->long_name,
  1136. t->name, *ts && **ts ?
  1137. key__decode_str(*ts, upbuf, "") : "(empty)");
  1138. (void) fputc('\n', el->el_outfile);
  1139. return (0);
  1140. }
  1141. /* term_settc():
  1142. * Change the current terminal characteristics
  1143. */
  1144. protected int
  1145. /*ARGSUSED*/
  1146. term_settc(EditLine *el, int argc, const char **argv)
  1147. {
  1148. const struct termcapstr *ts;
  1149. const struct termcapval *tv;
  1150. const char *what, *how;
  1151. if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
  1152. return (-1);
  1153. what = argv[1];
  1154. how = argv[2];
  1155. /*
  1156. * Do the strings first
  1157. */
  1158. for (ts = tstr; ts->name != NULL; ts++)
  1159. if (strcmp(ts->name, what) == 0)
  1160. break;
  1161. if (ts->name != NULL) {
  1162. term_alloc(el, ts, how);
  1163. term_setflags(el);
  1164. return (0);
  1165. }
  1166. /*
  1167. * Do the numeric ones second
  1168. */
  1169. for (tv = tval; tv->name != NULL; tv++)
  1170. if (strcmp(tv->name, what) == 0)
  1171. break;
  1172. if (tv->name != NULL) {
  1173. if (tv == &tval[T_pt] || tv == &tval[T_km] ||
  1174. tv == &tval[T_am] || tv == &tval[T_xn]) {
  1175. if (strcmp(how, "yes") == 0)
  1176. el->el_term.t_val[tv - tval] = 1;
  1177. else if (strcmp(how, "no") == 0)
  1178. el->el_term.t_val[tv - tval] = 0;
  1179. else {
  1180. (void) fprintf(el->el_errfile,
  1181. "settc: Bad value `%s'.\n", how);
  1182. return (-1);
  1183. }
  1184. term_setflags(el);
  1185. if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
  1186. return (-1);
  1187. return (0);
  1188. } else {
  1189. long i;
  1190. char *ep;
  1191. i = strtol(how, &ep, 10);
  1192. if (*ep != '\0') {
  1193. (void) fprintf(el->el_errfile,
  1194. "settc: Bad value `%s'.\n", how);
  1195. return (-1);
  1196. }
  1197. el->el_term.t_val[tv - tval] = (int) i;
  1198. el->el_term.t_size.v = Val(T_co);
  1199. el->el_term.t_size.h = Val(T_li);
  1200. if (tv == &tval[T_co] || tv == &tval[T_li])
  1201. if (term_change_size(el, Val(T_li), Val(T_co))
  1202. == -1)
  1203. return (-1);
  1204. return (0);
  1205. }
  1206. }
  1207. return (-1);
  1208. }
  1209. /* term_echotc():
  1210. * Print the termcap string out with variable substitution
  1211. */
  1212. protected int
  1213. /*ARGSUSED*/
  1214. term_echotc(EditLine *el, int argc, const char **argv)
  1215. {
  1216. char *cap, *scap, *ep;
  1217. int arg_need, arg_cols, arg_rows;
  1218. int verbose = 0, silent = 0;
  1219. char *area;
  1220. static const char fmts[] = "%s\n", fmtd[] = "%d\n";
  1221. const struct termcapstr *t;
  1222. char buf[TC_BUFSIZE];
  1223. long i;
  1224. area = buf;
  1225. if (argv == NULL || argv[1] == NULL)
  1226. return (-1);
  1227. argv++;
  1228. if (argv[0][0] == '-') {
  1229. switch (argv[0][1]) {
  1230. case 'v':
  1231. verbose = 1;
  1232. break;
  1233. case 's':
  1234. silent = 1;
  1235. break;
  1236. default:
  1237. /* stderror(ERR_NAME | ERR_TCUSAGE); */
  1238. break;
  1239. }
  1240. argv++;
  1241. }
  1242. if (!*argv || *argv[0] == '\0')
  1243. return (0);
  1244. if (strcmp(*argv, "tabs") == 0) {
  1245. (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
  1246. return (0);
  1247. } else if (strcmp(*argv, "meta") == 0) {
  1248. (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
  1249. return (0);
  1250. } else if (strcmp(*argv, "xn") == 0) {
  1251. (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
  1252. "yes" : "no");
  1253. return (0);
  1254. } else if (strcmp(*argv, "am") == 0) {
  1255. (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
  1256. "yes" : "no");
  1257. return (0);
  1258. } else if (strcmp(*argv, "baud") == 0) {
  1259. #ifdef notdef
  1260. int i;
  1261. for (i = 0; baud_rate[i].b_name != NULL; i++)
  1262. if (el->el_tty.t_speed == baud_rate[i].b_rate) {
  1263. (void) fprintf(el->el_outfile, fmts,
  1264. baud_rate[i].b_name);
  1265. return (0);
  1266. }
  1267. (void) fprintf(el->el_outfile, fmtd, 0);
  1268. #else
  1269. (void) fprintf(el->el_outfile, fmtd, el->el_tty.t_speed);
  1270. #endif
  1271. return (0);
  1272. } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
  1273. (void) fprintf(el->el_outfile, fmtd, Val(T_li));
  1274. return (0);
  1275. } else if (strcmp(*argv, "cols") == 0) {
  1276. (void) fprintf(el->el_outfile, fmtd, Val(T_co));
  1277. return (0);
  1278. }
  1279. /*
  1280. * Try to use our local definition first
  1281. */
  1282. scap = NULL;
  1283. for (t = tstr; t->name != NULL; t++)
  1284. if (strcmp(t->name, *argv) == 0) {
  1285. scap = el->el_term.t_str[t - tstr];
  1286. break;
  1287. }
  1288. if (t->name == NULL)
  1289. scap = tgetstr((char *)argv, &area);
  1290. if (!scap || scap[0] == '\0') {
  1291. if (!silent)
  1292. (void) fprintf(el->el_errfile,
  1293. "echotc: Termcap parameter `%s' not found.\n",
  1294. *argv);
  1295. return (-1);
  1296. }
  1297. /*
  1298. * Count home many values we need for this capability.
  1299. */
  1300. for (cap = scap, arg_need = 0; *cap; cap++)
  1301. if (*cap == '%')
  1302. switch (*++cap) {
  1303. case 'd':
  1304. case '2':
  1305. case '3':
  1306. case '.':
  1307. case '+':
  1308. arg_need++;
  1309. break;
  1310. case '%':
  1311. case '>':
  1312. case 'i':
  1313. case 'r':
  1314. case 'n':
  1315. case 'B':
  1316. case 'D':
  1317. break;
  1318. default:
  1319. /*
  1320. * hpux has lot's of them...
  1321. */
  1322. if (verbose)
  1323. (void) fprintf(el->el_errfile,
  1324. "echotc: Warning: unknown termcap %% `%c'.\n",
  1325. *cap);
  1326. /* This is bad, but I won't complain */
  1327. break;
  1328. }
  1329. switch (arg_need) {
  1330. case 0:
  1331. argv++;
  1332. if (*argv && *argv[0]) {
  1333. if (!silent)
  1334. (void) fprintf(el->el_errfile,
  1335. "echotc: Warning: Extra argument `%s'.\n",
  1336. *argv);
  1337. return (-1);
  1338. }
  1339. (void) tputs(scap, 1, term__putc);
  1340. break;
  1341. case 1:
  1342. argv++;
  1343. if (!*argv || *argv[0] == '\0') {
  1344. if (!silent)
  1345. (void) fprintf(el->el_errfile,
  1346. "echotc: Warning: Missing argument.\n");
  1347. return (-1);
  1348. }
  1349. arg_cols = 0;
  1350. i = strtol(*argv, &ep, 10);
  1351. if (*ep != '\0' || i < 0) {
  1352. if (!silent)
  1353. (void) fprintf(el->el_errfile,
  1354. "echotc: Bad value `%s' for rows.\n",
  1355. *argv);
  1356. return (-1);
  1357. }
  1358. arg_rows = (int) i;
  1359. argv++;
  1360. if (*argv && *argv[0]) {
  1361. if (!silent)
  1362. (void) fprintf(el->el_errfile,
  1363. "echotc: Warning: Extra argument `%s'.\n",
  1364. *argv);
  1365. return (-1);
  1366. }
  1367. (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
  1368. break;
  1369. default:
  1370. /* This is wrong, but I will ignore it... */
  1371. if (verbose)
  1372. (void) fprintf(el->el_errfile,
  1373. "echotc: Warning: Too many required arguments (%d).\n",
  1374. arg_need);
  1375. /* FALLTHROUGH */
  1376. case 2:
  1377. argv++;
  1378. if (!*argv || *argv[0] == '\0') {
  1379. if (!silent)
  1380. (void) fprintf(el->el_errfile,
  1381. "echotc: Warning: Missing argument.\n");
  1382. return (-1);
  1383. }
  1384. i = strtol(*argv, &ep, 10);
  1385. if (*ep != '\0' || i < 0) {
  1386. if (!silent)
  1387. (void) fprintf(el->el_errfile,
  1388. "echotc: Bad value `%s' for cols.\n",
  1389. *argv);
  1390. return (-1);
  1391. }
  1392. arg_cols = (int) i;
  1393. argv++;
  1394. if (!*argv || *argv[0] == '\0') {
  1395. if (!silent)
  1396. (void) fprintf(el->el_errfile,
  1397. "echotc: Warning: Missing argument.\n");
  1398. return (-1);
  1399. }
  1400. i = strtol(*argv, &ep, 10);
  1401. if (*ep != '\0' || i < 0) {
  1402. if (!silent)
  1403. (void) fprintf(el->el_errfile,
  1404. "echotc: Bad value `%s' for rows.\n",
  1405. *argv);
  1406. return (-1);
  1407. }
  1408. arg_rows = (int) i;
  1409. if (*ep != '\0') {
  1410. if (!silent)
  1411. (void) fprintf(el->el_errfile,
  1412. "echotc: Bad value `%s'.\n", *argv);
  1413. return (-1);
  1414. }
  1415. argv++;
  1416. if (*argv && *argv[0]) {
  1417. if (!silent)
  1418. (void) fprintf(el->el_errfile,
  1419. "echotc: Warning: Extra argument `%s'.\n",
  1420. *argv);
  1421. return (-1);
  1422. }
  1423. (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,
  1424. term__putc);
  1425. break;
  1426. }
  1427. return (0);
  1428. }