home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume3 / miles / part02 / io.c < prev    next >
C/C++ Source or Header  |  1988-01-25  |  13KB  |  476 lines

  1. /* io.c */
  2. /* machine dependent */
  3.  
  4. /**********************************************************************/
  5. /*                                                                    */
  6. /*           MM   MM  IIIIIII  L        L        EEEEEEE              */
  7. /*           M M M M     I     L        L        E                    */
  8. /*           M  M  M     I     L        L        EEEE                 */
  9. /*           M     M     I     L        L        E                    */
  10. /*           M     M  IIIIIII  LLLLLLL  LLLLLLL  EEEEEEE              */
  11. /*                                                                    */
  12. /*      BBBBBB    OOOOO   RRRRRR   NN    N  EEEEEEE   SSSSSS          */
  13. /*      B     B  O     O  R     R  N N   N  E        S                */
  14. /*      BBBBBB   O     O  RRRRRR   N  N  N  EEEEE     SSSSS           */
  15. /*      B     B  O     O  R    R   N   N N  E              S          */
  16. /*      BBBBBB    OOOOO   R     R  N    NN  EEEEEEE  SSSSSS           */
  17. /*                                                                    */
  18. /*                                                                    */
  19. /* Creation: Edmond Dujardin                                          */
  20. /*           (c) 1962 Parker Brothers, Inc.                           */
  21. /*                                                                    */
  22. /* Written by: Brett K. Carver                                        */
  23. /*             Hewlett-Packard, 1983.                                 */
  24. /*                                                                    */
  25. /* Copyright: (c) Brett K. Carver, Hewlett-Packard, 1986.             */
  26. /*                                                                    */
  27. /**********************************************************************/
  28. /*                                                                    */
  29. /* WARNING                                                            */
  30. /*                                                                    */
  31. /* The following file may be offensive to UNIX purists. The original  */
  32. /* implementation was created on a non-UNIX machine that did not have */
  33. /* any of the standard I/O facilities. Many of the shortcomings of    */
  34. /* its previous life live on in this ported version.                  */
  35. /*                                                                    */
  36. /* You have been warned...                                            */
  37. /*                                                                    */
  38. /**********************************************************************/
  39.  
  40. #include <curses.h>
  41. #include <signal.h>
  42. #include "miles.h"
  43.  
  44. /**********************************************************************/
  45. /*                                                                    */
  46. /*              CONSTANTS AND VARIABLES                               */
  47. /*                                                                    */
  48. /**********************************************************************/
  49.  
  50. /**********************************/
  51. /* external procedure definitions */
  52. /**********************************/
  53.  
  54. extern display_pick();
  55. extern random();
  56. extern refresh_screen();
  57.  
  58. /*********************************/
  59. /* external variable definitions */
  60. /*********************************/
  61.  
  62. extern int play;                /* play=TRUE, discard=FALSE, drawn=-1 */
  63. extern int extension;           /* boolean for extension */
  64. extern int debug;               /* flag for debug output */
  65. extern char * temp_screen;
  66.  
  67. extern char *T_extension_yes;
  68. extern char *T_extension_no;
  69. extern char *T_another_yes;
  70. extern char *T_another_no;
  71. extern char *T_match_yes;
  72. extern char *T_match_no;
  73. extern char *T_anychar;
  74. extern char *T_version;
  75. extern char *B_intro[];
  76.  
  77. /**********************************************************************/
  78. /*                                                                    */
  79. /*              DISPLAY AND FORMATTING UTILITIES                      */
  80. /*                                                                    */
  81. /**********************************************************************/
  82.  
  83. /*************************/
  84. /* cleanup the screen io */
  85. /*************************/
  86. cleanup_io()
  87. {
  88. clear();
  89. refresh();
  90. endwin();
  91. exit(0);
  92. }
  93.  
  94. /****************************/
  95. /* initialize the screen io */
  96. /****************************/
  97. initialize_io()
  98. {
  99. signal(SIGINT,cleanup_io);
  100. signal(SIGQUIT,cleanup_io);
  101. signal(SIGTERM,cleanup_io);
  102. initscr();
  103. noecho();
  104. cbreak();
  105. }
  106.  
  107. /************************/
  108. /* write out the screen */
  109. /************************/
  110. write_screen()
  111. {
  112. refresh();
  113. }
  114.  
  115. /***************************/
  116. /* write out screen banner */
  117. /***************************/
  118. display_banner()
  119. {
  120. char **s;
  121. int i;
  122. clear();
  123. s = B_intro;
  124. move(0,0);
  125. i = 0;
  126. while (*s) {
  127.     printw("%s\n",*s++);
  128.     i++;
  129.     if (i>22) {
  130.          move(23,0);
  131.          printw("%s",T_anychar);
  132.          refresh();
  133.          if (getch() == '\n') {
  134.               clear();
  135.               return;
  136.               }
  137.          clear();
  138.          move(0,0);
  139.          i = 0;
  140.          }
  141.     }
  142. move(23,0);
  143. printw("%s",T_anychar);
  144. refresh();
  145. getch();
  146. clear();
  147. }
  148.  
  149. /***************************************/
  150. /* blank screen from location thru end */
  151. /***************************************/
  152. clear_screen(row,column)
  153. int row;
  154. int column;
  155. {
  156. move(row,column);
  157. clrtobot();
  158. }
  159.  
  160. /************************************/
  161. /* write out a status/error message */
  162. /************************************/
  163. message (string,status)
  164. char * string;
  165. int status;
  166. {
  167. move(23,0);
  168. clrtoeol();
  169. move(23,0);
  170. if (status) {
  171.          addstr(string);
  172.          }
  173.     else {
  174.          addstr(string);
  175.          beep();
  176.          }
  177. refresh();
  178. }
  179.  
  180. /*******************************************************/
  181. /* interface to formatter                              */
  182. /* formats value to loaction pointed to by destination */
  183. /*******************************************************/
  184. format (value,row,column)
  185. int value ;
  186. int row;
  187. int column;
  188. {
  189. move(row,column);
  190. printw("%d  ",value);
  191. }
  192.  
  193. /*****************************************/
  194. /* places the string pointed to in a     */
  195. /* length character field, blank filling */
  196. /*****************************************/
  197. place_string(string,row,column,length)
  198. char *string;
  199. int row;
  200. int column;
  201. int length;
  202. {
  203. int i;
  204. char *temp;
  205. temp = temp_screen;
  206. for (i=0; i<length; i++)
  207.     *temp++ = ' ';
  208. *temp = '\0';
  209. move(row,column);
  210. addstr(temp_screen);
  211. move(row,column);
  212. addstr(string);
  213. }
  214.  
  215. /*****************************************/
  216. /* moves the string pointed to in a      */
  217. /* length character field, blank filling */
  218. /*****************************************/
  219. move_string(string,destination,length)
  220. char *string;
  221. char *destination;
  222. int length;
  223. {
  224. int i;
  225. i = 0;
  226. while ((*string != '\0') && (i < length)) {       /* copy string */
  227.     *destination++ = *string++;
  228.     i++;
  229.     }
  230. for (i; i<length; i++)
  231.     *destination++ = ' ';
  232. }
  233.  
  234. /**********************************************************************/
  235. /*                                                                    */
  236. /*              INPUT UTILITIES                                       */
  237. /*                                                                    */
  238. /**********************************************************************/
  239.  
  240. /*************************/
  241. /* waits for you to move */
  242. /*************************/
  243. wait_for_move()
  244. {
  245. while (TRUE) {
  246.     random();                      /* help randomize */
  247.     switch (getch()) {
  248.          case '5': 
  249.          case 'd': 
  250.          case 'k': {             /* toggle */
  251.               play = !play;
  252.               display_pick(0);
  253.               break;
  254.               }
  255.          case '6': 
  256.          case 's': 
  257.          case 'l': {             /* down */
  258.               display_pick(1);
  259.               break;
  260.               }
  261.          case '0': 
  262.          case ',': 
  263.          case 'a': 
  264.          case ';': 
  265.          case ' ': {             /* doit */
  266.               display_pick(-2);
  267.               return;
  268.               }
  269.          case '4': 
  270.          case 'f': 
  271.          case 'j': {             /* up */
  272.               display_pick(-1);
  273.               break;
  274.               }
  275.          case '?': {
  276.               display_banner();
  277.               clear();
  278.               refresh_screen(FALSE);
  279.               display_pick(0);
  280.               break;
  281.               }
  282.          case 'Q': {             /* quit */
  283.               cleanup_io();
  284.               break;
  285.               }
  286.          case '!': {             /* shell escape */
  287.               shell();
  288.               }      /* keep going */
  289.          case ' ': {
  290.               clear();
  291.               refresh_screen(FALSE);
  292.               display_pick(0);
  293.               break;
  294.               }
  295.          case 'v':
  296.          case 'V': {
  297.               message(T_version,TRUE);
  298.               break;
  299.               }
  300.          case 'D': {                            /* <=========== debug */
  301.               debug = !debug;                   /*                    */
  302.               break;                            /*                    */
  303.               }                                 /* <=========== debug */
  304.          }
  305.     }
  306. }
  307.  
  308. /*****************************/
  309. /* asks for extension yes/no */
  310. /*****************************/
  311. extension_question()
  312. {
  313. extension = TRUE;
  314. while (TRUE) {
  315.     if (extension)
  316.          message(T_extension_yes,TRUE);
  317.     else
  318.          message(T_extension_no,TRUE);
  319.     random();                      /* help randomize */
  320.     switch (getch()) {
  321.          case '5': 
  322.          case 'd': 
  323.          case 'k': {             /* toggle */
  324.               extension = !extension; /* extension */
  325.               break;
  326.               }
  327.          case '0': 
  328.          case ',': 
  329.          case 'a': 
  330.          case ';': 
  331.          case ' ': {             /* doit */
  332.               return(extension);
  333.               break;
  334.               }
  335.          case 'Q': {             /* quit */
  336.               cleanup_io();
  337.               break;
  338.               }
  339.          case '!': {             /* shell escape */
  340.               shell();
  341.               }      /* keep going */
  342.          case ' ': {
  343.               clear();
  344.               refresh_screen(FALSE);
  345.               break;
  346.               }
  347.          }
  348.     }
  349. }
  350.  
  351. /********************************/
  352. /* asks for another game yes/no */
  353. /********************************/
  354. another_question()
  355. {
  356. int another;
  357. another = TRUE;
  358. while (TRUE) {
  359.     if (another)
  360.          message(T_another_yes,TRUE);
  361.     else
  362.          message(T_another_no,TRUE);
  363.     random();                      /* help randomize */
  364.     switch (getch()) {
  365.          case '5': 
  366.          case 'd': 
  367.          case 'k': {             /* toggle */
  368.               another = !another; /* another */
  369.               break;
  370.               }
  371.          case '0': 
  372.          case ',': 
  373.          case 'a': 
  374.          case ';': 
  375.          case ' ': {             /* doit */
  376.               return(another);
  377.               break;
  378.               }
  379.          case 'Q': {             /* quit */
  380.               cleanup_io();
  381.               break;
  382.               }
  383.          case '!': {             /* shell escape */
  384.               shell();
  385.               }      /* keep going */
  386.          case ' ': {
  387.               clear();
  388.               refresh_screen(TRUE);
  389.               break;
  390.               }
  391.          }
  392.     }
  393. }
  394.  
  395. /*********************************/
  396. /* asks for another match yes/no */
  397. /*********************************/
  398. another_match()
  399. {
  400. int another;
  401. another = TRUE;
  402. while (TRUE) {
  403.     if (another)
  404.          message(T_match_yes,TRUE);
  405.     else
  406.          message(T_match_no,TRUE);
  407.     random();                      /* help randomize */
  408.     switch (getch()) {
  409.          case '5': 
  410.          case 'd': 
  411.          case 'k': {             /* toggle */
  412.               another = !another; /* another */
  413.               break;
  414.               }
  415.          case '0': 
  416.          case ',': 
  417.          case 'a': 
  418.          case ';': 
  419.          case ' ': {             /* doit */
  420.               return(another);
  421.               break;
  422.               }
  423.          case 'Q': {             /* quit */
  424.               cleanup_io();
  425.               break;
  426.               }
  427.          case '!': {             /* shell escape */
  428.               shell();
  429.               }      /* keep going */
  430.          case ' ': {
  431.               clear();
  432.               refresh_screen(TRUE);
  433.               break;
  434.               }
  435.          }
  436.     }
  437. }
  438.  
  439. /**************************/
  440. /* perform a shell escape */
  441. /**************************/
  442. shell()
  443. {
  444. extern char * getenv();    /* to stop the warning */
  445. int pid;
  446. char *sh;
  447. int ret_status;
  448. sh = getenv("SHELL");
  449. clear();
  450. move(0,0);
  451. refresh();
  452. endwin();
  453. fflush(stdout);
  454. while((pid = fork()) < 0)
  455.      sleep(1);
  456. if (pid == 0) {
  457.     setuid(getuid());
  458.     setgid(getgid());
  459.     execl(sh == NULL ? "/bin/sh" : sh, "shell", "-i", 0);
  460.     perror("No shelly");
  461.     exit(-1);
  462.     }
  463. else {
  464.     signal(SIGINT, SIG_IGN);
  465.     signal(SIGQUIT, SIG_IGN);
  466.     while (wait(&ret_status) != pid)
  467.          continue;
  468.     signal(SIGINT, cleanup_io);
  469.     signal(SIGQUIT, cleanup_io);
  470.     initscr();
  471.     noecho();
  472.     cbreak();
  473.     }
  474. }
  475. /*********** end of program **********/
  476.