cmd.c 3.5 KB

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