The R Project SVN R

Rev

Rev 26207 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 26207 Rev 26726
Line 1... Line 1...
1
/*
1
/*
2
 *  RProxy: Connector implementation between application and R language
2
 *  RProxy: Connector implementation between application and R language
3
 *  Copyright (C) 1999--2001 Thomas Baier
3
 *  Copyright (C) 1999--2001 Thomas Baier
4
 *
4
 *
5
 *  R_Proxy_init based on rtest.c,  Copyright (C) 1998--2000
5
 *  R_Proxy_init based on rtest.c,  Copyright (C) 1998--2000
6
 *                                  R Development Core Team
6
 *                                  R Development Core Team
7
 *
7
 *
8
 *
8
 *
9
 *  This library is free software; you can redistribute it and/or
9
 *  This library is free software; you can redistribute it and/or
10
 *  modify it under the terms of the GNU Library General Public
10
 *  modify it under the terms of the GNU Library General Public
11
 *  License as published by the Free Software Foundation; either
11
 *  License as published by the Free Software Foundation; either
12
 *  version 2 of the License, or (at your option) any later version.
12
 *  version 2 of the License, or (at your option) any later version.
13
 *
13
 *
14
 *  This library is distributed in the hope that it will be useful,
14
 *  This library is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 *  Library General Public License for more details.
17
 *  Library General Public License for more details.
18
 *
18
 *
19
 *  You should have received a copy of the GNU Library General Public
19
 *  You should have received a copy of the GNU Library General Public
20
 *  License along with this library; if not, write to the Free
20
 *  License along with this library; if not, write to the Free
21
 *  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21
 *  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22
 *  MA 02111-1307, USA
22
 *  MA 02111-1307, USA
23
 *
23
 *
24
 *  $Id: rproxy_impl.c,v 1.19 2003/09/13 15:14:09 murdoch Exp $
24
 *  $Id: rproxy_impl.c,v 1.20 2003/10/23 07:14:03 ripley Exp $
25
 */
25
 */
26
 
26
 
27
#define NONAMELESSUNION
27
#define NONAMELESSUNION
28
#include <windows.h>
28
#include <windows.h>
29
#include <assert.h>
29
#include <assert.h>
30
#include <stdio.h>
30
#include <stdio.h>
31
#include <stdlib.h>
31
#include <stdlib.h>
32
/*#include "globalvar.h"
32
/*#include "globalvar.h"
33
#undef CharacterMode
33
#undef CharacterMode
34
#undef R_Interactive*/
34
#undef R_Interactive*/
35
#include <config.h>
35
#include <config.h>
36
#include <Rversion.h>
36
#include <Rversion.h>
37
#include <Startup.h>
37
#include <Startup.h>
38
#include "bdx.h"
38
#include "bdx.h"
39
#include "SC_proxy.h"
39
#include "SC_proxy.h"
40
#include "rproxy_impl.h"
40
#include "rproxy_impl.h"
41
#include <IOStuff.h>
41
#include <IOStuff.h>
42
#include <Parse.h>
42
#include <Parse.h>
43
#include <Graphics.h>
43
#include <Graphics.h>
44
 
44
 
45
struct _R_Proxy_init_parameters
45
struct _R_Proxy_init_parameters
46
{
46
{
47
  int vsize;
47
  int vsize;
48
  int vsize_valid;
48
  int vsize_valid;
49
  int nsize;
49
  int nsize;
50
  int nsize_valid;
50
  int nsize_valid;
51
};
51
};
52
 
52
 
53
/* 01-12-07 | baier | no more extern for exported variables (crashes!) */
53
/* 01-12-07 | baier | no more extern for exported variables (crashes!) */
54
/*#define R_GlobalEnv (*__imp_R_GlobalEnv)
54
/*#define R_GlobalEnv (*__imp_R_GlobalEnv)
55
#define R_Visible (*__imp_R_Visible)
55
#define R_Visible (*__imp_R_Visible)
56
#define R_EvalDepth (*__imp_R_EvalDepth)
56
#define R_EvalDepth (*__imp_R_EvalDepth)
57
#define R_DimSymbol (*__imp_R_DimSymbol)*/
57
#define R_DimSymbol (*__imp_R_DimSymbol)*/
58
 
58
 
59
/*
59
/*
60
extern SEXP R_GlobalEnv;
60
extern SEXP R_GlobalEnv;
61
extern int R_Visible;
61
extern int R_Visible;
62
extern int R_EvalDepth;
62
extern int R_EvalDepth;
63
extern SEXP R_DimSymbol;
63
extern SEXP R_DimSymbol;
64
*/
64
*/
65
 
65
 
66
/* calls into the R DLL */
66
/* calls into the R DLL */
67
extern char *getDLLVersion();
67
extern char *getDLLVersion();
68
extern void R_DefParams(Rstart);
68
extern void R_DefParams(Rstart);
69
extern void R_SetParams(Rstart);
69
extern void R_SetParams(Rstart);
70
extern void setup_term_ui(void);
70
extern void setup_term_ui(void);
71
extern char *getRHOME();
71
extern char *getRHOME();
72
extern void end_Rmainloop(), R_ReplDLLinit();
72
extern void end_Rmainloop(), R_ReplDLLinit();
73
extern void askok(char *);
73
extern void askok(char *);
74
 
74
 
75
int R_Proxy_Graphics_Driver (NewDevDesc* pDD,
75
int R_Proxy_Graphics_Driver (NewDevDesc* pDD,
76
			     char* pDisplay,
76
			     char* pDisplay,
77
			     double pWidth,
77
			     double pWidth,
78
			     double pHeight,
78
			     double pHeight,
79
			     double pPointSize);
79
			     double pPointSize);
80
 
80
 
81
extern SC_CharacterDevice* __output_device;
81
extern SC_CharacterDevice* __output_device;
82
 
82
 
83
static int s_EvalInProgress = 0;
83
static int s_EvalInProgress = 0;
84
 
84
 
85
void R_Proxy_askok (char* pMsg)
85
void R_Proxy_askok (char* pMsg)
86
{
86
{
87
  askok(pMsg);
87
  askok(pMsg);
88
  return;
88
  return;
89
}
89
}
90
 
90
 
91
int R_Proxy_askyesnocancel (char* pMsg)
91
int R_Proxy_askyesnocancel (char* pMsg)
92
{
92
{
93
  return 1;
93
  return 1;
94
}
94
}
95
 
95
 
96
int R_Proxy_ReadConsole(char *prompt, char *buf, int len, int addtohistory)
96
int R_Proxy_ReadConsole(char *prompt, char *buf, int len, int addtohistory)
97
{
97
{
98
  return 0;
98
  return 0;
99
}
99
}
100
 
100
 
101
void R_Proxy_WriteConsole(char *buf, int len)
101
void R_Proxy_WriteConsole(char *buf, int len)
102
{
102
{
103
  if (__output_device)
103
  if (__output_device)
104
    {
104
    {
105
      __output_device->vtbl->write_string (__output_device,buf);
105
      __output_device->vtbl->write_string (__output_device,buf);
106
    }
106
    }
107
}
107
}
108
 
108
 
109
void R_Proxy_CallBack()
109
void R_Proxy_CallBack()
110
{
110
{
111
    /* called during i/o, eval, graphics in ProcessEvents */
111
    /* called during i/o, eval, graphics in ProcessEvents */
112
}
112
}
113
 
113
 
114
void R_Proxy_Busy(int which)
114
void R_Proxy_Busy(int which)
115
{
115
{
116
    /* set a busy cursor ... in which = 1, unset if which = 0 */
116
    /* set a busy cursor ... in which = 1, unset if which = 0 */
117
}
117
}
118
 
118
 
119
 
119
 
