The R Project SVN R

Rev

Rev 83755 | 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
83755 kalibera 2
 *  Copyright (C) 1998--2023 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 */
84952 kalibera 38
extern char *getDLLVersion(void), *getRUser(void), *get_R_HOME(void);
83755 kalibera 39
extern void freeRUser(char *), free_R_HOME(char *);
84952 kalibera 40
extern void R_SetParams(Rstart), R_setStartTime(void);
43357 ripley 41
extern void ProcessEvents(void);
84952 kalibera 42
extern int R_DefParamsEx(Rstart, int), R_ReplDLLdo1(void);
38841 ripley 43
 
44
 
45
/* simple input, simple output */
46
 
47
/* This version blocks all events: a real one needs to call ProcessEvents
48
   frequently. See rterm.c and ../system.c for one approach using
49
   a separate thread for input.
50
*/
81626 kalibera 51
static int myReadConsole(const char *prompt, unsigned char *buf, int len,
43767 ripley 52
			 int addtohistory)
38841 ripley 53
{
54
    fputs(prompt, stdout);
55
    fflush(stdout);
81626 kalibera 56
    if(fgets((char *)buf, len, stdin)) return 1;
38841 ripley 57
    else return 0;
58
}
59
 
43767 ripley 60
static void myWriteConsole(const char *buf, int len)
38841 ripley 61
{
62
    printf("%s", buf);
63
}
64
 
84952 kalibera 65
static void myCallBack(void)
38841 ripley 66
{
67
    /* called during i/o, eval, graphics in ProcessEvents */
68
}
69
 
38869 ripley 70
static void myBusy(int which)
38841 ripley 71
{
72
    /* set a busy cursor ... if which = 1, unset if which = 0 */
73
}
74
 
75
static void my_onintr(int sig)
76
{
77
    UserBreak = 1;
78
}
79
 
43958 murdoch 80
extern Rboolean R_LoadRconsole;
81
 
38854 ripley 82
int Rf_initialize_R(int argc, char **argv)
38841 ripley 83
{
84
    structRstart rp;
85
    Rstart Rp = &rp;
83755 kalibera 86
    char Rversion[25], *RHome, *RUser;
38841 ripley 87
 
38869 ripley 88
    snprintf(Rversion, 25, "%s.%s", R_MAJOR, R_MINOR);
89
    if(strncmp(getDLLVersion(), Rversion, 25) != 0) {
45070 ripley 90
	fprintf(stderr, "Error: R.DLL version does not match\n");
38846 ripley 91
	exit(1);
38841 ripley 92
    }
93
 
94
    R_setStartTime();
81952 kalibera 95
    R_DefParamsEx(Rp, RSTART_VERSION);
38841 ripley 96
    if((RHome = get_R_HOME()) == NULL) {
45070 ripley 97
	fprintf(stderr,
38841 ripley 98
		"R_HOME must be set in the environment or Registry\n");
38846 ripley 99
	exit(2);
38841 ripley 100
    }
101
    Rp->rhome = RHome;
83755 kalibera 102
    RUser = getRUser();
103
    Rp->home = RUser;
38841 ripley 104
    Rp->CharacterMode = LinkDLL;
79805 kalibera 105
    Rp->EmitEmbeddedUTF8 = FALSE;
38841 ripley 106
    Rp->ReadConsole = myReadConsole;
107
    Rp->WriteConsole = myWriteConsole;
108
    Rp->CallBack = myCallBack;
43847 ripley 109
    Rp->ShowMessage = askok;
110
    Rp->YesNoCancel = askyesnocancel;
38841 ripley 111
    Rp->Busy = myBusy;
112
 
113
    Rp->R_Quiet = TRUE;
114
    Rp->R_Interactive = TRUE;
115
    Rp->RestoreAction = SA_RESTORE;
116
    Rp->SaveAction = SA_NOSAVE;
117
    R_SetParams(Rp);
83755 kalibera 118
    freeRUser(RUser);
119
    free_R_HOME(RHome);
38841 ripley 120
    R_set_command_line_arguments(argc, argv);
121
 
122
    FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
123
 
124
    signal(SIGBREAK, my_onintr);
38960 ripley 125
    GA_initapp(0, 0);
43958 murdoch 126
    R_LoadRconsole = FALSE;
38960 ripley 127
    readconsolecfg();
45070 ripley 128
 
38854 ripley 129
    return 0;
130
}
131
 
132
int Rf_initEmbeddedR(int argc, char **argv)
133
{
134
    Rf_initialize_R(argc, argv);
38841 ripley 135
    setup_Rmainloop();
38854 ripley 136
    return(1);
137
}
38841 ripley 138
 
38854 ripley 139
/* use fatal !=0 for emergency bail out */
140
void Rf_endEmbeddedR(int fatal)
141
{
142
    R_RunExitFinalizers();
143
    CleanEd();
144
    R_CleanTempDir();
145
    if(!fatal){
146
	Rf_KillAllDevices();
147
	AllDevicesKilled = TRUE;
148
    }
149
    if(!fatal && R_CollectWarnings)
150
	PrintWarnings();	/* from device close and .Last */
151
    app_cleanup();
38841 ripley 152
}