]> git.pond.sub.org Git - empserver/blob - src/lib/player/dispatch.c
937b12f1bfdee8fdfded8638efd906b5b2e5a508
[empserver] / src / lib / player / dispatch.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  dispatch.c: Actually execute the command given
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1994
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2007-2012
33  */
34
35 #include <config.h>
36
37 #include "com.h"
38 #include "empio.h"
39 #include "file.h"
40 #include "journal.h"
41 #include "match.h"
42 #include "misc.h"
43 #include "nat.h"
44 #include "optlist.h"
45 #include "player.h"
46 #include "prototypes.h"
47 #include "server.h"
48
49 /*
50  * Execute command named by player->argp[0].
51  * BUF is the raw UTF-8 command line.  It should have been passed to
52  * parse() to set up player->argp.
53  * If REDIR is not null, it's the command's redirection, in UTF-8.
54  * Return -1 if the command is not unique or doesn't exist, else 0.
55  */
56 int
57 dispatch(char *buf, char *redir)
58 {
59     struct natstr *np;
60     struct cmndstr *command;
61     int cmd;
62
63     cmd = comtch(player->argp[0], player_coms, player->nstat);
64     if (cmd < 0) {
65         if (cmd == M_NOTUNIQUE)
66             pr("Command \"%s\" is ambiguous -- ", player->argp[0]);
67         else if (cmd == M_IGNORE)
68             return 0;
69         else
70             pr("\"%s\" is not a legal command\n", player->argp[0]);
71         return -1;
72     }
73     command = &player_coms[cmd];
74     np = getnatp(player->cnum);
75     if (np->nat_btu < command->c_cost && command->c_cost > 0) {
76         if (player->god || opt_BLITZ)
77             np->nat_btu = max_btus;
78         else {
79             pr("You don't have the BTU's, bozo\n");
80             return 0;
81         }
82     }
83     if (!command->c_addr) {
84         pr("Command not implemented\n");
85         return 0;
86     }
87     player->may_sleep = command->c_flags & C_MOD
88         ? PLAYER_SLEEP_ON_INPUT : PLAYER_SLEEP_FREELY;
89     player->command = command;
90     empth_rwlock_rdlock(update_lock);
91     if (redir) {
92         prredir(redir);
93         uprnf(buf);
94         pr("\n");
95     }
96     journal_command(command->c_form);
97     switch (command->c_addr()) {
98     case RET_OK:
99         player->btused += command->c_cost;
100         break;
101     case RET_FAIL:
102         pr("command failed\n");
103         player->btused += command->c_cost;
104         break;
105     case RET_SYN:
106         pr("Usage: %s\n", command->c_form);
107         break;
108     default:
109         CANT_REACH();
110         break;
111     }
112     empth_rwlock_unlock(update_lock);
113     player->command = NULL;
114     if (player->may_sleep != PLAYER_SLEEP_NEVER || !io_eof(player->iop))
115         player->may_sleep = PLAYER_SLEEP_FREELY;
116     /* else we're being kicked out */
117     return 0;
118 }