The R Project SVN R

Rev

Rev 77228 | Rev 78525 | Go to most recent revision | 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
77228 maechler 3
 *  Copyright (C) 1997-2019   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;
50
static char **CommandLineArgs = NULL;
51
 
52
 
53
void
54
R_set_command_line_arguments(int argc, char **argv)
55
{
56
    int i;
57
 
58
    NumCommandLineArgs = argc;
55420 luke 59
    CommandLineArgs = (char**) calloc((size_t) argc, sizeof(char*));
29748 ripley 60
 
61
    for(i = 0; i < argc; i++)
62
	CommandLineArgs[i] = strdup(argv[i]);
63
}
64
 
65
 
66
/*
67
  The .Internal which returns the command line arguments that are stored
68
  in global variables.
69
 */
36990 ripley 70
SEXP attribute_hidden
29748 ripley 71
do_commandArgs(SEXP call, SEXP op, SEXP args, SEXP env)
72
{
73
    int i;
74
    SEXP vals;
75
 
69326 luke 76
    checkArity(op, args);
35691 ripley 77
    /* need protection as mkChar allocates */
78
    vals = PROTECT(allocVector(STRSXP, NumCommandLineArgs));
29748 ripley 79
    for(i = 0; i < NumCommandLineArgs; i++)
80
	SET_STRING_ELT(vals, i, mkChar(CommandLineArgs[i]));
35691 ripley 81
    UNPROTECT(1);
82
    return vals;
29748 ripley 83
}
84
 
39740 murdoch 85
#ifdef Win32
42569 ripley 86
extern Rboolean R_LoadRconsole;
39740 murdoch 87
#endif
88
 
