The R Project SVN R

Rev

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

Rev Author Line No. Line
38841 ripley 1
/*  R : A Computer Language for Statistical Data Analysis
59039 ripley 2
 *  Copyright (C) 1998--2006 R Core Team
38841 ripley 3
 *
4
 *  This program is free software; you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  (at your option) any later version.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
42300 ripley 15
 *  along with this program; if not, a copy is available at
68956 ripley 16
 *  https://www.R-project.org/Licenses/
38841 ripley 17
 */
18
 
38854 ripley 19
#include <config.h>
20
#include <Defn.h>
21
#include <Rembedded.h>
22
 
38841 ripley 23
#define WIN32_LEAN_AND_MEAN 1
24
#include <windows.h>
25
#include <stdio.h>
26
#include <Rversion.h>
27
#include <R_ext/RStartup.h>
28
/* for askok and askyesnocancel */
38854 ripley 29
#include "graphapp/ga.h"
38841 ripley 30
 
31
/* for signal-handling code */
32
#include <psignal.h>
33
 
34
/* one way to allow user interrupts: called in ProcessEvents */
35
extern int UserBreak;
36
 
37
/* calls into the R DLL */
38
extern char *getDLLVersion(), *getRUser(), *get_R_HOME();
39
extern void R_DefParams(Rstart), R_SetParams(Rstart), R_setStartTime();
43357 ripley 40
extern void ProcessEvents(void);
38841 ripley 41
extern int R_ReplDLLdo1();
42
 
43
 
44
/* simple input, simple output */
45
 
46
/* This version blocks all events: a real one needs to call ProcessEvents
47
   frequently. See rterm.c and ../system.c for one approach using
48
   a separate thread for input.
49
*/
43767 ripley 50
static int myReadConsole(const char *prompt, char *buf, int len,
51
			 int addtohistory)
38841 ripley 52
{
53
    fputs(prompt, stdout);
54
    fflush(stdout);
55
    if(fgets(buf, len, stdin)) return 1;
56
    else return 0;
57
}
58
 
43767 ripley 59
static void myWriteConsole(const char *buf, int len)
38841 ripley 60
{
61
    printf("%s", buf);
62
}
63
 
38869 ripley 64
static void myCallBack()
38841 ripley 65
{
66
    /* called during i/o, eval, graphics in ProcessEvents */
67
}
68
 
38869 ripley 69
static void myBusy(int which)
38841 ripley 70
{
71
    /* set a busy cursor ... if which = 1, unset if which = 0 */
72
}
73
 
74
static void my_onintr(int sig)
75
{
76
    UserBreak = 1;
77
}
78
 
43958 murdoch 79
extern Rboolean R_LoadRconsole;
80
 
38854 ripley 81
int Rf_initialize_R(int argc, char **argv)
38841 ripley 82
{
83
    structRstart rp;
84
    Rstart Rp = &rp;
85
    char Rversion[25], *RHome;
86
 
38869 ripley 87
    snprintf(Rversion, 25, "%s.%s", R_MAJOR, R_MINOR);
88
    if(strncmp(getDLLVersion(), Rversion, 25) != 0) {
45070 ripley 89
	fprintf(stderr, "Error: R.DLL version does not match\n");
38846 ripley 90
	exit(1);
38841 ripley 91
    }
92
 
93
    R_setStartTime();
94
    R_DefParams(Rp);
95
    if((RHome = get_R_HOME()) == NULL) {
45070 ripley 96
	fprintf(stderr,
38841 ripley 97
		"R_HOME must be set in the environment or Registry\n");
38846 ripley 98
	exit(2);
38841 ripley 99
    }
100
    Rp->rhome = RHome;
101
    Rp->home = getRUser();
102
    Rp->CharacterMode = LinkDLL;
103
    Rp->ReadConsole = myReadConsole;
104
    Rp->WriteConsole = myWriteConsole;
105
    Rp->CallBack = myCallBack;
43847 ripley 106
    Rp->ShowMessage = askok;
107
    Rp->YesNoCancel = askyesnocancel;
38841 ripley 108
    Rp->Busy = myBusy;
109
 
110
    Rp->R_Quiet = TRUE;
111
    Rp->R_Interactive = TRUE;
112
    Rp->RestoreAction = SA_RESTORE;
113
    Rp->SaveAction = SA_NOSAVE;
114
    R_SetParams(Rp);
115
    R_set_command_line_arguments(argc, argv);
116
 
117
    FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
118
 
119
    signal(SIGBREAK, my_onintr);
38960 ripley 120
    GA_initapp(0, 0);
43958 murdoch 121
    R_LoadRconsole = FALSE;
38960 ripley 122
    readconsolecfg();
45070 ripley 123
 
38854 ripley 124
    return 0;
125
}
126
 
127
int Rf_initEmbeddedR(int argc, char **argv)
128
{
129
    Rf_initialize_R(argc, argv);
38841 ripley 130
    setup_Rmainloop();
38854 ripley 131
    return(1);
132
}
38841 ripley 133
 
38854 ripley 134
/* use fatal !=0 for emergency bail out */
135
void Rf_endEmbeddedR(int fatal)
136
{
137
    R_RunExitFinalizers();
138
    CleanEd();
139
    R_CleanTempDir();
140
    if(!fatal){
141
	Rf_KillAllDevices();
142
	AllDevicesKilled = TRUE;
143
    }
144
    if(!fatal && R_CollectWarnings)
145
	PrintWarnings();	/* from device close and .Last */
146
    app_cleanup();
38841 ripley 147
}