120
int SEXP2BDX_Data (SEXP pExpression,BDX_Data** pData)
120
int SEXP2BDX_Data (SEXP pExpression,BDX_Data** pData)
121
{
121
{
122
  BDX_Data* lData = 0;
122
  BDX_Data* lData = 0;
123
 
123
 
124
  /* allocate buffer */
124
  /* allocate buffer */
125
  lData = (BDX_Data*) malloc (sizeof (BDX_Data));
125
  lData = (BDX_Data*) malloc (sizeof (BDX_Data));
126
  *pData = lData;
126
  *pData = lData;
127
  assert (*pData != NULL);
127
  assert (*pData != NULL);
128
 
128
 
129
  /*
129
  /*
130
   * we support the following types at the moment
130
   * we support the following types at the moment
131
   *
131
   *
132
   *  integer (scalar, vectors and arrays)
132
   *  integer (scalar, vectors and arrays)
133
   *  real (scalars, vectors and arrays)
133
   *  real (scalars, vectors and arrays)
134
   *  logical (scalars, vectors and arrays)
134
   *  logical (scalars, vectors and arrays)
135
   *  string (scalars, vectors and arrays)
135
   *  string (scalars, vectors and arrays)
136
   *  null
136
   *  null
137
   *
137
   *
138
   * we should support soon
138
   * we should support soon
139
   *
139
   *
140
   *  complex vectors
140
   *  complex vectors
141
   *  generic vectors
141
   *  generic vectors
142
   */
142
   */
143
  switch (TYPEOF (pExpression))
143
  switch (TYPEOF (pExpression))
144
    {
144
    {
145
    case NILSXP	 :
145
    case NILSXP	 :
146
      lData->type = BDX_NULL;
146
      lData->type = BDX_NULL;
147
 
147
 
148
      /* dimensions: 1 */
148
      /* dimensions: 1 */
149
      lData->dim_count = 1;
149
      lData->dim_count = 1;
150
      lData->dimensions =
150
      lData->dimensions =
151
	(BDX_Dimension*) malloc (sizeof (BDX_Dimension));
151
	(BDX_Dimension*) malloc (sizeof (BDX_Dimension));
152
      lData->dimensions[0] = 0;
152
      lData->dimensions[0] = 0;
153
 
153
 
154
      /* data: empty (just a dummy data record) */
154
      /* data: empty (just a dummy data record) */
155
      lData->raw_data =
155
      lData->raw_data =
156
	(BDX_RawData*) malloc (sizeof (BDX_RawData));
156
	(BDX_RawData*) malloc (sizeof (BDX_RawData));
157
 
157
 
158
      /*      UNPROTECT (1); */
158
      /*      UNPROTECT (1); */
159
 
159
 
160
      return SC_PROXY_OK;
160
      return SC_PROXY_OK;
161
 
161
 
162
      break;
162
      break;
163
    case LGLSXP	 :
163
    case LGLSXP	 :
164
      lData->type = BDX_BOOL;
164
      lData->type = BDX_BOOL;
165
      break;
165
      break;
166
    case INTSXP	 :
166
    case INTSXP	 :
167
      lData->type = BDX_INT;
167
      lData->type = BDX_INT;
168
      break;
168
      break;
169
    case REALSXP	 :
169
    case REALSXP	 :
170
      lData->type = BDX_DOUBLE;
170
      lData->type = BDX_DOUBLE;
171
      break;
171
      break;
172
    case STRSXP	 :
172
    case STRSXP	 :
173
      lData->type = BDX_STRING;
173
      lData->type = BDX_STRING;
174
      break;
174
      break;
175
      /*
175
      /*
176
       case VECSXP	 : printf ("type: generic vectors\n");
176
       case VECSXP	 : printf ("type: generic vectors\n");
177
       break;
177
       break;
178
       case CPLXSXP	 : printf ("type: complex variables\n");
178
       case CPLXSXP	 : printf ("type: complex variables\n");
179
       break;
179
       break;
180
      */
180
      */
181
    default:
181
    default:
182
      /*      UNPROTECT (1); */
182
      /*      UNPROTECT (1); */
183
      free (lData);
183
      free (lData);
184
      *pData = NULL;
184
      *pData = NULL;
185
      return SC_PROXY_ERR_UNSUPPORTEDTYPE;
185
      return SC_PROXY_ERR_UNSUPPORTEDTYPE;
186
    }
186
    }
187
 
187
 
188
  /* the type is set now. NULL values have already returned */
188
  /* the type is set now. NULL values have already returned */
189
 
189
 
190
  /* is it a scalar, a vector or an array? */
190
  /* is it a scalar, a vector or an array? */
191
 
191
 
192
  /* bug: no dimensions stored */
192
  /* bug: no dimensions stored */
193
  if (LENGTH (pExpression) == 0)
193
  if (LENGTH (pExpression) == 0)
194
    {
194
    {
195
      free (lData);
195
      free (lData);
196
      *pData = NULL;
196
      *pData = NULL;
197
      return SC_PROXY_ERR_UNKNOWN;
197
      return SC_PROXY_ERR_UNKNOWN;
198
    }
198
    }
199
 
199
 
200
  /* scalar: length 1 */
200
  /* scalar: length 1 */
201
  if (LENGTH (pExpression) == 1)
201
  if (LENGTH (pExpression) == 1)
202
    {
202
    {
203
      lData->type |= BDX_SCALAR;
203
      lData->type |= BDX_SCALAR;
204
      lData->dim_count = 1;
204
      lData->dim_count = 1;
205
      lData->dimensions =
205
      lData->dimensions =
206
	(BDX_Dimension*) malloc (sizeof (BDX_Dimension));
206
	(BDX_Dimension*) malloc (sizeof (BDX_Dimension));
207
      lData->dimensions[0] = 1;
207
      lData->dimensions[0] = 1;
208
      lData->raw_data =
208
      lData->raw_data =
209
	(BDX_RawData*) malloc (sizeof (BDX_RawData));
209
	(BDX_RawData*) malloc (sizeof (BDX_RawData));
210
      switch (lData->type & BDX_SMASK)
210
      switch (lData->type & BDX_SMASK)
211
	{
211
	{
212
	case BDX_BOOL:
212
	case BDX_BOOL:
213
	  lData->raw_data[0].bool_value = LOGICAL (pExpression)[0];
213
	  lData->raw_data[0].bool_value = LOGICAL (pExpression)[0];
214
	  break;
214
	  break;
215
	case BDX_INT:
215
	case BDX_INT:
216
	  lData->raw_data[0].int_value = INTEGER (pExpression)[0];
216
	  lData->raw_data[0].int_value = INTEGER (pExpression)[0];
217
	  break;
217
	  break;
218
	case BDX_DOUBLE:
218
	case BDX_DOUBLE:
219
	  lData->raw_data[0].double_value = REAL (pExpression)[0];
219
	  lData->raw_data[0].double_value = REAL (pExpression)[0];
220
	  break;
220
	  break;
221
	case BDX_STRING:
221
	case BDX_STRING:
222
	  lData->raw_data[0].string_value = strdup (CHAR (STRING_ELT(pExpression, 0)));
222
	  lData->raw_data[0].string_value = strdup (CHAR (STRING_ELT(pExpression, 0)));
223
	  break;
223
	  break;
224
	}
224
	}
225
    }
225
    }
226
  else
226
  else
227
    {
227
    {
228
      /* is it a vector or an array? */
228
      /* is it a vector or an array? */
229
      SEXP lDimension;
229
      SEXP lDimension;
230
 
230
 
231
      lDimension = getAttrib (pExpression,R_DimSymbol);
231
      lDimension = getAttrib (pExpression,R_DimSymbol);
232
 
232
 
233
      PROTECT (lDimension);
233
      PROTECT (lDimension);
234
 
234
 
235
      if (TYPEOF (lDimension) == NILSXP)
235
      if (TYPEOF (lDimension) == NILSXP)
236
	{
236
	{
237
	  /* vector */
237
	  /* vector */
238
	  int i;
238
	  int i;
239
 
239
 
240
	  lData->type |= BDX_VECTOR;
240
	  lData->type |= BDX_VECTOR;
241
	  lData->dim_count = 1;
241
	  lData->dim_count = 1;
242
	  lData->dimensions =
242
	  lData->dimensions =
243
	    (BDX_Dimension*) malloc (sizeof (BDX_Dimension));
243
	    (BDX_Dimension*) malloc (sizeof (BDX_Dimension));
244
	  lData->dimensions[0] = LENGTH (pExpression);
244
	  lData->dimensions[0] = LENGTH (pExpression);
245
	  lData->raw_data =
245
	  lData->raw_data =
246
	    (BDX_RawData*) malloc (sizeof (BDX_RawData)
246
	    (BDX_RawData*) malloc (sizeof (BDX_RawData)
247
				   * lData->dimensions[0]);
247
				   * lData->dimensions[0]);
248
 
248
 
249
	  /* copy the data */
249
	  /* copy the data */
250
	  for (i = 0; i < lData->dimensions[0];i++)
250
	  for (i = 0; i < lData->dimensions[0];i++)
251
	    {
251
	    {
252
	      switch (lData->type & BDX_SMASK)
252
	      switch (lData->type & BDX_SMASK)
253
		{
253
		{
254
		case BDX_BOOL:
254
		case BDX_BOOL:
255
		  lData->raw_data[i].bool_value = LOGICAL (pExpression)[i];
255
		  lData->raw_data[i].bool_value = LOGICAL (pExpression)[i];
256
		  break;
256
		  break;
257
		case BDX_INT:
257
		case BDX_INT:
258
		  lData->raw_data[i].int_value = INTEGER (pExpression)[i];
258
		  lData->raw_data[i].int_value = INTEGER (pExpression)[i];
259
		  break;
259
		  break;
260
		case BDX_DOUBLE:
260
		case BDX_DOUBLE:
261
		  lData->raw_data[i].double_value = REAL (pExpression)[i];
261
		  lData->raw_data[i].double_value = REAL (pExpression)[i];
262
		  break;
262
		  break;
263
		case BDX_STRING:
263
		case BDX_STRING:
264
		  lData->raw_data[i].string_value = strdup (CHAR (STRING_ELT(pExpression, i)));
264
		  lData->raw_data[i].string_value = strdup (CHAR (STRING_ELT(pExpression, i)));
265
		  break;
265
		  break;
266
		}
266
		}
267
	    }
267
	    }
268
 
268
 
269
	  UNPROTECT (1); /* dimension */
269
	  UNPROTECT (1); /* dimension */
270
 
270
 
271
	  return SC_PROXY_OK;
271
	  return SC_PROXY_OK;
272
	}
272
	}
273
      else
273
      else
274
	{
274
	{
275
	  /* array with LENGTH(lDimension) dimensions */
275
	  /* array with LENGTH(lDimension) dimensions */
276
	  if (TYPEOF (lDimension) == INTSXP)
276
	  if (TYPEOF (lDimension) == INTSXP)
277
	    {
277
	    {
278
	      int i;
278
	      int i;
279
	      int lTotalSize = 1;
279
	      int lTotalSize = 1;
280
 
280
 
281
	      lData->type |= BDX_ARRAY;
281
	      lData->type |= BDX_ARRAY;
282
	      lData->dim_count = LENGTH (lDimension);
282
	      lData->dim_count = LENGTH (lDimension);
283
 
283
 
284
	      lData->dimensions =
284
	      lData->dimensions =
285
		(BDX_Dimension*) malloc (sizeof (BDX_Dimension)
285
		(BDX_Dimension*) malloc (sizeof (BDX_Dimension)
286
					 * lData->dim_count);
286
					 * lData->dim_count);
287
 
287
 
288
	      /* compute the total number of data elements */
288
	      /* compute the total number of data elements */
289
	      for (i = 0;i < lData->dim_count;i++)
289
	      for (i = 0;i < lData->dim_count;i++)
290
		{
290
		{
291
		  lData->dimensions[i] = INTEGER (lDimension)[i];
291
		  lData->dimensions[i] = INTEGER (lDimension)[i];
292
		  lTotalSize *= lData->dimensions[i];
292
		  lTotalSize *= lData->dimensions[i];
293
		}
293
		}
294
 
294
 
295
	      lData->raw_data =
295
	      lData->raw_data =
296
		(BDX_RawData*) malloc (sizeof (BDX_RawData)
296
		(BDX_RawData*) malloc (sizeof (BDX_RawData)
297
				       * lTotalSize);
297
				       * lTotalSize);
298
 
298
 
299
	      /* copy the data */
299
	      /* copy the data */
300
	      for (i = 0; i < lTotalSize;i++)
300
	      for (i = 0; i < lTotalSize;i++)
301
		{
301
		{
302
		  switch (lData->type & BDX_SMASK)
302
		  switch (lData->type & BDX_SMASK)
303
		    {
303
		    {
304
		    case BDX_BOOL:
304
		    case BDX_BOOL:
305
		      lData->raw_data[i].bool_value = LOGICAL (pExpression)[i];
305
		      lData->raw_data[i].bool_value = LOGICAL (pExpression)[i];
306
		      break;
306
		      break;
307
		    case BDX_INT:
307
		    case BDX_INT:
308
		      lData->raw_data[i].int_value = INTEGER (pExpression)[i];
308
		      lData->raw_data[i].int_value = INTEGER (pExpression)[i];
309
		      break;
309
		      break;
310
		    case BDX_DOUBLE:
310
		    case BDX_DOUBLE:
311
		      lData->raw_data[i].double_value = REAL (pExpression)[i];
311
		      lData->raw_data[i].double_value = REAL (pExpression)[i];
312
		      break;
312
		      break;
313
		    case BDX_STRING:
313
		    case BDX_STRING:
314
		      lData->raw_data[i].string_value = strdup (CHAR (STRING_ELT(pExpression, i)));
314
		      lData->raw_data[i].string_value = strdup (CHAR (STRING_ELT(pExpression, i)));
315
		      break;
315
		      break;
316
		    }
316
		    }
317
		}
317
		}
318
 
318
 
319
	      UNPROTECT (1); /* dimension */
319
	      UNPROTECT (1); /* dimension */
320
 
320
 
321
	      return SC_PROXY_OK;
321
	      return SC_PROXY_OK;
322
	    }
322
	    }
323
	  else
323
	  else
324
	    {
324
	    {
325
	      /* unknown error */
325
	      /* unknown error */
326
	      free (lData);
326
	      free (lData);
327
	      *pData = NULL;
327
	      *pData = NULL;
328
 
328
 
329
	      UNPROTECT (1); /* dimension */
329
	      UNPROTECT (1); /* dimension */
330
 
330
 
331
	      return SC_PROXY_ERR_UNKNOWN;
331
	      return SC_PROXY_ERR_UNKNOWN;
332
	    }
332
	    }
333
	}
333
	}
334
    }
334
    }
335
 
335
 
336
  return SC_PROXY_OK;
336
  return SC_PROXY_OK;
337
}
337
}
338
 
