The R Project SVN R

Rev

Rev 87852 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
29748 ripley 1
/*
42307 ripley 2
 *  R : A Computer Language for Statistical Data Analysis
84466 ripley 3
 *  Copyright (C) 1997-2023   The R Core Team
42307 ripley 4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, a copy is available at
68947 ripley 17
 *  https://www.R-project.org/Licenses/
29748 ripley 18
 */
19
 
20
#ifdef HAVE_CONFIG_H
21
#include <config.h>
22
#endif
23
 
42398 ripley 24
#include <string.h>
25
 
31534 ripley 26
#include <Defn.h>
31536 ripley 27
#include <R_ext/RStartup.h>
29748 ripley 28
 
29
 
30
/* Remove and process common command-line arguments
31
 *  Formally part of ../unix/sys-common.c.
32
 */
33
 
34
/*
35
  This copies the command line arguments to the Rstart
36
  structure. The memory is obtained from calloc, etc.
37
  since these are permanent and it is not intended that
38
  they be modified. This is why they are copied before
39
  being processed and removed from the list.
40
 
41
  We might store these as a SEXP. I have no strong opinion
42
  about this.
43
 */
44
 
45
/* Permanent copy of the command line arguments and the number
46
   of them passed to the application.
47
   These are populated via the routine R_set_command_line_arguments().
48
*/
49
static int    NumCommandLineArgs = 0;
78434 ripley 50
static char **CommandLineArgs = NULL; // this does not get freed
29748 ripley 51
 
52
 
53
void
54
R_set_command_line_arguments(int argc, char **argv)
55
{
78525 ripley 56
    // nothing here is ever freed.
29748 ripley 57
    NumCommandLineArgs = argc;
55420 luke 58
    CommandLineArgs = (char**) calloc((size_t) argc, sizeof(char*));
78434 ripley 59
    if(CommandLineArgs == NULL)
60
	R_Suicide("allocation failure in R_set_command_line_arguments");
29748 ripley 61
 
78525 ripley 62
    for(int i = 0; i < argc; i++) {
63
	CommandLineArgs[i] = strdup(argv[i]);
64
	if(CommandLineArgs[i] == NULL)
65
	    R_Suicide("allocation failure in R_set_command_line_arguments");
66
    }
29748 ripley 67
}
68
 
69
 
70
/*
71
  The .Internal which returns the command line arguments that are stored
72
  in global variables.
73
 */
83446 ripley 74
attribute_hidden
75
SEXP do_commandArgs(SEXP call, SEXP op, SEXP args, SEXP env)
29748 ripley 76
{
77
    int i;
78
    SEXP vals;
79
 
69326 luke 80
    checkArity(op, args);
35691 ripley 81
    /* need protection as mkChar allocates */
82
    vals = PROTECT(allocVector(STRSXP, NumCommandLineArgs));
29748 ripley 83
    for(i = 0; i < NumCommandLineArgs; i++)
84
	SET_STRING_ELT(vals, i, mkChar(CommandLineArgs[i]));
35691 ripley 85
    UNPROTECT(1);
86
    return vals;
29748 ripley 87
}
88
 
39740 murdoch 89
#ifdef Win32
42569 ripley 90
extern Rboolean R_LoadRconsole;
39740 murdoch 91
#endif
92
 
