The R Project SVN R

Rev

Rev 75449 | Rev 79007 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 75449 Rev 75571
Line 20... Line 20...
20
 
20
 
21
#ifdef HAVE_CONFIG_H
21
#ifdef HAVE_CONFIG_H
22
#include <config.h>
22
#include <config.h>
23
#endif
23
#endif
24
 
24
 
25
#define R_USE_SIGNALS 1
-
 
26
#include <Defn.h>
25
#include <Parse.h> // -> IOStuff.h, Defn.h
27
#include <Internal.h>
26
#include <Internal.h>
28
#include <Fileio.h>
27
#include <Fileio.h>
29
#include <IOStuff.h>
-
 
30
#include <Parse.h>
-
 
31
#include <Rconnections.h>
28
#include <Rconnections.h>
32
#include <IOStuff.h> // for R_ConsoleIob;
-
 
33
 
29
 
34
SEXP attribute_hidden getParseContext(void)
30
SEXP attribute_hidden getParseContext(void)
35
{
31
{
36
    int i, last = PARSE_CONTEXT_SIZE;
32
    int i, last = PARSE_CONTEXT_SIZE;
37
    char context[PARSE_CONTEXT_SIZE+1];
33
    char context[PARSE_CONTEXT_SIZE+1];
Line 200... Line 196...
200
 .Internal( parse(file, n, text, prompt, srcfile, encoding) )
196
 .Internal( parse(file, n, text, prompt, srcfile, encoding) )
201
 If there is text then that is read and the other arguments are ignored.
197
 If there is text then that is read and the other arguments are ignored.
202
*/
198
*/
203
SEXP attribute_hidden do_parse(SEXP call, SEXP op, SEXP args, SEXP env)
199
SEXP attribute_hidden do_parse(SEXP call, SEXP op, SEXP args, SEXP env)
204
{
200
{
205
    SEXP text, prompt, s, source;
-
 
206
    Rconnection con;
-
 
207
    Rboolean wasopen, allKnown = TRUE;
-
 
208
    int ifile, num, i;
-
 
209
    const char *encoding;
-
 
210
    ParseStatus status;
-
 
211
    RCNTXT cntxt;
-
 
212
    parse_cleanup_info pci;
-
 
213
 
-
 
214
    pci.con = NULL;
-
 
215
    pci.old_latin1 = known_to_be_latin1;
-
 
216
    pci.old_utf8 = known_to_be_utf8;
-
 
217
 
-
 
218
    checkArity(op, args);
201
    checkArity(op, args);
219
    if(!inherits(CAR(args), "connection"))
202
    if(!inherits(CAR(args), "connection"))
220
	error(_("'file' must be a character string or connection"));
203
	error(_("'file' must be a character string or connection"));
221
    R_ParseError = 0;
204
    R_ParseError = 0;
222
    R_ParseErrorMsg[0] = '\0';
205
    R_ParseErrorMsg[0] = '\0';
223
 
206
 
224
    ifile = asInteger(CAR(args));                       args = CDR(args);
207
    int ifile = asInteger(CAR(args));                   args = CDR(args);
225
 
-
 
226
    con = getConnection(ifile);
208
    Rconnection con = getConnection(ifile);
227
    wasopen = con->isopen;
209
    Rboolean wasopen = con->isopen;
228
    num = asInteger(CAR(args));				args = CDR(args);
210
    int num = asInteger(CAR(args));			args = CDR(args);
229
    if (num == 0)
211
    if (num == 0)
230
	return(allocVector(EXPRSXP, 0));
212
	return(allocVector(EXPRSXP, 0));
231
 
213
 
232
    PROTECT(text = coerceVector(CAR(args), STRSXP));
214
    SEXP text = PROTECT(coerceVector(CAR(args), STRSXP));
233
    if(length(CAR(args)) && !length(text))
215
    if(length(CAR(args)) && !length(text))
234
	error(_("coercion of 'text' to character was unsuccessful"));
216
	error(_("coercion of 'text' to character was unsuccessful"));
235
    args = CDR(args);
217
    args = CDR(args);
236
    prompt = CAR(args);					args = CDR(args);
218
    SEXP prompt = CAR(args);				args = CDR(args);
237
    source = CAR(args);					args = CDR(args);
219
    SEXP source = CAR(args);				args = CDR(args);
238
    if(!isString(CAR(args)) || LENGTH(CAR(args)) != 1)
220
    if(!isString(CAR(args)) || LENGTH(CAR(args)) != 1)
239
	error(_("invalid '%s' value"), "encoding");
221
	error(_("invalid '%s' value"), "encoding");
240
    encoding = CHAR(STRING_ELT(CAR(args), 0)); /* ASCII */
222
    const char *encoding = CHAR(STRING_ELT(CAR(args), 0)); /* ASCII */
241
 
223
 
-
 
224
    parse_cleanup_info pci;
-
 
225
    pci.con = NULL;
-
 
226
    pci.old_latin1 = known_to_be_latin1;
-
 
227
    pci.old_utf8 = known_to_be_utf8;
-
 
228
    RCNTXT cntxt;
242
    /* set up context to recover known_to_be_* and to close connection on
229
    /* set up context to recover known_to_be_* and to close connection on
243
       error if opened by do_parse */
230
       error if opened by do_parse */
244
    begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
231
    begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
245
		 R_NilValue, R_NilValue);
232
		 R_NilValue, R_NilValue);
246
    cntxt.cend = &parse_cleanup;
233
    cntxt.cend = &parse_cleanup;
247
    cntxt.cenddata = &pci;
234
    cntxt.cenddata = &pci;
248
 
235
 
249
    known_to_be_latin1 = known_to_be_utf8 = FALSE;
236
    known_to_be_latin1 = known_to_be_utf8 = FALSE;
-
 
237
    Rboolean allKnown = TRUE;
250
    /* allow 'encoding' to override declaration on 'text'. */
238
    /* allow 'encoding' to override declaration on 'text'. */
251
    if(streql(encoding, "latin1")) {
239
    if(streql(encoding, "latin1")) {
252
	known_to_be_latin1 = TRUE;
240
	known_to_be_latin1 = TRUE;
253
	allKnown = FALSE;
241
	allKnown = FALSE;
254
    } else if(streql(encoding, "UTF-8"))  {
242
    } else if(streql(encoding, "UTF-8"))  {
Line 260... Line 248...
260
    if (prompt == R_NilValue)
248
    if (prompt == R_NilValue)
261
	PROTECT(prompt);
249
	PROTECT(prompt);
262
    else
250
    else
263
	PROTECT(prompt = coerceVector(prompt, STRSXP));
251
	PROTECT(prompt = coerceVector(prompt, STRSXP));
264
 
252
 
-
 
253
    ParseStatus status;
-
 
254
    SEXP s;
265
    if (length(text) > 0) {
255
    if (length(text) > 0) {
266
	/* If 'text' has known encoding then we can be sure it will be
256
	/* If 'text' has known encoding then we can be sure it will be
267
	   correctly re-encoded to the current encoding by
257
	   correctly re-encoded to the current encoding by
268
	   translateChar in the parser and so could mark the result in
258
	   translateChar in the parser and so could mark the result in
269
	   a Latin-1 or UTF-8 locale.
259
	   a Latin-1 or UTF-8 locale.
270
 
260
 
271
	   A small complication is that different elements could have
261
	   A small complication is that different elements could have
272
	   different encodings, but all that matters is that all
262
	   different encodings, but all that matters is that all
273
	   non-ASCII elements have known encoding.
263
	   non-ASCII elements have known encoding.
274
	*/
264
	*/
275
	for(i = 0; i < length(text); i++)
265
	for(int i = 0; i < length(text); i++)
276
	    if(!ENC_KNOWN(STRING_ELT(text, i)) &&
266
	    if(!ENC_KNOWN(STRING_ELT(text, i)) &&
277
	       !IS_ASCII(STRING_ELT(text, i))) {
267
	       !IS_ASCII(STRING_ELT(text, i))) {
278
		allKnown = FALSE;
268
		allKnown = FALSE;
279
		break;
269
		break;
280
	    }
270
	    }