37379 ripley 89
void
29748 ripley 90
R_common_command_line(int *pac, char **argv, Rstart Rp)
91
{
92
    int ac = *pac, newac = 1;	/* argv[0] is process name */
42253 ripley 93
    long lval; /* this is only used for ppval, so 32-bit long is fine */
29748 ripley 94
    char *p, **av = argv, msg[1024];
95
    Rboolean processing = TRUE;
96
 
97
    R_RestoreHistory = 1;
98
    while(--ac) {
99
	if(processing && **++av == '-') {
100
	    if (!strcmp(*av, "--version")) {
62580 ripley 101
		PrintVersion(msg, 1024);
29748 ripley 102
		R_ShowMessage(msg);
103
		exit(0);
104
	    }
105
	    else if(!strcmp(*av, "--args")) {
106
		/* copy this through for further processing */
107
		argv[newac++] = *av;
108
		processing = FALSE;
109
	    }
110
	    else if(!strcmp(*av, "--save")) {
111
		Rp->SaveAction = SA_SAVE;
112
	    }
113
	    else if(!strcmp(*av, "--no-save")) {
114
		Rp->SaveAction = SA_NOSAVE;
115
	    }
116
	    else if(!strcmp(*av, "--restore")) {
117
		Rp->RestoreAction = SA_RESTORE;
118
	    }
119
	    else if(!strcmp(*av, "--no-restore")) {
120
		Rp->RestoreAction = SA_NORESTORE;
121
		R_RestoreHistory = 0;
122
	    }
123
	    else if(!strcmp(*av, "--no-restore-data")) {
124
		Rp->RestoreAction = SA_NORESTORE;
125
	    }
126
	    else if(!strcmp(*av, "--no-restore-history")) {
127
		R_RestoreHistory = 0;
128
	    }
129
	    else if (!strcmp(*av, "--silent") ||
130
		     !strcmp(*av, "--quiet") ||
131
		     !strcmp(*av, "-q")) {
132
		Rp->R_Quiet = TRUE;
133
	    }
134
	    else if (!strcmp(*av, "--vanilla")) {
135
		Rp->SaveAction = SA_NOSAVE; /* --no-save */
136
		Rp->RestoreAction = SA_NORESTORE; /* --no-restore */
53885 maechler 137
		R_RestoreHistory = 0;     // --no-restore-history (= part of --no-restore)
29748 ripley 138
		Rp->LoadSiteFile = FALSE; /* --no-site-file */
139
		Rp->LoadInitFile = FALSE; /* --no-init-file */
53885 maechler 140
		Rp->NoRenviron = TRUE;    // --no-environ
39740 murdoch 141
#ifdef Win32
42569 ripley 142
		R_LoadRconsole = FALSE;
39740 murdoch 143
#endif
29748 ripley 144
	    }
145
	    else if (!strcmp(*av, "--no-environ")) {
146
		Rp->NoRenviron = TRUE;
147
	    }
148
	    else if (!strcmp(*av, "--verbose")) {
149
		Rp->R_Verbose = TRUE;
150
	    }
77228 maechler 151
	    else if (!strcmp(*av, "--no-echo") ||
77229 maechler 152
		     !strcmp(*av, "--slave") || // "deprecated" from R 4.0.0 (spring 2020)
29748 ripley 153
		     !strcmp(*av, "-s")) {
154
		Rp->R_Quiet = TRUE;
77228 maechler 155
		Rp->R_NoEcho = TRUE;
29748 ripley 156
		Rp->SaveAction = SA_NOSAVE;
157
	    }
158
	    else if (!strcmp(*av, "--no-site-file")) {
159
		Rp->LoadSiteFile = FALSE;
160
	    }
161
	    else if (!strcmp(*av, "--no-init-file")) {
162
		Rp->LoadInitFile = FALSE;
163
	    }
164
	    else if (!strcmp(*av, "--debug-init")) {
45446 ripley 165
		Rp->DebugInitFile = TRUE;
29748 ripley 166
	    }
32564 ripley 167
	    else if (!strncmp(*av, "--encoding", 10)) {
168
		if(strlen(*av) < 12) {
56519 ripley 169
		    if(ac > 1) {ac--; av++; p = *av;} else p = NULL;
32564 ripley 170
		} else p = &(*av)[11];
171
		if (p == NULL) {
56519 ripley 172
		    R_ShowMessage(_("WARNING: no value given for --encoding"));
32564 ripley 173
		} else {
174
		    strncpy(R_StdinEnc, p, 30);
36499 ripley 175
		    R_StdinEnc[30] = '\0';
32564 ripley 176
		}
177
	    }
39740 murdoch 178
#ifdef Win32
179
	    else if (!strcmp(*av, "--no-Rconsole")) {
45446 ripley 180
		R_LoadRconsole = 0;
39740 murdoch 181
	    }
182
#endif
29748 ripley 183
	    else if (!strcmp(*av, "-save") ||
184
		     !strcmp(*av, "-nosave") ||
185
		     !strcmp(*av, "-restore") ||
186
		     !strcmp(*av, "-norestore") ||
187
		     !strcmp(*av, "-noreadline") ||
188
		     !strcmp(*av, "-quiet") ||
189
		     !strcmp(*av, "-nsize") ||
190
		     !strcmp(*av, "-vsize") ||
57136 ripley 191
		     !strncmp(*av, "--max-nsize", 11) ||
192
		     !strncmp(*av, "--max-vsize", 11) ||
29748 ripley 193
		     !strcmp(*av, "-V") ||
194
		     !strcmp(*av, "-n") ||
195
		     !strcmp(*av, "-v")) {
196
		snprintf(msg, 1024,
56519 ripley 197
			 _("WARNING: option '%s' no longer supported"), *av);
29748 ripley 198
		R_ShowMessage(msg);
199
	    }
58515 ripley 200
	    /* mop up --min-[nv]size */
201
	    else if( !strncmp(*av, "--min-nsize", 11) ||
202
		     !strncmp(*av, "--min-vsize", 11) ) {
203
		if(strlen(*av) < 13) {
204
		    if(ac > 1) {ac--; av++; p = *av;} else p = NULL;
60447 ripley 205
		} else p = &(*av)[12];
58515 ripley 206
		if (p == NULL) {
207
		    snprintf(msg, 1024,
208
			     _("WARNING: no value given for '%s'"), *av);
209
		    R_ShowMessage(msg);
210
		    break;
211
		}
212
		int ierr;
213
		R_size_t value;
214
		value = R_Decode2Long(p, &ierr);
215
		if(ierr) {
216
		    if(ierr < 0)
217
			snprintf(msg, 1024,
218
				 _("WARNING: '%s' value is invalid: ignored"),
219
				 *av);
220
		    else
221
			sprintf(msg,
222
				_("WARNING: %s: too large and ignored"),
223
				*av);
224
		    R_ShowMessage(msg);
225
 
226
		} else {
227
		    if(!strncmp(*av, "--min-nsize", 11)) Rp->nsize = value;
228
		    if(!strncmp(*av, "--min-vsize", 11)) Rp->vsize = value;
229
		}
230
	    }
29748 ripley 231
	    else if(strncmp(*av, "--max-ppsize", 12) == 0) {
232
		if(strlen(*av) < 14) {
56519 ripley 233
		    if(ac > 1) {ac--; av++; p = *av;} else p = NULL;
29748 ripley 234
		} else p = &(*av)[13];
235
		if (p == NULL) {
56519 ripley 236
		    R_ShowMessage(_("WARNING: no value given for '--max-ppsize'"));
29748 ripley 237
		    break;
238
		}
239
		lval = strtol(p, &p, 10);
240
		if (lval < 0)
56519 ripley 241
		    R_ShowMessage(_("WARNING: '--max-ppsize' value is negative: ignored"));
29748 ripley 242
		else if (lval < 10000)
56519 ripley 243
		    R_ShowMessage(_("WARNING: '--max-ppsize' value is too small: ignored"));
35580 maechler 244
		else if (lval > 500000)
56519 ripley 245
		    R_ShowMessage(_("WARNING: '--max-ppsize' value is too large: ignored"));
55540 luke 246
		else Rp->ppsize = (size_t) lval;
29748 ripley 247
	    }
248
	    else { /* unknown -option */
249
		argv[newac++] = *av;
250
	    }
251
	}
252
	else {
253
	    argv[newac++] = *av;
254
	}
255
    }
256
    *pac = newac;
257
    return;
258
}