read.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* read [-ACprs] [-d delim] [-u filenum] [-t timeout] [-n n] [-N n] [name...]
*
* David Korn
* AT&T Labs
*
*/
#include <ast.h>
#include <error.h>
#include "defs.h"
#include "variables.h"
#include "lexstates.h"
#include "io.h"
#include "name.h"
#include "builtins.h"
#include "history.h"
#include "terminal.h"
#include "edit.h"
struct read_save
{
char **argv;
char *prompt;
short fd;
short plen;
int flags;
long timeout;
};
{
register char *name;
if(argc==0)
{
if(rp)
return(0);
}
if(rp)
{
goto bypass;
}
{
case 'A':
break;
case 'C':
break;
case 't':
break;
case 'd':
{
}
break;
case 'p':
break;
case 'n': case 'N':
break;
case 'r':
break;
case 's':
/* save in history file */
break;
case 'S':
break;
case 'u':
fd = -1;
break;
case 'v':
break;
case ':':
break;
case '?':
break;
}
if(error_info.errors)
/* look for prompt */
else
r = 0;
{
}
{
}
shp->nextprompt = 0;
{
}
return(r);
}
/*
* here for read timeout
*/
{
sh_exit(1);
}
/*
* This is the code to read a line and to split it into tokens
* <names> is an array of variable names
* <fd> is the file descriptor
* <flags> is union of -A, -r, -s, and contains delimiter if not '\n'
* <timeout> is number of milli-seconds until timeout
*/
int sh_readline(register Shell_t *shp,char **names, volatile int fd, int flags,ssize_t size,long timeout)
{
register ssize_t c;
register unsigned char *cp;
char *ifs;
unsigned char *cpmax;
unsigned char *del;
char was_escape = 0;
char use_stak = 0;
volatile char was_write = 0;
volatile char was_share = 1;
volatile int keytrap;
long array_index = 0;
void *timeslot=0;
int delim = '\n';
int jmpval=0;
int binary;
char inquote = 0;
return(1);
{
*val = 0;
{
array_index = 1;
}
{
delim = -1;
}
else
if(val)
*val = '?';
}
else
{
name = 0;
else
}
{
{
}
}
{
/* set up state table based on IFS */
if(delim>0)
if(delim!='\n')
{
}
{
}
}
{
{
return(c);
}
}
{
}
if(fd==0)
{
if(jmpval)
goto done;
if(timeout)
}
{
/* reserved buffer */
{
sh_exit(1);
}
else
was_share = 1;
if(size==0)
{
c = 0;
}
else
{
ssize_t m;
int f;
for (;;)
{
c = size;
if(keytrap)
{
cp = 0;
f = 0;
m = 0;
m++;
if(m>0)
}
else
{
f = 1;
{
c = size;
f = 0;
}
else
{
}
}
{
*v++ = 0;
m = v-(char*)cp;
}
if((c=m)>size)
c = size;
if(c>0)
{
{
{
v = (char*)malloc(m+1);
}
else
}
if(f)
cur += c;
#if SHOPT_MULTIBYTE
{
int x;
int z;
mbinit();
*cur = 0;
x = z = 0;
{
up += z;
x++;
}
continue;
}
#endif
}
#if SHOPT_MULTIBYTE
#endif
*cur = 0;
{
if(m)
break;
}
size -= c;
}
}
if(timeslot)
{
else
{
nv_setsize(np,c);
nv_setsize(mp,c);
}
}
else
{
}
goto done;
}
{
}
if(timeslot)
{
sh_histinit((void*)shp);
}
if(cp)
{
#if SHOPT_CRNL
cpmax--;
#endif /* SHOPT_CRNL */
#if !SHOPT_MULTIBYTE
{
/* skip over leading blanks */
while(c==S_SPACE)
/* strip trailing delimiters */
cpmax--;
{
cpmax[1] = 0;
}
else
*cpmax =0;
{
jmpval = 1;
}
else
goto done;
}
#endif /* !SHOPT_MULTIBYTE */
}
else
c = S_NL;
/* val==0 at the start of a field */
val = 0;
del = 0;
while(1)
{
switch(c)
{
#if SHOPT_MULTIBYTE
case S_MBYTE:
if(val==0)
{
if(name)
cp[-1] = 0;
if(c>1)
cp += (c-1);
c = S_DELIM;
}
else
c = 0;
continue;
#endif /*SHOPT_MULTIBYTE */
case S_QUOTE:
if(val)
{
use_stak = 1;
*val = 0;
}
continue;
case S_ESC:
/* process escape character */
was_escape = 1;
else
c = 0;
if(val)
{
use_stak = 1;
was_escape = 1;
*val = 0;
}
continue;
case S_ERR:
cp++;
case S_EOF:
/* check for end of buffer */
{
use_stak = 1;
}
val = 0;
{
c = S_NL;
break;
}
/* eliminate null bytes */
c = 0;
continue;
case S_NL:
if(was_escape)
{
was_escape = 0;
if(cp)
{
val=0;
c = 0;
continue;
}
}
c = S_NL;
break;
case S_SPACE:
/* skip over blanks */
if(!val)
continue;
#if SHOPT_MULTIBYTE
if(c==S_MBYTE)
{
{
cp += (c-1);
c = S_DELIM;
}
else
c = 0;
}
#endif /* SHOPT_MULTIBYTE */
if(c!=S_DELIM)
break;
/* FALL THRU */
case S_DELIM:
if(!del)
if(name)
{
/* skip over trailing blanks */
break;
}
/* FALL THRU */
case 0:
if(val==0 || was_escape)
{
was_escape = 0;
}
/* skip over word characters */
wrd = -1;
while(1)
{
if(!wrd)
wrd = 1;
if(inquote)
{
if(c==S_QUOTE)
{
{
if(val)
{
use_stak = 1;
}
}
else
break;
}
if(c && c!=S_EOF)
{
if(c==S_NL)
{
if(val)
{
use_stak=1;
}
}
continue;
}
}
break;
if(wrd<0)
wrd = 0;
}
if(wrd>0)
del = (unsigned char*)"";
if(c!=S_MBYTE)
cp[-1] = 0;
continue;
}
/* assign value and advance to next variable */
if(!val)
val = "";
if(use_stak)
{
stakputc(0);
}
{
/* strip off trailing space delimiters */
{
vp--;
else
}
vp[1] = 0;
}
{
jmpval = 1;
}
else
val = 0;
del = 0;
if(use_stak)
{
use_stak = 0;
}
if(array_index)
{
if(c!=S_NL)
continue;
}
while(1)
{
{
}
if(name)
{
}
else
np = 0;
if(c!=S_NL)
break;
if(!np)
goto done;
{
jmpval = 1;
}
else
}
}
done:
if(was_write)
if(!was_share)
tty_cooked(fd);
if(jmpval > 1)
return(jmpval);
}