/usr.bin/talk/display.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 186 lines · 120 code · 15 blank · 51 comment · 39 complexity · 6a33adb4048d298ae4f2e23ba176f8ff MD5 · raw file

  1. /*
  2. * Copyright (c) 1983, 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[] = "@(#)display.c 8.1 (Berkeley) 6/6/93";
  33. #endif
  34. /*
  35. * The window 'manager', initializes curses and handles the actual
  36. * displaying of text
  37. */
  38. #include <ctype.h>
  39. #include <unistd.h>
  40. #include "talk.h"
  41. xwin_t my_win;
  42. xwin_t his_win;
  43. WINDOW *line_win;
  44. int curses_initialized = 0;
  45. /*
  46. * max HAS to be a function, it is called with
  47. * an argument of the form --foo at least once.
  48. */
  49. int
  50. max(int a, int b)
  51. {
  52. return (a > b ? a : b);
  53. }
  54. /*
  55. * Display some text on somebody's window, processing some control
  56. * characters while we are at it.
  57. */
  58. void
  59. display(xwin_t *win, char *text, int size)
  60. {
  61. int i;
  62. char cch;
  63. for (i = 0; i < size; i++) {
  64. if (*text == '\n' || *text == '\r') {
  65. waddch(win->x_win, '\n');
  66. getyx(win->x_win, win->x_line, win->x_col);
  67. text++;
  68. continue;
  69. }
  70. if (*text == 004 && win == &my_win) {
  71. /* control-D clears the screen */
  72. werase(my_win.x_win);
  73. getyx(my_win.x_win, my_win.x_line, my_win.x_col);
  74. wrefresh(my_win.x_win);
  75. werase(his_win.x_win);
  76. getyx(his_win.x_win, his_win.x_line, his_win.x_col);
  77. wrefresh(his_win.x_win);
  78. text++;
  79. continue;
  80. }
  81. /* erase character */
  82. if ( *text == win->cerase
  83. || *text == 010 /* BS */
  84. || *text == 0177 /* DEL */
  85. ) {
  86. wmove(win->x_win, win->x_line, max(--win->x_col, 0));
  87. getyx(win->x_win, win->x_line, win->x_col);
  88. waddch(win->x_win, ' ');
  89. wmove(win->x_win, win->x_line, win->x_col);
  90. getyx(win->x_win, win->x_line, win->x_col);
  91. text++;
  92. continue;
  93. }
  94. /*
  95. * On word erase search backwards until we find
  96. * the beginning of a word or the beginning of
  97. * the line.
  98. */
  99. if ( *text == win->werase
  100. || *text == 027 /* ^W */
  101. ) {
  102. int endcol, xcol, ii, c;
  103. endcol = win->x_col;
  104. xcol = endcol - 1;
  105. while (xcol >= 0) {
  106. c = readwin(win->x_win, win->x_line, xcol);
  107. if (c != ' ')
  108. break;
  109. xcol--;
  110. }
  111. while (xcol >= 0) {
  112. c = readwin(win->x_win, win->x_line, xcol);
  113. if (c == ' ')
  114. break;
  115. xcol--;
  116. }
  117. wmove(win->x_win, win->x_line, xcol + 1);
  118. for (ii = xcol + 1; ii < endcol; ii++)
  119. waddch(win->x_win, ' ');
  120. wmove(win->x_win, win->x_line, xcol + 1);
  121. getyx(win->x_win, win->x_line, win->x_col);
  122. text++;
  123. continue;
  124. }
  125. /* line kill */
  126. if ( *text == win->kill
  127. || *text == 025 /* ^U */
  128. ) {
  129. wmove(win->x_win, win->x_line, 0);
  130. wclrtoeol(win->x_win);
  131. getyx(win->x_win, win->x_line, win->x_col);
  132. text++;
  133. continue;
  134. }
  135. if (*text == '\f') {
  136. if (win == &my_win)
  137. wrefresh(curscr);
  138. text++;
  139. continue;
  140. }
  141. if (*text == '\7') {
  142. write(STDOUT_FILENO, text, 1);
  143. text++;
  144. continue;
  145. }
  146. if (!isprint((unsigned char)*text) && *text != '\t') {
  147. waddch(win->x_win, '^');
  148. getyx(win->x_win, win->x_line, win->x_col);
  149. cch = (*text & 63) + 64;
  150. waddch(win->x_win, cch);
  151. } else
  152. waddch(win->x_win, (unsigned char)*text);
  153. getyx(win->x_win, win->x_line, win->x_col);
  154. text++;
  155. }
  156. wrefresh(win->x_win);
  157. }
  158. /*
  159. * Read the character at the indicated position in win
  160. */
  161. int
  162. readwin(WINDOW *win, int line, int col)
  163. {
  164. int oldline, oldcol;
  165. int c;
  166. getyx(win, oldline, oldcol);
  167. wmove(win, line, col);
  168. c = winch(win);
  169. wmove(win, oldline, oldcol);
  170. return (c);
  171. }