338
 
339
/* 00-02-18 | baier | parse parameter string and fill parameter structure */
339
/* 00-02-18 | baier | parse parameter string and fill parameter structure */
340
int R_Proxy_parse_parameters (char const* pParameterString,
340
int R_Proxy_parse_parameters (char const* pParameterString,
341
			      struct _R_Proxy_init_parameters* pParameterStruct)
341
			      struct _R_Proxy_init_parameters* pParameterStruct)
342
{
342
{
343
  /*
343
  /*
344
   * parameter string is of the form name1=value1;name2=value2;...
344
   * parameter string is of the form name1=value1;name2=value2;...
345
   *
345
   *
346
   * currently recognized parameter names (case-sensitive):
346
   * currently recognized parameter names (case-sensitive):
347
   *
347
   *
348
   *   NSIZE ... number of cons cells, (unsigned int) parameter
348
   *   NSIZE ... number of cons cells, (unsigned int) parameter
349
   *   VSIZE ... size of vector heap, (unsigned int) parameter
349
   *   VSIZE ... size of vector heap, (unsigned int) parameter
350
   */
350
   */
351
  int lDone = 0;
351
  int lDone = 0;
352
#if 0
352
#if 0
353
  char const* lParameterStart = pParameterString;
353
  char const* lParameterStart = pParameterString;
354
  int lIndexOfSemicolon = 0;
354
  int lIndexOfSemicolon = 0;
355
  char* lTmpBuffer = NULL;
355
  char* lTmpBuffer = NULL;
356
  char* lPosOfSemicolon = NULL;
356
  char* lPosOfSemicolon = NULL;
357
#endif
357
#endif
358
 
358
 
359
  while (!lDone)
359
  while (!lDone)
360
    {
360
    {
361
#if 0
361
#if 0
362
      /* NSIZE? */
362
      /* NSIZE? */
363
      if (strncmp (lParameterStart,"NSIZE=",6) == 0)
363
      if (strncmp (lParameterStart,"NSIZE=",6) == 0)
364
	{
364
	{
365
	  lParameterStart += 6;
365
	  lParameterStart += 6;
366
 
366
 
367
	  lPosOfSemicolon = strchr (lParameterStart,';');
367
	  lPosOfSemicolon = strchr (lParameterStart,';');
368
	  lIndexOfSemicolon = lPosOfSemicolon - lParameterStart;
368
	  lIndexOfSemicolon = lPosOfSemicolon - lParameterStart;
369
 
369
 
370
	  if (lPosOfSemicolon)
370
	  if (lPosOfSemicolon)
371
	    {
371
	    {
372
	      lTmpBuffer = malloc (lIndexOfSemicolon + 1); /* to catch NSIZE=; */
372
	      lTmpBuffer = malloc (lIndexOfSemicolon + 1); /* to catch NSIZE=; */
373
	      strncpy (lTmpBuffer,lParameterStart,lIndexOfSemicolon);
373
	      strncpy (lTmpBuffer,lParameterStart,lIndexOfSemicolon);
374
	      *(lTmpBuffer + lIndexOfSemicolon) = 0x0;
374
	      *(lTmpBuffer + lIndexOfSemicolon) = 0x0;
375
	      pParameterStruct->nsize_valid = 1;
375
	      pParameterStruct->nsize_valid = 1;
376
	      pParameterStruct->nsize = atoi (lTmpBuffer);
376
	      pParameterStruct->nsize = atoi (lTmpBuffer);
377
	      free (lTmpBuffer);
377
	      free (lTmpBuffer);
378
	      lParameterStart += lIndexOfSemicolon + 1;
378
	      lParameterStart += lIndexOfSemicolon + 1;
379
	    }
379
	    }
380
	  else
380
	  else
381
	    {
381
	    {
382
	      pParameterStruct->nsize_valid = 1;
382
	      pParameterStruct->nsize_valid = 1;
383
	      pParameterStruct->nsize = atoi (lParameterStart);
383
	      pParameterStruct->nsize = atoi (lParameterStart);
384
	      lDone = 1;
384
	      lDone = 1;
385
	    }
385
	    }
386
	}
386
	}
387
      else if (strncmp (lParameterStart,"VSIZE=",6) == 0)
387
      else if (strncmp (lParameterStart,"VSIZE=",6) == 0)
388
	{
388
	{
389
	  lParameterStart += 6;
389
	  lParameterStart += 6;
390
 
390
 
391
	  lPosOfSemicolon = strchr (lParameterStart,';');
391
	  lPosOfSemicolon = strchr (lParameterStart,';');
392
	  lIndexOfSemicolon = lPosOfSemicolon - lParameterStart;
392
	  lIndexOfSemicolon = lPosOfSemicolon - lParameterStart;
393
 
393
 
394
	  if (lPosOfSemicolon)
394
	  if (lPosOfSemicolon)
395
	    {
395
	    {
396
	      lTmpBuffer = malloc (lIndexOfSemicolon + 1); /* to catch VSIZE=; */
396
	      lTmpBuffer = malloc (lIndexOfSemicolon + 1); /* to catch VSIZE=; */
397
	      strncpy (lTmpBuffer,lParameterStart,lIndexOfSemicolon);
397
	      strncpy (lTmpBuffer,lParameterStart,lIndexOfSemicolon);
398
	      *(lTmpBuffer + lIndexOfSemicolon) = 0x0;
398
	      *(lTmpBuffer + lIndexOfSemicolon) = 0x0;
399
	      pParameterStruct->vsize_valid = 1;
399
	      pParameterStruct->vsize_valid = 1;
400
	      pParameterStruct->vsize = atoi (lTmpBuffer);
400
	      pParameterStruct->vsize = atoi (lTmpBuffer);
401
	      free (lTmpBuffer);
401
	      free (lTmpBuffer);
402
	      lParameterStart += lIndexOfSemicolon + 1;
402
	      lParameterStart += lIndexOfSemicolon + 1;
403
	    }
403
	    }
404
	  else
404
	  else
405
	    {
405
	    {
406
	      pParameterStruct->vsize_valid = 1;
406
	      pParameterStruct->vsize_valid = 1;
407
	      pParameterStruct->vsize = atoi (lParameterStart);
407
	      pParameterStruct->vsize = atoi (lParameterStart);
408
	      lDone = 1;
408
	      lDone = 1;
409
	    }
409
	    }
410
	}
410
	}
411
      else
411
      else
412
#endif
412
#endif
413
	{
413
	{
414
	  lDone = 1;
414
	  lDone = 1;
415
	}
415
	}
416
    }
416
    }
417
 
417
 
418
  return 0;
418
  return 0;
419
}
419
}
420
 
