//---------------------------------------------------------------------------- // EDGE Console Main //---------------------------------------------------------------------------- // // Copyright (c) 1999-2005 The EDGE Team. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // //---------------------------------------------------------------------------- #include "i_defs.h" #include "con_main.h" #include "con_defs.h" #include "g_game.h" #include "gui_gui.h" #include "gui_ctls.h" #include "m_menu.h" #include "s_sound.h" #include "w_wad.h" #include "z_zone.h" #include "epi/math_crc.h" #include #include #include #include #include #define SCREENROWS 100 #define SCREENCOLS 80 #define BACKBUFFER 10 typedef struct { char *name; int flags; int (*cmd) (const char *arg); } con_cmd_t; int CON_CMDToggleMouse(const char *args); int CON_CMDHelloWorld(const char *args); int CON_CMDEat(const char *args); int CON_CMDExec(const char *args); int CON_CMDArgText(const char *args); int CON_CMDType(const char *args); int CON_CMDTypeOf(const char *args); int CON_CMDScreenShot(const char *args); int CON_CMDSet(const char *args); int CON_CMDWatch(const char *args); int CON_CMDQuitEDGE(const char *args); int CON_CMDCrc(const char *args); int CON_CMDPlaySound(const char *args); bool CON_CMDHelloWorldResponder(gui_t * gui, guievent_t * ev); // Current console commands. Needs extending badly. // 'Builtin' commands should be added here // TODO: add another file (i_exec.c) that can load in // 'external' commands. On a real operating system, // these 'external' commands could be loaded in using // dynamic linking, on DOS they must be linked statically. con_cmd_t consolecommands[] = { {"args", 0, CON_CMDArgText}, {"crc", 0, CON_CMDCrc}, {"playsound", 0, CON_CMDPlaySound}, {"eat", 0, CON_CMDEat}, {"exec", 0, CON_CMDExec}, {"mouse", 0, CON_CMDToggleMouse}, {"screenshot", 0, CON_CMDScreenShot}, {"set", 0, CON_CMDSet}, {"test", 0, CON_CMDHelloWorld}, {"type", 0, CON_CMDType}, {"typeof", 0, CON_CMDTypeOf}, {"quit", 0, CON_CMDQuitEDGE}, {"watch", 0, CON_CMDWatch}, }; static int GetArgs(const char *args, int argc, char **argv) { int i, j, k = 0, m; const int len = (const int)strlen(args); char *s; int quote; // skip any leading spaces for (i = 0; args[i] == ' '; i++) ; for (; i < len; i = j) { // find end of arg if (args[i] == '\"') { quote = 1; i++; for (j = i; args[j] != '\"' && j < len; j++) ; } else { quote = 0; for (j = i; args[j] != ' ' && j < len; j++) ; } s = Z_New(char, j - i + 1); for (m = 0; m < j - i; m++) { // -ES- 1999/07/25 Convert to lowercase, to avoid case sensitivity s[m] = tolower(args[i + m]); } s[j - i] = '\0'; argv[k] = s; if (++k == argc) break; if (quote) // skip the ending quote j++; // skip whitespace until the next arg while (args[j] == ' ' && j < len) j++; } return k; } static void KillArgs(int argc, char *(argv[])) { int i; for (i = 0; i < argc; i++) Z_Free(argv[i]); } void CON_TryCommand(const char *cmd) { int i, j, e; char *s; const char *c; while (isspace(*cmd)) cmd++; if (strlen(cmd) == 0) return; for (i = sizeof(consolecommands) / sizeof(consolecommands[0]); i--;) { s = consolecommands[i].name; c = cmd; for (j = strlen(consolecommands[i].name); j--; s++, c++) if (*s != *c) break; if (j == -1 && (!*c || *c == ' ')) { e = consolecommands[i].cmd(cmd); if (e) CON_Printf("Error %d\n", e); return; } } CON_Printf("Bad Command.\n"); } int CON_CMDToggleMouse(const char *args) { bool vis = !GUI_MainGetMouseVisibility(); GUI_MainSetMouseVisibility(vis); CON_Printf("Mouse: %s\n", vis ? "on" : "off"); return 0; } bool CON_CMDHelloWorldResponder(gui_t * gui, guievent_t * ev) { switch (ev->type) { case 112: GUI_Destroy(gui); return true; default: return false; } } int CON_CMDHelloWorld(const char *args) { gui_t *gui = Z_ClearNew(gui_t, 1); gui->Responder = &CON_CMDHelloWorldResponder; GUI_Start(GUI_NULL(), gui); gui->process = GUI_MSGStart(GUI_NULL(), gui, 112, 0, "Hello World"); return 0; } int CON_CMDExec(const char *args) { FILE *script; int argc; char *argv[2]; char buffer[SCREENCOLS]; argc = GetArgs(args, 2, argv); if (!argc) return 1; if (argc != 2) { CON_Printf("Usage: exec