[Rd] Command line length limits in R

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 25 08:48:53 CEST 2006


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().


GUI writers: please take a look at how your interface handles very long 
input lines, particularly quoted strings (which seem the most likely 
reason for long lines).

-- 
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