37379 ripley 93
void
29748 ripley 94
R_common_command_line(int *pac, char **argv, Rstart Rp)
95
{
96
    int ac = *pac, newac = 1;	/* argv[0] is process name */
84466 ripley 97
    long lval; /* this is used for ppval, so 32-bit long is fine */
29748 ripley 98
    char *p, **av = argv, msg[1024];
99
    Rboolean processing = TRUE;
100
 
101
    R_RestoreHistory = 1;
102
    while(--ac) {
103
	if(processing && **++av == '-') {
104
	    if (!strcmp(*av, "--version")) {
62580 ripley 105
		PrintVersion(msg, 1024);
29748 ripley 106
		R_ShowMessage(msg);
107
		exit(0);
108
	    }
109
	    else if(!strcmp(*av, "--args")) {
110
		/* copy this through for further processing */
111
		argv[newac++] = *av;
112
		processing = FALSE;
113
	    }
114
	    else if(!strcmp(*av, "--save")) {
115
		Rp->SaveAction = SA_SAVE;
116
	    }
117
	    else if(!strcmp(*av, "--no-save")) {
118
		Rp->SaveAction = SA_NOSAVE;
119
	    }
120
	    else if(!strcmp(*av, "--restore")) {
121
		Rp->RestoreAction = SA_RESTORE;
122
	    }
123
	    else if(!strcmp(*av, "--no-restore")) {
124
		Rp->RestoreAction = SA_NORESTORE;
125
		R_RestoreHistory = 0;
126
	    }
127
	    else if(!strcmp(*av, "--no-restore-data")) {
128
		Rp->RestoreAction = SA_NORESTORE;
129
	    }
130
	    else if(!strcmp(*av, "--no-restore-history")) {
131
		R_RestoreHistory = 0;
132
	    }
133
	    else if (!strcmp(*av, "--silent") ||
134
		     !strcmp(*av, "--quiet") ||
135
		     !strcmp(*av, "-q")) {
136
		Rp->R_Quiet = TRUE;
137
	    }
138
	    else if (!strcmp(*av, "--vanilla")) {
139
		Rp->SaveAction = SA_NOSAVE; /* --no-save */
140
		Rp->RestoreAction = SA_NORESTORE; /* --no-restore */
53885 maechler 141
		R_RestoreHistory = 0;     // --no-restore-history (= part of --no-restore)
29748 ripley 142
		Rp->LoadSiteFile = FALSE; /* --no-site-file */
143
		Rp->LoadInitFile = FALSE; /* --no-init-file */
53885 maechler 144
		Rp->NoRenviron = TRUE;    // --no-environ
39740 murdoch 145
#ifdef Win32
42569 ripley 146
		R_LoadRconsole = FALSE;
39740 murdoch 147
#endif
29748 ripley 148
	    }
149
	    else if (!strcmp(*av, "--no-environ")) {
150
		Rp->NoRenviron = TRUE;
151
	    }
152
	    else if (!strcmp(*av, "--verbose")) {
153
		Rp->R_Verbose = TRUE;
154
	    }
77228 maechler 155
	    else if (!strcmp(*av, "--no-echo") ||
77229 maechler 156
		     !strcmp(*av, "--slave") || // "deprecated" from R 4.0.0 (spring 2020)
29748 ripley 157
		     !strcmp(*av, "-s")) {
158
		Rp->R_Quiet = TRUE;
77228 maechler 159
		Rp->R_NoEcho = TRUE;
29748 ripley 160
		Rp->SaveAction = SA_NOSAVE;
161
	    }
162
	    else if (!strcmp(*av, "--no-site-file")) {
163
		Rp->LoadSiteFile = FALSE;
164
	    }
165
	    else if (!strcmp(*av, "--no-init-file")) {
166
		Rp->LoadInitFile = FALSE;
167
	    }
87860 ripley 168
	    /* Undocumented and unused.
169
	     else if (!strcmp(*av, "--debug-init")) {
170
	    	Rp->DebugInitFile = TRUE;
171
	     */
32564 ripley 172
	    else if (!strncmp(*av, "--encoding", 10)) {
173
		if(strlen(*av) < 12) {
56519 ripley 174
		    if(ac > 1) {ac--; av++; p = *av;} else p = NULL;
32564 ripley 175
		} else p = &(*av)[11];
176
		if (p == NULL) {
56519 ripley 177
		    R_ShowMessage(_("WARNING: no value given for --encoding"));
32564 ripley 178
		} else {
179
		    strncpy(R_StdinEnc, p, 30);
36499 ripley 180
		    R_StdinEnc[30] = '\0';
32564 ripley 181
		}
182
	    }
39740 murdoch 183
#ifdef Win32
184
	    else if (!strcmp(*av, "--no-Rconsole")) {
45446 ripley 185
		R_LoadRconsole = 0;
39740 murdoch 186
	    }
187
#endif
29748 ripley 188
	    else if (!strcmp(*av, "-save") ||
189
		     !strcmp(*av, "-nosave") ||
190
		     !strcmp(*av, "-restore") ||
191
		     !strcmp(*av, "-norestore") ||
192
		     !strcmp(*av, "-noreadline") ||
193
		     !strcmp(*av, "-quiet") ||
194
		     !strcmp(*av, "-nsize") ||
195
		     !strcmp(*av, "-vsize") ||
57136 ripley 196
		     !strncmp(*av, "--max-nsize", 11) ||
197
		     !strncmp(*av, "--max-vsize", 11) ||
29748 ripley 198
		     !strcmp(*av, "-V") ||
199
		     !strcmp(*av, "-n") ||
200
		     !strcmp(*av, "-v")) {
201
		snprintf(msg, 1024,
56519 ripley 202
			 _("WARNING: option '%s' no longer supported"), *av);
29748 ripley 203
		R_ShowMessage(msg);
204
	    }
58515 ripley 205
	    /* mop up --min-[nv]size */
206
	    else if( !strncmp(*av, "--min-nsize", 11) ||
207
		     !strncmp(*av, "--min-vsize", 11) ) {
208
		if(strlen(*av) < 13) {
209
		    if(ac > 1) {ac--; av++; p = *av;} else p = NULL;
60447 ripley 210
		} else p = &(*av)[12];
58515 ripley 211
		if (p == NULL) {
212
		    snprintf(msg, 1024,
213
			     _("WARNING: no value given for '%s'"), *av);
214
		    R_ShowMessage(msg);
215
		    break;
216
		}
217
		int ierr;
218
		R_size_t value;
219
		value = R_Decode2Long(p, &ierr);
220
		if(ierr) {
221
		    if(ierr < 0)
222
			snprintf(msg, 1024,
223
				 _("WARNING: '%s' value is invalid: ignored"),
224
				 *av);
225
		    else
83456 ripley 226
			snprintf(msg, 1024,
227
				 _("WARNING: %s: too large and ignored"),
228
				 *av);
58515 ripley 229
		    R_ShowMessage(msg);
230
 
231
		} else {
232
		    if(!strncmp(*av, "--min-nsize", 11)) Rp->nsize = value;
233
		    if(!strncmp(*av, "--min-vsize", 11)) Rp->vsize = value;
234
		}
235
	    }
29748 ripley 236
	    else if(strncmp(*av, "--max-ppsize", 12) == 0) {
237
		if(strlen(*av) < 14) {
56519 ripley 238
		    if(ac > 1) {ac--; av++; p = *av;} else p = NULL;
29748 ripley 239
		} else p = &(*av)[13];
240
		if (p == NULL) {
56519 ripley 241
		    R_ShowMessage(_("WARNING: no value given for '--max-ppsize'"));
29748 ripley 242
		    break;
243
		}
244
		lval = strtol(p, &p, 10);
245
		if (lval < 0)
56519 ripley 246
		    R_ShowMessage(_("WARNING: '--max-ppsize' value is negative: ignored"));
29748 ripley 247
		else if (lval < 10000)
56519 ripley 248
		    R_ShowMessage(_("WARNING: '--max-ppsize' value is too small: ignored"));
35580 maechler 249
		else if (lval > 500000)
56519 ripley 250
		    R_ShowMessage(_("WARNING: '--max-ppsize' value is too large: ignored"));
55540 luke 251
		else Rp->ppsize = (size_t) lval;
29748 ripley 252
	    }
84466 ripley 253
	    else if(strncmp(*av, "--max-connections", 17) == 0) {
254
		if(strlen(*av) < 19) {
255
		    if(ac > 1) {ac--; av++; p = *av;} else p = NULL;
256
		} else p = &(*av)[18];
257
		if (p == NULL) {
258
		    R_ShowMessage(_("WARNING: no value given for '--max-connections'"));
259
		    break;
260
		}
261
		lval = strtol(p, &p, 10);
262
		if (lval < 0)
263
		    R_ShowMessage(_("WARNING: '--max-connections' value is negative: ignored"));
264
		else if (lval < 128)
265
		    R_ShowMessage(_("WARNING: '--max-connections' value is too small: ignored"));
266
		else if (lval > 4096)
267
		    R_ShowMessage(_("WARNING: '--max-connections' value is too large: ignored"));
268
		else Rp->nconnections = (int) lval;
269
	    }
29748 ripley 270
	    else { /* unknown -option */
271
		argv[newac++] = *av;
272
	    }
273
	}
274
	else {
275
	    argv[newac++] = *av;
276
	}
277
    }
278
    *pac = newac;
279
    return;
280
}