420
 
421
/* 00-02-18 | baier | R_Proxy_init() now takes parameter string, parse it */
421
/* 00-02-18 | baier | R_Proxy_init() now takes parameter string, parse it */
422
/* 03-06-01 | baier | now we add %R_HOME%\bin to %PATH% */
422
/* 03-06-01 | baier | now we add %R_HOME%\bin to %PATH% */
423
int R_Proxy_init (char const* pParameterString)
423
int R_Proxy_init (char const* pParameterString)
424
{
424
{
425
  structRstart rp;
425
  structRstart rp;
426
  Rstart Rp = &rp;
426
  Rstart Rp = &rp;
427
  char Rversion[25];
427
  char Rversion[25];
428
  static char RUser[MAX_PATH], RHome[MAX_PATH];
428
  static char RUser[MAX_PATH], RHome[MAX_PATH];
429
  char *p;
429
  char *p;
430
 
430
 
431
  sprintf(Rversion, "%s.%s", R_MAJOR, R_MINOR);
431
  sprintf(Rversion, "%s.%s", R_MAJOR, R_MINOR);
432
  if(strcmp(getDLLVersion(), Rversion) != 0) {
432
  if(strcmp(getDLLVersion(), Rversion) != 0) {
433
    fprintf(stderr, "Error: R.DLL version does not match\n");
433
    fprintf(stderr, "Error: R.DLL version does not match\n");
434
    return SC_PROXY_ERR_UNKNOWN;
434
    return SC_PROXY_ERR_UNKNOWN;
435
  }
435
  }
436
 
436
 
437
  R_DefParams(Rp);
437
  R_DefParams(Rp);
438
 
438
 
439
  /* first, try process-local environment space (CRT) */
439
  /* first, try process-local environment space (CRT) */
440
  if (getenv("R_HOME")) {
440
  if (getenv("R_HOME")) {
441
      strcpy(RHome, getenv("R_HOME"));
441
      strcpy(RHome, getenv("R_HOME"));
442
  } else {
442
  } else {
443
    /* get variable from process-local environment space (Windows API) */
443
    /* get variable from process-local environment space (Windows API) */
444
      if (GetEnvironmentVariable ("R_HOME", RHome, sizeof (RHome)) == 0) {
444
      if (GetEnvironmentVariable ("R_HOME", RHome, sizeof (RHome)) == 0) {
445
	/* not found, fall back to getRHOME() */
445
	/* not found, fall back to getRHOME() */
446
	strcpy(RHome, getRHOME());
446
	strcpy(RHome, getRHOME());
447
      }
447
      }
448
    }
448
    }
449
 
449
 
450
  /* now we add %R_HOME%\bin to %PATH% (for dynamically loaded modules there) */
450
  /* now we add %R_HOME%\bin to %PATH% (for dynamically loaded modules there) */
451
  {
451
  {
452
    char buf[2048];
452
    char buf[2048];
453
    sprintf(buf,"PATH=%s\\bin;%s",RHome,getenv("PATH"));
453
    sprintf(buf,"PATH=%s\\bin;%s",RHome,getenv("PATH"));
454
    putenv(buf);
454
    putenv(buf);
455
  }
455
  }
456
 
456
 
457
  Rp->rhome = RHome;
457
  Rp->rhome = RHome;
458
  /*
458
  /*
459
   * try R_USER then HOME then working directory
459
   * try R_USER then HOME then working directory
460
   */
460
   */
461
  if (getenv("R_USER")) {
461
  if (getenv("R_USER")) {
462
    strcpy(RUser, getenv("R_USER"));
462
    strcpy(RUser, getenv("R_USER"));
463
  } else if (getenv("HOME")) {
463
  } else if (getenv("HOME")) {
464
      strcpy(RUser, getenv("HOME"));
464
      strcpy(RUser, getenv("HOME"));
465
  } else if (getenv("HOMEDRIVE")) {
465
  } else if (getenv("HOMEDRIVE")) {
466
      strcpy(RUser, getenv("HOMEDRIVE"));
466
      strcpy(RUser, getenv("HOMEDRIVE"));
467
      strcat(RUser, getenv("HOMEPATH"));
467
      strcat(RUser, getenv("HOMEPATH"));
468
  } else
468
  } else
469
      GetCurrentDirectory(MAX_PATH, RUser);
469
      GetCurrentDirectory(MAX_PATH, RUser);
470
  p = RUser + (strlen(RUser) - 1);
470
  p = RUser + (strlen(RUser) - 1);
471
 
471
 
472
  if (*p == '/' || *p == '\\') *p = '\0';
472
  if (*p == '/' || *p == '\\') *p = '\0';
473
  Rp->home = RUser;
473
  Rp->home = RUser;
474
  Rp->CharacterMode = LinkDLL;
474
  Rp->CharacterMode = LinkDLL;
475
  Rp->ReadConsole = R_Proxy_ReadConsole;
475
  Rp->ReadConsole = R_Proxy_ReadConsole;
476
  Rp->WriteConsole = R_Proxy_WriteConsole;
476
  Rp->WriteConsole = R_Proxy_WriteConsole;
477
  Rp->CallBack = R_Proxy_CallBack;
477
  Rp->CallBack = R_Proxy_CallBack;
478
  Rp->message = R_Proxy_askok;
478
  Rp->message = R_Proxy_askok;
479
 
479
 
480
  Rp->yesnocancel = R_Proxy_askyesnocancel;
480
  Rp->yesnocancel = R_Proxy_askyesnocancel;
481
  Rp->busy = R_Proxy_Busy;
481
  Rp->busy = R_Proxy_Busy;
482
  Rp->R_Quiet = 1;
482
  Rp->R_Quiet = 1;
483
#if 1
483
#if 1
484
  /* run as "interactive", so server won't be killed after an error */
484
  /* run as "interactive", so server won't be killed after an error */
485
  Rp->R_Slave = Rp->R_Verbose = 0;
485
  Rp->R_Slave = Rp->R_Verbose = 0;
486
  Rp->R_Interactive = 1;
486
  Rp->R_Interactive = 1;
487
#else
487
#else
488
  Rp->R_Slave = Rp->R_Interactive = Rp->R_Verbose = 0;
488
  Rp->R_Slave = Rp->R_Interactive = Rp->R_Verbose = 0;
489
#endif
489
#endif
490
  Rp->RestoreAction = 0; /* no restore */
490
  Rp->RestoreAction = 0; /* no restore */
491
  Rp->SaveAction = 2;    /* no save */
491
  Rp->SaveAction = 2;    /* no save */
492
  Rp->CommandLineArgs = NULL;
492
  Rp->CommandLineArgs = NULL;
493
  Rp->NumCommandLineArgs = 0;
493
  Rp->NumCommandLineArgs = 0;
494
 
494
 
495
/*  Rp->nsize = 300000;
495
/*  Rp->nsize = 300000;
496
    Rp->vsize = 6e6;*/
496
    Rp->vsize = 6e6;*/
497
  R_SetParams(Rp); /* so R_ShowMessage is set */
497
  R_SetParams(Rp); /* so R_ShowMessage is set */
498
  R_SizeFromEnv(Rp);
498
  R_SizeFromEnv(Rp);
499
 
499
 
500
  /* parse parameters */
500
  /* parse parameters */
501
#if 0
501
#if 0
502
  {
502
  {
503
    struct _R_Proxy_init_parameters lParameterStruct =
503
    struct _R_Proxy_init_parameters lParameterStruct =
504
    {
504
    {
505
      0,0,0,0
505
      0,0,0,0
506
    };
506
    };
507
 
507
 
508
    R_Proxy_parse_parameters (pParameterString,&lParameterStruct);
508
    R_Proxy_parse_parameters (pParameterString,&lParameterStruct);
509
 
509
 
510
    if (lParameterStruct.nsize_valid)
510
    if (lParameterStruct.nsize_valid)
511
      {
511
      {
512
	Rp->nsize = lParameterStruct.nsize;
512
	Rp->nsize = lParameterStruct.nsize;
513
      }
513
      }
514
    if (lParameterStruct.vsize_valid)
514
    if (lParameterStruct.vsize_valid)
515
      {
515
      {
516
	Rp->vsize = lParameterStruct.vsize;
516
	Rp->vsize = lParameterStruct.vsize;
517
      }
517
      }
518
  }
518
  }
519
#endif
519
#endif
520
 
520
 
521
  R_SetParams(Rp);
521
  R_SetParams(Rp);
522
 
522
 
523
  setup_term_ui();
523
  setup_term_ui();
524
  setup_Rmainloop();
524
  setup_Rmainloop();
525
  R_ReplDLLinit();
525
  R_ReplDLLinit();
526
 
526
 
527
  return SC_PROXY_OK;
527
  return SC_PROXY_OK;
528
}
528
}
529
 
