/src/r-source/src/main/scan.c
Line | Count | Source |
1 | | /* |
2 | | * R : A Computer Language for Statistical Data Analysis |
3 | | * Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka |
4 | | * Copyright (C) 1998-2024 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> |
22 | | byte-level access needed checks. |
23 | | OK in UTF-8 provided quotes, comment, sep and dec chars are ASCII. |
24 | | Also OK in DBCS. |
25 | | |
26 | | We use only ' ', tab, CR, LF as space chars. |
27 | | There is also the possibility of other digits (which we should |
28 | | probably continue to ignore). |
29 | | */ |
30 | | |
31 | | #ifdef HAVE_CONFIG_H |
32 | | #include <config.h> |
33 | | #endif |
34 | | |
35 | | #define R_USE_SIGNALS 1 |
36 | | #include <Defn.h> |
37 | | #include <Internal.h> |
38 | | #include <float.h> /* for DBL_DIG */ |
39 | | #include <Fileio.h> |
40 | | #include <Rconnections.h> |
41 | | #include <errno.h> |
42 | | #include <Print.h> |
43 | | #include "RBufferUtils.h" /* for R_StringBuffer, used in LocalData */ |
44 | | |
45 | | #include <rlocale.h> /* for btowc */ |
46 | | |
47 | | #ifdef Win32 |
48 | | #include <trioremap.h> /* for %lld */ |
49 | | #endif |
50 | | |
51 | | /* The size of vector initially allocated by scan */ |
52 | 18.4k | #define SCAN_BLOCKSIZE 1000 |
53 | | /* The size of the console buffer */ |
54 | | /* NB: in Windows this also needs to be set in gnuwin32/getline/getline.c */ |
55 | 0 | #define CONSOLE_PROMPT_SIZE 256 |
56 | | |
57 | 39.0k | #define NO_COMCHAR 100000 /* won't occur even in Unicode */ |
58 | | |
59 | | |
60 | | /* The number of distinct strings to track */ |
61 | | #define MAX_STRINGS 10000 |
62 | | |
63 | | |
64 | | static unsigned char ConsoleBuf[CONSOLE_BUFFER_SIZE+1], *ConsoleBufp; |
65 | | static int ConsoleBufCnt; |
66 | | static char ConsolePrompt[CONSOLE_PROMPT_SIZE]; |
67 | | |
68 | | typedef struct { |
69 | | SEXP NAstrings; |
70 | | int quiet; |
71 | | int sepchar; /* = 0 */ /* This gets compared to ints */ |
72 | | char decchar; /* = '.' */ /* This only gets compared to chars */ |
73 | | char *quoteset; /* = NULL */ |
74 | | int comchar; /* = NO_COMCHAR */ |
75 | | int ttyflag; /* = 0 */ |
76 | | Rconnection con; /* = NULL */ |
77 | | bool wasopen; /* = false */ |
78 | | bool escapes; /* = false */ |
79 | | int save; /* = 0; */ |
80 | | bool isLatin1; /* = false */ |
81 | | bool isUTF8; /* = false */ |
82 | | bool atStart; |
83 | | bool embedWarn; |
84 | | bool skipNul; |
85 | | char convbuf[100]; |
86 | | /* the string buffer scanVector/scanFrame are reading into, so the |
87 | | scan_cleanup context can free it if an error unwinds mid-scan */ |
88 | | R_StringBuffer *sbuf; |
89 | | } LocalData; |
90 | | |
91 | | static SEXP insertString(char *str, LocalData *l) |
92 | 9.95M | { |
93 | 9.95M | cetype_t enc = CE_NATIVE; |
94 | 9.95M | if (l->con->UTF8out || l->isUTF8) enc = CE_UTF8; |
95 | 5.05k | else if (l->isLatin1) enc = CE_LATIN1; |
96 | 9.95M | return mkCharCE(str, enc); |
97 | 9.95M | } |
98 | | |
99 | | static R_INLINE bool Rspace(unsigned int c) |
100 | 11.2M | { |
101 | 11.2M | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return true; |
102 | | #ifdef Win32 |
103 | | /* 0xa0 is NBSP in all 8-bit Windows locales */ |
104 | | if(!mbcslocale && c == 0xa0) return true; |
105 | | #else |
106 | | /* 0xa0 is NBSP in Latin-1 */ |
107 | 10.1M | if(known_to_be_latin1 && c == 0xa0) return true; |
108 | 10.1M | #endif |
109 | 10.1M | return false; |
110 | 10.1M | } |
111 | | |
112 | | |
113 | | /* used by readline() and menu() */ |
114 | | static int ConsoleGetchar(void) |
115 | 0 | { |
116 | 0 | if (--ConsoleBufCnt < 0) { |
117 | 0 | ConsoleBuf[CONSOLE_BUFFER_SIZE] = '\0'; |
118 | 0 | if (R_ReadConsole(ConsolePrompt, ConsoleBuf, |
119 | 0 | CONSOLE_BUFFER_SIZE, 0) == 0) { |
120 | 0 | R_ClearerrConsole(); |
121 | 0 | return R_EOF; |
122 | 0 | } |
123 | 0 | ConsoleBufp = ConsoleBuf; |
124 | 0 | ConsoleBufCnt = (int) strlen((char *)ConsoleBuf); |
125 | 0 | ConsoleBufCnt--; |
126 | 0 | } |
127 | | /* at this point we need to use unsigned char or similar */ |
128 | 0 | return (int) *ConsoleBufp++; |
129 | 0 | } |
130 | | |
131 | | /* used by scan() */ |
132 | | static int ConsoleGetcharWithPushBack(Rconnection con) |
133 | 0 | { |
134 | 0 | char *curLine; |
135 | 0 | int c; |
136 | |
|
137 | 0 | if(con->nPushBack > 0) { |
138 | 0 | curLine = con->PushBack[con->nPushBack-1]; |
139 | 0 | c = curLine[con->posPushBack++]; |
140 | 0 | if(con->posPushBack >= strlen(curLine)) { |
141 | | /* last character on a line, so pop the line */ |
142 | 0 | free(curLine); |
143 | 0 | con->nPushBack--; |
144 | 0 | con->posPushBack = 0; |
145 | 0 | if(con->nPushBack == 0) free(con->PushBack); |
146 | 0 | } |
147 | 0 | return c; |
148 | 0 | } else |
149 | 0 | return ConsoleGetchar(); |
150 | 0 | } |
151 | | |
152 | | /* Like strtol, but for ints not longs and returns NA_INTEGER on overflow */ |
153 | | static int Strtoi(const char *nptr, int base) |
154 | 39.5k | { |
155 | 39.5k | long res; |
156 | 39.5k | char *endp; |
157 | | |
158 | 39.5k | errno = 0; |
159 | 39.5k | res = strtol(nptr, &endp, base); |
160 | 39.5k | if (*endp != '\0') res = NA_INTEGER; |
161 | | /* next can happen on a 64-bit platform */ |
162 | 39.5k | if (res > INT_MAX || res < INT_MIN) res = NA_INTEGER; |
163 | 39.5k | if (errno == ERANGE) res = NA_INTEGER; |
164 | 39.5k | return (int) res; |
165 | 39.5k | } |
166 | | |
167 | | static double |
168 | | Strtod (const char *nptr, char **endptr, bool NA, LocalData *d) |
169 | 69.6k | { |
170 | 69.6k | return R_strtod4(nptr, endptr, d->decchar, NA); |
171 | 69.6k | } |
172 | | |
173 | | static Rcomplex |
174 | | strtoc(const char *nptr, char **endptr, bool NA, LocalData *d) |
175 | 52.2k | { |
176 | 52.2k | Rcomplex z; |
177 | 52.2k | double x, y; |
178 | 52.2k | char *s, *endp; |
179 | | |
180 | 52.2k | x = Strtod(nptr, &endp, NA, d); |
181 | 52.2k | if (isBlankString(endp)) { |
182 | 49.7k | z.r = x; z.i = 0; |
183 | 49.7k | } else if (*endp == 'i') { |
184 | 1.76k | z.r = 0; z.i = x; |
185 | 1.76k | endp++; |
186 | 1.76k | } else { |
187 | 706 | s = endp; |
188 | 706 | y = Strtod(s, &endp, NA, d); |
189 | 706 | if (*endp == 'i') { |
190 | 391 | z.r = x; z.i = y; |
191 | 391 | endp++; |
192 | 391 | } else { |
193 | 315 | z.r = 0; z.i = 0; |
194 | 315 | endp = (char *) nptr; /* -Wall */ |
195 | 315 | } |
196 | 706 | } |
197 | 52.2k | *endptr = endp; |
198 | 52.2k | return z; |
199 | 52.2k | } |
200 | | |
201 | | static Rbyte |
202 | | strtoraw (const char *nptr, char **endptr) |
203 | 36.3k | { |
204 | 36.3k | const char *p = nptr; |
205 | 36.3k | int i, val = 0; |
206 | | |
207 | | /* should have whitespace plus exactly 2 hex digits */ |
208 | 36.3k | while(Rspace(*p)) p++; |
209 | 73.6k | for(i = 1; i <= 2; i++, p++) { |
210 | 72.5k | val *= 16; |
211 | 72.5k | if(*p >= '0' && *p <= '9') val += *p - '0'; |
212 | 37.3k | else if (*p >= 'A' && *p <= 'F') val += *p - 'A' + 10; |
213 | 36.1k | else if (*p >= 'a' && *p <= 'f') val += *p - 'a' + 10; |
214 | 35.3k | else {val = 0; break;} |
215 | 72.5k | } |
216 | 36.3k | *endptr = (char *) p; |
217 | 36.3k | return (Rbyte) val; |
218 | 36.3k | } |
219 | | |
220 | | static R_INLINE int scanchar_raw(LocalData *d) |
221 | 28.7M | { |
222 | 28.7M | int c = (d->ttyflag) ? ConsoleGetcharWithPushBack(d->con) : |
223 | 28.7M | Rconn_fgetc(d->con); |
224 | 28.7M | if(c == 0) { |
225 | 103k | if(d->skipNul) { |
226 | 0 | do { |
227 | 0 | c = (d->ttyflag) ? ConsoleGetcharWithPushBack(d->con) : |
228 | 0 | Rconn_fgetc(d->con); |
229 | 0 | } while(c == 0); |
230 | 103k | } else d->embedWarn = true; |
231 | 103k | } |
232 | 28.7M | return c; |
233 | 28.7M | } |
234 | | |
235 | | static R_INLINE void unscanchar(int c, LocalData *d) |
236 | 850k | { |
237 | 850k | d->save = c; |
238 | 850k | } |
239 | | |
240 | | /* For second bytes in a DBCS: |
241 | | should not be called when a char is saved, but be cautious |
242 | | */ |
243 | | static R_INLINE int scanchar2(LocalData *d) |
244 | 0 | { |
245 | 0 | int next; |
246 | 0 | if (d->save) { |
247 | 0 | next = d->save; |
248 | 0 | d->save = 0; |
249 | 0 | } else |
250 | 0 | next = scanchar_raw(d); |
251 | 0 | return next; |
252 | 0 | } |
253 | | |
254 | | static int scanchar(bool inQuote, LocalData *d) |
255 | 29.4M | { |
256 | 29.4M | int next; |
257 | 29.4M | if (d->save) { |
258 | 839k | next = d->save; |
259 | 839k | d->save = 0; |
260 | 839k | } else |
261 | 28.6M | next = scanchar_raw(d); |
262 | 29.4M | if(next == d->comchar && !inQuote) { |
263 | 985 | do |
264 | 42.9k | next = scanchar_raw(d); |
265 | 42.9k | while (next != '\n' && next != R_EOF); |
266 | 985 | } |
267 | 29.4M | if(next == '\\' && d->escapes) { |
268 | 41.2k | next = scanchar_raw(d); |
269 | 41.2k | if ('0' <= next && next <= '8') { |
270 | 11.3k | int octal = next - '0'; |
271 | 11.3k | if ('0' <= (next = scanchar_raw(d)) && next <= '8') { |
272 | 9.04k | octal = 8 * octal + next - '0'; |
273 | 9.04k | if ('0' <= (next = scanchar_raw(d)) && next <= '8') { |
274 | 6.24k | octal = 8 * octal + next - '0'; |
275 | 6.24k | } else unscanchar(next, d); |
276 | 9.04k | } else unscanchar(next, d); |
277 | 11.3k | next = octal; |
278 | 11.3k | } else |
279 | 29.9k | switch(next) { |
280 | 237 | case 'a': next = '\a'; break; |
281 | 324 | case 'b': next = '\b'; break; |
282 | 277 | case 'f': next = '\f'; break; |
283 | 633 | case 'n': next = '\n'; break; |
284 | 4.38k | case 'r': next = '\r'; break; |
285 | 702 | case 't': next = '\t'; break; |
286 | 393 | case 'v': next = '\v'; break; |
287 | 10.9k | case 'x': { |
288 | 10.9k | int val = 0; int i, ext; |
289 | 17.3k | for(i = 0; i < 2; i++) { |
290 | 16.5k | next = scanchar_raw(d); |
291 | 16.5k | if(next >= '0' && next <= '9') ext = next - '0'; |
292 | 16.0k | else if (next >= 'A' && next <= 'F') ext = next - 'A' + 10; |
293 | 15.1k | else if (next >= 'a' && next <= 'f') ext = next - 'a' + 10; |
294 | 10.0k | else {unscanchar(next, d); break;} |
295 | 6.44k | val = 16*val + ext; |
296 | 6.44k | } |
297 | 10.9k | next = val; |
298 | 10.9k | } |
299 | 10.9k | break; |
300 | 12.0k | default: |
301 | | /* Any other char and even EOF escapes to itself, but we |
302 | | need to preserve \" etc inside quotes. |
303 | | */ |
304 | 12.0k | if(inQuote && strchr(d->quoteset, next)) { |
305 | 856 | unscanchar(next, d); |
306 | 856 | next = '\\'; |
307 | 856 | } |
308 | 12.0k | break; |
309 | 29.9k | } |
310 | 41.2k | } |
311 | 29.4M | return next; |
312 | 29.4M | } |
313 | | |
314 | | /* utility to close connections after interrupts */ |
315 | | static void scan_cleanup(void *data) |
316 | 1.56k | { |
317 | 1.56k | LocalData *ld = data; |
318 | 1.56k | if(ld->con && !ld->ttyflag && !ld->wasopen) { |
319 | 549 | if (ld->con->isopen) ld->con->close(ld->con); |
320 | 549 | ld->con = NULL; |
321 | 549 | } |
322 | 1.56k | if(ld->quoteset && ld->quoteset[0]) { |
323 | 1.56k | free(ld->quoteset); |
324 | 1.56k | ld->quoteset = NULL; |
325 | 1.56k | } |
326 | 1.56k | if(ld->sbuf) { |
327 | 1.56k | R_FreeStringBuffer(ld->sbuf); |
328 | 1.56k | ld->sbuf = NULL; |
329 | 1.56k | } |
330 | 1.56k | } |
331 | | |
332 | | |
333 | | /*XX Can we pass this routine an R_StringBuffer? appears so. |
334 | | But do we have to worry about continuation lines and whatever |
335 | | is currently in the buffer before we call this? In other words, |
336 | | what if this appends to the existing content. Appears it writes in |
337 | | directly at position 0. |
338 | | */ |
339 | | static char * |
340 | | fillBuffer(SEXPTYPE type, int strip, int *bch, LocalData *d, |
341 | | R_StringBuffer *buffer) |
342 | 6.38M | { |
343 | | /* The basic reader function, called from scanVector() and scanFrame(). |
344 | | Reads into _buffer_ which later will be read out by extractItem(). |
345 | | |
346 | | bch is used to distinguish \r, \n and EOF from more input available. |
347 | | */ |
348 | 6.38M | char *bufp; |
349 | 6.38M | int c, quote, filled, nbuf = MAXELTSIZE, m, mm = 0; |
350 | 6.38M | bool dbcslocale = (R_MB_CUR_MAX == 2) && !d->isUTF8 && !d->isLatin1; |
351 | | |
352 | 6.38M | m = 0; |
353 | 6.38M | filled = 1; |
354 | 6.38M | if (d->sepchar == 0) { |
355 | | /* skip all space or tabs: only look at lead bytes here */ |
356 | 3.33M | strip = 0; /* documented to be ignored in this case */ |
357 | 3.38M | while ((c = scanchar(false, d)) == ' ' || c == '\t') ; |
358 | 3.33M | if (c == '\n' || c == '\r' || c == R_EOF) { |
359 | 2.26M | filled = c; |
360 | 2.26M | goto donefill; |
361 | 2.26M | } |
362 | 1.07M | if ((type == STRSXP || type == NILSXP) && strchr(d->quoteset, c)) { |
363 | 32.8k | quote = c; |
364 | 3.26M | while ((c = scanchar(true, d)) != R_EOF && c != quote) { |
365 | 3.23M | if (m >= nbuf - 3) { |
366 | 99 | nbuf *= 2; |
367 | 99 | R_AllocStringBuffer(nbuf, buffer); |
368 | 99 | } |
369 | 3.23M | if (c == '\\') { |
370 | | /* If this is an embedded quote, unquote it, but |
371 | | otherwise keep backslashes */ |
372 | 1.80k | c = scanchar(true, d); |
373 | 1.80k | if (c == R_EOF) break; |
374 | 1.80k | if(c != quote) buffer->data[m++] = '\\'; |
375 | 1.80k | } |
376 | 3.23M | buffer->data[m++] = (char) c; |
377 | 3.23M | if(dbcslocale && btowc(c) == WEOF) |
378 | 0 | buffer->data[m++] = (char) scanchar2(d); |
379 | 3.23M | } |
380 | 32.8k | if (c == R_EOF) |
381 | 236 | warning(_("EOF within quoted string")); |
382 | 32.8k | c = scanchar(false, d); |
383 | 32.8k | mm = m; |
384 | 32.8k | } |
385 | 1.04M | else { /* not a quoted char string */ |
386 | 10.7M | do { |
387 | 10.7M | if (m >= nbuf - 3) { |
388 | 308 | nbuf *= 2; |
389 | 308 | R_AllocStringBuffer(nbuf, buffer); |
390 | 308 | } |
391 | 10.7M | buffer->data[m++] = (char) c; |
392 | 10.7M | if(dbcslocale && btowc(c) == WEOF) |
393 | 0 | buffer->data[m++] = (char) scanchar2(d); |
394 | 10.7M | c = scanchar(false, d); |
395 | 10.7M | } while (!Rspace(c) && c != R_EOF); |
396 | 1.04M | } |
397 | | /* skip all space or tabs: only look at lead bytes here */ |
398 | 1.96M | while (c == ' ' || c == '\t') c = scanchar(false, d); |
399 | 1.07M | if (c == '\n' || c == '\r' || c == R_EOF) |
400 | 254k | filled = c; |
401 | 819k | else |
402 | 819k | unscanchar(c, d); |
403 | 1.07M | } |
404 | 3.04M | else { /* have separator */ |
405 | 9.74M | while ((c = scanchar(false, d)) != d->sepchar && |
406 | 7.83M | c != '\n' && c != '\r' && c != R_EOF) |
407 | 6.70M | { |
408 | | /* eat white space */ |
409 | 6.70M | if (type != STRSXP) |
410 | 29.5k | while (c == ' ' || c == '\t') |
411 | 3.84k | if ((c = scanchar(false, d)) == d->sepchar |
412 | 3.65k | || c == '\n' || c == '\r' || c == R_EOF) { |
413 | 400 | filled = c; |
414 | 400 | goto donefill; |
415 | 400 | } |
416 | | /* CSV style quoted string handling */ |
417 | 6.70M | if ((type == STRSXP || type == NILSXP) |
418 | 6.67M | && c != 0 && strchr(d->quoteset, c)) { |
419 | 18.8k | quote = c; |
420 | 28.3k | inquote: |
421 | 1.19M | while ((c = scanchar(true, d)) != R_EOF && c != quote) { |
422 | 1.16M | if (m >= nbuf - 3) { |
423 | 79 | nbuf *= 2; |
424 | 79 | R_AllocStringBuffer(nbuf, buffer); |
425 | 79 | } |
426 | 1.16M | buffer->data[m++] = (char) c; |
427 | 1.16M | if(dbcslocale && btowc(c) == WEOF) |
428 | 0 | buffer->data[m++] = (char) scanchar2(d); |
429 | 1.16M | } |
430 | 28.3k | if (c == R_EOF) |
431 | 301 | warning(_("EOF within quoted string")); |
432 | 28.3k | c = scanchar(true, d); /* only peek at lead byte |
433 | | unless ASCII */ |
434 | 28.3k | if (c == quote) { |
435 | 9.56k | if (m >= nbuf - 3) { |
436 | 2 | nbuf *= 2; |
437 | 2 | R_AllocStringBuffer(nbuf, buffer); |
438 | 2 | } |
439 | 9.56k | buffer->data[m++] = (char) quote; |
440 | 9.56k | goto inquote; /* FIXME: Ick! Clean up logic */ |
441 | 9.56k | } |
442 | 18.8k | mm = m; |
443 | 18.8k | if (c == d->sepchar || c == '\n' || c == '\r' || c == R_EOF){ |
444 | 3.37k | filled = c; |
445 | 3.37k | goto donefill; |
446 | 3.37k | } |
447 | 15.4k | else { |
448 | 15.4k | unscanchar(c, d); |
449 | 15.4k | continue; |
450 | 15.4k | } |
451 | 18.8k | } /* end of CSV-style quote handling */ |
452 | 6.68M | if (!strip || m > 0 || !Rspace(c)) { /* only lead byte */ |
453 | 6.67M | if (m >= nbuf - 3) { |
454 | 255 | nbuf *= 2; |
455 | 255 | R_AllocStringBuffer(nbuf, buffer); |
456 | 255 | } |
457 | 6.67M | buffer->data[m++] = (char) c; |
458 | 6.67M | if(dbcslocale && btowc(c) == WEOF) |
459 | 0 | buffer->data[m++] = (char) scanchar2(d); |
460 | 6.67M | } |
461 | 6.68M | } |
462 | 3.04M | filled = c; /* last lead byte in a DBCS */ |
463 | 3.04M | } |
464 | 6.38M | donefill: |
465 | | /* strip trailing white space, if desired and if item is non-null */ |
466 | 6.38M | bufp = &buffer->data[m]; |
467 | 6.38M | if (strip && m > mm) { |
468 | 228k | do {c = (int)*--bufp;} while(m-- > mm && Rspace(c)); |
469 | 222k | bufp++; |
470 | 222k | } |
471 | 6.38M | *bufp = '\0'; |
472 | | /* Remove UTF-8 BOM */ |
473 | 6.38M | if(d->atStart && utf8locale && |
474 | 0 | !strncmp(buffer->data, "\xef\xbb\xbf", 3)) |
475 | 0 | memmove(buffer->data, buffer->data+3, strlen(buffer->data+3) + 1); |
476 | 6.38M | d->atStart = false; |
477 | 6.38M | *bch = filled; |
478 | 6.38M | return buffer->data; |
479 | 6.38M | } |
480 | | |
481 | | /* If mode = 0 use for numeric fields where "" is NA |
482 | | If mode = 1 use for character fields where "" is verbatim unless |
483 | | na.strings includes "" */ |
484 | | static R_INLINE int isNAstring(const char *buf, int mode, LocalData *d) |
485 | 10.6M | { |
486 | 10.6M | int i; |
487 | | |
488 | 10.6M | if(!mode && strlen(buf) == 0) return 1; |
489 | 19.0M | for (i = 0; i < length(d->NAstrings); i++) |
490 | 8.89M | if (!strcmp(CHAR(STRING_ELT(d->NAstrings, i)), buf)) return 1; |
491 | 10.1M | return 0; |
492 | 10.1M | } |
493 | | |
494 | | NORET static R_INLINE void expected(char *what, char *got, LocalData *d) |
495 | 994 | { |
496 | 994 | int c; |
497 | 994 | if (d->ttyflag) { /* This is safe in a MBCS */ |
498 | 0 | while ((c = scanchar(false, d)) != R_EOF && c != '\n') |
499 | 0 | ; |
500 | 0 | } |
501 | 994 | error(_("scan() expected '%s', got '%s'"), what, got); |
502 | 994 | } |
503 | | |
504 | | static void extractItem(char *buffer, SEXP ans, R_xlen_t i, LocalData *d) |
505 | 10.6M | { |
506 | 10.6M | char *endp; |
507 | 10.6M | switch(TYPEOF(ans)) { |
508 | 0 | case NILSXP: |
509 | 0 | break; |
510 | 61.8k | case LGLSXP: |
511 | 61.8k | if (isNAstring(buffer, 0, d)) |
512 | 631 | LOGICAL(ans)[i] = NA_INTEGER; |
513 | 61.2k | else { |
514 | 61.2k | int tr = StringTrue(buffer), fa = StringFalse(buffer); |
515 | 61.2k | if(tr || fa) LOGICAL(ans)[i] = tr; |
516 | 64 | else expected("a logical", buffer, d); |
517 | 61.2k | } |
518 | 61.8k | break; |
519 | 39.7k | case INTSXP: |
520 | 39.7k | if (isNAstring(buffer, 0, d)) |
521 | 200 | INTEGER(ans)[i] = NA_INTEGER; |
522 | 39.5k | else { |
523 | 39.5k | INTEGER(ans)[i] = Strtoi(buffer, 10); |
524 | 39.5k | if (INTEGER(ans)[i] == NA_INTEGER) |
525 | 107 | expected("an integer", buffer, d); |
526 | 39.5k | } |
527 | 39.7k | break; |
528 | 512k | case REALSXP: |
529 | 512k | if (isNAstring(buffer, 0, d)) |
530 | 495k | REAL(ans)[i] = NA_REAL; |
531 | 16.6k | else { |
532 | 16.6k | REAL(ans)[i] = Strtod(buffer, &endp, false, d); |
533 | 16.6k | if (!isBlankString(endp)) |
534 | 398 | expected("a real", buffer, d); |
535 | 16.6k | } |
536 | 512k | break; |
537 | 52.4k | case CPLXSXP: |
538 | 52.4k | if (isNAstring(buffer, 0, d)) |
539 | 195 | COMPLEX(ans)[i].r = COMPLEX(ans)[i].i = NA_REAL; |
540 | 52.2k | else { |
541 | 52.2k | COMPLEX(ans)[i] = strtoc(buffer, &endp, false, d); |
542 | 52.2k | if (!isBlankString(endp)) |
543 | 334 | expected("a complex", buffer, d); |
544 | 52.2k | } |
545 | 52.4k | break; |
546 | 9.96M | case STRSXP: |
547 | 9.96M | if (isNAstring(buffer, 1, d)) |
548 | 4.82k | SET_STRING_ELT(ans, i, NA_STRING); |
549 | 9.95M | else |
550 | 9.95M | SET_STRING_ELT(ans, i, insertString(buffer, d)); |
551 | 9.96M | break; |
552 | 36.5k | case RAWSXP: |
553 | 36.5k | if (isNAstring(buffer, 0, d)) |
554 | 194 | RAW(ans)[i] = 0; |
555 | 36.3k | else { |
556 | 36.3k | RAW(ans)[i] = strtoraw(buffer, &endp); |
557 | 36.3k | if (!isBlankString(endp)) |
558 | 91 | expected("a raw", buffer, d); |
559 | 36.3k | } |
560 | 36.5k | break; |
561 | 0 | default: |
562 | 0 | UNIMPLEMENTED_TYPE("extractItem", ans); |
563 | 10.6M | } |
564 | 10.6M | } |
565 | | |
566 | | static SEXP scanVector(SEXPTYPE type, R_xlen_t maxitems, R_xlen_t maxlines, |
567 | | int flush, SEXP stripwhite, int blskip, LocalData *d) |
568 | 14.4k | { |
569 | 14.4k | SEXP ans, bns; |
570 | 14.4k | int c, strip, bch, ic; |
571 | 14.4k | R_xlen_t i, blocksize, linesread, n, nprev; |
572 | 14.4k | char *buffer; |
573 | 14.4k | R_StringBuffer strBuf = {NULL, 0, MAXELTSIZE}; |
574 | | |
575 | 14.4k | if (maxitems > 0) blocksize = maxitems; |
576 | 13.4k | else blocksize = SCAN_BLOCKSIZE; |
577 | | |
578 | 14.4k | R_AllocStringBuffer(0, &strBuf); |
579 | 14.4k | d->sbuf = &strBuf; |
580 | 14.4k | PROTECT(ans = allocVector(type, blocksize)); |
581 | | |
582 | 14.4k | nprev = 0; n = 0; linesread = 0; bch = 1; |
583 | | |
584 | 14.4k | if (d->ttyflag) |
585 | 0 | snprintf(ConsolePrompt, CONSOLE_PROMPT_SIZE, "1: "); |
586 | | |
587 | 14.4k | strip = asLogical(stripwhite); |
588 | | |
589 | 14.4k | ic = 9999; |
590 | 4.78M | for (;;) { |
591 | 4.78M | if(!ic) { |
592 | 82 | R_CheckUserInterrupt(); |
593 | 82 | ic = 9999; |
594 | 82 | } |
595 | 4.78M | if (bch == R_EOF) { |
596 | 497 | if (d->ttyflag) R_ClearerrConsole(); |
597 | 497 | break; |
598 | 497 | } |
599 | 4.78M | else if (bch == '\n') { |
600 | 2.62M | linesread++; |
601 | 2.62M | if (linesread == maxlines) |
602 | 10.8k | break; |
603 | 2.61M | if (d->ttyflag) |
604 | 0 | snprintf(ConsolePrompt, CONSOLE_PROMPT_SIZE, |
605 | 0 | "%lld: ", (long long) (n + 1)); |
606 | 2.61M | nprev = n; |
607 | 2.61M | } |
608 | 4.76M | if (n == blocksize) { |
609 | | /* enlarge the vector*/ |
610 | 948 | bns = ans; |
611 | 948 | if(blocksize > R_XLEN_T_MAX/2) error(_("too many items")); |
612 | 948 | blocksize = 2 * blocksize; |
613 | 948 | ans = allocVector(type, blocksize); |
614 | 948 | UNPROTECT(1); |
615 | 948 | PROTECT(ans); |
616 | 948 | copyVector(ans, bns); |
617 | 948 | } |
618 | 4.76M | buffer = fillBuffer(type, strip, &bch, d, &strBuf); |
619 | 4.76M | if (nprev == n && strlen(buffer)==0 && |
620 | 2.37M | ((blskip && bch =='\n') || bch == R_EOF)) { |
621 | 2.27M | if (d->ttyflag || bch == R_EOF) |
622 | 1.57k | break; |
623 | 2.27M | } |
624 | 2.48M | else { |
625 | 2.48M | extractItem(buffer, ans, n, d); |
626 | 2.48M | ic--; |
627 | 2.48M | if (++n == maxitems) { |
628 | 15 | if (d->ttyflag && bch != '\n') { /* MBCS-safe */ |
629 | 0 | while ((c = scanchar(false, d)) != '\n') |
630 | 0 | ; |
631 | 0 | } |
632 | 15 | break; |
633 | 15 | } |
634 | 2.48M | } |
635 | 4.76M | if (flush && (bch != '\n') && (bch != R_EOF)) { /* MBCS-safe */ |
636 | 0 | while ((c = scanchar(false, d)) != '\n' && (c != R_EOF)); |
637 | 0 | bch = c; |
638 | 0 | } |
639 | 4.76M | } |
640 | 14.4k | if (!d->quiet) |
641 | 0 | REprintf("Read %lld item%s\n", (long long) n, (n == 1) ? "" : "s"); |
642 | 14.4k | if (d->ttyflag) ConsolePrompt[0] = '\0'; |
643 | | |
644 | 14.4k | if (n == 0) { |
645 | 563 | UNPROTECT(1); |
646 | 563 | R_FreeStringBuffer(&strBuf); |
647 | 563 | d->sbuf = NULL; |
648 | 563 | return allocVector(type,0); |
649 | 563 | } |
650 | 13.8k | if (n == maxitems) { |
651 | 15 | UNPROTECT(1); |
652 | 15 | R_FreeStringBuffer(&strBuf); |
653 | 15 | d->sbuf = NULL; |
654 | 15 | return ans; |
655 | 15 | } |
656 | | |
657 | 13.8k | bns = allocVector(type, n); |
658 | 13.8k | switch (type) { |
659 | 62 | case LGLSXP: |
660 | 206 | case INTSXP: |
661 | 76.1k | for (i = 0; i < n; i++) |
662 | 75.9k | INTEGER(bns)[i] = INTEGER(ans)[i]; |
663 | 206 | break; |
664 | 253 | case REALSXP: |
665 | 12.6k | for (i = 0; i < n; i++) |
666 | 12.4k | REAL(bns)[i] = REAL(ans)[i]; |
667 | 253 | break; |
668 | 127 | case CPLXSXP: |
669 | 22.8k | for (i = 0; i < n; i++) |
670 | 22.7k | COMPLEX(bns)[i] = COMPLEX(ans)[i]; |
671 | 127 | break; |
672 | 11.7k | case STRSXP: |
673 | 2.27M | for (i = 0; i < n; i++) |
674 | 2.26M | SET_STRING_ELT(bns, i, STRING_ELT(ans, i)); |
675 | 11.7k | break; |
676 | 61 | case RAWSXP: |
677 | 23.5k | for (i = 0; i < n; i++) |
678 | 23.4k | RAW(bns)[i] = RAW(ans)[i]; |
679 | 61 | break; |
680 | 0 | default: |
681 | 0 | UNIMPLEMENTED_TYPEt("scanVector", type); |
682 | 13.8k | } |
683 | 12.3k | UNPROTECT(1); |
684 | 12.3k | R_FreeStringBuffer(&strBuf); |
685 | 12.3k | d->sbuf = NULL; |
686 | 12.3k | return bns; |
687 | 13.8k | } |
688 | | |
689 | | |
690 | | static SEXP scanFrame(SEXP what, R_xlen_t maxitems, R_xlen_t maxlines, |
691 | | int flush, int fill, SEXP stripwhite, int blskip, |
692 | | int multiline, LocalData *d) |
693 | 5.10k | { |
694 | 5.10k | SEXP ans, new, old, w; |
695 | 5.10k | char *buffer = NULL; |
696 | 5.10k | int c, strip, bch, ic; |
697 | 5.10k | R_xlen_t blksize, i, ii, j, n, nc, linesread, colsread; |
698 | 5.10k | R_xlen_t badline; |
699 | 5.10k | R_StringBuffer buf = {NULL, 0, MAXELTSIZE}; |
700 | | |
701 | 5.10k | nc = xlength(what); |
702 | 5.10k | if (!nc) { |
703 | 0 | error(_("empty 'what' specified")); |
704 | 0 | } |
705 | | |
706 | 5.10k | if (maxitems > 0) blksize = maxitems; |
707 | 5.04k | else if (maxlines > 0) blksize = maxlines; |
708 | 5.04k | else blksize = SCAN_BLOCKSIZE; |
709 | | |
710 | 5.10k | R_AllocStringBuffer(0, &buf); |
711 | 5.10k | d->sbuf = &buf; |
712 | 5.10k | PROTECT(ans = allocVector(VECSXP, nc)); |
713 | 1.30M | for (i = 0; i < nc; i++) { |
714 | 1.30M | w = VECTOR_ELT(what, i); |
715 | 1.30M | if (!isNull(w)) { |
716 | 1.30M | if (!isVector(w)) { |
717 | 0 | error(_("invalid '%s' argument"), "what"); |
718 | 0 | } |
719 | 1.30M | SET_VECTOR_ELT(ans, i, allocVector(TYPEOF(w), blksize)); |
720 | 1.30M | } |
721 | 1.30M | } |
722 | 5.10k | setAttrib(ans, R_NamesSymbol, getAttrib(what, R_NamesSymbol)); |
723 | | |
724 | 5.10k | n = 0; linesread = 0; colsread = 0; ii = 0; |
725 | 5.10k | badline = 0; |
726 | 5.10k | bch = 1; |
727 | 5.10k | c = 0; /* -Wall */ |
728 | | |
729 | 5.10k | if (d->ttyflag) |
730 | 0 | snprintf(ConsolePrompt, CONSOLE_PROMPT_SIZE, "1: "); |
731 | | |
732 | | // we checked its type in do_scan |
733 | 5.10k | int *lstrip = LOGICAL(stripwhite); |
734 | 5.10k | bool vec_strip = (xlength(stripwhite) == xlength(what)); |
735 | 5.10k | strip = lstrip[0]; |
736 | | |
737 | 5.10k | ic = 999; |
738 | 1.61M | for (;;) { |
739 | 1.61M | if(!ic) { |
740 | 905 | R_CheckUserInterrupt(); |
741 | 905 | ic = 999; |
742 | 905 | } |
743 | | |
744 | 1.61M | if (bch == R_EOF) { |
745 | 298 | if (d->ttyflag) R_ClearerrConsole(); |
746 | 298 | goto done; |
747 | 298 | } |
748 | 1.61M | else if (bch == '\n') { |
749 | 1.01M | ic--; |
750 | 1.01M | linesread++; |
751 | 1.01M | if (colsread != 0) { |
752 | 575k | if (fill) { |
753 | 574k | buffer[0] = '\0'; |
754 | 7.29M | for (ii = colsread; ii < nc; ii++) { |
755 | 6.71M | extractItem(buffer, VECTOR_ELT(ans, ii), n, d); |
756 | 6.71M | } |
757 | 574k | n++; |
758 | 574k | ii = 0; |
759 | 574k | colsread = 0; |
760 | 574k | } else if (!badline && !multiline) |
761 | 24 | badline = linesread; |
762 | 575k | if(badline && !multiline) |
763 | 24 | error(_("line %lld did not have %lld elements"), |
764 | 24 | (long long) badline, (long long) nc); |
765 | 575k | } |
766 | 1.01M | if (maxitems > 0 && n >= maxitems) |
767 | 11 | goto done; |
768 | 1.01M | if (maxlines > 0 && linesread == maxlines) |
769 | 0 | goto done; |
770 | 1.01M | if (d->ttyflag) |
771 | 0 | snprintf(ConsolePrompt, CONSOLE_PROMPT_SIZE, |
772 | 0 | "%lld: ", (long long) (n + 1)); |
773 | 1.01M | } |
774 | 1.61M | if (n == blksize && colsread == 0) { |
775 | 308 | if(blksize > R_XLEN_T_MAX/2) error(_("too many items")); |
776 | 308 | blksize = 2 * blksize; |
777 | 1.49k | for (i = 0; i < nc; i++) { |
778 | 1.18k | old = VECTOR_ELT(ans, i); |
779 | 1.18k | if(!isNull(old)) { |
780 | 1.18k | new = allocVector(TYPEOF(old), blksize); |
781 | 1.18k | copyVector(new, old); |
782 | 1.18k | SET_VECTOR_ELT(ans, i, new); |
783 | 1.18k | } |
784 | 1.18k | } |
785 | 308 | } |
786 | | |
787 | 1.61M | if (vec_strip) strip = lstrip[colsread]; |
788 | 1.61M | buffer = fillBuffer(TYPEOF(VECTOR_ELT(ans, ii)), strip, &bch, d, &buf); |
789 | 1.61M | if (colsread == 0 && |
790 | 1.05M | strlen(buffer) == 0 && |
791 | 298k | ((blskip && bch =='\n') || bch == R_EOF)) { |
792 | 161k | if (d->ttyflag || bch == R_EOF) |
793 | 4.73k | break; |
794 | 161k | } |
795 | 1.45M | else { |
796 | 1.45M | extractItem(buffer, VECTOR_ELT(ans, ii), n, d); |
797 | 1.45M | ii++; |
798 | 1.45M | colsread++; |
799 | | /* increment n and reset i after filling a row */ |
800 | 1.45M | if (colsread == nc) { |
801 | 316k | n++; |
802 | 316k | ii = 0; |
803 | 316k | colsread = 0; |
804 | 316k | if (flush && (bch != '\n') && (bch != R_EOF)) { /* MBCS-safe */ |
805 | 8.66k | while ((c = scanchar(false, d)) != '\n' && c != R_EOF); |
806 | 225 | bch = c; |
807 | 225 | } |
808 | 316k | } |
809 | 1.45M | } |
810 | 1.61M | } |
811 | | |
812 | 5.04k | done: |
813 | 5.04k | if (colsread != 0) { |
814 | 214 | if (!fill) |
815 | 86 | warning(_("number of items read is not a multiple of the number of columns")); |
816 | 214 | buffer[0] = '\0'; /* this is an NA */ |
817 | 8.16k | for (ii = colsread; ii < nc; ii++) { |
818 | 7.95k | extractItem(buffer, VECTOR_ELT(ans, ii), n, d); |
819 | 7.95k | } |
820 | 214 | n++; |
821 | 214 | } |
822 | 5.04k | if (!d->quiet) |
823 | 0 | REprintf("Read %lld record%s\n", (long long) n, (n == 1) ? "" : "s"); |
824 | 5.04k | if (d->ttyflag) ConsolePrompt[0] = '\0'; |
825 | | |
826 | 1.26M | for (i = 0; i < nc; i++) { |
827 | 1.25M | old = VECTOR_ELT(ans, i); |
828 | 1.25M | new = allocVector(TYPEOF(old), n); |
829 | 1.25M | switch (TYPEOF(old)) { |
830 | 184 | case LGLSXP: |
831 | 184 | case INTSXP: |
832 | 1.47k | for (j = 0; j < n; j++) |
833 | 1.28k | INTEGER(new)[j] = INTEGER(old)[j]; |
834 | 184 | break; |
835 | 329 | case REALSXP: |
836 | 491k | for (j = 0; j < n; j++) |
837 | 491k | REAL(new)[j] = REAL(old)[j]; |
838 | 329 | break; |
839 | 0 | case CPLXSXP: |
840 | 0 | for (j = 0; j < n; j++) |
841 | 0 | COMPLEX(new)[j] = COMPLEX(old)[j]; |
842 | 0 | break; |
843 | 1.25M | case STRSXP: |
844 | 8.90M | for (j = 0; j < n; j++) |
845 | 7.65M | SET_STRING_ELT(new, j, STRING_ELT(old, j)); |
846 | 1.25M | break; |
847 | 0 | case RAWSXP: |
848 | 0 | for (j = 0; j < n; j++) |
849 | 0 | RAW(new)[j] = RAW(old)[j]; |
850 | 0 | break; |
851 | 0 | case NILSXP: |
852 | 0 | break; |
853 | 0 | default: |
854 | 0 | UNIMPLEMENTED_TYPE("scanFrame", old); |
855 | 1.25M | } |
856 | 1.25M | SET_VECTOR_ELT(ans, i, new); |
857 | 1.25M | } |
858 | 5.04k | UNPROTECT(1); |
859 | 5.04k | R_FreeStringBuffer(&buf); |
860 | 5.04k | d->sbuf = NULL; |
861 | 5.04k | return ans; |
862 | 5.04k | } |
863 | | |
864 | | attribute_hidden SEXP do_scan(SEXP call, SEXP op, SEXP args, SEXP rho) |
865 | 19.5k | { |
866 | 19.5k | SEXP ans, file, sep, what, stripwhite, dec, quotes, comstr; |
867 | 19.5k | int c, flush, fill, blskip, multiline, escapes, skipNul; |
868 | 19.5k | R_xlen_t nmax, nlines, nskip; |
869 | 19.5k | const char *p, *encoding; |
870 | 19.5k | RCNTXT cntxt; |
871 | 19.5k | LocalData data = {NULL, 0, 0, '.', NULL, NO_COMCHAR, 0, NULL, false, |
872 | 19.5k | false, 0, false, false, false, false, false, {false}}; |
873 | 19.5k | data.NAstrings = R_NilValue; |
874 | | |
875 | 19.5k | checkArity(op, args); |
876 | | |
877 | 19.5k | file = CAR(args); args = CDR(args); |
878 | 19.5k | what = CAR(args); args = CDR(args); |
879 | 19.5k | nmax = asXLength(CAR(args)); args = CDR(args); |
880 | 19.5k | sep = CAR(args); args = CDR(args); |
881 | 19.5k | dec = CAR(args); args = CDR(args); |
882 | 19.5k | quotes = CAR(args); args = CDR(args); |
883 | 19.5k | nskip = asXLength(CAR(args)); args = CDR(args); |
884 | 19.5k | nlines = asXLength(CAR(args)); args = CDR(args); |
885 | 19.5k | data.NAstrings = CAR(args); args = CDR(args); |
886 | 19.5k | flush = asLogical(CAR(args)); args = CDR(args); |
887 | 19.5k | fill = asLogical(CAR(args)); args = CDR(args); |
888 | 19.5k | stripwhite = CAR(args); args = CDR(args); |
889 | 19.5k | data.quiet = asLogical(CAR(args)); args = CDR(args); |
890 | 19.5k | blskip = asLogical(CAR(args)); args = CDR(args); |
891 | 19.5k | multiline = asLogical(CAR(args)); args = CDR(args); |
892 | 19.5k | comstr = CAR(args); args = CDR(args); |
893 | 19.5k | escapes = asLogical(CAR(args));args = CDR(args); |
894 | 19.5k | if(!isString(CAR(args)) || LENGTH(CAR(args)) != 1) |
895 | 0 | error(_("invalid '%s' argument"), "encoding"); |
896 | 19.5k | encoding = CHAR(STRING_ELT(CAR(args), 0)); args = CDR(args); /* ASCII */ |
897 | 19.5k | if(streql(encoding, "latin1")) data.isLatin1 = true; |
898 | 19.5k | if(streql(encoding, "UTF-8")) data.isUTF8 = true; |
899 | 19.5k | skipNul = asLogical(CAR(args)); |
900 | | |
901 | 19.5k | if (data.quiet == NA_LOGICAL) data.quiet = 0; |
902 | 19.5k | if (blskip == NA_LOGICAL) blskip = 1; |
903 | 19.5k | if (multiline == NA_LOGICAL) multiline = 1; |
904 | 19.5k | if (nskip < 0) nskip = 0; |
905 | 19.5k | if (nlines < 0) nlines = 0; |
906 | 19.5k | if (nmax < 0) nmax = 0; |
907 | | |
908 | 19.5k | if (TYPEOF(stripwhite) != LGLSXP) |
909 | 0 | error(_("invalid '%s' argument"), "strip.white"); |
910 | 19.5k | if (xlength(stripwhite) != 1 && xlength(stripwhite) != xlength(what)) |
911 | 0 | error(_("invalid 'strip.white' length")); |
912 | 19.5k | if (TYPEOF(data.NAstrings) != STRSXP) |
913 | 0 | error(_("invalid '%s' argument"), "na.strings"); |
914 | 19.5k | if (TYPEOF(comstr) != STRSXP || length(comstr) != 1) |
915 | 0 | error(_("invalid '%s' argument"), "comment.char"); |
916 | | |
917 | 19.5k | if (isString(sep) || isNull(sep)) { |
918 | 19.5k | if (length(sep) == 0) data.sepchar = 0; |
919 | 19.5k | else { |
920 | 19.5k | const char *sc = translateChar(STRING_ELT(sep, 0)); |
921 | 19.5k | if(strlen(sc) > 1) |
922 | 0 | error(_("invalid 'sep' value: must be one byte")); |
923 | 19.5k | data.sepchar = (unsigned char) sc[0]; |
924 | 19.5k | } |
925 | | /* gets compared to chars: bug prior to 1.7.0 */ |
926 | 19.5k | } else error(_("invalid '%s' argument"), "sep"); |
927 | | |
928 | 19.5k | if (isString(dec) || isNull(dec)) { |
929 | 19.5k | if (length(dec) == 0) |
930 | 0 | data.decchar = '.'; |
931 | 19.5k | else { |
932 | 19.5k | const char *dc = translateChar(STRING_ELT(dec, 0)); |
933 | 19.5k | if(strlen(dc) != 1) |
934 | 0 | error(_("invalid decimal separator: must be one byte")); |
935 | 19.5k | data.decchar = dc[0]; |
936 | 19.5k | } |
937 | 19.5k | } |
938 | 0 | else |
939 | 0 | error(_("invalid decimal separator")); |
940 | | |
941 | | /* set up a context which will close the connection if there is |
942 | | an error or user interrupt */ |
943 | 19.5k | begincontext(&cntxt, CTXT_CCODE, R_GlobalContext->call, R_BaseEnv, |
944 | 19.5k | R_BaseEnv, R_NilValue, R_NilValue); |
945 | 19.5k | cntxt.cend = &scan_cleanup; |
946 | 19.5k | cntxt.cenddata = &data; |
947 | | |
948 | 19.5k | if (isString(quotes)) { |
949 | 19.5k | const char *sc = translateChar(STRING_ELT(quotes, 0)); |
950 | 19.5k | if (strlen(sc)) data.quoteset = Rstrdup(sc); |
951 | 0 | else data.quoteset = ""; |
952 | 19.5k | } else if (isNull(quotes)) |
953 | 0 | data.quoteset = ""; |
954 | 0 | else |
955 | 0 | error(_("invalid quote symbol set")); |
956 | | |
957 | 19.5k | p = translateChar(STRING_ELT(comstr, 0)); |
958 | 19.5k | data.comchar = NO_COMCHAR; /* here for -Wall */ |
959 | 19.5k | if (strlen(p) > 1) |
960 | 0 | error(_("invalid '%s' argument"), "comment.char"); |
961 | 19.5k | else if (strlen(p) == 1) data.comchar = (unsigned char)*p; |
962 | 19.5k | if(escapes == NA_LOGICAL) |
963 | 0 | error(_("invalid '%s' argument"), "allowEscapes"); |
964 | 19.5k | data.escapes = escapes != 0; |
965 | 19.5k | if(skipNul == NA_LOGICAL) |
966 | 0 | error(_("invalid '%s' argument"), "skipNul"); |
967 | 19.5k | data.skipNul = skipNul != 0; |
968 | | |
969 | 19.5k | int ii = asInteger(file); |
970 | 19.5k | data.con = getConnection(ii); |
971 | 19.5k | if(ii == 0) { |
972 | 0 | data.atStart = false; |
973 | 0 | data.ttyflag = 1; |
974 | 19.5k | } else { |
975 | 19.5k | data.atStart = (nskip == 0); |
976 | 19.5k | data.ttyflag = 0; |
977 | 19.5k | data.wasopen = data.con->isopen; |
978 | 19.5k | if(!data.wasopen) { |
979 | 1.00k | data.con->UTF8out = true; /* a request */ |
980 | 1.00k | strcpy(data.con->mode, "r"); |
981 | 1.00k | if(!data.con->open(data.con)) |
982 | 0 | error(_("cannot open the connection")); |
983 | 1.00k | if(!data.con->canread) { |
984 | 0 | data.con->close(data.con); |
985 | 0 | error(_("cannot read from this connection")); |
986 | 0 | } |
987 | 18.5k | } else { |
988 | 18.5k | if(!data.con->canread) |
989 | 0 | error(_("cannot read from this connection")); |
990 | 18.5k | } |
991 | 19.5k | for(R_xlen_t i = 0, j = 10000; i < nskip; i++) { /* MBCS-safe */ |
992 | 184k | for(;;) { |
993 | 184k | c = scanchar(false, &data); |
994 | 184k | if (!j--) { |
995 | 17 | R_CheckUserInterrupt(); |
996 | 17 | j = 10000; |
997 | 17 | } |
998 | 184k | if (c == '\n' || c == R_EOF) |
999 | 36 | break; |
1000 | 184k | } |
1001 | 36 | } |
1002 | 19.5k | } |
1003 | | |
1004 | 19.5k | ans = R_NilValue; /* -Wall */ |
1005 | 19.5k | data.save = 0; |
1006 | | |
1007 | 19.5k | switch (TYPEOF(what)) { |
1008 | 127 | case LGLSXP: |
1009 | 378 | case INTSXP: |
1010 | 1.00k | case REALSXP: |
1011 | 1.46k | case CPLXSXP: |
1012 | 14.2k | case STRSXP: |
1013 | 14.4k | case RAWSXP: |
1014 | 14.4k | ans = scanVector(TYPEOF(what), nmax, nlines, flush, stripwhite, |
1015 | 14.4k | blskip, &data); |
1016 | 14.4k | break; |
1017 | | |
1018 | 5.10k | case VECSXP: |
1019 | 5.10k | ans = scanFrame(what, nmax, nlines, flush, fill, stripwhite, |
1020 | 5.10k | blskip, multiline, &data); |
1021 | 5.10k | break; |
1022 | 0 | default: |
1023 | 0 | error(_("invalid '%s' argument"), "what"); |
1024 | 19.5k | } |
1025 | 17.9k | PROTECT(ans); |
1026 | 17.9k | endcontext(&cntxt); |
1027 | | |
1028 | | /* we might have a character that was unscanchar-ed. |
1029 | | So pushback if possible */ |
1030 | 17.9k | if (data.save && !data.ttyflag && data.wasopen) { |
1031 | 134 | char line[2] = " "; |
1032 | 134 | line[0] = (char) data.save; |
1033 | 134 | con_pushback(data.con, false, line); |
1034 | 134 | } |
1035 | 17.9k | if (!data.ttyflag && !data.wasopen) |
1036 | 452 | data.con->close(data.con); |
1037 | 17.9k | if (data.quoteset[0]) free(data.quoteset); |
1038 | 17.9k | if (!skipNul && data.embedWarn) |
1039 | 106 | warning(_("embedded nul(s) found in input")); |
1040 | | |
1041 | 17.9k | UNPROTECT(1); /* ans */ |
1042 | 17.9k | return ans; |
1043 | 19.5k | } |
1044 | | |
1045 | | attribute_hidden SEXP do_readln(SEXP call, SEXP op, SEXP args, SEXP rho) |
1046 | 0 | { |
1047 | 0 | int c; |
1048 | 0 | char buffer[MAXELTSIZE], *bufp = buffer; |
1049 | 0 | SEXP ans, prompt; |
1050 | |
|
1051 | 0 | checkArity(op,args); |
1052 | |
|
1053 | 0 | prompt = CAR(args); |
1054 | 0 | if (prompt == R_NilValue) { |
1055 | 0 | ConsolePrompt[0] = '\0'; /* precaution */ |
1056 | 0 | PROTECT(prompt); |
1057 | 0 | } else { |
1058 | 0 | PROTECT(prompt = coerceVector(prompt, STRSXP)); |
1059 | 0 | if(length(prompt) > 0) { |
1060 | 0 | strncpy(ConsolePrompt, translateChar(STRING_ELT(prompt, 0)), |
1061 | 0 | CONSOLE_PROMPT_SIZE - 1); |
1062 | 0 | ConsolePrompt[CONSOLE_PROMPT_SIZE - 1] = '\0'; |
1063 | 0 | } |
1064 | 0 | } |
1065 | |
|
1066 | 0 | if(R_Interactive) { |
1067 | | /* skip space or tab */ |
1068 | 0 | while ((c = ConsoleGetchar()) == ' ' || c == '\t') ; |
1069 | 0 | if (c != '\n' && c != R_EOF) { |
1070 | 0 | *bufp++ = (char) c; |
1071 | 0 | while ((c = ConsoleGetchar())!= '\n' && c != R_EOF) { |
1072 | 0 | if (bufp >= &buffer[MAXELTSIZE - 2]) continue; |
1073 | 0 | *bufp++ = (char) c; |
1074 | 0 | } |
1075 | 0 | } |
1076 | | /* now strip white space off the end as well */ |
1077 | 0 | while (--bufp >= buffer && (*bufp == ' ' || *bufp == '\t')) |
1078 | 0 | ; |
1079 | 0 | *++bufp = '\0'; |
1080 | 0 | ConsolePrompt[0] = '\0'; |
1081 | |
|
1082 | 0 | ans = mkString(buffer); |
1083 | 0 | } else { |
1084 | | /* simulate CR as response */ |
1085 | 0 | Rprintf("%s\n", ConsolePrompt); |
1086 | 0 | ans = mkString(""); |
1087 | 0 | } |
1088 | 0 | UNPROTECT(1); |
1089 | 0 | return ans; |
1090 | 0 | } |