1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
/*************************************************************************
* parse the addgame command, and store in a database for later lookup
*************************************************************************/
/* Copyright 1998,1999,2000 Drake Diedrich
Distributed under the GNU General Public License.
See file COPYING for details. */
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "lafe.h"
struct game *game=NULL;
int games=0;
static char *token(char **str) {
char *t=NULL;
int i;
while (**str && isspace(**str)) (*str)++;
if (! **str) return NULL;
if (**str=='"') {
for (i=0;(*str)[i]!='"' && (*str)[i];i++) ;
} else {
for (i=0;!isspace( (*str)[i] ) && (*str)[i];i++) ;
}
t = malloc(i+1);
strncpy(t,*str,i);
t[i]=0;
(*str) += i;
if (**str) (*str)++;
return t;
}
void addgame(char *arg) {
if (!game) {
game=malloc(sizeof(*game));
} else {
game = realloc(game,(games+1)*sizeof(*game));
}
memset(game+games,0,sizeof(*game));
game[games].name = token(&arg);
if (game[games].name) {
game[games].country = token(&arg);
game[games].password = token(&arg);
game[games].host = token(&arg);
game[games].port = token(&arg);
game[games].directory = expand_tilde(token(&arg));
game[games].logfile = expand_tilde(token(&arg));
if (game[games].country && game[games].password &&
game[games].host && game[games].port) {
games++;
} else {
printf("addgame requires country password host and port parameters\n");
}
} else {
int i;
for (i=0;i<games;i++) {
printf("%s %s/%s %s/%s",game[i].name,game[i].country,
game[i].password,game[i].host,game[i].port);
if (game[i].directory) printf(" %s",game[i].directory);
else printf(" .");
if (game[i].logfile) printf(" %s",game[i].logfile);
else printf(" /dev/null");
printf("\n");
}
}
}
int lookup_game(char *name) {
int i;
for (i=0;i<games;i++) {
if (!strcmp(name,game[i].name)) return i;
}
return -1;
}
|