Home | History | Annotate | Line # | Download | only in sh
syntax.h revision 1.13.2.1
      1  1.13.2.1  perseant /*	$NetBSD: syntax.h,v 1.13.2.1 2025/08/02 05:18:26 perseant Exp $	*/
      2       1.1       dsl 
      3       1.1       dsl /*-
      4       1.1       dsl  * Copyright (c) 1991, 1993
      5       1.1       dsl  *	The Regents of the University of California.  All rights reserved.
      6       1.1       dsl  *
      7       1.1       dsl  * This code is derived from software contributed to Berkeley by
      8       1.1       dsl  * Kenneth Almquist.
      9       1.1       dsl  *
     10       1.1       dsl  * Redistribution and use in source and binary forms, with or without
     11       1.1       dsl  * modification, are permitted provided that the following conditions
     12       1.1       dsl  * are met:
     13       1.1       dsl  * 1. Redistributions of source code must retain the above copyright
     14       1.1       dsl  *    notice, this list of conditions and the following disclaimer.
     15       1.1       dsl  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       dsl  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       dsl  *    documentation and/or other materials provided with the distribution.
     18       1.1       dsl  * 3. Neither the name of the University nor the names of its contributors
     19       1.1       dsl  *    may be used to endorse or promote products derived from this software
     20       1.1       dsl  *    without specific prior written permission.
     21       1.1       dsl  *
     22       1.1       dsl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1       dsl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1       dsl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1       dsl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1       dsl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1       dsl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1       dsl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1       dsl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1       dsl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1       dsl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       dsl  * SUCH DAMAGE.
     33       1.1       dsl  */
     34       1.1       dsl 
     35       1.1       dsl #include <sys/cdefs.h>
     36       1.1       dsl #include <ctype.h>
     37      1.13       kre #include <limits.h>
     38       1.1       dsl 
     39  1.13.2.1  perseant #undef	CEOF			/* remove system defined version */
     40  1.13.2.1  perseant 
     41       1.1       dsl /* Syntax classes */
     42       1.1       dsl #define CWORD 0			/* character is nothing special */
     43       1.1       dsl #define CNL 1			/* newline character */
     44       1.1       dsl #define CBACK 2			/* a backslash character */
     45       1.1       dsl #define CSQUOTE 3		/* single quote */
     46       1.1       dsl #define CDQUOTE 4		/* double quote */
     47       1.1       dsl #define CBQUOTE 5		/* backwards single quote */
     48       1.1       dsl #define CVAR 6			/* a dollar sign */
     49       1.1       dsl #define CENDVAR 7		/* a '}' character */
     50       1.1       dsl #define CLP 8			/* a left paren in arithmetic */
     51       1.1       dsl #define CRP 9			/* a right paren in arithmetic */
     52       1.1       dsl #define CEOF 10			/* end of file */
     53      1.10       kre #define CSPCL 11		/* these terminate a word */
     54      1.10       kre #define CCTL 12			/* like CWORD, except it must be escaped */
     55       1.9       kre #define CSBACK 13		/* a backslash in a single quote syntax */
     56      1.11       kre #define CFAKE 14		/* a delimiter that does not exist */
     57      1.10       kre 	/*
     58      1.10       kre 	 * note CSBACK == (CCTL|1)
     59      1.10       kre 	 * the code does not rely upon that, but keeping it allows a
     60      1.10       kre 	 * smart enough compiler to optimise some tests
     61      1.10       kre 	 */
     62       1.1       dsl 
     63       1.1       dsl /* Syntax classes for is_ functions */
     64       1.1       dsl #define ISDIGIT 01		/* a digit */
     65       1.1       dsl #define ISUPPER 02		/* an upper case letter */
     66       1.1       dsl #define ISLOWER 04		/* a lower case letter */
     67       1.1       dsl #define ISUNDER 010		/* an underscore */
     68       1.1       dsl #define ISSPECL 020		/* the name of a special parameter */
     69       1.9       kre #define ISSPACE 040		/* a white space character */
     70       1.1       dsl 
     71      1.11       kre #define PEOF	(CHAR_MIN - 1)
     72      1.11       kre #define PFAKE	(CHAR_MIN - 2)
     73      1.11       kre #define SYNBASE	(-PFAKE)
     74       1.1       dsl 
     75       1.1       dsl 
     76       1.1       dsl #define BASESYNTAX (basesyntax + SYNBASE)
     77       1.1       dsl #define DQSYNTAX (dqsyntax + SYNBASE)
     78       1.1       dsl #define SQSYNTAX (sqsyntax + SYNBASE)
     79       1.1       dsl #define ARISYNTAX (arisyntax + SYNBASE)
     80       1.1       dsl 
     81       1.3  christos /* These defines assume that the digits are contiguous (which is guaranteed) */
     82       1.3  christos #define	is_digit(c)	((unsigned)((c) - '0') <= 9)
     83       1.9       kre #define	sh_ctype(c)	(is_type+SYNBASE)[(int)(c)]
     84       1.7       kre #define	is_upper(c)	(sh_ctype(c) & ISUPPER)
     85       1.7       kre #define	is_lower(c)	(sh_ctype(c) & ISLOWER)
     86       1.4  christos #define	is_alpha(c)	(sh_ctype(c) & (ISUPPER|ISLOWER))
     87       1.4  christos #define	is_name(c)	(sh_ctype(c) & (ISUPPER|ISLOWER|ISUNDER))
     88       1.4  christos #define	is_in_name(c)	(sh_ctype(c) & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))
     89       1.9       kre #define	is_special(c)	(sh_ctype(c) & (ISSPECL|ISDIGIT))
     90       1.9       kre #define	is_space(c)	(sh_ctype(c) & ISSPACE)
     91       1.9       kre #define	digit_val(c)	((c) - '0')
     92       1.1       dsl 
     93      1.10       kre /* true if the arg char needs CTLESC to protect it */
     94      1.10       kre #define	NEEDESC(c)	(SQSYNTAX[(int)(c)] == CCTL || \
     95      1.10       kre 			 SQSYNTAX[(int)(c)] == CSBACK)
     96      1.10       kre 
     97      1.12       kre #define	ISCTL(c)	((c) >= CTL_FIRST && (c) <= CTL_LAST)
     98      1.12       kre #if 0				/* alternative form (generally slower) */
     99      1.13       kre #define	ISCTL(c)	(BASESYNTAX[(int)(c)] == CCTL)
    100      1.12       kre #endif
    101      1.12       kre 
    102       1.1       dsl extern const char basesyntax[];
    103       1.1       dsl extern const char dqsyntax[];
    104       1.1       dsl extern const char sqsyntax[];
    105       1.1       dsl extern const char arisyntax[];
    106       1.1       dsl extern const char is_type[];
    107