Skip to content

Commit

Permalink
Add play_if_exist command
Browse files Browse the repository at this point in the history
  • Loading branch information
paxed committed Dec 7, 2015
1 parent 82176bc commit badc029
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config.l
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ cursor { return TYPE_CURSOR; }
"chroot_path" { return TYPE_PATH_CHROOT; }
"game_name" { return TYPE_NAME_GAME; }
"short_name" { return TYPE_GAME_SHORT_NAME; }
"game_id" { return TYPE_GAME_ID; }
"game_path" { return TYPE_PATH_GAME; }
"dglroot" { return TYPE_PATH_DGLDIR; }
"spooldir" { return TYPE_PATH_SPOOL; }
Expand Down Expand Up @@ -120,6 +121,7 @@ ask_login { yylval.i = DGLCMD_LOGIN; return TYPE_DGLCMD0; }
ask_register { yylval.i = DGLCMD_REGISTER; return TYPE_DGLCMD0; }
quit { yylval.i = DGLCMD_QUIT; return TYPE_DGLCMD0; }
play_game { yylval.i = DGLCMD_PLAYGAME; return TYPE_DGLCMD1; }
play_if_exist { yylval.i = DGLCMD_PLAY_IF_EXIST; return TYPE_DGLCMD2; }
submenu { yylval.i = DGLCMD_SUBMENU; return TYPE_DGLCMD1; }
return { yylval.i = DGLCMD_RETURN; return TYPE_DGLCMD0; }
rawprint { yylval.i = DGLCMD_RAWPRINT; return TYPE_DGLCMD1; }
Expand Down
11 changes: 10 additions & 1 deletion config.y
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static int sortmode_number(const char *sortmode_name) {

%token TYPE_SUSER TYPE_SGROUP TYPE_SGID TYPE_SUID TYPE_MAX TYPE_MAXNICKLEN
%token TYPE_GAME_SHORT_NAME TYPE_WATCH_SORTMODE TYPE_BANNERVARS
%token TYPE_ALLOW_REGISTRATION TYPE_WATCH_COLUMNS
%token TYPE_ALLOW_REGISTRATION TYPE_WATCH_COLUMNS TYPE_GAME_ID
%token TYPE_PATH_GAME TYPE_NAME_GAME TYPE_PATH_DGLDIR TYPE_PATH_SPOOL
%token TYPE_PATH_BANNER TYPE_PATH_CANNED TYPE_PATH_CHROOT
%token TYPE_PATH_PASSWD TYPE_PATH_LOCKFILE TYPE_PATH_TTYREC
Expand Down Expand Up @@ -496,6 +496,11 @@ game_definition : TYPE_CMDQUEUE
myconfig[ncnf]->shortname = strdup($3);
break;

case TYPE_GAME_ID:
if (myconfig[ncnf]->game_id) free (myconfig[ncnf]->game_id);
myconfig[ncnf]->game_id = strdup($3);
break;

case TYPE_RC_FMT:
if (myconfig[ncnf]->rc_fmt) free(myconfig[ncnf]->rc_fmt);
myconfig[ncnf]->rc_fmt = strdup($3);
Expand Down Expand Up @@ -575,6 +580,8 @@ definegame : TYPE_DEFINE_GAME '{'
}
game_definitions '}'
{
if (myconfig[ncnf]->game_id == NULL && myconfig[ncnf]->shortname)
myconfig[ncnf]->game_id = strdup(myconfig[ncnf]->shortname);
ncnf++;
num_games = ncnf;
}
Expand Down Expand Up @@ -644,6 +651,7 @@ KeyType : TYPE_SUSER { $$ = TYPE_SUSER; }
| TYPE_PATH_GAME { $$ = TYPE_PATH_GAME; }
| TYPE_NAME_GAME { $$ = TYPE_NAME_GAME; }
| TYPE_GAME_SHORT_NAME { $$ = TYPE_GAME_SHORT_NAME; }
| TYPE_GAME_ID { $$ = TYPE_GAME_ID; }
| TYPE_PATH_DGLDIR { $$ = TYPE_PATH_DGLDIR; }
| TYPE_PATH_SPOOL { $$ = TYPE_PATH_SPOOL; }
| TYPE_PATH_BANNER { $$ = TYPE_PATH_BANNER; }
Expand Down Expand Up @@ -680,6 +688,7 @@ const char* lookup_token (int t)
case TYPE_NAME_GAME: return "game_name";
case TYPE_ALLOW_REGISTRATION: return "allow_new_nicks";
case TYPE_GAME_SHORT_NAME: return "short_name";
case TYPE_GAME_ID: return "game_id";
case TYPE_PATH_DGLDIR: return "dglroot";
case TYPE_PATH_SPOOL: return "spooldir";
case TYPE_PATH_BANNER: return "banner";
Expand Down
2 changes: 2 additions & 0 deletions dgamelaunch.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ typedef enum
DGLCMD_CHMAIL, /* chmail */
DGLCMD_CHPASSWD, /* chpasswd */
DGLCMD_PLAYGAME, /* play_game "foo" */
DGLCMD_PLAY_IF_EXIST, /* play_if_exist "game" "file" */
DGLCMD_SUBMENU, /* submenu "foo" */
DGLCMD_RETURN /* return */
} dglcmd_actions;
Expand Down Expand Up @@ -198,6 +199,7 @@ struct dg_config
{
char* game_path;
char* game_name;
char* game_id;
char* shortname;
char* rcfile;
char* ttyrecdir;
Expand Down
17 changes: 15 additions & 2 deletions dgl-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct dg_config **myconfig = NULL;
struct dg_config defconfig = {
/* game_path = */ "/bin/nethack",
/* game_name = */ "NetHack",
/* game_id = */ NULL,
/* shortname = */ "NH",
/* rcfile = */ NULL, /*"/dgl-default-rcfile",*/
/* ttyrecdir =*/ "%ruserdata/%n/ttyrec/",
Expand Down Expand Up @@ -215,6 +216,7 @@ dgl_exec_cmdqueue(struct dg_cmdpart *queue, int game, struct dg_user *me)
struct dg_cmdpart *tmp = queue;
char *p1;
char *p2;
int played = 0;

if (!queue) return 1;

Expand Down Expand Up @@ -338,12 +340,22 @@ dgl_exec_cmdqueue(struct dg_cmdpart *queue, int game, struct dg_user *me)
case DGLCMD_RETURN:
return_from_submenu = 1;
break;
case DGLCMD_PLAY_IF_EXIST:
if (!(loggedin && me && p1 && p2)) break;
{
FILE *tmpfile;
tmpfile = fopen(p2, "r");
if (tmpfile) {
fclose(tmpfile);
} else break;
}
/* else fall through to playgame */
case DGLCMD_PLAYGAME:
if (loggedin && me && p1) {
if (loggedin && me && p1 && !played) {
int userchoice, i;
char *tmpstr;
for (userchoice = 0; userchoice < num_games; userchoice++) {
if (!strcmp(myconfig[userchoice]->game_name, p1) || !strcmp(myconfig[userchoice]->shortname, p1)) {
if (!strcmp(myconfig[userchoice]->game_id, p1) || !strcmp(myconfig[userchoice]->game_name, p1) || !strcmp(myconfig[userchoice]->shortname, p1)) {
if (purge_stale_locks(userchoice)) {
if (myconfig[userchoice]->rcfile) {
if (access (dgl_format_str(userchoice, me, myconfig[userchoice]->rc_fmt, NULL), R_OK) == -1)
Expand Down Expand Up @@ -378,6 +390,7 @@ dgl_exec_cmdqueue(struct dg_cmdpart *queue, int game, struct dg_user *me)
dgl_format_str(userchoice, me, myconfig[userchoice]->ttyrecdir, NULL),
gen_ttyrec_filename());
idle_alarm_set_enabled(1);
played = 1;
/* lastly, run the generic "do these when a game is left" commands */
signal (SIGHUP, catch_sighup);
signal (SIGINT, catch_sighup);
Expand Down
5 changes: 5 additions & 0 deletions examples/dgamelaunch.conf
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ default_term = "xterm"
# registration of new nicks is allowed.
# play_game "foo" = start game which has the short name "foo"
# (user must be logged in)
# play_if_exist "foo" "file" = start game "foo", if file "file" exists.
# submenu "foo" = go to submenu "foo"
# return = return from submenu
#
Expand Down Expand Up @@ -260,6 +261,10 @@ menu["watchmenu_help"] {
# # Short name, used in the watching menu
# short_name = "NHstb"
#
# # Game ID - should be unique. Defaults to game_name, if not defined.
# # Used to determine which game is which for "play_game" and "play_if_exists" commands
# game_id = "NHstb"
#
# # arguments for when we exec the binary
# game_args = "/bin/nethackstub",
# "foo",
Expand Down

0 comments on commit badc029

Please sign in to comment.