529
 
530
/* 01-06-05 | baier | SETJMP and fatal error handling around eval() */
530
/* 01-06-05 | baier | SETJMP and fatal error handling around eval() */
531
int R_Proxy_evaluate (char const* pCmd,BDX_Data** pData)
531
int R_Proxy_evaluate (char const* pCmd,BDX_Data** pData)
532
{
532
{
533
  SEXP rho = R_GlobalEnv;
533
  SEXP rho = R_GlobalEnv;
534
  IoBuffer lBuffer;
534
  IoBuffer lBuffer;
535
  SEXP lSexp;
535
  SEXP lSexp;
536
  int lRc;
536
  int lRc;
537
  ParseStatus lStatus;
537
  ParseStatus lStatus;
538
  SEXP lResult;
538
  SEXP lResult;
539
 
539
 
540
  /* for SETJMP/LONGJMP */
540
  /* for SETJMP/LONGJMP */
541
  s_EvalInProgress = 0;
541
  s_EvalInProgress = 0;
542
 
542
 
543
  R_IoBufferInit (&lBuffer);
543
  R_IoBufferInit (&lBuffer);
544
  R_IoBufferPuts ((char*) pCmd,&lBuffer);
544
  R_IoBufferPuts ((char*) pCmd,&lBuffer);
545
  R_IoBufferPuts ("\n",&lBuffer);
545
  R_IoBufferPuts ("\n",&lBuffer);
546
 
546
 
547
  /* don't generate code, just a try */
547
  /* don't generate code, just a try */
548
  R_IoBufferReadReset (&lBuffer);
548
  R_IoBufferReadReset (&lBuffer);
549
  lSexp = R_Parse1Buffer (&lBuffer,0,&lStatus);
549
  lSexp = R_Parse1Buffer (&lBuffer,0,&lStatus);
550
 
550
 
551
  switch (lStatus)
551
  switch (lStatus)
552
    {
552
    {
553
    case PARSE_NULL:
553
    case PARSE_NULL:
554
      /* we forget the IoBuffer "lBuffer", so don't do anything here */
554
      /* we forget the IoBuffer "lBuffer", so don't do anything here */
555
      lRc = SC_PROXY_ERR_PARSE_INVALID;
555
      lRc = SC_PROXY_ERR_PARSE_INVALID;
556
      break;
556
      break;
557
    case PARSE_OK:
557
    case PARSE_OK:
558
      /* now generate code */
558
      /* now generate code */
559
      R_IoBufferReadReset (&lBuffer);
559
      R_IoBufferReadReset (&lBuffer);
560
      lSexp = R_Parse1Buffer (&lBuffer,1,&lStatus);
560
      lSexp = R_Parse1Buffer (&lBuffer,1,&lStatus);
561
      R_Visible = 0;
561
      R_Visible = 0;
562
      R_EvalDepth = 0;
562
      R_EvalDepth = 0;
563
      PROTECT(lSexp);
563
      PROTECT(lSexp);
564
      {
564
      {
565
	SETJMP (R_Toplevel.cjmpbuf);
565
	SETJMP (R_Toplevel.cjmpbuf);
566
	R_GlobalContext = R_ToplevelContext = &R_Toplevel;
566
	R_GlobalContext = R_ToplevelContext = &R_Toplevel;
567
 
567
 
568
	if (!s_EvalInProgress)
568
	if (!s_EvalInProgress)
569
	  {
569
	  {
570
	    s_EvalInProgress = 1;
570
	    s_EvalInProgress = 1;
571
	    lResult = eval (lSexp,rho);
571
	    lResult = eval (lSexp,rho);
572
	    s_EvalInProgress = 0;
572
	    s_EvalInProgress = 0;
573
	  }
573
	  }
574
	else
574
	else
575
	  {
575
	  {
576
	    return SC_PROXY_ERR_EVALUATE_STOP;
576
	    return SC_PROXY_ERR_EVALUATE_STOP;
577
	  }
577
	  }
578
      }
578
      }
579
      lRc = SEXP2BDX_Data (lResult,pData);
579
      lRc = SEXP2BDX_Data (lResult,pData);
580
      /* no last value */
580
      /* no last value */
581
      UNPROTECT(1);
581
      UNPROTECT(1);
582
      break;
582
      break;
583
    case PARSE_ERROR:
583
    case PARSE_ERROR:
584
      lRc = SC_PROXY_ERR_PARSE_INVALID;
584
      lRc = SC_PROXY_ERR_PARSE_INVALID;
585
      break;
585
      break;
586
    case PARSE_INCOMPLETE:
586
    case PARSE_INCOMPLETE:
587
      lRc = SC_PROXY_ERR_PARSE_INCOMPLETE;
587
      lRc = SC_PROXY_ERR_PARSE_INCOMPLETE;
588
      break;
588
      break;
589
    case PARSE_EOF:
589
    case PARSE_EOF:
590
      lRc = SC_PROXY_ERR_PARSE_INVALID;
590
      lRc = SC_PROXY_ERR_PARSE_INVALID;
591
      break;
591
      break;
592
    default:
592
    default:
593
      /* never reached */
593
      /* never reached */
594
      lRc = SC_PROXY_ERR_UNKNOWN;
594
      lRc = SC_PROXY_ERR_UNKNOWN;
595
      break;
595
      break;
596
    }
596
    }
597
 
597
 
598
  return lRc;
598
  return lRc;
599
}
599
}
600
 
