2 * Copyright (c) 1983-2003, Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University of California, San Francisco nor
15 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $OpenBSD: hunt.c,v 1.13 2008/03/17 09:17:56 sobrado Exp $
32 * $NetBSD: hunt.c,v 1.8 1998/09/13 15:27:28 hubertf Exp $
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <sys/sockio.h>
52 #include <netinet/in.h>
55 #include <arpa/inet.h>
63 #define __attribute__(x)
66 FLAG Am_monitor = FALSE;
68 char map_key[256]; /* what to map keys to */
70 char *Send_message = NULL;
72 static char *Sock_host;
73 static char *use_port;
74 static FLAG Query_driver = FALSE;
75 static FLAG Show_scores = FALSE;
76 static struct sockaddr Daemon;
79 static char name[NAMELEN];
80 static char team = '-';
84 static void dump_scores(void);
85 static long env_init(long);
86 static void fill_in_blanks(void);
87 static void leave(int, const char *) __attribute__((__noreturn__));
88 static void sigterm(int);
89 static int find_driver(void);
93 * Main program for local process
96 main(int ac, char **av)
103 enter_status = env_init((long) Q_CLOAK);
104 while ((c = getopt(ac, av, "Sbcfh:l:mn:op:qst:w:")) != -1) {
106 case 'l': /* rsh compatibility */
108 strlcpy(name, optarg, sizeof name);
112 if (!isdigit(team) && team != ' ') {
113 warnx("Team names must be numeric or space");
126 case 'q': /* query whether hunt is running */
130 Send_message = optarg;
137 Server_port = atoi(use_port);
140 enter_status = Q_CLOAK;
143 enter_status = Q_FLY;
146 enter_status = Q_SCAN;
153 fputs("usage: hunt [-bcfmqSs] [-n name] [-p port] "
154 "[-t team] [-w message] [[-h] host]\n",
161 else if (optind + 1 == ac)
162 Sock_host = av[ac - 1];
164 if (Server_port == 0) {
165 se = getservbyname("hunt", "udp");
167 Server_port = ntohs(se->s_port);
169 Server_port = HUNT_PORT;
178 struct driver *driver;
180 probe_drivers(C_MESSAGE, Sock_host);
181 while ((driver = next_driver()) != NULL) {
182 printf("%d player%s hunting on %s!\n",
184 (driver->response == 1) ? "" : "s",
185 driver_name(driver));
193 errx(1, "otto mode incompatible with monitor mode");
194 strlcpy(name, "otto", sizeof name);
202 if (LINES < SCREEN_HEIGHT || COLS < SCREEN_WIDTH) {
204 leave(1, "Need a larger window");
206 display_clear_the_screen();
207 signal(SIGINT, intr);
208 signal(SIGTERM, sigterm);
209 /* signal(SIGPIPE, SIG_IGN); */
213 while (!find_driver()) {
216 leave(1, "No one playing");
219 if (Sock_host == NULL) {
221 leave(1, "huntd not running");
232 Socket = socket(Daemon.sa_family, SOCK_STREAM, 0);
237 if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK,
238 &option, sizeof option) < 0)
239 warn("setsockopt loopback");
242 if (connect(Socket, &Daemon, Daemon.sa_len) == -1) {
243 if (errno == ECONNREFUSED)
248 do_connect(name, team, enter_status);
249 if (Send_message != NULL) {
251 if (enter_status == Q_MESSAGE)
257 if ((enter_status = quit(enter_status)) == Q_QUIT)
266 * Set Daemon to be the address of a hunt driver, or return 0 on failure.
268 * We start quietly probing for drivers. As soon as one driver is found
269 * we show it in the list. If we run out of drivers and we only have one
270 * then we choose it. Otherwise we present a list of the found drivers.
275 int last_driver, numdrivers, waiting, is_current;
276 struct driver *driver;
281 probe_drivers(Am_monitor ? C_MONITOR : C_PLAYER, Sock_host);
287 if (numdrivers == 0) {
288 /* Silently wait for at least one driver */
289 driver = next_driver();
290 } else if (!waiting || (driver =
291 next_driver_fd(STDIN_FILENO)) == (struct driver *)-1) {
292 /* We have a key waiting, or no drivers left */
294 if (c == '\r' || c == '\n' || c == ' ') {
297 else if (last_driver != -1)
298 c = 'a' + last_driver;
300 if (c < 'a' || c >= numdrivers + 'a') {
304 driver = &drivers[c - 'a'];
308 if (driver == NULL) {
310 if (numdrivers == 0) {
312 return 0; /* Failure */
314 if (numdrivers == 1) {
315 driver = &drivers[0];
321 /* Use the preferred host straight away. */
325 if (numdrivers == 0) {
326 display_clear_the_screen();
328 display_put_str("Pick one:");
331 /* Mark the last driver we used with an asterisk */
332 is_current = (last_driver == -1 && Daemon.sa_len != 0 &&
333 memcmp(&Daemon, &driver->addr, Daemon.sa_len) == 0);
335 last_driver = numdrivers;
337 /* Display it in the list if there is room */
338 if (numdrivers < HEIGHT - 3) {
339 xname = driver_name(driver);
340 display_move(3 + numdrivers, 0);
341 snprintf(buf, sizeof buf, "%6c %c %s",
342 is_current ? '*' : ' ', 'a' + numdrivers, xname);
343 display_put_str(buf);
346 /* Clear the last 'Enter letter' line if any */
347 display_move(4 + numdrivers, 0);
350 if (last_driver != -1)
351 snprintf(buf, sizeof buf, "Enter letter [%c]: ",
354 snprintf(buf, sizeof buf, "Enter letter: ");
356 display_move(5 + numdrivers, 0);
357 display_put_str(buf);
363 display_clear_the_screen();
364 Daemon = driver->addr;
367 return 1; /* Success */
373 struct driver *driver;
377 probe_drivers(C_SCORES, Sock_host);
378 while ((driver = next_driver()) != NULL) {
379 printf("\n%s:\n", driver_name(driver));
382 if ((s = socket(driver->addr.sa_family, SOCK_STREAM, 0)) < 0) {
386 if (connect(s, &driver->addr, driver->addr.sa_len) < 0) {
391 while ((cnt = read(s, buf, sizeof buf)) > 0) {
392 /* Whittle out bad characters */
393 for (i = 0; i < cnt; i++)
394 if ((buf[i] < ' ' || buf[i] > '~') &&
395 buf[i] != '\n' && buf[i] != '\t')
397 fwrite(buf, cnt, 1, stdout);
411 * We had a bad connection. For the moment we assume that this
412 * means the game is full.
417 leave(1, "lost connection to huntd");
422 * version number mismatch.
428 leave(1, "Version number mismatch. No go.");
433 * Handle a terminate signal
436 sigterm(int signo __unused)
443 * Remove a '\n' at the end of a string if there is one
450 cp = strrchr(s, '\n');
457 * Handle a interrupt signal
460 intr(int dummy __unused)
466 signal(SIGINT, SIG_IGN);
467 display_getyx(&y, &x);
468 display_move(HEIGHT, 0);
469 display_put_str("Really quit? ");
479 write(Socket, "q", 1);
484 else if (ch == 'n') {
485 signal(SIGINT, intr);
491 display_put_str("(Yes or No) ");
502 * Leave the game somewhat gracefully, restoring all current
506 leave(int eval, const char *mesg)
512 display_move(HEIGHT, 0);
518 if (errno == 0 && mesg != NULL)
519 errx(eval, "%s", mesg);
520 else if (mesg != NULL)
521 err(eval, "%s", mesg);
527 * initialise game parameters from the HUNT envvar
530 env_init(long enter_status)
533 char *envp, *envname, *s;
535 /* Map all keys to themselves: */
536 for (i = 0; i < 256; i++)
537 map_key[i] = (char) i;
540 if ((envp = getenv("HUNT")) != NULL) {
541 while ((s = strpbrk(envp, "=,")) != NULL) {
542 if (strncmp(envp, "cloak,", s - envp + 1) == 0) {
543 enter_status = Q_CLOAK;
546 else if (strncmp(envp, "scan,", s - envp + 1) == 0) {
547 enter_status = Q_SCAN;
550 else if (strncmp(envp, "fly,", s - envp + 1) == 0) {
551 enter_status = Q_FLY;
554 else if (strncmp(envp, "nobeep,", s - envp + 1) == 0) {
558 else if (strncmp(envp, "name=", s - envp + 1) == 0) {
560 if ((s = strchr(envp, ',')) == NULL) {
562 strlcpy(name, envname, sizeof name);
566 strlcpy(name, envname, sizeof name);
569 else if (strncmp(envp, "port=", s - envp + 1) == 0) {
571 Server_port = atoi(use_port);
572 if ((s = strchr(envp, ',')) == NULL) {
579 else if (strncmp(envp, "host=", s - envp + 1) == 0) {
581 if ((s = strchr(envp, ',')) == NULL) {
588 else if (strncmp(envp, "message=", s - envp + 1) == 0) {
589 Send_message = s + 1;
590 if ((s = strchr(envp, ',')) == NULL) {
597 else if (strncmp(envp, "team=", s - envp + 1) == 0) {
601 if ((s = strchr(envp, ',')) == NULL) {
607 } /* must be last option */
608 else if (strncmp(envp, "mapkey=", s - envp + 1) == 0) {
609 for (s = s + 1; *s != '\0'; s += 2) {
610 map_key[(unsigned int) *s] = *(s + 1);
611 if (*(s + 1) == '\0') {
619 printf("unknown option %s\n", envp);
620 if ((s = strchr(envp, ',')) == NULL) {
629 strlcpy(name, envp, sizeof name);
631 printf("unknown option %s\n", envp);
639 * quiz the user for the information they didn't provide earlier
648 if (name[0] != '\0') {
649 printf("Entering as '%s'", name);
650 if (team != ' ' && team != '-')
651 printf(" on team %c.\n", team);
655 printf("Enter your code name: ");
656 if (fgets(name, sizeof name, stdin) == NULL)
660 if (name[0] == '\0') {
661 printf("You have to have a code name!\n");
664 for (cp = name; *cp != '\0'; cp++)
667 printf("Illegal character in your code name.\n");
671 printf("Enter your team (0-9 or nothing): ");
675 else if (i == '\n' || i == EOF || i == ' ')
677 /* ignore trailing chars */
678 while (i != '\n' && i != EOF)
681 printf("Teams must be numeric.\n");