/usr.bin/tset/term.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 156 lines · 90 code · 19 blank · 47 comment · 29 complexity · 9626b23856ecbee02aff453bed85159c MD5 · raw file

  1. /*-
  2. * Copyright (c) 1991, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. #ifndef lint
  32. static const char sccsid[] = "@(#)term.c 8.1 (Berkeley) 6/9/93";
  33. #endif
  34. #include <sys/types.h>
  35. #include <err.h>
  36. #include <errno.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <termcap.h>
  41. #include <unistd.h>
  42. #include <ttyent.h>
  43. #include "extern.h"
  44. char tbuf[1024]; /* Termcap entry. */
  45. const char *askuser(const char *);
  46. char *ttys(char *);
  47. /*
  48. * Figure out what kind of terminal we're dealing with, and then read in
  49. * its termcap entry.
  50. */
  51. const char *
  52. get_termcap_entry(char *userarg, char **tcapbufp)
  53. {
  54. struct ttyent *t;
  55. int rval;
  56. char *p, *ttypath;
  57. const char *ttype;
  58. if (userarg) {
  59. ttype = userarg;
  60. goto found;
  61. }
  62. /* Try the environment. */
  63. if ((ttype = getenv("TERM")))
  64. goto map;
  65. /* Try ttyname(3); check for dialup or other mapping. */
  66. if ((ttypath = ttyname(STDERR_FILENO))) {
  67. if ((p = strrchr(ttypath, '/')))
  68. ++p;
  69. else
  70. p = ttypath;
  71. if ((t = getttynam(p))) {
  72. ttype = t->ty_type;
  73. goto map;
  74. }
  75. }
  76. /* If still undefined, use "unknown". */
  77. ttype = "unknown";
  78. map: ttype = mapped(ttype);
  79. /*
  80. * If not a path, remove TERMCAP from the environment so we get a
  81. * real entry from /etc/termcap. This prevents us from being fooled
  82. * by out of date stuff in the environment.
  83. */
  84. found: if ((p = getenv("TERMCAP")) != NULL && *p != '/')
  85. unsetenv("TERMCAP");
  86. /*
  87. * ttype now contains a pointer to the type of the terminal.
  88. * If the first character is '?', ask the user.
  89. */
  90. if (ttype[0] == '?') {
  91. if (ttype[1] != '\0')
  92. ttype = askuser(ttype + 1);
  93. else
  94. ttype = askuser(NULL);
  95. }
  96. /* Find the termcap entry. If it doesn't exist, ask the user. */
  97. while ((rval = tgetent(tbuf, ttype)) == 0) {
  98. warnx("terminal type %s is unknown", ttype);
  99. ttype = askuser(NULL);
  100. }
  101. if (rval == -1)
  102. errx(1, "termcap: %s", strerror(errno ? errno : ENOENT));
  103. *tcapbufp = tbuf;
  104. return (ttype);
  105. }
  106. /* Prompt the user for a terminal type. */
  107. const char *
  108. askuser(const char *dflt)
  109. {
  110. static char answer[256];
  111. char *p;
  112. /* We can get recalled; if so, don't continue uselessly. */
  113. if (feof(stdin) || ferror(stdin)) {
  114. (void)fprintf(stderr, "\n");
  115. exit(1);
  116. }
  117. for (;;) {
  118. if (dflt)
  119. (void)fprintf(stderr, "Terminal type? [%s] ", dflt);
  120. else
  121. (void)fprintf(stderr, "Terminal type? ");
  122. (void)fflush(stderr);
  123. if (fgets(answer, sizeof(answer), stdin) == NULL) {
  124. if (dflt == NULL) {
  125. (void)fprintf(stderr, "\n");
  126. exit(1);
  127. }
  128. return (dflt);
  129. }
  130. if ((p = strchr(answer, '\n')))
  131. *p = '\0';
  132. if (answer[0])
  133. return (answer);
  134. if (dflt != NULL)
  135. return (dflt);
  136. }
  137. }