The R Project SVN R

Rev

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