1 /*
2  *  R : A Computer Language for Statistical Data Analysis
3  *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
4  *  Copyright (C) 1997--2006  The R Core Team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, a copy is available at
18  *  https://www.R-project.org/Licenses/
19  */
20 
21 /* <UTF8> char here is handled as a whole string */
22 
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 #include <Defn.h>
27 
28 int Rf_initialize_R(int ac, char **av); /* in ../unix/system.c */
29 void setup_Rmainloop(void); /* in main.c */
30 void fpu_setup(Rboolean start);  /* in ../unix/sys-std.c */
31 extern void R_CleanTempDir(void);
32 
33 
34 /*
35  This is the routine that can be called to initialize the R environment
36  when it is embedded within another application (by loading libR.so).
37 
38  The arguments are the command line arguments that would be passed to
39  the regular standalone R, including the first value identifying the
40  name of the `application' being run.  This can be used to indicate in
41  which application R is embedded and used by R code (e.g. in the
42  Rprofile) to determine how to initialize itself. These are accessible
43  via the R function commandArgs().
44 
45  The return value indicates whether the initialization was successful
46  (Currently there is a possibility to do a long jump within the
47  initialization code so that will we never return here.)
48 
49  Example:
50 	 0) name of executable
51 	 1) don't load the X11 module
52 	 2) don't show the banner at startup.
53 
54 
55     char *argv[]= {"REmbeddedPostgres", "--gui=none", "--silent"};
56     Rf_initEmbeddedR(sizeof(argv)/sizeof(argv[0]), argv);
57 */
58 
Rf_initEmbeddedR(int argc,char ** argv)59 int Rf_initEmbeddedR(int argc, char **argv)
60 {
61     Rf_initialize_R(argc, argv);
62     R_Interactive = TRUE;  /* Rf_initialize_R set this based on isatty */
63     setup_Rmainloop();
64     return(1);
65 }
66 
67 /* use fatal !=0 for emergency bail out */
Rf_endEmbeddedR(int fatal)68 void Rf_endEmbeddedR(int fatal)
69 {
70     R_RunExitFinalizers();
71     CleanEd();
72     if(!fatal) KillAllDevices();
73     R_CleanTempDir();
74     if(!fatal && R_CollectWarnings)
75 	PrintWarnings();	/* from device close and .Last */
76     fpu_setup(FALSE);
77 }
78