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