cmd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * cmd.c
  3. * doom
  4. *
  5. * Created by John Carmack on 4/14/09.
  6. * Copyright 2009 id Software. All rights reserved.
  7. *
  8. */
  9. /*
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include "doomiphone.h"
  23. typedef struct cmd_function_s {
  24. struct cmd_function_s *next;
  25. const char *name;
  26. int hashid;
  27. xcommand_t function;
  28. } cmd_function_t;
  29. #define MAX_STRING_TOKENS 16
  30. #define MAX_STRING_CHARS 1024
  31. int cmd_argc;
  32. char *cmd_argv[ MAX_STRING_TOKENS ];
  33. cmd_function_t *cmd_functions; // possible commands to execute
  34. int Cmd_Argc( void ) {
  35. return cmd_argc;
  36. }
  37. const char *Cmd_Argv( int arg ) {
  38. if( arg >= cmd_argc ) {
  39. return "";
  40. }
  41. return cmd_argv[ arg ];
  42. }
  43. void Cmd_TokenizeString( const char *text ) {
  44. static char *stringCopy;
  45. // clear the args from the last string
  46. // This better not be called recursively...
  47. if ( stringCopy ) {
  48. free( stringCopy );
  49. stringCopy = NULL;
  50. }
  51. cmd_argc = 0;
  52. if( ! text ) {
  53. return;
  54. }
  55. stringCopy = strdup( text );
  56. char *strval = stringCopy;
  57. while( 1 ) {
  58. char *start = strsep( &strval," \t\r\n");
  59. if ( !start ) {
  60. break;
  61. }
  62. if ( start[0] != 0 ) {
  63. cmd_argv[cmd_argc] = start;
  64. if ( ++cmd_argc == MAX_STRING_TOKENS ) {
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. void Cmd_ListCommands_f() {
  71. for( cmd_function_t *cmd = cmd_functions ; cmd ; cmd = cmd->next ) {
  72. Com_Printf( "%s\n", cmd->name );
  73. }
  74. }
  75. void Cmd_AddCommand( const char *cmd_name, xcommand_t function ) {
  76. cmd_function_t *cmd;
  77. int hashid;
  78. hashid = HashString( cmd_name );
  79. // fail if the command already exists
  80. for( cmd = cmd_functions ; cmd ; cmd = cmd->next ) {
  81. if( hashid == cmd->hashid && !strcmp( cmd_name, cmd->name ) ) {
  82. Com_Printf( "Cmd_AddCommand: \"%s\" already defined\n", cmd_name );
  83. return;
  84. }
  85. }
  86. cmd = malloc( sizeof( cmd_function_t ) );
  87. cmd->name = cmd_name;
  88. cmd->hashid = hashid;
  89. cmd->function = function;
  90. cmd->next = cmd_functions;
  91. cmd_functions = cmd;
  92. }
  93. void Cmd_ExecuteString( const char *str ) {
  94. int l = strlen( str );
  95. if ( str[l-1] == '\n' ) {
  96. char *stripped = alloca( l+1 );
  97. strcpy( stripped, str );
  98. str = stripped;
  99. stripped[l-1] = 0;
  100. }
  101. Com_Printf( "%s\n", str );
  102. Cmd_TokenizeString( str );
  103. const char *arg0 = Cmd_Argv( 0 );
  104. int hashid = HashString( arg0 );
  105. // check commands first
  106. for( cmd_function_t *cmd = cmd_functions ; cmd ; cmd = cmd->next ) {
  107. if( hashid == cmd->hashid && !strcmp( arg0, cmd->name ) ) {
  108. cmd->function();
  109. return;
  110. }
  111. }
  112. // then check cvars
  113. cvar_t *cvar = Cvar_FindVar( arg0 );
  114. if ( cvar ) {
  115. Cvar_Set( arg0, Cmd_Argv( 1 ) );
  116. return;
  117. }
  118. Com_Printf( "Unknown command: %s\n", arg0 );
  119. }
  120. // execute each line of the config file
  121. void Cmd_ExecuteFile( const char *fullPathName ) {
  122. Com_Printf( "Executing command file '%s'\n", fullPathName );
  123. FILE *f = fopen( fullPathName, "rb" );
  124. if ( !f ) {
  125. Com_Printf( "Failed to open.\n" );
  126. return;
  127. }
  128. char line[1024];
  129. while( fgets( line, sizeof( line ), f ) ) {
  130. Cmd_ExecuteString( line );
  131. }
  132. fclose( f );
  133. }