/legacy/trunk/util/m_argv.cpp

# · C++ · 176 lines · 110 code · 23 blank · 43 comment · 32 complexity · 555ad65d3306c3013f5b28e305eafe1a MD5 · raw file

  1. // Emacs style mode select -*- C++ -*-
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id: m_argv.cpp 508 2007-12-18 21:00:41Z jussip $
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. // Copyright (C) 1998-2004 by DooM Legacy Team.
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public License
  11. // as published by the Free Software Foundation; either version 2
  12. // of the License, or (at your option) any later version.
  13. //
  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. //
  19. //
  20. //-----------------------------------------------------------------------------
  21. /// \file
  22. /// \brief Commandline argument processing
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "doomdef.h"
  26. #include "doomtype.h"
  27. #include "command.h"
  28. int myargc;
  29. char** myargv;
  30. static int found = 0;
  31. //
  32. // M_CheckParm
  33. // Checks for the given parameter
  34. // in the program's command line arguments.
  35. // Returns the argument number (1 to argc-1)
  36. // or 0 if not present
  37. int M_CheckParm(const char *check)
  38. {
  39. int i;
  40. for (i = 1;i<myargc;i++)
  41. {
  42. if ( !strcasecmp(check, myargv[i]) )
  43. {
  44. found = i;
  45. return i;
  46. }
  47. }
  48. found = 0;
  49. return 0;
  50. }
  51. // return true if there is available parameters
  52. // called after M_CheckParm
  53. bool M_IsNextParm()
  54. {
  55. if(found > 0 && found+1 < myargc && myargv[found+1][0] != '-' && myargv[found+1][0] != '+')
  56. return true;
  57. return false;
  58. }
  59. // return the next parameter after a M_CheckParm
  60. // NULL if not found use M_IsNext to find if there is a parameter
  61. char *M_GetNextParm()
  62. {
  63. if(M_IsNextParm())
  64. {
  65. found++;
  66. return myargv[found];
  67. }
  68. return NULL;
  69. }
  70. // push all parameters begining by '+'
  71. void M_PushSpecialParameters()
  72. {
  73. int i;
  74. char s[256];
  75. bool onetime=false;
  76. for (i = 1;i<myargc;i++)
  77. {
  78. if ( myargv[i][0]=='+' )
  79. {
  80. strcpy(s,&myargv[i][1]);
  81. i++;
  82. // get the parameter of the command too
  83. for(;i<myargc && myargv[i][0]!='+' && myargv[i][0]!='-' ;i++)
  84. {
  85. strcat(s," ");
  86. if(!onetime) { strcat(s,"\"");onetime=true; }
  87. strcat(s,myargv[i]);
  88. }
  89. if( onetime ) { strcat(s,"\"");onetime=false; }
  90. strcat(s,"\n");
  91. // push it
  92. COM.AppendText(s);
  93. i--;
  94. }
  95. }
  96. }
  97. //
  98. // Find a Response File
  99. //
  100. void M_FindResponseFile()
  101. {
  102. #define MAXARGVS 256
  103. int i;
  104. for (i = 1;i < myargc;i++)
  105. if (myargv[i][0] == '@') {
  106. FILE *handle;
  107. int size;
  108. int j, k;
  109. int indexinfile;
  110. bool inquote = false;
  111. byte *infile;
  112. //char *file;
  113. char *moreargs[20];
  114. char *firstargv;
  115. // READ THE RESPONSE FILE INTO MEMORY
  116. handle = fopen (&myargv[i][1],"rb");
  117. if (handle == NULL) I_Error ("\nResponse file %s not found !",&myargv[i][1]);
  118. CONS_Printf("Found response file %s!\n",&myargv[i][1]);
  119. fseek (handle, 0, SEEK_END);
  120. size = ftell(handle);
  121. fseek (handle, 0, SEEK_SET);
  122. infile = (byte *)malloc(size);
  123. fread (infile, size, 1, handle);
  124. fclose (handle);
  125. // KEEP ALL CMDLINE ARGS FOLLOWING @RESPONSEFILE ARG
  126. for (j = 0, k = i+1; k < myargc; k++)
  127. moreargs[j++] = myargv[k];
  128. firstargv = myargv[0];
  129. myargv = (char **)malloc(sizeof(char *) * MAXARGVS);
  130. if(myargv == NULL) I_Error("\nNot enough memory for cmdline args");
  131. memset(myargv, 0, sizeof(char *) * MAXARGVS);
  132. myargv[0] = firstargv;
  133. //infile = file;
  134. indexinfile = k = 0;
  135. indexinfile++; // SKIP PAST ARGV[0] (KEEP IT)
  136. do {
  137. inquote = infile[k] == '"';
  138. if (inquote) k++; // strip enclosing double-quote
  139. myargv[indexinfile++] = (char *)&infile[k];
  140. while (k < size && ((inquote && infile[k]!='"') || (!inquote && infile[k] > ' '))) k++;
  141. infile[k] = 0; // can cause crash (responsefile with size 0 or just one ")
  142. while(k < size && (infile[k] <= ' ')) k++;
  143. } while(k < size);
  144. for (k = 0; k < j; k++) myargv[indexinfile++] = moreargs[k];
  145. myargc = indexinfile;
  146. // DISPLAY ARGS
  147. CONS_Printf("%d command-line args:\n", myargc);
  148. for (k = 1; k < myargc; k++)
  149. CONS_Printf("%s\n",myargv[k]);
  150. break;
  151. }
  152. }