| 1 |
/* netToe Version 1.5.1 |
| 2 |
* |
| 3 |
* Copyright 2000,2001 Gabriele Giorgetti <[email protected]> |
| 4 |
* 2009-2014 Mats Erik Andersson <[email protected]> |
| 5 |
* |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <string.h> |
| 30 |
#include <time.h> |
| 31 |
|
| 32 |
/* Macros AF_* for address families. */ |
| 33 |
#ifdef HAVE_SYS_TYPES_H |
| 34 |
# include <sys/types.h> |
| 35 |
#endif |
| 36 |
#ifdef HAVE_SYS_SOCKET_H |
| 37 |
# include <sys/socket.h> |
| 38 |
#endif |
| 39 |
#ifdef HAVE_NETINET_IN_H |
| 40 |
# include <netinet/in.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#include "game.h" |
| 44 |
#include "misc.h" |
| 45 |
#include "terminal.h" |
| 46 |
|
| 47 |
int who_start_first(void) |
| 48 |
{ |
| 49 |
long generated_number; |
| 50 |
|
| 51 |
generated_number = 1 + (random() % 10); |
| 52 |
|
| 53 |
if ( (generated_number == 2) || (generated_number == 4) |
| 54 |
|| (generated_number == 6) || (generated_number == 8) |
| 55 |
|| (generated_number == 10) ) |
| 56 |
{ |
| 57 |
return 1; |
| 58 |
} |
| 59 |
|
| 60 |
return 2; |
| 61 |
} /* who_start_first(void) */ |
| 62 |
|
| 63 |
void get_player_pname(char * str, size_t maxlen) |
| 64 |
{ |
| 65 |
char buffer[1024], *pek; |
| 66 |
size_t n = 0; |
| 67 |
|
| 68 |
str[0] ='\0'; |
| 69 |
if ( maxlen <= 1 ) |
| 70 |
return; |
| 71 |
|
| 72 |
if (!fgets(buffer, sizeof(buffer), stdin)) |
| 73 |
buffer[0] = '\0'; |
| 74 |
|
| 75 |
pek = buffer; |
| 76 |
|
| 77 |
/* Skip initial white space. */ |
| 78 |
while ( *pek == ' ' || *pek == '\t' || *pek == '\r' ) |
| 79 |
++pek; |
| 80 |
|
| 81 |
while ( *pek && (n + 1 < maxlen) && (n + 1 < sizeof(buffer)) ) |
| 82 |
{ |
| 83 |
if ( *pek == '\n' ) |
| 84 |
break; |
| 85 |
|
| 86 |
/* Promote LF and TAB to simple space. */ |
| 87 |
if ( *pek == '\r' || *pek == '\t' ) |
| 88 |
*pek = ' '; |
| 89 |
|
| 90 |
/* Avoid duplicate white space. */ |
| 91 |
if ( (n > 0) && (str[n - 1] == ' ') && (*pek == ' ') ) |
| 92 |
{ |
| 93 |
++pek; |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
str[n++] = *(pek++); |
| 98 |
} |
| 99 |
|
| 100 |
/* Skip trailing white space. */ |
| 101 |
while ( (n > 0) && (str[n - 1] == ' ') ) |
| 102 |
--n; |
| 103 |
|
| 104 |
/* Safe guard for empty name. */ |
| 105 |
if ( n == 0 ) |
| 106 |
strcpy(str, "Anon"); |
| 107 |
else |
| 108 |
str[n] = '\0'; |
| 109 |
} /* get_player_pname(char *, size_t) */ |
| 110 |
|
| 111 |
int check_pname(const char *pname, size_t maxlen) |
| 112 |
{ |
| 113 |
if ( pname == NULL ) |
| 114 |
return 1; |
| 115 |
|
| 116 |
if ( (strlen(pname)) > maxlen ) |
| 117 |
return 1; |
| 118 |
|
| 119 |
return 0; |
| 120 |
} /* check_pname(const char *, size_t) */ |
| 121 |
|
| 122 |
void print_infos_screen (void) |
| 123 |
{ |
| 124 |
nettoe_term_reset_color(); |
| 125 |
printf(" netToe is a Tic Tac Toe-like game for Linux and UNIX. \n"); |
| 126 |
printf(" It is possible to play it against the computer, another \n"); |
| 127 |
printf(" player on the same PC, or against another player over \n"); |
| 128 |
printf(" a network (Internet, and everything using TCP/IP). \n"); |
| 129 |
printf(" To play it over a network you must first set up a server.\n"); |
| 130 |
printf(" This is done in the network game options menu, selecting \n"); |
| 131 |
printf(" \"Host the game\". Then the (remote) second player must \n"); |
| 132 |
printf(" connect to the server by typing its IP address, or name. \n"); |
| 133 |
printf(" That should be enough for you to have some leisure. \n"); |
| 134 |
printf(" For a detailed guide on HOW TO PLAY, and for other info, \n"); |
| 135 |
printf(" do read the manual page "); |
| 136 |
nettoe_term_set_color (COLOR_BLUE, ATTRIB_BRIGHT); |
| 137 |
printf("nettoe"); |
| 138 |
nettoe_term_reset_color(); |
| 139 |
printf("(6).\n\n"); |
| 140 |
|
| 141 |
printf(" netToe %s (%s)\n", PACKAGE_VERSION, PACKAGE_RELEASE_DATE); |
| 142 |
printf(" Copyright 2000,2001 Gabriele Giorgetti\n"); |
| 143 |
printf(" 2009-2014 Mats Erik Andersson\n"); |
| 144 |
printf(" %s\n", HOMEPAGE); |
| 145 |
printf("\n Press"); |
| 146 |
nettoe_term_set_color (COLOR_RED, ATTRIB_BRIGHT); |
| 147 |
printf(" enter"); |
| 148 |
nettoe_term_reset_color(); |
| 149 |
printf(" to go back to the main menu. "); |
| 150 |
|
| 151 |
fflush (stdin); |
| 152 |
getchar (); |
| 153 |
|
| 154 |
nettoe_term_set_default_color(); |
| 155 |
} /* print_infos_screen(void) */ |
| 156 |
|
| 157 |
void parse_cmd_args(int argc, char *argv[]) |
| 158 |
{ |
| 159 |
int i; |
| 160 |
|
| 161 |
/* Any address family will do. */ |
| 162 |
addrfamily = AF_UNSPEC; |
| 163 |
|
| 164 |
if ( argc < 2 ) |
| 165 |
return; |
| 166 |
|
| 167 |
for (i = 1; i <= argc - 1; i++) |
| 168 |
{ |
| 169 |
if ( (!strcmp(argv[i], "-h")) || (!strcmp(argv[i], "--help")) ) |
| 170 |
{ |
| 171 |
fprintf(stdout, "netToe %s the enhanced, and networked, " |
| 172 |
"Tic Tac Toe game.\n", PACKAGE_VERSION); |
| 173 |
fprintf(stdout, "\n"); |
| 174 |
fprintf(stdout, "Usage: nettoe [OPTIONS]\n"); |
| 175 |
fprintf(stdout, "\n"); |
| 176 |
fprintf(stdout, "-4, --ipv4 only IPv4 networking\n"); |
| 177 |
fprintf(stdout, "-6, --ipv6 only IPv6 networking\n"); |
| 178 |
fprintf(stdout, "-nb, --no-beep disable beeps\n"); |
| 179 |
fprintf(stdout, "-nc, --no-colors disable colors\n"); |
| 180 |
fprintf(stdout, "-h, --help display this help," |
| 181 |
" then exit\n"); |
| 182 |
fprintf(stdout, "-v, --version output version information," |
| 183 |
" then exit\n"); |
| 184 |
fprintf(stdout, "\n"); |
| 185 |
fprintf(stdout, "The netToe project can be found at: \n"); |
| 186 |
fprintf(stdout, " %s\n", HOMEPAGE); |
| 187 |
fprintf(stdout, "\n"); |
| 188 |
fprintf(stdout, "Please send any bug reports, or comments to:\n"); |
| 189 |
fprintf(stdout, " %s\n", AUTHOR2_EMAIL); |
| 190 |
|
| 191 |
exit(EXIT_SUCCESS); |
| 192 |
} |
| 193 |
else if ( (!strcmp(argv[i], "-v")) |
| 194 |
|| (!strcmp(argv[i], "--version")) ) |
| 195 |
{ |
| 196 |
fprintf(stdout, "netToe %s (%s)", |
| 197 |
PACKAGE_VERSION, RELEASE_DATE); |
| 198 |
#if USE_TERMINFO |
| 199 |
fprintf(stdout, " with terminfo support"); |
| 200 |
#elif USE_TERMCAP |
| 201 |
fprintf(stdout, " with termcap support"); |
| 202 |
#endif |
| 203 |
fprintf(stdout, ", at TCP port %d.", SERVER_PORT_NUMBER); |
| 204 |
fprintf(stdout, "\n\n"); |
| 205 |
fprintf(stdout, "Written by Gabriele Giorgetti %s\n", |
| 206 |
AUTHOR_EMAIL); |
| 207 |
fprintf(stdout, "Copyright 2000,2001 Gabriele Giorgetti\n"); |
| 208 |
fprintf(stdout, " 2009-2014 Mats Erik Andersson\n"); |
| 209 |
fprintf(stdout, "\n"); |
| 210 |
fprintf(stdout, "This software is released under GNU GPL 2.\n"); |
| 211 |
|
| 212 |
exit(EXIT_SUCCESS); |
| 213 |
} |
| 214 |
else if ( (!strcmp(argv[i], "-4")) |
| 215 |
|| (!strcmp(argv[i], "--ipv4")) ) |
| 216 |
{ |
| 217 |
addrfamily = AF_INET; |
| 218 |
continue; |
| 219 |
} |
| 220 |
else if ( (!strcmp(argv[i], "-6")) |
| 221 |
|| (!strcmp(argv[i], "--ipv6")) ) |
| 222 |
{ |
| 223 |
addrfamily = AF_INET6; |
| 224 |
continue; |
| 225 |
} |
| 226 |
else if ( (!strcmp(argv[i], "-nb")) |
| 227 |
|| (!strcmp(argv[i], "--no-beep")) ) |
| 228 |
{ |
| 229 |
NO_BEEP = 1; |
| 230 |
continue; |
| 231 |
} |
| 232 |
else if ( (!strcmp(argv[i], "-nc")) |
| 233 |
|| (!strcmp(argv[i], "--no-colors")) ) |
| 234 |
{ |
| 235 |
NO_COLORS = 1; |
| 236 |
continue; |
| 237 |
} else { |
| 238 |
fprintf(stdout, "%s: unrecognized option `%s'\n", |
| 239 |
argv[0], argv[i]); |
| 240 |
fprintf(stdout, "Try `%s --help' for more information.\n", |
| 241 |
argv[0]); |
| 242 |
|
| 243 |
exit (EXIT_SUCCESS); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return; |
| 248 |
} /* parse_cmd_args(int, char *[]) */ |
| 249 |
|
| 250 |
/* |
| 251 |
* vim: sw=4 ts=4 |
| 252 |
*/ |