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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
/*
* Crossfire -- cooperative multi-player graphical RPG and adventure game
*
* Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
* Copyright (c) 1992 Frank Tore Johansen
*
* Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
* welcome to redistribute it under certain conditions. For details, please
* see COPYING and LICENSE.
*
* The authors can be reached via e-mail at <crossfire@metalforge.org>.
*/
/**
* @file
* Contains a lot about the commands typed into the client.
*/
#include <ctype.h>
#include "client.h"
#include "external.h"
#include "p_cmd.h"
#include "script.h"
#include "mapdata.h"
/**
* @defgroup PCmdHelpCommands Common client player commands.
* @{
*/
#define H1(a) draw_ext_info(NDI_BLACK, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE, a)
#define H2(a) draw_ext_info(NDI_BLACK, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE, a)
#define LINE(a) draw_ext_info(NDI_BLACK, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE, a)
extern void config_check(void);
extern void map_check_resize(void);
/** If true, the next drawextinfo is expected to contain a map path. */
bool arm_mapedit = false;
/* TODO Help topics other than commands? Refer to other documents? */
static int get_num_commands(void);
int render_debug_layers = MAXLAYERS;
static void do_clienthelp_list() {
ConsoleCommand **sorted_cmds = get_cat_sorted_commands();
CommCat category = COMM_CAT_MISC;
GString *line = g_string_new(NULL);
H1("Client commands:");
for (int i = 0; i < get_num_commands(); i++) {
ConsoleCommand *cmd = sorted_cmds[i];
if (cmd->cat != category) {
// If moving on to next category, dump line_buf and print header.
char buf[MAX_BUF];
snprintf(buf, sizeof(buf), "%s commands:",
get_category_name(cmd->cat));
LINE(line->str);
H2(buf);
category = cmd->cat;
g_string_free(line, true);
line = g_string_new(NULL);
}
g_string_append_printf(line, "%s ", cmd->name);
}
LINE(line->str);
g_string_free(line, true);
}
static void show_help(const ConsoleCommand *cc) {
char buf[MAX_BUF];
if (cc->desc != NULL) {
snprintf(buf, MAX_BUF - 1, "%s - %s", cc->name, cc->desc);
} else {
snprintf(buf, MAX_BUF - 1, "Help for '%s':", cc->name);
}
H2(buf);
if (cc->helpfunc != NULL) {
const char *long_help = NULL;
long_help = cc->helpfunc();
if (long_help != NULL) {
LINE(long_help);
} else {
LINE("Extended help for this command is broken.");
}
} else {
LINE("No extended help is available for this command.");
}
}
static void command_help(const char *cpnext) {
if (cpnext) {
const ConsoleCommand * cc;
char buf[MAX_BUF];
cc = find_command(cpnext);
if (cc != NULL) {
show_help(cc);
} else {
snprintf(buf, sizeof(buf), "help %s", cpnext);
/* maybe not a must send, but we probably don't want to drop it */
send_command(buf, -1, 1);
}
} else {
do_clienthelp_list();
/* Now fetch (in theory) command list from the server.
TODO Protocol command - feed it to the tab completer.
Nope! It effectivey fetches '/help commands for commands'.
*/
send_command("help", -1, 1); /* TODO make install in server branch doesn't install def_help. */
}
}
static const char * help_help(void) {
return
"Syntax:\n"
"\n"
" help\n"
" help <topic>\n"
"\n"
"Without any arguments, displays a list of client-side "
"commands, and fetches the without-arguments help from "
"the server.\n"
"\n"
"With arguments, first checks if there's a client command "
"named <topic>. If there is, display it's help. If there "
"isn't, send the topic to the server.";
}
/**
* @} */ /* EndOf PCmdHelpCommands
*/
static void set_command_window(const char *cpnext) {
if (!cpnext) {
draw_ext_info(NDI_BLACK, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE,
"cwindow command requires a number parameter");
} else {
want_config[CONFIG_CWINDOW] = atoi(cpnext);
if (want_config[CONFIG_CWINDOW]<1 || want_config[CONFIG_CWINDOW]>127) {
want_config[CONFIG_CWINDOW]=COMMAND_WINDOW;
} else {
use_config[CONFIG_CWINDOW] = want_config[CONFIG_CWINDOW];
}
}
}
static void command_foodbeep() {
if (want_config[CONFIG_FOODBEEP]) {
want_config[CONFIG_FOODBEEP] = 0;
draw_ext_info(NDI_BLACK, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE,
"Warning bell when low on food disabled");
} else {
want_config[CONFIG_FOODBEEP] = 1;
draw_ext_info(NDI_BLACK, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE,
"Warning bell when low on food enabled");
}
use_config[CONFIG_FOODBEEP] = want_config[CONFIG_FOODBEEP];
}
const char * get_category_name(CommCat cat) {
const char * cat_name;
/* HACK Need to keep this in sync. with player.h */
switch(cat) {
case COMM_CAT_MISC:
cat_name = "Miscellaneous";
break;
case COMM_CAT_INFO:
cat_name = "Informational";
break;
case COMM_CAT_SETUP:
cat_name = "Configuration";
break;
case COMM_CAT_SCRIPT:
cat_name = "Scripting";
break;
case COMM_CAT_DEBUG:
cat_name = "Debugging";
break;
default:
cat_name = "PROGRAMMER ERROR";
break;
}
return cat_name;
}
/*
* Command table.
*
* Implementation basically stolen verbatim from the server.
*/
static void do_script_list() { script_list(); }
static void do_clearinfo() { menu_clear(); }
static void do_disconnect() { client_disconnect(); }
static void do_inv() { print_inventory(cpl.ob); }
static void do_magicmap() {
draw_magic_map();
}
static void do_savedefaults() { save_defaults(); }
static void do_savewinpos() { save_winpos(); }
static void do_mapedit() {
send_command("mapinfo", -1, 1);
arm_mapedit = true;
}
static void do_set_mapscale(const char *used) {
if (used != NULL) {
long long scale = strtoll(used, NULL, 0);
want_config[CONFIG_MAPSCALE] = scale;
config_check();
} else {
draw_ext_info(NDI_RED, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_CONFIG,
"mapscale command requires an argument");
}
}
static void do_take(const char *used) {
command_take("take", used); /* I dunno why they want it. */
}
/* Help "typecasters". */
#include "chelp.h"
static const char * help_bind(void) {
return HELP_BIND_LONG;
}
static const char * help_unbind(void) {
return HELP_UNBIND_LONG;
}
static const char * help_magicmap(void) {
return HELP_MAGICMAP_LONG;
}
static const char * help_inv(void) {
return HELP_INV_LONG;
}
static const char * help_cwindow(void) {
return
"Syntax:\n"
"\n"
" cwindow <val>\n"
"\n"
"set size of command"
"window (if val is exceeded"
"client won't send new"
"commands to server\n\n"
"(What does this mean, 'put a lid on it'?) TODO";
}
static const char * help_script(void) {
return
"Syntax: script <path>\n\n"
"Start an executable client script located at <path>. For details on "
"client-side scripting, please see the Crossfire Wiki.";
}
static const char * help_scripttell(void) {
return
"Syntax:\n"
"\n"
" scripttell <yourname> <data>\n"
"\n"
"?";
}
/* Toolkit-dependent. */
static const char * help_savewinpos(void) {
return
"Syntax:\n"
"\n"
" savewinpos\n"
"\n"
"save window positions - split windows mode only.";
}
static const char * help_scriptkill(void) {
return
"Syntax:\n"
"\n"
" scriptkill <name>\n"
"\n"
"Stop scripts named <name>.\n"
"(Not guaranteed to work?)";
}
static const char * help_scriptkillall(void) {
return
"Syntax:\n"
"\n"
" scriptkillall\n"
"\n"
"Stop all active scripts.\n"
"(Not guaranteed to work?)";
}
static void cmd_raw(const char *cmd) {
cs_print_string(csocket.fd, "%s", cmd);
}
void cmd_debugrender(const char *cmd) {
if (render_debug_layers == MAXLAYERS) {
render_debug_layers = 1;
} else {
render_debug_layers++;
}
char buf[MAX_BUF];
snprintf(buf, sizeof(buf), "Rendering %d layers.", render_debug_layers);
draw_ext_info(NDI_BROWN, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_DEBUG, buf);
}
static ConsoleCommand CommonCommands[] = {
{"cmd", COMM_CAT_DEBUG, cmd_raw, NULL, "Send a raw command to the server"},
{"debugrender", COMM_CAT_DEBUG, cmd_debugrender, NULL, "Enable renderer debugging"},
{"bind", COMM_CAT_SETUP, bind_key, help_bind, HELP_BIND_SHORT},
{"script", COMM_CAT_SCRIPT, script_init, help_script, NULL},
#ifdef HAVE_LUA
{"lua_load", COMM_CAT_SCRIPT, script_lua_load, NULL, NULL},
{"lua_list", COMM_CAT_SCRIPT, script_lua_list, NULL, NULL},
{"lua_kill", COMM_CAT_SCRIPT, script_lua_kill, NULL, NULL},
#endif
{"scripts", COMM_CAT_SCRIPT, do_script_list, NULL, "List running scripts"},
{"scriptkill", COMM_CAT_SCRIPT, script_kill, help_scriptkill, NULL},
{"scriptkillall", COMM_CAT_SCRIPT, script_killall_wrapper, help_scriptkillall, NULL},
{"scripttell", COMM_CAT_SCRIPT, script_tell, help_scripttell, NULL},
{"clearinfo", COMM_CAT_MISC, do_clearinfo, NULL, "Clear message window"},
{"cwindow", COMM_CAT_SETUP, set_command_window, help_cwindow, NULL},
{"disconnect", COMM_CAT_MISC, do_disconnect, NULL, NULL},
{"foodbeep", COMM_CAT_SETUP, command_foodbeep, NULL,
"toggle audible low on food warning"},
{"help", COMM_CAT_MISC, command_help, help_help, NULL},
{"inv", COMM_CAT_DEBUG, do_inv, help_inv, HELP_INV_SHORT},
{"magicmap", COMM_CAT_MISC, do_magicmap, help_magicmap,
HELP_MAGICMAP_SHORT},
{"mapedit", COMM_CAT_MISC, do_mapedit, NULL, NULL},
{"mapscale", COMM_CAT_MISC, do_set_mapscale, NULL, NULL},
{"savedefaults", COMM_CAT_SETUP, do_savedefaults, NULL,
HELP_SAVEDEFAULTS_SHORT},
{
"savewinpos", COMM_CAT_SETUP, do_savewinpos, help_savewinpos,
"Saves the position and sizes of windows." /* Panes? */
},
{"take", COMM_CAT_MISC, do_take, NULL, NULL},
{"unbind", COMM_CAT_SETUP, unbind_key, help_unbind, NULL},
{"show", COMM_CAT_SETUP, command_show, NULL,
"Change what items to show in inventory"},
};
const size_t num_commands = sizeof(CommonCommands) / sizeof(ConsoleCommand);
static int get_num_commands() {
return num_commands;
}
static ConsoleCommand ** name_sorted_commands;
static int sort_by_name(const void * a_, const void * b_) {
ConsoleCommand * a = *((ConsoleCommand **)a_);
ConsoleCommand * b = *((ConsoleCommand **)b_);
return strcmp(a->name, b->name);
}
static ConsoleCommand ** cat_sorted_commands;
/* Sort by category, then by name. */
static int sort_by_category(const void *a_, const void *b_) {
/* Typecasts, so it goes. */
ConsoleCommand * a = *((ConsoleCommand **)a_);
ConsoleCommand * b = *((ConsoleCommand **)b_);
if (a->cat == b->cat) {
return strcmp(a->name, b->name);
}
return a->cat - b->cat;
}
void init_commands() {
/* XXX Leak! */
name_sorted_commands = g_malloc(sizeof(ConsoleCommand *) * num_commands);
for (size_t i = 0; i < num_commands; i++) {
name_sorted_commands[i] = &CommonCommands[i];
}
/* Sort them. */
qsort(name_sorted_commands, num_commands, sizeof(ConsoleCommand *), sort_by_name);
/* Copy the list, then sort it by category. */
cat_sorted_commands = g_malloc(sizeof(ConsoleCommand *) * num_commands);
memcpy(cat_sorted_commands, name_sorted_commands, sizeof(ConsoleCommand *) * num_commands);
qsort(cat_sorted_commands, num_commands, sizeof(ConsoleCommand *), sort_by_category);
/* TODO Add to the list of tab-completion items. */
}
const ConsoleCommand * find_command(const char *cmd) {
ConsoleCommand ** asp_p = NULL, dummy;
ConsoleCommand * dummy_p;
ConsoleCommand * asp;
char *cp, *cmd_cpy;
cmd_cpy = g_strdup(cmd);
for (cp=cmd_cpy; *cp; cp++) {
*cp =tolower(*cp);
}
dummy.name = cmd_cpy;
dummy_p = &dummy;
asp_p = bsearch(
(void *)&dummy_p,
(void *)name_sorted_commands,
num_commands,
sizeof(ConsoleCommand *),
sort_by_name);
if (asp_p == NULL) {
free(cmd_cpy);
return NULL;
}
asp = *asp_p;
/* TODO The server's find_command() searches first the commands,
then the emotes. We might have to do something similar someday, too. */
/* if (asp == NULL) search something else? */
free(cmd_cpy);
return asp;
}
/**
* Returns a pointer to the head of an array of ConsoleCommands sorted by
* category, then by name. It's num_commands long.
*/
ConsoleCommand ** get_cat_sorted_commands(void) {
return cat_sorted_commands;
}
/**
* Tries to handle command cp (with optional params in cpnext, which may be
* null) as a local command. If this was a local command, returns true to
* indicate command was handled. This code was moved from extended_command so
* scripts ca issue local commands to handle keybindings or anything else.
*
* @param cp
* @param cpnext
*/
int handle_local_command(const char* cp, const char *cpnext) {
const ConsoleCommand * cc = NULL;
cc = find_command(cp);
if (cc == NULL) {
return FALSE;
}
if (cc->dofunc == NULL) {
char buf[MAX_BUF];
snprintf(buf, MAX_BUF - 1, "Client command %s has no implementation!", cc->name);
draw_ext_info(NDI_RED, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE, buf);
return FALSE;
}
cc->dofunc(cpnext);
return TRUE;
}
/**
* This is an extended command (ie, 'who, 'whatever, etc). In general, we
* just send the command to the server, but there are a few that we care about
* (bind, unbind)
*
* The command passed to us can not be modified - if it is a keybinding, we
* get passed the string that is that binding - modifying it effectively
* changes the binding.
*
* @param ocommand
*/
void extended_command(const char *ocommand) {
const char *cp = ocommand;
char *cpnext, command[MAX_BUF];
if ((cpnext = strchr(cp, ' '))!=NULL) {
int len = cpnext - ocommand;
if (len > (MAX_BUF -1 )) {
len = MAX_BUF-1;
}
strncpy(command, ocommand, len);
command[len] = '\0';
cp = command;
while (*cpnext == ' ') {
cpnext++;
}
if (*cpnext == 0) {
cpnext = NULL;
}
}
/*
* Try to prevent potential client hang by trying to delete a
* character when there is no character to delete.
* Thus, only send quit command if there is a player to delete.
*/
if (cpl.title[0] == '\0' && strcmp(cp, "quit") == 0){
// Bail here, there isn't anything this should be doing.
return;
}
/* cp now contains the command (everything before first space),
* and cpnext contains everything after that first space. cpnext
* could be NULL.
*/
#ifdef HAVE_LUA
if ( script_lua_command(cp, cpnext) ) {
return;
}
#endif
/* If this isn't a client-side command, send it to the server. */
if (!handle_local_command(cp, cpnext)) {
/* just send the command(s) (if `ocommand' is a compound command */
/* then split it and send each part separately */
/* TODO Remove this from the server; end of commands.c. */
strncpy(command, ocommand, MAX_BUF-1);
command[MAX_BUF-1]=0;
cp = strtok(command, ";");
while ( cp ) {
while( *cp == ' ' ) {
cp++;
} /* throw out leading spaces; server
does not like them */
send_command(cp, cpl.count, 0);
cp = strtok(NULL, ";");
}
}
}
/* ------------------------------------------------------------------ */
/* This list is used for the 'tab' completion, and nothing else.
* Therefore, if it is out of date, it isn't that terrible, but
* ideally it should stay somewhat up to date with regards to
* the commands the server supports.
*/
/* TODO Dynamically generate. */
static const char *const commands[] = {
"accuse", "afk", "apply", "applymode", "archs", "beg", "bleed", "blush",
"body", "bounce", "bow", "bowmode", "brace", "build", "burp", "cackle", "cast",
"chat", "chuckle", "clap", "cointoss", "cough", "cringe", "cry", "dance",
"disarm", "dm", "dmhide", "drop", "dropall", "east", "examine", "explore",
"fire", "fire_stop", "fix_me", "flip", "frown", "gasp", "get", "giggle",
"glare", "grin", "groan", "growl", "gsay", "help", "hiccup", "hiscore", "hug",
"inventory", "invoke", "killpets", "kiss", "laugh", "lick", "listen", "logs",
"mapinfo", "maps", "mark", "me", "motd", "nod", "north", "northeast",
"northwest", "orcknuckle", "output-count", "output-sync", "party", "peaceful",
"petmode", "pickup", "players", "poke", "pout", "prepare", "printlos", "puke",
"quests", "quit", "ready_skill", "rename", "reply", "resistances",
"rotateshoottype", "run", "run_stop", "save", "say", "scream", "search",
"search-items", "shake", "shiver", "shout", "showpets", "shrug", "shutdown",
"sigh", "skills", "slap", "smile", "smirk", "snap", "sneeze", "snicker",
"sniff", "snore", "sound", "south", "southeast", "southwest", "spit",
"statistics", "stay", "strings", "strut", "sulk", "take", "tell", "thank",
"think", "throw", "time", "title", "twiddle", "use_skill", "usekeys",
"version", "wave", "weather", "west", "whereabouts", "whereami", "whistle",
"who", "wimpy", "wink", "yawn",
};
const size_t num_server_commands = sizeof(commands) / sizeof(char *);
/**
* Player has entered 'command' and hit tab to complete it. See if we can
* find a completion. Returns matching command. Returns NULL if no command
* matches.
*
* @param command
*/
const char * complete_command(const char *command) {
int len, display = 0;
const char *match;
static char result[64];
char list[500];
len = strlen(command);
if (len == 0) {
return NULL;
}
strcpy(list, "Matching commands:");
/* TODO Partial match, e.g.:
If the completion list was:
wear
wet #?
If we type 'w' then hit tab, put in the e.
Basically part of bash (readline?)'s behaviour.
*/
match = NULL;
/* check server side commands */
for (size_t i = 0; i < num_server_commands; i++) {
if (!strncmp(command, commands[i], len)) {
if (display) {
snprintf(list + strlen(list), 499 - strlen(list), " %s", commands[i]);
} else if (match != NULL) {
display = 1;
snprintf(list + strlen(list), 499 - strlen(list), " %s %s", match, commands[i]);
match = NULL;
} else {
match = commands[i];
}
}
}
/* check client side commands */
for (size_t i = 0; i < num_commands; i++) {
if (!strncmp(command, CommonCommands[i].name, len)) {
if (display) {
snprintf(list + strlen(list), 499 - strlen(list), " %s", CommonCommands[i].name);
} else if (match != NULL) {
display = 1;
snprintf(list + strlen(list), 499 - strlen(list), " %s %s", match, CommonCommands[i].name);
match = NULL;
} else {
match = CommonCommands[i].name;
}
}
}
if (match == NULL) {
if (display) {
strncat(list, "\n", 499 - strlen(list));
draw_ext_info(
NDI_BLACK, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE, list);
} else
draw_ext_info(NDI_BLACK, MSG_TYPE_CLIENT, MSG_TYPE_CLIENT_NOTICE,
"No matching command.\n");
/* No match. */
return NULL;
}
/*
* Append a space to allow typing arguments. For commands without arguments
* the excess space should be stripped off automatically.
*/
snprintf(result, sizeof(result), "%s ", match);
return result;
}
|