600
 
601
/* 01-06-05 | baier | SETJMP and fatal error handling around eval() */
601
/* 01-06-05 | baier | SETJMP and fatal error handling around eval() */
602
int R_Proxy_evaluate_noreturn (char const* pCmd)
602
int R_Proxy_evaluate_noreturn (char const* pCmd)
603
{
603
{
604
  SEXP rho = R_GlobalEnv;
604
  SEXP rho = R_GlobalEnv;
605
  IoBuffer lBuffer;
605
  IoBuffer lBuffer;
606
  SEXP lSexp;
606
  SEXP lSexp;
607
  int lRc;
607
  int lRc;
608
  ParseStatus lStatus;
608
  ParseStatus lStatus;
609
 
609
 
610
  /* for SETJMP/LONGJMP */
610
  /* for SETJMP/LONGJMP */
611
  s_EvalInProgress = 0;
611
  s_EvalInProgress = 0;
612
 
612
 
613
  R_IoBufferInit (&lBuffer);
613
  R_IoBufferInit (&lBuffer);
614
  R_IoBufferPuts ((char*) pCmd,&lBuffer);
614
  R_IoBufferPuts ((char*) pCmd,&lBuffer);
615
  R_IoBufferPuts ("\n",&lBuffer);
615
  R_IoBufferPuts ("\n",&lBuffer);
616
 
616
 
617
  /* don't generate code, just a try */
617
  /* don't generate code, just a try */
618
  R_IoBufferReadReset (&lBuffer);
618
  R_IoBufferReadReset (&lBuffer);
619
  lSexp = R_Parse1Buffer (&lBuffer,0,&lStatus);
619
  lSexp = R_Parse1Buffer (&lBuffer,0,&lStatus);
620
 
620
 
621
  switch (lStatus)
621
  switch (lStatus)
622
    {
622
    {
623
    case PARSE_NULL:
623
    case PARSE_NULL:
624
      /* we forget the IoBuffer "lBuffer", so don't do anything here */
624
      /* we forget the IoBuffer "lBuffer", so don't do anything here */
625
      lRc = SC_PROXY_ERR_PARSE_INVALID;
625
      lRc = SC_PROXY_ERR_PARSE_INVALID;
626
      break;
626
      break;
627
    case PARSE_OK:
627
    case PARSE_OK:
628
      /* now generate code */
628
      /* now generate code */
629
      R_IoBufferReadReset (&lBuffer);
629
      R_IoBufferReadReset (&lBuffer);
630
      lSexp = R_Parse1Buffer (&lBuffer,1,&lStatus);
630
      lSexp = R_Parse1Buffer (&lBuffer,1,&lStatus);
631
      R_Visible = 0;
631
      R_Visible = 0;
632
      R_EvalDepth = 0;
632
      R_EvalDepth = 0;
633
      PROTECT(lSexp);
633
      PROTECT(lSexp);
634
      /* at the moment, discard the result of the eval */
634
      /* at the moment, discard the result of the eval */
635
      {
635
      {
636
	SETJMP (R_Toplevel.cjmpbuf);
636
	SETJMP (R_Toplevel.cjmpbuf);
637
	R_GlobalContext = R_ToplevelContext = &R_Toplevel;
637
	R_GlobalContext = R_ToplevelContext = &R_Toplevel;
638
 
638
 
639
	if (!s_EvalInProgress)
639
	if (!s_EvalInProgress)
640
	  {
640
	  {
641
	    s_EvalInProgress = 1;
641
	    s_EvalInProgress = 1;
642
	    eval (lSexp,rho);
642
	    eval (lSexp,rho);
643
	    s_EvalInProgress = 0;
643
	    s_EvalInProgress = 0;
644
	  }
644
	  }
645
	else
645
	else
646
	  {
646
	  {
647
	    return SC_PROXY_ERR_EVALUATE_STOP;
647
	    return SC_PROXY_ERR_EVALUATE_STOP;
648
	  }
648
	  }
649
      }
649
      }
650
      /* no last value */
650
      /* no last value */
651
      UNPROTECT(1);
651
      UNPROTECT(1);
652
      lRc = SC_PROXY_OK;
652
      lRc = SC_PROXY_OK;
653
      break;
653
      break;
654
    case PARSE_ERROR:
654
    case PARSE_ERROR:
655
      lRc = SC_PROXY_ERR_PARSE_INVALID;
655
      lRc = SC_PROXY_ERR_PARSE_INVALID;
656
      break;
656
      break;
657
    case PARSE_INCOMPLETE:
657
    case PARSE_INCOMPLETE:
658
      lRc = SC_PROXY_ERR_PARSE_INCOMPLETE;
658
      lRc = SC_PROXY_ERR_PARSE_INCOMPLETE;
659
      break;
659
      break;
660
    case PARSE_EOF:
660
    case PARSE_EOF:
661
      lRc = SC_PROXY_ERR_PARSE_INVALID;
661
      lRc = SC_PROXY_ERR_PARSE_INVALID;
662
      break;
662
      break;
663
    default:
663
    default:
664
      /* never reached */
664
      /* never reached */
665
      lRc = SC_PROXY_ERR_UNKNOWN;
665
      lRc = SC_PROXY_ERR_UNKNOWN;
666
      break;
666
      break;
667
    }
667
    }
668
 
668
 
669
  return lRc;
669
  return lRc;
670
}
670
}
671
 
