[Rd] Command line length limits in R

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 25 21:06:21 CEST 2006


Yes, that's the essence of the problem: long lines get truncated by the 
R-readline interface and so the next line is appended to part of the 
previous line (without the closing " in this case).

It works from redirected input because the first chunk is syntactically 
incomplete (and --no-readline has no affect unless input is from a tty).

BTW, 'file redirection' seems incorrect: no file is told to 
go elsewhere!  OTOH, input is redirected to come from the file.

On Fri, 25 Aug 2006, Jeffrey Horner wrote:

> Prof Brian Ripley wrote:
> > I've been trying to track down some of the issues with command line length
> > limits, and those writing GUIs/front-ends need to pay some attention to the
> > issue.
> > 
> > src/unix/system.txt says
> > 
> >  *    int   R_ReadConsole(char *prompt, char *buf, int buflen, int hist)
> >  *
> >  *  This function prints the given prompt at the console and then
> >  *  does a gets(3)-like operation, transferring up to "buflen" characters
> >  *  into the buffer "buf".  The last two characters are set to "\n\0"
> >  *  to preserve sanity.
> > 
> > but that isn't actually true for some of the standard interfaces and seems
> > undesirable.  (Also, for 'characters' read 'bytes'.)
> > 
> > What happens is that all calls to R_ReadConsole have buflen=1024.  (These
> > can be an input line of R code, or from scan() or from a stdin()
> > connection.) If this is command input, the result is parsed, and if
> > incomplete and not an error, more input is requested until it is complete.
> > 
> > Suppose the user entered a very long line. Should the first 1024 bytes be
> > syntactically complete, this will not do what he expected and it will be as
> > if a LF had been inserted after 1024 bytes.  But that is unlikely, and he
> > may well get away with it, unless R_ReadConsole did actually does as
> > documented and inserts "\n\0" (the Rgui and readline consoles do, but
> > reading from a file in Linux or Windows does not).
> > 
> > It seems the correct advice is that R_ReadConsole should only send a "\n"
> > when there is no more input available, e.g. on EOF.  I am changing the
> > calling code to have a 1025 char buffer with last '\0' to ensure
> > null-termination.
> > 
> > Some consoles try to ensure that the user cannot enter more than 1024 bytes.
> > That's a bit awkward in a MBCS, and also when input is being pasted in
> > (possibly in the middle of a line).  Generally this does not work too well:
> > e.g. the readline console truncates at 1022 chars and appends a \n.
> > 
> > I have no idea how an R user was expected to know there was a line limit.
> > I've added some documentation to R-intro (but I will need to correct it as I
> > didn't know the readline console used 1022).  The limit applies to
> > redirected input from a file, but not to source().
> 
> Just thought I'd throw a test case at you. If I run the following code:
> 
> longline <- function(len){
>     n <- floor(len/10)
>     m = len %% 10
>     if (m>0){
>         x <- paste(rep(seq(0,9),n),collapse='')
>         y <- paste(seq(0,m-1),collapse='')
>         z <- paste(x,y,sep='')
>     } else {
>         z <- paste(rep(seq(0,9),n),collapse='')
>     }
>     z
> }
> cat("x<-\"", longline(1020), "\";",sep="");
> cat("cat(x,\"\\n\"",")\n",sep="");
> cat("cat(nchar(x),\"\\n\"",")\n",sep="");
> 
> and save the output in test_longout.R, it behaves correctly (on linux R-2.3.1
> built with readline support) with file redirection (is this what you mean by
> redirected input from a file?):
> 
> R --vanilla < ./test_longline.R
> 
> with or without --no-readline. Now, if I start R manually and then paste the
> output of test_longline.R into the console, I get undesired behavior:
> 
> Error: syntax error in "cat(nchar(x),"\"
> 
> but if I start R without readline and paste, no problem.
> 
> Jeff
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list