usched: Allow process to change self cpu affinity
[dragonfly.git] / games / atc / main.c
blob500bcf1504cf3e10c41f69ddf6c367ed3d8262df
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Ed James.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)main.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/atc/main.c,v 1.9 1999/11/30 03:48:21 billf Exp $
38 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
40 * Copy permission is hereby granted provided that this notice is
41 * retained on all partial or complete copies.
43 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
46 #include <stdlib.h>
47 #include "include.h"
48 #include "pathnames.h"
50 extern FILE *yyin;
51 extern int yyparse(void);
52 static int read_file(const char *);
53 static const char *default_game(void);
54 static const char *okay_game(const char *);
55 static int list_games(void);
57 int
58 main(int argc __unused, char *argv[])
60 int seed = 0;
61 int f_usage = 0, f_list = 0, f_showscore = 0;
62 int f_printpath = 0;
63 const char *file = NULL;
64 char *p_name, *ptr;
65 struct itimerval itv;
67 /* Open the score file then revoke setgid privileges */
68 open_score_file();
69 setregid(getgid(), getgid());
71 start_time = time(0);
73 p_name = *argv++;
74 while (*argv) {
75 #ifndef SAVEDASH
76 if (**argv == '-')
77 ++*argv;
78 else
79 break;
80 #endif
81 ptr = *argv++;
82 while (*ptr) {
83 switch (*ptr) {
84 case '?':
85 case 'u':
86 f_usage++;
87 break;
88 case 'l':
89 f_list++;
90 break;
91 case 's':
92 case 't':
93 f_showscore++;
94 break;
95 case 'p':
96 f_printpath++;
97 break;
98 case 'r':
99 srandom(atoi(*argv));
100 seed = 1;
101 argv++;
102 break;
103 case 'f':
104 case 'g':
105 file = *argv;
106 argv++;
107 break;
108 default:
109 fprintf(stderr, "Unknown option '%c'\n", *ptr);
110 f_usage++;
111 break;
113 ptr++;
116 if (!seed)
117 srandomdev();
119 if (f_usage)
120 fprintf(stderr,
121 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
122 p_name);
123 if (f_showscore)
124 log_score(1);
125 if (f_list)
126 list_games();
127 if (f_printpath) {
128 char buf[100];
130 strcpy(buf, _PATH_GAMES);
131 buf[strlen(buf) - 1] = '\0';
132 puts(buf);
135 if (f_usage || f_showscore || f_list || f_printpath)
136 exit(0);
138 if (file == NULL)
139 file = default_game();
140 else
141 file = okay_game(file);
143 if (file == NULL || read_file(file) < 0)
144 exit(1);
146 init_gr();
147 setup_screen(sp);
149 addplane();
151 signal(SIGINT, quit);
152 signal(SIGQUIT, quit);
153 signal(SIGTSTP, SIG_IGN);
154 signal(SIGSTOP, SIG_IGN);
155 signal(SIGHUP, log_score_quit);
156 signal(SIGTERM, log_score_quit);
158 tcgetattr(fileno(stdin), &tty_start);
159 bcopy(&tty_start, &tty_new, sizeof(tty_new));
160 tty_new.c_lflag &= ~(ICANON|ECHO);
161 tty_new.c_cc[VMIN] = 1;
162 tty_new.c_cc[VTIME] = 0;
163 tcsetattr(fileno(stdin), TCSANOW, &tty_new);
164 signal(SIGALRM, update);
166 itv.it_value.tv_sec = 0;
167 itv.it_value.tv_usec = 1;
168 itv.it_interval.tv_sec = sp->update_secs;
169 itv.it_interval.tv_usec = 0;
170 setitimer(ITIMER_REAL, &itv, NULL);
172 for (;;) {
173 if (getcommand() != 1)
174 planewin();
175 else {
176 itv.it_value.tv_sec = 0;
177 itv.it_value.tv_usec = 0;
178 setitimer(ITIMER_REAL, &itv, NULL);
180 update(0);
182 itv.it_value.tv_sec = sp->update_secs;
183 itv.it_value.tv_usec = 0;
184 itv.it_interval.tv_sec = sp->update_secs;
185 itv.it_interval.tv_usec = 0;
186 setitimer(ITIMER_REAL, &itv, NULL);
191 static int
192 read_file(const char *s)
194 int retval;
196 filename = s;
197 yyin = fopen(s, "r");
198 if (yyin == NULL) {
199 perror(s);
200 return (-1);
202 retval = yyparse();
203 fclose(yyin);
205 if (retval != 0)
206 return (-1);
207 else
208 return (0);
211 static const char *
212 default_game(void)
214 FILE *fp;
215 static char file[256];
216 char line[256], games[256];
218 strcpy(games, _PATH_GAMES);
219 strcat(games, GAMES);
221 if ((fp = fopen(games, "r")) == NULL) {
222 perror(games);
223 return (NULL);
225 if (fgets(line, sizeof(line), fp) == NULL) {
226 fprintf(stderr, "%s: no default game available\n", games);
227 return (NULL);
229 fclose(fp);
230 line[strlen(line) - 1] = '\0';
231 strcpy(file, _PATH_GAMES);
232 strcat(file, line);
233 return (file);
236 static const char *
237 okay_game(const char *s)
239 FILE *fp;
240 static char file[256];
241 const char *ret = NULL;
242 char line[256], games[256];
244 strcpy(games, _PATH_GAMES);
245 strcat(games, GAMES);
247 if ((fp = fopen(games, "r")) == NULL) {
248 perror(games);
249 return (NULL);
251 while (fgets(line, sizeof(line), fp) != NULL) {
252 line[strlen(line) - 1] = '\0';
253 if (strcmp(s, line) == 0) {
254 strcpy(file, _PATH_GAMES);
255 strcat(file, line);
256 ret = file;
257 break;
260 fclose(fp);
261 if (ret == NULL) {
262 test_mode = 1;
263 ret = s;
264 fprintf(stderr, "%s: %s: game not found\n", games, s);
265 fprintf(stderr, "Your score will not be logged.\n");
266 sleep(2); /* give the guy time to read it */
268 return (ret);
271 static int
272 list_games(void)
274 FILE *fp;
275 char line[256], games[256];
276 int num_games = 0;
278 strcpy(games, _PATH_GAMES);
279 strcat(games, GAMES);
281 if ((fp = fopen(games, "r")) == NULL) {
282 perror(games);
283 return (-1);
285 puts("available games:");
286 while (fgets(line, sizeof(line), fp) != NULL) {
287 printf(" %s", line);
288 num_games++;
290 fclose(fp);
291 if (num_games == 0) {
292 fprintf(stderr, "%s: no games available\n", games);
293 return (-1);
295 return (0);