671
 
672
int R_Proxy_get_symbol (char const* pSymbol,BDX_Data** pData)
672
int R_Proxy_get_symbol (char const* pSymbol,BDX_Data** pData)
673
{
673
{
674
  IoBuffer lBuffer;
674
  IoBuffer lBuffer;
675
  SEXP lSexp;
675
  SEXP lSexp;
676
  SEXP lVar;
676
  SEXP lVar;
677
  ParseStatus lStatus;
677
  ParseStatus lStatus;
678
 
678
 
679
  R_IoBufferInit (&lBuffer);
679
  R_IoBufferInit (&lBuffer);
680
  R_IoBufferPuts ((char*) pSymbol,&lBuffer);
680
  R_IoBufferPuts ((char*) pSymbol,&lBuffer);
681
  R_IoBufferPuts ("\n",&lBuffer);
681
  R_IoBufferPuts ("\n",&lBuffer);
682
 
682
 
683
  /* don't generate code, just a try */
683
  /* don't generate code, just a try */
684
  R_IoBufferReadReset (&lBuffer);
684
  R_IoBufferReadReset (&lBuffer);
685
  lSexp = R_Parse1Buffer (&lBuffer,0,&lStatus);
685
  lSexp = R_Parse1Buffer (&lBuffer,0,&lStatus);
686
 
686
 
687
  if (lStatus == PARSE_OK)
687
  if (lStatus == PARSE_OK)
688
    {
688
    {
689
      /* now generate code */
689
      /* now generate code */
690
      R_IoBufferReadReset (&lBuffer);
690
      R_IoBufferReadReset (&lBuffer);
691
      lSexp = R_Parse1Buffer (&lBuffer,1,&lStatus);
691
      lSexp = R_Parse1Buffer (&lBuffer,1,&lStatus);
692
      R_Visible = 0;
692
      R_Visible = 0;
693
      R_EvalDepth = 0;
693
      R_EvalDepth = 0;
694
      PROTECT(lSexp);
694
      PROTECT(lSexp);
695
 
695
 
696
      /* check for valid symbol... */
696
      /* check for valid symbol... */
697
      if (TYPEOF (lSexp) != SYMSXP)
697
      if (TYPEOF (lSexp) != SYMSXP)
698
	{
698
	{
699
	  printf (">> %s is not a symbol\n",pSymbol);
699
	  printf (">> %s is not a symbol\n",pSymbol);
700
	  UNPROTECT (1);
700
	  UNPROTECT (1);
701
	  return SC_PROXY_ERR_INVALIDSYMBOL;
701
	  return SC_PROXY_ERR_INVALIDSYMBOL;
702
	}
702
	}
703
 
703
 
704
      lVar = findVar (lSexp,R_GlobalEnv);
704
      lVar = findVar (lSexp,R_GlobalEnv);
705
 
705
 
706
      if (lVar == R_UnboundValue)
706
      if (lVar == R_UnboundValue)
707
	{
707
	{
708
	  printf (">> %s is an unbound value\n",pSymbol);
708
	  printf (">> %s is an unbound value\n",pSymbol);
709
	  UNPROTECT (1);
709
	  UNPROTECT (1);
710
	  return SC_PROXY_ERR_INVALIDSYMBOL;
710
	  return SC_PROXY_ERR_INVALIDSYMBOL;
711
	}
711
	}
712
 
712
 
713
      {
713
      {
714
	int lRc = SEXP2BDX_Data (lVar,pData);
714
	int lRc = SEXP2BDX_Data (lVar,pData);
715
	UNPROTECT (1);
715
	UNPROTECT (1);
716
 
716
 
717
	return lRc;
717
	return lRc;
718
      }
718
      }
719
    }
719
    }
720
 
720
 
721
  return SC_PROXY_OK;
721
  return SC_PROXY_OK;
722
}
722
}
723
 
723
 
