* Copyright (c) 1980, 1991 The Regents of the University of California.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)lex.c 5.16 (Berkeley) 6/8/91";
* These lexical routines read input and form lists of words.
* There is some involved processing here, because of the complications
* of input buffering, and especially because of history substitution.
static Char
*word
__P((void));
static int getC1
__P((int));
static void getdol
__P((void));
static void getexcl
__P((int));
*findev
__P((Char
*, bool));
static void setexclp
__P((Char
*));
static int bgetc
__P((void));
static void bfree
__P((void));
static int matchs
__P((Char
*, Char
*));
static int getsel
__P((int *, int *, int));
*getsub
__P((struct wordent
*));
static Char
*subword
__P((Char
*, int, bool *));
*dosub
__P((int, struct wordent
*, bool));
* Peekc is a peek character for getC, peekread for readc.
* There is a subtlety here in many places... history routines
* will read ahead and then insert stuff into the input stream.
* If they push back a character then they must push it behind
* the text substituted by the history substitution. On the other
* hand in several places we need 2 peek characters. To make this
* all work, the history routines read with getC, and make use both
* of ungetC and unreadc. The key observation is that the state
* of getC at the call of a history reference is such that calls
* to getC from the history routines will always yield calls of
* readc, unless this peeking is involved. That is to say that during
* getexcl the variables lap, exclp, and exclnxt are all zero.
* Getdol invokes history substitution, hence the extra peek, peekd,
* which it can ungetD to be before history substitutions.
static Char peekc
= 0, peekd
= 0;
static Char peekread
= 0;
/* (Tail of) current word from ! subst */
static Char
*exclp
= NULL
;
/* The rest of the ! subst words */
static struct wordent
*exclnxt
= NULL
;
/* Count of remaining words in ! subst */
/* "Globp" for alias resubstitution */
static Char
*alvecp
= NULL
;
* Labuf implements a general buffer for lookahead during lexical operations.
* Text which is to be placed in the input stream can be stuck here.
* We stick parsed ahead $ constructs during initial input,
* process id's from `$$', and modified variable values (from qualifiers
* during expansion in sh.dol.c) here.
static Char labuf
[BUFSIZ
];
* Lex returns to its caller not only a wordlist (as a "var" parameter)
* but also whether a history substitution occurred. This is used in
* the main (process) routine to determine whether to echo, and also
* when called by the alias routine to determine whether to keep the
* Avoid alias expansion recursion via \!#
#define getC(f) ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
#define ungetC(c) peekc = c
#define ungetD(c) peekd = c
register struct wordent
*hp
;
register struct wordent
*wdp
;
hp
->next
= hp
->prev
= hp
;
while (c
== ' ' || c
== '\t');
if (c
== HISTSUB
&& intty
)
/* ^lef^rit from tty is short !:s^lef^rit */
* The following loop is written so that the links needed by freelex will
* be ready and rarin to go even if it is interrupted.
register struct wordent
*new;
new = (struct wordent
*) xmalloc((size_t) sizeof(*wdp
));
} while (wdp
->word
[0] != '\n');
register struct wordent
*sp
= sp0
->next
;
xprintf("%s", short2str(sp
->word
));
register struct wordent
*hp
;
register struct wordent
*fp
;
register struct wordent
*wdp
;
register struct wordent
*new;
new = (struct wordent
*) xmalloc((size_t) sizeof(*wdp
));
wdp
->word
= Strsave(fp
->word
);
} while (wdp
->word
[0] != '\n');
register struct wordent
*vp
;
register struct wordent
*fp
;
while ((c
= getC(DOALL
)) == ' ' || c
== '\t');
if (cmap(c
, _META
| _ESC
))
* if (c1 == '`') c = ' '; else
seterror(ERR_UNMATCHED
, c1
);
else if (cmap(c
, _META
| _Q
| _Q1
| _ESC
)) {
else if (cmap(c
, _Q
| _Q1
)) { /* '"` */
dolflg
= c
== '"' ? DOALL
: DOEXCL
;
else if (c
!= '#' || !intty
) {
if (cmap(c
, _META
| _Q
| _Q1
))
if (exclnxt
&& --exclc
>= 0) {
if (c
== '$' && (flag
& DODOL
)) {
if (c
== HIST
&& (flag
& DOEXCL
)) {
Char name
[4 * MAXVARLEN
+ 1];
bool special
= 0, toolong
;
*np
++ = c
, c
= getC(DOEXCL
);
if (c
== '#' || c
== '?')
special
++, *np
++ = c
, c
= getC(DOEXCL
);
/* let $?0 pass for now */
/* we know that np < &name[4] */
while (c
= getC(DOEXCL
)) {
/* we know that np < &name[4] */
while (c
= getC(DOEXCL
)) {
/* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
if (!letter(c
) && !Isdigit(c
))
seterror(ERR_VARTOOLONG
);
* Name up to here is a max of MAXVARLEN + 8.
ep
= &np
[2 * MAXVARLEN
+ 8];
* Michael Greim: Allow $ expansion to take place in selector
* expressions. (limits the number of characters returned)
c
= getC(DOEXCL
| DODOL
);
* Name up to here is a max of 2 * MAXVARLEN + 8.
* if the :g modifier is followed by a newline, then error right away!
*np
++ = c
, c
= getC(DOEXCL
);
gmodflag
++, *np
++ = c
, c
= getC(DOEXCL
);
if (gmodflag
&& c
== '\n')
stderror(ERR_VARSYN
); /* strike */
seterror(ERR_MISSING
, '}');
if (Strlen(cp
) + (lap
? Strlen(lap
) : 0) >=
(sizeof(labuf
) - 4) / sizeof(Char
)) {
(void) Strcpy(labuf
, cp
);
(void) Strcat(labuf
, buf
);
register struct wordent
*hp
, *ip
;
for (ip
= hp
->next
->next
; ip
!= alhistt
; ip
= ip
->next
)
for (ip
= hp
->next
->next
; ip
!= hp
->prev
; ip
= ip
->next
)
ungetC('s'), unreadc(HISTSUB
), c
= ':';
if (letter(c
) || c
== '&') {
if (!getsel(&left
, &right
, dol
))
if (!getsel(&left
, &right
, dol
))
exclc
= right
- left
+ 1;
if (sc
== HISTSUB
|| c
== ':') {
Char orhsb
[sizeof(rhsb
) / sizeof(Char
)];
global
++, sc
= c
= getC(0);
(void) Strcpy(lhsb
, slhs
);
if (letter(delim
) || Isdigit(delim
) || any(" \t\n", delim
)) {
if (cp
> &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2]) {
if (c
!= delim
&& c
!= '\\')
(void) Strcpy(orhsb
, cp
);
if (&cp
[Strlen(orhsb
)] > &rhsb
[sizeof(rhsb
) / sizeof(Char
) - 2])
(void) Strcpy(cp
, orhsb
);
if (cp
> &rhsb
[sizeof(rhsb
) / sizeof(Char
) - 2]) {
if (c
!= delim
/* && c != '~' */ )
seterror(ERR_BADBANGMOD
, c
);
(void) Strcpy(slhs
, lhsb
);
en
= dosub(sc
, en
, global
);
struct wordent
*hp
= &lexi
;
register struct wordent
*wdp
;
register struct wordent
*new;
new = (struct wordent
*) xcalloc(1, sizeof *wdp
);
wdp
->word
= (en
->word
&& (global
||didsub
== 0)) ?
subword(en
->word
, sc
, &didsub
) : Strsave(en
->word
);
return (&enthist(-1000, &lexi
, 0)->Hlex
);
register Char
*wp
, *mp
, *np
;
for (np
= rhsb
; *np
; np
++)
for (xp
= wp
; c
= *xp
; xp
++)
if ((c
!= ' ' && c
!= '\t') || type
== 'q')
if (!any(short2str(cp
), '/'))
return (type
== 't' ? Strsave(cp
) : 0);
xp
= Strsave(cp
), xp
[wp
- cp
] = 0;
for (wp
--; wp
>= cp
&& *wp
!= '/'; wp
--)
xp
= Strsave(cp
), xp
[wp
- cp
] = 0;
return (Strsave(type
== 'e' ? STRNULL
: cp
));
register Char
*str
, *pat
;
while (*str
&& *pat
&& *str
== *pat
)
register int c
= getC(0);
seterror(ERR_BADBANGARG
);
if (*al
> *ar
|| *ar
> dol
) {
seterror(ERR_BADBANGARG
);
register struct Hist
*hp
;
c
= sc
== HISTSUB
? HIST
: getC(0);
if (lastev
== eventno
&& alhistp
)
case '#': /* !# is command being typed in (mrh) */
while (!any(": \t\\\n}", c
)) {
if (event
!= -1 && Isdigit(c
))
event
= event
* 10 + c
- '0';
if (np
< &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2])
* History had only digits
event
= eventno
+ (alhistp
== 0) - (event
? event
: 0);
if (np
< &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2])
for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
)
seterror(ERR_NOEVENT
, short2str(np
));
register struct Hist
*hp
;
for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
) {
register struct wordent
*lp
= hp
->Hlex
.next
;
* The entries added by alias substitution don't have a newline but do
* have a negative event number. Savehist() trims off these entries,
* but it happens before alias expansion, too early to delete those
* from the previous command.
for (dp
= lp
->word
; *dp
; dp
++) {
} while (lp
->word
[0] != '\n');
seterror(ERR_NOEVENT
, short2str(cp
));
if (evalvec
== (Char
**) 1) {
if (arginp
== (Char
*) 1 || onelflg
== 1) {
if ((c
= *arginp
++) == 0) {
/* was isatty but raw with ignoreeof yields problems */
if (tcgetattr(SHIN
, &tty
) == 0 && (tty
.c_lflag
& ICANON
))
/* was 'short' for FILEC */
(ctpgrp
= tcgetpgrp(FSHTTY
)) != -1 &&
(void) tcsetpgrp(FSHTTY
, tpgrp
);
(void) killpg((pid_t
) ctpgrp
, SIGHUP
);
xprintf("Reset tty pgrp from %d to %d\n", ctpgrp
, tpgrp
);
if (adrof(STRignoreeof
)) {
xprintf("\nUse \"logout\" to logout.\n");
xprintf("\nUse \"exit\" to leave csh.\n");
if (c
== '\n' && onelflg
)
register int buf
, off
, c
;
register int numleft
= 0, roomleft
;
if (fseekp
< fbobp
|| fseekp
> feobp
) {
(void) lseek(SHIN
, fseekp
, L_SET
);
c
= read(SHIN
, tbuf
, BUFSIZ
);
while (c
< 0 && errno
== EINTR
);
fbuf
[0][i
] = (unsigned char) tbuf
[i
];
c
= fbuf
[0][fseekp
- fbobp
];
buf
= (int) fseekp
/ BUFSIZ
;
(Char
**) xcalloc((size_t) (fblocks
+ 2),
(void) blkcpy(nfbuf
, fbuf
);
fbuf
[fblocks
] = (Char
*) xcalloc(BUFSIZ
, sizeof(Char
));
buf
= (int) feobp
/ BUFSIZ
;
off
= (int) feobp
% BUFSIZ
;
c
= numleft
? numleft
: tenex(ttyline
, BUFSIZ
);
/* start with fresh buffer */
feobp
= fseekp
= fblocks
* BUFSIZ
;
bcopy(ttyline
, fbuf
[buf
] + off
, c
* sizeof(Char
));
c
= read(SHIN
, tbuf
, roomleft
);
Char
*ptr
= fbuf
[buf
] + off
;
ptr
[i
] = (unsigned char) tbuf
[i
];
if (errno
== EWOULDBLOCK
) {
(void) ioctl(SHIN
, FIONBIO
, (ioctl_t
) & off
);
c
= fbuf
[buf
][(int) fseekp
% BUFSIZ
];
sb
= (int) (fseekp
- 1) / BUFSIZ
;
(void) blkcpy(fbuf
, &fbuf
[sb
]);
register struct whyle
*wp
;
* Christos: I don't understand this? both wp and l are local. What is
* this used for? I suspect the author meant fseek = wp->w_start
* This seek/tell stuff needs to be re-written...
for (wp
= whyles
; wp
->w_next
; wp
= wp
->w_next
)
(void) lseek(SHIN
, (off_t
) 0, L_XTND
);
if (arginp
|| onelflg
|| intty
)
if (lseek(SHIN
, (off_t
) 0, L_INCR
) < 0 || errno
== ESPIPE
)
fbuf
= (Char
**) xcalloc(2, sizeof(Char
**));
fbuf
[0] = (Char
*) xcalloc(BUFSIZ
, sizeof(Char
));
fseekp
= fbobp
= feobp
= lseek(SHIN
, (off_t
) 0, L_INCR
);