demo_termcap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /****************************************************************************
  2. * Copyright (c) 2005-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: Thomas E. Dickey
  30. *
  31. * $Id: demo_termcap.c,v 1.7 2008/02/09 18:08:36 tom Exp $
  32. *
  33. * A simple demo of the termcap interface.
  34. */
  35. #define USE_TINFO
  36. #include <test.priv.h>
  37. #if HAVE_TGETENT
  38. #define isCapName(c) (isgraph(c) && strchr("^#=:\\", c) == 0)
  39. static void
  40. dumpit(char *cap)
  41. {
  42. /*
  43. * One of the limitations of the termcap interface is that the library
  44. * cannot determine the size of the buffer passed via tgetstr(), nor the
  45. * amount of space remaining. This demo simply reuses the whole buffer
  46. * for each call; a normal termcap application would try to use the buffer
  47. * to hold all of the strings extracted from the terminal entry.
  48. */
  49. char area[1024], *ap = area;
  50. char *str;
  51. int num;
  52. if ((str = tgetstr(cap, &ap)) != 0) {
  53. /*
  54. * Note that the strings returned are mostly terminfo format, since
  55. * ncurses does not convert except for a handful of special cases.
  56. */
  57. printf("str %s = ", cap);
  58. while (*str != 0) {
  59. int ch = UChar(*str++);
  60. switch (ch) {
  61. case '\177':
  62. fputs("^?", stdout);
  63. break;
  64. case '\033':
  65. fputs("\\E", stdout);
  66. break;
  67. case '\b':
  68. fputs("\\b", stdout);
  69. break;
  70. case '\f':
  71. fputs("\\f", stdout);
  72. break;
  73. case '\n':
  74. fputs("\\n", stdout);
  75. break;
  76. case '\r':
  77. fputs("\\r", stdout);
  78. break;
  79. case ' ':
  80. fputs("\\s", stdout);
  81. break;
  82. case '\t':
  83. fputs("\\t", stdout);
  84. break;
  85. case '^':
  86. fputs("\\^", stdout);
  87. break;
  88. case ':':
  89. fputs("\\072", stdout);
  90. break;
  91. case '\\':
  92. fputs("\\\\", stdout);
  93. break;
  94. default:
  95. if (isgraph(ch))
  96. fputc(ch, stdout);
  97. else if (ch < 32)
  98. printf("^%c", ch + '@');
  99. else
  100. printf("\\%03o", ch);
  101. break;
  102. }
  103. }
  104. printf("\n");
  105. } else if ((num = tgetnum(cap)) >= 0) {
  106. printf("num %s = %d\n", cap, num);
  107. } else if ((num = tgetflag(cap)) > 0) {
  108. printf("flg %s\n", cap);
  109. }
  110. fflush(stdout);
  111. }
  112. static void
  113. demo_termcap(char *name)
  114. {
  115. char buffer[1024];
  116. printf("Terminal type %s\n", name);
  117. if (tgetent(buffer, name) >= 0) {
  118. char cap[3];
  119. int c1, c2;
  120. cap[2] = 0;
  121. for (c1 = 0; c1 < 256; ++c1) {
  122. cap[0] = c1;
  123. if (isCapName(c1)) {
  124. for (c2 = 0; c2 < 256; ++c2) {
  125. cap[1] = c2;
  126. if (isCapName(c2)) {
  127. dumpit(cap);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. int
  135. main(int argc, char *argv[])
  136. {
  137. int n;
  138. char *name;
  139. if (argc > 1) {
  140. for (n = 1; n < argc; ++n) {
  141. demo_termcap(argv[n]);
  142. }
  143. } else if ((name = getenv("TERM")) != 0) {
  144. demo_termcap(name);
  145. } else {
  146. static char dumb[] = "dumb";
  147. demo_termcap(dumb);
  148. }
  149. ExitProgram(EXIT_SUCCESS);
  150. }
  151. #else
  152. int
  153. main(int argc GCC_UNUSED,
  154. char *argv[]GCC_UNUSED)
  155. {
  156. printf("This program requires termcap\n");
  157. exit(EXIT_FAILURE);
  158. }
  159. #endif