724
int R_Proxy_set_symbol (char const* pSymbol,BDX_Data const* pData)
724
int R_Proxy_set_symbol (char const* pSymbol,BDX_Data const* pData)
725
{
725
{
726
  SEXP lSymbol = 0;
726
  SEXP lSymbol = 0;
727
  SEXP lData = 0;
727
  SEXP lData = 0;
728
  int lProtectCount = 1;
728
  int lProtectCount = 1;
729
  int lRet = SC_PROXY_OK;
729
  int lRet = SC_PROXY_OK;
730
 
730
 
731
  switch (pData->type & BDX_CMASK)
731
  switch (pData->type & BDX_CMASK)
732
    {
732
    {
733
      /* scalar? */
733
      /* scalar? */
734
    case BDX_SCALAR:
734
    case BDX_SCALAR:
735
      {
735
      {
736
	switch (pData->type & BDX_SMASK)
736
	switch (pData->type & BDX_SMASK)
737
	  {
737
	  {
738
	  case BDX_BOOL:
738
	  case BDX_BOOL:
739
	    lData = PROTECT (allocVector (LGLSXP,1));
739
	    lData = PROTECT (allocVector (LGLSXP,1));
740
	    LOGICAL(lData)[0] = pData->raw_data[0].bool_value;
740
	    LOGICAL(lData)[0] = pData->raw_data[0].bool_value;
741
	    break;
741
	    break;
742
	  case BDX_INT:
742
	  case BDX_INT:
743
	    lData = PROTECT (allocVector (INTSXP,1));
743
	    lData = PROTECT (allocVector (INTSXP,1));
744
	    INTEGER(lData)[0] = pData->raw_data[0].int_value;
744
	    INTEGER(lData)[0] = pData->raw_data[0].int_value;
745
	    break;
745
	    break;
746
	  case BDX_DOUBLE:
746
	  case BDX_DOUBLE:
747
	    lData = PROTECT (allocVector (REALSXP,1));
747
	    lData = PROTECT (allocVector (REALSXP,1));
748
	    REAL(lData)[0] = pData->raw_data[0].double_value;
748
	    REAL(lData)[0] = pData->raw_data[0].double_value;
749
	    break;
749
	    break;
750
	  case BDX_STRING:
750
	  case BDX_STRING:
751
	    {
751
	    {
752
	      SEXP lStringSExp =
752
	      SEXP lStringSExp =
753
		allocString (strlen (pData->raw_data[0].string_value));
753
		allocString (strlen (pData->raw_data[0].string_value));
754
	      PROTECT (lStringSExp); lProtectCount++;
754
	      PROTECT (lStringSExp); lProtectCount++;
755
	      strcpy (CHAR(lStringSExp),pData->raw_data[0].string_value);
755
	      strcpy (CHAR(lStringSExp),pData->raw_data[0].string_value);
756
	      lData = PROTECT (allocVector (STRSXP,1));
756
	      lData = PROTECT (allocVector (STRSXP,1));
757
	      SET_STRING_ELT(lData, 0, lStringSExp);
757
	      SET_STRING_ELT(lData, 0, lStringSExp);
758
	    }
758
	    }
759
	    break;
759
	    break;
760
	  default:
760
	  default:
761
	    lRet = SC_PROXY_ERR_UNSUPPORTEDTYPE;
761
	    lRet = SC_PROXY_ERR_UNSUPPORTEDTYPE;
762
	  }
762
	  }
763
      }
763
      }
764
 
764
 
765
      break;
765
      break;
766
      /* vectors or arrays */
766
      /* vectors or arrays */
767
    case BDX_VECTOR:
767
    case BDX_VECTOR:
768
    case BDX_ARRAY:
768
    case BDX_ARRAY:
769
      {
769
      {
770
	/* allocate a dimensions vector */
770
	/* allocate a dimensions vector */
771
	SEXP lDimensions;
771
	SEXP lDimensions;
772
	unsigned int i;
772
	unsigned int i;
773
	unsigned int lTotalSize = 1;
773
	unsigned int lTotalSize = 1;
774
 
774
 
775
	PROTECT (lDimensions = allocVector (INTSXP,pData->dim_count));
775
	PROTECT (lDimensions = allocVector (INTSXP,pData->dim_count));
776
	lProtectCount++;
776
	lProtectCount++;
777
 
777
 
778
	for (i = 0;i < pData->dim_count;i++)
778
	for (i = 0;i < pData->dim_count;i++)
779
	  {
779
	  {
780
	    INTEGER (lDimensions)[i] = pData->dimensions[i];
780
	    INTEGER (lDimensions)[i] = pData->dimensions[i];
781
	    lTotalSize *= pData->dimensions[i];
781
	    lTotalSize *= pData->dimensions[i];
782
	  }
782
	  }
783
 
783
 
784
	switch (pData->type & BDX_SMASK)
784
	switch (pData->type & BDX_SMASK)
785
	  {
785
	  {
786
	  case BDX_BOOL:
786
	  case BDX_BOOL:
787
	    lData = PROTECT (allocVector (LGLSXP,lTotalSize));
787
	    lData = PROTECT (allocVector (LGLSXP,lTotalSize));
788
	    setAttrib (lData,R_DimSymbol,lDimensions);
788
	    setAttrib (lData,R_DimSymbol,lDimensions);
789
 
789
 
790
	    for (i = 0;i < lTotalSize;i++)
790
	    for (i = 0;i < lTotalSize;i++)
791
	      {
791
	      {
792
		LOGICAL(lData)[i] = pData->raw_data[i].bool_value;
792
		LOGICAL(lData)[i] = pData->raw_data[i].bool_value;
793
	      }
793
	      }
794
	    break;
794
	    break;
795
	  case BDX_INT:
795
	  case BDX_INT:
796
	    lData = PROTECT (allocVector (INTSXP,lTotalSize));
796
	    lData = PROTECT (allocVector (INTSXP,lTotalSize));
797
	    setAttrib (lData,R_DimSymbol,lDimensions);
797
	    setAttrib (lData,R_DimSymbol,lDimensions);
798
 
798
 
799
	    for (i = 0;i < lTotalSize;i++)
799
	    for (i = 0;i < lTotalSize;i++)
800
	      {
800
	      {
801
		INTEGER(lData)[i] = pData->raw_data[i].int_value;
801
		INTEGER(lData)[i] = pData->raw_data[i].int_value;
802
	      }
802
	      }
803
	    break;
803
	    break;
804
	  case BDX_DOUBLE:
804
	  case BDX_DOUBLE:
805
	    lData = PROTECT (allocVector (REALSXP,lTotalSize));
805
	    lData = PROTECT (allocVector (REALSXP,lTotalSize));
806
	    setAttrib (lData,R_DimSymbol,lDimensions);
806
	    setAttrib (lData,R_DimSymbol,lDimensions);
807
 
807
 
808
	    for (i = 0;i < lTotalSize;i++)
808
	    for (i = 0;i < lTotalSize;i++)
809
	      {
809
	      {
810
		REAL(lData)[i] = pData->raw_data[i].double_value;
810
		REAL(lData)[i] = pData->raw_data[i].double_value;
811
	      }
811
	      }
812
	    break;
812
	    break;
813
	  case BDX_STRING:
813
	  case BDX_STRING:
814
	    {
814
	    {
815
	      lData = PROTECT (allocVector (STRSXP,lTotalSize));
815
	      lData = PROTECT (allocVector (STRSXP,lTotalSize));
816
	      setAttrib (lData,R_DimSymbol,lDimensions);
816
	      setAttrib (lData,R_DimSymbol,lDimensions);
817
 
817
 
818
	      for (i = 0;i < lTotalSize;i++)
818
	      for (i = 0;i < lTotalSize;i++)
819
		{
819
		{
820
		  SEXP lStringSExp;
820
		  SEXP lStringSExp;
821
		  lStringSExp =
821
		  lStringSExp =
822
		    allocString (strlen (pData->raw_data[i].string_value));
822
		    allocString (strlen (pData->raw_data[i].string_value));
823
		  PROTECT (lStringSExp); lProtectCount++;
823
		  PROTECT (lStringSExp); lProtectCount++;
824
		  strcpy (CHAR(lStringSExp),pData->raw_data[i].string_value);
824
		  strcpy (CHAR(lStringSExp),pData->raw_data[i].string_value);
825
		  SET_STRING_ELT(lData, i, lStringSExp);
825
		  SET_STRING_ELT(lData, i, lStringSExp);
826
		}
826
		}
827
	    }
827
	    }
828
	    break;
828
	    break;
829
	  default:
829
	  default:
830
	    lRet = SC_PROXY_ERR_UNSUPPORTEDTYPE;
830
	    lRet = SC_PROXY_ERR_UNSUPPORTEDTYPE;
831
	  }
831
	  }
832
      }
832
      }
833
      break;
833
      break;
834
    default:
834
    default:
835
      lRet = SC_PROXY_ERR_UNSUPPORTEDTYPE;
835
      lRet = SC_PROXY_ERR_UNSUPPORTEDTYPE;
836
    }
836
    }
837
 
837
 
838
  if (lRet != SC_PROXY_OK)
838
  if (lRet != SC_PROXY_OK)
839
    {
839
    {
840
      return lRet;
840
      return lRet;
841
    }
841
    }
842
 
842
 
843
  /* install a new symbol or get the existing symbol */
843
  /* install a new symbol or get the existing symbol */
844
  lSymbol = install ((char*) pSymbol);
844
  lSymbol = install ((char*) pSymbol);
845
 
845
 
846
  /* and set the data to the symbol */
846
  /* and set the data to the symbol */
847
  setVar(lSymbol,lData,R_GlobalEnv);
847
  setVar(lSymbol,lData,R_GlobalEnv);
848
 
848
 
849
  UNPROTECT (lProtectCount);
849
  UNPROTECT (lProtectCount);
850
 
850
 
851
  return SC_PROXY_OK;
851
  return SC_PROXY_OK;
852
}
852
}
853
 
853
 
854
int R_Proxy_term ()
854
int R_Proxy_term ()
855
{
855
{
856
  end_Rmainloop();
856
  end_Rmainloop();
857
 
857
 
858
  return SC_PROXY_OK;
858
  return SC_PROXY_OK;
859
}
859
}
860
 
860