1*38821839Sdholland /* $NetBSD: dr_main.c,v 1.15 2010/08/06 09:14:40 dholland Exp $ */
2982615d7Scgd
361f28255Scgd /*
4982615d7Scgd * Copyright (c) 1983, 1993
5982615d7Scgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32bf72b656Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34982615d7Scgd #if 0
35982615d7Scgd static char sccsid[] = "@(#)dr_main.c 8.2 (Berkeley) 4/16/94";
36982615d7Scgd #else
37*38821839Sdholland __RCSID("$NetBSD: dr_main.c,v 1.15 2010/08/06 09:14:40 dholland Exp $");
38982615d7Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
4196e08ad4Sdholland #include <err.h>
42*38821839Sdholland #include <setjmp.h>
4361a39e0bSjwise #include <signal.h>
44aac077fcSjwise #include <stdio.h>
45bf72b656Schristos #include <stdlib.h>
46bf72b656Schristos #include <unistd.h>
47aac077fcSjwise #include "extern.h"
48aac077fcSjwise #include "driver.h"
49*38821839Sdholland #include "player.h" /* XXX for LEAVE_FORK */
50*38821839Sdholland #include "restart.h"
51*38821839Sdholland
52*38821839Sdholland static int driver_wait_fd = -1;
5361f28255Scgd
54bf72b656Schristos int
dr_main(void)55dfea9f08Sjwise dr_main(void)
5661f28255Scgd {
57bf72b656Schristos int n;
58bf72b656Schristos struct ship *sp;
5961f28255Scgd int nat[NNATION];
6061f28255Scgd int value = 0;
6161f28255Scgd
62*38821839Sdholland /*
63*38821839Sdholland * XXX need a way to print diagnostics back to the player
64*38821839Sdholland * process instead of stomping on the curses screen.
65*38821839Sdholland */
66*38821839Sdholland
67dfea9f08Sjwise signal(SIGINT, SIG_IGN);
68dfea9f08Sjwise signal(SIGQUIT, SIG_IGN);
69dfea9f08Sjwise signal(SIGTSTP, SIG_IGN);
7061f28255Scgd if (game < 0 || game >= NSCENE) {
71*38821839Sdholland errx(1, "\ndriver: Bad game number %d", game);
7261f28255Scgd }
7361f28255Scgd cc = &scene[game];
7461f28255Scgd ls = SHIP(cc->vessels);
7561f28255Scgd if (sync_open() < 0) {
76*38821839Sdholland err(1, "\ndriver: syncfile");
7761f28255Scgd }
7861f28255Scgd for (n = 0; n < NNATION; n++)
7961f28255Scgd nat[n] = 0;
8061f28255Scgd foreachship(sp) {
8161f28255Scgd if (sp->file == NULL &&
823b678797Sdholland (sp->file = calloc(1, sizeof (struct File))) == NULL) {
83*38821839Sdholland fprintf(stderr, "\nDRIVER: Out of memory.\n");
8461f28255Scgd exit(1);
8561f28255Scgd }
8661f28255Scgd sp->file->index = sp - SHIP(0);
8761f28255Scgd sp->file->loadL = L_ROUND;
8861f28255Scgd sp->file->loadR = L_ROUND;
8961f28255Scgd sp->file->readyR = R_LOADED|R_INITIAL;
9061f28255Scgd sp->file->readyL = R_LOADED|R_INITIAL;
9161f28255Scgd sp->file->stern = nat[sp->nationality]++;
9261f28255Scgd sp->file->dir = sp->shipdir;
9361f28255Scgd sp->file->row = sp->shiprow;
9461f28255Scgd sp->file->col = sp->shipcol;
9561f28255Scgd }
9661f28255Scgd windspeed = cc->windspeed;
9761f28255Scgd winddir = cc->winddir;
9861f28255Scgd people = 0;
99*38821839Sdholland
100*38821839Sdholland /* report back to the player process that we've started */
101*38821839Sdholland if (driver_wait_fd >= 0) {
102*38821839Sdholland close(driver_wait_fd);
103*38821839Sdholland }
104*38821839Sdholland
10561f28255Scgd for (;;) {
10661f28255Scgd sleep(7);
10761f28255Scgd if (Sync() < 0) {
10861f28255Scgd value = 1;
10961f28255Scgd break;
11061f28255Scgd }
11161f28255Scgd if (next() < 0)
11261f28255Scgd break;
11361f28255Scgd unfoul();
11461f28255Scgd checkup();
11561f28255Scgd prizecheck();
11661f28255Scgd moveall();
11761f28255Scgd thinkofgrapples();
11861f28255Scgd boardcomp();
11961f28255Scgd compcombat();
12061f28255Scgd resolve();
12161f28255Scgd reload();
12261f28255Scgd checksails();
12361f28255Scgd if (Sync() < 0) {
12461f28255Scgd value = 1;
12561f28255Scgd break;
12661f28255Scgd }
12761f28255Scgd }
12861f28255Scgd sync_close(1);
12961f28255Scgd return value;
13061f28255Scgd }
131*38821839Sdholland
132*38821839Sdholland void
startdriver(void)133*38821839Sdholland startdriver(void)
134*38821839Sdholland {
135*38821839Sdholland int fds[2];
136*38821839Sdholland char c;
137*38821839Sdholland
138*38821839Sdholland if (pipe(fds)) {
139*38821839Sdholland warn("pipe");
140*38821839Sdholland leave(LEAVE_FORK);
141*38821839Sdholland return;
142*38821839Sdholland }
143*38821839Sdholland
144*38821839Sdholland switch (fork()) {
145*38821839Sdholland case 0:
146*38821839Sdholland close(fds[0]);
147*38821839Sdholland driver_wait_fd = fds[1];
148*38821839Sdholland longjmp(restart, MODE_DRIVER);
149*38821839Sdholland /*NOTREACHED*/
150*38821839Sdholland case -1:
151*38821839Sdholland warn("fork");
152*38821839Sdholland close(fds[0]);
153*38821839Sdholland close(fds[1]);
154*38821839Sdholland leave(LEAVE_FORK);
155*38821839Sdholland break;
156*38821839Sdholland default:
157*38821839Sdholland hasdriver++;
158*38821839Sdholland close(fds[1]);
159*38821839Sdholland read(fds[0], &c, 1);
160*38821839Sdholland close(fds[0]);
161*38821839Sdholland }
162*38821839Sdholland }
163