main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* $NetBSD: main.c,v 1.13 2003/08/07 09:36:54 agc Exp $ */
  2. /*-
  3. * Copyright (c) 1990, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Ed James.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. /*
  34. * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
  35. *
  36. * Copy permission is hereby granted provided that this notice is
  37. * retained on all partial or complete copies.
  38. *
  39. * For more info on this and all of my stuff, mail edjames@berkeley.edu.
  40. */
  41. #include <sys/cdefs.h>
  42. #ifndef lint
  43. __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
  44. The Regents of the University of California. All rights reserved.\n");
  45. #endif /* not lint */
  46. #ifndef lint
  47. #if 0
  48. static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
  49. #else
  50. __RCSID("$NetBSD: main.c,v 1.13 2003/08/07 09:36:54 agc Exp $");
  51. #endif
  52. #endif /* not lint */
  53. #include "include.h"
  54. #include "pathnames.h"
  55. extern FILE *yyin;
  56. int
  57. main(ac, av)
  58. int ac;
  59. char *av[];
  60. {
  61. int seed;
  62. int f_usage = 0, f_list = 0, f_showscore = 0;
  63. int f_printpath = 0;
  64. const char *file = NULL;
  65. int ch;
  66. struct sigaction sa;
  67. #ifdef BSD
  68. struct itimerval itv;
  69. #endif
  70. /* Open the score file then revoke setgid privileges */
  71. open_score_file();
  72. setregid(getgid(), getgid());
  73. start_time = seed = time(NULL);
  74. while ((ch = getopt(ac, av, "ulstpg:f:r:")) != -1) {
  75. switch (ch) {
  76. case '?':
  77. case 'u':
  78. default:
  79. f_usage++;
  80. break;
  81. case 'l':
  82. f_list++;
  83. break;
  84. case 's':
  85. case 't':
  86. f_showscore++;
  87. break;
  88. case 'p':
  89. f_printpath++;
  90. break;
  91. case 'r':
  92. seed = atoi(optarg);
  93. break;
  94. case 'f':
  95. case 'g':
  96. file = optarg;
  97. break;
  98. }
  99. }
  100. if (optind < ac)
  101. f_usage++;
  102. srandom(seed);
  103. if (f_usage)
  104. fprintf(stderr,
  105. "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
  106. av[0]);
  107. if (f_showscore)
  108. log_score(1);
  109. if (f_list)
  110. list_games();
  111. if (f_printpath) {
  112. char buf[100];
  113. strcpy(buf, _PATH_GAMES);
  114. buf[strlen(buf) - 1] = '\0';
  115. puts(buf);
  116. }
  117. if (f_usage || f_showscore || f_list || f_printpath)
  118. exit(0);
  119. if (file == NULL)
  120. file = default_game();
  121. else
  122. file = okay_game(file);
  123. if (file == NULL || read_file(file) < 0)
  124. exit(1);
  125. init_gr();
  126. setup_screen(sp);
  127. addplane();
  128. signal(SIGINT, quit);
  129. signal(SIGQUIT, quit);
  130. #ifdef BSD
  131. signal(SIGTSTP, SIG_IGN);
  132. signal(SIGSTOP, SIG_IGN);
  133. #endif
  134. signal(SIGHUP, log_score_quit);
  135. signal(SIGTERM, log_score_quit);
  136. tcgetattr(fileno(stdin), &tty_start);
  137. tty_new = tty_start;
  138. tty_new.c_lflag &= ~(ICANON|ECHO);
  139. tty_new.c_iflag |= ICRNL;
  140. tty_new.c_cc[VMIN] = 1;
  141. tty_new.c_cc[VTIME] = 0;
  142. tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
  143. sa.sa_handler = update;
  144. sigemptyset(&sa.sa_mask);
  145. sigaddset(&sa.sa_mask, SIGALRM);
  146. sigaddset(&sa.sa_mask, SIGINT);
  147. sa.sa_flags = 0;
  148. sigaction(SIGALRM, &sa, (struct sigaction *)0);
  149. #ifdef BSD
  150. itv.it_value.tv_sec = 0;
  151. itv.it_value.tv_usec = 1;
  152. itv.it_interval.tv_sec = sp->update_secs;
  153. itv.it_interval.tv_usec = 0;
  154. setitimer(ITIMER_REAL, &itv, NULL);
  155. #endif
  156. #ifdef SYSV
  157. alarm(sp->update_secs);
  158. #endif
  159. for (;;) {
  160. if (getcommand() != 1)
  161. planewin();
  162. else {
  163. #ifdef BSD
  164. itv.it_value.tv_sec = 0;
  165. itv.it_value.tv_usec = 0;
  166. setitimer(ITIMER_REAL, &itv, NULL);
  167. #endif
  168. #ifdef SYSV
  169. alarm(0);
  170. #endif
  171. update(0);
  172. #ifdef BSD
  173. itv.it_value.tv_sec = sp->update_secs;
  174. itv.it_value.tv_usec = 0;
  175. itv.it_interval.tv_sec = sp->update_secs;
  176. itv.it_interval.tv_usec = 0;
  177. setitimer(ITIMER_REAL, &itv, NULL);
  178. #endif
  179. #ifdef SYSV
  180. alarm(sp->update_secs);
  181. #endif
  182. }
  183. }
  184. }
  185. int
  186. read_file(s)
  187. const char *s;
  188. {
  189. int retval;
  190. file = s;
  191. yyin = fopen(s, "r");
  192. if (yyin == NULL) {
  193. warn("fopen %s", s);
  194. return (-1);
  195. }
  196. retval = yyparse();
  197. fclose(yyin);
  198. if (retval != 0)
  199. return (-1);
  200. else
  201. return (0);
  202. }
  203. const char *
  204. default_game()
  205. {
  206. FILE *fp;
  207. static char file[256];
  208. char line[256], games[256];
  209. strcpy(games, _PATH_GAMES);
  210. strcat(games, GAMES);
  211. if ((fp = fopen(games, "r")) == NULL) {
  212. warn("fopen %s", games);
  213. return (NULL);
  214. }
  215. if (fgets(line, sizeof(line), fp) == NULL) {
  216. fprintf(stderr, "%s: no default game available\n", games);
  217. return (NULL);
  218. }
  219. fclose(fp);
  220. line[strlen(line) - 1] = '\0';
  221. strcpy(file, _PATH_GAMES);
  222. strcat(file, line);
  223. return (file);
  224. }
  225. const char *
  226. okay_game(s)
  227. const char *s;
  228. {
  229. FILE *fp;
  230. static char file[256];
  231. const char *ret = NULL;
  232. char line[256], games[256];
  233. strcpy(games, _PATH_GAMES);
  234. strcat(games, GAMES);
  235. if ((fp = fopen(games, "r")) == NULL) {
  236. warn("fopen %s", games);
  237. return (NULL);
  238. }
  239. while (fgets(line, sizeof(line), fp) != NULL) {
  240. line[strlen(line) - 1] = '\0';
  241. if (strcmp(s, line) == 0) {
  242. strcpy(file, _PATH_GAMES);
  243. strcat(file, line);
  244. ret = file;
  245. break;
  246. }
  247. }
  248. fclose(fp);
  249. if (ret == NULL) {
  250. test_mode = 1;
  251. ret = s;
  252. fprintf(stderr, "%s: %s: game not found\n", games, s);
  253. fprintf(stderr, "Your score will not be logged.\n");
  254. sleep(2); /* give the guy time to read it */
  255. }
  256. return (ret);
  257. }
  258. int
  259. list_games()
  260. {
  261. FILE *fp;
  262. char line[256], games[256];
  263. int num_games = 0;
  264. strcpy(games, _PATH_GAMES);
  265. strcat(games, GAMES);
  266. if ((fp = fopen(games, "r")) == NULL) {
  267. warn("fopen %s", games);
  268. return (-1);
  269. }
  270. puts("available games:");
  271. while (fgets(line, sizeof(line), fp) != NULL) {
  272. printf(" %s", line);
  273. num_games++;
  274. }
  275. fclose(fp);
  276. if (num_games == 0) {
  277. fprintf(stderr, "%s: no games available\n", games);
  278. return (-1);
  279. }
  280. return (0);
  281. }