term.c 38 KB

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