[go: up one dir, main page]

Menu

[904618]: / src / m_argv.c  Maximize  Restore  History

Download this file

222 lines (196 with data), 6.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
// ----------------------------------------------------------------------------
// :oCCCCOCoc.
// .cCO8OOOOOOOOO8Oo:
// .oOO8OOOOOOOOOOOOOOOCc
// cO8888: .:oOOOOC. TM
// :888888: :CCCc .oOOOOC. ### ### #########
// C888888: .ooo: .C######## ##### ##### ###### ###### ##########
// O888888: .oO### ### ##### ##### ######## ######## #### ###
// C888888: :8O. .C########## ### #### ### ## ## ## ## #### ###
// :8@@@@8: :888c o### ### #### ### ######## ######## ##########
// :8@@@@C C@@@@ oo######## ### ## ### ###### ###### #########
// cO@@@@@@@@@@@@@@@@@Oc0
// :oO8@@@@@@@@@@Oo.
// .oCOOOOOCc. http://remood.org/
// ----------------------------------------------------------------------------
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 1998-2000 by DooM Legacy Team.
// Copyright (C) 2008-2013 GhostlyDeath <ghostlydeath@remood.org>
// <ghostlydeath@gmail.com>
// ----------------------------------------------------------------------------
// 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 3
// 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.
// ----------------------------------------------------------------------------
// DESCRIPTION:
#include "doomtype.h"
#include "console.h"
int myargc;
char** myargv;
static int found;
//
// M_CheckParm
// Checks for the given parameter
// in the program's command line arguments.
// Returns the argument number (1 to argc-1)
// or 0 if not present
int M_CheckParm(char* check)
{
int i;
for (i = 1; i < myargc; i++)
{
if (!strcasecmp(check, myargv[i]))
{
found = i;
return i;
}
}
found = 0;
return 0;
}
// return true if there is available parameters
bool_t M_IsNextParm(void)
{
if (found > 0 && found + 1 < myargc && myargv[found + 1][0] != '-' && myargv[found + 1][0] != '+')
return true;
return false;
}
// return the next parameter after a M_CheckParm
// NULL if not found use M_IsNext to find if there is a parameter
char* M_GetNextParm(void)
{
if (M_IsNextParm())
{
found++;
return myargv[found];
}
return NULL;
}
/* M_PushSpecialParametersAsOne() -- To prevent code repeation between M_PushSpecialParameters and M_PushSpecialPlusParameters */
void M_PushSpecialParametersAsOne(const bool_t plusplus)
{
#define BUFSIZE 256
int i;
char s[BUFSIZE];
bool_t onetime = false;
for (i = 1; i < myargc; i++)
{
if ((!plusplus && (myargv[i][0] == '+' && myargv[i][1] != '+')) || (plusplus && (myargv[i][0] == '+' && myargv[i][1] == '+')))
{
if (plusplus)
strncpy(s, &myargv[i][2], BUFSIZE);
else
strncpy(s, &myargv[i][1], BUFSIZE);
i++;
// get the parameter of the command too
for (; i < myargc && myargv[i][0] != '+' && myargv[i][0] != '-'; i++)
{
strncat(s, " ", BUFSIZE);
strncat(s, "\"", BUFSIZE);
strncat(s, myargv[i], BUFSIZE);
strncat(s, "\"", BUFSIZE);
}
strncat(s, "\n", BUFSIZE);
// push it
#if 0
if (devparm)
{
if (plusplus)
CONL_PrintFUL(SRCSTR__M_ARGV_C__EXECPP, L"%s", s);
else
CONL_PrintFUL(SRCSTR__M_ARGV_C__EXECP, L"%s", s);
}
#endif
// GhostlyDeath <May 9, 2012> -- Execute on new console instead
CONL_InputF("%s", s);
i--;
}
}
#undef BUFSIZE
}
/* M_PushSpecialParameters() -- Push all + parameters */
void M_PushSpecialParameters(void)
{
M_PushSpecialParametersAsOne(false);
}
/* M_PushSpecialPlusParameters() -- Push all ++ parameters */
void M_PushSpecialPlusParameters(void)
{
M_PushSpecialParametersAsOne(true);
}
//
// Find a Response File
//
void M_FindResponseFile(void)
{
int i;
#define MAXARGVS 256
for (i = 1; i < myargc; i++)
if (myargv[i][0] == '@')
{
FILE* handle;
int size;
int k;
int index;
int indexinfile;
bool_t inquote = false;
uint8_t* infile;
char* file;
char* moreargs[20];
char* firstargv;
// READ THE RESPONSE FILE INTO MEMORY
handle = fopen(&myargv[i][1], "rb");
if (!handle)
{
I_Error("\nResponse file %s not found !", &myargv[i][1]);
exit(1);
}
CONL_PrintF("Found response file %s!\n", &myargv[i][1]);
fseek(handle, 0, SEEK_END);
size = ftell(handle);
fseek(handle, 0, SEEK_SET);
file = malloc(size);
fread(file, size, 1, handle);
fclose(handle);
// KEEP ALL CMDLINE ARGS FOLLOWING @RESPONSEFILE ARG
for (index = 0, k = i + 1; k < myargc; k++)
moreargs[index++] = myargv[k];
firstargv = myargv[0];
myargv = malloc(sizeof(char*) * MAXARGVS);
if (!myargv)
I_Error("no enought memory");
memset(myargv, 0, sizeof(char*) * MAXARGVS);
myargv[0] = firstargv;
infile = file;
indexinfile = k = 0;
indexinfile++; // SKIP PAST ARGV[0] (KEEP IT)
do
{
inquote = infile[k] == '"';
if (inquote) // strip encllosing double-quote
k++;
myargv[indexinfile++] = &infile[k];
while (k < size && ((inquote && infile[k] != '"') || (!inquote && infile[k] > ' ')))
k++;
infile[k] = 0;
while (k < size && (infile[k] <= ' '))
k++;
}
while (k < size);
for (k = 0; k < index; k++)
myargv[indexinfile++] = moreargs[k];
myargc = indexinfile;
// DISPLAY ARGS
CONL_PrintF("%d command-line args:\n", myargc);
for (k = 1; k < myargc; k++)
CONL_PrintF("%s\n", myargv[k]);
break;
}
}