| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* Copyright (C) 1999 by |
| 8 |
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman |
| 9 |
* Copyright (C) 1999-2000 by |
| 10 |
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze |
| 11 |
* Copyright 2005, 2006 by |
| 12 |
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or |
| 15 |
* modify it under the terms of the GNU General Public License |
| 16 |
* as published by the Free Software Foundation; either version 2 |
| 17 |
* of the License, or (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program; if not, write to the Free Software |
| 26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
* 02111-1307, USA. |
| 28 |
* |
| 29 |
* DESCRIPTION: |
| 30 |
* Some argument handling. |
| 31 |
* |
| 32 |
*-----------------------------------------------------------------------------*/ |
| 33 |
|
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include "doomtype.h" |
| 37 |
|
| 38 |
#include "m_argv.h" |
| 39 |
|
| 40 |
// |
| 41 |
// M_CheckParmEx |
| 42 |
// Checks for the given parameter in the given params list. |
| 43 |
// Returns the argument number (0 to paramscount-1) or -1 if not present |
| 44 |
// |
| 45 |
|
| 46 |
int M_CheckParmEx(const char *check, char **params, int paramscount) |
| 47 |
{ |
| 48 |
if (paramscount > 0 && check && params && *params) |
| 49 |
{ |
| 50 |
while (--paramscount >= 0) |
| 51 |
{ |
| 52 |
if (!strcasecmp(check, params[paramscount])) |
| 53 |
{ |
| 54 |
return paramscount; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
// |
| 63 |
// M_ParseCmdLine |
| 64 |
// |
| 65 |
// Parses the command line and sets up the argv[] array. |
| 66 |
// On entry, cmdstart should point to the command line, |
| 67 |
// argv should point to memory for the argv array, args |
| 68 |
// points to memory to place the text of the arguments. |
| 69 |
// If these are NULL, then no storing (only counting) |
| 70 |
// is done. On exit, *numargs has the number of |
| 71 |
// arguments (plus one for a final NULL argument), |
| 72 |
// and *numchars has the number of bytes used in the buffer |
| 73 |
// pointed to by args. |
| 74 |
// |
| 75 |
// Entry: |
| 76 |
// char *cmdstart - pointer to command line |
| 77 |
// char **argv - where to build argv array; NULL means don't build array |
| 78 |
// char *args - where to place argument text; NULL means don't store text |
| 79 |
// |
| 80 |
// Exit: |
| 81 |
// no return value |
| 82 |
// int *numargs - returns number of argv entries created |
| 83 |
// int *numchars - number of characters used in args buffer |
| 84 |
// |
| 85 |
|
| 86 |
void M_ParseCmdLine(char *cmdstart, char **argv, char *args, int *numargs, int *numchars) |
| 87 |
{ |
| 88 |
#define IsSpace(c) ((c) == 0x20 || ((c) >= 0x09 && (c) <= 0x0D)) |
| 89 |
|
| 90 |
char *p; |
| 91 |
int inquote; /* 1 = inside quotes */ |
| 92 |
int copychar; /* 1 = copy char to *args */ |
| 93 |
unsigned numslash; /* num of backslashes seen */ |
| 94 |
|
| 95 |
*numchars = 0; |
| 96 |
*numargs = 0; |
| 97 |
|
| 98 |
p = cmdstart; |
| 99 |
|
| 100 |
inquote = 0; |
| 101 |
|
| 102 |
/* loop on each argument */ |
| 103 |
for(;;) { |
| 104 |
|
| 105 |
while (IsSpace((int)*p)) ++p; |
| 106 |
|
| 107 |
if (*p == '\0')break; /* end of args */ |
| 108 |
|
| 109 |
/* scan an argument */ |
| 110 |
if (argv) *argv++ = args; /* store ptr to arg */ |
| 111 |
++*numargs; |
| 112 |
|
| 113 |
/* loop through scanning one argument */ |
| 114 |
for (;;) { |
| 115 |
copychar = 1; |
| 116 |
/* Rules: 2N backslashes + " ==> N backslashes and begin/end quote |
| 117 |
2N+1 backslashes + " ==> N backslashes + literal " |
| 118 |
N backslashes ==> N backslashes */ |
| 119 |
numslash = 0; |
| 120 |
while (*p == '\\') { /* count number of backslashes for use below */ |
| 121 |
++p; |
| 122 |
++numslash; |
| 123 |
} |
| 124 |
if (*p == '\"') { |
| 125 |
/* if 2N backslashes before, start/end quote, otherwise |
| 126 |
copy literally */ |
| 127 |
if ((numslash & 1) == 0) { |
| 128 |
if (inquote) { |
| 129 |
if (p[1] == '\"') |
| 130 |
p++; /* Double quote inside quoted string */ |
| 131 |
else /* skip first quote char and copy second */ |
| 132 |
copychar = 0; |
| 133 |
} else copychar = 0; /* don't copy quote */ |
| 134 |
inquote = !inquote; |
| 135 |
} |
| 136 |
numslash >>= 1; /* divide numslash by two */ |
| 137 |
} |
| 138 |
|
| 139 |
/* copy slashes */ |
| 140 |
while (numslash--) { |
| 141 |
if (args) *args++ = '\\'; |
| 142 |
++*numchars; |
| 143 |
} |
| 144 |
|
| 145 |
/* if at end of arg, break loop */ |
| 146 |
if (*p == '\0' || (!inquote && IsSpace((int)*p))) break; |
| 147 |
|
| 148 |
/* copy character into argument */ |
| 149 |
if (copychar) { |
| 150 |
if (args) *args++ = *p; |
| 151 |
++*numchars; |
| 152 |
} |
| 153 |
++p; |
| 154 |
} |
| 155 |
|
| 156 |
/* null-terminate the argument */ |
| 157 |
|
| 158 |
if (args) *args++ = '\0'; /* terminate string */ |
| 159 |
++*numchars; |
| 160 |
} |
| 161 |
} |