tset.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /****************************************************************************
  2. * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /****************************************************************************
  29. * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
  30. * and: Eric S. Raymond <esr@snark.thyrsus.com> *
  31. * and: Thomas E. Dickey 1996-on *
  32. ****************************************************************************/
  33. /*
  34. * tset.c - terminal initialization utility
  35. *
  36. * This code was mostly swiped from 4.4BSD tset, with some obsolescent
  37. * cruft removed and substantial portions rewritten. A Regents of the
  38. * University of California copyright applies to some portions of the
  39. * code, and is reproduced below:
  40. */
  41. /*-
  42. * Copyright (c) 1980, 1991, 1993
  43. * The Regents of the University of California. All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms, with or without
  46. * modification, are permitted provided that the following conditions
  47. * are met:
  48. * 1. Redistributions of source code must retain the above copyright
  49. * notice, this list of conditions and the following disclaimer.
  50. * 2. Redistributions in binary form must reproduce the above copyright
  51. * notice, this list of conditions and the following disclaimer in the
  52. * documentation and/or other materials provided with the distribution.
  53. * 3. All advertising materials mentioning features or use of this software
  54. * must display the following acknowledgement:
  55. * This product includes software developed by the University of
  56. * California, Berkeley and its contributors.
  57. * 4. Neither the name of the University nor the names of its contributors
  58. * may be used to endorse or promote products derived from this software
  59. * without specific prior written permission.
  60. *
  61. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  62. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  63. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  64. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  65. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  66. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  67. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  68. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  69. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  70. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  71. * SUCH DAMAGE.
  72. */
  73. #define USE_LIBTINFO
  74. #define __INTERNAL_CAPS_VISIBLE /* we need to see has_hardware_tabs */
  75. #include <progs.priv.h>
  76. #include <errno.h>
  77. #include <stdio.h>
  78. #include <termcap.h>
  79. #include <fcntl.h>
  80. #if HAVE_GETTTYNAM && HAVE_TTYENT_H
  81. #include <ttyent.h>
  82. #endif
  83. #ifdef NeXT
  84. char *ttyname(int fd);
  85. #endif
  86. #if HAVE_SIZECHANGE
  87. # if !defined(sun) || !TERMIOS
  88. # if HAVE_SYS_IOCTL_H
  89. # include <sys/ioctl.h>
  90. # endif
  91. # endif
  92. #endif
  93. #if NEED_PTEM_H
  94. /* they neglected to define struct winsize in termios.h -- it's only
  95. in termio.h */
  96. #include <sys/stream.h>
  97. #include <sys/ptem.h>
  98. #endif
  99. #include <dump_entry.h>
  100. #include <transform.h>
  101. MODULE_ID("$Id: tset.c,v 1.76 2008/10/11 19:26:19 tom Exp $")
  102. /*
  103. * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
  104. * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
  105. */
  106. #ifdef TIOCGSIZE
  107. # define IOCTL_GET_WINSIZE TIOCGSIZE
  108. # define IOCTL_SET_WINSIZE TIOCSSIZE
  109. # define STRUCT_WINSIZE struct ttysize
  110. # define WINSIZE_ROWS(n) n.ts_lines
  111. # define WINSIZE_COLS(n) n.ts_cols
  112. #else
  113. # ifdef TIOCGWINSZ
  114. # define IOCTL_GET_WINSIZE TIOCGWINSZ
  115. # define IOCTL_SET_WINSIZE TIOCSWINSZ
  116. # define STRUCT_WINSIZE struct winsize
  117. # define WINSIZE_ROWS(n) n.ws_row
  118. # define WINSIZE_COLS(n) n.ws_col
  119. # endif
  120. #endif
  121. extern char **environ;
  122. #undef CTRL
  123. #define CTRL(x) ((x) & 0x1f)
  124. const char *_nc_progname = "tset";
  125. static TTY mode, oldmode, original;
  126. static bool opt_c; /* set control-chars */
  127. static bool opt_w; /* set window-size */
  128. static bool can_restore = FALSE;
  129. static bool isreset = FALSE; /* invoked as reset */
  130. static int terasechar = -1; /* new erase character */
  131. static int intrchar = -1; /* new interrupt character */
  132. static int tkillchar = -1; /* new kill character */
  133. static int tlines, tcolumns; /* window size */
  134. #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
  135. static int
  136. CaselessCmp(const char *a, const char *b)
  137. { /* strcasecmp isn't portable */
  138. while (*a && *b) {
  139. int cmp = LOWERCASE(*a) - LOWERCASE(*b);
  140. if (cmp != 0)
  141. break;
  142. a++, b++;
  143. }
  144. return LOWERCASE(*a) - LOWERCASE(*b);
  145. }
  146. static void
  147. exit_error(void)
  148. {
  149. if (can_restore)
  150. SET_TTY(STDERR_FILENO, &original);
  151. (void) fprintf(stderr, "\n");
  152. fflush(stderr);
  153. ExitProgram(EXIT_FAILURE);
  154. /* NOTREACHED */
  155. }
  156. static void
  157. err(const char *fmt,...)
  158. {
  159. va_list ap;
  160. va_start(ap, fmt);
  161. (void) fprintf(stderr, "%s: ", _nc_progname);
  162. (void) vfprintf(stderr, fmt, ap);
  163. va_end(ap);
  164. exit_error();
  165. /* NOTREACHED */
  166. }
  167. static void
  168. failed(const char *msg)
  169. {
  170. char temp[BUFSIZ];
  171. unsigned len = strlen(_nc_progname) + 2;
  172. if ((int) len < (int) sizeof(temp) - 12) {
  173. strcpy(temp, _nc_progname);
  174. strcat(temp, ": ");
  175. } else {
  176. strcpy(temp, "tset: ");
  177. }
  178. perror(strncat(temp, msg, sizeof(temp) - strlen(temp) - 2));
  179. exit_error();
  180. /* NOTREACHED */
  181. }
  182. static void
  183. cat(char *file)
  184. {
  185. FILE *fp;
  186. size_t nr;
  187. char buf[BUFSIZ];
  188. if ((fp = fopen(file, "r")) == 0)
  189. failed(file);
  190. while ((nr = fread(buf, sizeof(char), sizeof(buf), fp)) != 0)
  191. if (fwrite(buf, sizeof(char), nr, stderr) != nr)
  192. failed("write to stderr");
  193. fclose(fp);
  194. }
  195. static int
  196. outc(int c)
  197. {
  198. return putc(c, stderr);
  199. }
  200. /* Prompt the user for a terminal type. */
  201. static const char *
  202. askuser(const char *dflt)
  203. {
  204. static char answer[256];
  205. char *p;
  206. /* We can get recalled; if so, don't continue uselessly. */
  207. clearerr(stdin);
  208. if (feof(stdin) || ferror(stdin)) {
  209. (void) fprintf(stderr, "\n");
  210. exit_error();
  211. /* NOTREACHED */
  212. }
  213. for (;;) {
  214. if (dflt)
  215. (void) fprintf(stderr, "Terminal type? [%s] ", dflt);
  216. else
  217. (void) fprintf(stderr, "Terminal type? ");
  218. (void) fflush(stderr);
  219. if (fgets(answer, sizeof(answer), stdin) == 0) {
  220. if (dflt == 0) {
  221. exit_error();
  222. /* NOTREACHED */
  223. }
  224. return (dflt);
  225. }
  226. if ((p = strchr(answer, '\n')) != 0)
  227. *p = '\0';
  228. if (answer[0])
  229. return (answer);
  230. if (dflt != 0)
  231. return (dflt);
  232. }
  233. }
  234. /**************************************************************************
  235. *
  236. * Mapping logic begins here
  237. *
  238. **************************************************************************/
  239. /* Baud rate conditionals for mapping. */
  240. #define GT 0x01
  241. #define EQ 0x02
  242. #define LT 0x04
  243. #define NOT 0x08
  244. #define GE (GT | EQ)
  245. #define LE (LT | EQ)
  246. typedef struct map {
  247. struct map *next; /* Linked list of maps. */
  248. const char *porttype; /* Port type, or "" for any. */
  249. const char *type; /* Terminal type to select. */
  250. int conditional; /* Baud rate conditionals bitmask. */
  251. int speed; /* Baud rate to compare against. */
  252. } MAP;
  253. static MAP *cur, *maplist;
  254. typedef struct speeds {
  255. const char *string;
  256. int speed;
  257. } SPEEDS;
  258. static const SPEEDS speeds[] =
  259. {
  260. {"0", B0},
  261. {"50", B50},
  262. {"75", B75},
  263. {"110", B110},
  264. {"134", B134},
  265. {"134.5", B134},
  266. {"150", B150},
  267. {"200", B200},
  268. {"300", B300},
  269. {"600", B600},
  270. {"1200", B1200},
  271. {"1800", B1800},
  272. {"2400", B2400},
  273. {"4800", B4800},
  274. {"9600", B9600},
  275. /* sgttyb may define up to this point */
  276. #ifdef B19200
  277. {"19200", B19200},
  278. #endif
  279. #ifdef B38400
  280. {"38400", B38400},
  281. #endif
  282. #ifdef B19200
  283. {"19200", B19200},
  284. #endif
  285. #ifdef B38400
  286. {"38400", B38400},
  287. #endif
  288. #ifdef B19200
  289. {"19200", B19200},
  290. #else
  291. #ifdef EXTA
  292. {"19200", EXTA},
  293. #endif
  294. #endif
  295. #ifdef B38400
  296. {"38400", B38400},
  297. #else
  298. #ifdef EXTB
  299. {"38400", EXTB},
  300. #endif
  301. #endif
  302. #ifdef B57600
  303. {"57600", B57600},
  304. #endif
  305. #ifdef B115200
  306. {"115200", B115200},
  307. #endif
  308. #ifdef B230400
  309. {"230400", B230400},
  310. #endif
  311. #ifdef B460800
  312. {"460800", B460800},
  313. #endif
  314. {(char *) 0, 0}
  315. };
  316. static int
  317. tbaudrate(char *rate)
  318. {
  319. const SPEEDS *sp;
  320. int found = FALSE;
  321. /* The baudrate number can be preceded by a 'B', which is ignored. */
  322. if (*rate == 'B')
  323. ++rate;
  324. for (sp = speeds; sp->string; ++sp) {
  325. if (!CaselessCmp(rate, sp->string)) {
  326. found = TRUE;
  327. break;
  328. }
  329. }
  330. if (!found)
  331. err("unknown baud rate %s", rate);
  332. return (sp->speed);
  333. }
  334. /*
  335. * Syntax for -m:
  336. * [port-type][test baudrate]:terminal-type
  337. * The baud rate tests are: >, <, @, =, !
  338. */
  339. static void
  340. add_mapping(const char *port, char *arg)
  341. {
  342. MAP *mapp;
  343. char *copy, *p;
  344. const char *termp;
  345. char *base = 0;
  346. copy = strdup(arg);
  347. mapp = (MAP *) malloc(sizeof(MAP));
  348. if (copy == 0 || mapp == 0)
  349. failed("malloc");
  350. mapp->next = 0;
  351. if (maplist == 0)
  352. cur = maplist = mapp;
  353. else {
  354. cur->next = mapp;
  355. cur = mapp;
  356. }
  357. mapp->porttype = arg;
  358. mapp->conditional = 0;
  359. arg = strpbrk(arg, "><@=!:");
  360. if (arg == 0) { /* [?]term */
  361. mapp->type = mapp->porttype;
  362. mapp->porttype = 0;
  363. goto done;
  364. }
  365. if (arg == mapp->porttype) /* [><@=! baud]:term */
  366. termp = mapp->porttype = 0;
  367. else
  368. termp = base = arg;
  369. for (;; ++arg) { /* Optional conditionals. */
  370. switch (*arg) {
  371. case '<':
  372. if (mapp->conditional & GT)
  373. goto badmopt;
  374. mapp->conditional |= LT;
  375. break;
  376. case '>':
  377. if (mapp->conditional & LT)
  378. goto badmopt;
  379. mapp->conditional |= GT;
  380. break;
  381. case '@':
  382. case '=': /* Not documented. */
  383. mapp->conditional |= EQ;
  384. break;
  385. case '!':
  386. mapp->conditional |= NOT;
  387. break;
  388. default:
  389. goto next;
  390. }
  391. }
  392. next:
  393. if (*arg == ':') {
  394. if (mapp->conditional)
  395. goto badmopt;
  396. ++arg;
  397. } else { /* Optional baudrate. */
  398. arg = strchr(p = arg, ':');
  399. if (arg == 0)
  400. goto badmopt;
  401. *arg++ = '\0';
  402. mapp->speed = tbaudrate(p);
  403. }
  404. if (arg == (char *) 0) /* Non-optional type. */
  405. goto badmopt;
  406. mapp->type = arg;
  407. /* Terminate porttype, if specified. */
  408. if (termp != 0)
  409. *base = '\0';
  410. /* If a NOT conditional, reverse the test. */
  411. if (mapp->conditional & NOT)
  412. mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
  413. /* If user specified a port with an option flag, set it. */
  414. done:
  415. if (port) {
  416. if (mapp->porttype) {
  417. badmopt:
  418. err("illegal -m option format: %s", copy);
  419. }
  420. mapp->porttype = port;
  421. }
  422. free(copy);
  423. #ifdef MAPDEBUG
  424. (void) printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
  425. (void) printf("type: %s\n", mapp->type);
  426. (void) printf("conditional: ");
  427. p = "";
  428. if (mapp->conditional & GT) {
  429. (void) printf("GT");
  430. p = "/";
  431. }
  432. if (mapp->conditional & EQ) {
  433. (void) printf("%sEQ", p);
  434. p = "/";
  435. }
  436. if (mapp->conditional & LT)
  437. (void) printf("%sLT", p);
  438. (void) printf("\nspeed: %d\n", mapp->speed);
  439. #endif
  440. }
  441. /*
  442. * Return the type of terminal to use for a port of type 'type', as specified
  443. * by the first applicable mapping in 'map'. If no mappings apply, return
  444. * 'type'.
  445. */
  446. static const char *
  447. mapped(const char *type)
  448. {
  449. MAP *mapp;
  450. int match;
  451. for (mapp = maplist; mapp; mapp = mapp->next)
  452. if (mapp->porttype == 0 || !strcmp(mapp->porttype, type)) {
  453. switch (mapp->conditional) {
  454. case 0: /* No test specified. */
  455. match = TRUE;
  456. break;
  457. case EQ:
  458. match = (ospeed == mapp->speed);
  459. break;
  460. case GE:
  461. match = (ospeed >= mapp->speed);
  462. break;
  463. case GT:
  464. match = (ospeed > mapp->speed);
  465. break;
  466. case LE:
  467. match = (ospeed <= mapp->speed);
  468. break;
  469. case LT:
  470. match = (ospeed < mapp->speed);
  471. break;
  472. default:
  473. match = FALSE;
  474. }
  475. if (match)
  476. return (mapp->type);
  477. }
  478. /* No match found; return given type. */
  479. return (type);
  480. }
  481. /**************************************************************************
  482. *
  483. * Entry fetching
  484. *
  485. **************************************************************************/
  486. /*
  487. * Figure out what kind of terminal we're dealing with, and then read in
  488. * its termcap entry.
  489. */
  490. static const char *
  491. get_termcap_entry(char *userarg)
  492. {
  493. int errret;
  494. char *p;
  495. const char *ttype;
  496. #if HAVE_GETTTYNAM
  497. struct ttyent *t;
  498. #else
  499. FILE *fp;
  500. #endif
  501. char *ttypath;
  502. if (userarg) {
  503. ttype = userarg;
  504. goto found;
  505. }
  506. /* Try the environment. */
  507. if ((ttype = getenv("TERM")) != 0)
  508. goto map;
  509. if ((ttypath = ttyname(STDERR_FILENO)) != 0) {
  510. p = _nc_basename(ttypath);
  511. #if HAVE_GETTTYNAM
  512. /*
  513. * We have the 4.3BSD library call getttynam(3); that means
  514. * there's an /etc/ttys to look up device-to-type mappings in.
  515. * Try ttyname(3); check for dialup or other mapping.
  516. */
  517. if ((t = getttynam(p))) {
  518. ttype = t->ty_type;
  519. goto map;
  520. }
  521. #else
  522. if ((fp = fopen("/etc/ttytype", "r")) != 0
  523. || (fp = fopen("/etc/ttys", "r")) != 0) {
  524. char buffer[BUFSIZ];
  525. char *s, *t, *d;
  526. while (fgets(buffer, sizeof(buffer) - 1, fp) != 0) {
  527. for (s = buffer, t = d = 0; *s; s++) {
  528. if (isspace(UChar(*s)))
  529. *s = '\0';
  530. else if (t == 0)
  531. t = s;
  532. else if (d == 0 && s != buffer && s[-1] == '\0')
  533. d = s;
  534. }
  535. if (t != 0 && d != 0 && !strcmp(d, p)) {
  536. ttype = strdup(t);
  537. fclose(fp);
  538. goto map;
  539. }
  540. }
  541. fclose(fp);
  542. }
  543. #endif /* HAVE_GETTTYNAM */
  544. }
  545. /* If still undefined, use "unknown". */
  546. ttype = "unknown";
  547. map:ttype = mapped(ttype);
  548. /*
  549. * If not a path, remove TERMCAP from the environment so we get a
  550. * real entry from /etc/termcap. This prevents us from being fooled
  551. * by out of date stuff in the environment.
  552. */
  553. found:if ((p = getenv("TERMCAP")) != 0 && !_nc_is_abs_path(p)) {
  554. /* 'unsetenv("TERMCAP")' is not portable.
  555. * The 'environ' array is better.
  556. */
  557. int n;
  558. for (n = 0; environ[n] != 0; n++) {
  559. if (!strncmp("TERMCAP=", environ[n], 8)) {
  560. while ((environ[n] = environ[n + 1]) != 0) {
  561. n++;
  562. }
  563. break;
  564. }
  565. }
  566. }
  567. /*
  568. * ttype now contains a pointer to the type of the terminal.
  569. * If the first character is '?', ask the user.
  570. */
  571. if (ttype[0] == '?') {
  572. if (ttype[1] != '\0')
  573. ttype = askuser(ttype + 1);
  574. else
  575. ttype = askuser(0);
  576. }
  577. /* Find the terminfo entry. If it doesn't exist, ask the user. */
  578. while (setupterm((NCURSES_CONST char *) ttype, STDOUT_FILENO, &errret)
  579. != OK) {
  580. if (errret == 0) {
  581. (void) fprintf(stderr, "%s: unknown terminal type %s\n",
  582. _nc_progname, ttype);
  583. ttype = 0;
  584. } else {
  585. (void) fprintf(stderr,
  586. "%s: can't initialize terminal type %s (error %d)\n",
  587. _nc_progname, ttype, errret);
  588. ttype = 0;
  589. }
  590. ttype = askuser(ttype);
  591. }
  592. #if BROKEN_LINKER
  593. tgetflag("am"); /* force lib_termcap.o to be linked for 'ospeed' */
  594. #endif
  595. return (ttype);
  596. }
  597. /**************************************************************************
  598. *
  599. * Mode-setting logic
  600. *
  601. **************************************************************************/
  602. /* some BSD systems have these built in, some systems are missing
  603. * one or more definitions. The safest solution is to override unless the
  604. * commonly-altered ones are defined.
  605. */
  606. #if !(defined(CERASE) && defined(CINTR) && defined(CKILL) && defined(CQUIT))
  607. #undef CEOF
  608. #undef CERASE
  609. #undef CINTR
  610. #undef CKILL
  611. #undef CLNEXT
  612. #undef CRPRNT
  613. #undef CQUIT
  614. #undef CSTART
  615. #undef CSTOP
  616. #undef CSUSP
  617. #endif
  618. /* control-character defaults */
  619. #ifndef CEOF
  620. #define CEOF CTRL('D')
  621. #endif
  622. #ifndef CERASE
  623. #define CERASE CTRL('H')
  624. #endif
  625. #ifndef CINTR
  626. #define CINTR 127 /* ^? */
  627. #endif
  628. #ifndef CKILL
  629. #define CKILL CTRL('U')
  630. #endif
  631. #ifndef CLNEXT
  632. #define CLNEXT CTRL('v')
  633. #endif
  634. #ifndef CRPRNT
  635. #define CRPRNT CTRL('r')
  636. #endif
  637. #ifndef CQUIT
  638. #define CQUIT CTRL('\\')
  639. #endif
  640. #ifndef CSTART
  641. #define CSTART CTRL('Q')
  642. #endif
  643. #ifndef CSTOP
  644. #define CSTOP CTRL('S')
  645. #endif
  646. #ifndef CSUSP
  647. #define CSUSP CTRL('Z')
  648. #endif
  649. #if defined(_POSIX_VDISABLE)
  650. #define DISABLED(val) (((_POSIX_VDISABLE != -1) \
  651. && ((val) == _POSIX_VDISABLE)) \
  652. || ((val) <= 0))
  653. #else
  654. #define DISABLED(val) ((int)(val) <= 0)
  655. #endif
  656. #define CHK(val, dft) (DISABLED(val) ? dft : val)
  657. static bool set_tabs(void);
  658. /*
  659. * Reset the terminal mode bits to a sensible state. Very useful after
  660. * a child program dies in raw mode.
  661. */
  662. static void
  663. reset_mode(void)
  664. {
  665. #ifdef TERMIOS
  666. tcgetattr(STDERR_FILENO, &mode);
  667. #else
  668. stty(STDERR_FILENO, &mode);
  669. #endif
  670. #ifdef TERMIOS
  671. #if defined(VDISCARD) && defined(CDISCARD)
  672. mode.c_cc[VDISCARD] = CHK(mode.c_cc[VDISCARD], CDISCARD);
  673. #endif
  674. mode.c_cc[VEOF] = CHK(mode.c_cc[VEOF], CEOF);
  675. mode.c_cc[VERASE] = CHK(mode.c_cc[VERASE], CERASE);
  676. #if defined(VFLUSH) && defined(CFLUSH)
  677. mode.c_cc[VFLUSH] = CHK(mode.c_cc[VFLUSH], CFLUSH);
  678. #endif
  679. mode.c_cc[VINTR] = CHK(mode.c_cc[VINTR], CINTR);
  680. mode.c_cc[VKILL] = CHK(mode.c_cc[VKILL], CKILL);
  681. #if defined(VLNEXT) && defined(CLNEXT)
  682. mode.c_cc[VLNEXT] = CHK(mode.c_cc[VLNEXT], CLNEXT);
  683. #endif
  684. mode.c_cc[VQUIT] = CHK(mode.c_cc[VQUIT], CQUIT);
  685. #if defined(VREPRINT) && defined(CRPRNT)
  686. mode.c_cc[VREPRINT] = CHK(mode.c_cc[VREPRINT], CRPRNT);
  687. #endif
  688. #if defined(VSTART) && defined(CSTART)
  689. mode.c_cc[VSTART] = CHK(mode.c_cc[VSTART], CSTART);
  690. #endif
  691. #if defined(VSTOP) && defined(CSTOP)
  692. mode.c_cc[VSTOP] = CHK(mode.c_cc[VSTOP], CSTOP);
  693. #endif
  694. #if defined(VSUSP) && defined(CSUSP)
  695. mode.c_cc[VSUSP] = CHK(mode.c_cc[VSUSP], CSUSP);
  696. #endif
  697. #if defined(VWERASE) && defined(CWERASE)
  698. mode.c_cc[VWERASE] = CHK(mode.c_cc[VWERASE], CWERASE);
  699. #endif
  700. mode.c_iflag &= ~(IGNBRK | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
  701. #ifdef IUCLC
  702. | IUCLC
  703. #endif
  704. #ifdef IXANY
  705. | IXANY
  706. #endif
  707. | IXOFF);
  708. mode.c_iflag |= (BRKINT | IGNPAR | ICRNL | IXON
  709. #ifdef IMAXBEL
  710. | IMAXBEL
  711. #endif
  712. );
  713. mode.c_oflag &= ~(0
  714. #ifdef OLCUC
  715. | OLCUC
  716. #endif
  717. #ifdef OCRNL
  718. | OCRNL
  719. #endif
  720. #ifdef ONOCR
  721. | ONOCR
  722. #endif
  723. #ifdef ONLRET
  724. | ONLRET
  725. #endif
  726. #ifdef OFILL
  727. | OFILL
  728. #endif
  729. #ifdef OFDEL
  730. | OFDEL
  731. #endif
  732. #ifdef NLDLY
  733. | NLDLY
  734. #endif
  735. #ifdef CRDLY
  736. | CRDLY
  737. #endif
  738. #ifdef TABDLY
  739. | TABDLY
  740. #endif
  741. #ifdef BSDLY
  742. | BSDLY
  743. #endif
  744. #ifdef VTDLY
  745. | VTDLY
  746. #endif
  747. #ifdef FFDLY
  748. | FFDLY
  749. #endif
  750. );
  751. mode.c_oflag |= (OPOST
  752. #ifdef ONLCR
  753. | ONLCR
  754. #endif
  755. );
  756. mode.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CLOCAL);
  757. mode.c_cflag |= (CS8 | CREAD);
  758. mode.c_lflag &= ~(ECHONL | NOFLSH
  759. #ifdef TOSTOP
  760. | TOSTOP
  761. #endif
  762. #ifdef ECHOPTR
  763. | ECHOPRT
  764. #endif
  765. #ifdef XCASE
  766. | XCASE
  767. #endif
  768. );
  769. mode.c_lflag |= (ISIG | ICANON | ECHO | ECHOE | ECHOK
  770. #ifdef ECHOCTL
  771. | ECHOCTL
  772. #endif
  773. #ifdef ECHOKE
  774. | ECHOKE
  775. #endif
  776. );
  777. #endif
  778. SET_TTY(STDERR_FILENO, &mode);
  779. }
  780. /*
  781. * Returns a "good" value for the erase character. This is loosely based on
  782. * the BSD4.4 logic.
  783. */
  784. #ifdef TERMIOS
  785. static int
  786. default_erase(void)
  787. {
  788. int result;
  789. if (over_strike
  790. && key_backspace != 0
  791. && strlen(key_backspace) == 1)
  792. result = key_backspace[0];
  793. else
  794. result = CERASE;
  795. return result;
  796. }
  797. #endif
  798. /*
  799. * Update the values of the erase, interrupt, and kill characters in 'mode'.
  800. *
  801. * SVr4 tset (e.g., Solaris 2.5) only modifies the intr, quit or erase
  802. * characters if they're unset, or if we specify them as options. This differs
  803. * from BSD 4.4 tset, which always sets erase.
  804. */
  805. static void
  806. set_control_chars(void)
  807. {
  808. #ifdef TERMIOS
  809. if (DISABLED(mode.c_cc[VERASE]) || terasechar >= 0)
  810. mode.c_cc[VERASE] = (terasechar >= 0) ? terasechar : default_erase();
  811. if (DISABLED(mode.c_cc[VINTR]) || intrchar >= 0)
  812. mode.c_cc[VINTR] = (intrchar >= 0) ? intrchar : CINTR;
  813. if (DISABLED(mode.c_cc[VKILL]) || tkillchar >= 0)
  814. mode.c_cc[VKILL] = (tkillchar >= 0) ? tkillchar : CKILL;
  815. #endif
  816. }
  817. /*
  818. * Set up various conversions in 'mode', including parity, tabs, returns,
  819. * echo, and case, according to the termcap entry. If the program we're
  820. * running was named with a leading upper-case character, map external
  821. * uppercase to internal lowercase.
  822. */
  823. static void
  824. set_conversions(void)
  825. {
  826. #ifdef __OBSOLETE__
  827. /*
  828. * Conversion logic for some *really* ancient terminal glitches,
  829. * not supported in terminfo. Left here for succeeding generations
  830. * to marvel at.
  831. */
  832. if (tgetflag("UC")) {
  833. #ifdef IUCLC
  834. mode.c_iflag |= IUCLC;
  835. mode.c_oflag |= OLCUC;
  836. #endif
  837. } else if (tgetflag("LC")) {
  838. #ifdef IUCLC
  839. mode.c_iflag &= ~IUCLC;
  840. mode.c_oflag &= ~OLCUC;
  841. #endif
  842. }
  843. mode.c_iflag &= ~(PARMRK | INPCK);
  844. mode.c_lflag |= ICANON;
  845. if (tgetflag("EP")) {
  846. mode.c_cflag |= PARENB;
  847. mode.c_cflag &= ~PARODD;
  848. }
  849. if (tgetflag("OP")) {
  850. mode.c_cflag |= PARENB;
  851. mode.c_cflag |= PARODD;
  852. }
  853. #endif /* __OBSOLETE__ */
  854. #ifdef TERMIOS
  855. #ifdef ONLCR
  856. mode.c_oflag |= ONLCR;
  857. #endif
  858. mode.c_iflag |= ICRNL;
  859. mode.c_lflag |= ECHO;
  860. #ifdef OXTABS
  861. mode.c_oflag |= OXTABS;
  862. #endif /* OXTABS */
  863. /* test used to be tgetflag("NL") */
  864. if (newline != (char *) 0 && newline[0] == '\n' && !newline[1]) {
  865. /* Newline, not linefeed. */
  866. #ifdef ONLCR
  867. mode.c_oflag &= ~ONLCR;
  868. #endif
  869. mode.c_iflag &= ~ICRNL;
  870. }
  871. #ifdef __OBSOLETE__
  872. if (tgetflag("HD")) /* Half duplex. */
  873. mode.c_lflag &= ~ECHO;
  874. #endif /* __OBSOLETE__ */
  875. #ifdef OXTABS
  876. /* test used to be tgetflag("pt") */
  877. if (has_hardware_tabs) /* Print tabs. */
  878. mode.c_oflag &= ~OXTABS;
  879. #endif /* OXTABS */
  880. mode.c_lflag |= (ECHOE | ECHOK);
  881. #endif
  882. }
  883. /* Output startup string. */
  884. static void
  885. set_init(void)
  886. {
  887. char *p;
  888. bool settle;
  889. #ifdef __OBSOLETE__
  890. if (pad_char != (char *) 0) /* Get/set pad character. */
  891. PC = pad_char[0];
  892. #endif /* OBSOLETE */
  893. #ifdef TAB3
  894. if (oldmode.c_oflag & (TAB3 | ONLCR | OCRNL | ONLRET)) {
  895. oldmode.c_oflag &= (TAB3 | ONLCR | OCRNL | ONLRET);
  896. SET_TTY(STDERR_FILENO, &oldmode);
  897. }
  898. #endif
  899. settle = set_tabs();
  900. if (isreset) {
  901. if ((p = reset_1string) != 0) {
  902. tputs(p, 0, outc);
  903. settle = TRUE;
  904. }
  905. if ((p = reset_2string) != 0) {
  906. tputs(p, 0, outc);
  907. settle = TRUE;
  908. }
  909. /* What about rf, rs3, as per terminfo man page? */
  910. /* also might be nice to send rmacs, rmul, rmm */
  911. if ((p = reset_file) != 0
  912. || (p = init_file) != 0) {
  913. cat(p);
  914. settle = TRUE;
  915. }
  916. }
  917. if (settle) {
  918. (void) putc('\r', stderr);
  919. (void) fflush(stderr);
  920. (void) napms(1000); /* Settle the terminal. */
  921. }
  922. }
  923. /*
  924. * Set the hardware tabs on the terminal, using the ct (clear all tabs),
  925. * st (set one tab) and ch (horizontal cursor addressing) capabilities.
  926. * This is done before if and is, so they can patch in case we blow this.
  927. * Return TRUE if we set any tab stops, FALSE if not.
  928. */
  929. static bool
  930. set_tabs(void)
  931. {
  932. if (set_tab && clear_all_tabs) {
  933. int c;
  934. (void) putc('\r', stderr); /* Force to left margin. */
  935. tputs(clear_all_tabs, 0, outc);
  936. for (c = 8; c < tcolumns; c += 8) {
  937. /* Get to the right column. In BSD tset, this
  938. * used to try a bunch of half-clever things
  939. * with cup and hpa, for an average saving of
  940. * somewhat less than two character times per
  941. * tab stop, less than .01 sec at 2400cps. We
  942. * lost all this cruft because it seemed to be
  943. * introducing some odd bugs.
  944. * -----------12345678----------- */
  945. (void) fputs(" ", stderr);
  946. tputs(set_tab, 0, outc);
  947. }
  948. putc('\r', stderr);
  949. return (TRUE);
  950. }
  951. return (FALSE);
  952. }
  953. /**************************************************************************
  954. *
  955. * Main sequence
  956. *
  957. **************************************************************************/
  958. /*
  959. * Tell the user if a control key has been changed from the default value.
  960. */
  961. #ifdef TERMIOS
  962. static void
  963. report(const char *name, int which, unsigned def)
  964. {
  965. unsigned older, newer;
  966. char *p;
  967. newer = mode.c_cc[which];
  968. older = oldmode.c_cc[which];
  969. if (older == newer && older == def)
  970. return;
  971. (void) fprintf(stderr, "%s %s ", name, older == newer ? "is" : "set to");
  972. if (DISABLED(newer))
  973. (void) fprintf(stderr, "undef.\n");
  974. /*
  975. * Check 'delete' before 'backspace', since the key_backspace value
  976. * is ambiguous.
  977. */
  978. else if (newer == 0177)
  979. (void) fprintf(stderr, "delete.\n");
  980. else if ((p = key_backspace) != 0
  981. && newer == (unsigned char) p[0]
  982. && p[1] == '\0')
  983. (void) fprintf(stderr, "backspace.\n");
  984. else if (newer < 040) {
  985. newer ^= 0100;
  986. (void) fprintf(stderr, "control-%c (^%c).\n", UChar(newer), UChar(newer));
  987. } else
  988. (void) fprintf(stderr, "%c.\n", UChar(newer));
  989. }
  990. #endif
  991. /*
  992. * Convert the obsolete argument forms into something that getopt can handle.
  993. * This means that -e, -i and -k get default arguments supplied for them.
  994. */
  995. static void
  996. obsolete(char **argv)
  997. {
  998. for (; *argv; ++argv) {
  999. char *parm = argv[0];
  1000. if (parm[0] == '-' && parm[1] == '\0') {
  1001. argv[0] = strdup("-q");
  1002. continue;
  1003. }
  1004. if ((parm[0] != '-')
  1005. || (argv[1] && argv[1][0] != '-')
  1006. || (parm[1] != 'e' && parm[1] != 'i' && parm[1] != 'k')
  1007. || (parm[2] != '\0'))
  1008. continue;
  1009. switch (argv[0][1]) {
  1010. case 'e':
  1011. argv[0] = strdup("-e^H");
  1012. break;
  1013. case 'i':
  1014. argv[0] = strdup("-i^C");
  1015. break;
  1016. case 'k':
  1017. argv[0] = strdup("-k^U");
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. static void
  1023. usage(void)
  1024. {
  1025. static const char *tbl[] =
  1026. {
  1027. ""
  1028. ,"Options:"
  1029. ," -c set control characters"
  1030. ," -e ch erase character"
  1031. ," -I no initialization strings"
  1032. ," -i ch interrupt character"
  1033. ," -k ch kill character"
  1034. ," -m mapping map identifier to type"
  1035. ," -Q do not output control key settings"
  1036. ," -r display term on stderr"
  1037. ," -s output TERM set command"
  1038. ," -V print curses-version"
  1039. ," -w set window-size"
  1040. };
  1041. unsigned n;
  1042. (void) fprintf(stderr, "Usage: %s [options] [terminal]\n", _nc_progname);
  1043. for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); ++n)
  1044. fprintf(stderr, "%s\n", tbl[n]);
  1045. exit_error();
  1046. /* NOTREACHED */
  1047. }
  1048. static char
  1049. arg_to_char(void)
  1050. {
  1051. return (char) ((optarg[0] == '^' && optarg[1] != '\0')
  1052. ? ((optarg[1] == '?') ? '\177' : CTRL(optarg[1]))
  1053. : optarg[0]);
  1054. }
  1055. int
  1056. main(int argc, char **argv)
  1057. {
  1058. int ch, noinit, noset, quiet, Sflag, sflag, showterm;
  1059. const char *p;
  1060. const char *ttype;
  1061. obsolete(argv);
  1062. noinit = noset = quiet = Sflag = sflag = showterm = 0;
  1063. while ((ch = getopt(argc, argv, "a:cd:e:Ii:k:m:np:qQSrsVw")) != -1) {
  1064. switch (ch) {
  1065. case 'c': /* set control-chars */
  1066. opt_c = TRUE;
  1067. break;
  1068. case 'a': /* OBSOLETE: map identifier to type */
  1069. add_mapping("arpanet", optarg);
  1070. break;
  1071. case 'd': /* OBSOLETE: map identifier to type */
  1072. add_mapping("dialup", optarg);
  1073. break;
  1074. case 'e': /* erase character */
  1075. terasechar = arg_to_char();
  1076. break;
  1077. case 'I': /* no initialization strings */
  1078. noinit = 1;
  1079. break;
  1080. case 'i': /* interrupt character */
  1081. intrchar = arg_to_char();
  1082. break;
  1083. case 'k': /* kill character */
  1084. tkillchar = arg_to_char();
  1085. break;
  1086. case 'm': /* map identifier to type */
  1087. add_mapping(0, optarg);
  1088. break;
  1089. case 'n': /* OBSOLETE: set new tty driver */
  1090. break;
  1091. case 'p': /* OBSOLETE: map identifier to type */
  1092. add_mapping("plugboard", optarg);
  1093. break;
  1094. case 'Q': /* don't output control key settings */
  1095. quiet = 1;
  1096. break;
  1097. case 'q': /* display term only */
  1098. noset = 1;
  1099. break;
  1100. case 'r': /* display term on stderr */
  1101. showterm = 1;
  1102. break;
  1103. case 'S': /* OBSOLETE: output TERM & TERMCAP */
  1104. Sflag = 1;
  1105. break;
  1106. case 's': /* output TERM set command */
  1107. sflag = 1;
  1108. break;
  1109. case 'V': /* print curses-version */
  1110. puts(curses_version());
  1111. ExitProgram(EXIT_SUCCESS);
  1112. case 'w': /* set window-size */
  1113. opt_w = TRUE;
  1114. break;
  1115. case '?':
  1116. default:
  1117. usage();
  1118. }
  1119. }
  1120. _nc_progname = _nc_rootname(*argv);
  1121. argc -= optind;
  1122. argv += optind;
  1123. if (argc > 1)
  1124. usage();
  1125. if (!opt_c && !opt_w)
  1126. opt_c = opt_w = TRUE;
  1127. if (GET_TTY(STDERR_FILENO, &mode) < 0)
  1128. failed("standard error");
  1129. can_restore = TRUE;
  1130. original = oldmode = mode;
  1131. #ifdef TERMIOS
  1132. ospeed = (NCURSES_OSPEED) cfgetospeed(&mode);
  1133. #else
  1134. ospeed = (NCURSES_OSPEED) mode.sg_ospeed;
  1135. #endif
  1136. if (!strcmp(_nc_progname, PROG_RESET)) {
  1137. isreset = TRUE;
  1138. reset_mode();
  1139. }
  1140. ttype = get_termcap_entry(*argv);
  1141. if (!noset) {
  1142. tcolumns = columns;
  1143. tlines = lines;
  1144. #if HAVE_SIZECHANGE
  1145. if (opt_w) {
  1146. STRUCT_WINSIZE win;
  1147. /* Set window size if not set already */
  1148. (void) ioctl(STDERR_FILENO, IOCTL_GET_WINSIZE, &win);
  1149. if (WINSIZE_ROWS(win) == 0 &&
  1150. WINSIZE_COLS(win) == 0 &&
  1151. tlines > 0 && tcolumns > 0) {
  1152. WINSIZE_ROWS(win) = tlines;
  1153. WINSIZE_COLS(win) = tcolumns;
  1154. (void) ioctl(STDERR_FILENO, IOCTL_SET_WINSIZE, &win);
  1155. }
  1156. }
  1157. #endif
  1158. if (opt_c) {
  1159. set_control_chars();
  1160. set_conversions();
  1161. if (!noinit)
  1162. set_init();
  1163. /* Set the modes if they've changed. */
  1164. if (memcmp(&mode, &oldmode, sizeof(mode))) {
  1165. SET_TTY(STDERR_FILENO, &mode);
  1166. }
  1167. }
  1168. }
  1169. /* Get the terminal name from the entry. */
  1170. ttype = _nc_first_name(cur_term->type.term_names);
  1171. if (noset)
  1172. (void) printf("%s\n", ttype);
  1173. else {
  1174. if (showterm)
  1175. (void) fprintf(stderr, "Terminal type is %s.\n", ttype);
  1176. /*
  1177. * If erase, kill and interrupt characters could have been
  1178. * modified and not -Q, display the changes.
  1179. */
  1180. #ifdef TERMIOS
  1181. if (!quiet) {
  1182. report("Erase", VERASE, CERASE);
  1183. report("Kill", VKILL, CKILL);
  1184. report("Interrupt", VINTR, CINTR);
  1185. }
  1186. #endif
  1187. }
  1188. if (Sflag)
  1189. err("The -S option is not supported under terminfo.");
  1190. if (sflag) {
  1191. int len;
  1192. char *var;
  1193. char *leaf;
  1194. /*
  1195. * Figure out what shell we're using. A hack, we look for an
  1196. * environmental variable SHELL ending in "csh".
  1197. */
  1198. if ((var = getenv("SHELL")) != 0
  1199. && ((len = (int) strlen(leaf = _nc_basename(var))) >= 3)
  1200. && !strcmp(leaf + len - 3, "csh"))
  1201. p = "set noglob;\nsetenv TERM %s;\nunset noglob;\n";
  1202. else
  1203. p = "TERM=%s;\n";
  1204. (void) printf(p, ttype);
  1205. }
  1206. ExitProgram(EXIT_SUCCESS);
  1207. }