NetBSD/bin/sh/syntax.c
kre 727a69dc1d A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code.  The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good.  That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO).  There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
2017-06-07 05:08:32 +00:00

109 lines
2.4 KiB
C

/* $NetBSD: syntax.c,v 1.4 2017/06/07 05:08:32 kre Exp $ */
#include <sys/cdefs.h>
__RCSID("$NetBSD: syntax.c,v 1.4 2017/06/07 05:08:32 kre Exp $");
#include <limits.h>
#include "shell.h"
#include "syntax.h"
#include "parser.h"
#if CWORD != 0
#error initialisation assumes 'CWORD' is zero
#endif
#define ndx(ch) (ch + 1 - CHAR_MIN)
#define set(ch, val) [ndx(ch)] = val,
#define set_range(s, e, val) [ndx(s) ... ndx(e)] = val,
/* syntax table used when not in quotes */
const char basesyntax[257] = { CEOF,
set_range(CTL_FIRST, CTL_LAST, CCTL)
set('\n', CNL)
set('\\', CBACK)
set('\'', CSQUOTE)
set('"', CDQUOTE)
set('`', CBQUOTE)
set('$', CVAR)
set('}', CENDVAR)
set('<', CSPCL)
set('>', CSPCL)
set('(', CSPCL)
set(')', CSPCL)
set(';', CSPCL)
set('&', CSPCL)
set('|', CSPCL)
set(' ', CSPCL)
set('\t', CSPCL)
};
/* syntax table used when in double quotes */
const char dqsyntax[257] = { CEOF,
set_range(CTL_FIRST, CTL_LAST, CCTL)
set('\n', CNL)
set('\\', CBACK)
set('"', CDQUOTE)
set('`', CBQUOTE)
set('$', CVAR)
set('}', CENDVAR)
/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
set('!', CCTL)
set('*', CCTL)
set('?', CCTL)
set('[', CCTL)
set('=', CCTL)
set('~', CCTL)
set(':', CCTL)
set('/', CCTL)
set('-', CCTL)
};
/* syntax table used when in single quotes */
const char sqsyntax[257] = { CEOF,
set_range(CTL_FIRST, CTL_LAST, CCTL)
set('\n', CNL)
set('\'', CSQUOTE)
/* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
set('!', CCTL)
set('*', CCTL)
set('?', CCTL)
set('[', CCTL)
set('=', CCTL)
set('~', CCTL)
set(':', CCTL)
set('/', CCTL)
set('-', CCTL)
};
/* syntax table used when in arithmetic */
const char arisyntax[257] = { CEOF,
set_range(CTL_FIRST, CTL_LAST, CCTL)
set('\n', CNL)
set('\\', CBACK)
set('`', CBQUOTE)
set('\'', CSQUOTE)
set('"', CDQUOTE)
set('$', CVAR)
set('}', CENDVAR)
set('(', CLP)
set(')', CRP)
};
/* character classification table */
const char is_type[257] = { 0,
set_range('0', '9', ISDIGIT)
set_range('a', 'z', ISLOWER)
set_range('A', 'Z', ISUPPER)
set('_', ISUNDER)
set('#', ISSPECL)
set('?', ISSPECL)
set('$', ISSPECL)
set('!', ISSPECL)
set('-', ISSPECL)
set('*', ISSPECL)
set('@', ISSPECL)
set(' ', ISSPACE)
set('\t', ISSPACE)
set('\n', ISSPACE)
};