The R Project SVN R

Rev

Rev 73564 | Rev 77926 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
51536 ripley 1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
72846 kalibera 3
 *  Copyright (C) 2010--2017  R Core Team
51536 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
68956 ripley 17
 *  https://www.R-project.org/Licenses/
51536 ripley 18
 */
19
 
72846 kalibera 20
#define WIN32_LEAN_AND_MEAN 1
21
#include <windows.h> /* for SetConsoleCtrlHandler */
22
#include <stdlib.h>  /* for exit */
51536 ripley 23
#include <string.h>
24
#include <stdio.h>
25
 
52990 ripley 26
extern char *getRHOME(int); /* in ../rhome.c */
51891 ripley 27
 
52990 ripley 28
static void Usage (char *RCMD, char *arch)
29
{
30
    fprintf(stderr, "%s %s %s", "Usage:", RCMD, "[command args]\n\n");
31
    fprintf(stderr, "%s%s%s",
32
	    "where 'command args' can be\n\n",
33
	    "  --arch n   for n=i386, x64, 32 or 64\n",
34
	    "  any other arguments listed by ");
35
    fprintf(stderr, "%s --arch %s --help\n", RCMD, arch);
36
}
51891 ripley 37
 
51536 ripley 38
#define CMD_LEN 10000
39
int main (int argc, char **argv)
40
{
41
    int cmdarg = 1;
72846 kalibera 42
    int interactive = 0;
51891 ripley 43
    char arch[10] = R_ARCH, cmd[CMD_LEN], *p;
51536 ripley 44
 
52990 ripley 45
    if (argc > 1 && strcmp(argv[1], "--help") == 0) {
46
	Usage(argv[0], arch);
47
	exit(0);
48
    }
49
 
50
    if (argc > 1 && strcmp(argv[1], "--arch") == 0) {
51536 ripley 51
	cmdarg = 3;
52990 ripley 52
	if(argc < 3) {
53
	    Usage(argv[0], arch);
54
	    exit(0);
55
	}
51536 ripley 56
	strncpy(arch, argv[2], 10); arch[9] = '\0';
57
	if(strcmp(arch, "32") == 0) strcpy(arch, "i386");
58
	if(strcmp(arch, "64") == 0) strcpy(arch, "x64");
59
	if(strcmp(arch, "i386") && strcmp(arch, "x64")) {
60
	    fprintf(stderr, "valid values for --arch are i386, x64, 32, 64\n");
61
	    exit(1);
62
	}
51891 ripley 63
    } else if ((p = getenv("R_ARCH")))
64
	strncpy(arch, p+1, 10); /* skip leading slash */
65
 
66
    if (stricmp(argv[0] + strlen(argv[0]) - 11, "Rscript.exe") == 0
67
	|| stricmp(argv[0] + strlen(argv[0]) - 7, "Rscript") == 0)
73849 kalibera 68
	snprintf(cmd, CMD_LEN, "\"\"%s\\bin\\%s\\Rscript.exe\"", getRHOME(2), arch);
72846 kalibera 69
    else {
73849 kalibera 70
    	snprintf(cmd, CMD_LEN, "\"\"%s\\bin\\%s\\R.exe\"", getRHOME(2), arch);
72846 kalibera 71
	interactive = 1;
72
    }
51891 ripley 73
 
51536 ripley 74
    for(int i = cmdarg; i < argc; i++) {
72846 kalibera 75
	if (interactive && !strcmp(argv[i], "CMD"))
76
	    interactive = 0;
51536 ripley 77
	strcat(cmd, " ");
78
	if(strchr(argv[i], ' ')) {
51891 ripley 79
	    strcat(cmd, "\"");
51875 ripley 80
	    /* We should really escape " here, I believe */
51536 ripley 81
	    strcat(cmd, argv[i]);
82
	    strcat(cmd, "\"");
83
	} else strcat(cmd, argv[i]);
84
    }
73849 kalibera 85
    /* the outermost double quotes are needed for cmd.exe */
86
    strcat(cmd, "\"");
72846 kalibera 87
 
88
    if (interactive)
89
	/* Ignore Ctrl-C so that Rterm.exe can handle it */
90
	SetConsoleCtrlHandler(NULL, TRUE);   
51536 ripley 91
 
92
    exit(system(cmd));
93
 }