filter.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /****************************************************************************
  2. * Copyright (c) 1998-2006,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 <dickey@clark.net> 1998
  30. *
  31. * $Id: filter.c,v 1.12 2008/12/06 21:59:27 tom Exp $
  32. */
  33. #include <test.priv.h>
  34. #if HAVE_FILTER
  35. /*
  36. * An example of the 'filter()' function in ncurses, this program prompts
  37. * for commands and executes them (like a command shell). It illustrates
  38. * how ncurses can be used to implement programs that are not full-screen.
  39. *
  40. * Ncurses differs slightly from SVr4 curses. The latter does not flush its
  41. * state when exiting program mode, so the attributes on the command lines of
  42. * this program 'bleed' onto the executed commands. Rather than use the
  43. * reset_shell_mode() and reset_prog_mode() functions, we could invoke endwin()
  44. * and refresh(), but that does not work any better.
  45. */
  46. static int
  47. new_command(char *buffer, int length, attr_t underline)
  48. {
  49. int code;
  50. attron(A_BOLD);
  51. printw("Command: ");
  52. attron(underline);
  53. code = getnstr(buffer, length);
  54. /*
  55. * If this returns anything except ERR/OK, it would be one of ncurses's
  56. * extensions. Fill the buffer with something harmless that the shell
  57. * will execute as a comment.
  58. */
  59. #ifdef KEY_EVENT
  60. if (code == KEY_EVENT)
  61. strcpy(buffer, "# event!");
  62. #endif
  63. #ifdef KEY_RESIZE
  64. if (code == KEY_RESIZE) {
  65. strcpy(buffer, "# resize!");
  66. getch();
  67. }
  68. #endif
  69. attroff(underline);
  70. attroff(A_BOLD);
  71. printw("\n");
  72. return code;
  73. }
  74. static void
  75. usage(void)
  76. {
  77. static const char *msg[] =
  78. {
  79. "Usage: filter [options]"
  80. ,""
  81. ,"Options:"
  82. ," -i use initscr() rather than newterm()"
  83. };
  84. unsigned n;
  85. for (n = 0; n < SIZEOF(msg); n++)
  86. fprintf(stderr, "%s\n", msg[n]);
  87. ExitProgram(EXIT_FAILURE);
  88. }
  89. int
  90. main(int argc, char *argv[])
  91. {
  92. int ch;
  93. char buffer[80];
  94. attr_t underline;
  95. bool i_option = FALSE;
  96. setlocale(LC_ALL, "");
  97. while ((ch = getopt(argc, argv, "i")) != -1) {
  98. switch (ch) {
  99. case 'i':
  100. i_option = TRUE;
  101. break;
  102. default:
  103. usage();
  104. }
  105. }
  106. printf("starting filter program using %s...\n",
  107. i_option ? "initscr" : "newterm");
  108. filter();
  109. if (i_option) {
  110. initscr();
  111. } else {
  112. (void) newterm((char *) 0, stdout, stdin);
  113. }
  114. cbreak();
  115. keypad(stdscr, TRUE);
  116. if (has_colors()) {
  117. int background = COLOR_BLACK;
  118. start_color();
  119. #if HAVE_USE_DEFAULT_COLORS
  120. if (use_default_colors() != ERR)
  121. background = -1;
  122. #endif
  123. init_pair(1, COLOR_CYAN, background);
  124. underline = COLOR_PAIR(1);
  125. } else {
  126. underline = A_UNDERLINE;
  127. }
  128. while (new_command(buffer, sizeof(buffer) - 1, underline) != ERR
  129. && strlen(buffer) != 0) {
  130. reset_shell_mode();
  131. printf("\n");
  132. fflush(stdout);
  133. system(buffer);
  134. reset_prog_mode();
  135. touchwin(stdscr);
  136. erase();
  137. refresh();
  138. }
  139. printw("done");
  140. refresh();
  141. endwin();
  142. ExitProgram(EXIT_SUCCESS);
  143. }
  144. #else
  145. int
  146. main(void)
  147. {
  148. printf("This program requires the filter function\n");
  149. ExitProgram(EXIT_FAILURE);
  150. }
  151. #endif /* HAVE_FILTER */