rh

Check-in [498f1a49a5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initial revision
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:498f1a49a5d8f8a6390f144999880bc714ddf3b614616caf6bc1566047b85811
User & Date: ajv-899-334-8894@vsta.org 2008-12-27 00:56:03
Context
2016-08-10
18:23
Initial commit check-in: f65c760fdd user: ajv-900-660-8060@vsta.org tags: master, trunk
2009-10-02
15:28
Null strings no longer open "." check-in: 70902a4866 user: ajv-899-334-8894@vsta.org tags: master, trunk
2008-12-27
00:56
Initial revision check-in: 498f1a49a5 user: ajv-899-334-8894@vsta.org tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added Makefile.































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# rcsid: $Id: Makefile,v 1.3 1994/01/26 03:17:29 rick Exp $

PROGRAM = rh

CFLAGS=-g
CC=gcc

# Possible C preprocessor flags:
#	-DBSD			    BSD directory handling
#	-DSUNOS_4		    compiling on SunOS 4.1.x
#	-DPOSIX_DIRECTORY_LIBRARY   POSIX directory handling
#
# If neither -DBSD or -DPOSIX_DIRECTORY_LIBRARY is specified then System V
# directory handling will be used.
#

CPPFLAGS= -DBSD -DSUNOS_4 $(DEBUG_CPPFLAGS)
CPP=gcc -E

YACC=byacc

LEX = flex
LFLAGS = $(DEBUG_LFLAGS) -s

DEPENDS = $(PROGRAM).depends

SRCS =			\
	alloc.c		\
	errmsg.c	\
	getdate.c	\
	resname.c	\
	rh.c		\
	rhcmds.c	\
	rhdata.c	\
	rhdir.c		\
	rhgram.c	\
	rhlex.c		\
	strtol.c	\
	strtoul.c	\
	sysinfo.c	\
	wildmat.c	\
	$(DEBUG_SRCS)

# strtol.o and strtoul.o can be removed from the object files list
# if they work correctly on your system
# NOTE: strtol() is broken under SunOS. It doesn't always set the
# 'end' pointer. Also, strtoul() does not exist under SunOS 4.1.x.

OBJS =			\
	alloc.o		\
	errmsg.o	\
	getdate.o	\
	resname.o	\
	rh.o		\
	rhcmds.o	\
	rhdata.o	\
	rhdir.o		\
	rhgram.o	\
	rhlex.o		\
	strtol.o	\
	strtoul.o	\
	sysinfo.o	\
	wildmat.o	\
	$(DEBUG_OBJS)

all: debug

$(PROGRAM): $(OBJS)
	$(CC) $(OBJS) -o $(PROGRAM)

insight:
	$(MAKE) DEBUG_YFLAGS=-t DEBUG_LFLAGS=-d		\
		DEBUG_SRCS=dump.c DEBUG_OBJS=dump.o	\
		DEBUG_CPPFLAGS="-DDEBUG"		\
		CC=insight $(PROGRAM)

purify: $(OBJS)
	purify -logfile=purify.out		\
	       -inuse-at-exit			\
	       -chain-length=200		\
	       -free-queue-length=1000		\
	    $(CC) $(OBJS) -o $(PROGRAM).p

tc: $(OBJS)
	proof $(CC) $(OBJS) -o $(PROGRAM).tc

debug:
	$(MAKE) DEBUG_YFLAGS=-t DEBUG_LFLAGS=-d		\
		DEBUG_SRCS=dump.c DEBUG_OBJS=dump.o	\
		DEBUG_CPPFLAGS="-DDEBUG" $(PROGRAM)

debug_p:
	$(MAKE) DEBUG_YFLAGS=-t DEBUG_LFLAGS=-d		\
		DEBUG_SRCS=dump.c DEBUG_OBJS=dump.o	\
		DEBUG_CPPFLAGS="-DDEBUG" purify

rhgram.c rhgram.h: rhgram.y
	$(YACC) $(DEBUG_YFLAGS) -d -p rh_ rhgram.y
	mv y.tab.c rhgram.c
	mv y.tab.h rhgram.h
	# $(YACC) $(DEBUG_YFLAGS) -d -B -p rh_ rhgram.y

getdate.c: getdate.y
	$(YACC) $(DEBUG_YFLAGS) -p gd_ getdate.y
	mv y.tab.c getdate.c
	# $(YACC) $(DEBUG_YFLAGS) B -p gd_ getdate.y

cc_src: $(SRCS)
	#load $(CPPFLAGS) $(SRCS)
	#link

clean:
	$(RM) *.o core *~ *.output
	$(RM) purify.out .purifyversion lex.backtrack *.pure_* .pure
	$(RM) *.bak *.BAK rhgram.tab.[ch] getdate.tab.[ch]
	$(RM) -r TC.Cache rh.tc*
	$(RM) tca.map tca.log *.out *.ins.c

clobber: clean
	$(RM) rhgram.[ch] rhlex.c getdate.c
	$(RM) $(PROGRAM) $(PROGRAM).p $(PROGRAM).tc

depend: $(SRCS) dump.c
	./makedep -r -e /usr/include/ -e /usr/local/gnu/	\
		-m $(DEPENDS) -o '$(CPPFLAGS)' $(SRCS) dump.c

include $(DEPENDS)

Added README.

















































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# rcsid: $Id: README,v 1.1 1994/01/26 01:55:11 rick Exp $

An ANSI/ISO C compiler is required

If rhlex.c is deleted then you must have flex version 2.4.6 or higher
to create it.

If getdate.c, rhgram.c, or rhgram.h is deleted then you must have perl-byacc
version 1.8.3 or higher to create the deleted file(s).


KNOWN BUGS:

	The pointer to the error in error messages may be wrong. (Actually,
	it is more likely to be wrong than correct.)

	Function parameters can not have the same names as functions.
	Example:
		b(c) { return c + 2; }
		a(b) { return b + 2; }	/* won't compile - b is a function */ 

	The file system operator only works on SunOS 4.1.x

	No configure script - coming RSN!

Added SCOPTIONS.



















>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
# rcsid: $Id: SCOPTIONS,v 1.1 1994/01/12 19:32:53 rick Exp $
MATH=68881
CPU=68030
ANSI
OPTIMIZE
STRINGSCONST
NOVERSION
STRICT
MEMORYSIZE=HUGE

Added TODO.





















>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
# rcsid: $Id: TODO,v 1.2 1994/01/26 01:54:18 rick Exp $
ino == inode

separate symbol table for parameters?

	b(c) { return c + 2; } a(b) { return b + 2; }
    parses wrong because b is a function

set static symbols value to instr_value?
	now->value becomes now->value.date_info.seconds ...

Added alloc.c.

















































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * alloc.c
 *
 * Written by: Rick Ohnemus	(rick@sterling.com)
 *
 * This file contains functions related to dynamic memory allocation.
 *
 * Global Functions: allocate, reallocate, savestr
 */

#if !defined(lint)
static char rcsid[] = "$Id: alloc.c,v 1.1 1994/01/12 19:33:22 rick Exp $";
#endif

#include <stdlib.h>
#include <string.h>

#include "errmsg.h"
 
/*
 * allocate:
 *	Allocate 'n' bytes of memory. If the allocation fails a
 *	message is displayed containing the name of the source
 *	file and line number in the file that allocate() was
 *	called from.
 */

void *allocate(size_t n, const char *file, const int line)
{
    void *p = malloc(n);
    if (p == NULL) {
	error("%s : %d : no space : %m", file, line);
    }
    return p;
}
 
/*
 * reallocate:
 *	Reallocate 'n' bytes of memory. If the address to
 *	reallocate is null then just call allocate() to
 *	allocate a new block of memory.
 *	If the allocation fails a message is displayed
 *	containing the name of the source file and line
 *	number in the file that reallocate() was called from.
 */

void *reallocate(void *a, size_t n, const char *file, const int line)
{
    void *p;

    if (a == NULL) {
	return allocate(n, file, line);
    }

    p = realloc(a, n);
    if (p == NULL) {
	error("%s : %d : no space : %m", file, line);
    }

    return p;
}
 
/*
 * savestr:
 *	Allocate space for a string then copy the string
 *	into the newly allocated memory.
 */

char *savestr(const char *str, const char *file, const int line)
{
    return strcpy(allocate(strlen(str) + 1, file, line), str);
}

Added alloc.h.









































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * alloc.h
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 *
 * rcsid: $Id: alloc.h,v 1.1 1994/01/12 19:33:38 rick Exp $
 */

#if !defined(__ALLOC_H__)
#define __ALLOC_H__

#define ALLOCATE(n)	 allocate((n), __FILE__, __LINE__)
#define REALLOCATE(a, n) reallocate((a), (n), __FILE__, __LINE__)
#define SAVESTR(str)	 savestr((str), __FILE__, __LINE__)

extern void *allocate(size_t n, const char *file, const int line);
extern void *reallocate(void *a, size_t n, const char *file, const int line);
extern char *savestr(const char *str, const char *file, const int line);

#endif	/* __ALLOC_H__ */

Added dump.c.



















































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * dump.c
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 *
 * This file contain data structures and functions to dump an rh
 * compiled program in a human readable form.
 *
 * Global Functions: dump_instr, dump_program
 */

#if !defined(lint)
static char rcsid[] = "$Id: dump.c,v 1.2 1994/01/26 01:46:21 rick Exp $";
#endif

#include <time.h>
#include "rh.h"
#include "rhcmds.h"
#include "rhdata.h"

extern int		activate(int pc);
extern void		deactivate(int pc);
extern const char *	func_name(int pc);

struct fmap {
    void (*func)(instr_value *);	/* function address */
    char *name;				/* function name */
};

/* map of rh C function addresses to names */

static const struct fmap funcinfo[] = {
    { c_and,		"c_and" },
    { c_atime,		"c_atime" },
    { c_band,		"c_band" },
    { c_baselen,	"c_baselen" },
    { c_bnot,		"c_bnot" },
    { c_bor,		"c_bor" },
    { c_bxor,		"c_bxor", },
    { c_colon,		"c_colon" },
    { c_ctime,		"c_ctime" },
    { c_date,		"c_date" },
    { c_depth,		"c_depth" },
    { c_dev,		"c_dev" },
    { c_dirlen,		"c_dirlen" },
    { c_div,		"c_div" },
    { c_eq,		"c_eq" },
    { c_fncase,		"c_fncase" },
    { c_fnigncase,	"c_fnigncase" },
    { c_fstype,		"c_fstype" },
    { c_func,		"c_func" },
    { c_ge,		"c_ge" },
    { c_gid,		"c_gid" },
    { c_gt,		"c_gt" },
    { c_ino,		"c_ino" },
    { c_isblk,		"c_isblk" },
    { c_ischr,		"c_ischr" },
    { c_isdir,		"c_isdir" },
    { c_isfifo,		"c_isfifo" },
    { c_islnk,		"c_islnk" },
    { c_isreg,		"c_isreg" },
    { c_issock,		"c_issock" },
    { c_le,		"c_le" },
    { c_lshift,		"c_lshift" },
    { c_lt,		"c_lt" },
    { c_minus,		"c_minus" },
    { c_mod,		"c_mod" },
    { c_mode,		"c_mode" },
    { c_mtime,		"c_mtime" },
    { c_mul,		"c_mul" },
    { c_ne,		"c_ne" },
    { c_nlink,		"c_nlink" },
    { c_nogroup,	"c_nogroup" },
    { c_nop,		"c_nop" },
    { c_not,		"c_not" },
    { c_nouser,		"c_nouser" },
    { c_number,		"c_number" },
    { c_or,		"c_or" },
    { c_param,		"c_param" },
    { c_pathlen,	"c_pathlen" },
    { c_pattern,	"c_pattern" },
    { c_plus,		"c_plus" },
    { c_prune,		"c_prune" },
    { c_qm,		"c_qm" },
    { c_rdev,		"c_rdev" },
    { c_return,		"c_return" },
    { c_rshift,		"c_rshift" },
    { c_samefs,		"c_samefs" },
    { c_size,		"c_size" },
    { c_syscall,	"c_syscall" },
    { c_uid,		"c_uid" },
    { c_uniminus,	"c_uniminus" },
    { NUM_PARAMS,	"num_params" },
    { NULL_INSTR,	NULL }
};
 
/*
 * dump_instr:
 *	Dump an rh instruction in a human readable form.
 *
 *	format of dump:
 *	    Calling system function
 *		[pc] C_func_name system_func_name (num_params)
 *	    Calling compiled function
 *		[pc] C_func_name RH_func_name [new PC] (num_params)
 *	    Date, file system type, or file name pattern matching
 *		[pc] C_func_name "pattern"
 *	    Return
 *		[pc] c_return (num_params)
 *	    Parameter
 *		[pc] c_param (parameter_number)
 *	    Typical instruction
 *		[pc] C_func_name value
 *	    Unknown C function address (this should never happen...)
 *		[pc] hex_address value
 *	    End of an rh function or program
 *		[pc] END
 */

void dump_instr(int pc, const instruction *ip)
{
    const struct fmap *	mp;

    for (mp = funcinfo; mp->func != NULL_INSTR; mp++) {
	if (mp->func == ip->func) {
	    if (ip->func == c_syscall) {
		(void) fprintf(stderr, "[%05d] %s %s {%d}\n",
			       pc, mp->name, ip->value.sym->name,
			       ip->value.sym->num_params);
	    }
	    else if (ip->func == c_func) {
		(void) fprintf(stderr, "[%05d] %s %s [%05d] (%d)\n",
			       pc, mp->name, func_name(ip->value.pc),
			       ip->value.pc,
			       StackProgram[ip->value.pc].value.num_params);
	    }
	    else if (ip->func == c_date) {
		(void) fprintf(stderr, "[%05d] %s \"%.24s\"\n",
			       pc, mp->name,
			       asctime(localtime(&ip->value.di->seconds)));
	    }
	    else if (ip->func == c_fstype) {
		(void) fprintf(stderr, "[%05d] %s \"%s\"\n",
			       pc, mp->name, ip->value.fstype);
	    }
	    else if (ip->func == c_pattern
		     || ip->func == c_fncase
		     || ip->func == c_fnigncase) {
		(void) fprintf(stderr, "[%05d] %s \"%s\"\n",
			       pc, mp->name, ip->value.pattern);
	    }
	    else if (ip->func == c_samefs) {
		(void) fprintf(stderr, "[%05d] %s %04x %04x\n",
			       pc, mp->name,
			       (u_int) major(ip->value.dev),
			       (u_int) minor(ip->value.dev));
	    }
	    else if (ip->func == c_return) {
		(void) fprintf(stderr, "[%05d] %s (%d)\n",
			       pc, mp->name, ip->value.num_params);
	    }
	    else if (ip->func == c_param) {
		(void) fprintf(stderr, "[%05d] %s (%d)\n",
			       pc, mp->name, ip->value.param);
	    }
	    else {
		(void) fprintf(stderr, "[%05d] %s %ld\n",
			       pc, mp->name, ip->value.l);
	    }
	    break;
	}
    }

    if (mp->func == NULL_INSTR) {
	/* Something is wrong if this gets executed! */
	(void) fprintf(stderr, "[%05d] %p %ld\n", pc, ip->func, ip->value.l);
    }

    return;
}
 
/*
 * dump_program:
 *	Dump an rh compiled program in a human readable	form.
 *
 *	If 'flag' is true then rh function calls will be followed.
 */

int dump_program(int pc, int flag)
{
    const char *	fn;	/* compiled function name */
    const instruction *	ip;	/* pointer to current instruction */
    
    if (StackProgram[pc].func == END_OF_PROG) {
	return -1;
    }

    /* display function name if at beginning of compiled function */

    if ((fn = func_name(pc)) != NULL) {
	(void) fprintf(stderr, "  function %s\n", fn);
    }

    for (ip = &StackProgram[pc]; ip->func != NULL_INSTR; ip++, pc++) {
	dump_instr(pc, ip);
	if (flag && ip->func == c_func) {
	    if (activate(ip->value.pc)) {
		(void) dump_program(ip->value.pc, flag);
		deactivate(ip->value.pc);
	    }
	}
    }

    (void) fprintf(stderr, "[%05d] END\n", pc);

    return pc + 1;
}    

Added dump.h.































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * dump.h
 *
 * WRITTEN BY:	Rick Ohnemus	(rick@sterling.com)
 *
 * rcsid: $Id: dump.h,v 1.1 1994/01/12 19:34:18 rick Exp $
 */

#if !defined(__DUMP_H__)
#define __DUMP_H__

extern void	dump_instr(int pc, const instruction *ip);
extern int	dump_program(int pc, int flag);

#endif	/* __DUMP_H__ */

Added errmsg.c.











































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * errmsg.c
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 *
 * This file contains functions to write messages to stderr with
 * program information and expanded errno messages.
 *
 * Global Functions: error, warning
 *
 * Static Functions: percent_m, errmsg
 */

#if !defined(lint)
static char rcsid[] = "$Id: errmsg.c,v 1.2 1994/01/26 16:18:48 rick Exp $";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

const char *			Program_name;
#include <errno.h>

/*
 * percent_m:
 *	Return pointer to '%m' in a string or (char *) NULL if
 *	it is not found. '%' escaping is checked. This means
 *	that " %%m " does not contain '%m' since the '%' is
 *	escaped.
 */

static const char *percent_m(const char *str)
{
    while (*str != '\0') {
	if (*str == '%') {
	    if (*(str + 1) == 'm') {
		return str;
	    }
	    else if (*(str + 1) != '\0') {
		str++;
	    }
	}
	str++;
    }

    return NULL;
}
 
/*
 * errmsg:
 *	Write a message to stderr with the program name at the
 *	beginning of the message. If '%m' is found in the
 *	message then replace it with text describing the value
 *	in errno. Finally output a line feed at the end of the
 *	message if it does not end with a line feed.
 */

static void errmsg(const char *format, va_list args)
{
    const char *	m_ptr;
    char *		nfp;
    const char *	p;
    char		new_format[BUFSIZ * 2];
    int			save_errno = errno;


    (void) fprintf(stderr, "%s: ", Program_name);
    
    if ((m_ptr = percent_m(format)) != NULL) {
	(void) strncpy(new_format, format, m_ptr - format);
	nfp = new_format + (m_ptr - format);
	if (save_errno < sys_nerr) {
	    for (p = sys_errlist[save_errno]; *p != '\0'; *nfp++ = *p++) {
		;	/* empty loop */
	    }
	}
	else {
	    (void) sprintf(nfp, "Unknown error : %d", save_errno);
	    nfp += strlen(nfp);
	}
	(void) strcpy(nfp, m_ptr + 2);
	format = new_format;
    }
    
    (void) vfprintf(stderr, format, args);
    
    for (p = format; *p != '\0'; p++) {
	;		/* empty loop */
    }
    
    if (*(p - 1) != '\n') {
	(void) putc('\n', stderr);
    }
    
    (void) fflush(stderr);

    return;
}
 
/*
 * error:
 *	Calls errmsg() and exits.
 */

void error(const char *format, ...)
{
    va_list args;
    
    va_start(args, format);
    (void) fflush(stdout);
    errmsg(format, args);
    va_end(args);
    exit(1);
    /*NOTREACHED*/
}
 
/*
 * warning:
 *	Calls errmsg() and returns.
 */

void warning(const char *format, ...)
{
    va_list args;
    
    va_start(args, format);
    (void) fflush(stdout);
    errmsg(format, args);
    va_end(args);
    return;
}

Added errmsg.h.



































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * errmsg.h
 *
 * WRITTEN BY:	Rick Ohnemus	(rick@sterling.com)
 *
 * rcsid: $Id: errmsg.h,v 1.1 1994/01/12 19:35:00 rick Exp $
 */

#if !defined(__ERRMSG_H__)
#define __ERRMSG_H__

extern const char *Program_name;

extern void error(const char *format, ...);
extern void warning(const char *format, ...);

#endif	/* __ERRMSG_H__ */

Added getdate.c.







































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
#ifndef lint
static const char yysccsid[] = "@(#)yaccpar	1.9 (Berkeley) 02/21/93";
#endif

#include <stdlib.h>
#include <string.h>

#define YYBYACC 1
#define YYMAJOR 1
#define YYMINOR 9
#define YYPATCH 20070509

#define YYEMPTY (-1)
#define yyclearin    (yychar = YYEMPTY)
#define yyerrok      (yyerrflag = 0)
#define YYRECOVERING (yyerrflag != 0)

extern int yyparse(void);

static int yygrowstack(void);
#define yyparse gd_parse
#define yylex gd_lex
#define yyerror gd_error
#define yychar gd_char
#define yyval gd_val
#define yylval gd_lval
#define yydebug gd_debug
#define yynerrs gd_nerrs
#define yyerrflag gd_errflag
#define yyss gd_ss
#define yyssp gd_ssp
#define yyvs gd_vs
#define yyvsp gd_vsp
#define yylhs gd_lhs
#define yylen gd_len
#define yydefred gd_defred
#define yydgoto gd_dgoto
#define yysindex gd_sindex
#define yyrindex gd_rindex
#define yygindex gd_gindex
#define yytable gd_table
#define yycheck gd_check
#define yyname gd_name
#define yyrule gd_rule
#define YYPREFIX "gd_"
#line 3 "getdate.y"
	/* getdate.y						*/
	/*							*/
	/* 	Steven M. Bellovin (unc!smb)			*/
	/*	Dept. of Computer Science			*/
	/*	University of North Carolina at Chapel Hill	*/
	/*	@(#)getdate.y	2.13	9/16/86			*/

#if !defined(lint)
static char rcsid[] = "$Id: getdate.y,v 1.1 1994/01/12 19:35:54 rick Exp $";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include "getdate.h"

#define daysec (24L*60L*60L)

static int timeflag, zoneflag, dateflag, dayflag, relflag;
static time_t relsec, relmonth;
static int hh, mm, ss, merid, dylight;
static int dayord, dayreq;
static int month, day, year;
static int ourzone;

#define AM 1
#define PM 2
#define DAYLIGHT 1
#define STANDARD 2
#define MAYBE    3

static int gd_lex(), lookup();
static time_t timeconv(), daylcorr(), dateconv(), daylcorr();

#line 89 "y.tab.c"
#define ID 257
#define MONTH 258
#define DAY 259
#define MERIDIAN 260
#define NUMBER 261
#define UNIT 262
#define MUNIT 263
#define SUNIT 264
#define ZONE 265
#define DAYZONE 266
#define AGO 267
#define YYERRCODE 256
short gd_lhs[] = {                                        -1,
    0,    0,    1,    1,    1,    1,    1,    1,    7,    2,
    2,    2,    2,    2,    2,    2,    3,    3,    5,    5,
    5,    4,    4,    4,    4,    4,    4,    6,    6,    6,
    6,    6,    6,    6,
};
short gd_len[] = {                                         2,
    0,    2,    1,    1,    1,    1,    1,    1,    1,    2,
    3,    4,    4,    5,    6,    6,    1,    1,    1,    2,
    2,    3,    5,    2,    4,    2,    3,    2,    2,    2,
    1,    1,    1,    2,
};
short gd_defred[] = {                                      1,
    0,    0,    0,    0,   31,   32,   33,   17,   18,    2,
    3,    4,    5,    6,    0,    8,    0,   20,    0,   21,
   10,   28,   29,   30,    0,    0,   34,    0,   27,    0,
    0,   25,   12,   13,    0,    0,    0,   23,   15,   16,
};
short gd_dgoto[] = {                                       1,
   10,   11,   12,   13,   14,   15,   16,
};
short gd_sindex[] = {                                      0,
 -241, -256,  -38,  -47,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0, -260,    0,  -36,    0, -252,    0,
    0,    0,    0,    0, -249, -248,    0, -247,    0,  -56,
  -32,    0,    0,    0, -245, -235, -257,    0,    0,    0,
};
short gd_rindex[] = {                                      0,
    0,    0,    1,   61,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,   10,    0,   19,    0,   37,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,   46,
   28,    0,    0,    0,    0,    0,   55,    0,    0,    0,
};
short gd_gindex[] = {                                      0,
    0,    0,    0,    0,    0,    0,    0,
};
#define YYTABLESIZE 327
short gd_table[] = {                                      26,
   19,   35,   39,   40,   17,   18,   27,   28,   29,    7,
   25,   30,   31,   32,   36,   37,    2,    3,   24,    4,
    5,    6,    7,    8,    9,   38,    0,   22,    0,    0,
    0,    0,    0,    0,    0,    0,   26,    0,    0,    0,
    0,    0,    0,    0,    0,   11,    0,    0,    0,    0,
    0,    0,    0,    0,   14,    0,    0,    0,    0,    0,
    9,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,   33,   34,    0,    0,    0,    0,    0,
   19,   20,   21,    0,   22,   23,   24,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,   19,   19,
    0,   19,   19,   19,   19,   19,   19,    7,    7,    0,
    7,    7,    7,    7,    7,    7,   24,   24,    0,   24,
   24,   24,   24,   24,   24,   22,   22,    0,   22,   22,
   22,   22,   22,   22,   26,   26,    0,    0,   26,   26,
   26,   26,   26,   11,   11,    0,    0,   11,   11,   11,
   11,   11,   14,   14,    0,    0,   14,   14,   14,   14,
   14,    9,    0,    0,    0,    9,    9,
};
short gd_check[] = {                                      47,
    0,   58,  260,  261,  261,   44,  267,   44,  261,    0,
   58,  261,  261,  261,   47,  261,  258,  259,    0,  261,
  262,  263,  264,  265,  266,  261,   -1,    0,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,    0,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,    0,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,    0,   -1,   -1,   -1,   -1,   -1,
    0,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,  260,  261,   -1,   -1,   -1,   -1,   -1,
  258,  259,  260,   -1,  262,  263,  264,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  258,  259,
   -1,  261,  262,  263,  264,  265,  266,  258,  259,   -1,
  261,  262,  263,  264,  265,  266,  258,  259,   -1,  261,
  262,  263,  264,  265,  266,  258,  259,   -1,  261,  262,
  263,  264,  265,  266,  258,  259,   -1,   -1,  262,  263,
  264,  265,  266,  258,  259,   -1,   -1,  262,  263,  264,
  265,  266,  258,  259,   -1,   -1,  262,  263,  264,  265,
  266,  261,   -1,   -1,   -1,  265,  266,
};
#define YYFINAL 1
#ifndef YYDEBUG
#define YYDEBUG 1
#endif
#define YYMAXTOKEN 267
#if YYDEBUG
char *gd_name[] = {
"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,"','",0,0,"'/'",0,0,0,0,0,0,0,0,0,0,"':'",0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"ID","MONTH","DAY",
"MERIDIAN","NUMBER","UNIT","MUNIT","SUNIT","ZONE","DAYZONE","AGO",
};
char *gd_rule[] = {
"$accept : timedate",
"timedate :",
"timedate : timedate item",
"item : tspec",
"item : zone",
"item : dtspec",
"item : dyspec",
"item : rspec",
"item : nspec",
"nspec : NUMBER",
"tspec : NUMBER MERIDIAN",
"tspec : NUMBER ':' NUMBER",
"tspec : NUMBER ':' NUMBER MERIDIAN",
"tspec : NUMBER ':' NUMBER NUMBER",
"tspec : NUMBER ':' NUMBER ':' NUMBER",
"tspec : NUMBER ':' NUMBER ':' NUMBER MERIDIAN",
"tspec : NUMBER ':' NUMBER ':' NUMBER NUMBER",
"zone : ZONE",
"zone : DAYZONE",
"dyspec : DAY",
"dyspec : DAY ','",
"dyspec : NUMBER DAY",
"dtspec : NUMBER '/' NUMBER",
"dtspec : NUMBER '/' NUMBER '/' NUMBER",
"dtspec : MONTH NUMBER",
"dtspec : MONTH NUMBER ',' NUMBER",
"dtspec : NUMBER MONTH",
"dtspec : NUMBER MONTH NUMBER",
"rspec : NUMBER UNIT",
"rspec : NUMBER MUNIT",
"rspec : NUMBER SUNIT",
"rspec : UNIT",
"rspec : MUNIT",
"rspec : SUNIT",
"rspec : rspec AGO",
};
#endif
#ifndef YYSTYPE
typedef int YYSTYPE;
#endif
#if YYDEBUG
#include <stdio.h>
#endif

/* define the initial stack-sizes */
#ifdef YYSTACKSIZE
#undef YYMAXDEPTH
#define YYMAXDEPTH  YYSTACKSIZE
#else
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 500
#define YYMAXDEPTH  500
#endif
#endif

#define YYINITSTACKSIZE 500

int      yydebug;
int      yynerrs;
int      yyerrflag;
int      yychar;
short   *yyssp;
YYSTYPE *yyvsp;
YYSTYPE  yyval;
YYSTYPE  yylval;

/* variables for the parser stack */
static short   *yyss;
static short   *yysslim;
static YYSTYPE *yyvs;
static int      yystacksize;
#line 138 "getdate.y"

static int mdays[12] =
	{31, 0, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31};
#define epoch 1970

extern struct tm *localtime();

static time_t
dateconv(mm, dd, yy, h, m, s, mer, zone, dayflag)
int mm, dd, yy, h, m, s, mer, zone, dayflag;
{
	time_t tod, jdate;
	register int i;

	if (yy < 0) yy = -yy;
	if (yy < 100) yy += 1900;
	mdays[1] =
	    28 + ((yy % 4 == 0 && (yy % 100 != 0 || yy % 400 == 0)) ? 1 : 0);
	if (yy < epoch || yy > 1999 || mm < 1 || mm > 12 ||
		dd < 1 || dd > mdays[--mm]) return (-1);
	jdate = dd-1;
        for (i=0; i<mm; i++) jdate += mdays[i];
	for (i = epoch; i < yy; i++) {
	    jdate += 365 + ((i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) ? 1 : 0);
	}
	jdate *= daysec;
	jdate += zone * 60L;
	if ((tod = timeconv(h, m, s, mer)) < 0) return (-1);
	jdate += tod;
	if (dayflag == DAYLIGHT
	    || (dayflag == MAYBE && localtime(&jdate)->tm_isdst)) {
	    jdate += -1*60*60;
	}
	return (jdate);
}

static time_t
dayconv(ord, day, now)
int ord, day; time_t now;
{
	register struct tm *loctime;
	time_t tod;

	tod = now;
	loctime = localtime(&tod);
	tod += daysec * ((day - loctime->tm_wday + 7) % 7);
	tod += 7 * daysec * ((ord <= 0) ? ord : (ord - 1));
	return daylcorr(tod, now);
}

static time_t
timeconv(hh, mm, ss, mer)
register int hh, mm, ss, mer;
{
	if (mm < 0 || mm > 59 || ss < 0 || ss > 59) return (-1);
	switch (mer) {
		case AM: if (hh < 1 || hh > 12) return(-1);
			 return (60L * ((hh%12)*60L + mm)+ss);
		case PM: if (hh < 1 || hh > 12) return(-1);
			 return (60L * ((hh%12 +12)*60L + mm)+ss);
		case 24: if (hh < 0 || hh > 23) return (-1);
			 return (60L * (hh*60L + mm)+ss);
		default: return (-1);
	}
}

static time_t
monthadd(sdate, relmonth)
time_t sdate, relmonth;
{
	struct tm *ltime;
	int mm, yy;

	if (relmonth == 0) return 0;
	ltime = localtime(&sdate);
	mm = 12*ltime->tm_year + ltime->tm_mon + relmonth;
	yy = mm/12;
	mm = mm%12 + 1;
	return daylcorr(dateconv(mm, ltime->tm_mday, yy, ltime->tm_hour,
		ltime->tm_min, ltime->tm_sec, 24, ourzone, MAYBE), sdate);
}

static time_t
daylcorr(future, now)
time_t future, now;
{
	int fdayl, nowdayl;

	nowdayl = (localtime(&now)->tm_hour+1) % 24;
	fdayl = (localtime(&future)->tm_hour+1) % 24;
	return (future-now) + 60L * 60L * (nowdayl - fdayl);
}

static char *lptr;

static int gd_lex()
{
	extern int gd_lval;
	int sign;
	register char c;
	register char *p;
	char idbuf[20];
	int pcnt;

	for (;;) {
		while (isspace(*lptr)) lptr++;

		if (isdigit(c = *lptr) || c == '-' || c == '+') {
			if (c== '-' || c == '+') {
				if (c=='-') sign = -1;
				else sign = 1;
				if (!isdigit(*++lptr)) {
					/* gd_lval = sign; return (NUMBER); */
					return gd_lex();/* skip the '-' sign */
				}
			} else sign = 1;
			gd_lval = 0;
			while (isdigit(c = *lptr++)) gd_lval = 10*gd_lval + c - '0';
			gd_lval *= sign;
			lptr--;
			return (NUMBER);

		} else if (isalpha(c)) {
			p = idbuf;
			while (isalpha(c = *lptr++) || c=='.')
				if (p < &idbuf[sizeof(idbuf)-1])
					*p++ = c;
			*p = '\0';
			lptr--;
			return (lookup(idbuf));
		}

		else if (c == '(') {
			pcnt = 0;
			do {
				c = *lptr++;
				if (c == '\0') return(c);
				else if (c == '(') pcnt++;
				else if (c == ')') pcnt--;
			} while (pcnt > 0);
		}

		else return (*lptr++);
	}
}

struct table {
	char *name;
	int type, value;
};

static struct table mdtab[] = {
	{"January", MONTH, 1},
	{"February", MONTH, 2},
	{"March", MONTH, 3},
	{"April", MONTH, 4},
	{"May", MONTH, 5},
	{"June", MONTH, 6},
	{"July", MONTH, 7},
	{"August", MONTH, 8},
	{"September", MONTH, 9},
	{"Sept", MONTH, 9},
	{"October", MONTH, 10},
	{"November", MONTH, 11},
	{"December", MONTH, 12},

	{"Sunday", DAY, 0},
	{"Monday", DAY, 1},
	{"Tuesday", DAY, 2},
	{"Tues", DAY, 2},
	{"Wednesday", DAY, 3},
	{"Wednes", DAY, 3},
	{"Thursday", DAY, 4},
	{"Thur", DAY, 4},
	{"Thurs", DAY, 4},
	{"Friday", DAY, 5},
	{"Saturday", DAY, 6},
	{0, 0, 0}};

#define HRS *60
#define HALFHR 30
static struct table mztab[] = {
	{"a.m.", MERIDIAN, AM},
	{"am", MERIDIAN, AM},
	{"p.m.", MERIDIAN, PM},
	{"pm", MERIDIAN, PM},
	{"nst", ZONE, 3 HRS + HALFHR},		/* Newfoundland */
	{"n.s.t.", ZONE, 3 HRS + HALFHR},
	{"ast", ZONE, 4 HRS},		/* Atlantic */
	{"a.s.t.", ZONE, 4 HRS},
	{"adt", DAYZONE, 4 HRS},
	{"a.d.t.", DAYZONE, 4 HRS},
	{"est", ZONE, 5 HRS},		/* Eastern */
	{"e.s.t.", ZONE, 5 HRS},
	{"edt", DAYZONE, 5 HRS},
	{"e.d.t.", DAYZONE, 5 HRS},
	{"cst", ZONE, 6 HRS},		/* Central */
	{"c.s.t.", ZONE, 6 HRS},
	{"cdt", DAYZONE, 6 HRS},
	{"c.d.t.", DAYZONE, 6 HRS},
	{"mst", ZONE, 7 HRS},		/* Mountain */
	{"m.s.t.", ZONE, 7 HRS},
	{"mdt", DAYZONE, 7 HRS},
	{"m.d.t.", DAYZONE, 7 HRS},
	{"pst", ZONE, 8 HRS},		/* Pacific */
	{"p.s.t.", ZONE, 8 HRS},
	{"pdt", DAYZONE, 8 HRS},
	{"p.d.t.", DAYZONE, 8 HRS},
	{"yst", ZONE, 9 HRS},		/* Yukon */
	{"y.s.t.", ZONE, 9 HRS},
	{"ydt", DAYZONE, 9 HRS},
	{"y.d.t.", DAYZONE, 9 HRS},
	{"hst", ZONE, 10 HRS},		/* Hawaii */
	{"h.s.t.", ZONE, 10 HRS},
	{"hdt", DAYZONE, 10 HRS},
	{"h.d.t.", DAYZONE, 10 HRS},

	{"gmt", ZONE, 0 HRS},
	{"g.m.t.", ZONE, 0 HRS},
	{"bst", DAYZONE, 0 HRS},		/* British Summer Time */
	{"b.s.t.", DAYZONE, 0 HRS},
	{"eet", ZONE, 0 HRS},		/* European Eastern Time */
	{"e.e.t.", ZONE, 0 HRS},
	{"eest", DAYZONE, 0 HRS},	/* European Eastern Summer Time */
	{"e.e.s.t.", DAYZONE, 0 HRS},
	{"met", ZONE, -1 HRS},		/* Middle European Time */
	{"m.e.t.", ZONE, -1 HRS},
	{"mest", DAYZONE, -1 HRS},	/* Middle European Summer Time */
	{"m.e.s.t.", DAYZONE, -1 HRS},
	{"wet", ZONE, -2 HRS },		/* Western European Time */
	{"w.e.t.", ZONE, -2 HRS },
	{"west", DAYZONE, -2 HRS},	/* Western European Summer Time */
	{"w.e.s.t.", DAYZONE, -2 HRS},

	{"jst", ZONE, -9 HRS},		/* Japan Standard Time */
	{"j.s.t.", ZONE, -9 HRS},	/* Japan Standard Time */
					/* No daylight savings time */

	{"aest", ZONE, -10 HRS},	/* Australian Eastern Time */
	{"a.e.s.t.", ZONE, -10 HRS},
	{"aesst", DAYZONE, -10 HRS},	/* Australian Eastern Summer Time */
	{"a.e.s.s.t.", DAYZONE, -10 HRS},
	{"acst", ZONE, -(9 HRS + HALFHR)},	/* Australian Central Time */
	{"a.c.s.t.", ZONE, -(9 HRS + HALFHR)},
	{"acsst", DAYZONE, -(9 HRS + HALFHR)},	/* Australian Central Summer */
	{"a.c.s.s.t.", DAYZONE, -(9 HRS + HALFHR)},
	{"awst", ZONE, -8 HRS},		/* Australian Western Time */
	{"a.w.s.t.", ZONE, -8 HRS},	/* (no daylight time there, I'm told */
	{0, 0, 0}};

static struct table unittb[] = {
	{"year", MUNIT, 12},
	{"month", MUNIT, 1},
	{"fortnight", UNIT, 14*24*60},
	{"week", UNIT, 7*24*60},
	{"day", UNIT, 1*24*60},
	{"hour", UNIT, 60},
	{"minute", UNIT, 1},
	{"min", UNIT, 1},
	{"second", SUNIT, 1},
	{"sec", SUNIT, 1},
	{0, 0, 0}};

static struct table othertb[] = {
	{"tomorrow", UNIT, 1*24*60},
	{"yesterday", UNIT, -1*24*60},
	{"today", UNIT, 0},
	{"now", UNIT, 0},
	{"last", NUMBER, -1},
	{"this", UNIT, 0},
	{"next", NUMBER, 2},
	{"first", NUMBER, 1},
	/* {"second", NUMBER, 2}, */
	{"third", NUMBER, 3},
	{"fourth", NUMBER, 4},
	{"fifth", NUMBER, 5},
	{"sixth", NUMBER, 6},
	{"seventh", NUMBER, 7},
	{"eigth", NUMBER, 8},
	{"ninth", NUMBER, 9},
	{"tenth", NUMBER, 10},
	{"eleventh", NUMBER, 11},
	{"twelfth", NUMBER, 12},
	{"ago", AGO, 1},
	{0, 0, 0}};

static struct table milzone[] = {
	{"a", ZONE, 1 HRS},
	{"b", ZONE, 2 HRS},
	{"c", ZONE, 3 HRS},
	{"d", ZONE, 4 HRS},
	{"e", ZONE, 5 HRS},
	{"f", ZONE, 6 HRS},
	{"g", ZONE, 7 HRS},
	{"h", ZONE, 8 HRS},
	{"i", ZONE, 9 HRS},
	{"k", ZONE, 10 HRS},
	{"l", ZONE, 11 HRS},
	{"m", ZONE, 12 HRS},
	{"n", ZONE, -1 HRS},
	{"o", ZONE, -2 HRS},
	{"p", ZONE, -3 HRS},
	{"q", ZONE, -4 HRS},
	{"r", ZONE, -5 HRS},
	{"s", ZONE, -6 HRS},
	{"t", ZONE, -7 HRS},
	{"u", ZONE, -8 HRS},
	{"v", ZONE, -9 HRS},
	{"w", ZONE, -10 HRS},
	{"x", ZONE, -11 HRS},
	{"y", ZONE, -12 HRS},
	{"z", ZONE, 0 HRS},
	{0, 0, 0}};

static int lookup(id)
char *id;
{
#define gotit (gd_lval = i->value , i->type)
#define getid for(j = idvar, k = id; (*j++ = *k++) != '\0'; /* void */)

	char idvar[20];
	register char *j, *k;
	register struct table *i;
	int abbrev;

	getid;
	if (strlen(idvar) == 3) {
	    abbrev = 1;
	}
	else if (strlen(idvar) == 4 && idvar[3] == '.') {
		abbrev = 1;
		idvar[3] = '\0';
	}
	else abbrev = 0;

	if (islower(*idvar)) *idvar = toupper(*idvar);

	for (i = mdtab; i->name; i++) {
		k = idvar;
		for (j = i->name; *j++ == *k++;) {
			if (abbrev && j==i->name+3) return gotit;
			if (j[-1] == 0) return gotit;
		}
	}

	getid;
	for (i = mztab; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	for (j = idvar; *j; j++)
		if (isupper(*j)) *j = tolower(*j);
	for (i=mztab; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	getid;
	for (i=unittb; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	if (idvar[strlen(idvar)-1] == 's')
		idvar[strlen(idvar)-1] = '\0';
	for (i=unittb; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	getid;
	for (i = othertb; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	getid;
	if (strlen(idvar) == 1 && isalpha(*idvar)) {
		if (isupper(*idvar)) *idvar = tolower(*idvar);
		for (i = milzone; i->name; i++)
			if (strcmp(i->name, idvar) == 0) return gotit;
	}

	return(ID);
}

date_info getdate(p, now)
char *p;
struct timeb *now;
{
#define mcheck(f)	if (f>1) err++
    int gd_parse();
    time_t monthadd();
    int err;
    struct tm *lt;
    struct timeb ftz;
    date_info value;
    
    time_t sdate, tod;
    
    lptr = p;
    if (now == ((struct timeb *) NULL)) {
	now = &ftz;
	ftime(&ftz);
    }
    lt = localtime(&now->time);
    year = lt->tm_year;
    month = lt->tm_mon+1;
    day = lt->tm_mday;
    relsec = 0; relmonth = 0;
    timeflag = zoneflag = dateflag = dayflag = relflag = 0;
    ourzone = now->timezone;
    dylight = MAYBE;
    hh = mm = ss = 0;
    merid = 24;
    
    if (err = gd_parse()) {
	value.seconds = -1;
	return value;
    }
    
    mcheck(timeflag);
    mcheck(zoneflag);
    mcheck(dateflag);
    mcheck(dayflag);
    
    if (err) {
	value.seconds = -1;
	return value;
    }
    if (dateflag || timeflag || dayflag) {
	sdate = dateconv(month,day,year,hh,mm,ss,merid,ourzone,dylight);
	if (sdate < 0) {
	    value.seconds = -1;
	    return value;
	}
    }
    else {
	sdate = now->time;
	if (relflag == 0)
	    sdate -= (lt->tm_sec + lt->tm_min * 60
		      + lt->tm_hour * (60L * 60L));
    }
    
    sdate += relsec;
    sdate += monthadd(sdate, relmonth);
    
    if (dayflag && !dateflag) {
	tod = dayconv(dayord, dayreq, sdate);
	sdate += tod;
    }
    
#if defined(DEBUG2)
    (void) fprintf(stderr, "timeflag = %d, zoneflag = %d, dateflag = %d\n",
		   timeflag, zoneflag, dateflag);
    (void) fprintf(stderr, "dayflag = %d, relflag = %d\n",
		   dayflag, relflag);
    (void) fprintf(stderr, "seconds = %lu\n", sdate);
#endif
    
    value.seconds = sdate;
    return value;
}

/*ARGSUSED*/
void gd_error(char *s)
{
    return;
}
#line 760 "y.tab.c"
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
static int yygrowstack(void)
{
    int newsize, i;
    short *newss;
    YYSTYPE *newvs;

    if ((newsize = yystacksize) == 0)
        newsize = YYINITSTACKSIZE;
    else if (newsize >= YYMAXDEPTH)
        return -1;
    else if ((newsize *= 2) > YYMAXDEPTH)
        newsize = YYMAXDEPTH;

    i = yyssp - yyss;
    newss = (yyss != 0)
          ? (short *)realloc(yyss, newsize * sizeof(*newss))
          : (short *)malloc(newsize * sizeof(*newss));
    if (newss == 0)
        return -1;

    yyss  = newss;
    yyssp = newss + i;
    newvs = (yyvs != 0)
          ? (YYSTYPE *)realloc(yyvs, newsize * sizeof(*newvs))
          : (YYSTYPE *)malloc(newsize * sizeof(*newvs));
    if (newvs == 0)
        return -1;

    yyvs = newvs;
    yyvsp = newvs + i;
    yystacksize = newsize;
    yysslim = yyss + newsize - 1;
    return 0;
}

#define YYABORT goto yyabort
#define YYREJECT goto yyabort
#define YYACCEPT goto yyaccept
#define YYERROR goto yyerrlab
int
yyparse(void)
{
    register int yym, yyn, yystate;
#if YYDEBUG
    register const char *yys;

    if ((yys = getenv("YYDEBUG")) != 0)
    {
        yyn = *yys;
        if (yyn >= '0' && yyn <= '9')
            yydebug = yyn - '0';
    }
#endif

    yynerrs = 0;
    yyerrflag = 0;
    yychar = YYEMPTY;

    if (yyss == NULL && yygrowstack()) goto yyoverflow;
    yyssp = yyss;
    yyvsp = yyvs;
    *yyssp = yystate = 0;

yyloop:
    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
    if (yychar < 0)
    {
        if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, reading %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
    }
    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: state %d, shifting to state %d\n",
                    YYPREFIX, yystate, yytable[yyn]);
#endif
        if (yyssp >= yysslim && yygrowstack())
        {
            goto yyoverflow;
        }
        *++yyssp = yystate = yytable[yyn];
        *++yyvsp = yylval;
        yychar = YYEMPTY;
        if (yyerrflag > 0)  --yyerrflag;
        goto yyloop;
    }
    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
        yyn = yytable[yyn];
        goto yyreduce;
    }
    if (yyerrflag) goto yyinrecovery;

    yyerror("syntax error");

#ifdef lint
    goto yyerrlab;
#endif

yyerrlab:
    ++yynerrs;

yyinrecovery:
    if (yyerrflag < 3)
    {
        yyerrflag = 3;
        for (;;)
        {
            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: state %d, error recovery shifting\
 to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
#endif
                if (yyssp >= yysslim && yygrowstack())
                {
                    goto yyoverflow;
                }
                *++yyssp = yystate = yytable[yyn];
                *++yyvsp = yylval;
                goto yyloop;
            }
            else
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: error recovery discarding state %d\n",
                            YYPREFIX, *yyssp);
#endif
                if (yyssp <= yyss) goto yyabort;
                --yyssp;
                --yyvsp;
            }
        }
    }
    else
    {
        if (yychar == 0) goto yyabort;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
        yychar = YYEMPTY;
        goto yyloop;
    }

yyreduce:
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
                YYPREFIX, yystate, yyn, yyrule[yyn]);
#endif
    yym = yylen[yyn];
    if (yym)
        yyval = yyvsp[1-yym];
    else
        memset(&yyval, 0, sizeof yyval);
    switch (yyn)
    {
case 3:
#line 50 "getdate.y"

		{timeflag++;}
break;
case 4:
#line 52 "getdate.y"

		{zoneflag++;}
break;
case 5:
#line 54 "getdate.y"

		{dateflag++;}
break;
case 6:
#line 56 "getdate.y"

		{dayflag++;}
break;
case 7:
#line 58 "getdate.y"

		{relflag++;}
break;
case 9:
#line 62 "getdate.y"

		{if (timeflag && dateflag && !relflag) year = yyvsp[0];
		else {timeflag++;hh = yyvsp[0]/100;mm = yyvsp[0]%100;ss = 0;merid = 24;}}
break;
case 10:
#line 66 "getdate.y"

		{hh = yyvsp[-1]; mm = 0; ss = 0; merid = yyvsp[0];}
break;
case 11:
#line 68 "getdate.y"

		{hh = yyvsp[-2]; mm = yyvsp[0]; merid = 24;}
break;
case 12:
#line 70 "getdate.y"

		{hh = yyvsp[-3]; mm = yyvsp[-1]; merid = yyvsp[0];}
break;
case 13:
#line 72 "getdate.y"

		{hh = yyvsp[-3]; mm = yyvsp[-1]; merid = 24;
		dylight = STANDARD; ourzone = yyvsp[0]%100 + 60*yyvsp[0]/100;}
break;
case 14:
#line 75 "getdate.y"

		{hh = yyvsp[-4]; mm = yyvsp[-2]; ss = yyvsp[0]; merid = 24;}
break;
case 15:
#line 77 "getdate.y"

		{hh = yyvsp[-5]; mm = yyvsp[-3]; ss = yyvsp[-1]; merid = yyvsp[0];}
break;
case 16:
#line 79 "getdate.y"

		{hh = yyvsp[-5]; mm = yyvsp[-3]; ss = yyvsp[-1]; merid = 24;
		dylight = STANDARD; ourzone = yyvsp[0]%100 + 60*yyvsp[0]/100;}
break;
case 17:
#line 83 "getdate.y"

		{ourzone = yyvsp[0]; dylight = STANDARD;}
break;
case 18:
#line 85 "getdate.y"

		{ourzone = yyvsp[0]; dylight = DAYLIGHT;}
break;
case 19:
#line 88 "getdate.y"

		{dayord = 1; dayreq = yyvsp[0];}
break;
case 20:
#line 90 "getdate.y"

		{dayord = 1; dayreq = yyvsp[-1];}
break;
case 21:
#line 92 "getdate.y"

		{dayord = yyvsp[-1]; dayreq = yyvsp[0];}
break;
case 22:
#line 95 "getdate.y"

		{month = yyvsp[-2]; day = yyvsp[0];}
break;
case 23:
#line 97 "getdate.y"

		{
		    /*
		     * HACK ALERT!!!!
		     * The 1000 is a magic number to attempt to force
		     * use of 4 digit years if year/month/day can be
		     * parsed. This was only done for backwards
		     * compatibility in rh.
		     */
		    if (yyvsp[-4] > 1000) {
			year = yyvsp[-4]; month = yyvsp[-2]; day = yyvsp[0];
		    }
		    else {
			month = yyvsp[-4]; day = yyvsp[-2]; year = yyvsp[0];
		    }
		}
break;
case 24:
#line 113 "getdate.y"

		{month = yyvsp[-1]; day = yyvsp[0];}
break;
case 25:
#line 115 "getdate.y"

		{month = yyvsp[-3]; day = yyvsp[-2]; year = yyvsp[0];}
break;
case 26:
#line 117 "getdate.y"

		{month = yyvsp[0]; day = yyvsp[-1];}
break;
case 27:
#line 119 "getdate.y"

		{month = yyvsp[-1]; day = yyvsp[-2]; year = yyvsp[0];}
break;
case 28:
#line 123 "getdate.y"

		{relsec +=  60L * yyvsp[-1] * yyvsp[0];}
break;
case 29:
#line 125 "getdate.y"

		{relmonth += yyvsp[-1] * yyvsp[0];}
break;
case 30:
#line 127 "getdate.y"

		{relsec += yyvsp[-1];}
break;
case 31:
#line 129 "getdate.y"

		{relsec +=  60L * yyvsp[0];}
break;
case 32:
#line 131 "getdate.y"

		{relmonth += yyvsp[0];}
break;
case 33:
#line 133 "getdate.y"

		{relsec++;}
break;
case 34:
#line 135 "getdate.y"

		{relsec = -relsec; relmonth = -relmonth;}
break;
#line 1113 "y.tab.c"
    }
    yyssp -= yym;
    yystate = *yyssp;
    yyvsp -= yym;
    yym = yylhs[yyn];
    if (yystate == 0 && yym == 0)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: after reduction, shifting from state 0 to\
 state %d\n", YYPREFIX, YYFINAL);
#endif
        yystate = YYFINAL;
        *++yyssp = YYFINAL;
        *++yyvsp = yyval;
        if (yychar < 0)
        {
            if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
            if (yydebug)
            {
                yys = 0;
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
                if (!yys) yys = "illegal-symbol";
                printf("%sdebug: state %d, reading %d (%s)\n",
                        YYPREFIX, YYFINAL, yychar, yys);
            }
#endif
        }
        if (yychar == 0) goto yyaccept;
        goto yyloop;
    }
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
        yystate = yytable[yyn];
    else
        yystate = yydgoto[yym];
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: after reduction, shifting from state %d \
to state %d\n", YYPREFIX, *yyssp, yystate);
#endif
    if (yyssp >= yysslim && yygrowstack())
    {
        goto yyoverflow;
    }
    *++yyssp = yystate;
    *++yyvsp = yyval;
    goto yyloop;

yyoverflow:
    yyerror("yacc stack overflow");

yyabort:
    return (1);

yyaccept:
    return (0);
}

Added getdate.h.









































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * getdate.h
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 *
 * This file contains the structure returned by getdate().
 *
 * rcsid: $Id: getdate.h,v 1.1 1994/01/12 19:35:41 rick Exp $
 */

#if !defined(__GETDATE_H__)
#define __GETDATE_H__

typedef struct {
    time_t		seconds;

    time_t		date;
    time_t		time;
    time_t		zone;

    unsigned int	dateflag  : 1;
    unsigned int	timeflag  : 1;
    unsigned int	zoneflag  : 1;

    unsigned int	hourflag  : 1;
    unsigned int	minflag   : 1;
    unsigned int	secflag   : 1;
    unsigned int	monthflag : 1;
    unsigned int	yearflag  : 1;
    unsigned int	dayflag   : 1;

} date_info;

extern date_info getdate();

#endif	/* __GETDATE_H__ */

Added getdate.y.











































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
%token ID MONTH DAY MERIDIAN NUMBER UNIT MUNIT SUNIT ZONE DAYZONE AGO
%{
	/* getdate.y						*/
	/*							*/
	/* 	Steven M. Bellovin (unc!smb)			*/
	/*	Dept. of Computer Science			*/
	/*	University of North Carolina at Chapel Hill	*/
	/*	@(#)getdate.y	2.13	9/16/86			*/

#if !defined(lint)
static char rcsid[] = "$Id: getdate.y,v 1.1 1994/01/12 19:35:54 rick Exp $";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include "getdate.h"

#define daysec (24L*60L*60L)

static int timeflag, zoneflag, dateflag, dayflag, relflag;
static time_t relsec, relmonth;
static int hh, mm, ss, merid, dylight;
static int dayord, dayreq;
static int month, day, year;
static int ourzone;

#define AM 1
#define PM 2
#define DAYLIGHT 1
#define STANDARD 2
#define MAYBE    3

static int gd_lex(), lookup();
static time_t timeconv(), daylcorr(), dateconv(), daylcorr();

%}

%%
timedate: 		/* empty */
	| timedate item;

item:	tspec =
		{timeflag++;}
	| zone =
		{zoneflag++;}
	| dtspec =
		{dateflag++;}
	| dyspec =
		{dayflag++;}
	| rspec =
		{relflag++;}
	| nspec;

nspec:	NUMBER =
		{if (timeflag && dateflag && !relflag) year = $1;
		else {timeflag++;hh = $1/100;mm = $1%100;ss = 0;merid = 24;}};

tspec:	NUMBER MERIDIAN =
		{hh = $1; mm = 0; ss = 0; merid = $2;}
	| NUMBER ':' NUMBER =
		{hh = $1; mm = $3; merid = 24;}
	| NUMBER ':' NUMBER MERIDIAN =
		{hh = $1; mm = $3; merid = $4;}
	| NUMBER ':' NUMBER NUMBER =
		{hh = $1; mm = $3; merid = 24;
		dylight = STANDARD; ourzone = $4%100 + 60*$4/100;}
	| NUMBER ':' NUMBER ':' NUMBER =
		{hh = $1; mm = $3; ss = $5; merid = 24;}
	| NUMBER ':' NUMBER ':' NUMBER MERIDIAN =
		{hh = $1; mm = $3; ss = $5; merid = $6;}
	| NUMBER ':' NUMBER ':' NUMBER NUMBER =
		{hh = $1; mm = $3; ss = $5; merid = 24;
		dylight = STANDARD; ourzone = $6%100 + 60*$6/100;};

zone:	ZONE =
		{ourzone = $1; dylight = STANDARD;}
	| DAYZONE =
		{ourzone = $1; dylight = DAYLIGHT;};

dyspec:	DAY =
		{dayord = 1; dayreq = $1;}
	| DAY ',' =
		{dayord = 1; dayreq = $1;}
	| NUMBER DAY =
		{dayord = $1; dayreq = $2;};

dtspec:	NUMBER '/' NUMBER =
		{month = $1; day = $3;}
	| NUMBER '/' NUMBER '/' NUMBER =
		{
		    /*
		     * HACK ALERT!!!!
		     * The 1000 is a magic number to attempt to force
		     * use of 4 digit years if year/month/day can be
		     * parsed. This was only done for backwards
		     * compatibility in rh.
		     */
		    if ($1 > 1000) {
			year = $1; month = $3; day = $5;
		    }
		    else {
			month = $1; day = $3; year = $5;
		    }
		}
	| MONTH NUMBER =
		{month = $1; day = $2;}
	| MONTH NUMBER ',' NUMBER =
		{month = $1; day = $2; year = $4;}
	| NUMBER MONTH =
		{month = $2; day = $1;}
	| NUMBER MONTH NUMBER =
		{month = $2; day = $1; year = $3;};


rspec:	NUMBER UNIT =
		{relsec +=  60L * $1 * $2;}
	| NUMBER MUNIT =
		{relmonth += $1 * $2;}
	| NUMBER SUNIT =
		{relsec += $1;}
	| UNIT =
		{relsec +=  60L * $1;}
	| MUNIT =
		{relmonth += $1;}
	| SUNIT =
		{relsec++;}
	| rspec AGO =
		{relsec = -relsec; relmonth = -relmonth;};
%%

static int mdays[12] =
	{31, 0, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31};
#define epoch 1970

extern struct tm *localtime();

static time_t
dateconv(mm, dd, yy, h, m, s, mer, zone, dayflag)
int mm, dd, yy, h, m, s, mer, zone, dayflag;
{
	time_t tod, jdate;
	register int i;

	if (yy < 0) yy = -yy;
	if (yy < 100) yy += 1900;
	mdays[1] =
	    28 + ((yy % 4 == 0 && (yy % 100 != 0 || yy % 400 == 0)) ? 1 : 0);
	if (yy < epoch || yy > 1999 || mm < 1 || mm > 12 ||
		dd < 1 || dd > mdays[--mm]) return (-1);
	jdate = dd-1;
        for (i=0; i<mm; i++) jdate += mdays[i];
	for (i = epoch; i < yy; i++) {
	    jdate += 365 + ((i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) ? 1 : 0);
	}
	jdate *= daysec;
	jdate += zone * 60L;
	if ((tod = timeconv(h, m, s, mer)) < 0) return (-1);
	jdate += tod;
	if (dayflag == DAYLIGHT
	    || (dayflag == MAYBE && localtime(&jdate)->tm_isdst)) {
	    jdate += -1*60*60;
	}
	return (jdate);
}

static time_t
dayconv(ord, day, now)
int ord, day; time_t now;
{
	register struct tm *loctime;
	time_t tod;

	tod = now;
	loctime = localtime(&tod);
	tod += daysec * ((day - loctime->tm_wday + 7) % 7);
	tod += 7 * daysec * ((ord <= 0) ? ord : (ord - 1));
	return daylcorr(tod, now);
}

static time_t
timeconv(hh, mm, ss, mer)
register int hh, mm, ss, mer;
{
	if (mm < 0 || mm > 59 || ss < 0 || ss > 59) return (-1);
	switch (mer) {
		case AM: if (hh < 1 || hh > 12) return(-1);
			 return (60L * ((hh%12)*60L + mm)+ss);
		case PM: if (hh < 1 || hh > 12) return(-1);
			 return (60L * ((hh%12 +12)*60L + mm)+ss);
		case 24: if (hh < 0 || hh > 23) return (-1);
			 return (60L * (hh*60L + mm)+ss);
		default: return (-1);
	}
}

static time_t
monthadd(sdate, relmonth)
time_t sdate, relmonth;
{
	struct tm *ltime;
	int mm, yy;

	if (relmonth == 0) return 0;
	ltime = localtime(&sdate);
	mm = 12*ltime->tm_year + ltime->tm_mon + relmonth;
	yy = mm/12;
	mm = mm%12 + 1;
	return daylcorr(dateconv(mm, ltime->tm_mday, yy, ltime->tm_hour,
		ltime->tm_min, ltime->tm_sec, 24, ourzone, MAYBE), sdate);
}

static time_t
daylcorr(future, now)
time_t future, now;
{
	int fdayl, nowdayl;

	nowdayl = (localtime(&now)->tm_hour+1) % 24;
	fdayl = (localtime(&future)->tm_hour+1) % 24;
	return (future-now) + 60L * 60L * (nowdayl - fdayl);
}

static char *lptr;

static int gd_lex()
{
	extern int gd_lval;
	int sign;
	register char c;
	register char *p;
	char idbuf[20];
	int pcnt;

	for (;;) {
		while (isspace(*lptr)) lptr++;

		if (isdigit(c = *lptr) || c == '-' || c == '+') {
			if (c== '-' || c == '+') {
				if (c=='-') sign = -1;
				else sign = 1;
				if (!isdigit(*++lptr)) {
					/* gd_lval = sign; return (NUMBER); */
					return gd_lex();/* skip the '-' sign */
				}
			} else sign = 1;
			gd_lval = 0;
			while (isdigit(c = *lptr++)) gd_lval = 10*gd_lval + c - '0';
			gd_lval *= sign;
			lptr--;
			return (NUMBER);

		} else if (isalpha(c)) {
			p = idbuf;
			while (isalpha(c = *lptr++) || c=='.')
				if (p < &idbuf[sizeof(idbuf)-1])
					*p++ = c;
			*p = '\0';
			lptr--;
			return (lookup(idbuf));
		}

		else if (c == '(') {
			pcnt = 0;
			do {
				c = *lptr++;
				if (c == '\0') return(c);
				else if (c == '(') pcnt++;
				else if (c == ')') pcnt--;
			} while (pcnt > 0);
		}

		else return (*lptr++);
	}
}

struct table {
	char *name;
	int type, value;
};

static struct table mdtab[] = {
	{"January", MONTH, 1},
	{"February", MONTH, 2},
	{"March", MONTH, 3},
	{"April", MONTH, 4},
	{"May", MONTH, 5},
	{"June", MONTH, 6},
	{"July", MONTH, 7},
	{"August", MONTH, 8},
	{"September", MONTH, 9},
	{"Sept", MONTH, 9},
	{"October", MONTH, 10},
	{"November", MONTH, 11},
	{"December", MONTH, 12},

	{"Sunday", DAY, 0},
	{"Monday", DAY, 1},
	{"Tuesday", DAY, 2},
	{"Tues", DAY, 2},
	{"Wednesday", DAY, 3},
	{"Wednes", DAY, 3},
	{"Thursday", DAY, 4},
	{"Thur", DAY, 4},
	{"Thurs", DAY, 4},
	{"Friday", DAY, 5},
	{"Saturday", DAY, 6},
	{0, 0, 0}};

#define HRS *60
#define HALFHR 30
static struct table mztab[] = {
	{"a.m.", MERIDIAN, AM},
	{"am", MERIDIAN, AM},
	{"p.m.", MERIDIAN, PM},
	{"pm", MERIDIAN, PM},
	{"nst", ZONE, 3 HRS + HALFHR},		/* Newfoundland */
	{"n.s.t.", ZONE, 3 HRS + HALFHR},
	{"ast", ZONE, 4 HRS},		/* Atlantic */
	{"a.s.t.", ZONE, 4 HRS},
	{"adt", DAYZONE, 4 HRS},
	{"a.d.t.", DAYZONE, 4 HRS},
	{"est", ZONE, 5 HRS},		/* Eastern */
	{"e.s.t.", ZONE, 5 HRS},
	{"edt", DAYZONE, 5 HRS},
	{"e.d.t.", DAYZONE, 5 HRS},
	{"cst", ZONE, 6 HRS},		/* Central */
	{"c.s.t.", ZONE, 6 HRS},
	{"cdt", DAYZONE, 6 HRS},
	{"c.d.t.", DAYZONE, 6 HRS},
	{"mst", ZONE, 7 HRS},		/* Mountain */
	{"m.s.t.", ZONE, 7 HRS},
	{"mdt", DAYZONE, 7 HRS},
	{"m.d.t.", DAYZONE, 7 HRS},
	{"pst", ZONE, 8 HRS},		/* Pacific */
	{"p.s.t.", ZONE, 8 HRS},
	{"pdt", DAYZONE, 8 HRS},
	{"p.d.t.", DAYZONE, 8 HRS},
	{"yst", ZONE, 9 HRS},		/* Yukon */
	{"y.s.t.", ZONE, 9 HRS},
	{"ydt", DAYZONE, 9 HRS},
	{"y.d.t.", DAYZONE, 9 HRS},
	{"hst", ZONE, 10 HRS},		/* Hawaii */
	{"h.s.t.", ZONE, 10 HRS},
	{"hdt", DAYZONE, 10 HRS},
	{"h.d.t.", DAYZONE, 10 HRS},

	{"gmt", ZONE, 0 HRS},
	{"g.m.t.", ZONE, 0 HRS},
	{"bst", DAYZONE, 0 HRS},		/* British Summer Time */
	{"b.s.t.", DAYZONE, 0 HRS},
	{"eet", ZONE, 0 HRS},		/* European Eastern Time */
	{"e.e.t.", ZONE, 0 HRS},
	{"eest", DAYZONE, 0 HRS},	/* European Eastern Summer Time */
	{"e.e.s.t.", DAYZONE, 0 HRS},
	{"met", ZONE, -1 HRS},		/* Middle European Time */
	{"m.e.t.", ZONE, -1 HRS},
	{"mest", DAYZONE, -1 HRS},	/* Middle European Summer Time */
	{"m.e.s.t.", DAYZONE, -1 HRS},
	{"wet", ZONE, -2 HRS },		/* Western European Time */
	{"w.e.t.", ZONE, -2 HRS },
	{"west", DAYZONE, -2 HRS},	/* Western European Summer Time */
	{"w.e.s.t.", DAYZONE, -2 HRS},

	{"jst", ZONE, -9 HRS},		/* Japan Standard Time */
	{"j.s.t.", ZONE, -9 HRS},	/* Japan Standard Time */
					/* No daylight savings time */

	{"aest", ZONE, -10 HRS},	/* Australian Eastern Time */
	{"a.e.s.t.", ZONE, -10 HRS},
	{"aesst", DAYZONE, -10 HRS},	/* Australian Eastern Summer Time */
	{"a.e.s.s.t.", DAYZONE, -10 HRS},
	{"acst", ZONE, -(9 HRS + HALFHR)},	/* Australian Central Time */
	{"a.c.s.t.", ZONE, -(9 HRS + HALFHR)},
	{"acsst", DAYZONE, -(9 HRS + HALFHR)},	/* Australian Central Summer */
	{"a.c.s.s.t.", DAYZONE, -(9 HRS + HALFHR)},
	{"awst", ZONE, -8 HRS},		/* Australian Western Time */
	{"a.w.s.t.", ZONE, -8 HRS},	/* (no daylight time there, I'm told */
	{0, 0, 0}};

static struct table unittb[] = {
	{"year", MUNIT, 12},
	{"month", MUNIT, 1},
	{"fortnight", UNIT, 14*24*60},
	{"week", UNIT, 7*24*60},
	{"day", UNIT, 1*24*60},
	{"hour", UNIT, 60},
	{"minute", UNIT, 1},
	{"min", UNIT, 1},
	{"second", SUNIT, 1},
	{"sec", SUNIT, 1},
	{0, 0, 0}};

static struct table othertb[] = {
	{"tomorrow", UNIT, 1*24*60},
	{"yesterday", UNIT, -1*24*60},
	{"today", UNIT, 0},
	{"now", UNIT, 0},
	{"last", NUMBER, -1},
	{"this", UNIT, 0},
	{"next", NUMBER, 2},
	{"first", NUMBER, 1},
	/* {"second", NUMBER, 2}, */
	{"third", NUMBER, 3},
	{"fourth", NUMBER, 4},
	{"fifth", NUMBER, 5},
	{"sixth", NUMBER, 6},
	{"seventh", NUMBER, 7},
	{"eigth", NUMBER, 8},
	{"ninth", NUMBER, 9},
	{"tenth", NUMBER, 10},
	{"eleventh", NUMBER, 11},
	{"twelfth", NUMBER, 12},
	{"ago", AGO, 1},
	{0, 0, 0}};

static struct table milzone[] = {
	{"a", ZONE, 1 HRS},
	{"b", ZONE, 2 HRS},
	{"c", ZONE, 3 HRS},
	{"d", ZONE, 4 HRS},
	{"e", ZONE, 5 HRS},
	{"f", ZONE, 6 HRS},
	{"g", ZONE, 7 HRS},
	{"h", ZONE, 8 HRS},
	{"i", ZONE, 9 HRS},
	{"k", ZONE, 10 HRS},
	{"l", ZONE, 11 HRS},
	{"m", ZONE, 12 HRS},
	{"n", ZONE, -1 HRS},
	{"o", ZONE, -2 HRS},
	{"p", ZONE, -3 HRS},
	{"q", ZONE, -4 HRS},
	{"r", ZONE, -5 HRS},
	{"s", ZONE, -6 HRS},
	{"t", ZONE, -7 HRS},
	{"u", ZONE, -8 HRS},
	{"v", ZONE, -9 HRS},
	{"w", ZONE, -10 HRS},
	{"x", ZONE, -11 HRS},
	{"y", ZONE, -12 HRS},
	{"z", ZONE, 0 HRS},
	{0, 0, 0}};

static int lookup(id)
char *id;
{
#define gotit (gd_lval = i->value , i->type)
#define getid for(j = idvar, k = id; (*j++ = *k++) != '\0'; /* void */)

	char idvar[20];
	register char *j, *k;
	register struct table *i;
	int abbrev;

	getid;
	if (strlen(idvar) == 3) {
	    abbrev = 1;
	}
	else if (strlen(idvar) == 4 && idvar[3] == '.') {
		abbrev = 1;
		idvar[3] = '\0';
	}
	else abbrev = 0;

	if (islower(*idvar)) *idvar = toupper(*idvar);

	for (i = mdtab; i->name; i++) {
		k = idvar;
		for (j = i->name; *j++ == *k++;) {
			if (abbrev && j==i->name+3) return gotit;
			if (j[-1] == 0) return gotit;
		}
	}

	getid;
	for (i = mztab; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	for (j = idvar; *j; j++)
		if (isupper(*j)) *j = tolower(*j);
	for (i=mztab; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	getid;
	for (i=unittb; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	if (idvar[strlen(idvar)-1] == 's')
		idvar[strlen(idvar)-1] = '\0';
	for (i=unittb; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	getid;
	for (i = othertb; i->name; i++)
		if (strcmp(i->name, idvar) == 0) return gotit;

	getid;
	if (strlen(idvar) == 1 && isalpha(*idvar)) {
		if (isupper(*idvar)) *idvar = tolower(*idvar);
		for (i = milzone; i->name; i++)
			if (strcmp(i->name, idvar) == 0) return gotit;
	}

	return(ID);
}

date_info getdate(p, now)
char *p;
struct timeb *now;
{
#define mcheck(f)	if (f>1) err++
    int gd_parse();
    time_t monthadd();
    int err;
    struct tm *lt;
    struct timeb ftz;
    date_info value;
    
    time_t sdate, tod;
    
    lptr = p;
    if (now == ((struct timeb *) NULL)) {
	now = &ftz;
	ftime(&ftz);
    }
    lt = localtime(&now->time);
    year = lt->tm_year;
    month = lt->tm_mon+1;
    day = lt->tm_mday;
    relsec = 0; relmonth = 0;
    timeflag = zoneflag = dateflag = dayflag = relflag = 0;
    ourzone = now->timezone;
    dylight = MAYBE;
    hh = mm = ss = 0;
    merid = 24;
    
    if (err = gd_parse()) {
	value.seconds = -1;
	return value;
    }
    
    mcheck(timeflag);
    mcheck(zoneflag);
    mcheck(dateflag);
    mcheck(dayflag);
    
    if (err) {
	value.seconds = -1;
	return value;
    }
    if (dateflag || timeflag || dayflag) {
	sdate = dateconv(month,day,year,hh,mm,ss,merid,ourzone,dylight);
	if (sdate < 0) {
	    value.seconds = -1;
	    return value;
	}
    }
    else {
	sdate = now->time;
	if (relflag == 0)
	    sdate -= (lt->tm_sec + lt->tm_min * 60
		      + lt->tm_hour * (60L * 60L));
    }
    
    sdate += relsec;
    sdate += monthadd(sdate, relmonth);
    
    if (dayflag && !dateflag) {
	tod = dayconv(dayord, dayreq, sdate);
	sdate += tod;
    }
    
#if defined(DEBUG2)
    (void) fprintf(stderr, "timeflag = %d, zoneflag = %d, dateflag = %d\n",
		   timeflag, zoneflag, dateflag);
    (void) fprintf(stderr, "dayflag = %d, relflag = %d\n",
		   dayflag, relflag);
    (void) fprintf(stderr, "seconds = %lu\n", sdate);
#endif
    
    value.seconds = sdate;
    return value;
}

/*ARGSUSED*/
void gd_error(char *s)
{
    return;
}

Added makedep.







































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/local/bin/perl --
#
# rcsid: $Id: makedep,v 1.1 1994/01/12 19:36:03 rick Exp $
#
#*********************************************************************#
#                                                                      
#  PERL SCRIPT:  makedep
#  AUTHOR:       Rick Ohnemus
#  ABSTRACT:
#
# This script generates the make dependencies for normal files and
# library files and writes them to the file specified on the command line.
#
# Options:
#	-a		output $(ARCH) in dependencies and compile lines
#	-c		output compile lines
#	-e exclusion	pattern to exclude from dependency list (may be
#			specified multiple times)
#	-l library	library name if doing dependencies for a library
#	-m makefile	makefile to modify
#	-o pp_options	preprocessor options
#	-p preprocessor	preprocessor to use instead of builtin
#	-r		replace entire contents of makefile
#	-s		separate dependencies from rest of file with
#			"### DO NOT DELETE THIS LINE - make depend needs it"
#	-t string	pattern to strip from object names (may be specified
#			multiple times)
#
# Other arguments (in order of command line use):
#	make variables and values
#		make macros to substitute in generated dependencies
#		variable=value
#	files to generate dependencies for
#
#*********************************************************************#
#

# Set default preprocessor.

$Preprocessor = "gcc -E";
#$Preprocessor = "cc -E";
#$Preprocessor = "/lib/cpp";

$Usage = "Usage: $0 [-acrs] [-e exclusion] [-t strip_string] [-l library] [-m makefile] [-o preprocessor_options] [-p preprocessor] [variable=value ...] file [...]\n";

# get switches and values

die $Usage unless &Getopts('ace&t&l:m:o:p:rs');

$Arch = defined($opt_a) ? 1 : 0;	# use $(ARCH) macro in output
$Cc = defined($opt_c) ? 1 : 0;		# output CC lines

# things to exclude from dependencies
if (defined(@opt_e)) {
    if ($#opt_e == $[) {
	$Exclude = "($opt_e[$[])";
    }
    else {
	for ($i = $[; $i <= $#opt_e; ++$i) {
	    $Exclude .= "($opt_e[$i])|";
	}
	chop($Exclude);		# remove trailing '|'
    }
}

# things to strip from object names
if (defined(@opt_t)) {
    if ($#opt_t == $[) {
	$Strips = "($opt_t[$[])";
    }
    else {
	for ($i = $[; $i <= $#opt_t; ++$i) {
	    $Strips .= "($opt_t[$i])|";
	}
	chop($Strips);		# remove trailing '|'
    }
}

$Library = $opt_l if (defined($opt_l));	# do library depenedencies
$Makefile = defined($opt_m) ? $opt_m : 'Makefile';	# makefile to modify
$PPOptions = defined($opt_o) ? $opt_o : '';	# preprocessor options
$Preprocessor = $opt_p if (defined($opt_p));	# preprocessor
$Replace = defined($opt_r) ? 1 : 0;	# replace contents of makefile
$Separate = defined($opt_s) ? 1 : 0;	# output separation line

undef($opt_a);
undef($opt_c);
undef(@opt_e);
undef($opt_l);
undef($opt_m);
undef($opt_o);
undef($opt_p);
undef($opt_r);
undef($opt_s);
undef(@opt_t);

# Is the makefile readable?
die "$0: $Makefile: $!\n" if (! -r $Makefile);

# Check for invalid preprocessor options
@PpOpts = split(' ', $PPOptions);
@PpOpts = grep(!/^-[BCDIMPRU]/o, @PpOpts);
die "$0: invalid preprocessor options: @PpOpts\n" if ($#PpOpts >= 0);
undef(@PpOpts);

# Make associative arrays for all make variables from the command line
# and anything else (hopefully source files)

%Vmap = ();
for (@ARGV) {
    if (/(\w.*)=(.+)/o) {
	$Vmap{$2} = $1;
    }
    else {
	push(@Sources, $_);
    }
}

# Sort the macros in reverse length order (longest first)
@Vkeys = sort { length($b) <=> length($a) } keys(%Vmap);

die "$Usage" unless $#Sources >= 0;	# need at least 1 source file

# are all of the source files readable?
$badfiles = 0;
for (@Sources) {
    if (! -r $_) {
	warn "$0: $_: $!\n";
	++$badfiles;
    }
}
exit(1) if $badfiles;	# exit if 1 or more source files is not readable

# Create temporary output file name and set signal handlers to remove
# the temporary file.

$TempFile = "$Makefile.new.$$";

$SIG{'INT'} = 'cleanup';
$SIG{'QUIT'} = 'cleanup';
$SIG{'HUP'} = 'cleanup';

# open the output file

open(OUTPUT, "> $TempFile") || die "can't open temp file - $!\n";
select(OUTPUT);

# copy the contents of the makefile if replacement wasn't specified

if (!$Replace) {
    open(INPUT, "< $Makefile") || die "can't open $Makefile - $!\n";

    # If separation was requested then copy the make file until the
    # separation line or end of file is found. If separation was not
    # requested then just copy the contents of the makefile.

    if ($Separate) {
	while (<INPUT>) {
	    /^### DO NOT DELETE THIS LINE/o && last;
	    print;
	}
	print "### DO NOT DELETE THIS LINE - make depend needs it\n\n";
    }
    else {
	while (<INPUT>) {
	    print;
	}
    }

    # don't need the original makefile any more
    close(INPUT) || die "can't close $Makefile -$!\n";
}

# Output the dependencies for the library itself if doing a library.

if (defined($Library)) {
    local($Temp);

    for (@Sources) {
	print "$Library: $_\n";
    }
    for (@Sources) {
	$Temp = $_;
	$Temp =~ s/\.c/.o/o;
	print "$Library: $Library($Temp)\n";
    }
    print "\tranlib $Library\n\t-chmod 664 $Library\n\n";
}

# output the dependencies for each source file

$badfiles = 0;
foreach $src (@Sources) {

    # get dependencies
    open(CPP, "$Preprocessor $PPOptions $src|")
	|| die "'$Preprocessor' for $src failed: $!\n";

    %incfiles = ();
    while (<CPP>) {
	$incfiles{$filename}++ if (($filename) = /^# +\d+ +"(.*)"/o);
    }
    close(CPP);

    @dependencies = sort keys(%incfiles);

    # do make variable substitution
    if ($#Vkeys >= 0) {
	for (@dependencies) {
	    foreach $key (@Vkeys) {
		s/$key/\$($Vmap{$key})/;
	    }
	}
    }

    if (defined($Exclude)) {
	@dependencies = grep(!/$Exclude/, @dependencies);
    }

    if ($#dependencies < 0) {
	print STDERR "$0: no dependencies for $src\n";
	++$badfiles;
	next;
    }

    $Object = $src;
    $Object =~ s/\.c/\.o/o;

    if (defined($Strips)) {
	$Object =~ s/$Strips//g;
    }

    # print dependencies and compiler line to makefile
    if (defined($Library)) {
	for (@dependencies) {
	    print "$Library($Object): $_\n";
	}
	if ($Cc) {
	    print "\t\$(CC) \$(CFLAGS) -c $src";
	    if ($Arch) {
		print " -o \$(ARCH)/$Object";
	    }
	    print "\n\tar rv $Library $Object\n";
	    print "\tranlib $Library\n";
	}
    }
    else {
	if ($Arch) {
	    for (@dependencies) {
		print '$(ARCH)/', "$Object: $_\n";
	    }
	    $Cc && print "\t" , '$(CC) $(CFLAGS) -c ' , "$src", ' -o $@', "\n";
	}
	else {
	    for (@dependencies) {
		print "$Object: $_\n";
	    }
	    $Cc && print "\t" , '$(CC) $(CFLAGS) -c ' , "$src\n";
	}
    }
    print "\n";
}

if ($badfiles) {
    unlink $TempFile;
    exit 1;
}

rename($Makefile, "$Makefile.BAK")
    || die "can't rename $Makefile to $Makefile.BAK - $!\n";

rename("$TempFile", $Makefile)
    || die "can't rename $TempFile to $Makefile - $!\n";

exit 0;


sub Getopts {
    local($argumentative) = @_;
    local(@args,$_,$first,$rest);
    local($errs) = 0;
    local($[) = 0;

    @args = split( / */o, $argumentative );

    while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/o) {
	($first,$rest) = ($1,$2);
	$pos = index($argumentative,$first);
	if($pos >= $[) {
	    if($args[$pos+1] eq ':') {
		shift(@ARGV);
		if($rest eq '') {
		    ++$errs unless @ARGV;
		    $rest = shift(@ARGV);
		}
		eval "\$opt_$first = \$rest;";
	    }
	    elsif($args[$pos+1] eq '&') {
		shift(@ARGV);
		if($rest eq '') {
		    ++$errs unless @ARGV;
		    $rest = shift(@ARGV);
		}
		eval "push(\@opt_$first, \$rest);";
	    }
	    else {
		eval "\$opt_$first = 1";
		if($rest eq '') {
		    shift(@ARGV);
		}
		else {
		    $ARGV[0] = "-$rest";
		}
	    }
	}
	else {
	    print STDERR "Unknown option: $first\n";
	    ++$errs;
	    if($rest ne '') {
		$ARGV[0] = "-$rest";
	    }
	    else {
		shift(@ARGV);
	    }
	}
    }
    $errs == 0;
}


sub cleanup {
    unlink $TempFile;
    exit 0;
}


# Local Variables:
# mode: perl
# End:

Added resname.c.





































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * resname.c
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 *
 * This file contains a function that finds a file given a colon
 * separated list of directories to search and a file name.
 * The resolved name is returned in allocated memory.
 *
 * Global Functions: resolve_name
 */

#if !defined(lint)
static char rcsid[] = "$Id: resname.c,v 1.2 1994/01/25 22:29:11 rick Exp $";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#include "alloc.h"

#if !defined(MAX_PATH_LEN)
#  define MAX_PATH_LEN	1024
#endif

char *resolve_name(const char *ro_paths, const char *name)
{
    int		fd;
    char	file_name[MAX_PATH_LEN + 1];
    int		name_len;
    char *	p;
    char *	paths;
    char *	pathbufp;
    
    if (ro_paths == (char *) NULL || strchr(name, '/') != NULL) {
	return SAVESTR(name);
    }

    pathbufp = paths = SAVESTR(ro_paths);
    
    name_len = strlen(name);
    
    while (*pathbufp != '\0') {
	if ((p = strchr(pathbufp, ':')) != (char *) NULL) {
	    *p = '\0';
	}
	if ((strlen(pathbufp) + name_len + 1) <= MAX_PATH_LEN) {
	    (void) sprintf(file_name, "%s/%s",
			   (*pathbufp == '\0') ? "." : pathbufp, name);
	    if ((fd = open(file_name, O_RDONLY)) >= 0) {
		(void) close(fd);
		free(paths);
		return SAVESTR(file_name);
	    }
	}
	if (p == (char *) NULL) {
	    break;
	}
	pathbufp = p + 1;
    }
    
    free(paths);
    return SAVESTR(name);
}

Added rh.c.





















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
/*
 * rh.c
 *
 * Written by:	Ken Stauffer
 *		Rick Ohnemus	(rick@sterling.com)
 * 
 * Global Functions: main, func_name, activate, deactivate
 *
 * Static Functions: printhelp, max_descriptors, fmt_req_stat,
 *		     prog_req_stat, execute, exam1, exam2,
 *		     exam3, exam4, get_rhpath, get_func,
 *		     push_instr, write_tree, write_functions
 */

#if !defined(lint)
static char rcsid[] = "$Id: rh.c,v 1.3 1994/01/26 16:18:29 rick Exp $";
#endif

#include "rh.h"
#include "rhcmds.h"
#include "rhdata.h"
#include "rhgram.h"
#include "alloc.h"
#include "errmsg.h"

#include <fcntl.h>
#include <pwd.h>

#if defined(DEBUG)
#  include <assert.h>
#  include "dump.h"
#endif

#define VERSION "Version 3.0r"

extern void	ftrw(char *file,
		     void (*fn)(),
		     int depth,
		     int dashr,
		     int samefsPC);
extern char *	graphic(const char *name);
extern void	printentry(int verbose,
			   int str_owner,
			   int full_date,
			   struct stat *buf,
			   const char *name);
extern void	printformat(struct stat *buf,
			    const char *name,
			    int depth);
extern int	rh_parse();
extern void	rhfinish(void);
extern void	rhinit(void);


static int *	active_funcs = (int *) NULL;
static int	num_active_funcs = 0;

#if defined(DEBUG)
extern int	gd_debug;
extern int	rh_debug;
extern int	yy_flex_debug;

int		Trace = 0;
#endif

#if defined(S_IFLNK)

#if defined(DEBUG)
#define SWITCHES "CDFLTUVWXYZbcde:f:hilp:qrstvwx:"
#else
#define SWITCHES "CDFLVbcde:f:hilp:qrstvwx:"
#endif

static char *usage =
    "Usage: %s [-CDFLVbcdhilqrsvw] [-f filename] [-e expression]\n"
    "\t\t[-p format] [-x command] [directory/file ...]\n";

#else

#if defined(DEBUG)
#define SWITCHES "CTUVWXYZbcde:f:hilp:qrstvwx:"
#else
#define SWITCHES "CVbcde:f:hilp:qrstvwx:"
#endif

static char *usage = 
    "Usage: %s [-CVbcdhilqrsvw] [-f filename] [-e expression]\n"
    "\t\t[-p format] [-x command] [directory/file ...]\n";
#endif

 
/*
 * printhelp:
 *	Print out the help screen. The string 's' is argv[0].
 *	Called when the -h option is used.
 */

static void printhelp(const char *s)
{
    int			i;
    const symbol *	p;

    (void) fprintf(stderr, usage, s);
    (void) fprintf(stderr, "\n%s\n", VERSION);
    (void) fprintf(stderr, "\noptions:\n"
#if defined(DEBUG)
		   "\t-T\ttrace\n"
		   "\t-U\tdump entire compiled program\n"
		   "\t-W\tdump executable compiled program\n"
		   "\t-X\tgetdate debug\n"
		   "\t-Y\trhgram debug\n"
		   "\t-Z\trhlex debug\n"
#endif
#if defined(S_IFLNK)
		   "\t-D\tfollow symbolic links that point to directories\n"
		   "\t-F\tfollow symbolic links that point to non-directories\n"
		   "\t-L\tfollow all symbolic links\n"
#endif
		   "\t-C\tdo not cross file systems\n"
		   "\t-V\twrite version number to stderr and exit\n"
		   "\t-b\tprint leading \"./\" if no directory\n"
		   "\t-c\tdo not read RHRC file\n"
		   "\t-d\tleading '?' or '*' matches leading '.' in file name\n"
		   "\t-e\tget expression from the command line\n"
		   "\t-f\tget expression from file\n"
		   "\t-i\tignore case during file name matching\n"
		   "\t-h\tshow this message and exit\n"
		   "\t-l\tlong filename output\n"
		   "\t-p format\tprint file information using 'format'\n"
		   "\t-q\tdisplay non-graphic characters in filenames as ?\n"
		   "\t-r\tdo not go down the directory tree (non-recursive)\n"
		   "\t-s\tprint users/groups as strings instead of numbers\n"
		   "\t-t\tprint full date/time instead of ls(1) type date/time\n"
		   "\t-v\tverbose output\n"
		   "\t-w\tdisplay warning messages\n"
		   "\t-x command\texecute 'command' for matching files\n"
		   "\nvalid symbols:\n"
		   );
		   
    for (i = 1, p = Symbol_table; p != (symbol *) NULL; p = p->next, i++) {
	(void) fprintf(stderr, "%12s%s", p->name,
		      (((i - 1) % 5) == 4
		       || p->next == (symbol *) NULL) ? "\n" : " ");
    }
    
    (void) fprintf(stderr, "\nC operators:\n"
		   "\t! ~ - * / %% + < <= > >= == != & ^ | << >> && || ?:\n"
		   "\nspecial operators:\n"
		   "\t$username , @groupname , \"*.c\" , [date/time spec]\n\n"
		   );
    
    exit(1);
}
 
#if defined(RLIMIT_NOFILE)

/*
 * max_descriptors:
 *	Returns the maximum number of file descriptors.
 */

static int max_descriptors(void)
{
    struct rlimit newrlp;
    struct rlimit rlp;
    
    if (getrlimit(RLIMIT_NOFILE, &rlp) == 0) {
	if (rlp.rlim_cur < NOFILE) {
	    newrlp.rlim_max = newrlp.rlim_cur = NOFILE;
	    if (setrlimit(RLIMIT_NOFILE, &newrlp) == 0)
		return NOFILE;
	}
	return rlp.rlim_cur;
    }
    else {
	return NOFILE;
    }
}
#endif
 
/*
 * func_name:
 *	If a PC corresponds to a compiled function then
 *	return the function's name. Otherwise, return
 *	NULL.
 */

const char *func_name(int pc)
{
    const symbol *	s;

    for (s = Symbol_table; s != (symbol *) NULL; s = s->next) {
	if (s->type == FUNCTION && s->value == pc) {
	    return s->name;
	}
    }

    return (char *) NULL;
}
 
/*
 * activate:
 *	If a function is active then return 0 (FALSE). If the function
 *	is not active then save the pc and return 1 (TRUE).
 */

int activate(int pc)
{
    int		i;

    for (i = 0; i < num_active_funcs; i++) {
	if (pc == active_funcs[i]) {
	    return 0;
	}
    }

    active_funcs =
	REALLOCATE(active_funcs, (num_active_funcs + 1) * sizeof(int));

    active_funcs[num_active_funcs++] = pc;
    return 1;
}
 
/*
 * deactivate:
 *	Remove a function's pc from the active list.
 */

void deactivate(int pc)
{
    const char *	fn;
    int			i;

    for (i = 0; i < num_active_funcs; i++) {
	if (pc == active_funcs[i]) {
	    if (num_active_funcs == 1) {
		num_active_funcs = 0;
		free((char *) active_funcs);
		active_funcs = (int *) NULL;
	    }
	    else {
		for (--num_active_funcs; i < num_active_funcs; i++) {
		    active_funcs[i] = active_funcs[i + 1];
		}
	    }
	    return;
	}
    }

    fn = func_name(pc);
    error("deactivate: function '%s' not active\n", (fn == NULL) ? "" : fn);
}
 
/*
 * fmt_req_stat:
 *	Scan '-p' switch format string for anything that
 *	requires a stat() call to be made.
 *	Return TRUE if stat() is needed or FALSE if it isn't.
 */

static int fmt_req_stat(void)
{
    const char *	fp;
    int			stat_all = FALSE;

    for (fp = attr.format; *fp != '\0' && !stat_all; fp++) {
	if (*fp != '%') {
	    continue;
	}

	++fp;

	switch (*fp) {
	case '\0':
	    error("format character missing after '%%'");
	    /*NOTREACHED*/
	    
	case 'a':
#if BSD
	case 'b':
	case 'B':
#endif
	case 'c':
	case 'g':
	case 'G':
	case 'i':
	case 'l':
	case 'm':
	case 'p':
	case 'P':
	case 'r':
	case 'R':
	case 'u':
	case 'U':
	case 'w':
	case 'W':
	case 'y':
	case 'z':
	    stat_all = TRUE;
	    break;
	}
    }

    return stat_all;
}
 
/*
 * prog_req_stat:
 *	Scan the compiled program for any builtin functions
 *	that require a stat() call to be made.
 *	Return TRUE if stat() is needed or FALSE if it isn't.
 */

static int prog_req_stat(int pc)
{
    const instruction *	ip;
    const symbol *	sym;

    if (StackProgram[pc].func == END_OF_PROG) {
	return -1;
    }

    for (ip = &StackProgram[pc]; ip->func != NULL_INSTR; ip++, pc++) {
	for (sym = Symbol_table; sym != (symbol *) NULL; sym = sym->next) {
	    if (ip->func == sym->func) {
		if (sym->req_stat) {
		    return 1;
		}
		break;
	    }
	}

	if (ip->func == c_func) {
	    if (activate(ip->value.pc)) {
		if (prog_req_stat((int) ip->value.pc)) {
		    deactivate(ip->value.pc);
		    return 1;
		}
		deactivate(ip->value.pc);
	    }
	}
    }

    return 0;
}
 
/*
 * execute:
 *	Execute the program contained in the StackProgram[] array.
 *	Each element of the StackProgram[] array contains a pointer
 *	to a C function.
 *	Programs are NULL_INSTR terminated. (NULL_INSTR is a NULL
 *	function pointer.)
 *	Returns the result of the expression.
 */

static long execute(void)
{
    SP = 0;

    if (startPC == -1) {
	return 1;
    }

    for (PC = startPC; StackProgram[PC].func != NULL_INSTR; PC++) {
#if defined(DEBUG)
	if (Trace) {
	    dump_instr(PC, &StackProgram[PC]);
	}
#endif
	(*StackProgram[PC].func)(&StackProgram[PC].value);
	if (SP < 0) {
	    error("data stack underflow");
	}
	else if (SP >= MAX_STACK_LEN) {
	    error("data stack overflow");
	}
    }
#if defined(DEBUG)
    assert(SP == 1);
    if (Trace) {
	(void) fprintf(stderr, "execute() returning %ld\n", Stack[0]);
    }
#endif
    return Stack[0];
}
 
/*
 * exam1:
 * exam2:
 * exam3:
 * exam4:
 *	One of these functions is called for every file that 'rh'
 *	examines. Each function first calls execute() to see if the
 *	expression is TRUE. If the expression is TRUE (non-zero)
 *	then the action specified by the command line options is
 *	executed.
 */

/* print file out by itself */

static void exam1(void)
{
    if (execute()) {
	(void) printf("%s\n", attr.graphic ? graphic(attr.fname) : attr.fname);
    }
    return;
}

/* long output of file */

static void exam2(void)
{
    if (execute()) {
	printentry(attr.verbose, attr.str_owner, attr.full_date,
		   attr.buf, attr.fname);
    }
    return;
}

/* formatted output of file information */

static void exam3(void)
{
    if (execute()) {
	printformat(attr.buf, attr.fname, attr.depth);
    }
    return;
}

/* do a system(3) call of desired command */

static void exam4(void)
{
    char        command[NCARGS];
    char *      end;
    int         f_l;
    char *      n;
    int         n_l;
    char *      p;
    char *      q;
    char *      r;
    int         rv;
    
    if (execute()) {
	p = command;
	end = command + sizeof(command);
	n = (char *) NULL;
	q = attr.command;
	f_l = strlen(attr.fname);
	while (*q != '\0') {
	    if (*q != '%') {
		*p++ = *q++;
	    }
	    else {
		++q;
		if (*q == 's') {
		    if ((p + f_l) >= end) {
			warning("%s: maximum command line length exceeded",
				attr.fname);
			return;
		    }
		    r = attr.fname;
		    while ((*p++ = *r++) != '\0')
			/* void */ ;
		    p--;
		}
		else if (*q == 'S') {
		    if (n == (char *) NULL) {
			if ((n = strrchr(attr.fname, '/')) == (char *) NULL) {
			    n = attr.fname;
			}
			else {
			    ++n;
			}
			n_l = strlen(n);
		    }
		    if ((p + n_l) >= end) {
			warning("%s: maximum command line length exceeded",
				attr.fname);
			return;
		    }
		    r = n;
		    while ((*p++ = *r++) != '\0')
			/* void */ ;
		    p--;
		}
		else {
		    if ((p + 2) >= end) {
			warning("%s: maximum command line length exceeded",
				attr.fname);
			return;
		    }
		    *p++ = '%';
		    *p++ = *q;
		}
		++q;
	    }
	    if (p == end) {
		warning("%s: maximum command line length exceeded",
			attr.fname);
		return;
	    }
	}
	*p = '\0';
	rv = system(command);
	if (rv != 0) {
	    ++attr.num_errors;
	}
	if (attr.verbose) {
	    (void) printf("%s exit(%d)\n", command, rv);
	}
    }
    
    return;
}
 
/*
 * get_rhpath:
 *	Return value of RHPATH environment variable.
 *	If RHPATH is not set then attempt to return
 *	the user's home directory.
 *
 *	The value is returned in allocated memory.
 */

static char *get_rhpath(void)
{
    const char *	home;
    char *		rhpath;

    rhpath = getenv(RHPATH_ENV);
    if (rhpath == (char *) NULL) {
	home = getenv(HOME_ENV);
	if (home == (char *) NULL) {
	    struct passwd *	info;
	    
	    info = getpwuid(getuid());
	    if (info == (struct passwd *) NULL) {
		rhpath = SAVESTR(".");
	    }
	    else {
		rhpath = ALLOCATE(strlen(info->pw_dir) + 3);
		(void) sprintf(rhpath, ".:%s", info->pw_dir);
	    }
	}
	else {
	    rhpath = ALLOCATE(strlen(home) + 3);
	    (void) sprintf(rhpath, ".:%s", home);
	}
    }
    else {
	rhpath = SAVESTR(rhpath);
    }

    return rhpath;
}
 
/*
 * get_func:
 *	Returns the symbol table entry for an rh
 *	language function given the name of the function.
 */

symbol *get_func(char *func_name)
{
    symbol *	sym;

    for (sym = Symbol_table; sym != (symbol *) NULL; sym = sym->next) {
	if (sym->type == FUNCTION && strcmp(sym->name, func_name) == 0) {
	    return sym;
	}
    }
    
    error("function '%s' lookup failure", func_name);

    /*NOTREACHED*/
}
 
/*
 * push_instr:
 *	Pushes an instruction on to the program stack.
 */

static void push_instr(void	      (*func)(instr_value *),
		       instr_value *	value)
{
    if (PC >= MAX_PROG_LEN) {
	error("program is too big");
    }
    StackProgram[PC].func = func;
    StackProgram[PC++].value = *value;
    return;
}
 
/*
 * write_tree:
 *	Compiles an expression tree on the program stack.
 */

static void write_tree(tree)
node *	tree;
{
    instr_value	value;

    if (tree == (node *) NULL) {
	return;
    }

    if (tree->code.func == UNRES_FUNC) {
	symbol *sym = get_func(tree->code.value.func_name);
	if (tree->count > 0) {
	    int i;
	    
	    for (i = 0; i < tree->count; i++) {
		write_tree(tree->trees[i]);
	    }
	    free((char *) tree->trees);
	}
	tree->count = 0;
	if (sym->value == -1) {
	    push_instr(UNRES_FUNC, &tree->code.value);
	}
	else {
	    value.pc = sym->value;
	    push_instr(c_func, &value);
	}
    }
    else if (tree->code.func == TERTIARY) {
	int colon_pc;
	int qm_pc;

	write_tree(tree->left);
	qm_pc = PC;
	value.pc = 0;
	push_instr(c_qm, &value);
	write_tree(tree->middle);
	colon_pc = PC;
	push_instr(c_colon, &value);
	write_tree(tree->right);
	StackProgram[qm_pc].value.pc = colon_pc;
	StackProgram[colon_pc].value.pc = PC - 1;
    }
    else if (tree->code.func == c_syscall) {
	if (tree->count > 0) {
	    int i;
	    
	    for (i = 0; i < tree->count; i++) {
		write_tree(tree->trees[i]);
	    }
	    free((char *) tree->trees);
	}
	tree->count = 0;
	push_instr(c_syscall, &tree->code.value);
    }
    else {
	write_tree(tree->left);
	write_tree(tree->right);
	push_instr(tree->code.func, &tree->code.value);
    }

    free((char *) tree);

    return;
}
 
/*
 * write_functions:
 *	Compiles all rh language functions on to the program stack.
 */

static void write_functions(void)
{
    instruction *	ip;
    symbol *		sym;
    instr_value		value;

    for (sym = Symbol_table; sym != (symbol *) NULL; sym = sym->next) {
	if (sym->type == FUNCTION) {
	    sym->value = PC;

	    value.num_params = sym->num_params;
	    push_instr(NUM_PARAMS, &value);

	    write_tree(sym->tree);

	    value.num_params = sym->num_params;
	    push_instr(c_return, &value);

	    value.l = 0L;
	    push_instr(NULL_INSTR, &value);

	    sym->tree = (node *) NULL;
	}
    }

    value.l = 0L;
    push_instr(NULL_INSTR, &value);
    push_instr(END_OF_PROG, &value);
    PC--;

    for (ip = StackProgram; ip->func != END_OF_PROG; ip++) {
	if (ip->func == UNRES_FUNC) {
	    ip->func = c_func;
	    sym = get_func(ip->value.func_name);
	    ip->value.pc = sym->value;
	}
    }

    return;
}
 
/*
 * main:
 *	Parse arguments.
 *	  -b, -c, -D, -d, -F, -i, -L, -l, -q, -r, -s, -t, and -w switches
 *	  can occur as often as desired even though only one occurence
 *	  is necessaty.
 *
 *	  -e and -f can occur one or more times and each occurence must
 *	  have an associated argument.
 *
 *	  First occurence of -h or -V causes a message to be output then
 *	  rh exits.
 *
 *	  -p and -x can occur only once and must have an associated argument.
 *
 *	  -T, -W, -X, -Y, and -Z are only available if rh is compiled with
 *	  debug turned on.
 *
 *      Parse any RHRC file.
 *	Parse any -f filenames.
 *	Parse any -e expressions.
 *	If there is a start expression then compile it and all rh language
 *	functions.
 *	Perform the recursive hunt on remaining arguments.
 */

void main(int argc, char *argv[])
{
    extern int		getopt();
    extern char *	optarg;
    extern int		opterr;
    extern int		optind;
    extern char *	resolve_name(const char *paths, const char *name);
    extern void		yyrestart();

    char **		cl_exprs = (char **) NULL;
    char **		cl_fnames = (char **) NULL;
    int			colon_pc;
    int			dashb = FALSE;
    int			dashl = FALSE;
    int			dashr = TRUE;
    int			depth;
    int			dont_cross_filesystems = FALSE;
    void	      (*examptr)(void) = exam1;	/* default output function */
    int			first_parse = TRUE;
    int			i;
    int			num_cl_exprs = 0;
    int			num_cl_fnames = 0;
    int			qm_pc;
    int			read_rhrc = TRUE;
    char *		rhpath = (char *) NULL;	/* RHPATH env. variable */
    int			samefsPC = -1;
    instr_value		value;

#if defined(DEBUG)
    int			dump_all;
    int			dump_exec;
#endif


    Program_name = strrchr(argv[0], '/');
    Program_name = (Program_name == NULL) ? argv[0] : (Program_name + 1);

#if defined(DEBUG)
    dump_all = dump_exec = gd_debug = rh_debug = yy_flex_debug = 0;
#endif

    attr.command = (char *) NULL;	/* -x option */
    attr.format = (char *) NULL;	/* -p option */
    attr.dot_special = TRUE;		/* -d option */
#if defined(S_IFLNK)
    attr.follow_dir = FALSE;		/* -D & -L options */
    attr.follow_file = FALSE;		/* -F & -L options */
#endif
    attr.graphic = FALSE;		/* -q option */
    attr.ignore_case = FALSE;		/* -i option */
    attr.prune = FALSE;			/* prune (obviously :-) */
    attr.stat_all = FALSE;		/* don't need to stat everything */
    attr.str_owner = FALSE;		/* -u option */
    attr.verbose = FALSE;		/* -v option */
    attr.full_date = FALSE;		/* -t option */
    attr.num_errors = 0;		/* no errors yet */


    rhinit();

    opterr = 1;

    while ((i = getopt(argc, argv, SWITCHES)) != EOF) {

	switch (i) {
	    
	case 'b':
	    dashb = TRUE;
	    break;
	    
	case 'C':
	    dont_cross_filesystems = TRUE;
	    attr.stat_all = TRUE;	/* <<< is this necessary? */
	    break;

	case 'c':
	    read_rhrc = FALSE;
	    break;

#if defined(S_IFLNK)
	case 'D':
	    attr.follow_dir = TRUE;
	    attr.stat_all = TRUE;
	    break;
#endif
	    
	case 'd':
	    attr.dot_special = FALSE;
	    break;
	    
	case 'e':
	    cl_exprs =
		REALLOCATE(cl_exprs, sizeof(char *) * (num_cl_exprs + 1));
	    cl_exprs[num_cl_exprs++] = optarg;
	    break;
	    
#if defined(S_IFLNK)
	case 'F':
	    attr.follow_file = TRUE;
	    attr.stat_all = TRUE;
	    break;
#endif
	    
	case 'f':
	    cl_fnames =
		REALLOCATE(cl_fnames, sizeof(char *) * (num_cl_fnames + 1));
	    cl_fnames[num_cl_fnames++] = optarg;
	    break;
	    
	case 'h':
	    printhelp(argv[0]);
	    /*NOTREACHED*/
	    
	case 'i':
	    attr.ignore_case = TRUE;
	    break;
	    
#if defined(S_IFLNK)
	case 'L':
	    attr.follow_dir = TRUE;
	    attr.follow_file = TRUE;
	    attr.stat_all = TRUE;
	    break;
#endif
	    
	case 'l':
	    examptr = exam2;
	    dashl = TRUE;
	    attr.stat_all = TRUE;
	    break;
	    
	case 'p':
	    if (attr.format != (char *) NULL) {
		error("only one -p option is allowed");
	    }
	    attr.format = optarg;
	    examptr = exam3;
	    break;
	    
	case 'q':
	    attr.graphic = TRUE;
	    break;
	    
	case 'r':
	    dashr = FALSE;
	    break;
	    
	case 's':
	    attr.str_owner = TRUE;
	    break;
	    
	case 't':
	    attr.full_date = TRUE;
	    break;

	case 'V':
	    error(VERSION);
	    /*NOTREACHED*/
	    
	case 'v':
	    attr.verbose = TRUE;
	    break;
	    
	case 'w':
	    Issue_warnings = TRUE;
	    break;

	case 'x': 
	    if (attr.command != (char *) NULL) {
		error("only one -x option is allowed");
	    }
	    examptr = exam4;
	    attr.command = optarg;
	    break;
	    
#if defined(DEBUG)
	case 'T':
	    ++Trace;
	    break;

	case 'U':
	    ++dump_all;
	    break;

	case 'W':
	    ++dump_exec;
	    break;

	case 'X':
	    ++gd_debug;
	    break;

	case 'Y':
	    ++rh_debug;
	    break;

	case 'Z':
	    ++yy_flex_debug;
	    break;
#endif

	default:
	    warning("use -h for help");
	    exit(1);
	}
	
    }
    
    /* perform a little bit of sanity checking on switch combinations */

    if (attr.command != (char *) NULL) {
	if (dashl) {
	    error("cannot have both -x and -l options");
	}
	else if (attr.format != (char *) NULL) {
	    warning("-x overrides -p, -p ignored");
	    attr.format = (char *) NULL;
	}
    }
    else if (attr.format != (char *) NULL) {
	if (dashl) {
	    warning("-p overrides -l, -l ignored");
	    dashl = FALSE;
	}
	if (attr.verbose) {
	    warning("-p overrides -v, -v ignored");
	    attr.verbose = FALSE;
	}
    }

    /*
     * Always display control characters in file names as a '?'
     * if the output is a terminal.
     */

    if (isatty(fileno(stdout))) {
	attr.graphic = TRUE;
    }
    
    /*
     * Read and parse functions/expression in files.
     */

    startPC = -1;
    
    if (read_rhrc || num_cl_fnames) {
	rhpath = get_rhpath();
	if (read_rhrc) {
	    char *rhrc = getenv(RHRC_ENV);
	    if (rhrc == (char *) NULL) {
		rhrc = RHRC;
	    }
	    Input_name = resolve_name(rhpath, rhrc);
	    Input_file = open(Input_name, O_RDONLY, 0);
	    if (Input_file != -1) {
		if (rh_parse()) {
		    exit(1);
		}
		first_parse = FALSE;
		(void) close(Input_file);
	    }
	    free(Input_name);
	}
	if (num_cl_fnames) {
	    for (i = 0; i < num_cl_fnames; i++) {
		if (!first_parse) {
		    yyrestart(stdin);
		}
		if (strcmp(cl_fnames[i], "-") == 0) {
		    Input_name = "stdin";
		    if (feof(stdin)) {
			Input_file = open("/dev/tty", O_RDONLY, 0);
			if (Input_file == -1)  {
			    error("can't reopen stdin : %m");
			}
			if (rh_parse()) {
			    exit(1);
			}
			(void) close(Input_file);
		    }
		    else {
			Input_file = fileno(stdin);
			if (rh_parse()) {
			    exit(1);
			}
		    }
		}
		else {
		    Input_name = resolve_name(rhpath, cl_fnames[i]);
		    Input_file = open(Input_name, O_RDONLY, 0);
		    if (Input_file == -1) {
			error("can't open %s : %m", Input_name);
		    }
		    if (rh_parse()) {
			exit(1);
		    }
		    (void) close(Input_file);
		    free(Input_name);
		}
		first_parse = FALSE;
	    }
	    free((char *) cl_fnames);
	}
    }

    if (rhpath != (char *) NULL) {
	free(rhpath);
    }
    
    /*
     * Parse expressions from -e switches
     */

    if (num_cl_exprs) {
	Input_file = -1;
	Input_name = "cmd line";
	for (i = 0; i < num_cl_exprs; i++) {
	    Input_expr_ptr = cl_exprs[i];
	    Input_expr_end = Input_expr_ptr + strlen(Input_expr_ptr);
	    if (!first_parse) {
		yyrestart(stdin);
	    }
	    if (rh_parse()) {
		exit(1);
	    }
	    first_parse = FALSE;
	}
	free((char *) cl_exprs);
    }

    /*
     * Compile the parse tree if a start expression was specified.
     */

    if (Start_expr == (node *) NULL) {
	if (Issue_warnings) {
	    warning("no start expression specified");
	}
	if (dont_cross_filesystems) {
	    samefsPC = startPC = PC = 0;

	    value.dev = (dev_t) 0;
	    push_instr(c_samefs, &value);

	    qm_pc = PC;
	    value.pc = 0;
	    push_instr(c_qm, &value);

	    value.l = 1L;
	    push_instr(c_number, &value);

	    colon_pc = PC;
	    value.pc = 0;
	    push_instr(c_colon, &value);

	    value.l = 0L;
	    push_instr(c_prune, &value);

	    StackProgram[qm_pc].value.pc = colon_pc;
	    StackProgram[colon_pc].value.pc = PC - 1;

	    value.l = 0L;
	    push_instr(NULL_INSTR, &value);
	    push_instr(END_OF_PROG, &value);
	}
    }
    else {
	PC = 0;
	write_functions();
	startPC = PC;
	if (dont_cross_filesystems) {
	    samefsPC = PC;
	    value.dev = (dev_t) 0;
	    push_instr(c_samefs, &value);

	    qm_pc = PC;
	    value.pc = 0;
	    push_instr(c_qm, &value);
	}
	write_tree(Start_expr);
	if (dont_cross_filesystems) {
	    colon_pc = PC;
	    value.pc = 0;
	    push_instr(c_colon, &value);

	    value.l = 0L;
	    push_instr(c_prune, &value);

	    StackProgram[qm_pc].value.pc = colon_pc;
	    StackProgram[colon_pc].value.pc = PC - 1;
	}
	value.l = 0L;
	push_instr(NULL_INSTR, &value);
	push_instr(END_OF_PROG, &value);
    }

#if defined(DEBUG)
    if (startPC != -1) {
	if (dump_all) {
	    int opc = 0;
	    int npc;

	    while ((npc = dump_program(opc, 0)) != -1) {
		opc = npc;
	    }
	}
	else if (dump_exec) {
	    (void) dump_program(startPC, 1);
	}
    }
#endif

    rhfinish();
    
    if (!attr.stat_all) {
	if (attr.format != (char *) NULL) {
	    attr.stat_all = fmt_req_stat();
	}
	if (!attr.stat_all && startPC != -1) {
	    /* scan program for anything that requires a stat() call */
	    attr.stat_all = prog_req_stat(startPC);
	}
    }

#if defined(DEBUG2)
    (void) fprintf(stderr, "stat all = %s", attr.stat_all ? "true" : "false");
#endif

    depth = dashr ? DEPTH : 1;
    
    if (optind >= argc) {
	ftrw(dashb ? "." : "", examptr, depth, dashr, samefsPC);
    }
    else {
	for (/* void */; optind < argc; optind++) {
	    if (samefsPC != -1) {
		StackProgram[samefsPC].value.dev = (dev_t) 0;
	    }
	    ftrw(argv[optind], examptr, depth, dashr, samefsPC);
	}
    }
    
#if defined(DEBUG)
    {
	symbol *sym;

	while (Symbol_table->type == FUNCTION) {
	    sym = Symbol_table;
	    Symbol_table = Symbol_table->next;
	    free(sym->name);
	    free((char *) sym);
	}
    }
    if (startPC != -1) {
	int pc;

	for (pc = 0; StackProgram[pc].func != END_OF_PROG; ++pc) {
	    if (StackProgram[pc].func == c_pattern
		|| StackProgram[pc].func == c_fncase
		|| StackProgram[pc].func == c_fnigncase) {
		free(StackProgram[pc].value.pattern);
	    }
	    else if (StackProgram[pc].func == c_date) {
		free((char *) StackProgram[pc].value.di);
	    }
	}
    }
#endif

    exit((attr.num_errors == 0) ? 0 : 1);
    /*NOTREACHED*/
}

Added rh.depends.























































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
alloc.o: alloc.c
alloc.o: errmsg.h

errmsg.o: errmsg.c

getdate.o: getdate.c
getdate.o: getdate.h
getdate.o: getdate.y

resname.o: alloc.h
resname.o: resname.c

rh.o: alloc.h
rh.o: errmsg.h
rh.o: getdate.h
rh.o: rh.c
rh.o: rh.h
rh.o: rhcmds.h
rh.o: rhdata.h
rh.o: rhgram.h

rhcmds.o: getdate.h
rhcmds.o: rh.h
rhcmds.o: rhcmds.c
rhcmds.o: rhdata.h
rhcmds.o: sysinfo.h

rhdata.o: getdate.h
rhdata.o: rh.h
rhdata.o: rhcmds.h
rhdata.o: rhdata.c
rhdata.o: rhgram.h

rhdir.o: errmsg.h
rhdir.o: getdate.h
rhdir.o: rh.h
rhdir.o: rhdata.h
rhdir.o: rhdir.c
rhdir.o: sysinfo.h

rhgram.o: alloc.h
rhgram.o: errmsg.h
rhgram.o: getdate.h
rhgram.o: rh.h
rhgram.o: rhcmds.h
rhgram.o: rhdata.h
rhgram.o: rhgram.c
rhgram.o: rhgram.y

rhlex.o: alloc.h
rhlex.o: errmsg.h
rhlex.o: getdate.h
rhlex.o: rh.h
rhlex.o: rhdata.h
rhlex.o: rhgram.h
rhlex.o: rhlex.c
rhlex.o: rhlex.l
rhlex.o: sysinfo.h

strtol.o: strtol.c

strtoul.o: strtoul.c

sysinfo.o: alloc.h
sysinfo.o: sysinfo.c
sysinfo.o: sysinfo.h

wildmat.o: wildmat.c

dump.o: dump.c
dump.o: getdate.h
dump.o: rh.h
dump.o: rhcmds.h
dump.o: rhdata.h

Added rh.h.

































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * rh.h
 *
 * Written by:	Ken Stauffer
 *		Rick Ohnemus	(rick@sterling.com)
 *
 * rcsid: $Id: rh.h,v 1.1 1994/01/12 19:37:16 rick Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/unistd.h>


#if !defined(FALSE)
#  define FALSE	0
#endif

#if !defined(TRUE)
#  define TRUE	1
#endif


#define RHRC		".rhrc"		/* start up file */
#define RHRC_ENV	"RHRC"		/* rh environment variable */
#define RHPATH_ENV	"RHPATH"	/* paths to search for program files */
#define HOME_ENV	"HOME"		/* path to your home directory */

#if !defined(MAX_PROG_LEN)
#  define MAX_PROG_LEN	2000	/* size of stack program */
#endif

#if !defined(MAX_STACK_LEN)
#  define MAX_STACK_LEN	1000	/* size of stack */
#endif

#define NULL_INSTR	((void (*)(instr_value *)) NULL)
#define END_OF_PROG	((void (*)(instr_value *)) -1)
#define EXPR_LIST	((void (*)(instr_value *)) -2)
#define TERTIARY	((void (*)(instr_value *)) -3)
#define UNRES_FUNC	((void (*)(instr_value *)) -4)
#define NUM_PARAMS	((void (*)(instr_value *)) -5)
#define UNUSED_FUNC	((void (*)(instr_value *)) -6)

/* The '- 3' in DEPTH defines is for stdin, stdout, and stderr */

#if defined(BSD)
#  if defined(SUNOS_4)
#    define POSIX_DIRECTORY_LIBRARY
#    include <sys/time.h>
#    include <sys/resource.h>
#    if defined(RLIMIT_NOFILE)
#      define DEPTH	(max_descriptors() - 3)
#    else
#      define DEPTH	(getdtablesize() - 3)
#    endif /* RLIMIT_NOFILE */
#  else
#    define DEPTH	(getdtablesize() - 3)
#  endif /* SUNOS_4 */
#endif /* BSD */

#if defined(SYSV)
#  if defined(SYSVR3)
#    define DEPTH	(ulimit(4, 0L) - 3)
#    define POSIX_DIRECTORY_LIBRARY
#  else /* SYSVR3 */
     /* This value was arbitrarily chosen */
#    define DEPTH	(20 - 3)
#  endif /* SYSVR3 */
#endif /* SYSV */


/*
 * XXX - on BSD systems, this is defined in <sys/param.h>.
 * On System V Release 3, it's defined inside "nami.c", and is,
 * unfortunately, not in any include file.
 * On systems other than those, no such simple limit exists.
 * On BSD and S5R3 systems, as distributed by Berkeley and AT&T,
 * respectively, it's 1024, so we set it to that.
 */

#if !defined(MAXPATHLEN)
#  if defined(PATH_MAX)
#    define MAXPATHLEN	PATH_MAX
#  else
#    if defined(MAXNAMLEN)
#      define MAXPATHLEN  MAXNAMLEN
#    else
#      define MAXPATHLEN  1024
#    endif
#  endif
#endif

#if !defined(MAXNAMLEN)
#  if defined(PATH_MAX)
#    define MAXNAMLEN	PATH_MAX
#  else
#    define MAXNAMLEN	255
#  endif
#endif


#include <string.h>
#include "getdate.h"


/*
 * Some System do not define these macro's.
 */

#if !defined(major)
#  define major(b)	((((unsigned) (b)) >> 8) & 0xff)
#endif

#if !defined(minor)
#  define minor(b)	(((unsigned) (b)) & 0xff)
#endif

#if !defined(S_ISBLK)
#  define S_ISBLK(m)	(((m) & S_IFMT) == S_IFBLK)
#endif

#if !defined(S_ISCHR)
#  define S_ISCHR(m)	(((m) & S_IFMT) == S_IFCHR)
#endif

#if !defined(S_ISDIR)
#  define S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)
#endif

#if defined(S_IFIFO)
#  if !defined(S_ISFIFO)
#    define S_ISFIFO(m)	(((m) & S_IFMT) == S_IFIFO)
#  endif
#else
#  define S_ISFIFO(m)	(0)
#endif

#if defined(S_IFLNK)
#  if !defined(S_ISLNK)
#    define S_ISLNK(m)	(((m) & S_IFMT) == S_IFLNK)
#  endif
#else
#  define S_ISLNK(m)	(0)
#endif

#if !defined(S_ISREG)
#  define S_ISREG(m)	(((m) & S_IFMT) == S_IFREG)
#endif

#if defined(S_IFSOCK)
#  if !defined(S_ISSOCK)
#    define S_ISSOCK(m)	(((m) & S_IFMT) == S_IFSOCK)
#  endif
#else
#  define S_ISSOCK(m)	(0)
#endif


/*
 * Builtin system calls
 */

#define RH_SYSCALL_EXIT		0


typedef struct _instruction	instruction;
typedef struct _node		node;
typedef struct _symbol		symbol;


/*
 * Structure of a "rh-assembly" instruction.
 */

typedef union {
    dev_t	dev;
    date_info *	di;
    char *	fstype;
    char *	func_name;
    long	l;
    int		num_params;
    int		param;
    char *	pattern;
    int		pc;
    symbol *	sym;
    node *	tree;
} instr_value;

struct _instruction {
   void	      (*func)(instr_value *);
   instr_value	value;
};


/*
 * Structure of a parse tree node
 */

struct _node {
    instruction	code;
    node *	left;
    node *	right;
    node *	middle;
    int		count;
    node **	trees;
};


/*
 * Structure of a symbol.
 */

struct _symbol {
   char *	name;		/* name */
   short	type;		/* type */
   short	req_stat;	/* requires stat() call */
   long		value;		/* value */
   void	      (*func)(instr_value *);	/* C function to execute */
   symbol *	next;		/* next symbol in list */
   short	ref_count;      /* func. parameter reference count */
   short	num_params;	/* # of params an rh function has */
   node *	tree;		/* parse tree for an rh function */
};



/*
 * Structure defining the rh runtime environment.
 */

struct runtime {
   struct stat *	buf;	/* stat info of current file */
   char *		fname;	/* file name of current file */
   int			depth;	/* relative depth of current file */
   void		      (*func)(void); /* examination function */
   char *		command; /* command to exec for current file */
   char *		format;	/* format for printing file information */
   int			num_errors; /* number of errors */
   /* ---------- flags ---------- */
   char			dot_special;	/* dot at start of filename special? */
#if defined(S_IFLNK)
   char			follow_dir;	/* follow directory symlinks */
   char			follow_file;	/* follow non-directory symlinks */
#endif
   char			graphic;	/* display non-graphic characters in filenames as ? */
   char			ignore_case;	/* ignore case when comparing filenames */
   char			prune;		/* flag to indicate prunning */
   char			stat_all;	/* stat() all files */
   char			str_owner;	/* print user/group strings instead of numbers */
   char			verbose;	/* used by the (*func)() routine */
   char			full_date;	/* print full date instead of ls(1) type date */
};

Added rh.man.



















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
.TH RH 1 "18 July 1993"
.SH NAME
rh \- recursive file locater (rawhide) >> VERSION 3.0r <<
.LP
.SH SYNOPSIS
\fBrh\fP [\fB-CDFLVbcdhilqrstvw\fP] [\fB-f filename\fP]
[\fB-e expression\fP] [\fB-p format\fP] [\fB-x command\fP]
[\fBfile ...\fP]
.RE
.LP
.SH DESCRIPTION
.B rh
recursively searches the file system starting at each given
.I file
for files that make a C expression true. If no files
are listed, the current working directory is used.
.LP
Expressions for \fBrh\fP can come from the command line (with the
\fI-e\fP option) or a file (with the \fI-f\fP option).
The basic form of an \fBrh\fP expression is a C expression which 
may optionally define and or call user defined functions. These
C expressions may contain constants, variables, and all the usual
C operators.
.LP
\fIConstants\fP are either numeric or symbolic. Symbolic constants
are based on the constants defined in the file \fIstat.h\fP (found in
system include directory, \fI/usr/include/sys/\fP on many systems).
(Only the useful constants are implemented.)
The ``\fBS_\fP'' prefix from the symbol name is omitted.
(eg. \fBS_IFMT\fP would used as \fBIFMT\fP).
.LP
\fIVariables\fP are symbols that specify a field in the stat structure
(e.g., \fBst_size\fP, \fBst_mode\fP, etc.) or some other attribute of 
the file. For each file examined by \fBrh\fP,
these internal variables are updated to match the current file. For
convenience, the ``\fBst_\fP'' prefix is dropped from variable names.
.LP
\fIFunctions\fP are a means of associating a C expression with a function 
name. This allows complex expressions to be easily composed from simpler 
ones. The value of a function call is the value of the expression 
represented by the function. For example:
.LP
.RS 8
foo(x)
.br
{
.br
    return( x-1 );
.br
}
.RE
.LP
If the above function were given to \fBrh\fP, it would define a function
that could be used later. If \fIfoo\fP were called with 667, then \fIfoo\fP
would return 666.
.LP
.SH OPTIONS
\fBrh\fP options can appear in any order; multiple options can be given 
within the same argument.
.TP
.I \-b
Print leading "./" as directory name if no directories are specified.
.TP
.I \-c
Do not read \fBrh\fP startup file (specified by
.SB RHRC
environment variable). (See
.SM \fBSearch Order\fP
for more information.)
.TP
.I \-d
Causes a leading '?' or '*' in a filename pattern to match a
leading '.' in the filename being matched.
.TP
.I \-e expression
\fIexpression\fP is \fBrh\fP code to compile. It can consist of 
function definitions and/or an expression. Since many of the operators
are also shell meta-characters and since \fBrh\fP expressions may
contain spaces, it is strongly recommended that the \fIexpression\fP
be enclosed in single quotes ('').  If both \fB-e\fP and \fB-f\fP
options occur together then the \fB-f\fP options are processed FIRST.
This means that expressions specified with \fB-e\fP options may use
functions defined in the files specified with \fB-f\fP options.
.RS
.LP
This option may be used any number of times. \fI-e\fP switch values are
processed in the order that they occur in the command line.
.RE
.TP
.I \-f filename
Uses \fIfilename\fP as the name of a file containing \fBrh\fP
expressions. Functions may also be defined in this file. If
\fIfilename\fP is ``-'' then expressions are read from standard input
.RS
.LP
This option may be used any number of times. Files are processed in the
order that they occur in the command line.
.RE
.TP
.I \-h
Causes \fBrh\fP to display a help message and exit. The message explains 
the command line usage, a list of available constants and variables, and
a list of valid operators.
.TP
.I \-i
Ignore case of letters when matching filenames against a pattern
(see 
.SM \fBSpecial Operators\fP 
below).
.TP
.I \-l
Normally \fBrh\fP prints each matching filename on a line by itself.  
The \fI-l\fP option causes the matching files' permission modes and 
sizes to be displayed as well. The format is similar to that of the
\fIls\fP(1) command. (Also see \fI-v\fP and \fI-s\fP below.)
.TP
.I \-p format
Output file names and/or other file information using \fIformat\fP.
The \fIformat\fP is a string containing three types of objects: normal 
characters that are output as is, escaped characters that are converted 
to control characters, and substitution markers that indicate which part 
of a file name or piece of file information is to be output.
.RS
.LP
Most of the standard C-like escape characters (and possibly a few that
aren't standard) are recognized. Valid escape characters are:
.RE
.RS
.RS
.PD 0
.TP
.B \ea
.SM ALERT
(bell)
.TP
.B \eb
.SM BACKSPACE
.TP
.B \ef
.SM FORMFEED
.TP
.B \en
.SM NEWLINE
.TP
.B \er
.SM RETURN
.TP
.B \et
.SM TAB
.TP
.B \ev
.SM vertical TAB
.TP
.BI \e0 n
the 8-bit character whose
.SM ASCII
code is the 1-, 2-, or 3-digit octal number \fIn\fP.
.TP
.BI \e0X n
the 8-bit character whose
.SM ASCII
code is the 1- or 2-digit hexadecimal number \fIn\fP.
The `\fBX\fP' may be either lower or upper case.
.RE
Any other characters preceded by a `\fB\e\fP' are output `as is'.
Note that strings containing `\fB\e\fP' characters should be quoted
in a way that prevents the shell from interpretting them.
.PD
.LP
Substition markers are a `\fB%\fP' (percent sign) followed by a character
indicating what is to be output. The substitution characters are:
.RS
.PD 0
.TP
.B a
Last access time of file as an \fIls\fP(1) type time string
.TP
.B A
Last access time of file in seconds
.TP
.B b
Size of file in blocks
.TP
.B B
Filesystem's preferred block size in bytes
.TP
.B c
Last status change time of file as an \fIls\fP(1) type time string
.TP
.B C
Last status change time of file in seconds
.TP
.B d
Parent directory of file
.TP
.B D
Depth of file relative to search starting point
.TP
.B g
Numerical group id of file owner
.TP
.B G
Group name (from /etc/group) of file owner or numerical group id if a
group name is not found
.TP
.B i
File's inode number
.TP
.B l
Number of hard links to file
.TP
.B m
Last modification time of file as an \fIls\fP(1) type time string
.TP
.B M
Last modification time of file in seconds
.TP
.B n
Full path of file
.TP
.B N
Base name of file
.TP
.B p
File permissions in symbolic form
.TP
.B P
File permissions as an octal number
.TP
.B r
If the file is a block or character special device then output the minor
device number, else output `\fIxxxx\fP'.
.TP
.B R
If the file is a block or character special device then output the major
device number, else output `\fIxxxx\fP'.
.TP
.B s
Full path of file
.TP
.B S
Base name of file
.TP
.B u
Numerical user id of file owner
.TP
.B U
User name (from /etc/passwd) of file owner or numerical user id if a
user name is not found
.TP
.B w
The minor device number of device where file resides.
.TP
.B W
The major device number of device where file resides.
.TP
.B z
File size in bytes.
.RE
Any other characters preceded by a `\fB%\fP' are output `as is'.
.RE
.PD
.TP
.I \-q
Display non-graphic (i.e. non-ascii, control, or white space) characters
in filenames as a \fB?\fP. This is automatically turned on if output
is to a terminal.
.TP
.I \-r
Prevents \fBrh\fP from recursively searching for files.
.TP
.I \-s
Print owner and group instead of the owner's UID and group's GID when
both \fI-l\fP and \fI-v\fP options are used.
.TP
.I \-t
Print full dates/times instead of \fIls\fP(1) format dates/times when
using the \fI-l\fP and \fI-v\fP switches or any of the date/time
formats with the \fI-p\fP switch.
.TP
.I \-v
Causes the \fI-l\fP option to output more information and the
\fI-x\fP option to print out the command executed and the command's
exit value.
.TP
.I \-w
Enable warnings about function parameters that are not referenced, etc.
.TP
.I \-x command
Execute \fIcommand\fP using \fBsystem\fP(3) for each matching file.
The string \fIcommand\fP may contain a \fB%s\fP which will be
substituted with the full path name. A \fB%S\fP (uppercase 'S') will
be substituted with the base name. For example, given the file
\fI/etc/passwd\fP the values for \fB%s\fP and \fB%S\fP would be:
\fI/etc/passwd\fP and \fIpasswd\fP, respectively.
.TP
.I \-C
Do not descend into directories that reside on a file system that is different
than the file system the current command line argument path resides on.
.TP
.I \-D
Follow symbolic links that point to directories when searching for files.
.TP
.I \-F
Follow symbolic links that point to non-directories.
.TP
.I \-L
Follow all symbolic links.
.TP
.I \-V
Writes the version of \fBrh\fP to stderr and exits.
.LP
.SH USAGE
.SS "rh grammar"
This is the grammar that \fBrh\fP will accept.
.LP
.TP
<program> ::=
.RS 6
		<function list> <expression> EOF
.br
		| <function list> <expression> ;
.RE
.LP
.TP
<function list> ::=
.RS 6
		<function list> <function>
.br
		| <function>
.br
		| /* empty */
.RE
.LP
.TP
<function> ::=
.RS 6
		<function heading> { RETURN <expression> ; }
.RE
.LP
.TP
<function heading> ::=
.RS 6
		IDENTIFIER
.br
		| IDENTIFIER ( )
.br
		| IDENTIFIER ( <idlist> )
.RE
.LP
.TP
<idlist> ::=
.RS 6
		<idlist> , IDENTIFIER
.br
		| IDENTIFIER
.RE
.LP
.TP
<expression> ::=
.RS 6
		<expression> ? <expression> : <expression>
.br
		| <expression> || <expression>
.br
		| <expression> && <expression>
.br
		| <expression> | <expression>
.br
		| <expression> ^ <expression>
.br
		| <expression> & <expression>
.br
		| <expression> == <expression>
.br
		| <expression> != <expression>
.br
		| <expression> < <expression>
.br
		| <expression> > <expression>
.br
		| <expression> <= <expression>
.br
		| <expression> >= <expression>
.br
		| <expression> >> <expression>
.br
		| <expression> << <expression>
.br
		| <expression> + <expression>
.br
		| <expression> - <expression>
.br
		| <expression> * <expression>
.br
		| <expression> / <expression>
.br
		| <expression> % <expression>
.br
		| ~ <expression>
.br
		| ! <expression>
.br
		| - <expression>
.br
		| <factor>
.RE
.LP
.TP
<factor> ::=
.RS 6
		( <expression> )
.br
		|    NUMBER
.br
		|    <function call>
.br
		|    IDENTIFIER
.br
		|    [ <datetimespec> ]
.br
		|    STRING
.RE
.LP
.TP
<function call> ::=
.RS 6
		IDENTIFIER
.br
		| IDENTIFIER ( <exprlist> )
.br
		| IDENTIFIER ( )
.RE
.LP
.TP
<exprlist> ::=
.RS 6
		<exprlist> , <expression>
.br
		| <expression>
.RE
.LP
.TP
<datetimespec> ::=
.RS 6
		[ <anything getdate will accept> ]
.RE
.LP
.SS "Search Order"
\fBrh\fP first attempts to read the \fBrh\fP startup file (specified
by the
.SB RHRC
environment variable). Next, any files specified
with \fI-f\fP switches are read (in command line order). Finally,
expressions specified with \fI-e\fP switches are read (in command
line order). If an expression that is out side of a function is not
found then \fBrh\fP will simply print out the names of all files in
the directory trees given on the command line (or the current
directory if no directories were specified).
.LP
\fBrh\fP uses two environment variables,
.SB RHRC
and
.SB RHPATH
, when looking for expression/function files to compile.
.LP
.SB
RHRC
contains the name of a file that is read before any files
specified with the \fI-f\fP switch or expressions specified with the
\fI-e\fP switch. If
.SB RHRC
is not set then `\fI.rhrc\fP' is used.
This file will not be read if the \fI-c\fP switch is specified.
.LP
.SB RHPATH
contains a colon-separated list of directories that
\fBrh\fP searches in order for the
.SB RHRC
file and files specified with the \fI-f\fP switch. If
.SB RHPATH
is not set then the user's home directory is used.
.LP
.SS "The valid constants are:"
.IP NOW
Time, in seconds since the epoch, that \fBrh\fP started execution. It
is normally used to make comparisons with atime, ctime, or mtime.
.IP days
Number of seconds in a day.
.IP hours
Number of seconds in an hour.
.IP weeks
Number of seconds in a week.
.IP "IEXEC IFBLK IFCHR IFDIR IFIFO IFLNK IFMT IFREG IFSOCK"
.PD 0
.IP "IREAD IRGRP IROTH IRUSR IRWXG IRWXO IRWXU ISGID ISUID"
.IP "ISVTX IWGRP IWOTH IWRITE IWUSR IXGRP IXOTH IXUSR"
.PD
see \fIstat\fP(2) for an explanation.
.SS "The valid variables are:"
.LP
.IP depth
This variable is set to the relative depth in the directory search
that the current file is at.
.IP "strlen or baselen"
This is set to the length of the filename. For example, strlen would be
equal to 4 given the file "/tmp/core" because "core" is 4 characters
long.
.IP dirlen
This is set to the length of directory. For example, dirlen would be
equal to 4 given the file "/tmp/core" because "/tmp" is 4 characters
long. Another example: dirlen would be set to 1 given the file "/vmunix"
because "/" is 1 character long.
.IP pathlen
This is set to the length of the entire path (directory and filename).
For example, pathlen would be equal to 9 given the file "/tmp/core".
.IP prune
This variable always returns 0, but as a side-effect causes the
search path to be "cut-short" when evaluated. This can be used to prune the
directory search.
.I prune
is usually used with the ?: operator to conditionally evaluate the prune
variable.
.IP nogroup
This variable is true if the file belongs to a group that is not in the
system group file.
.IP nouser
This variable is true if the file belongs to a user that is not in the
system user file.
.IP "atime ctime dev gid ino mode mtime nlink rdev size uid"
see
.IR stat (2)
for an explanation.
.IP "owner user"
synonyms for uid.
.IP group
synonym for gid.
.IP isblk
This variable is true if the file is a block special file.
.IP ischr
This variable is true if the file is a character special file.
.IP isdir
This variable is true if the file is a directory file.
.IP isfifo
This variable is true if the file is a pipe or FIFO special file.
.IP islnk
This variable is true if the file is a symbolic link.
.IP isreg
This variable is true if the file is a regular file.
.IP issock
This variable is true if the file is a socket.
.SS "The valid C operators are:"
.LP
!  ~  -  *  /  %  +  <  <=  >  >=  ==  !=  &  ^  |  <<  >>  &&  ||  ?:
.LP
Operator precedence, associativity and semantics are the same as
in C.
.SS "Special operators:"
.IP \fB$\fIusername\fP
This operator evaluates to the integer user id of \fIusername\fP.
As a special case the symbol `\fB$$\fP' evaluates to the
uid of the user currently running \fBrh\fP.
.IP \fB@\fIgroupname\fP
This operator evaluates to the integer group id of \fIgroupname\fP.
As a special case the symbol `\fB@@\fP' evaluates to the
gid of the user currently running \fBrh\fP.
.IP \fB\`\fIfilesystem-type\fP
This operator (back-tick) is true if the current file resides on a
filesystem that is of type \fIfilesystem-type\fP.
.IP \fB"\fP*.c\fB"\fP
This operator evaluates to true if the current filename matches
the quoted expression, which is a shell globbing pattern.
The recognized meta-characters are:
.PD 0
.RS
.RS
.IP \fB*\fP
matches any string, including the null string. 
A leading ``.'' is not matched unless the \fI-d\fP switch is specified.
.IP \fB?\fP
matches any single character.
A leading ``.'' is not matched unless the \fI-d\fP switch is specified.
.IP \fB[\fISET\fB]\fP
matches any character in the given set. A range of characters may be
specified by using a hyphen between two characters (e.g. a-l matches
any character lexically between `a' and `l').
.IP \fB[^\fISET\fB]\fP
matches any character not in the given set (ranges are also allowed)
.RE
.LP
`\e' can be used to escape the special meaning of any of the above 
meta-characters.
.br
Only the base name is examined when doing comparisions. Leading paths
are not checked.
.RE
.PD
.IP \fB[\fIdate/time\fB]\fP
The \fIdate/time\fP enclosed in the brackets, ``\fB[]\fP'', will evaluate
to a number of seconds past January 1, 1970, which is suitable for
comparing with atime, mtime or ctime.
.br
There are numerous formats that can be used for the date/time specification.
Some examples are:
.PD 0
.RS
.IP
[Thu Mar 02 19:07:25 CST 1992]
.IP
[Mar 02 19:07:25]
.IP
[03/02 19:07:25]
.IP
[12/30/91]
.IP
[10:15]
.IP
[yesterday]
.IP
[4 days ago]
.RE
.IP
There are many other formats that are acceptable. In addition the special
format ``[yyyy/mm/dd]'' is accepted for compatibility with older versions 
of \fBrh\fP.
.PD
.LP
The special operators have higher precedence than the C operators.
.SS "Lexical conventions:"
.LP
Numbers may be entered in octal by preceding them with a leading zero or
in hexadecimal by preceding them with a leading
.RB ` 0x '
(the
.RB ` x '
may be either lower or upper case). Otherwise numbers are taken to be in
decimal.
.LP
Decimal numbers that are followed by one of
.RB ` mhdw '
with no intervening white space are converted to the number of seconds
in the corresponding number of minutes, hours, days, or weeks,
respectively. Also, decimal numbers that are followed by one of
.RB ` KMG '
with no intervening white space are multiplied by 1024 (kilo-), 1024 *
1024 (mega-), or 1024 * 1024 * 1024 (giga-), respectively.
.LP
Text enclosed in \fB/*\fP and \fB*/\fP will be ignored. This can be
used for commenting \fBrh\fP expression files.
.LP
The start expression may be terminated by either
a ``;'', the end of the file, or the end of the argument.
.LP
.SH EXAMPLES
The following are examples of
.B rh
expressions.
.LP
(mode & 022) && (uid == $joe );
.RS 8
Matches all files that have uid equal to username ``joe'' and
are writable by other people.
.RE
.LP
!uid && (mode & ISUID ) && (mode & 02);
.RS 8
Matches all files that are owned by root (uid==0) and that
have set-uid on execution bit set, and are writable.
.RE
.LP
(size > 10K) && (mode & 0111) && (atime <= NOW-24h);
.RS 8
Finds all executable files larger than 10K that
have not been executed in the last 24 hours.
.RE
.LP
size < ( ("*.c") ? 4K : 32K );
.RS 8
Finds C source files smaller than 4K and other files smaller than 32K.
No other files will match.
.RE
.LP
!(size % 1K);
.RS 8
Matches files that are a multiple of 1K.
.RE
.LP
mtime >= [1982/3/1] && mtime <= [1982/3/31];
.RS 8
Finds files that were modified during March, 1982.
.RE
.LP
strlen >= 4 && strlen <= 10;
.RS 8
This expression will print files whose filenames are between
4 and 10 characters in length.
.RE
.LP
depth > 3;
.RS 8
Matches files that are at a RELATIVE depth of 3 or more.
.RE
.LP
( "tmp" || "bin" ) ? prune : "*.c";
.RS 8
This expression does a search for all "*.c" files, however it will
not look into any directories called "bin" or "tmp". This is because when
such a filename is encountered the prune variable is evaluated, causing
further searching with the current path to stop. The general form of this
would be:
.LP
  ("baddir1" || "baddir2" || ... || "baddirn") ?
.br
.RS 8
prune : <search expr>;
.RE
.RE
.LP
.SH "ADVANCED EXAMPLES"
The following examples show the use of function definitions and other
advanced features of \fBrh\fP.
.LP
Consider:
.LP
.RS 8
dir()
.br
{
.br
    return ( (mode & IFMT) == IFDIR );
.br
}
.br
.RE
.LP
This declares a function that returns true if the current file is a directory
and false otherwise. The function
.I dir
now may be used in other expressions.
.LP
.RS 8
dir() && !mine();
.RE
.LP
This matches files that are directories and are not owned by
the user. This assumes the user has written a mine() function. Since
.I dir
and
.I mine
take no arguments they may be called like:
.LP
.RS 8
dir && !mine;
.RE
.LP
Also when declaring a function that takes no arguments the parenthesis
may be omitted. For example:
.LP
.RS 8
mine
.br
{
.br
    return uid == $joe;
.br
}
.br
.RE
.LP
This declares a function mine, that evaluates true when a file
is owned by user name `joe'. An alternate way to write mine would be:
.LP
.RS 8
mine(who)
.br
{
.br
    return uid == who;
.br
}
.br
.RE
.LP
This would allow mine to be called with an argument, for example:
.LP
.RS 8
mine( $sue ) || mine( $joe );
.RE
.LP
This expression is true of any file owned by user name `sue' or `joe'.
Since the parenthesis are optional for functions that take no arguments,
it would be possible to define functions that can be used exactly like
constants, or handy macros. Suppose the above definition of
.I dir
was placed in a user's 
.I $HOME/.rhrc
Then the command:
.LP
.RS 8
rh -e dir
.RE
.LP
would execute the expression `dir' which will print out all directories.
.LP
.SH "SPECIAL NOTES"
\fBrh\fP functions can be recursive.
.LP
\fBrh\fP works by "compiling" functions and expressions into an internal
stack based language. During this "compilation" process some simple
optimizations are done on expressions.
.RS
.TP
Constant Expression Folding.
Arithmetic expressions are replaced with their value instead of
evaluating the expression for each file \fBrh\fP examines.
.TP
Constant Function Substitution.
Functions that are reduced to a constant (after Constant Expression 
Folding) are never called. When they are referenced in another function
or expression the compiler will use the constant value instead.
.TP
Simple Function Substitution.
Functions that just return one of their parameters are never called.
When they are referenced, the parameter that is to be returned is
"compiled" in the function's place (even if the parameter is a
complex expression).
.RE
.LP
.SH FILES
.TP 10
.SB $HOME/.rhrc
Default file \fBrh\fP compiles of \fI-c\fP switch is not specified
and both
.SB RHRC
and
.SB RHPATH
environment variables are not set.
.LP
.SH ENVIRONMENT
.TP 10
.SB RHPATH
Colon-separated list of directories to search for the \fBrh\fP
start up file and any files specified using the \fI-f\fP switch.
See 
.SM \fBSearch Order\fP
for more information. If
.SB RHPATH
is not set then the user's home directory is used for the search
path.
.TP 10
.SB RHRC
The name of an \fBrh\fP startup file. If 
.SB RHRC
is not set in the environment then \fI.rhrc\fP will be used.
.LP
.SH "SEE ALSO"
.BR chmod (1),
.BR find (1),
.BR ls (1),
.BR stat (2),
.BR getopt (3),
.BR group (5),
.BR passwd (5)
.LP
The C programming language.
.LP
.SH DIAGNOSTICS
Exit status is 0 on success and 1 if an error occurs (permission error,
command executed with \fI-x\fP fails, etc.).
.LP
.SH AUTHOR
Ken Stauffer (University of Calgary)
.br
stauffer@sixk
.LP
.sp
Enhancements, bug fixes, and other modifications by:
.br
Rick Ohnemus (Sterling Software)
.br
rick@Sterling.COM  or uunet!sparky!rick
.br
(Please report bugs in this version to me instead of Ken Stauffer.)
.LP
.SH BUGS
The date operator can be off by a day, if the time on the file is close
to midnight. (is this still true???)
.LP
Functions must be defined before they are referenced.

Added rhcmds.c.































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * rhcmds.c
 *
 * Written by:	Ken Stauffer
 *		Rick Ohnemus	(rick@sterling.com)
 * 
 * This file contains the functions that do the evaluation of
 * the stack program.
 * These functions are simple, and behave like RPN operators, that is
 * they use the last two values on the stack, apply an operator
 * and push the result. Similarly for unary ops.
 *
 */

#if !defined(lint)
static char rcsid[] = "$Id: rhcmds.c,v 1.2 1994/01/26 01:49:21 rick Exp $";
#endif

#include "rh.h"
#include "rhdata.h"
#include "sysinfo.h"

void c_or(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] || Stack[SP - 1];
    SP--;
    return;
}

void c_and(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] && Stack[SP - 1];
    SP--;
    return;
}

void c_le(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] <= Stack[SP - 1];
    SP--;
    return;
}

void c_lt(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] < Stack[SP - 1];
    SP--;
    return;
}

void c_ge(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] >= Stack[SP - 1];
    SP--;
    return;
}

void c_gt(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] > Stack[SP - 1];
    SP--;
    return;
}

void c_ne(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] != Stack[SP - 1];
    SP--;
    return;
}

void c_eq(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] == Stack[SP - 1];
    SP--;
    return;
}

void c_bor(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] | Stack[SP - 1];
    SP--;
    return;
}

void c_band(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] & Stack[SP - 1];
    SP--;
    return;
}

void c_bxor(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] ^ Stack[SP - 1];
    SP--;
    return;
}

void c_lshift(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] << Stack[SP - 1];
    SP--;
    return;
}

void c_rshift(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] >> Stack[SP - 1];
    SP--;
    return;
}

void c_plus(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] + Stack[SP - 1];
    SP--;
    return;
}

void c_mul(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] * Stack[SP - 1];
    SP--;
    return;
}

void c_minus(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2]  -  Stack[SP-1];
    SP--;
    return;
}

void c_div(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] / Stack[SP - 1];
    SP--;
    return;
}

void c_mod(instr_value *v)
{
    Stack[SP - 2] = Stack[SP - 2] % Stack[SP - 1];
    SP--;
    return;
}


/* unary instructions */

void c_not(instr_value *v)
{
    Stack[SP - 1] = !Stack[SP - 1];
    return;
}

void c_bnot(instr_value *v)
{
    Stack[SP - 1] = ~Stack[SP - 1];
    return;
}

void c_uniminus(instr_value *v)
{
    Stack[SP - 1] = -Stack[SP - 1];
    return;
}


/* trinary operator ?: */

void c_qm(instr_value *v)
{
    PC = (Stack[SP - 1]) ? PC : v->pc;
    SP--;
    return;
}

void c_colon(instr_value *v)
{
    PC = v->pc;
    return;
}


/* accessing a parameter */

void c_param(instr_value *v)
{
    Stack[SP++] = Stack[FP + v->param];
    return;
}


/* calling a function */

void c_func(instr_value *v)
{
    Stack[SP++] = PC;
    Stack[SP++] = FP;
    PC = v->pc;
    FP = SP - (StackProgram[PC].value.pc + 2);
    return;
}


/* returning from a function */

void c_return(instr_value *v)
{
    PC = Stack[SP - 3];
    FP = Stack[SP - 2];
    Stack[SP - (3 + v->param)] = Stack[SP - 1];
    SP -= (2 + v->param);
    return;
}


/* operand functions */

void c_nop(instr_value *v)
{
    return;
}

void c_date(instr_value *v)
{
    Stack[SP++] = v->di->seconds;
    return;
}

void c_number(instr_value *v)
{
    Stack[SP++] = v->l;
    return;
}

void c_atime(instr_value *v)
{
    Stack[SP++] = attr.buf->st_atime;
    return;
}

void c_ctime(instr_value *v)
{
    Stack[SP++] = attr.buf->st_ctime;
    return;
}

void c_dev(instr_value *v)
{
    Stack[SP++] = attr.buf->st_dev;
    return;
}

void c_gid(instr_value *v)
{
    Stack[SP++] = attr.buf->st_gid;
    return;
}

void c_ino(instr_value *v)
{
    Stack[SP++] = attr.buf->st_ino;
    return;
}

void c_isblk(instr_value *v)
{
    Stack[SP++] = S_ISBLK(attr.buf->st_mode);
    return;
}

void c_ischr(instr_value *v)
{
    Stack[SP++] = S_ISCHR(attr.buf->st_mode);
    return;
}

void c_isdir(instr_value *v)
{
    Stack[SP++] = S_ISDIR(attr.buf->st_mode);
    return;
}

void c_isfifo(instr_value *v)
{
    Stack[SP++] = S_ISFIFO(attr.buf->st_mode);
    return;
}

void c_islnk(instr_value *v)
{
    Stack[SP++] = S_ISLNK(attr.buf->st_mode);
    return;
}

void c_isreg(instr_value *v)
{
    Stack[SP++] = S_ISREG(attr.buf->st_mode);
    return;
}

void c_issock(instr_value *v)
{
    Stack[SP++] = S_ISSOCK(attr.buf->st_mode);
    return;
}

void c_mode(instr_value *v)
{
    Stack[SP++] = attr.buf->st_mode;
    return;
}

void c_mtime(instr_value *v)
{
    Stack[SP++] = attr.buf->st_mtime;
    return;
}

void c_nlink(instr_value *v)
{
    Stack[SP++] = attr.buf->st_nlink;
    return;
}

void c_rdev(instr_value *v)
{
    Stack[SP++] = attr.buf->st_rdev;
    return;
}

void c_size(instr_value *v)
{
    Stack[SP++] = attr.buf->st_size;
    return;
}

void c_uid(instr_value *v)
{
    Stack[SP++] = attr.buf->st_uid;
    return;
}

void c_nogroup(instr_value *v)
{
    Stack[SP++] = getgigid(attr.buf->st_gid) == (group_info *) NULL;
    return;
}

void c_nouser(instr_value *v)
{
    Stack[SP++] = getuiuid(attr.buf->st_uid) == (user_info *) NULL;
    return;
}

void c_depth(instr_value *v)
{
    Stack[SP++] = attr.depth;
    return;
}

void c_prune(instr_value *v)
{
    Stack[SP++] = 0;
    attr.prune = TRUE;
    return;
}


/* calculate the filename length */

void c_baselen(instr_value *v)
{
    register char *	c;
    register int	len;
    
    len = 0;
    for (c = attr.fname; *c != '\0'; c++) {
	if (*c == '/') {
	    len = 0;
	}
	else {
	    ++len;
	}
    }
    Stack[SP++] = len;
    return;
}


/* calculate the length of the directory portion of the path */

void c_dirlen(instr_value *v)
{
    register char *	c;
    register char *	slash = (char *) NULL;
    register int	len;

    for (c = attr.fname; *c != '\0'; c++) {
	if (*c == '/') {
	    slash = c;
	}
    }
    if (slash == (char *) NULL) {
	len = 0;
    }
    else {
	len = (slash == attr.fname) ? 1 : (slash - attr.fname);
    }
    Stack[SP++] = len;
    return;
}


/* calculate length of the entire path */

void c_pathlen(instr_value *v)
{
    Stack[SP++] = strlen(attr.fname);
    return;
}


/*
 * c_fncase:
 *    This function does an exact file name match.
 */

void c_fncase(instr_value *v)
{
    char *	tail;
    
    if ((tail = strrchr(attr.fname, '/')) == (char *) NULL) {
	tail = attr.fname;
    }
    else {
	++tail;
    }

    Stack[SP++] = (strcmp(tail, v->pattern) == 0);

    return;
}

/*
 * c_fnigncase:
 *    This function does a case-insensitive file name match.
 */

void c_fnigncase(instr_value *v)
{
    char *	tail;
    
    if ((tail = strrchr(attr.fname, '/')) == (char *) NULL) {
	tail = attr.fname;
    }
    else {
	++tail;
    }

    Stack[SP++] = (strcasecmp(tail, v->pattern) == 0);

    return;
}

/*
 * c_pattern:
 *    This function implements the regular expression stuff.
 *    str is the the actual '\0' terminated string that occured in the
 *    expression (eg "*.BAK" ), minus the quotes "".
 */

void c_pattern(instr_value *v)
{
    extern int	wildmat(unsigned char *text, unsigned char *pattern,
			int dotspec, int igncase);

    char *	tail;

    
    if ((tail = strrchr(attr.fname, '/')) == (char *) NULL) {
	tail = attr.fname;
    }
    else {
	++tail;
    }

    Stack[SP++] = wildmat((unsigned char *) tail, (unsigned char *) v->pattern,
			  attr.dot_special, attr.ignore_case);

    return;
}

#if defined(sun)
void c_fstype(instr_value *v)
{
    fs_info *	p;

    Stack[SP] = 0;

    if (v->fstype != (char *) NULL) {
	if ((p = getfidev(attr.buf->st_dev)) != (fs_info *) NULL) {
	    if (strcmp(v->fstype, p->fs_type) == 0) {
		Stack[SP] = 1;
	    }
	}
    }
    SP++;
    return;
}
#else
void c_fstype(instr_value *v)
{
    Stack[SP++] = 0;
    return;
}
#endif

void c_samefs(instr_value *v)
{
    Stack[SP++] = (v->dev == (dev_t) 0) ? 1 : (attr.buf->st_dev == v->dev);
    return;
}

void c_syscall(instr_value *v)
{
    switch (v->sym->value) {

    case RH_SYSCALL_EXIT:
	exit(Stack[--SP]);

    default:
	error("%lu: unknown syscall", v->sym->value);
    }

    Stack[SP - v->sym->num_params] = Stack[SP - 1];
    SP -= v->sym->num_params - 1;

    return;
}

Added rhcmds.h.



















































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * rhcmds.h
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 *
 * rcsid: $Id: rhcmds.h,v 1.2 1994/01/25 22:39:30 rick Exp $
 */

#if !defined(__RHCMDS_H__)
#define __RHCMDS_H__

extern void c_and(instr_value *);
extern void c_atime(instr_value *);
extern void c_band(instr_value *);
extern void c_baselen(instr_value *);
extern void c_bnot(instr_value *);
extern void c_bor(instr_value *);
extern void c_bxor(instr_value *);
extern void c_colon(instr_value *);
extern void c_ctime(instr_value *);
extern void c_date(instr_value *);
extern void c_depth(instr_value *);
extern void c_dev(instr_value *);
extern void c_dirlen(instr_value *);
extern void c_div(instr_value *);
extern void c_eq(instr_value *);
extern void c_fncase(instr_value *);
extern void c_fnigncase(instr_value *);
extern void c_func(instr_value *);
extern void c_fstype(instr_value *);
extern void c_ge(instr_value *);
extern void c_gid(instr_value *);
extern void c_gt(instr_value *);
extern void c_ino(instr_value *);
extern void c_isblk(instr_value *);
extern void c_ischr(instr_value *);
extern void c_isdir(instr_value *);
extern void c_isfifo(instr_value *);
extern void c_islnk(instr_value *);
extern void c_isreg(instr_value *);
extern void c_issock(instr_value *);
extern void c_le(instr_value *);
extern void c_lshift(instr_value *);
extern void c_lt(instr_value *);
extern void c_minus(instr_value *);
extern void c_mod(instr_value *);
extern void c_mode(instr_value *);
extern void c_mtime(instr_value *);
extern void c_mul(instr_value *);
extern void c_ne(instr_value *);
extern void c_nlink(instr_value *);
extern void c_nogroup(instr_value *);
extern void c_nop(instr_value *);
extern void c_not(instr_value *);
extern void c_nouser(instr_value *);
extern void c_number(instr_value *);
extern void c_or(instr_value *);
extern void c_param(instr_value *);
extern void c_pathlen(instr_value *);
extern void c_pattern(instr_value *);
extern void c_plus(instr_value *);
extern void c_prune(instr_value *);
extern void c_qm(instr_value *);
extern void c_rdev(instr_value *);
extern void c_return(instr_value *);
extern void c_rshift(instr_value *);
extern void c_samefs(instr_value *);
extern void c_size(instr_value *);
extern void c_syscall(instr_value *);
extern void c_uid(instr_value *);
extern void c_uniminus(instr_value *);

#endif	/* __RHCMDS_H__ */

Added rhdata.c.



























































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * rhdata.c
 *
 * Written by:	Ken Stauffer
 *		Rick Ohnemus	(rick@sterling.com)
 *
 * Global Functions: rhfinish, rhinit
 */

#if !defined(lint)
static char rcsid[] = "$Id: rhdata.c,v 1.1 1994/01/12 19:38:27 rick Exp $";
#endif

#include "rh.h"
#include "rhcmds.h"
#include "rhgram.h"

#if !defined(S_IREAD)
#  define S_IREAD	0400
#  define S_IWRITE	0200
#  define S_IEXEC	0100
#endif

#if !defined(S_IRWXU)
#  define S_IRWXU	0700
#  define S_IRWXG	0070
#  define S_IRWXO	0007
#endif

#if !defined(S_IRUSR)
#  define S_IRGRP	0040
#  define S_IROTH	0004
#  define S_IRUSR	0400
#  define S_IWGRP	0020
#  define S_IWOTH	0002
#  define S_IWUSR	0200
#  define S_IXGRP	0010
#  define S_IXOTH	0001
#  define S_IXUSR	0100
#endif

int Issue_warnings = FALSE;

int Input_file;
char *Input_expr_end;
char *Input_expr_ptr;
char *Input_name;

node *Start_expr = (node *) NULL;

symbol *Symbol_table;

instruction StackProgram[MAX_PROG_LEN];
int PC;
int startPC;

long Stack[MAX_STACK_LEN + 3];
int SP;
int FP;

struct runtime attr;

/*
 * NOW - 6 months (actually just 180 days)
 */

long past_time;

/*
 * symbol table entry for NOW
 */

symbol *now;
 
/*
 * Do not move the location of NOW in the following table
 * without changing the code in rhinit() that sets 'now'.
 */

static symbol init_syms[] = {
    { "NOW",      NUMBER,      FALSE,  0,                 c_number },
    { "IEXEC",    NUMBER,      FALSE,  S_IEXEC,           c_number },
    { "IFBLK",    NUMBER,      FALSE,  S_IFBLK,           c_number },
    { "IFCHR",    NUMBER,      FALSE,  S_IFCHR,           c_number },
    { "IFDIR",    NUMBER,      FALSE,  S_IFDIR,           c_number },
#if defined(S_IFIFO)
    { "IFIFO",    NUMBER,      FALSE,  S_IFIFO,           c_number },
#endif
#if defined(S_IFLNK)
    { "IFLNK",    NUMBER,      FALSE,  S_IFLNK,           c_number },
#endif
    { "IFMT",     NUMBER,      FALSE,  S_IFMT,            c_number },
    { "IFREG",    NUMBER,      FALSE,  S_IFREG,           c_number },
#if defined(S_IFSOCK)
    { "IFSOCK",   NUMBER,      FALSE,  S_IFSOCK,          c_number },
#endif
    { "IREAD",    NUMBER,      FALSE,  S_IREAD,           c_number },
    { "IRGRP",    NUMBER,      FALSE,  S_IRGRP,           c_number },
    { "IROTH",    NUMBER,      FALSE,  S_IROTH,           c_number },
    { "IRUSR",    NUMBER,      FALSE,  S_IRUSR,           c_number },
    { "IRWXG",    NUMBER,      FALSE,  S_IRWXG,           c_number },
    { "IRWXO",    NUMBER,      FALSE,  S_IRWXO,           c_number },
    { "IRWXU",    NUMBER,      FALSE,  S_IRWXU,           c_number },
    { "ISGID",    NUMBER,      FALSE,  S_ISGID,           c_number },
    { "ISUID",    NUMBER,      FALSE,  S_ISUID,           c_number },
    { "ISVTX",    NUMBER,      FALSE,  S_ISVTX,           c_number },
    { "IWGRP",    NUMBER,      FALSE,  S_IWGRP,           c_number },
    { "IWOTH",    NUMBER,      FALSE,  S_IWOTH,           c_number },
    { "IWRITE",   NUMBER,      FALSE,  S_IWRITE,          c_number },
    { "IWUSR",    NUMBER,      FALSE,  S_IWUSR,           c_number },
    { "IXGRP",    NUMBER,      FALSE,  S_IXGRP,           c_number },
    { "IXOTH",    NUMBER,      FALSE,  S_IXOTH,           c_number },
    { "IXUSR",    NUMBER,      FALSE,  S_IXUSR,           c_number },
    { "atime",    TIME_FIELD,  TRUE,   0,                 c_atime },
    { "baselen",  BLTIN,       FALSE,  0,                 c_baselen },
    { "ctime",    TIME_FIELD,  TRUE,   0,                 c_ctime },
    { "days",     NUMBER,      FALSE,  24 * 60 * 60,      c_number },
    { "depth",    BLTIN,       FALSE,  0,                 c_depth },
    { "dev",      FIELD,       TRUE,   0,                 c_dev },
    { "dirlen",   BLTIN,       FALSE,  0,                 c_dirlen },
    { "gid",      FIELD,       TRUE,   0,                 c_gid },
    { "group",    FIELD,       TRUE,   0,                 c_gid },
    { "hours",    NUMBER,      FALSE,  60 * 60,           c_number },
    { "ino",      FIELD,       TRUE,   0,                 c_ino },
    { "isblk",    BLTIN,       TRUE,   0,                 c_isblk },
    { "ischr",    BLTIN,       TRUE,   0,                 c_ischr },
    { "isdir",    BLTIN,       TRUE,   0,                 c_isdir },
#if defined(S_IFIFO)
    { "isfifo",   BLTIN,       TRUE,   0,                 c_isfifo },
#endif
#if defined(S_IFLNK)
    { "islnk",    BLTIN,       TRUE,   0,                 c_islnk },
#endif
    { "isreg",    BLTIN,       TRUE,   0,                 c_isreg },
#if defined(S_IFSOCK)
    { "issock",   BLTIN,       TRUE,   0,                 c_issock },
#endif
    { "mode",     FIELD,       TRUE,   0,                 c_mode },
    { "mtime",    TIME_FIELD,  TRUE,   0,                 c_mtime },
    { "nlink",    FIELD,       TRUE,   0,                 c_nlink },
    { "nogroup",  BLTIN,       TRUE,   0,                 c_nogroup },
    { "nouser",   BLTIN,       TRUE,   0,                 c_nouser },
    { "owner",    FIELD,       TRUE,   0,                 c_uid },
    { "pathlen",  BLTIN,       FALSE,  0,                 c_pathlen },
    { "prune",    BLTIN,       FALSE,  0,                 c_prune },
    { "rdev",     FIELD,       TRUE,   0,                 c_rdev },
    { "return",   RETURN,      FALSE,  0,                 c_return },
    { "size",     FIELD,       TRUE,   0,                 c_size },
    { "strlen",   BLTIN,       FALSE,  0,                 c_baselen },
    { "uid",      FIELD,       TRUE,   0,                 c_uid },
    { "user",     FIELD,       TRUE,   0,                 c_uid },
    { "weeks",    NUMBER,      FALSE,  24 * 60 * 60 * 7,  c_number },
    { "exit",	  SYSTEM_CALL, FALSE,  RH_SYSCALL_EXIT,	  c_syscall,
	  (symbol *) NULL, 0, 1 }
};
 
void rhinit(void)
{
    extern time_t time();
    
    int i;
    
    
    Symbol_table = &init_syms[0];
    
    for (i = 0; i < ((sizeof(init_syms) / sizeof(symbol)) - 1); i++) {
        init_syms[i].next = &init_syms[i + 1];
    }
    init_syms[i].next = (symbol *) NULL;
    
    /*
     * get symbol table entry for NOW variable
     * 		could use this instead:  now = locatename("NOW");
     */

    now = &init_syms[0];

    /* Initialize the NOW variable to the time right now */

    now->value = (long) time((time_t *) 0);
    
    past_time = now->value - (6L * 30L * 24L * 60L * 60L);
    
    return;
}
 
void rhfinish(void)
{
    symbol *	s;

    extern void free_tree(node *);

    
#if defined(DEBUG)
    for (s = Symbol_table; s != (symbol *) NULL; s = s->next) {
        if (s->type == PARAM) {
            error("symbol table is corrupt!");
        }
        else if (s->type != FUNCTION) {
            break;
        }
        else {
            free_tree(s->tree);
            s->tree = (node *) NULL;
        }
    }
#else
    while (Symbol_table->type == PARAM || Symbol_table->type == FUNCTION) {
        if (Symbol_table->type == PARAM) {
            error("symbol table is corrupt!");
        }
        s = Symbol_table;
        Symbol_table = Symbol_table->next;
        free_tree(s->tree);
        free(s->name);
        free((char *) s);
    }
#endif
    
    return;
}

Added rhdata.h.











































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * rhdata.h
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 *
 * rcsid: $Id: rhdata.h,v 1.1 1994/01/12 19:38:51 rick Exp $
 */

#if !defined(__RHDATA_H__)
#define __RHDATA_H__

extern int	Issue_warnings;

extern int	Input_file;
extern char *	Input_expr_end;
extern char *	Input_expr_ptr;
extern char *	Input_name;

extern node *	Start_expr;

extern symbol *	Symbol_table;

extern instruction	StackProgram[];
extern int		PC;
extern int		startPC;

extern long		Stack[];
int			SP;
int			FP;

extern struct runtime	attr;

extern time_t		past_time;

extern symbol *		now;

#endif	/* __RHDATA_H__ */

Added rhdir.c.





























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
 * rhdir.c
 *
 * Written by:	Ken Stauffer
 *		Rick Ohnemus	(rick@sterling.com)
 * 
 * This file contains the "non portable" stuff dealing with
 * directories.
 *
 * Global Functions: ftrw, graphic, printentry, printformat
 *
 * Static Functions: fwt1, handle_file, ls_date
 */

#if !defined(lint)
static char rcsid[] = "$Id: rhdir.c,v 1.2 1994/01/25 22:37:55 rick Exp $";
#endif

#include <ctype.h>
#include "rh.h"
#include "rhdata.h"
#include "errmsg.h"
#include "sysinfo.h"

#define user_index(b)	((000777 & (b)) >> 6) + ((b) & S_ISUID ? 8 : 0) 
#define group_index(b)	((000077 & b) >> 3) + ((b) & S_ISGID ? 8 : 0)
#define all_index(b)	((000007 & (b)) + (((b) & S_ISVTX) ? 8 : 0))
#define ftype_index(b)	((b) >> 13)

#define hassubdir(b)	((b)->st_nlink > 2)

#define isdot(s)	((s)[1] == '\0' && (s)[0] == '.')
#define isdotdot(s)	((s)[2] == '\0' && (s)[1] == '.' && (s)[0] == '.')

#if !defined(S_IFLNK)
#  define    lstat        stat
#endif

#define isproper(m)    (!attr.prune && S_ISDIR(m))

#if defined(POSIX_DIRECTORY_LIBRARY)
#  include <dirent.h>
#else
#  include <sys/dir.h>
#endif

extern char *ctime();


static char *ftype[] = { "p", "c" , "d" , "b" , "-" , "l" , "s" , "t" };

static char *perm[] = {
    "---", "--x", "-w-", "-wx" , "r--", "r-x", "rw-", "rwx" ,
    "--S", "--s", "-wS", "-ws" , "r-S", "r-s", "rwS", "rws"
};

static char *perm2[] = {
    "---", "--x", "-w-", "-wx" , "r--", "r-x", "rw-", "rwx" ,
    "--T", "--t", "-wT", "-wt" , "r-T", "r-t", "rwT", "rwt"
};

#if defined(DEBUG)
extern int	Trace;
#endif
 
/*
 * graphic:
 *	Returns copy of input string with all non-graphics characters
 *	replaced with '?'.
 */

char *graphic(const char *name)
{
    static char new_name[MAXPATHLEN + 1];
    
    char *p = new_name;
    
    while (*name != '\0') {
	if (isascii(*name) && isgraph(*name))
	    *p++ = *name++;
	else {
	    *p++ = '?';
	    name++;
	}
    }
    *p = '\0';
    return new_name;
}
 
/*
 * ls_date:
 *	Returns a time converted to a string that may resemble
 *	what ls(1) outputs.
 *
 *	If full_date is	true then the time string from ctime
 *	is copied and returned.
 *	If full_date is false and the time is more than 180 days
 *	in the past or more than 5 minutes into the future then
 *	the month, day,and year are returned.
 *	Otherwise, return the month, day, time, and year.
 */

static char *ls_date(time_t ftime,
		     int full_date)
{
#if SUNOS_4
    char *ts = ctime(&ftime);
#else
    char *ts = ctime((long *) &ftime);
#endif
    static char date[32];
    
    if (full_date) {
	char *to = date;
	while (*ts != '\n' && *ts != '\0') {
	    *to++ = *ts++;
	}
	*to = '\0';
    }
    else if (ftime < past_time || ftime > (now->value + 5L * 60L)) {
	(void) sprintf(date, "%6.6s  %4.4s", ts + 4, ts + 20);
    }
    else {
	(void) sprintf(date, "%12.12s", ts + 4);
    }
    
    return date;
}
 
/*
 * printformat:
 *	Outputs file information using the format specified
 *	using the '-p' switch.
 */

void printformat(struct stat *buf,
		 const char *name,
		 int depth)
{
    static char *hex_digits = "0123456789abcdef";

    unsigned char	c;
    char *		fp;
    group_info *	gi;
    int			i;
    char *		p;
    user_info *		ui;
    
    
    for (fp = attr.format; *fp != '\0'; fp++) {
	if (*fp != '%') {
	    if (*fp != '\\') {
		(void) putc(*fp, stdout);
		continue;
	    }
	    fp++;
	    
	    switch (*fp) {
		
	    case '\0':
		(void) putc('\\', stdout);
		break;
		
	    case '0':
		if (*(fp + 1) == 'x' || *(fp + 1) == 'X') {
		    if (isxdigit(*(fp + 2))) {
			fp += 2;
			for (i = c = 0; i < 2 && isxdigit(*fp); i++, fp++) {
			    c <<= 4;
			    if (isupper(*fp))
				*fp = tolower(*fp);
			    c += strchr(hex_digits, *fp) - hex_digits;
			}
			(void) putc((char) c, stdout);
			fp--;
		    }
		    else
			(void) putc(*fp, stdout);
		}
		else if (*(fp + 1) >= '0' && *(fp + 1) <= '7') {
		    fp++;
		    for (i = c = 0;
			 i < 3 && *fp >= '0' && *fp <= '7';
			 i++, fp++) {
			c <<= 3;
			c += *fp - '0';
		    }
		    (void) putc((char) c, stdout);
		    fp--;
		}
		else
		    (void) putc(*fp, stdout);
		break;
		
	    case 'a':
		(void) putc('\007', stdout);
		break;
		
	    case 'b':
		(void) putc('\b', stdout);
		break;
		
	    case 'f':
		(void) putc('\f', stdout);
		break;
		
	    case 'n':
		(void) putc('\n', stdout);
		break;
		
	    case 'r':
		(void) putc('\r', stdout);
		break;
		
	    case 't':
		(void) putc('\t', stdout);
		break;
		
	    case 'v':
		(void) putc('\v', stdout);
		break;
		
	    default:
		(void) putc(*fp, stdout);
		break;
	    }
	    
	    continue;
	}
	
	fp++;
	
	switch (*fp) {
	    
	case '\0':
	    error("format character missing after '%%'");
	    /*NOTREACHED*/
	    
	case 'a':	/* atime as ls date */
	    (void) printf("%s", ls_date(buf->st_atime, attr.full_date));
	    break;
	    
	case 'A':	/* atime in seconds */
	    (void) printf("%10lu", (u_long) buf->st_atime);
	    break;

#if BSD
	case 'b':	/* number blocks allocated to file */
	    (void) printf("%10ld", buf->st_blocks);
	    break;
	    
	case 'B':	/* optimal block size for file system i/o */
	    (void) printf("%6ld", buf->st_blksize);
	    break;
#endif
	    
	case 'c':	/* ctime as ls date */
	    (void) printf("%s", ls_date(buf->st_ctime, attr.full_date));
	    break;
	    
	case 'C':	/* ctime in seconds */
	    (void) printf("%10lu", (u_long) buf->st_ctime);
	    break;

	case 'd':	/* parent directory */
	    if ((p = strrchr(name, '/')) == (char *) NULL)
		(void) putc('.', stdout);
	    else if (p == name)
		(void) putc('/', stdout);
	    else {
		*p = '\0';
		(void) printf("%s", attr.graphic ? graphic(name) : name);
		*p = '/';
	    }
	    break;
	    
	case 'D':	/* depth of file relative to search start point */
	    (void) printf("%4d", depth);
	    break;
	    
	case 'g':	/* numeric group id  */
	    (void) printf("%8d", buf->st_gid);
	    break;
	    
	case 'G':	/* symbolic group id */
	    if ((gi = getgigid(buf->st_gid)) == (group_info *) NULL) {
		(void) printf("%8d", buf->st_gid);
	    }
	    else {
		(void) printf("%-8.8s", gi->name);
	    }
	    break;
	    
	case 'i':	/* inode number */
	    (void) printf("%6lu", buf->st_ino);
	    break;
	    
	case 'l':	/* number of links */
	    (void) printf("%3d", buf->st_nlink);
	    break;
	    
	case 'm':	/* mtime as ls date */
	    (void) printf("%s", ls_date(buf->st_mtime, attr.full_date));
	    break;
	    
	case 'M':	/* mtime in seconds */
	    (void) printf("%10lu", (u_long) buf->st_mtime);
	    break;

	case 'p':	/* permissions bits in ls format */
	    (void) printf("%s%s%s%s", 
			  ftype[ftype_index(buf->st_mode)],
			  perm[user_index(buf->st_mode)],
			  perm[group_index(buf->st_mode)],
			  perm2[all_index(buf->st_mode)]);
	    break;
	    
	case 'P':	/* numeric permission bits */
	    (void) printf("%#6o", (u_int) buf->st_mode);
	    break;
	    
	case 'r':	/* minor device if device file */
	    if (S_ISCHR(buf->st_mode) || S_ISBLK(buf->st_mode))
		(void) printf("%4u", (u_int) minor(buf->st_rdev));
	    else
		(void) printf("xxxx");
	    break;
	    
	case 'R':	/* major device if device file */
	    if (S_ISCHR(buf->st_mode) || S_ISBLK(buf->st_mode))
		(void) printf("%4u", (u_int) major(buf->st_rdev));
	    else
		(void) printf("xxxx");
	    break;
	    
	case 'n':	/* full path of file (relative to start point) */
	case 's':
	    (void) printf("%s", attr.graphic ? graphic(name) : name);
	    break;
	    
	case 'N':	/* base name of file */
	case 'S':
	    if ((p = strrchr(name, '/')) == (char *) NULL)
		(void) printf(attr.graphic ? graphic(name) : name);
	    else
		(void) printf(attr.graphic ? graphic(p + 1) : (p + 1));
	    break;
	    
	case 'u':	/* numeric user id */
	    (void) printf("%8d", buf->st_uid);
	    break;
	    
	case 'U':	/* symbolic user id */
	    if ((ui = getuiuid(buf->st_uid)) == (user_info *) NULL) {
		(void) printf("%8d", buf->st_uid);
	    }
	    else {
		(void) printf("%-8.8s", ui->name);
	    }
	    break;
	    
	case 'w':	/* minor device of device where file resides */
	    (void) printf("%02x", (u_int) minor(buf->st_dev));
	    break;
	    
	case 'W':	/* major device of device where file resides */
	    (void) printf("%02x", (u_int) major(buf->st_dev));
	    break;
	    
	case 'y':	/* numeric device of device where file resides */
	    (void) printf("%04x", (u_int) buf->st_dev);
	    break;
	    
	case 'z':	/* file size in bytes */
	    (void) printf("%9ld", buf->st_size);
	    break;
	    
	default:
	    (void) putc(*fp, stdout);
	    break;
	}
    }
    
    (void) putc('\n', stdout);
    return;
}
 
/*
 * printentry:
 *	Display filename,permissions and size in a '/bin/ls' like
 *	format. If verbose is non-zero then more information is
 *	displayed.
 * uses the macros:
 *	user_index(b)
 *	group_index(b)
 *	all_index(b)
 *	ftype_index(b)
 *
 */

void printentry(int verbose,
		int str_owner,
		int full_date,
		struct stat *buf,
		const char *name)
{
    group_info *	gi;
    char		gid_buf[20];
    user_info *		ui;
    char		uid_buf[20];
    
    
    if (verbose) {
	if (str_owner) {
	    if ((ui = getuiuid(buf->st_uid)) == (user_info *) NULL) {
		(void) sprintf(uid_buf, "%8d", buf->st_uid);
	    }
	    else {
		(void) sprintf(uid_buf, "%-8.8s", ui->name);
	    }
	    if ((gi = getgigid(buf->st_gid)) == (group_info *) NULL){ 
		(void) sprintf(gid_buf, "%8d", buf->st_gid);
	    }
	    else {
		(void) sprintf(gid_buf, "%-8.8s", gi->name);
	    }
	}
	else {
	    (void) sprintf(uid_buf, "%8d", buf->st_uid);
	    (void) sprintf(gid_buf, "%8d", buf->st_gid);
	}
	if (S_ISCHR(buf->st_mode) || S_ISBLK(buf->st_mode)) {
	    (void) printf("%s%s%s%s %s %s %3d,%3d   %s %s\n",
			  ftype[ftype_index(buf->st_mode)],
			  perm[user_index(buf->st_mode)],
			  perm[group_index(buf->st_mode)],
			  perm2[all_index(buf->st_mode)],
			  uid_buf,
			  gid_buf,
			  major(buf->st_rdev),
			  minor(buf->st_rdev),
			  ls_date(buf->st_mtime, full_date),
			  attr.graphic ? graphic(name) : name);
	}
	else {
	    (void) printf("%s%s%s%s %s %s %9ld %s %s\n",
			  ftype[ftype_index(buf->st_mode)],
			  perm[user_index(buf->st_mode)],
			  perm[group_index(buf->st_mode)],
			  perm2[all_index(buf->st_mode)],
			  uid_buf,
			  gid_buf,
			  buf->st_size,
			  ls_date(buf->st_mtime, full_date),
			  attr.graphic ? graphic(name) : name);
	}
    }
    else {
	if (S_ISCHR(buf->st_mode) || S_ISBLK(buf->st_mode)) {
	    (void) printf("%s%s%s%s %3d,%3d   %s\n",
			  ftype[ftype_index(buf->st_mode)],
			  perm[user_index(buf->st_mode)],
			  perm[group_index(buf->st_mode)],
			  perm2[all_index(buf->st_mode)],
			  major(buf->st_rdev),
			  minor(buf->st_rdev),
			  attr.graphic ? graphic(name) : name);
	}
	else {
	    (void) printf("%s%s%s%s %9ld %s\n",
			  ftype[ftype_index(buf->st_mode)],
			  perm[user_index(buf->st_mode)],
			  perm[group_index(buf->st_mode)],
			  perm2[all_index(buf->st_mode)],
			  buf->st_size,
			  attr.graphic ? graphic(name) : name);
	}
    }
    
    return;
}
 
/*
 * handle_file:
 *	If symbolic links:
 *		lstat() file
 *		if failure then print warning and return
 *		if success and file is a link then
 *			stat() the file
 *			if directory and following directory links
 *				update file name with link value
 *				update stat buffer if not link to current dir.
 *			if regular file and following file links
 *				update stat buffer
 *		call examination function to process file
 *			
 *
 *	If no symbolic links:	(life is much simpler)
 *		stat() file
 *		if failure then print warning and return
 *		if success then call examination function to process file
 */

static int handle_file(void)
{
    struct stat	statbuf;

#if defined(DEBUG)
    if (Trace) {
	(void) fprintf(stderr, "handle_file(\"%s\")\n", attr.fname);
    }
#endif

#if defined(S_IFLNK)
    if (lstat(attr.fname, attr.buf) < 0) {
	warning("%s: %m", attr.fname);
	attr.num_errors++;
	return 0;
    }
    if ((attr.follow_dir || attr.follow_file) && S_ISLNK(attr.buf->st_mode)) {
	if (stat(attr.fname, &statbuf) == 0) {
	    if (S_ISDIR(statbuf.st_mode)) {
		if (attr.follow_dir) {
		    int   len;
		    char  linkbuf[MAXPATHLEN];

		    len = readlink(attr.fname, linkbuf, sizeof(linkbuf));
		    if (len != 1 || *linkbuf != '.') {
			*attr.buf = statbuf;
		    }
		}
	    }
	    else if (attr.follow_file) {
		*attr.buf = statbuf;
	    }
	}
	else {
	    attr.num_errors++;
	}
    }
#else
    if (stat(attr.fname, attr.buf) < 0) {
	warning("%s: %m", attr.fname);
	attr.num_errors++;
	return 0;
    }
#endif

    (*attr.func)();
    return 1;
}
 
/*
 * fwt1:
 *	'p' points to the end of the string in attr.fname
 *
 *	2 versions of this routine currently live here:
 *	"new-style", for systems with a BSD or POSIX-style
 *	directory library, and systems without such a
 *	directory library. They both differ in
 *	the manner in which they access directories.
 *	Any changes needed to work on another system
 *	should only have to made for this routine.
 *
 *	Below is the "directory library" version of fwt1()
 *
 */

#if defined(POSIX_DIRECTORY_LIBRARY) || defined(BSD)

static void fwt1(int depth,
		 int dashr,
		 char *p,
		 int stat_all,
		 int nlink)
{
#if defined(POSIX_DIRECTORY_LIBRARY)
    struct dirent *	dp;
#  define DIRTYPE   struct dirent
#else
    struct direct *	dp;
#  define DIRTYPE  struct direct
#endif

    DIR *		dirp;
    char *		q;
    char *		s;

    
    if (depth == 0) {
	if (dashr) {
	    warning("%s: file tree depth exceeded", attr.fname);
	}
	return;
    }
    attr.depth++;
    
    if ((dirp = opendir(attr.fname)) == (DIR *) NULL) {
	warning("%s: %m", attr.fname);
	return;
    }
    
    nlink -= 2;

    if (stat_all) {
	if (attr.stat_all) {
	    while ((dp = readdir(dirp)) != (DIRTYPE *) NULL) {
		if (isdot(dp->d_name) || isdotdot(dp->d_name)) {
		    continue;
		}
		s = p;
		q = dp->d_name;
		while ((*s++ = *q++) != '\0') {
		    ;		/* empty loop */
		}
		s--;

		attr.prune = FALSE;
		if (!handle_file()) {
		    continue;
		}
		if (isproper(attr.buf->st_mode)) {
		    *s++ = '/';
		    *s = '\0';
		    fwt1(depth - 1, dashr, s,
			 attr.stat_all | hassubdir(attr.buf),
			 attr.buf->st_nlink);
		}
	    }
	}
	else {
	    while ((dp = readdir(dirp)) != (DIRTYPE *) NULL) {
		if (isdot(dp->d_name) || isdotdot(dp->d_name)) {
		    continue;
		}
		s = p;
		q = dp->d_name;
		while ((*s++ = *q++) != '\0') {
		    ;		/* empty loop */
		}
		s--;
		
		attr.prune = FALSE;
		if (!handle_file()) {
		    continue;
		}
		if (isproper(attr.buf->st_mode)) {
		    *s++ = '/';
		    *s = '\0';
		    fwt1(depth - 1, dashr, s,
			 attr.stat_all | hassubdir(attr.buf),
			 attr.buf->st_nlink);
		    nlink--;
		    if (nlink == 0) {
			break;
		    }
		}
	    }
	    if (dp != (DIRTYPE *) NULL) {
		while ((dp = readdir(dirp)) != (DIRTYPE *) NULL) {
		    if (isdot(dp->d_name) || isdotdot(dp->d_name)) {
			continue;
		    }
		    s = p;
		    q = dp->d_name;
		    while ((*s++ = *q++) != '\0') {
			;		/* empty loop */
		    }
		    (*attr.func)();
		}
	    }
	}
    }
    else {
	while ((dp = readdir(dirp)) != (DIRTYPE *) NULL) {
	    if (isdot(dp->d_name) || isdotdot(dp->d_name)) {
		continue;
	    }
	    s = p;
	    q = dp->d_name;
	    while ((*s++ = *q++) != '\0') {
		;		/* empty loop */
	    }
	    (*attr.func)();
	}
    }
    
    (void) closedir(dirp);
    attr.depth--;
    *p = '\0';
    
    return;
}
#else

/*
 * fwt1:
 *	This function does the same thing as fwt1() above, but is
 *	meant for systems without a directory library, that does
 *	directory reading "by hand".
 *
 *    Below is the "no directory library" version of fwt1()
 *
 */

static void fwt1(int depth,
		 int dashr,
		 char *p,
		 int stat_all,
		 int nlink)
{
    FILE *		dirp;
    struct direct	dp;
    char *		q;
    char *		s;
    
    if (depth == 0) {
	if (dashr) {
	    warning("%s: file tree depth exceeded", attr.fname);
	}
	return;
    }
    attr.depth++;
    
    if ((dirp = fopen(attr.fname, "r")) == (FILE *) NULL) {
	warning("%s: %m", attr.fname);
	return;
    }
    
    nlink -= 2;

    if (stat_all) {
	if (attr.stat_all) {
	    while (fread(&dp, sizeof(struct direct), 1, dirp) == 1) {
		if (isdot(dp.d_name) || isdotdot(dp.d_name)) {
		    continue;
		}
		s = p;
		q = dp.d_name;
		while ((*s++ = *q++) != '\0') {
		    ;		/* empty loop */
		}
		s--;

		attr.prune = FALSE;
		if (!handle_file()) {
		    continue;
		}
		if (isproper(attr.buf->st_mode)) {
		    *s++ = '/';
		    *s = '\0';
		    fwt1(depth - 1, dashr, s,
			 attr.stat_all | hassubdir(attr.buf),
			 attr.buf->st_nlink);
		}
	    }
	}
	else {
	    while (fread(&dp, sizeof(struct direct), 1, dirp) == 1) {
		if (isdot(dp.d_name) || isdotdot(dp.d_name)) {
		    continue;
		}
		s = p;
		q = dp.d_name;
		while ((*s++ = *q++) != '\0') {
		    ;		/* empty loop */
		}
		s--;
		
		attr.prune = FALSE;
		if (!handle_file()) {
		    continue;
		}
		if (isproper(attr.buf->st_mode)) {
		    *s++ = '/';
		    *s = '\0';
		    fwt1(depth - 1, dashr, s,
			 attr.stat_all | hassubdir(attr.buf),
			 attr.buf->st_nlink);
		    nlink--;
		    if (nlink == 0) {
			break;
		    }
		}
	    }
	    if (dp != 1) {
		while (fread(&dp, sizeof(struct direct), 1, dirp) == 1) {
		    if (isdot(dp.d_name) || isdotdot(dp.d_name)) {
			continue;
		    }
		    s = p;
		    q = dp.d_name;
		    while ((*s++ = *q++) != '\0') {
			;		/* empty loop */
		    }
		    (*attr.func)();
		}
	    }
	}
    }
    else {
	while (fread(&dp, sizeof(struct direct), 1, dirp) == 1) {
	    if (isdot(dp.d_name) || isdotdot(dp.d_name)) {
		continue;
	    }
	    s = p;
	    q = dp.d_name;
	    while ((*s++ = *q++) != '\0') {
		;		/* empty loop */
	    }
	    (*attr.func)();
	}
    }
    
    (void) fclose(dirp);
    attr.depth--;
    *p = '\0';

    return;
}

#endif
 
/*
 * ftrw:
 *	Entry point to do the search, ftrw is a front end
 *	to the recursive fwt1.
 *	ftrw() initializes some global variables and
 *	builds the initial filename string which is passed to
 *	fwt1().
 */

void ftrw(char *file,
	  void (*fn)(),
	  int depth,
	  int dashr,
	  int samefsPC)
{
    char	filebuf[MAXPATHLEN + 1];
    char *	name_end;
    struct stat	statbuf;
    
    
#if defined(DEBUG)
    if (Trace) {
	(void) fprintf(stderr, "ftrw(\"%s\")\n", file);
    }
#endif

    attr.prune = FALSE;
    attr.depth = 0;
    attr.func = fn;
    name_end = attr.fname = filebuf;
    attr.buf = &statbuf;

    if (*file == '\0') {
	*name_end = '\0';
	if (stat(".", attr.buf) == 0) {
	    if (samefsPC != -1) {
		StackProgram[samefsPC].value.dev = attr.buf->st_dev;
	    }
	    fwt1(depth, dashr, name_end,
		 attr.stat_all | hassubdir(attr.buf), attr.buf->st_nlink);
	}
	else {
	    warning(".: %m");
	    attr.num_errors++;
	}
    }
    else {
	while ((*name_end++ = *file++) != '\0') {
	    ;		/* empty loop */
	}
	name_end--;
	
	if (!handle_file()) {
	    return;
	}
	
	if (*(name_end - 1) != '/') {
	    *name_end++ = '/';
	    *name_end = '\0';
	}
	if (isproper(attr.buf->st_mode)) {
	    if (samefsPC != -1) {
		StackProgram[samefsPC].value.dev = attr.buf->st_dev;
	    }
	    fwt1(depth, dashr, name_end,
		 attr.stat_all | hassubdir(attr.buf), attr.buf->st_nlink);
	}
    }
    
    return;
}

Added rhgram.c.



































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
#ifndef lint
static const char yysccsid[] = "@(#)yaccpar	1.9 (Berkeley) 02/21/93";
#endif

#include <stdlib.h>
#include <string.h>

#define YYBYACC 1
#define YYMAJOR 1
#define YYMINOR 9
#define YYPATCH 20070509

#define YYEMPTY (-1)
#define yyclearin    (yychar = YYEMPTY)
#define yyerrok      (yyerrflag = 0)
#define YYRECOVERING (yyerrflag != 0)

extern int yyparse(void);

static int yygrowstack(void);
#define yyparse rh_parse
#define yylex rh_lex
#define yyerror rh_error
#define yychar rh_char
#define yyval rh_val
#define yylval rh_lval
#define yydebug rh_debug
#define yynerrs rh_nerrs
#define yyerrflag rh_errflag
#define yyss rh_ss
#define yyssp rh_ssp
#define yyvs rh_vs
#define yyvsp rh_vsp
#define yylhs rh_lhs
#define yylen rh_len
#define yydefred rh_defred
#define yydgoto rh_dgoto
#define yysindex rh_sindex
#define yyrindex rh_rindex
#define yygindex rh_gindex
#define yytable rh_table
#define yycheck rh_check
#define yyname rh_name
#define yyrule rh_rule
#define YYPREFIX "rh_"
#line 2 "rhgram.y"

/*
 * rhgram.y
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 */

#if !defined(lint)
static char rcsid[] = "$Id: rhgram.y,v 1.3 1994/01/26 16:17:54 rick Exp $";
#endif

#include "rh.h"
#include "rhcmds.h"
#include "rhdata.h"
#include "alloc.h"
#include "errmsg.h"

extern char *	Line;		/* line being parsed */
extern int	Line_num;	/* line number */
extern int	Line_pos;	/* position in line */

static node *	copy_tree(node *);	/* copy a tree */
extern void	free_tree(node *);	/* free expression tree memory */ 
static node *	make_node(void (*)(), instr_value *);	/* make a tree node */
static int	pattern(const char *);	/* is string a shell fn pattern? */
extern void	rh_error(const char *);	/* output an error message and exit */
extern int	rh_lex(void);		/* lexer */
extern void	rh_lex_finish(void);	/* lexer clean-up */

static instr_value	value;

#line 35 "rhgram.y"
typedef union {
    char *              str;
    symbol *		sym;
    node *		tree;
    long                value;
    void	      (*func)(instr_value *);
    date_info *		di;
    struct {
	char *	line;
	int	line_num;
	int	line_pos;
    } line_info;
} YYSTYPE;
#line 93 "y.tab.c"
#define BLTIN 257
#define DATESPEC 258
#define EQ 259
#define FIELD 260
#define FILENAME_PATTERN 261
#define FILETIME_FIELD 262
#define FSTYPE 263
#define FUNCTION 264
#define GE 265
#define IDENTIFIER 266
#define LE 267
#define LOGAND 268
#define LOGOR 269
#define LSHIFT 270
#define NE 271
#define NUMBER 272
#define PARAM 273
#define RETURN 274
#define RSHIFT 275
#define SYSTEM_CALL 276
#define TIME_FIELD 277
#define UNIMINUS 278
#define YYERRCODE 256
short rh_lhs[] = {                                        -1,
    0,    0,    0,    7,    7,    9,    8,   10,   11,    8,
    8,    5,    5,    5,    5,    4,    4,   12,   12,    1,
    1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
    1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
    1,    1,   13,    3,   14,    3,    3,    3,    3,    3,
    3,    3,    3,    3,    3,    3,    3,    6,    6,    2,
    2,    2,
};
short rh_len[] = {                                         2,
    1,    2,    3,    0,    2,    0,    3,    0,    0,    7,
    1,    0,    1,    3,    1,    5,    4,    0,    1,    1,
    5,    3,    3,    3,    3,    3,    3,    3,    3,    3,
    3,    3,    3,    3,    3,    3,    3,    3,    3,    2,
    2,    2,    0,    3,    0,    3,    3,    1,    1,    1,
    1,    1,    1,    1,    1,    1,    1,    0,    3,    0,
    1,    3,
};
short rh_defred[] = {                                      4,
    0,    0,   11,   51,   55,   52,   49,   56,   50,   43,
    0,   48,   53,   45,   57,    0,    0,    0,    0,    0,
   20,    5,    0,    0,    0,    0,   54,   42,   41,   40,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    3,    0,   44,    0,    7,    0,   46,   47,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,   37,   38,   39,    0,    0,   19,
    0,   15,   13,    0,    0,   59,    0,    0,    9,    0,
    0,    0,    0,   17,    0,   14,   16,   10,
};
short rh_dgoto[] = {                                       1,
   20,   79,   21,   55,   84,   53,    2,   22,   24,   25,
   95,   81,   23,   26,
};
short rh_sindex[] = {                                      0,
    0,  -21,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,  312,  312,  312,  312,  648,
    0,    0,  -38, -116,  -29,  -38,    0,    0,    0,    0,
  677,  312,  312,  312,  312,  312,  312,  312,  312,  312,
  312,  312,  312,  312,  312,  312,  312,  312,  312,  312,
    0,  312,    0, -258,    0, -253,    0,    0,  803,  -22,
  -22,  761,  747,  -37,  803,  -37,  706,  602,  775,  789,
  -22,  -22,  -33,  -33,    0,    0,    0,  720,  -15,    0,
  312,    0,    0,  -14,  312,    0,  312,  458,    0, -249,
  720,  720, -107,    0, -116,    0,    0,    0,
};
short rh_rindex[] = {                                      0,
    0,   22,    0,    0,    0,    0,    0,    0,    0,    0,
   28,    0,    0,    0,    0,    0,    0,    0,    0,   47,
    0,    0,    1,    0,    0,    1,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,  -10,    0,  355,    0,   -9,    0,    0,  383,  138,
  164,  261,  148,   96,  399,  109,    0,  420,  408,  410,
  291,  331,   41,   69,    0,    0,    0,   -8,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
  360,   -4,    0,    0,    0,    0,    0,    0,
};
short rh_gindex[] = {                                      0,
 1058,    0,    0,  -46,    0,   24,    0,    0,    0,    0,
    0,    0,    0,    0,
};
#define YYTABLESIZE 1145
short rh_table[] = {                                      50,
   58,   52,   82,   50,   48,   46,   54,   47,   48,   49,
   56,   17,   83,   49,   50,   80,   96,   97,   19,   48,
   46,    1,   47,   16,   49,   86,   89,   54,   87,   90,
   60,   12,   61,   60,   12,   61,   62,   58,   58,   62,
   35,   58,   58,   58,   58,   58,    2,   58,   98,   57,
    0,    0,    0,    0,    0,    0,    0,    0,   58,   58,
   58,    0,   58,   58,   54,   54,    0,    8,   36,   54,
   54,    0,   54,    0,   54,    0,    0,    0,   35,    0,
    0,   35,    0,   35,   35,   35,   54,   54,    0,   54,
   54,    0,    0,    0,   58,   34,    0,    0,   35,   35,
   35,    0,   35,   35,   18,    0,   36,    0,   33,   36,
    0,   36,   36,   36,    0,    0,    0,    0,    0,    0,
    0,   54,    0,    0,   58,   58,   36,   36,   36,    0,
   36,   36,    0,   34,   35,    0,   34,   32,    0,   34,
    0,    0,    0,    0,    0,    0,   33,   22,    0,   33,
    6,   54,   33,   34,   34,   34,    0,   34,   34,    0,
    0,    0,   36,   31,   35,   35,   33,   33,   33,    0,
   33,   33,    0,    0,    0,   32,    0,    0,   32,    0,
    0,   32,    0,    0,    0,    0,    0,    0,   22,   34,
    0,   22,   36,   36,    0,   32,   32,   32,    0,   32,
   32,   31,   33,    0,   31,   22,   22,   31,    0,    0,
   22,    0,    0,    0,    0,    0,    0,    0,    0,   34,
   34,   31,   31,   31,    0,   31,   31,    0,    0,    0,
    0,   32,   33,   33,    3,    4,    5,    0,    6,    7,
    8,    9,   10,    0,   11,    0,    0,   37,    0,    0,
   12,   13,   39,    0,   14,   15,    0,   31,    0,   58,
   23,   32,   32,    0,    0,   58,    0,   58,   58,   58,
   58,   58,   22,    0,    0,   58,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,   54,   31,   31,    0,
   29,    0,   54,    0,   54,   54,   54,   54,   54,   35,
    0,   23,   54,    0,   23,   35,    0,   35,   35,   35,
   35,   35,    0,    0,    0,   35,    0,    0,   23,   23,
    0,    0,    0,   23,    0,    0,    0,   36,   29,    0,
   30,   29,    0,   36,   29,   36,   36,   36,   36,   36,
    0,    0,    0,   36,   17,    0,    0,    0,   29,   29,
   29,   19,   29,   29,   34,    0,   16,    0,    0,   21,
   34,    0,   34,   34,   34,   34,   34,   33,   30,    0,
   34,   30,    0,   33,   30,   33,   33,   33,   33,   33,
    0,    0,   27,   33,   29,   23,    0,   18,   30,   30,
   30,    0,   30,   30,   18,    0,   32,    0,   28,   18,
   21,    0,   32,   21,   32,   32,   32,   25,   32,   26,
    0,    0,    0,    0,   29,   29,   22,   21,   21,   24,
   27,    0,   31,   27,   30,    0,   27,    0,   31,    0,
   31,   31,   31,    0,   31,    0,   28,   18,    0,   28,
   27,   27,   28,    0,    0,   27,    0,   26,   25,    0,
   26,   25,    0,   26,   30,   30,   28,   28,    0,    0,
   24,   28,    0,   24,    0,   25,   25,   26,   26,    0,
   25,    0,   26,    0,    0,    0,   27,   24,   24,    0,
   18,    0,   24,    0,   21,    0,    0,    0,    0,    0,
    0,    0,   28,    0,   50,   43,    0,    0,    0,   48,
   46,   25,   47,   26,   49,    0,   27,   27,    0,    0,
    0,    0,    0,    0,    0,    0,   93,   44,    0,   45,
   40,    0,   28,   28,    0,    0,    0,    0,   23,   23,
    0,   25,   25,   26,   26,    0,    0,    0,    0,    0,
    0,    0,    0,   24,   24,    0,    0,    0,    0,   29,
    0,   42,    0,    0,    0,   29,    0,   29,   29,   29,
    0,   29,    0,    0,    0,    0,    0,    0,    4,    5,
    0,    6,    7,    8,    9,   10,    0,   27,    0,    0,
    0,   41,   94,   12,   13,    0,    0,   14,   15,   30,
    0,    0,    0,    0,    0,   30,    0,   30,   30,   30,
    0,   30,    0,    0,    0,    0,    0,    0,    0,    0,
    0,   18,   18,    0,   18,   18,   18,   18,   18,    0,
   18,    0,    0,    0,    0,    0,   18,   18,    0,    0,
   18,   18,    0,    0,    0,    0,    0,    0,   50,   43,
    0,   27,    0,   48,   46,    0,   47,    0,   49,    0,
   27,   27,    0,   27,    0,    0,    0,   28,    0,    0,
    0,   44,    0,   45,    0,    0,   28,   28,    0,   28,
    0,    0,    0,    0,    0,   25,   25,   26,   26,    0,
    0,    0,    0,    0,   50,   43,    0,   24,   24,   48,
   46,    0,   47,    0,   49,   42,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,   51,   44,    0,   45,
   40,    0,    0,   50,   43,    0,   32,   58,   48,   46,
    0,   47,   33,   49,   34,   35,   36,   37,   38,    0,
    0,    0,   39,    0,    0,    0,   44,    0,   45,   40,
    0,   42,   50,   43,    0,    0,    0,   48,   46,    0,
   47,    0,   49,    0,    0,    0,   50,   43,    0,    0,
    0,   48,   46,   85,   47,   44,   49,   45,   40,    0,
   42,   41,    0,    0,    0,    0,    0,    0,    0,   44,
    0,   45,   40,   50,   43,    0,    0,    0,   48,   46,
    0,   47,    0,   49,    0,    0,    0,   50,   43,   42,
   41,    0,   48,   46,    0,   47,   44,   49,   45,    0,
    0,   50,   43,   42,    0,    0,   48,   46,    0,   47,
   44,   49,   45,    0,    0,   50,    0,    0,    0,   41,
   48,   46,    0,   47,   44,   49,   45,    0,    0,   50,
   42,    0,    0,   41,   48,   46,    0,   47,   44,   49,
   45,    0,    0,    0,   42,    0,    0,    0,    0,    0,
   32,    0,   44,    0,   45,    0,   33,    0,   34,    0,
   41,   37,   38,    0,    0,    0,   39,    0,    0,    0,
    0,    0,    0,    0,   41,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,   32,    0,    0,    0,
    0,    0,   33,    0,   34,   35,   36,   37,   38,    0,
    0,    0,   39,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,   32,    0,    0,    0,    0,
    0,   33,    0,   34,   35,   36,   37,   38,    0,    0,
    0,   39,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,   32,    0,    0,    0,    0,    0,
   33,    0,   34,   35,   36,   37,   38,    0,   32,    0,
   39,    0,    0,    0,   33,    0,   34,   35,   36,   37,
   38,    0,    0,    0,   39,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,   32,    0,    0,    0,    0,
    0,   33,    0,   34,   35,    0,   37,   38,    0,   32,
    0,   39,    0,    0,    0,   33,    0,   34,    0,    0,
   37,   38,    0,   32,    0,   39,    0,    0,    0,   33,
    0,   34,    0,    0,   37,   38,    0,   32,    0,   39,
    0,    0,    0,   33,    0,   34,    0,    0,   37,   38,
    0,    0,    0,   39,    0,    0,    0,   33,    0,   34,
    0,    0,   37,   28,   29,   30,   31,   39,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,   59,
   60,   61,   62,   63,   64,   65,   66,   67,   68,   69,
   70,   71,   72,   73,   74,   75,   76,   77,    0,   78,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,   88,    0,
    0,    0,   91,    0,   92,
};
short rh_check[] = {                                      37,
    0,   40,  256,   37,   42,   43,  123,   45,   42,   47,
   40,   33,  266,   47,   37,  274,  266,  125,   40,   42,
   43,    0,   45,   45,   47,   41,   41,    0,   44,   44,
   41,   41,   41,   44,   44,   44,   41,   37,   38,   44,
    0,   41,   42,   43,   44,   45,    0,   47,   95,   26,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   58,   59,
   60,   -1,   62,   63,   37,   38,   -1,   40,    0,   42,
   43,   -1,   45,   -1,   47,   -1,   -1,   -1,   38,   -1,
   -1,   41,   -1,   43,   44,   45,   59,   60,   -1,   62,
   63,   -1,   -1,   -1,   94,    0,   -1,   -1,   58,   59,
   60,   -1,   62,   63,  126,   -1,   38,   -1,    0,   41,
   -1,   43,   44,   45,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   94,   -1,   -1,  124,  125,   58,   59,   60,   -1,
   62,   63,   -1,   38,   94,   -1,   41,    0,   -1,   44,
   -1,   -1,   -1,   -1,   -1,   -1,   38,    0,   -1,   41,
  123,  124,   44,   58,   59,   60,   -1,   62,   63,   -1,
   -1,   -1,   94,    0,  124,  125,   58,   59,   60,   -1,
   62,   63,   -1,   -1,   -1,   38,   -1,   -1,   41,   -1,
   -1,   44,   -1,   -1,   -1,   -1,   -1,   -1,   41,   94,
   -1,   44,  124,  125,   -1,   58,   59,   60,   -1,   62,
   63,   38,   94,   -1,   41,   58,   59,   44,   -1,   -1,
   63,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  124,
  125,   58,   59,   60,   -1,   62,   63,   -1,   -1,   -1,
   -1,   94,  124,  125,  256,  257,  258,   -1,  260,  261,
  262,  263,  264,   -1,  266,   -1,   -1,  270,   -1,   -1,
  272,  273,  275,   -1,  276,  277,   -1,   94,   -1,  259,
    0,  124,  125,   -1,   -1,  265,   -1,  267,  268,  269,
  270,  271,  125,   -1,   -1,  275,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,  259,  124,  125,   -1,
    0,   -1,  265,   -1,  267,  268,  269,  270,  271,  259,
   -1,   41,  275,   -1,   44,  265,   -1,  267,  268,  269,
  270,  271,   -1,   -1,   -1,  275,   -1,   -1,   58,   59,
   -1,   -1,   -1,   63,   -1,   -1,   -1,  259,   38,   -1,
    0,   41,   -1,  265,   44,  267,  268,  269,  270,  271,
   -1,   -1,   -1,  275,   33,   -1,   -1,   -1,   58,   59,
   60,   40,   62,   63,  259,   -1,   45,   -1,   -1,    0,
  265,   -1,  267,  268,  269,  270,  271,  259,   38,   -1,
  275,   41,   -1,  265,   44,  267,  268,  269,  270,  271,
   -1,   -1,    0,  275,   94,  125,   -1,   33,   58,   59,
   60,   -1,   62,   63,   40,   -1,  259,   -1,    0,   45,
   41,   -1,  265,   44,  267,  268,  269,    0,  271,    0,
   -1,   -1,   -1,   -1,  124,  125,  269,   58,   59,    0,
   38,   -1,  259,   41,   94,   -1,   44,   -1,  265,   -1,
  267,  268,  269,   -1,  271,   -1,   38,  126,   -1,   41,
   58,   59,   44,   -1,   -1,   63,   -1,   38,   41,   -1,
   41,   44,   -1,   44,  124,  125,   58,   59,   -1,   -1,
   41,   63,   -1,   44,   -1,   58,   59,   58,   59,   -1,
   63,   -1,   63,   -1,   -1,   -1,   94,   58,   59,   -1,
  126,   -1,   63,   -1,  125,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   94,   -1,   37,   38,   -1,   -1,   -1,   42,
   43,   94,   45,   94,   47,   -1,  124,  125,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   59,   60,   -1,   62,
   63,   -1,  124,  125,   -1,   -1,   -1,   -1,  268,  269,
   -1,  124,  125,  124,  125,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,  124,  125,   -1,   -1,   -1,   -1,  259,
   -1,   94,   -1,   -1,   -1,  265,   -1,  267,  268,  269,
   -1,  271,   -1,   -1,   -1,   -1,   -1,   -1,  257,  258,
   -1,  260,  261,  262,  263,  264,   -1,  266,   -1,   -1,
   -1,  124,  125,  272,  273,   -1,   -1,  276,  277,  259,
   -1,   -1,   -1,   -1,   -1,  265,   -1,  267,  268,  269,
   -1,  271,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,  257,  258,   -1,  260,  261,  262,  263,  264,   -1,
  266,   -1,   -1,   -1,   -1,   -1,  272,  273,   -1,   -1,
  276,  277,   -1,   -1,   -1,   -1,   -1,   -1,   37,   38,
   -1,  259,   -1,   42,   43,   -1,   45,   -1,   47,   -1,
  268,  269,   -1,  271,   -1,   -1,   -1,  259,   -1,   -1,
   -1,   60,   -1,   62,   -1,   -1,  268,  269,   -1,  271,
   -1,   -1,   -1,   -1,   -1,  268,  269,  268,  269,   -1,
   -1,   -1,   -1,   -1,   37,   38,   -1,  268,  269,   42,
   43,   -1,   45,   -1,   47,   94,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   59,   60,   -1,   62,
   63,   -1,   -1,   37,   38,   -1,  259,   41,   42,   43,
   -1,   45,  265,   47,  267,  268,  269,  270,  271,   -1,
   -1,   -1,  275,   -1,   -1,   -1,   60,   -1,   62,   63,
   -1,   94,   37,   38,   -1,   -1,   -1,   42,   43,   -1,
   45,   -1,   47,   -1,   -1,   -1,   37,   38,   -1,   -1,
   -1,   42,   43,   58,   45,   60,   47,   62,   63,   -1,
   94,  124,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   60,
   -1,   62,   63,   37,   38,   -1,   -1,   -1,   42,   43,
   -1,   45,   -1,   47,   -1,   -1,   -1,   37,   38,   94,
  124,   -1,   42,   43,   -1,   45,   60,   47,   62,   -1,
   -1,   37,   38,   94,   -1,   -1,   42,   43,   -1,   45,
   60,   47,   62,   -1,   -1,   37,   -1,   -1,   -1,  124,
   42,   43,   -1,   45,   60,   47,   62,   -1,   -1,   37,
   94,   -1,   -1,  124,   42,   43,   -1,   45,   60,   47,
   62,   -1,   -1,   -1,   94,   -1,   -1,   -1,   -1,   -1,
  259,   -1,   60,   -1,   62,   -1,  265,   -1,  267,   -1,
  124,  270,  271,   -1,   -1,   -1,  275,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,  124,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,  259,   -1,   -1,   -1,
   -1,   -1,  265,   -1,  267,  268,  269,  270,  271,   -1,
   -1,   -1,  275,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,  259,   -1,   -1,   -1,   -1,
   -1,  265,   -1,  267,  268,  269,  270,  271,   -1,   -1,
   -1,  275,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,  259,   -1,   -1,   -1,   -1,   -1,
  265,   -1,  267,  268,  269,  270,  271,   -1,  259,   -1,
  275,   -1,   -1,   -1,  265,   -1,  267,  268,  269,  270,
  271,   -1,   -1,   -1,  275,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,  259,   -1,   -1,   -1,   -1,
   -1,  265,   -1,  267,  268,   -1,  270,  271,   -1,  259,
   -1,  275,   -1,   -1,   -1,  265,   -1,  267,   -1,   -1,
  270,  271,   -1,  259,   -1,  275,   -1,   -1,   -1,  265,
   -1,  267,   -1,   -1,  270,  271,   -1,  259,   -1,  275,
   -1,   -1,   -1,  265,   -1,  267,   -1,   -1,  270,  271,
   -1,   -1,   -1,  275,   -1,   -1,   -1,  265,   -1,  267,
   -1,   -1,  270,   16,   17,   18,   19,  275,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   32,
   33,   34,   35,   36,   37,   38,   39,   40,   41,   42,
   43,   44,   45,   46,   47,   48,   49,   50,   -1,   52,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
   -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,   81,   -1,
   -1,   -1,   85,   -1,   87,
};
#define YYFINAL 1
#ifndef YYDEBUG
#define YYDEBUG 1
#endif
#define YYMAXTOKEN 278
#if YYDEBUG
char *rh_name[] = {
"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"'!'",0,0,0,"'%'","'&'",0,"'('","')'","'*'","'+'","','","'-'",0,"'/'",0,0,0,0,0,
0,0,0,0,0,"':'","';'","'<'",0,"'>'","'?'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,"'^'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,"'{'","'|'","'}'","'~'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"BLTIN","DATESPEC","EQ","FIELD",
"FILENAME_PATTERN","FILETIME_FIELD","FSTYPE","FUNCTION","GE","IDENTIFIER","LE",
"LOGAND","LOGOR","LSHIFT","NE","NUMBER","PARAM","RETURN","RSHIFT","SYSTEM_CALL",
"TIME_FIELD","UNIMINUS",
};
char *rh_rule[] = {
"$accept : program",
"program : functions",
"program : functions expression",
"program : functions expression ';'",
"functions :",
"functions : functions function",
"$$1 :",
"function : IDENTIFIER $$1 function_body",
"$$2 :",
"$$3 :",
"function : IDENTIFIER $$2 '(' idlist ')' $$3 function_body",
"function : error",
"idlist :",
"idlist : IDENTIFIER",
"idlist : idlist ',' IDENTIFIER",
"idlist : error",
"function_body : '{' return expression ';' '}'",
"function_body : '{' return expression '}'",
"return :",
"return : RETURN",
"expression : factor",
"expression : expression '?' expression ':' expression",
"expression : expression LOGOR expression",
"expression : expression LOGAND expression",
"expression : expression '|' expression",
"expression : expression '^' expression",
"expression : expression '&' expression",
"expression : expression EQ expression",
"expression : expression NE expression",
"expression : expression '<' expression",
"expression : expression '>' expression",
"expression : expression LE expression",
"expression : expression GE expression",
"expression : expression RSHIFT expression",
"expression : expression LSHIFT expression",
"expression : expression '+' expression",
"expression : expression '-' expression",
"expression : expression '*' expression",
"expression : expression '/' expression",
"expression : expression '%' expression",
"expression : '~' expression",
"expression : '!' expression",
"expression : '-' expression",
"$$4 :",
"factor : FUNCTION $$4 paramlist",
"$$5 :",
"factor : SYSTEM_CALL $$5 paramlist",
"factor : '(' expression ')'",
"factor : NUMBER",
"factor : FILENAME_PATTERN",
"factor : FSTYPE",
"factor : BLTIN",
"factor : FIELD",
"factor : PARAM",
"factor : IDENTIFIER",
"factor : DATESPEC",
"factor : FILETIME_FIELD",
"factor : TIME_FIELD",
"paramlist :",
"paramlist : '(' exprlist ')'",
"exprlist :",
"exprlist : expression",
"exprlist : exprlist ',' expression",
};
#endif
#if YYDEBUG
#include <stdio.h>
#endif

/* define the initial stack-sizes */
#ifdef YYSTACKSIZE
#undef YYMAXDEPTH
#define YYMAXDEPTH  YYSTACKSIZE
#else
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 500
#define YYMAXDEPTH  500
#endif
#endif

#define YYINITSTACKSIZE 500

int      yydebug;
int      yynerrs;
int      yyerrflag;
int      yychar;
short   *yyssp;
YYSTYPE *yyvsp;
YYSTYPE  yyval;
YYSTYPE  yylval;

/* variables for the parser stack */
static short   *yyss;
static short   *yysslim;
static YYSTYPE *yyvs;
static int      yystacksize;
#line 726 "rhgram.y"
 
void rh_error(const char *msg)
{
    extern const int		yyleng;
    extern const char * const	yytext;

#if defined(DEBUG2)
    (void) fprintf(stderr, "yyleng = %d, yytext = '%s'\n", yyleng, yytext);
#endif

    if (yyleng) {
	if (Line_num < 0) {
	    (void) fprintf(stderr, "%s: %s: %d: %s\n",
			   Program_name, Input_name, -Line_num, msg);
	}
	else {
	    (void) fprintf(stderr, "%s: %s: %d: %s at '%s'\n",
			   Program_name, Input_name, Line_num, msg, yytext);
	}
	(void) fprintf(stderr, " Line: %s\nError: %*s\n",
		       Line, Line_pos - (yyleng - 1), "^");
    }
    else {
	(void) fprintf(stderr, "%s: %s: %s\n", Program_name, Input_name, msg);
	(void) fprintf(stderr, "%s: unexpected end of file after line %d\n",
		       Program_name, Line_num);
	(void) fprintf(stderr, "Last Line: %s\n", Line);
    }
    exit(1);
    /*NOTREACHED*/
}
 
static node *make_node(void	      (*func)(),
		       instr_value *	value)
{
    node *	new_node;

    new_node = ALLOCATE(sizeof(node));
    new_node->code.func = func;
    new_node->code.value = *value;
    new_node->left = new_node->right = (node *) NULL;
    new_node->middle = (node *) NULL;
    new_node->count = -1;
    new_node->trees = (node **) NULL;

    return new_node;
}
 
node *copy_tree(node *tree)
{
    node *	new_tree;


    if (tree == (node *) NULL) {
	return tree;
    }

    new_tree = ALLOCATE(sizeof(node));

    new_tree->code.func = tree->code.func;
    if (new_tree->code.func == c_date) {
	new_tree->code.value.di = ALLOCATE(sizeof(date_info));
	*(new_tree->code.value.di) = *tree->code.value.di;
    }
    else if (new_tree->code.func == c_pattern
	     || new_tree->code.func == c_fncase
	     || new_tree->code.func == c_fnigncase) {
	new_tree->code.value.pattern = SAVESTR(tree->code.value.pattern);
    }
    else {
	new_tree->code.value = tree->code.value;
    }
    
    new_tree->left = copy_tree(tree->left);
    new_tree->right = copy_tree(tree->right);
    new_tree->middle = copy_tree(tree->middle);
    
    new_tree->count = tree->count;
    if (new_tree->count <= 0) {
	new_tree->trees = (node **) NULL;
    }
    else {
	int	i;
	
	new_tree->trees = ALLOCATE(sizeof(node *) * new_tree->count);
	for (i = 0; i < new_tree->count; i++) {
	    new_tree->trees[i] = copy_tree(tree->trees[i]);
	}
    }
    
    return new_tree;
}
 
void free_tree(node *tree)
{
    if (tree != (node *) NULL) {
	free_tree(tree->left);
	free_tree(tree->right);
	free_tree(tree->middle);
	if (tree->code.func == c_date) {
	    free((char *) tree->code.value.di);
	}
	else if (tree->code.func == c_fstype) {
	    free(tree->code.value.fstype);
	}
	else if (tree->code.func == c_pattern
		 || tree->code.func == c_fncase
		 || tree->code.func == c_fnigncase) {
	    free(tree->code.value.pattern);
	}
	else if (tree->code.func == UNRES_FUNC && tree->count > 0) {
	    int i;
	    
	    for (i = 0; i < tree->count; i++) {
		free_tree(tree->trees[i]);
	    }
	    free((char *) tree->trees);
	}
	free((char *) tree);
    }

    return;
}
 
static int pattern(const char *p)
{
    for (/*void*/ ; *p != '\0' ; ++p) {
	if (*p == '\\') {
	    if (*++p == '\0') {
		break;
	    }
	}
	else if (*p == '?' || *p == '*' || *p == '[') {
	    return 1;
	}
    }
    return 0;
}
#line 671 "y.tab.c"
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
static int yygrowstack(void)
{
    int newsize, i;
    short *newss;
    YYSTYPE *newvs;

    if ((newsize = yystacksize) == 0)
        newsize = YYINITSTACKSIZE;
    else if (newsize >= YYMAXDEPTH)
        return -1;
    else if ((newsize *= 2) > YYMAXDEPTH)
        newsize = YYMAXDEPTH;

    i = yyssp - yyss;
    newss = (yyss != 0)
          ? (short *)realloc(yyss, newsize * sizeof(*newss))
          : (short *)malloc(newsize * sizeof(*newss));
    if (newss == 0)
        return -1;

    yyss  = newss;
    yyssp = newss + i;
    newvs = (yyvs != 0)
          ? (YYSTYPE *)realloc(yyvs, newsize * sizeof(*newvs))
          : (YYSTYPE *)malloc(newsize * sizeof(*newvs));
    if (newvs == 0)
        return -1;

    yyvs = newvs;
    yyvsp = newvs + i;
    yystacksize = newsize;
    yysslim = yyss + newsize - 1;
    return 0;
}

#define YYABORT goto yyabort
#define YYREJECT goto yyabort
#define YYACCEPT goto yyaccept
#define YYERROR goto yyerrlab
int
yyparse(void)
{
    register int yym, yyn, yystate;
#if YYDEBUG
    register const char *yys;

    if ((yys = getenv("YYDEBUG")) != 0)
    {
        yyn = *yys;
        if (yyn >= '0' && yyn <= '9')
            yydebug = yyn - '0';
    }
#endif

    yynerrs = 0;
    yyerrflag = 0;
    yychar = YYEMPTY;

    if (yyss == NULL && yygrowstack()) goto yyoverflow;
    yyssp = yyss;
    yyvsp = yyvs;
    *yyssp = yystate = 0;

yyloop:
    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
    if (yychar < 0)
    {
        if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, reading %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
    }
    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: state %d, shifting to state %d\n",
                    YYPREFIX, yystate, yytable[yyn]);
#endif
        if (yyssp >= yysslim && yygrowstack())
        {
            goto yyoverflow;
        }
        *++yyssp = yystate = yytable[yyn];
        *++yyvsp = yylval;
        yychar = YYEMPTY;
        if (yyerrflag > 0)  --yyerrflag;
        goto yyloop;
    }
    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
        yyn = yytable[yyn];
        goto yyreduce;
    }
    if (yyerrflag) goto yyinrecovery;

    yyerror("syntax error");

#ifdef lint
    goto yyerrlab;
#endif

yyerrlab:
    ++yynerrs;

yyinrecovery:
    if (yyerrflag < 3)
    {
        yyerrflag = 3;
        for (;;)
        {
            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: state %d, error recovery shifting\
 to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
#endif
                if (yyssp >= yysslim && yygrowstack())
                {
                    goto yyoverflow;
                }
                *++yyssp = yystate = yytable[yyn];
                *++yyvsp = yylval;
                goto yyloop;
            }
            else
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: error recovery discarding state %d\n",
                            YYPREFIX, *yyssp);
#endif
                if (yyssp <= yyss) goto yyabort;
                --yyssp;
                --yyvsp;
            }
        }
    }
    else
    {
        if (yychar == 0) goto yyabort;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
        yychar = YYEMPTY;
        goto yyloop;
    }

yyreduce:
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
                YYPREFIX, yystate, yyn, yyrule[yyn]);
#endif
    yym = yylen[yyn];
    if (yym)
        yyval = yyvsp[1-yym];
    else
        memset(&yyval, 0, sizeof yyval);
    switch (yyn)
    {
case 1:
#line 96 "rhgram.y"
{
			    rh_lex_finish();
			}
break;
case 2:
#line 100 "rhgram.y"
{
			    rh_lex_finish();
			    if (Start_expr == (node *) NULL) {
				Start_expr = yyvsp[0].tree;
			    }
			    else {
				error("too many start expressions specified");
			    }
			}
break;
case 3:
#line 110 "rhgram.y"
{
			    rh_lex_finish();
			    if (Start_expr == (node *) NULL) {
				Start_expr = yyvsp[-1].tree;
			    }
			    else {
				error("too many start expressions specified");
			    }
			}
break;
case 6:
#line 126 "rhgram.y"
{
			    yyvsp[0].sym->type = FUNCTION;
			    yyvsp[0].sym->func = c_func;
			    yyvsp[0].sym->value = -1;
			    yyvsp[0].sym->tree = (node *) NULL;
			}
break;
case 7:
#line 133 "rhgram.y"
{
			    yyvsp[-2].sym->tree = yyvsp[0].tree;
			    yyvsp[-2].sym->num_params = 0;
			}
break;
case 8:
#line 138 "rhgram.y"
{
			    yyvsp[0].sym->type = FUNCTION;
			    yyvsp[0].sym->func = c_func;
			    yyvsp[0].sym->value = -1;
			    yyvsp[0].sym->tree = (node *) NULL;
			}
break;
case 9:
#line 145 "rhgram.y"
{
			    yyvsp[-4].sym->num_params = yyvsp[-1].value;
			}
break;
case 10:
#line 149 "rhgram.y"
{
			    symbol *sym;

			    yyvsp[-6].sym->tree = yyvsp[0].tree;

			    while (Symbol_table->type == PARAM) {
				if (Symbol_table->ref_count == 0
				    && Issue_warnings) {
				    warning("warning: function '%s' parameter '%s' is not referenced",
					    yyvsp[-6].sym->name, Symbol_table->name);
				}
				sym = Symbol_table;
				Symbol_table = Symbol_table->next;
				free(sym->name);
				free((char *) sym);
			    }
			}
break;
case 11:
#line 167 "rhgram.y"
{
			    rh_error("expected function definition");
			}
break;
case 12:
#line 173 "rhgram.y"
{
			    yyval.value = 0;
			}
break;
case 13:
#line 177 "rhgram.y"
{
			    yyvsp[0].sym->type = PARAM;
			    yyvsp[0].sym->func = c_param;
			    yyvsp[0].sym->value = 0;
			    yyvsp[0].sym->ref_count = 0;
			    yyval.value = 1;
			}
break;
case 14:
#line 185 "rhgram.y"
{
			    yyvsp[0].sym->type = PARAM;
			    yyvsp[0].sym->func = c_param;
			    yyvsp[0].sym->value = yyvsp[-2].value;
			    yyvsp[0].sym->ref_count = 0;
			    yyval.value = yyvsp[-2].value + 1;
			}
break;
case 15:
#line 193 "rhgram.y"
{
			    rh_error("expected identifier");
			}
break;
case 16:
#line 199 "rhgram.y"
{ yyval.tree = yyvsp[-2].tree; }
break;
case 17:
#line 201 "rhgram.y"
{ yyval.tree = yyvsp[-1].tree; }
break;
case 20:
#line 209 "rhgram.y"
{ yyval.tree = yyvsp[0].tree; }
break;
case 21:
#line 211 "rhgram.y"
{
			    if (yyvsp[-4].tree->code.func == c_number
				&& yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-4].tree;
				yyval.tree->code.value.l =
				    yyvsp[-4].tree->code.value.l ? yyvsp[-2].tree->code.value.l : yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[-2].tree);
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(TERTIARY, &value);
				yyval.tree->left = yyvsp[-4].tree;
				yyval.tree->middle = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 22:
#line 230 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l || yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_or, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 23:
#line 246 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l && yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_and, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 24:
#line 262 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l | yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_bor, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 25:
#line 278 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l ^ yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_bxor, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 26:
#line 294 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l & yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_band, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 27:
#line 310 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l == yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_eq, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 28:
#line 326 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l != yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_ne, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 29:
#line 342 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l < yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_lt, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 30:
#line 358 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l > yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_gt, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 31:
#line 374 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l <= yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_le, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 32:
#line 390 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l >= yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_ge, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 33:
#line 406 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l >> yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_rshift, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 34:
#line 422 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l << yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L; 
				yyval.tree = make_node(c_lshift, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 35:
#line 438 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l + yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_plus, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 36:
#line 454 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l - yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_minus, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 37:
#line 470 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l * yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_mul, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 38:
#line 486 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l / yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_div, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 39:
#line 502 "rhgram.y"
{
			    if (yyvsp[-2].tree->code.func == c_number
				&& yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[-2].tree;
				yyval.tree->code.value.l =
				    yyvsp[-2].tree->code.value.l % yyvsp[0].tree->code.value.l;
				free((char *) yyvsp[0].tree);
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_mod, &value);
				yyval.tree->left = yyvsp[-2].tree;
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 40:
#line 518 "rhgram.y"
{
			    if (yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[0].tree;
				yyval.tree->code.value.l = ~ yyval.tree->code.value.l;
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_bnot, &value);
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 41:
#line 530 "rhgram.y"
{
			    if (yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[0].tree;
				yyval.tree->code.value.l = ! yyval.tree->code.value.l;
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_not, &value);
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 42:
#line 542 "rhgram.y"
{
			    if (yyvsp[0].tree->code.func == c_number) {
				yyval.tree = yyvsp[0].tree;
				yyval.tree->code.value.l = - yyval.tree->code.value.l;
			    }
			    else {
				value.l = 0L;
				yyval.tree = make_node(c_uniminus, &value);
				yyval.tree->right = yyvsp[0].tree;
			    }
			}
break;
case 43:
#line 556 "rhgram.y"
{
			    yyval.line_info.line = SAVESTR(Line);
			    yyval.line_info.line_num = Line_num;
			    yyval.line_info.line_pos = Line_pos - strlen(yyvsp[0].sym->name);
			}
break;
case 44:
#line 562 "rhgram.y"
{
			    if (yyvsp[-2].sym->num_params != yyvsp[0].tree->count) {
				free(Line);
				Line = yyvsp[-1].line_info.line;
				Line_num = -(yyvsp[-1].line_info.line_num);
				Line_pos = yyvsp[-1].line_info.line_pos;
				rh_error("function called with wrong number of arguments");
			    }
			    if (yyvsp[-2].sym->tree != (node *) NULL
				&& yyvsp[-2].sym->tree->left == (node *) NULL
				&& yyvsp[-2].sym->tree->right == (node *) NULL) {
				int i;
				
				if (yyvsp[-2].sym->tree->code.func == c_param) {
				    for (i = 0; i < yyvsp[0].tree->count; i++) {
					if (i == yyvsp[-2].sym->tree->code.value.param) {
					    yyval.tree = yyvsp[0].tree->trees[i];
					}
					else {
					    free_tree(yyvsp[0].tree->trees[i]);
					}
				    }
				}
				else {
				    yyval.tree = copy_tree(yyvsp[-2].sym->tree);
				    for (i = 0; i < yyvsp[0].tree->count; i++) {
					free_tree(yyvsp[0].tree->trees[i]);
				    }
				}
				if (yyvsp[0].tree->trees != (node **) NULL) {
				    free((char *) yyvsp[0].tree->trees);
				}
				free((char *) yyvsp[0].tree);
			    }
			    else {
				yyval.tree = yyvsp[0].tree;
				yyval.tree->code.func = UNRES_FUNC;
				yyval.tree->code.value.func_name = yyvsp[-2].sym->name;
			    }
			    free(yyvsp[-1].line_info.line);
			}
break;
case 45:
#line 604 "rhgram.y"
{
			    yyval.line_info.line = SAVESTR(Line);
			    yyval.line_info.line_num = Line_num;
			    yyval.line_info.line_pos = Line_pos - strlen(yyvsp[0].sym->name);
			}
break;
case 46:
#line 610 "rhgram.y"
{
			    if (yyvsp[-2].sym->num_params != yyvsp[0].tree->count) {
				free(Line);
				Line = yyvsp[-1].line_info.line;
				Line_num = -(yyvsp[-1].line_info.line_num);
				Line_pos = yyvsp[-1].line_info.line_pos;
				rh_error("function called with wrong number of arguments");
			    }
			    yyval.tree = yyvsp[0].tree;
			    yyval.tree->code.func = c_syscall;
			    yyval.tree->code.value.sym = yyvsp[-2].sym;
			    free(yyvsp[-1].line_info.line);
			}
break;
case 47:
#line 624 "rhgram.y"
{ yyval.tree = yyvsp[-1].tree; }
break;
case 48:
#line 626 "rhgram.y"
{
			    value.l = yyvsp[0].value;
			    yyval.tree = make_node(c_number, &value);
			}
break;
case 49:
#line 631 "rhgram.y"
{
			    if (attr.dot_special && strcmp(yyvsp[0].str, "*") == 0) {
				value.l = 1L;
				yyval.tree = make_node(c_number, &value);
			    }
			    else {
				value.pattern = yyvsp[0].str;
				if (pattern(yyvsp[0].str)) {
				    yyval.tree = make_node(c_pattern, &value);
				}
				else if (attr.ignore_case) {
				    yyval.tree = make_node(c_fnigncase, &value);
				}
				else {
				    yyval.tree = make_node(c_fncase, &value);
				}
			    }
			}
break;
case 50:
#line 650 "rhgram.y"
{
			    value.fstype = yyvsp[0].str;
			    yyval.tree = make_node(c_fstype, &value);
			}
break;
case 51:
#line 655 "rhgram.y"
{
			    value.l = yyvsp[0].sym->value;
			    yyval.tree = make_node(yyvsp[0].sym->func, &value);
			}
break;
case 52:
#line 660 "rhgram.y"
{
			    value.l = yyvsp[0].sym->value;
			    yyval.tree = make_node(yyvsp[0].sym->func, &value);
			}
break;
case 53:
#line 665 "rhgram.y"
{
			    value.param = yyvsp[0].sym->value;
			    yyval.tree = make_node(c_param, &value);
			    yyvsp[0].sym->ref_count++;
			}
break;
case 54:
#line 671 "rhgram.y"
{ rh_error("undefined identifier"); }
break;
case 55:
#line 673 "rhgram.y"
{
			    value.di = yyvsp[0].di;
			    yyval.tree = make_node(c_date, &value);
			}
break;
case 56:
#line 678 "rhgram.y"
{
			    value.di = yyvsp[0].di;
			    yyval.tree = make_node(c_date, &value);
			}
break;
case 57:
#line 683 "rhgram.y"
{
			    value.l = yyvsp[0].sym->value;
			    yyval.tree = make_node(yyvsp[0].sym->func, &value);
			}
break;
case 58:
#line 690 "rhgram.y"
{
			    value.l = 0L;
			    yyval.tree = make_node(EXPR_LIST, &value);
			    yyval.tree->count = 0;
			    yyval.tree->trees = (node **) NULL;
			}
break;
case 59:
#line 697 "rhgram.y"
{ yyval.tree = yyvsp[-1].tree; }
break;
case 60:
#line 700 "rhgram.y"
{
			    value.l = 0L;
			    yyval.tree = make_node(EXPR_LIST, &value);
			    yyval.tree->count = 0;
			    yyval.tree->trees = (node **) NULL;
			}
break;
case 61:
#line 707 "rhgram.y"
{
			    value.l = 0L;
			    yyval.tree = make_node(EXPR_LIST, &value);
			    yyval.tree->count = 1;
			    yyval.tree->trees = ALLOCATE(sizeof(node *));
			    yyval.tree->trees[0] = yyvsp[0].tree;
			}
break;
case 62:
#line 715 "rhgram.y"
{
			    yyval.tree = yyvsp[-2].tree;
			    yyval.tree->count++;
			    yyval.tree->trees =
				REALLOCATE(yyval.tree->trees,
					   (sizeof(node *)) * yyval.tree->count);
			    yyval.tree->trees[yyval.tree->count - 1] = yyvsp[0].tree;
			}
break;
#line 1575 "y.tab.c"
    }
    yyssp -= yym;
    yystate = *yyssp;
    yyvsp -= yym;
    yym = yylhs[yyn];
    if (yystate == 0 && yym == 0)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: after reduction, shifting from state 0 to\
 state %d\n", YYPREFIX, YYFINAL);
#endif
        yystate = YYFINAL;
        *++yyssp = YYFINAL;
        *++yyvsp = yyval;
        if (yychar < 0)
        {
            if ((yychar = yylex()) < 0) yychar = 0;
#if YYDEBUG
            if (yydebug)
            {
                yys = 0;
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
                if (!yys) yys = "illegal-symbol";
                printf("%sdebug: state %d, reading %d (%s)\n",
                        YYPREFIX, YYFINAL, yychar, yys);
            }
#endif
        }
        if (yychar == 0) goto yyaccept;
        goto yyloop;
    }
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
        yystate = yytable[yyn];
    else
        yystate = yydgoto[yym];
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: after reduction, shifting from state %d \
to state %d\n", YYPREFIX, *yyssp, yystate);
#endif
    if (yyssp >= yysslim && yygrowstack())
    {
        goto yyoverflow;
    }
    *++yyssp = yystate;
    *++yyvsp = yyval;
    goto yyloop;

yyoverflow:
    yyerror("yacc stack overflow");

yyabort:
    return (1);

yyaccept:
    return (0);
}

Added rhgram.h.









































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define BLTIN 257
#define DATESPEC 258
#define EQ 259
#define FIELD 260
#define FILENAME_PATTERN 261
#define FILETIME_FIELD 262
#define FSTYPE 263
#define FUNCTION 264
#define GE 265
#define IDENTIFIER 266
#define LE 267
#define LOGAND 268
#define LOGOR 269
#define LSHIFT 270
#define NE 271
#define NUMBER 272
#define PARAM 273
#define RETURN 274
#define RSHIFT 275
#define SYSTEM_CALL 276
#define TIME_FIELD 277
#define UNIMINUS 278
typedef union {
    char *              str;
    symbol *		sym;
    node *		tree;
    long                value;
    void	      (*func)(instr_value *);
    date_info *		di;
    struct {
	char *	line;
	int	line_num;
	int	line_pos;
    } line_info;
} YYSTYPE;
extern YYSTYPE rh_lval;

Added rhgram.y.































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
%{

/*
 * rhgram.y
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 */

#if !defined(lint)
static char rcsid[] = "$Id: rhgram.y,v 1.3 1994/01/26 16:17:54 rick Exp $";
#endif

#include "rh.h"
#include "rhcmds.h"
#include "rhdata.h"
#include "alloc.h"
#include "errmsg.h"

extern char *	Line;		/* line being parsed */
extern int	Line_num;	/* line number */
extern int	Line_pos;	/* position in line */

static node *	copy_tree(node *);	/* copy a tree */
extern void	free_tree(node *);	/* free expression tree memory */ 
static node *	make_node(void (*)(), instr_value *);	/* make a tree node */
static int	pattern(const char *);	/* is string a shell fn pattern? */
extern void	rh_error(const char *);	/* output an error message and exit */
extern int	rh_lex(void);		/* lexer */
extern void	rh_lex_finish(void);	/* lexer clean-up */

static instr_value	value;

%}

%union {
    char *              str;
    symbol *		sym;
    node *		tree;
    long                value;
    void	      (*func)(instr_value *);
    date_info *		di;
    struct {
	char *	line;
	int	line_num;
	int	line_pos;
    } line_info;
}

%token <sym>    BLTIN
%token <di>     DATESPEC
%token          EQ
%token <sym>    FIELD
%token <str>    FILENAME_PATTERN
%token <di>	FILETIME_FIELD
%token <str>    FSTYPE
%token <sym>    FUNCTION
%token          GE
%token <sym>    IDENTIFIER
%token          LE
%token          LOGAND
%token          LOGOR
%token          LSHIFT
%token          NE
%token <value>  NUMBER
%token <sym>    PARAM
%token          RETURN
%token          RSHIFT
%token <sym>	SYSTEM_CALL
%token <sym>    TIME_FIELD

%right	'?' ':'
%left	LOGOR
%left	LOGAND
%left	'|'
%left	'^'
%left	'&'
%left	EQ NE
%left	'<' '>' LE GE
%left	RSHIFT LSHIFT
%left	'+' '-'
%left	'*' '/' '%'
%nonassoc UNIMINUS
%nonassoc '!'
%nonassoc '~'

%type <tree>	expression
%type <tree>	exprlist
%type <tree>	factor
%type <tree>	function_body
%type <value>	idlist
%type <tree>	paramlist

%%

program:	  functions
			{
			    rh_lex_finish();
			}
		| functions expression
			{
			    rh_lex_finish();
			    if (Start_expr == (node *) NULL) {
				Start_expr = $2;
			    }
			    else {
				error("too many start expressions specified");
			    }
			}
		| functions expression ';'
			{
			    rh_lex_finish();
			    if (Start_expr == (node *) NULL) {
				Start_expr = $2;
			    }
			    else {
				error("too many start expressions specified");
			    }
			}
		;

functions: 	  /* empty */
		| functions function
		;

function:	  IDENTIFIER
			{
			    $1->type = FUNCTION;
			    $1->func = c_func;
			    $1->value = -1;
			    $1->tree = (node *) NULL;
			}
		  function_body
			{
			    $1->tree = $3;
			    $1->num_params = 0;
			}
		| IDENTIFIER
			{
			    $1->type = FUNCTION;
			    $1->func = c_func;
			    $1->value = -1;
			    $1->tree = (node *) NULL;
			}
		  '(' idlist ')'
			{
			    $1->num_params = $4;
			}
		  function_body
			{
			    symbol *sym;

			    $1->tree = $7;

			    while (Symbol_table->type == PARAM) {
				if (Symbol_table->ref_count == 0
				    && Issue_warnings) {
				    warning("warning: function '%s' parameter '%s' is not referenced",
					    $1->name, Symbol_table->name);
				}
				sym = Symbol_table;
				Symbol_table = Symbol_table->next;
				free(sym->name);
				free((char *) sym);
			    }
			}
		| error
			{
			    rh_error("expected function definition");
			}
		;

idlist:		  /* empty */
			{
			    $$ = 0;
			}
		| IDENTIFIER
			{
			    $1->type = PARAM;
			    $1->func = c_param;
			    $1->value = 0;
			    $1->ref_count = 0;
			    $$ = 1;
			}
		| idlist ',' IDENTIFIER
			{
			    $3->type = PARAM;
			    $3->func = c_param;
			    $3->value = $1;
			    $3->ref_count = 0;
			    $$ = $1 + 1;
			}
		| error
			{
			    rh_error("expected identifier");
			}
		;

function_body:	  '{' return expression ';' '}'
			{ $$ = $3; }
		| '{' return expression '}'
			{ $$ = $3; }
		;

return:		  /* empty */
		| RETURN
		;

expression:	  factor
			{ $$ = $1; }
		| expression '?' expression ':' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number
				&& $5->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l ? $3->code.value.l : $5->code.value.l;
				free((char *) $3);
				free((char *) $5);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(TERTIARY, &value);
				$$->left = $1;
				$$->middle = $3;
				$$->right = $5;
			    }
			}
		| expression LOGOR expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l || $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_or, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression LOGAND expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l && $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_and, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '|' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l | $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_bor, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '^' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l ^ $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_bxor, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '&' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l & $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_band, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression EQ expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l == $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_eq, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression NE expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l != $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_ne, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '<' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l < $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_lt, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '>' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l > $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_gt, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression LE expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l <= $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_le, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression GE expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l >= $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_ge, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression RSHIFT expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l >> $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_rshift, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression LSHIFT expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l << $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L; 
				$$ = make_node(c_lshift, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '+' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l + $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_plus, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '-' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l - $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_minus, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '*' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l * $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_mul, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '/' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l / $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_div, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| expression '%' expression
			{
			    if ($1->code.func == c_number
				&& $3->code.func == c_number) {
				$$ = $1;
				$$->code.value.l =
				    $1->code.value.l % $3->code.value.l;
				free((char *) $3);
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_mod, &value);
				$$->left = $1;
				$$->right = $3;
			    }
			}
		| '~' expression
			{
			    if ($2->code.func == c_number) {
				$$ = $2;
				$$->code.value.l = ~ $$->code.value.l;
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_bnot, &value);
				$$->right = $2;
			    }
			}
		| '!' expression
			{
			    if ($2->code.func == c_number) {
				$$ = $2;
				$$->code.value.l = ! $$->code.value.l;
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_not, &value);
				$$->right = $2;
			    }
			}
		| '-' expression	%prec UNIMINUS
			{
			    if ($2->code.func == c_number) {
				$$ = $2;
				$$->code.value.l = - $$->code.value.l;
			    }
			    else {
				value.l = 0L;
				$$ = make_node(c_uniminus, &value);
				$$->right = $2;
			    }
			}
		;

factor:		  FUNCTION
			{
			    $<line_info>$.line = SAVESTR(Line);
			    $<line_info>$.line_num = Line_num;
			    $<line_info>$.line_pos = Line_pos - strlen($1->name);
			}
		  paramlist
			{
			    if ($1->num_params != $3->count) {
				free(Line);
				Line = $<line_info>2.line;
				Line_num = -($<line_info>2.line_num);
				Line_pos = $<line_info>2.line_pos;
				rh_error("function called with wrong number of arguments");
			    }
			    if ($1->tree != (node *) NULL
				&& $1->tree->left == (node *) NULL
				&& $1->tree->right == (node *) NULL) {
				int i;
				
				if ($1->tree->code.func == c_param) {
				    for (i = 0; i < $3->count; i++) {
					if (i == $1->tree->code.value.param) {
					    $$ = $3->trees[i];
					}
					else {
					    free_tree($3->trees[i]);
					}
				    }
				}
				else {
				    $$ = copy_tree($1->tree);
				    for (i = 0; i < $3->count; i++) {
					free_tree($3->trees[i]);
				    }
				}
				if ($3->trees != (node **) NULL) {
				    free((char *) $3->trees);
				}
				free((char *) $3);
			    }
			    else {
				$$ = $3;
				$$->code.func = UNRES_FUNC;
				$$->code.value.func_name = $1->name;
			    }
			    free($<line_info>2.line);
			}
		| SYSTEM_CALL
			{
			    $<line_info>$.line = SAVESTR(Line);
			    $<line_info>$.line_num = Line_num;
			    $<line_info>$.line_pos = Line_pos - strlen($1->name);
			}
		  paramlist
			{
			    if ($1->num_params != $3->count) {
				free(Line);
				Line = $<line_info>2.line;
				Line_num = -($<line_info>2.line_num);
				Line_pos = $<line_info>2.line_pos;
				rh_error("function called with wrong number of arguments");
			    }
			    $$ = $3;
			    $$->code.func = c_syscall;
			    $$->code.value.sym = $1;
			    free($<line_info>2.line);
			}
		| '(' expression ')'
			{ $$ = $2; }
		| NUMBER
			{
			    value.l = $1;
			    $$ = make_node(c_number, &value);
			}
		| FILENAME_PATTERN
			{
			    if (attr.dot_special && strcmp($1, "*") == 0) {
				value.l = 1L;
				$$ = make_node(c_number, &value);
			    }
			    else {
				value.pattern = $1;
				if (pattern($1)) {
				    $$ = make_node(c_pattern, &value);
				}
				else if (attr.ignore_case) {
				    $$ = make_node(c_fnigncase, &value);
				}
				else {
				    $$ = make_node(c_fncase, &value);
				}
			    }
			}
		| FSTYPE
			{
			    value.fstype = $1;
			    $$ = make_node(c_fstype, &value);
			}
		| BLTIN
			{
			    value.l = $1->value;
			    $$ = make_node($1->func, &value);
			}
		| FIELD
			{
			    value.l = $1->value;
			    $$ = make_node($1->func, &value);
			}
		| PARAM
			{
			    value.param = $1->value;
			    $$ = make_node(c_param, &value);
			    $1->ref_count++;
			}
		| IDENTIFIER
			{ rh_error("undefined identifier"); }
		| DATESPEC
			{
			    value.di = $1;
			    $$ = make_node(c_date, &value);
			}
		| FILETIME_FIELD
			{
			    value.di = $1;
			    $$ = make_node(c_date, &value);
			}
		| TIME_FIELD
			{
			    value.l = $1->value;
			    $$ = make_node($1->func, &value);
			}
		;

paramlist:	  /* empty */
			{
			    value.l = 0L;
			    $$ = make_node(EXPR_LIST, &value);
			    $$->count = 0;
			    $$->trees = (node **) NULL;
			}
		| '(' exprlist ')'
			{ $$ = $2; }

exprlist:	  /* empty */
			{
			    value.l = 0L;
			    $$ = make_node(EXPR_LIST, &value);
			    $$->count = 0;
			    $$->trees = (node **) NULL;
			}
		| expression
			{
			    value.l = 0L;
			    $$ = make_node(EXPR_LIST, &value);
			    $$->count = 1;
			    $$->trees = ALLOCATE(sizeof(node *));
			    $$->trees[0] = $1;
			}
		| exprlist ',' expression
			{
			    $$ = $1;
			    $$->count++;
			    $$->trees =
				REALLOCATE($$->trees,
					   (sizeof(node *)) * $$->count);
			    $$->trees[$$->count - 1] = $3;
			}
		;

%%
 
void rh_error(const char *msg)
{
    extern const int		yyleng;
    extern const char * const	yytext;

#if defined(DEBUG2)
    (void) fprintf(stderr, "yyleng = %d, yytext = '%s'\n", yyleng, yytext);
#endif

    if (yyleng) {
	if (Line_num < 0) {
	    (void) fprintf(stderr, "%s: %s: %d: %s\n",
			   Program_name, Input_name, -Line_num, msg);
	}
	else {
	    (void) fprintf(stderr, "%s: %s: %d: %s at '%s'\n",
			   Program_name, Input_name, Line_num, msg, yytext);
	}
	(void) fprintf(stderr, " Line: %s\nError: %*s\n",
		       Line, Line_pos - (yyleng - 1), "^");
    }
    else {
	(void) fprintf(stderr, "%s: %s: %s\n", Program_name, Input_name, msg);
	(void) fprintf(stderr, "%s: unexpected end of file after line %d\n",
		       Program_name, Line_num);
	(void) fprintf(stderr, "Last Line: %s\n", Line);
    }
    exit(1);
    /*NOTREACHED*/
}
 
static node *make_node(void	      (*func)(),
		       instr_value *	value)
{
    node *	new_node;

    new_node = ALLOCATE(sizeof(node));
    new_node->code.func = func;
    new_node->code.value = *value;
    new_node->left = new_node->right = (node *) NULL;
    new_node->middle = (node *) NULL;
    new_node->count = -1;
    new_node->trees = (node **) NULL;

    return new_node;
}
 
node *copy_tree(node *tree)
{
    node *	new_tree;


    if (tree == (node *) NULL) {
	return tree;
    }

    new_tree = ALLOCATE(sizeof(node));

    new_tree->code.func = tree->code.func;
    if (new_tree->code.func == c_date) {
	new_tree->code.value.di = ALLOCATE(sizeof(date_info));
	*(new_tree->code.value.di) = *tree->code.value.di;
    }
    else if (new_tree->code.func == c_pattern
	     || new_tree->code.func == c_fncase
	     || new_tree->code.func == c_fnigncase) {
	new_tree->code.value.pattern = SAVESTR(tree->code.value.pattern);
    }
    else {
	new_tree->code.value = tree->code.value;
    }
    
    new_tree->left = copy_tree(tree->left);
    new_tree->right = copy_tree(tree->right);
    new_tree->middle = copy_tree(tree->middle);
    
    new_tree->count = tree->count;
    if (new_tree->count <= 0) {
	new_tree->trees = (node **) NULL;
    }
    else {
	int	i;
	
	new_tree->trees = ALLOCATE(sizeof(node *) * new_tree->count);
	for (i = 0; i < new_tree->count; i++) {
	    new_tree->trees[i] = copy_tree(tree->trees[i]);
	}
    }
    
    return new_tree;
}
 
void free_tree(node *tree)
{
    if (tree != (node *) NULL) {
	free_tree(tree->left);
	free_tree(tree->right);
	free_tree(tree->middle);
	if (tree->code.func == c_date) {
	    free((char *) tree->code.value.di);
	}
	else if (tree->code.func == c_fstype) {
	    free(tree->code.value.fstype);
	}
	else if (tree->code.func == c_pattern
		 || tree->code.func == c_fncase
		 || tree->code.func == c_fnigncase) {
	    free(tree->code.value.pattern);
	}
	else if (tree->code.func == UNRES_FUNC && tree->count > 0) {
	    int i;
	    
	    for (i = 0; i < tree->count; i++) {
		free_tree(tree->trees[i]);
	    }
	    free((char *) tree->trees);
	}
	free((char *) tree);
    }

    return;
}
 
static int pattern(const char *p)
{
    for (/*void*/ ; *p != '\0' ; ++p) {
	if (*p == '\\') {
	    if (*++p == '\0') {
		break;
	    }
	}
	else if (*p == '?' || *p == '*' || *p == '[') {
	    return 1;
	}
    }
    return 0;
}

Added rhlex.c.





























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134

#line 3 "<stdout>"

#define  YY_INT_ALIGNED short int

/* A lexical scanner generated by flex */

/* %not-for-header */

/* %if-c-only */
/* %if-not-reentrant */

/* %endif */
/* %endif */
/* %ok-for-header */

#define FLEX_SCANNER
#define YY_FLEX_MAJOR_VERSION 2
#define YY_FLEX_MINOR_VERSION 5
#define YY_FLEX_SUBMINOR_VERSION 35
#if YY_FLEX_SUBMINOR_VERSION > 0
#define FLEX_BETA
#endif

/* %if-c++-only */
/* %endif */

/* %if-c-only */
    
/* %endif */

/* %if-c-only */

/* %endif */

/* First, we deal with  platform-specific or compiler-specific issues. */

/* begin standard C headers. */
/* %if-c-only */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
/* %endif */

/* %if-tables-serialization */
/* %endif */
/* end standard C headers. */

/* %if-c-or-c++ */
/* flex integer type definitions */

#ifndef FLEXINT_H
#define FLEXINT_H

/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */

#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L

/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h,
 * if you want the limit (max/min) macros for int types. 
 */
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS 1
#endif

#include <inttypes.h>
typedef int8_t flex_int8_t;
typedef uint8_t flex_uint8_t;
typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t;
#else
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
typedef int flex_int32_t;
typedef unsigned char flex_uint8_t; 
typedef unsigned short int flex_uint16_t;
typedef unsigned int flex_uint32_t;

/* Limits of integral types. */
#ifndef INT8_MIN
#define INT8_MIN               (-128)
#endif
#ifndef INT16_MIN
#define INT16_MIN              (-32767-1)
#endif
#ifndef INT32_MIN
#define INT32_MIN              (-2147483647-1)
#endif
#ifndef INT8_MAX
#define INT8_MAX               (127)
#endif
#ifndef INT16_MAX
#define INT16_MAX              (32767)
#endif
#ifndef INT32_MAX
#define INT32_MAX              (2147483647)
#endif
#ifndef UINT8_MAX
#define UINT8_MAX              (255U)
#endif
#ifndef UINT16_MAX
#define UINT16_MAX             (65535U)
#endif
#ifndef UINT32_MAX
#define UINT32_MAX             (4294967295U)
#endif

#endif /* ! C99 */

#endif /* ! FLEXINT_H */

/* %endif */

/* %if-c++-only */
/* %endif */

#ifdef __cplusplus

/* The "const" storage-class-modifier is valid. */
#define YY_USE_CONST

#else	/* ! __cplusplus */

/* C99 requires __STDC__ to be defined as 1. */
#if defined (__STDC__)

#define YY_USE_CONST

#endif	/* defined (__STDC__) */
#endif	/* ! __cplusplus */

#ifdef YY_USE_CONST
#define yyconst const
#else
#define yyconst
#endif

/* %not-for-header */

/* Returned upon end-of-file. */
#define YY_NULL 0
/* %ok-for-header */

/* %not-for-header */

/* Promotes a possibly negative, possibly signed char to an unsigned
 * integer for use as an array index.  If the signed char is negative,
 * we want to instead treat it as an 8-bit unsigned char, hence the
 * double cast.
 */
#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c)
/* %ok-for-header */

/* %if-reentrant */
/* %endif */

/* %if-not-reentrant */

/* %endif */

/* Enter a start condition.  This macro really ought to take a parameter,
 * but we do it the disgusting crufty way forced on us by the ()-less
 * definition of BEGIN.
 */
#define BEGIN (yy_start) = 1 + 2 *

/* Translate the current start state into a value that can be later handed
 * to BEGIN to return to the state.  The YYSTATE alias is for lex
 * compatibility.
 */
#define YY_START (((yy_start) - 1) / 2)
#define YYSTATE YY_START

/* Action number for EOF rule of a given start state. */
#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1)

/* Special action meaning "start processing a new file". */
#define YY_NEW_FILE yyrestart(yyin  )

#define YY_END_OF_BUFFER_CHAR 0

/* Size of default input buffer. */
#ifndef YY_BUF_SIZE
#define YY_BUF_SIZE 16384
#endif

/* The state buf must be large enough to hold one state per character in the main buffer.
 */
#define YY_STATE_BUF_SIZE   ((YY_BUF_SIZE + 2) * sizeof(yy_state_type))

#ifndef YY_TYPEDEF_YY_BUFFER_STATE
#define YY_TYPEDEF_YY_BUFFER_STATE
typedef struct yy_buffer_state *YY_BUFFER_STATE;
#endif

/* %if-not-reentrant */
extern int yyleng;
/* %endif */

/* %if-c-only */
/* %if-not-reentrant */
extern FILE *yyin, *yyout;
/* %endif */
/* %endif */

#define EOB_ACT_CONTINUE_SCAN 0
#define EOB_ACT_END_OF_FILE 1
#define EOB_ACT_LAST_MATCH 2

    #define YY_LESS_LINENO(n)
    
/* Return all but the first "n" matched characters back to the input stream. */
#define yyless(n) \
	do \
		{ \
		/* Undo effects of setting up yytext. */ \
        int yyless_macro_arg = (n); \
        YY_LESS_LINENO(yyless_macro_arg);\
		*yy_cp = (yy_hold_char); \
		YY_RESTORE_YY_MORE_OFFSET \
		(yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \
		YY_DO_BEFORE_ACTION; /* set up yytext again */ \
		} \
	while ( 0 )

#define unput(c) yyunput( c, (yytext_ptr)  )

#ifndef YY_TYPEDEF_YY_SIZE_T
#define YY_TYPEDEF_YY_SIZE_T
typedef size_t yy_size_t;
#endif

#ifndef YY_STRUCT_YY_BUFFER_STATE
#define YY_STRUCT_YY_BUFFER_STATE
struct yy_buffer_state
	{
/* %if-c-only */
	FILE *yy_input_file;
/* %endif */

/* %if-c++-only */
/* %endif */

	char *yy_ch_buf;		/* input buffer */
	char *yy_buf_pos;		/* current position in input buffer */

	/* Size of input buffer in bytes, not including room for EOB
	 * characters.
	 */
	yy_size_t yy_buf_size;

	/* Number of characters read into yy_ch_buf, not including EOB
	 * characters.
	 */
	int yy_n_chars;

	/* Whether we "own" the buffer - i.e., we know we created it,
	 * and can realloc() it to grow it, and should free() it to
	 * delete it.
	 */
	int yy_is_our_buffer;

	/* Whether this is an "interactive" input source; if so, and
	 * if we're using stdio for input, then we want to use getc()
	 * instead of fread(), to make sure we stop fetching input after
	 * each newline.
	 */
	int yy_is_interactive;

	/* Whether we're considered to be at the beginning of a line.
	 * If so, '^' rules will be active on the next match, otherwise
	 * not.
	 */
	int yy_at_bol;

    int yy_bs_lineno; /**< The line count. */
    int yy_bs_column; /**< The column count. */
    
	/* Whether to try to fill the input buffer when we reach the
	 * end of it.
	 */
	int yy_fill_buffer;

	int yy_buffer_status;

#define YY_BUFFER_NEW 0
#define YY_BUFFER_NORMAL 1
	/* When an EOF's been seen but there's still some text to process
	 * then we mark the buffer as YY_EOF_PENDING, to indicate that we
	 * shouldn't try reading from the input source any more.  We might
	 * still have a bunch of tokens to match, though, because of
	 * possible backing-up.
	 *
	 * When we actually see the EOF, we change the status to "new"
	 * (via yyrestart()), so that the user can continue scanning by
	 * just pointing yyin at a new input file.
	 */
#define YY_BUFFER_EOF_PENDING 2

	};
#endif /* !YY_STRUCT_YY_BUFFER_STATE */

/* %if-c-only Standard (non-C++) definition */
/* %not-for-header */

/* %if-not-reentrant */

/* Stack of input buffers. */
static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */
static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */
static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */
/* %endif */
/* %ok-for-header */

/* %endif */

/* We provide macros for accessing buffer states in case in the
 * future we want to put the buffer states in a more general
 * "scanner state".
 *
 * Returns the top of the stack, or NULL.
 */
#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \
                          ? (yy_buffer_stack)[(yy_buffer_stack_top)] \
                          : NULL)

/* Same as previous macro, but useful when we know that the buffer stack is not
 * NULL or when we need an lvalue. For internal use only.
 */
#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)]

/* %if-c-only Standard (non-C++) definition */

/* %if-not-reentrant */
/* %not-for-header */

/* yy_hold_char holds the character lost when yytext is formed. */
static char yy_hold_char;
static int yy_n_chars;		/* number of characters read into yy_ch_buf */
int yyleng;

/* Points to current character in buffer. */
static char *yy_c_buf_p = (char *) 0;
static int yy_init = 0;		/* whether we need to initialize */
static int yy_start = 0;	/* start state number */

/* Flag which is used to allow yywrap()'s to do buffer switches
 * instead of setting up a fresh yyin.  A bit of a hack ...
 */
static int yy_did_buffer_switch_on_eof;
/* %ok-for-header */

/* %endif */

void yyrestart (FILE *input_file  );
void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer  );
YY_BUFFER_STATE yy_create_buffer (FILE *file,int size  );
void yy_delete_buffer (YY_BUFFER_STATE b  );
void yy_flush_buffer (YY_BUFFER_STATE b  );
void yypush_buffer_state (YY_BUFFER_STATE new_buffer  );
void yypop_buffer_state (void );

static void yyensure_buffer_stack (void );
static void yy_load_buffer_state (void );
static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file  );

#define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER )

YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size  );
YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str  );
YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len  );

/* %endif */

void *yyalloc (yy_size_t  );
void *yyrealloc (void *,yy_size_t  );
void yyfree (void *  );

#define yy_new_buffer yy_create_buffer

#define yy_set_interactive(is_interactive) \
	{ \
	if ( ! YY_CURRENT_BUFFER ){ \
        yyensure_buffer_stack (); \
		YY_CURRENT_BUFFER_LVALUE =    \
            yy_create_buffer(yyin,YY_BUF_SIZE ); \
	} \
	YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \
	}

#define yy_set_bol(at_bol) \
	{ \
	if ( ! YY_CURRENT_BUFFER ){\
        yyensure_buffer_stack (); \
		YY_CURRENT_BUFFER_LVALUE =    \
            yy_create_buffer(yyin,YY_BUF_SIZE ); \
	} \
	YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \
	}

#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol)

/* %% [1.0] yytext/yyin/yyout/yy_state_type/yylineno etc. def's & init go here */
/* Begin user sect3 */

#define FLEX_DEBUG

typedef unsigned char YY_CHAR;

FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;

typedef int yy_state_type;

extern int yylineno;

int yylineno = 1;

extern char *yytext;
#define yytext_ptr yytext

/* %if-c-only Standard (non-C++) definition */

static yy_state_type yy_get_previous_state (void );
static yy_state_type yy_try_NUL_trans (yy_state_type current_state  );
static int yy_get_next_buffer (void );
static void yy_fatal_error (yyconst char msg[]  );

/* %endif */

/* Done after the current pattern has been matched and before the
 * corresponding action - sets up yytext.
 */
#define YY_DO_BEFORE_ACTION \
	(yytext_ptr) = yy_bp; \
/* %% [2.0] code to fiddle yytext and yyleng for yymore() goes here \ */\
	(yytext_ptr) -= (yy_more_len); \
	yyleng = (size_t) (yy_cp - (yytext_ptr)); \
	(yy_hold_char) = *yy_cp; \
	*yy_cp = '\0'; \
/* %% [3.0] code to copy yytext_ptr to yytext[] goes here, if %array \ */\
	(yy_c_buf_p) = yy_cp;

/* %% [4.0] data tables for the DFA and the user's section 1 definitions go here */
#define YY_NUM_RULES 70
#define YY_END_OF_BUFFER 71
/* This struct is not used in this scanner,
   but its presence is necessary. */
struct yy_trans_info
	{
	flex_int32_t yy_verify;
	flex_int32_t yy_nxt;
	};
static yyconst flex_int16_t yy_accept[107] =
    {   0,
        0,    0,    5,    5,   10,   10,   15,   15,   20,   20,
        1,    1,    3,    3,   71,   69,   66,   68,   25,   61,
       67,   69,   27,   28,   30,   31,   32,   33,   34,   35,
       36,   58,   57,   37,   38,   39,   69,   43,   46,   56,
       60,   62,   47,   69,   48,   49,   51,   52,    5,    6,
        7,   10,   14,   12,   13,   15,   19,   18,   17,   20,
       24,   23,   22,    1,    2,    3,    4,   66,   26,   53,
       54,   29,   63,   64,   58,    0,   57,   57,   40,   41,
       42,   44,   45,   56,   55,   60,   62,   65,   50,    5,
        6,    7,    8,    7,    9,   10,   11,   15,   16,   20,

       21,    1,    3,   64,   59,    0
    } ;

static yyconst flex_int32_t yy_ec[256] =
    {   0,
        1,    1,    1,    1,    1,    1,    1,    1,    2,    3,
        1,    4,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    2,    5,    6,    7,    8,    9,   10,    1,   11,
       12,   13,   14,   15,   16,   17,   18,   19,   20,   20,
       20,   20,   20,   20,   20,   21,   21,   22,   23,   24,
       25,   26,   27,   28,   29,   29,   29,   29,   29,   29,
       30,   31,   31,   31,   30,   31,   30,   31,   31,   31,
       31,   31,   31,   31,   31,   31,   31,   32,   31,   31,
       33,   34,   35,   36,   37,   38,   29,   29,   29,   39,

       29,   29,   31,   30,   31,   31,   31,   31,   30,   31,
       31,   31,   31,   31,   31,   31,   31,   31,   30,   32,
       31,   31,   40,   41,   42,   43,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,

        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1
    } ;

static yyconst flex_int32_t yy_meta[44] =
    {   0,
        1,    1,    2,    1,    1,    3,    4,    5,    1,    1,
        1,    1,    6,    1,    1,    1,    7,    1,    8,    8,
        8,    1,    1,    1,    1,    1,    1,    9,    8,    8,
        8,    8,    1,   10,   11,    1,   12,    1,    8,    1,
        1,    1,    1
    } ;

static yyconst flex_int16_t yy_base[128] =
    {   0,
        0,    0,   41,   42,   43,   45,   47,   53,   54,   55,
      121,  120,  119,  118,  120,  354,   45,  354,   94,  354,
      354,  103,  354,  100,  354,  354,  354,  354,  354,  354,
       47,   32,   52,  354,  354,   42,   82,   43,  354,   77,
        0,  102,  354,    0,  354,   62,  354,  354,   98,  354,
       72,    0,  354,    0,  354,    0,  354,  354,    0,    0,
      354,  354,    0,    0,  354,    0,  354,   72,  354,  354,
       92,  354,  354,    0,   64,   73,   76,  354,  354,  354,
      354,  354,  354,   58,  354,    0,   68,    0,  354,   60,
      354,   95,  354,   96,  354,    0,  354,    0,  354,    0,

      354,    0,    0,    0,   97,  354,  136,  148,  160,  172,
      184,  196,  204,  205,  207,  213,  221,  233,  245,  257,
      269,  281,  293,  305,  317,  329,  341
    } ;

static yyconst flex_int16_t yy_def[128] =
    {   0,
      106,    1,  107,  107,  108,  108,  109,  109,  110,  110,
      111,  111,  112,  112,  106,  106,  106,  106,  106,  106,
      106,  113,  106,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106,  106,  106,  114,
      115,  106,  106,  116,  106,  106,  106,  106,  117,  106,
      118,  119,  106,  120,  106,  121,  106,  106,  122,  123,
      106,  106,  124,  125,  106,  126,  106,  106,  106,  106,
      113,  106,  106,  127,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  114,  106,  115,  106,  116,  106,  117,
      106,  118,  106,  118,  106,  119,  106,  121,  106,  123,

      106,  125,  126,  127,  106,    0,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106
    } ;

static yyconst flex_int16_t yy_nxt[398] =
    {   0,
       16,   17,   18,   17,   19,   20,   21,   22,   23,   24,
       25,   26,   27,   28,   29,   30,   16,   31,   32,   33,
       33,   34,   35,   36,   37,   38,   39,   40,   41,   41,
       41,   41,   42,   16,   16,   43,   41,   44,   41,   45,
       46,   47,   48,   50,   50,   53,   68,   53,   68,   57,
       75,   75,   58,   51,   51,   57,   61,   61,   58,   73,
       62,   62,   91,   76,   74,   79,   80,   82,   83,   87,
       77,   77,   77,   68,   93,   68,   54,   55,   54,   55,
       59,   78,   75,   75,   94,  106,   59,   63,   63,   95,
       78,  105,  105,  105,   77,   77,   77,   93,   93,  106,

       91,  105,   89,   87,   85,   78,   81,  106,   94,   72,
       70,  105,  106,   95,   78,  105,  105,  105,   69,  106,
       67,   67,   65,   65,  106,  105,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  105,   49,   49,   49,   49,
       49,   49,   49,   49,   49,   49,   49,   49,   52,   52,
       52,   52,   52,   52,   52,   52,   52,   52,   52,   52,
       56,   56,   56,   56,   56,   56,   56,   56,   56,   56,
       56,   56,   60,   60,   60,   60,   60,   60,   60,   60,
       60,   60,   60,   60,   64,   64,   64,   64,   64,   64,
       64,   64,   64,   64,   64,   64,   66,   66,   66,   66,

       66,   66,   66,   66,   66,   66,   66,   66,   71,  106,
      106,   71,   84,   84,   86,  106,  106,  106,   86,   88,
       88,   90,   90,   90,   90,   90,  106,   90,   90,   90,
       90,   90,   90,   92,   92,   92,   92,   92,   92,   92,
       92,   92,   92,   92,   92,   96,  106,   96,   96,   96,
       96,   96,   96,   96,  106,  106,   96,   97,  106,   97,
       97,   97,   97,   97,   97,   97,   97,   97,   97,   98,
      106,  106,   98,   98,   98,   98,   98,   98,  106,   98,
       98,   99,  106,   99,   99,   99,   99,   99,   99,   99,
       99,   99,   99,  100,  106,  100,  106,  100,  100,  100,

      100,  100,  106,  100,  100,  101,  106,  101,  101,  101,
      101,  101,  101,  101,  101,  101,  101,  102,  106,  102,
      102,  102,  102,  102,  102,  102,  102,  102,  102,  103,
      106,  103,  103,  103,  103,  103,  103,  103,  103,  103,
      103,  104,  106,  104,  104,  104,  104,  104,  104,  104,
      104,  104,  104,   15,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106

    } ;

static yyconst flex_int16_t yy_chk[398] =
    {   0,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
        1,    1,    1,    3,    4,    5,   17,    6,   17,    7,
       32,   32,    7,    3,    4,    8,    9,   10,    8,   31,
        9,   10,   90,   32,   31,   36,   36,   38,   38,   87,
       33,   33,   33,   68,   51,   68,    5,    5,    6,    6,
        7,   33,   75,   75,   51,   84,    8,    9,   10,   51,
       33,   76,   76,   76,   77,   77,   77,   92,   94,   71,

       49,   76,   46,   42,   40,   77,   37,   92,   94,   24,
       22,   76,   92,   94,   77,  105,  105,  105,   19,   15,
       14,   13,   12,   11,    0,  105,    0,    0,    0,    0,
        0,    0,    0,    0,    0,  105,  107,  107,  107,  107,
      107,  107,  107,  107,  107,  107,  107,  107,  108,  108,
      108,  108,  108,  108,  108,  108,  108,  108,  108,  108,
      109,  109,  109,  109,  109,  109,  109,  109,  109,  109,
      109,  109,  110,  110,  110,  110,  110,  110,  110,  110,
      110,  110,  110,  110,  111,  111,  111,  111,  111,  111,
      111,  111,  111,  111,  111,  111,  112,  112,  112,  112,

      112,  112,  112,  112,  112,  112,  112,  112,  113,    0,
        0,  113,  114,  114,  115,    0,    0,    0,  115,  116,
      116,  117,  117,  117,  117,  117,    0,  117,  117,  117,
      117,  117,  117,  118,  118,  118,  118,  118,  118,  118,
      118,  118,  118,  118,  118,  119,    0,  119,  119,  119,
      119,  119,  119,  119,    0,    0,  119,  120,    0,  120,
      120,  120,  120,  120,  120,  120,  120,  120,  120,  121,
        0,    0,  121,  121,  121,  121,  121,  121,    0,  121,
      121,  122,    0,  122,  122,  122,  122,  122,  122,  122,
      122,  122,  122,  123,    0,  123,    0,  123,  123,  123,

      123,  123,    0,  123,  123,  124,    0,  124,  124,  124,
      124,  124,  124,  124,  124,  124,  124,  125,    0,  125,
      125,  125,  125,  125,  125,  125,  125,  125,  125,  126,
        0,  126,  126,  126,  126,  126,  126,  126,  126,  126,
      126,  127,    0,  127,  127,  127,  127,  127,  127,  127,
      127,  127,  127,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106,  106,  106,  106,
      106,  106,  106,  106,  106,  106,  106

    } ;

static yy_state_type yy_last_accepting_state;
static char *yy_last_accepting_cpos;

extern int yy_flex_debug;
int yy_flex_debug = 1;

static yyconst flex_int16_t yy_rule_linenum[70] =
    {   0,
       80,   90,  102,  148,  158,  160,  162,  164,  166,  178,
      180,  182,  184,  206,  216,  218,  220,  222,  235,  245,
      247,  249,  251,  268,  278,  279,  280,  281,  282,  283,
      284,  285,  286,  287,  288,  289,  290,  291,  292,  293,
      294,  295,  296,  297,  298,  299,  300,  301,  302,  303,
      304,  305,  307,  313,  328,  334,  349,  388,  394,  400,
      405,  411,  426,  434,  436,  442,  460,  466,  468
    } ;

/* The intent behind this definition is that it'll catch
 * any uses of REJECT which flex missed.
 */
#define REJECT reject_used_but_not_detected
static int yy_more_flag = 0;
static int yy_more_len = 0;
#define yymore() ((yy_more_flag) = 1)
#define YY_MORE_ADJ (yy_more_len)
#define YY_RESTORE_YY_MORE_OFFSET
char *yytext;
#line 1 "rhlex.l"
/* -*-C-*- */
#line 4 "rhlex.l"

/*
 * rhlex.l
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 */

#if !defined(lint)
static char rcsid[] = "$Id: rhlex.l,v 1.1 1994/01/12 19:39:35 rick Exp $";
#endif

#include <memory.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <errno.h>

#if defined(DEBUG)
#  include <assert.h>
#endif

#include "rh.h"
#include "rhdata.h"
#include "rhgram.h"
#include "alloc.h"
#include "errmsg.h"
#include "sysinfo.h"

/* #define YY_DECL int rh_lex YY_PROTO(( void )) */
#define YY_DECL int rh_lex(void)

#define CHUNK_SIZE	32

#undef YY_INPUT
#define YY_INPUT(buf, result, max_size) (result = rh_read(buf, max_size))

#define RH_ATIME	1
#define RH_CTIME	2
#define RH_MTIME	3

static char *	Start_line = (char *) NULL;
static int	Start_line_num;
static int	Start_line_pos;
static int	First_line = 1;
char *		Line = (char *) NULL;
int		Line_len = 0;
int		Line_num = 0;
int		Line_pos = 0;
static int	OldSC;

static date_info		get_file_time(char *str);
static void			lex_error(const char * line,
					  int line_num,
					  const char *msg);
static int			lookup(const char *str);
extern void			rh_error();
static int			rh_read(char *buf, int max_size);
static void			remove_escapes(char *str);
extern long			strtol();







#line 742 "<stdout>"

#define INITIAL 0
#define COMMENT 1
#define DATE_SPEC 2
#define FILE_NAME 3
#define FILE_TIME 4
#define FIRST_LINE 5
#define NEW_LINE 6

#ifndef YY_NO_UNISTD_H
/* Special case for "unistd.h", since it is non-ANSI. We include it way
 * down here because we want the user's section 1 to have been scanned first.
 * The user has a chance to override it with an option.
 */
/* %if-c-only */
#include <unistd.h>
/* %endif */
/* %if-c++-only */
/* %endif */
#endif

#ifndef YY_EXTRA_TYPE
#define YY_EXTRA_TYPE void *
#endif

/* %if-c-only Reentrant structure and macros (non-C++). */
/* %if-reentrant */
/* %if-c-only */

static int yy_init_globals (void );

/* %endif */
/* %if-reentrant */
/* %endif */
/* %endif End reentrant structures and macros. */

/* Accessor methods to globals.
   These are made visible to non-reentrant scanners for convenience. */

int yylex_destroy (void );

int yyget_debug (void );

void yyset_debug (int debug_flag  );

YY_EXTRA_TYPE yyget_extra (void );

void yyset_extra (YY_EXTRA_TYPE user_defined  );

FILE *yyget_in (void );

void yyset_in  (FILE * in_str  );

FILE *yyget_out (void );

void yyset_out  (FILE * out_str  );

int yyget_leng (void );

char *yyget_text (void );

int yyget_lineno (void );

void yyset_lineno (int line_number  );

/* %if-bison-bridge */
/* %endif */

/* Macros after this point can all be overridden by user definitions in
 * section 1.
 */

#ifndef YY_SKIP_YYWRAP
#ifdef __cplusplus
extern "C" int yywrap (void );
#else
extern int yywrap (void );
#endif
#endif

/* %not-for-header */

    static void yyunput (int c,char *buf_ptr  );
    
/* %ok-for-header */

/* %endif */

#ifndef yytext_ptr
static void yy_flex_strncpy (char *,yyconst char *,int );
#endif

#ifdef YY_NEED_STRLEN
static int yy_flex_strlen (yyconst char * );
#endif

#ifndef YY_NO_INPUT
/* %if-c-only Standard (non-C++) definition */
/* %not-for-header */

#ifdef __cplusplus
static int yyinput (void );
#else
static int input (void );
#endif
/* %ok-for-header */

/* %endif */
#endif

/* %if-c-only */

/* %endif */

/* Amount of stuff to slurp up with each read. */
#ifndef YY_READ_BUF_SIZE
#define YY_READ_BUF_SIZE 8192
#endif

/* Copy whatever the last rule matched to the standard output. */
#ifndef ECHO
/* %if-c-only Standard (non-C++) definition */
/* This used to be an fputs(), but since the string might contain NUL's,
 * we now use fwrite().
 */
#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0)
/* %endif */
/* %if-c++-only C++ definition */
/* %endif */
#endif

/* Gets input and stuffs it into "buf".  number of characters read, or YY_NULL,
 * is returned in "result".
 */
#ifndef YY_INPUT
#define YY_INPUT(buf,result,max_size) \
/* %% [5.0] fread()/read() definition of YY_INPUT goes here unless we're doing C++ \ */\
	if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
		{ \
		int c = '*'; \
		size_t n; \
		for ( n = 0; n < max_size && \
			     (c = getc( yyin )) != EOF && c != '\n'; ++n ) \
			buf[n] = (char) c; \
		if ( c == '\n' ) \
			buf[n++] = (char) c; \
		if ( c == EOF && ferror( yyin ) ) \
			YY_FATAL_ERROR( "input in flex scanner failed" ); \
		result = n; \
		} \
	else \
		{ \
		errno=0; \
		while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
			{ \
			if( errno != EINTR) \
				{ \
				YY_FATAL_ERROR( "input in flex scanner failed" ); \
				break; \
				} \
			errno=0; \
			clearerr(yyin); \
			} \
		}\
\
/* %if-c++-only C++ definition \ */\
/* %endif */

#endif

/* No semi-colon after return; correct usage is to write "yyterminate();" -
 * we don't want an extra ';' after the "return" because that will cause
 * some compilers to complain about unreachable statements.
 */
#ifndef yyterminate
#define yyterminate() return YY_NULL
#endif

/* Number of entries by which start-condition stack grows. */
#ifndef YY_START_STACK_INCR
#define YY_START_STACK_INCR 25
#endif

/* Report a fatal error. */
#ifndef YY_FATAL_ERROR
/* %if-c-only */
#define YY_FATAL_ERROR(msg) yy_fatal_error( msg )
/* %endif */
/* %if-c++-only */
/* %endif */
#endif

/* %if-tables-serialization structures and prototypes */
/* %not-for-header */

/* %ok-for-header */

/* %not-for-header */

/* %tables-yydmap generated elements */
/* %endif */
/* end tables serialization structures and prototypes */

/* %ok-for-header */

/* Default declaration of generated scanner - a define so the user can
 * easily add parameters.
 */
#ifndef YY_DECL
#define YY_DECL_IS_OURS 1
/* %if-c-only Standard (non-C++) definition */

extern int yylex (void);

#define YY_DECL int yylex (void)
/* %endif */
/* %if-c++-only C++ definition */
/* %endif */
#endif /* !YY_DECL */

/* Code executed at the beginning of each rule, after yytext and yyleng
 * have been set up.
 */
#ifndef YY_USER_ACTION
#define YY_USER_ACTION
#endif

/* Code executed at the end of each rule. */
#ifndef YY_BREAK
#define YY_BREAK break;
#endif

/* %% [6.0] YY_RULE_SETUP definition goes here */
#define YY_RULE_SETUP \
	YY_USER_ACTION

/* %not-for-header */

/** The main scanner function which does all the work.
 */
YY_DECL
{
	register yy_state_type yy_current_state;
	register char *yy_cp, *yy_bp;
	register int yy_act;
    
/* %% [7.0] user's declarations go here */
#line 73 "rhlex.l"


    if (First_line) {
	BEGIN(FIRST_LINE);
	First_line = 0;
    }

#line 998 "<stdout>"

	if ( !(yy_init) )
		{
		(yy_init) = 1;

#ifdef YY_USER_INIT
		YY_USER_INIT;
#endif

		if ( ! (yy_start) )
			(yy_start) = 1;	/* first start state */

		if ( ! yyin )
/* %if-c-only */
			yyin = stdin;
/* %endif */
/* %if-c++-only */
/* %endif */

		if ( ! yyout )
/* %if-c-only */
			yyout = stdout;
/* %endif */
/* %if-c++-only */
/* %endif */

		if ( ! YY_CURRENT_BUFFER ) {
			yyensure_buffer_stack ();
			YY_CURRENT_BUFFER_LVALUE =
				yy_create_buffer(yyin,YY_BUF_SIZE );
		}

		yy_load_buffer_state( );
		}

	while ( 1 )		/* loops until end-of-file is reached */
		{
/* %% [8.0] yymore()-related code goes here */
		(yy_more_len) = 0;
		if ( (yy_more_flag) )
			{
			(yy_more_len) = (yy_c_buf_p) - (yytext_ptr);
			(yy_more_flag) = 0;
			}
		yy_cp = (yy_c_buf_p);

		/* Support of yytext. */
		*yy_cp = (yy_hold_char);

		/* yy_bp points to the position in yy_ch_buf of the start of
		 * the current run.
		 */
		yy_bp = yy_cp;

/* %% [9.0] code to set up and find next match goes here */
		yy_current_state = (yy_start);
yy_match:
		do
			{
			register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)];
			if ( yy_accept[yy_current_state] )
				{
				(yy_last_accepting_state) = yy_current_state;
				(yy_last_accepting_cpos) = yy_cp;
				}
			while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
				{
				yy_current_state = (int) yy_def[yy_current_state];
				if ( yy_current_state >= 107 )
					yy_c = yy_meta[(unsigned int) yy_c];
				}
			yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
			++yy_cp;
			}
		while ( yy_base[yy_current_state] != 354 );

yy_find_action:
/* %% [10.0] code to find the action number goes here */
		yy_act = yy_accept[yy_current_state];
		if ( yy_act == 0 )
			{ /* have to back up */
			yy_cp = (yy_last_accepting_cpos);
			yy_current_state = (yy_last_accepting_state);
			yy_act = yy_accept[yy_current_state];
			}

		YY_DO_BEFORE_ACTION;

/* %% [11.0] code for yylineno update goes here */

do_action:	/* This label is used only to access EOF actions. */

/* %% [12.0] debug code goes here */
		if ( yy_flex_debug )
			{
			if ( yy_act == 0 )
				fprintf( stderr, "--scanner backing up\n" );
			else if ( yy_act < 70 )
				fprintf( stderr, "--accepting rule at line %ld (\"%s\")\n",
				         (long)yy_rule_linenum[yy_act], yytext );
			else if ( yy_act == 70 )
				fprintf( stderr, "--accepting default rule (\"%s\")\n",
				         yytext );
			else if ( yy_act == 71 )
				fprintf( stderr, "--(end of buffer or a NUL)\n" );
			else
				fprintf( stderr, "--EOF (start condition %d)\n", YY_START );
			}

		switch ( yy_act )
	{ /* beginning of action switch */
/* %% [13.0] actions go here */
			case 0: /* must back up */
			/* undo the effects of YY_DO_BEFORE_ACTION */
			*yy_cp = (yy_hold_char);
			yy_cp = (yy_last_accepting_cpos);
			yy_current_state = (yy_last_accepting_state);
			goto yy_find_action;

case 1:
YY_RULE_SETUP
#line 80 "rhlex.l"
{
			    Line = ALLOCATE(yyleng + 1);
			    (void) strcpy(Line, yytext);
			    Line_len = yyleng;
			    Line_num = 1;
			    Line_pos = 0;
			    BEGIN(INITIAL);
			    yyless(0);
			}
	YY_BREAK
case 2:
/* rule 2 can match eol */
YY_RULE_SETUP
#line 90 "rhlex.l"
{
			    Line = ALLOCATE(81);
			    *Line = '\0';
			    Line_len = 80;
			    Line_num = 1;
			    Line_pos = 0;
			    BEGIN(INITIAL);
			    yyless(0);
			}
	YY_BREAK
case YY_STATE_EOF(FIRST_LINE):
#line 100 "rhlex.l"
{ yyterminate(); }
	YY_BREAK
case 3:
YY_RULE_SETUP
#line 102 "rhlex.l"
{
			    int adjustment = 0;
			    int len;
			    int lp;
			    char *from;
			    char *to;

			    for (from = yytext; *from != '\0'; from++) {
				if (*from == '\t') {
				    adjustment += 8;
				}
			    }
			    len = yyleng + adjustment;
			    if (len > Line_len) {
				Line = REALLOCATE(Line, len + 1);
				Line_len = len;
			    }
			    if (adjustment) {
				for (from = yytext, to = Line, lp = 0;
				     *from != '\0';
				     /* void */) {
				    if (*from == '\t') {
					adjustment = 8 - (lp + 1) % 8;
					lp += adjustment;
					while (adjustment) {
					    *to++ = ' ';
					    adjustment--;
					}
					from++;
				    }
				    else {
					*to++ = *from++;
					lp++;
				    }
				}
				*to = *from;
			    }
			    else {
				(void) strcpy(Line, yytext);
			    }
			    Line_num++;
			    Line_pos = 0;
			    BEGIN(OldSC);
			    yyless(0);
			}
	YY_BREAK
case 4:
/* rule 4 can match eol */
YY_RULE_SETUP
#line 148 "rhlex.l"
{ *Line = '\0'; Line_num++; Line_pos = 0; }
	YY_BREAK
case YY_STATE_EOF(NEW_LINE):
#line 150 "rhlex.l"
{
			    if (OldSC == COMMENT) {
				lex_error(Start_line, Start_line_num,
					  "unterminated comment");
			    }
			    yyterminate();
			}
	YY_BREAK
case 5:
YY_RULE_SETUP
#line 158 "rhlex.l"
{ Line_pos += yyleng; }
	YY_BREAK
case 6:
/* rule 6 can match eol */
YY_RULE_SETUP
#line 160 "rhlex.l"
{ OldSC = COMMENT; BEGIN(NEW_LINE); }
	YY_BREAK
case 7:
YY_RULE_SETUP
#line 162 "rhlex.l"
{ Line_pos += yyleng; }
	YY_BREAK
case 8:
/* rule 8 can match eol */
YY_RULE_SETUP
#line 164 "rhlex.l"
{ OldSC = COMMENT; BEGIN(NEW_LINE); }
	YY_BREAK
case 9:
YY_RULE_SETUP
#line 166 "rhlex.l"
{
			    Line_pos += yyleng;
			    free(Start_line);
			    Start_line = (char *) NULL;
			    BEGIN(INITIAL);
			}
	YY_BREAK
case YY_STATE_EOF(COMMENT):
#line 173 "rhlex.l"
{
			    lex_error(Start_line, Start_line_num,
				      "unterminated comment");
			}
	YY_BREAK
case 10:
YY_RULE_SETUP
#line 178 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 11:
YY_RULE_SETUP
#line 180 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 12:
YY_RULE_SETUP
#line 182 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 13:
YY_RULE_SETUP
#line 184 "rhlex.l"
{
			    char *date_str;

			    if (yyleng == 1) {
				lex_error(Line, Line_num,
					  "empty date specification");
			    }
			    Line_pos += yyleng;
			    date_str = SAVESTR(yytext);
			    date_str[yyleng - 1] = '\0';
			    remove_escapes(date_str);
			    rh_lval.di = ALLOCATE(sizeof(date_info));
			    *rh_lval.di = getdate(date_str,
						  (struct timeb *) NULL);
			    free(date_str);
			    if (rh_lval.di->seconds == -1) {
				rh_error("invalid date specification");
			    }
			    BEGIN(INITIAL);
			    return DATESPEC;
			}
	YY_BREAK
case 14:
/* rule 14 can match eol */
YY_RULE_SETUP
#line 206 "rhlex.l"
{
			    lex_error(Line, Line_num,
				      "unterminated date specification");
			}
	YY_BREAK
case YY_STATE_EOF(DATE_SPEC):
#line 211 "rhlex.l"
{
			    lex_error(Line, Line_num,
				      "unterminated date specification");
			}
	YY_BREAK
case 15:
YY_RULE_SETUP
#line 216 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 16:
YY_RULE_SETUP
#line 218 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 17:
YY_RULE_SETUP
#line 220 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 18:
YY_RULE_SETUP
#line 222 "rhlex.l"
{
			    if (yyleng == 1) {
				lex_error(Line, Line_num,
					  "empty file name pattern");
			    }
			    Line_pos += yyleng;
			    rh_lval.str = SAVESTR(yytext);
			    rh_lval.str[yyleng - 1] = '\0';
			    BEGIN(INITIAL);
			    remove_escapes(rh_lval.str);
			    return FILENAME_PATTERN;
			}
	YY_BREAK
case 19:
/* rule 19 can match eol */
YY_RULE_SETUP
#line 235 "rhlex.l"
{
			    lex_error(Line, Line_num,
				      "unterminated file name pattern");
			}
	YY_BREAK
case YY_STATE_EOF(FILE_NAME):
#line 240 "rhlex.l"
{
			    lex_error(Line, Line_num,
				      "unterminated file name pattern");
			}
	YY_BREAK
case 20:
YY_RULE_SETUP
#line 245 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 21:
YY_RULE_SETUP
#line 247 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 22:
YY_RULE_SETUP
#line 249 "rhlex.l"
{ yymore(); }
	YY_BREAK
case 23:
YY_RULE_SETUP
#line 251 "rhlex.l"
{
			    char *ftspec;

			    if (yyleng == 1) {
				lex_error(Line, Line_num,
					  "empty file time operator");
			    }
			    ftspec = SAVESTR(yytext);
			    ftspec[yyleng - 1] = '\0';
			    rh_lval.di = ALLOCATE(sizeof(date_info));
			    *rh_lval.di = get_file_time(ftspec);
			    Line_pos += yyleng;
			    free(ftspec);
			    BEGIN(INITIAL);
			    return FILETIME_FIELD;
			}
	YY_BREAK
case 24:
/* rule 24 can match eol */
YY_RULE_SETUP
#line 268 "rhlex.l"
{
			    lex_error(Line, Line_num,
				      "unterminated file time operator");
			}
	YY_BREAK
case YY_STATE_EOF(FILE_TIME):
#line 273 "rhlex.l"
{
			    lex_error(Line, Line_num,
				      "unterminated file time operator");
			}
	YY_BREAK
case 25:
YY_RULE_SETUP
#line 278 "rhlex.l"
{ Line_pos += yyleng; return '!'; }
	YY_BREAK
case 26:
YY_RULE_SETUP
#line 279 "rhlex.l"
{ Line_pos += yyleng; return NE; }
	YY_BREAK
case 27:
YY_RULE_SETUP
#line 280 "rhlex.l"
{ Line_pos += yyleng; return '%'; }
	YY_BREAK
case 28:
YY_RULE_SETUP
#line 281 "rhlex.l"
{ Line_pos += yyleng; return '&'; }
	YY_BREAK
case 29:
YY_RULE_SETUP
#line 282 "rhlex.l"
{ Line_pos += yyleng; return LOGAND; }
	YY_BREAK
case 30:
YY_RULE_SETUP
#line 283 "rhlex.l"
{ Line_pos += yyleng; return '('; }
	YY_BREAK
case 31:
YY_RULE_SETUP
#line 284 "rhlex.l"
{ Line_pos += yyleng; return ')'; }
	YY_BREAK
case 32:
YY_RULE_SETUP
#line 285 "rhlex.l"
{ Line_pos += yyleng; return '*'; }
	YY_BREAK
case 33:
YY_RULE_SETUP
#line 286 "rhlex.l"
{ Line_pos += yyleng; return '+'; }
	YY_BREAK
case 34:
YY_RULE_SETUP
#line 287 "rhlex.l"
{ Line_pos += yyleng; return ','; }
	YY_BREAK
case 35:
YY_RULE_SETUP
#line 288 "rhlex.l"
{ Line_pos += yyleng; return '-'; }
	YY_BREAK
case 36:
YY_RULE_SETUP
#line 289 "rhlex.l"
{ Line_pos += yyleng; return '/'; }
	YY_BREAK
case 37:
YY_RULE_SETUP
#line 290 "rhlex.l"
{ Line_pos += yyleng; return ':'; }
	YY_BREAK
case 38:
YY_RULE_SETUP
#line 291 "rhlex.l"
{ Line_pos += yyleng; return ';'; }
	YY_BREAK
case 39:
YY_RULE_SETUP
#line 292 "rhlex.l"
{ Line_pos += yyleng; return '<'; }
	YY_BREAK
case 40:
YY_RULE_SETUP
#line 293 "rhlex.l"
{ Line_pos += yyleng; return LSHIFT; }
	YY_BREAK
case 41:
YY_RULE_SETUP
#line 294 "rhlex.l"
{ Line_pos += yyleng; return LE; }
	YY_BREAK
case 42:
YY_RULE_SETUP
#line 295 "rhlex.l"
{ Line_pos += yyleng; return EQ; }
	YY_BREAK
case 43:
YY_RULE_SETUP
#line 296 "rhlex.l"
{ Line_pos += yyleng; return '>'; }
	YY_BREAK
case 44:
YY_RULE_SETUP
#line 297 "rhlex.l"
{ Line_pos += yyleng; return GE; }
	YY_BREAK
case 45:
YY_RULE_SETUP
#line 298 "rhlex.l"
{ Line_pos += yyleng; return RSHIFT; }
	YY_BREAK
case 46:
YY_RULE_SETUP
#line 299 "rhlex.l"
{ Line_pos += yyleng; return '?'; }
	YY_BREAK
case 47:
YY_RULE_SETUP
#line 300 "rhlex.l"
{ Line_pos += yyleng; return '^'; }
	YY_BREAK
case 48:
YY_RULE_SETUP
#line 301 "rhlex.l"
{ Line_pos += yyleng; return '{'; }
	YY_BREAK
case 49:
YY_RULE_SETUP
#line 302 "rhlex.l"
{ Line_pos += yyleng; return '|'; }
	YY_BREAK
case 50:
YY_RULE_SETUP
#line 303 "rhlex.l"
{ Line_pos += yyleng; return LOGOR; }
	YY_BREAK
case 51:
YY_RULE_SETUP
#line 304 "rhlex.l"
{ Line_pos += yyleng; return '}'; }
	YY_BREAK
case 52:
YY_RULE_SETUP
#line 305 "rhlex.l"
{ Line_pos += yyleng; return '~'; }
	YY_BREAK
case 53:
YY_RULE_SETUP
#line 307 "rhlex.l"
{
			    Line_pos += yyleng;
			    rh_lval.value = getuid();
			    return NUMBER;
			}
	YY_BREAK
case 54:
YY_RULE_SETUP
#line 313 "rhlex.l"
{
			    user_info *uip;

			    Line_pos += yyleng;
			    if (yyleng == 1) {
				rh_error("user name missing");
			    }
			    uip = getuinam(yytext + 1);
			    if (uip == (user_info *) NULL) {
				rh_error("no such user");
			    }
			    rh_lval.value = uip->uid;
			    return NUMBER;
			}
	YY_BREAK
case 55:
YY_RULE_SETUP
#line 328 "rhlex.l"
{
			    Line_pos += yyleng;
			    rh_lval.value = getgid();
			    return NUMBER;
			}
	YY_BREAK
case 56:
YY_RULE_SETUP
#line 334 "rhlex.l"
{
			    group_info *gip;

			    Line_pos += yyleng;
			    if (yyleng == 1) {
				rh_error("group name missing");
			    }
			    gip = getginam(yytext + 1);
			    if (gip == (group_info *) NULL) {
				rh_error("no such group");
			    }
			    rh_lval.value = gip->gid;
			    return NUMBER;
			}
	YY_BREAK
case 57:
YY_RULE_SETUP
#line 349 "rhlex.l"
{
			    char *end;

			    Line_pos += yyleng;
			    rh_lval.value = strtol(yytext, &end, 10);

			    switch (*end) {

			    case 'G':
				rh_lval.value *= 1024 * 1024 * 1024;
				break;

			    case 'K':
				rh_lval.value *= 1024;
				break;

			    case 'M':
				rh_lval.value *= 1024 * 1024;
				break;

			    case 'd':
				rh_lval.value *= 60 * 60 * 24;
				break;

			    case 'h':
				rh_lval.value *= 60 * 60;
				break;

			    case 'm':
				rh_lval.value *= 60;
				break;

			    case 'w':
				rh_lval.value *= 60 * 60 * 24 * 7;
				break;
			    }
			    return NUMBER;
			}
	YY_BREAK
case 58:
YY_RULE_SETUP
#line 388 "rhlex.l"
{
			    Line_pos += yyleng;
			    rh_lval.value = strtol(yytext, (char **) NULL, 0);
			    return NUMBER;
			}
	YY_BREAK
case 59:
YY_RULE_SETUP
#line 394 "rhlex.l"
{
			    Line_pos += yyleng;
			    rh_lval.value = strtol(yytext, (char **) NULL, 0);
			    return NUMBER;
			}
	YY_BREAK
case 60:
YY_RULE_SETUP
#line 400 "rhlex.l"
{
			    Line_pos += yyleng;
			    return lookup(yytext);
			}
	YY_BREAK
case 61:
YY_RULE_SETUP
#line 405 "rhlex.l"
{
			    Start_line_pos = Line_pos + 1;
			    Line_pos += yyleng;
			    BEGIN(FILE_NAME);
			}
	YY_BREAK
case 62:
YY_RULE_SETUP
#line 411 "rhlex.l"
{
			    char *p;

			    Start_line_pos = Line_pos + 1;
			    for (p = yytext; *p != '\0'; p++) {
				if (*p == '\t') {
				    Line_pos += (8 - (Line_pos + 1) % 8);
				}
				else {
				    Line_pos++;
				}
			    }
			    BEGIN(DATE_SPEC);
			}
	YY_BREAK
case 63:
YY_RULE_SETUP
#line 426 "rhlex.l"
{
			    Start_line_pos = Line_pos + 1;
			    Start_line_num = Line_num;
			    Start_line = SAVESTR(Line);
			    Line_pos += yyleng;
			    BEGIN(COMMENT);
			}
	YY_BREAK
case 64:
YY_RULE_SETUP
#line 434 "rhlex.l"
{ }	/* simple comment - ignore to end of line */
	YY_BREAK
case 65:
YY_RULE_SETUP
#line 436 "rhlex.l"
{
			    Line_pos += yyleng;
			    rh_lval.str = SAVESTR(yytext);
			    return FSTYPE;
			}
	YY_BREAK
case 66:
YY_RULE_SETUP
#line 442 "rhlex.l"
{			/* ignore white space */
			    char *p;

			    for (p = yytext; *p != '\0'; p++) {
				if (*p == '\f') {
				    Line_num++;
				    Line_pos = 0;
				    *Line = '\0';
				}
				else if (*p == ' ') {
				    Line_pos++;
				}
				else {
				    Line_pos += (8 - (Line_pos + 1) % 8);
				}
			    }
			}
	YY_BREAK
case 67:
YY_RULE_SETUP
#line 460 "rhlex.l"
{
			    Start_line_pos = Line_pos + 1;
			    Line_pos += yyleng;
			    BEGIN(FILE_TIME);
			}
	YY_BREAK
case 68:
/* rule 68 can match eol */
YY_RULE_SETUP
#line 466 "rhlex.l"
{ OldSC = INITIAL; BEGIN(NEW_LINE); }
	YY_BREAK
case 69:
YY_RULE_SETUP
#line 468 "rhlex.l"
{ Line_pos += yyleng; return yytext[0]; }
	YY_BREAK
case YY_STATE_EOF(INITIAL):
#line 470 "rhlex.l"
{ yyterminate(); }
	YY_BREAK
case 70:
YY_RULE_SETUP
#line 472 "rhlex.l"
YY_FATAL_ERROR( "flex scanner jammed" );
	YY_BREAK
#line 1771 "<stdout>"

	case YY_END_OF_BUFFER:
		{
		/* Amount of text matched not including the EOB char. */
		int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1;

		/* Undo the effects of YY_DO_BEFORE_ACTION. */
		*yy_cp = (yy_hold_char);
		YY_RESTORE_YY_MORE_OFFSET

		if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW )
			{
			/* We're scanning a new file or input source.  It's
			 * possible that this happened because the user
			 * just pointed yyin at a new source and called
			 * yylex().  If so, then we have to assure
			 * consistency between YY_CURRENT_BUFFER and our
			 * globals.  Here is the right place to do so, because
			 * this is the first action (other than possibly a
			 * back-up) that will match for the new input source.
			 */
			(yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars;
			YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin;
			YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL;
			}

		/* Note that here we test for yy_c_buf_p "<=" to the position
		 * of the first EOB in the buffer, since yy_c_buf_p will
		 * already have been incremented past the NUL character
		 * (since all states make transitions on EOB to the
		 * end-of-buffer state).  Contrast this with the test
		 * in input().
		 */
		if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] )
			{ /* This was really a NUL. */
			yy_state_type yy_next_state;

			(yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text;

			yy_current_state = yy_get_previous_state(  );

			/* Okay, we're now positioned to make the NUL
			 * transition.  We couldn't have
			 * yy_get_previous_state() go ahead and do it
			 * for us because it doesn't know how to deal
			 * with the possibility of jamming (and we don't
			 * want to build jamming into it because then it
			 * will run more slowly).
			 */

			yy_next_state = yy_try_NUL_trans( yy_current_state );

			yy_bp = (yytext_ptr) + YY_MORE_ADJ;

			if ( yy_next_state )
				{
				/* Consume the NUL. */
				yy_cp = ++(yy_c_buf_p);
				yy_current_state = yy_next_state;
				goto yy_match;
				}

			else
				{
/* %% [14.0] code to do back-up for compressed tables and set up yy_cp goes here */
				yy_cp = (yy_c_buf_p);
				goto yy_find_action;
				}
			}

		else switch ( yy_get_next_buffer(  ) )
			{
			case EOB_ACT_END_OF_FILE:
				{
				(yy_did_buffer_switch_on_eof) = 0;

				if ( yywrap( ) )
					{
					/* Note: because we've taken care in
					 * yy_get_next_buffer() to have set up
					 * yytext, we can now set up
					 * yy_c_buf_p so that if some total
					 * hoser (like flex itself) wants to
					 * call the scanner after we return the
					 * YY_NULL, it'll still work - another
					 * YY_NULL will get returned.
					 */
					(yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ;

					yy_act = YY_STATE_EOF(YY_START);
					goto do_action;
					}

				else
					{
					if ( ! (yy_did_buffer_switch_on_eof) )
						YY_NEW_FILE;
					}
				break;
				}

			case EOB_ACT_CONTINUE_SCAN:
				(yy_c_buf_p) =
					(yytext_ptr) + yy_amount_of_matched_text;

				yy_current_state = yy_get_previous_state(  );

				yy_cp = (yy_c_buf_p);
				yy_bp = (yytext_ptr) + YY_MORE_ADJ;
				goto yy_match;

			case EOB_ACT_LAST_MATCH:
				(yy_c_buf_p) =
				&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)];

				yy_current_state = yy_get_previous_state(  );

				yy_cp = (yy_c_buf_p);
				yy_bp = (yytext_ptr) + YY_MORE_ADJ;
				goto yy_find_action;
			}
		break;
		}

	default:
		YY_FATAL_ERROR(
			"fatal flex scanner internal error--no action found" );
	} /* end of action switch */
		} /* end of scanning one token */
} /* end of yylex */
/* %ok-for-header */

/* %if-c++-only */
/* %not-for-header */

/* %ok-for-header */

/* %endif */

/* yy_get_next_buffer - try to read in a new buffer
 *
 * Returns a code representing an action:
 *	EOB_ACT_LAST_MATCH -
 *	EOB_ACT_CONTINUE_SCAN - continue scanning from current position
 *	EOB_ACT_END_OF_FILE - end of file
 */
/* %if-c-only */
static int yy_get_next_buffer (void)
/* %endif */
/* %if-c++-only */
/* %endif */
{
    	register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf;
	register char *source = (yytext_ptr);
	register int number_to_move, i;
	int ret_val;

	if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] )
		YY_FATAL_ERROR(
		"fatal flex scanner internal error--end of buffer missed" );

	if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 )
		{ /* Don't try to fill the buffer, so this is an EOF. */
		if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 )
			{
			/* We matched a single character, the EOB, so
			 * treat this as a final EOF.
			 */
			return EOB_ACT_END_OF_FILE;
			}

		else
			{
			/* We matched some text prior to the EOB, first
			 * process it.
			 */
			return EOB_ACT_LAST_MATCH;
			}
		}

	/* Try to read more data. */

	/* First move last chars to start of buffer. */
	number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1;

	for ( i = 0; i < number_to_move; ++i )
		*(dest++) = *(source++);

	if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING )
		/* don't do the read, it's not guaranteed to return an EOF,
		 * just force an EOF
		 */
		YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0;

	else
		{
			int num_to_read =
			YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1;

		while ( num_to_read <= 0 )
			{ /* Not enough room in the buffer - grow it. */

			/* just a shorter name for the current buffer */
			YY_BUFFER_STATE b = YY_CURRENT_BUFFER;

			int yy_c_buf_p_offset =
				(int) ((yy_c_buf_p) - b->yy_ch_buf);

			if ( b->yy_is_our_buffer )
				{
				int new_size = b->yy_buf_size * 2;

				if ( new_size <= 0 )
					b->yy_buf_size += b->yy_buf_size / 8;
				else
					b->yy_buf_size *= 2;

				b->yy_ch_buf = (char *)
					/* Include room in for 2 EOB chars. */
					yyrealloc((void *) b->yy_ch_buf,b->yy_buf_size + 2  );
				}
			else
				/* Can't grow it, we don't own it. */
				b->yy_ch_buf = 0;

			if ( ! b->yy_ch_buf )
				YY_FATAL_ERROR(
				"fatal error - scanner input buffer overflow" );

			(yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset];

			num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size -
						number_to_move - 1;

			}

		if ( num_to_read > YY_READ_BUF_SIZE )
			num_to_read = YY_READ_BUF_SIZE;

		/* Read in more data. */
		YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
			(yy_n_chars), (size_t) num_to_read );

		YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars);
		}

	if ( (yy_n_chars) == 0 )
		{
		if ( number_to_move == YY_MORE_ADJ )
			{
			ret_val = EOB_ACT_END_OF_FILE;
			yyrestart(yyin  );
			}

		else
			{
			ret_val = EOB_ACT_LAST_MATCH;
			YY_CURRENT_BUFFER_LVALUE->yy_buffer_status =
				YY_BUFFER_EOF_PENDING;
			}
		}

	else
		ret_val = EOB_ACT_CONTINUE_SCAN;

	if ((yy_size_t) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
		/* Extend the array by 50%, plus the number we really need. */
		yy_size_t new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1);
		YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size  );
		if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
			YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" );
	}

	(yy_n_chars) += number_to_move;
	YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR;
	YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR;

	(yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0];

	return ret_val;
}

/* yy_get_previous_state - get the state just before the EOB char was reached */

/* %if-c-only */
/* %not-for-header */

    static yy_state_type yy_get_previous_state (void)
/* %endif */
/* %if-c++-only */
/* %endif */
{
	register yy_state_type yy_current_state;
	register char *yy_cp;
    
/* %% [15.0] code to get the start state into yy_current_state goes here */
	yy_current_state = (yy_start);

	for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp )
		{
/* %% [16.0] code to find the next state goes here */
		register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1);
		if ( yy_accept[yy_current_state] )
			{
			(yy_last_accepting_state) = yy_current_state;
			(yy_last_accepting_cpos) = yy_cp;
			}
		while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
			{
			yy_current_state = (int) yy_def[yy_current_state];
			if ( yy_current_state >= 107 )
				yy_c = yy_meta[(unsigned int) yy_c];
			}
		yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
		}

	return yy_current_state;
}

/* yy_try_NUL_trans - try to make a transition on the NUL character
 *
 * synopsis
 *	next_state = yy_try_NUL_trans( current_state );
 */
/* %if-c-only */
    static yy_state_type yy_try_NUL_trans  (yy_state_type yy_current_state )
/* %endif */
/* %if-c++-only */
/* %endif */
{
	register int yy_is_jam;
    /* %% [17.0] code to find the next state, and perhaps do backing up, goes here */
	register char *yy_cp = (yy_c_buf_p);

	register YY_CHAR yy_c = 1;
	if ( yy_accept[yy_current_state] )
		{
		(yy_last_accepting_state) = yy_current_state;
		(yy_last_accepting_cpos) = yy_cp;
		}
	while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
		{
		yy_current_state = (int) yy_def[yy_current_state];
		if ( yy_current_state >= 107 )
			yy_c = yy_meta[(unsigned int) yy_c];
		}
	yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
	yy_is_jam = (yy_current_state == 106);

	return yy_is_jam ? 0 : yy_current_state;
}

/* %if-c-only */

    static void yyunput (int c, register char * yy_bp )
/* %endif */
/* %if-c++-only */
/* %endif */
{
	register char *yy_cp;
    
    yy_cp = (yy_c_buf_p);

	/* undo effects of setting up yytext */
	*yy_cp = (yy_hold_char);

	if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
		{ /* need to shift things up to make room */
		/* +2 for EOB chars. */
		register int number_to_move = (yy_n_chars) + 2;
		register char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[
					YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2];
		register char *source =
				&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move];

		while ( source > YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
			*--dest = *--source;

		yy_cp += (int) (dest - source);
		yy_bp += (int) (dest - source);
		YY_CURRENT_BUFFER_LVALUE->yy_n_chars =
			(yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_buf_size;

		if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
			YY_FATAL_ERROR( "flex scanner push-back overflow" );
		}

	*--yy_cp = (char) c;

/* %% [18.0] update yylineno here */

	(yytext_ptr) = yy_bp;
	(yy_hold_char) = *yy_cp;
	(yy_c_buf_p) = yy_cp;
}
/* %if-c-only */

/* %endif */

/* %if-c-only */
#ifndef YY_NO_INPUT
#ifdef __cplusplus
    static int yyinput (void)
#else
    static int input  (void)
#endif

/* %endif */
/* %if-c++-only */
/* %endif */
{
	int c;
    
	*(yy_c_buf_p) = (yy_hold_char);

	if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR )
		{
		/* yy_c_buf_p now points to the character we want to return.
		 * If this occurs *before* the EOB characters, then it's a
		 * valid NUL; if not, then we've hit the end of the buffer.
		 */
		if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] )
			/* This was really a NUL. */
			*(yy_c_buf_p) = '\0';

		else
			{ /* need more input */
			int offset = (yy_c_buf_p) - (yytext_ptr);
			++(yy_c_buf_p);

			switch ( yy_get_next_buffer(  ) )
				{
				case EOB_ACT_LAST_MATCH:
					/* This happens because yy_g_n_b()
					 * sees that we've accumulated a
					 * token and flags that we need to
					 * try matching the token before
					 * proceeding.  But for input(),
					 * there's no matching to consider.
					 * So convert the EOB_ACT_LAST_MATCH
					 * to EOB_ACT_END_OF_FILE.
					 */

					/* Reset buffer status. */
					yyrestart(yyin );

					/*FALLTHROUGH*/

				case EOB_ACT_END_OF_FILE:
					{
					if ( yywrap( ) )
						return EOF;

					if ( ! (yy_did_buffer_switch_on_eof) )
						YY_NEW_FILE;
#ifdef __cplusplus
					return yyinput();
#else
					return input();
#endif
					}

				case EOB_ACT_CONTINUE_SCAN:
					(yy_c_buf_p) = (yytext_ptr) + offset;
					break;
				}
			}
		}

	c = *(unsigned char *) (yy_c_buf_p);	/* cast for 8-bit char's */
	*(yy_c_buf_p) = '\0';	/* preserve yytext */
	(yy_hold_char) = *++(yy_c_buf_p);

/* %% [19.0] update BOL and yylineno */

	return c;
}
/* %if-c-only */
#endif	/* ifndef YY_NO_INPUT */
/* %endif */

/** Immediately switch to a different input stream.
 * @param input_file A readable stream.
 * 
 * @note This function does not reset the start condition to @c INITIAL .
 */
/* %if-c-only */
    void yyrestart  (FILE * input_file )
/* %endif */
/* %if-c++-only */
/* %endif */
{
    
	if ( ! YY_CURRENT_BUFFER ){
        yyensure_buffer_stack ();
		YY_CURRENT_BUFFER_LVALUE =
            yy_create_buffer(yyin,YY_BUF_SIZE );
	}

	yy_init_buffer(YY_CURRENT_BUFFER,input_file );
	yy_load_buffer_state( );
}

/** Switch to a different input buffer.
 * @param new_buffer The new input buffer.
 * 
 */
/* %if-c-only */
    void yy_switch_to_buffer  (YY_BUFFER_STATE  new_buffer )
/* %endif */
/* %if-c++-only */
/* %endif */
{
    
	/* TODO. We should be able to replace this entire function body
	 * with
	 *		yypop_buffer_state();
	 *		yypush_buffer_state(new_buffer);
     */
	yyensure_buffer_stack ();
	if ( YY_CURRENT_BUFFER == new_buffer )
		return;

	if ( YY_CURRENT_BUFFER )
		{
		/* Flush out information for old buffer. */
		*(yy_c_buf_p) = (yy_hold_char);
		YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p);
		YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars);
		}

	YY_CURRENT_BUFFER_LVALUE = new_buffer;
	yy_load_buffer_state( );

	/* We don't actually know whether we did this switch during
	 * EOF (yywrap()) processing, but the only time this flag
	 * is looked at is after yywrap() is called, so it's safe
	 * to go ahead and always set it.
	 */
	(yy_did_buffer_switch_on_eof) = 1;
}

/* %if-c-only */
static void yy_load_buffer_state  (void)
/* %endif */
/* %if-c++-only */
/* %endif */
{
    	(yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars;
	(yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos;
	yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file;
	(yy_hold_char) = *(yy_c_buf_p);
}

/** Allocate and initialize an input buffer state.
 * @param file A readable stream.
 * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE.
 * 
 * @return the allocated buffer state.
 */
/* %if-c-only */
    YY_BUFFER_STATE yy_create_buffer  (FILE * file, int  size )
/* %endif */
/* %if-c++-only */
/* %endif */
{
	YY_BUFFER_STATE b;
    
	b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state )  );
	if ( ! b )
		YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );

	b->yy_buf_size = size;

	/* yy_ch_buf has to be 2 characters longer than the size given because
	 * we need to put in 2 end-of-buffer characters.
	 */
	b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2  );
	if ( ! b->yy_ch_buf )
		YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );

	b->yy_is_our_buffer = 1;

	yy_init_buffer(b,file );

	return b;
}

/** Destroy the buffer.
 * @param b a buffer created with yy_create_buffer()
 * 
 */
/* %if-c-only */
    void yy_delete_buffer (YY_BUFFER_STATE  b )
/* %endif */
/* %if-c++-only */
/* %endif */
{
    
	if ( ! b )
		return;

	if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */
		YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0;

	if ( b->yy_is_our_buffer )
		yyfree((void *) b->yy_ch_buf  );

	yyfree((void *) b  );
}

/* %if-c-only */

#ifndef __cplusplus
extern int isatty (int );
#endif /* __cplusplus */
    
/* %endif */

/* %if-c++-only */
/* %endif */

/* Initializes or reinitializes a buffer.
 * This function is sometimes called more than once on the same buffer,
 * such as during a yyrestart() or at EOF.
 */
/* %if-c-only */
    static void yy_init_buffer  (YY_BUFFER_STATE  b, FILE * file )
/* %endif */
/* %if-c++-only */
/* %endif */

{
	int oerrno = errno;
    
	yy_flush_buffer(b );

	b->yy_input_file = file;
	b->yy_fill_buffer = 1;

    /* If b is the current buffer, then yy_init_buffer was _probably_
     * called from yyrestart() or through yy_get_next_buffer.
     * In that case, we don't want to reset the lineno or column.
     */
    if (b != YY_CURRENT_BUFFER){
        b->yy_bs_lineno = 1;
        b->yy_bs_column = 0;
    }

/* %if-c-only */

        b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0;
    
/* %endif */
/* %if-c++-only */
/* %endif */
	errno = oerrno;
}

/** Discard all buffered characters. On the next scan, YY_INPUT will be called.
 * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER.
 * 
 */
/* %if-c-only */
    void yy_flush_buffer (YY_BUFFER_STATE  b )
/* %endif */
/* %if-c++-only */
/* %endif */
{
    	if ( ! b )
		return;

	b->yy_n_chars = 0;

	/* We always need two end-of-buffer characters.  The first causes
	 * a transition to the end-of-buffer state.  The second causes
	 * a jam in that state.
	 */
	b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR;
	b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR;

	b->yy_buf_pos = &b->yy_ch_buf[0];

	b->yy_at_bol = 1;
	b->yy_buffer_status = YY_BUFFER_NEW;

	if ( b == YY_CURRENT_BUFFER )
		yy_load_buffer_state( );
}

/* %if-c-or-c++ */
/** Pushes the new state onto the stack. The new state becomes
 *  the current state. This function will allocate the stack
 *  if necessary.
 *  @param new_buffer The new state.
 *  
 */
/* %if-c-only */
void yypush_buffer_state (YY_BUFFER_STATE new_buffer )
/* %endif */
/* %if-c++-only */
/* %endif */
{
    	if (new_buffer == NULL)
		return;

	yyensure_buffer_stack();

	/* This block is copied from yy_switch_to_buffer. */
	if ( YY_CURRENT_BUFFER )
		{
		/* Flush out information for old buffer. */
		*(yy_c_buf_p) = (yy_hold_char);
		YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p);
		YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars);
		}

	/* Only push if top exists. Otherwise, replace top. */
	if (YY_CURRENT_BUFFER)
		(yy_buffer_stack_top)++;
	YY_CURRENT_BUFFER_LVALUE = new_buffer;

	/* copied from yy_switch_to_buffer. */
	yy_load_buffer_state( );
	(yy_did_buffer_switch_on_eof) = 1;
}
/* %endif */

/* %if-c-or-c++ */
/** Removes and deletes the top of the stack, if present.
 *  The next element becomes the new top.
 *  
 */
/* %if-c-only */
void yypop_buffer_state (void)
/* %endif */
/* %if-c++-only */
/* %endif */
{
    	if (!YY_CURRENT_BUFFER)
		return;

	yy_delete_buffer(YY_CURRENT_BUFFER );
	YY_CURRENT_BUFFER_LVALUE = NULL;
	if ((yy_buffer_stack_top) > 0)
		--(yy_buffer_stack_top);

	if (YY_CURRENT_BUFFER) {
		yy_load_buffer_state( );
		(yy_did_buffer_switch_on_eof) = 1;
	}
}
/* %endif */

/* %if-c-or-c++ */
/* Allocates the stack if it does not exist.
 *  Guarantees space for at least one push.
 */
/* %if-c-only */
static void yyensure_buffer_stack (void)
/* %endif */
/* %if-c++-only */
/* %endif */
{
	int num_to_alloc;
    
	if (!(yy_buffer_stack)) {

		/* First allocation is just for 2 elements, since we don't know if this
		 * scanner will even need a stack. We use 2 instead of 1 to avoid an
		 * immediate realloc on the next call.
         */
		num_to_alloc = 1;
		(yy_buffer_stack) = (struct yy_buffer_state**)yyalloc
								(num_to_alloc * sizeof(struct yy_buffer_state*)
								);
		if ( ! (yy_buffer_stack) )
			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
								  
		memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*));
				
		(yy_buffer_stack_max) = num_to_alloc;
		(yy_buffer_stack_top) = 0;
		return;
	}

	if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){

		/* Increase the buffer to prepare for a possible push. */
		int grow_size = 8 /* arbitrary grow size */;

		num_to_alloc = (yy_buffer_stack_max) + grow_size;
		(yy_buffer_stack) = (struct yy_buffer_state**)yyrealloc
								((yy_buffer_stack),
								num_to_alloc * sizeof(struct yy_buffer_state*)
								);
		if ( ! (yy_buffer_stack) )
			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );

		/* zero only the new slots.*/
		memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*));
		(yy_buffer_stack_max) = num_to_alloc;
	}
}
/* %endif */

/* %if-c-only */
/** Setup the input buffer state to scan directly from a user-specified character buffer.
 * @param base the character buffer
 * @param size the size in bytes of the character buffer
 * 
 * @return the newly allocated buffer state object. 
 */
YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size )
{
	YY_BUFFER_STATE b;
    
	if ( size < 2 ||
	     base[size-2] != YY_END_OF_BUFFER_CHAR ||
	     base[size-1] != YY_END_OF_BUFFER_CHAR )
		/* They forgot to leave room for the EOB's. */
		return 0;

	b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state )  );
	if ( ! b )
		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" );

	b->yy_buf_size = size - 2;	/* "- 2" to take care of EOB's */
	b->yy_buf_pos = b->yy_ch_buf = base;
	b->yy_is_our_buffer = 0;
	b->yy_input_file = 0;
	b->yy_n_chars = b->yy_buf_size;
	b->yy_is_interactive = 0;
	b->yy_at_bol = 1;
	b->yy_fill_buffer = 0;
	b->yy_buffer_status = YY_BUFFER_NEW;

	yy_switch_to_buffer(b  );

	return b;
}
/* %endif */

/* %if-c-only */
/** Setup the input buffer state to scan a string. The next call to yylex() will
 * scan from a @e copy of @a str.
 * @param yystr a NUL-terminated string to scan
 * 
 * @return the newly allocated buffer state object.
 * @note If you want to scan bytes that may contain NUL values, then use
 *       yy_scan_bytes() instead.
 */
YY_BUFFER_STATE yy_scan_string (yyconst char * yystr )
{
    
	return yy_scan_bytes(yystr,strlen(yystr) );
}
/* %endif */

/* %if-c-only */
/** Setup the input buffer state to scan the given bytes. The next call to yylex() will
 * scan from a @e copy of @a bytes.
 * @param bytes the byte buffer to scan
 * @param len the number of bytes in the buffer pointed to by @a bytes.
 * 
 * @return the newly allocated buffer state object.
 */
YY_BUFFER_STATE yy_scan_bytes  (yyconst char * yybytes, int  _yybytes_len )
{
	YY_BUFFER_STATE b;
	char *buf;
	yy_size_t n;
	int i;
    
	/* Get memory for full buffer, including space for trailing EOB's. */
	n = _yybytes_len + 2;
	buf = (char *) yyalloc(n  );
	if ( ! buf )
		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );

	for ( i = 0; i < _yybytes_len; ++i )
		buf[i] = yybytes[i];

	buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR;

	b = yy_scan_buffer(buf,n );
	if ( ! b )
		YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" );

	/* It's okay to grow etc. this buffer, and we should throw it
	 * away when we're done.
	 */
	b->yy_is_our_buffer = 1;

	return b;
}
/* %endif */

#ifndef YY_EXIT_FAILURE
#define YY_EXIT_FAILURE 2
#endif

/* %if-c-only */
static void yy_fatal_error (yyconst char* msg )
{
    	(void) fprintf( stderr, "%s\n", msg );
	exit( YY_EXIT_FAILURE );
}
/* %endif */
/* %if-c++-only */
/* %endif */

/* Redefine yyless() so it works in section 3 code. */

#undef yyless
#define yyless(n) \
	do \
		{ \
		/* Undo effects of setting up yytext. */ \
        int yyless_macro_arg = (n); \
        YY_LESS_LINENO(yyless_macro_arg);\
		yytext[yyleng] = (yy_hold_char); \
		(yy_c_buf_p) = yytext + yyless_macro_arg; \
		(yy_hold_char) = *(yy_c_buf_p); \
		*(yy_c_buf_p) = '\0'; \
		yyleng = yyless_macro_arg; \
		} \
	while ( 0 )

/* Accessor  methods (get/set functions) to struct members. */

/* %if-c-only */
/* %if-reentrant */
/* %endif */

/** Get the current line number.
 * 
 */
int yyget_lineno  (void)
{
        
    return yylineno;
}

/** Get the input stream.
 * 
 */
FILE *yyget_in  (void)
{
        return yyin;
}

/** Get the output stream.
 * 
 */
FILE *yyget_out  (void)
{
        return yyout;
}

/** Get the length of the current token.
 * 
 */
int yyget_leng  (void)
{
        return yyleng;
}

/** Get the current token.
 * 
 */

char *yyget_text  (void)
{
        return yytext;
}

/* %if-reentrant */
/* %endif */

/** Set the current line number.
 * @param line_number
 * 
 */
void yyset_lineno (int  line_number )
{
    
    yylineno = line_number;
}

/** Set the input stream. This does not discard the current
 * input buffer.
 * @param in_str A readable stream.
 * 
 * @see yy_switch_to_buffer
 */
void yyset_in (FILE *  in_str )
{
        yyin = in_str ;
}

void yyset_out (FILE *  out_str )
{
        yyout = out_str ;
}

int yyget_debug  (void)
{
        return yy_flex_debug;
}

void yyset_debug (int  bdebug )
{
        yy_flex_debug = bdebug ;
}

/* %endif */

/* %if-reentrant */
/* %if-bison-bridge */
/* %endif */
/* %endif if-c-only */

/* %if-c-only */
static int yy_init_globals (void)
{
        /* Initialization is the same as for the non-reentrant scanner.
     * This function is called from yylex_destroy(), so don't allocate here.
     */

    (yy_buffer_stack) = 0;
    (yy_buffer_stack_top) = 0;
    (yy_buffer_stack_max) = 0;
    (yy_c_buf_p) = (char *) 0;
    (yy_init) = 0;
    (yy_start) = 0;

/* Defined in main.c */
#ifdef YY_STDINIT
    yyin = stdin;
    yyout = stdout;
#else
    yyin = (FILE *) 0;
    yyout = (FILE *) 0;
#endif

    /* For future reference: Set errno on error, since we are called by
     * yylex_init()
     */
    return 0;
}
/* %endif */

/* %if-c-only SNIP! this currently causes conflicts with the c++ scanner */
/* yylex_destroy is for both reentrant and non-reentrant scanners. */
int yylex_destroy  (void)
{
    
    /* Pop the buffer stack, destroying each element. */
	while(YY_CURRENT_BUFFER){
		yy_delete_buffer(YY_CURRENT_BUFFER  );
		YY_CURRENT_BUFFER_LVALUE = NULL;
		yypop_buffer_state();
	}

	/* Destroy the stack itself. */
	yyfree((yy_buffer_stack) );
	(yy_buffer_stack) = NULL;

    /* Reset the globals. This is important in a non-reentrant scanner so the next time
     * yylex() is called, initialization will occur. */
    yy_init_globals( );

/* %if-reentrant */
/* %endif */
    return 0;
}
/* %endif */

/*
 * Internal utility routines.
 */

#ifndef yytext_ptr
static void yy_flex_strncpy (char* s1, yyconst char * s2, int n )
{
	register int i;
	for ( i = 0; i < n; ++i )
		s1[i] = s2[i];
}
#endif

#ifdef YY_NEED_STRLEN
static int yy_flex_strlen (yyconst char * s )
{
	register int n;
	for ( n = 0; s[n]; ++n )
		;

	return n;
}
#endif

void *yyalloc (yy_size_t  size )
{
	return (void *) malloc( size );
}

void *yyrealloc  (void * ptr, yy_size_t  size )
{
	/* The cast to (char *) in the following accommodates both
	 * implementations that use char* generic pointers, and those
	 * that use void* generic pointers.  It works with the latter
	 * because both ANSI C and C++ allow castless assignment from
	 * any pointer type to void*, and deal with argument conversions
	 * as though doing an assignment.
	 */
	return (void *) realloc( (char *) ptr, size );
}

void yyfree (void * ptr )
{
	free( (char *) ptr );	/* see yyrealloc() for (char *) cast */
}

/* %if-tables-serialization definitions */
/* %define-yytables   The name for this specific scanner's tables. */
#define YYTABLES_NAME "yytables"
/* %endif */

/* %ok-for-header */

#line 472 "rhlex.l"


 
static void lex_error(const char *line, int line_num, const char *msg)
{
    (void) fprintf(stderr, "%s: %s: %d: %s\n",
		   Program_name, Input_name, line_num, msg);
    (void) fprintf(stderr, "Line: %s\nError: %*s\n",
		   line, Start_line_pos, "^");
    exit(1);
    return;
}
 
void rh_lex_finish(void)
{
    if (Line) {
	free(Line);
	Line = (char *) NULL;
	Line_len = Line_num = Line_pos = 0;
    }
    First_line = 1;
    yy_delete_buffer(YY_CURRENT_BUFFER);
    return;
}
 
static symbol *insertname(const char *s,
			  int	t,
			  long	val
			  )
{
    symbol *	sym;


    sym = ALLOCATE(sizeof(symbol));
    sym->name = SAVESTR(s);
    sym->type = t;
    sym->value = val;
    sym->req_stat = FALSE;
    sym->func = UNUSED_FUNC;
    sym->ref_count = 0;
    sym->num_params = 0;
    sym->tree = (node *) NULL;

    sym->next = Symbol_table;
    Symbol_table = sym;
    
    return (sym);
}
 
static symbol *locatename(const char *s)
{
    symbol *	p;


    for (p = Symbol_table; p != (symbol *) NULL; p = p->next) {
	if (strcmp(s, p->name) == 0) {
	    return (p);
	}
    }
    return ((symbol *) NULL);
}
 
static int lookup(const char *str)
{
    symbol *	sym;

#if defined(DEBUG2)
    (void) fprintf(stderr, "lookup(%s)\n", str);
#endif

    sym = locatename(str);
    if (sym == (symbol *) NULL) {
	sym = insertname(str, IDENTIFIER, 0L);
    }
    if (sym->type == NUMBER) {
	rh_lval.value = sym->value;
    }
    else {
	rh_lval.sym = sym;
    }
    return sym->type;
}
 
static void remove_escapes(char *str)
{
    char *from;
    char *to;
    
    for (to = from = str; *from; /*void*/) {
	if (*from == '\\') {
	    if (*++from) {
		*to++ = *from++;
	    }
	    else {
		*to++ = '\\';
	    }
	}
	else {
	    *to++ = *from++;
	}
    }

    *to = *from;

    return;
}
 
static int rh_read(char *buf,
		   int max_size
		   )
{
    int result;

    if (Input_file == -1) {
	result = (max_size < (Input_expr_end - Input_expr_ptr))
	    ? max_size : (Input_expr_end - Input_expr_ptr);
	if (result > 0) {
	    (void) memcpy(buf, Input_expr_ptr, result);
	    Input_expr_ptr += result;
	}
#if defined(DEBUG)
	else {
	    assert(result == 0);
	}
#endif
    }
    else {
	result = read(Input_file, buf, max_size);
	if (result < 0) {
	    error("read from '%s' failed : %m", Input_name);
	}
    }

    return result;
}
 
static date_info get_file_time(char *str)
{
    int		field;
    date_info	file_time;
    char *	from;
    int		stat_error;
    struct stat statb;
    char *	to;
    char *	type = (char *) NULL;

    for (to = from = str; *from != '\0'; /* void */) {
	if (*from == '\\') {
	    if (*++from) {
		*to++ = *from++;
	    }
	    else {
		*to++ = '\\';
	    }
	}
	else if (*from == '(') {
	    for (type = ++from; *from != '\0' && *from != ')'; ++from) {
		;	/* empty loop */
	    }
	    if (*from != ')'
		|| *(from + 1) != '\0') {
		lex_error(Line, Line_num, "invalid file time operator");
	    }
	    *from++ = '\0';		/* terminate type */
	    
	    if (*from != '\0') {	/* must be at end of str */
	    }
	    break;
	}
	else {
	    *to++ = *from++;
	}
    }

    if (type == (str + 1)) {
	lex_error(Line, Line_num, "no file specified with file time operator");
    }	

    *to = '\0';

    if (type == (char *) NULL) {
	field = RH_MTIME;
    }
    else {
	if (strcasecmp(type, "atime") == 0) {
	    field = RH_ATIME;
	}
	else if (strcasecmp(type, "ctime") == 0) {
	    field = RH_CTIME;
	}
	else if (strcasecmp(type, "mtime") == 0) {
	    field = RH_MTIME;
	}
	else {
	    lex_error(Line, Line_num, "invalid file time field");
	}
	*type = '\0';
    }

    if (stat(str, &statb) == -1) {
	stat_error = errno;
	(void) fprintf(stderr, "%s: %s: %d: file time operator file - '%s'\n",
		       Program_name, Input_name, Line_num,
		       strerror(stat_error));
	(void) fprintf(stderr, " Line: %s\nError: %*s\n",
		       Line, Start_line_pos + 1, "^");
	exit(1);
    }

    switch (field) {

    case RH_ATIME:
	file_time.seconds = statb.st_atime;
	break;

    case RH_CTIME:
	file_time.seconds = statb.st_ctime;
	break;

    case RH_MTIME:
	file_time.seconds = statb.st_mtime;
	break;
    }

    return file_time;
}
int yywrap(void)
{
    return 1;
}

Added rhlex.l.

























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/* -*-C-*- */

%{

/*
 * rhlex.l
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 */

#if !defined(lint)
static char rcsid[] = "$Id: rhlex.l,v 1.1 1994/01/12 19:39:35 rick Exp $";
#endif

#include <memory.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <errno.h>

#if defined(DEBUG)
#  include <assert.h>
#endif

#include "rh.h"
#include "rhdata.h"
#include "rhgram.h"
#include "alloc.h"
#include "errmsg.h"
#include "sysinfo.h"

/* #define YY_DECL int rh_lex YY_PROTO(( void )) */
#define YY_DECL int rh_lex(void)

#define CHUNK_SIZE	32

#undef YY_INPUT
#define YY_INPUT(buf, result, max_size) (result = rh_read(buf, max_size))

#define RH_ATIME	1
#define RH_CTIME	2
#define RH_MTIME	3

static char *	Start_line = (char *) NULL;
static int	Start_line_num;
static int	Start_line_pos;
static int	First_line = 1;
char *		Line = (char *) NULL;
int		Line_len = 0;
int		Line_num = 0;
int		Line_pos = 0;
static int	OldSC;

static date_info		get_file_time(char *str);
static void			lex_error(const char * line,
					  int line_num,
					  const char *msg);
static int			lookup(const char *str);
extern void			rh_error();
static int			rh_read(char *buf, int max_size);
static void			remove_escapes(char *str);
extern long			strtol();

%}

%x COMMENT
%x DATE_SPEC
%x FILE_NAME
%x FILE_TIME
%x FIRST_LINE
%x NEW_LINE

%%

    if (First_line) {
	BEGIN(FIRST_LINE);
	First_line = 0;
    }

<FIRST_LINE>.*		{
			    Line = ALLOCATE(yyleng + 1);
			    (void) strcpy(Line, yytext);
			    Line_len = yyleng;
			    Line_num = 1;
			    Line_pos = 0;
			    BEGIN(INITIAL);
			    yyless(0);
			}

<FIRST_LINE>\n		{
			    Line = ALLOCATE(81);
			    *Line = '\0';
			    Line_len = 80;
			    Line_num = 1;
			    Line_pos = 0;
			    BEGIN(INITIAL);
			    yyless(0);
			}

<FIRST_LINE><<EOF>>	{ yyterminate(); }

<NEW_LINE>.*		{
			    int adjustment = 0;
			    int len;
			    int lp;
			    char *from;
			    char *to;

			    for (from = yytext; *from != '\0'; from++) {
				if (*from == '\t') {
				    adjustment += 8;
				}
			    }
			    len = yyleng + adjustment;
			    if (len > Line_len) {
				Line = REALLOCATE(Line, len + 1);
				Line_len = len;
			    }
			    if (adjustment) {
				for (from = yytext, to = Line, lp = 0;
				     *from != '\0';
				     /* void */) {
				    if (*from == '\t') {
					adjustment = 8 - (lp + 1) % 8;
					lp += adjustment;
					while (adjustment) {
					    *to++ = ' ';
					    adjustment--;
					}
					from++;
				    }
				    else {
					*to++ = *from++;
					lp++;
				    }
				}
				*to = *from;
			    }
			    else {
				(void) strcpy(Line, yytext);
			    }
			    Line_num++;
			    Line_pos = 0;
			    BEGIN(OldSC);
			    yyless(0);
			}

<NEW_LINE>\n		{ *Line = '\0'; Line_num++; Line_pos = 0; }

<NEW_LINE><<EOF>>	{
			    if (OldSC == COMMENT) {
				lex_error(Start_line, Start_line_num,
					  "unterminated comment");
			    }
			    yyterminate();
			}

<COMMENT>[^*\n]*	{ Line_pos += yyleng; }

<COMMENT>[^*\n]*\n	{ OldSC = COMMENT; BEGIN(NEW_LINE); }

<COMMENT>"*"+[^*/\n]*	{ Line_pos += yyleng; }

<COMMENT>"*"+[^*/\n]*\n	{ OldSC = COMMENT; BEGIN(NEW_LINE); }

<COMMENT>"*"+"/"	{
			    Line_pos += yyleng;
			    free(Start_line);
			    Start_line = (char *) NULL;
			    BEGIN(INITIAL);
			}

<COMMENT><<EOF>>	{
			    lex_error(Start_line, Start_line_num,
				      "unterminated comment");
			}

<DATE_SPEC>[^\\\]\n]*	{ yymore(); }

<DATE_SPEC>\\[^\n]	{ yymore(); }

<DATE_SPEC>\\		{ yymore(); }

<DATE_SPEC>\]		{
			    char *date_str;

			    if (yyleng == 1) {
				lex_error(Line, Line_num,
					  "empty date specification");
			    }
			    Line_pos += yyleng;
			    date_str = SAVESTR(yytext);
			    date_str[yyleng - 1] = '\0';
			    remove_escapes(date_str);
			    rh_lval.di = ALLOCATE(sizeof(date_info));
			    *rh_lval.di = getdate(date_str,
						  (struct timeb *) NULL);
			    free(date_str);
			    if (rh_lval.di->seconds == -1) {
				rh_error("invalid date specification");
			    }
			    BEGIN(INITIAL);
			    return DATESPEC;
			}

<DATE_SPEC>\n		{
			    lex_error(Line, Line_num,
				      "unterminated date specification");
			}

<DATE_SPEC><<EOF>>	{
			    lex_error(Line, Line_num,
				      "unterminated date specification");
			}

<FILE_NAME>[^\\\"\n]*	{ yymore(); }

<FILE_NAME>\\[^\n]	{ yymore(); }

<FILE_NAME>\\		{ yymore(); }

<FILE_NAME>\"		{
			    if (yyleng == 1) {
				lex_error(Line, Line_num,
					  "empty file name pattern");
			    }
			    Line_pos += yyleng;
			    rh_lval.str = SAVESTR(yytext);
			    rh_lval.str[yyleng - 1] = '\0';
			    BEGIN(INITIAL);
			    remove_escapes(rh_lval.str);
			    return FILENAME_PATTERN;
			}

<FILE_NAME>\n		{
			    lex_error(Line, Line_num,
				      "unterminated file name pattern");
			}

<FILE_NAME><<EOF>>	{
			    lex_error(Line, Line_num,
				      "unterminated file name pattern");
			}

<FILE_TIME>[^\\#\n]*	{ yymore(); }

<FILE_TIME>\\[^\n]	{ yymore(); }

<FILE_TIME>\\		{ yymore(); }

<FILE_TIME>#		{
			    char *ftspec;

			    if (yyleng == 1) {
				lex_error(Line, Line_num,
					  "empty file time operator");
			    }
			    ftspec = SAVESTR(yytext);
			    ftspec[yyleng - 1] = '\0';
			    rh_lval.di = ALLOCATE(sizeof(date_info));
			    *rh_lval.di = get_file_time(ftspec);
			    Line_pos += yyleng;
			    free(ftspec);
			    BEGIN(INITIAL);
			    return FILETIME_FIELD;
			}

<FILE_TIME>\n		{
			    lex_error(Line, Line_num,
				      "unterminated file time operator");
			}

<FILE_TIME><<EOF>>	{
			    lex_error(Line, Line_num,
				      "unterminated file time operator");
			}

"!"			{ Line_pos += yyleng; return '!'; }
"!="			{ Line_pos += yyleng; return NE; }
"%"			{ Line_pos += yyleng; return '%'; }
"&"			{ Line_pos += yyleng; return '&'; }
"&&"			{ Line_pos += yyleng; return LOGAND; }
"("			{ Line_pos += yyleng; return '('; }
")"			{ Line_pos += yyleng; return ')'; }
"*"			{ Line_pos += yyleng; return '*'; }
"+"			{ Line_pos += yyleng; return '+'; }
","			{ Line_pos += yyleng; return ','; }
"-"			{ Line_pos += yyleng; return '-'; }
"/"			{ Line_pos += yyleng; return '/'; }
":"			{ Line_pos += yyleng; return ':'; }
";"			{ Line_pos += yyleng; return ';'; }
"<"			{ Line_pos += yyleng; return '<'; }
"<<"			{ Line_pos += yyleng; return LSHIFT; }
"<="			{ Line_pos += yyleng; return LE; }
"=="			{ Line_pos += yyleng; return EQ; }
">"			{ Line_pos += yyleng; return '>'; }
">="			{ Line_pos += yyleng; return GE; }
">>"			{ Line_pos += yyleng; return RSHIFT; }
"?"			{ Line_pos += yyleng; return '?'; }
"^"			{ Line_pos += yyleng; return '^'; }
"{"			{ Line_pos += yyleng; return '{'; }
"|"			{ Line_pos += yyleng; return '|'; }
"||"			{ Line_pos += yyleng; return LOGOR; }
"}"			{ Line_pos += yyleng; return '}'; }
"~"			{ Line_pos += yyleng; return '~'; }

"$$"			{
			    Line_pos += yyleng;
			    rh_lval.value = getuid();
			    return NUMBER;
			}

"$"[a-zA-Z0-9]+		{
			    user_info *uip;

			    Line_pos += yyleng;
			    if (yyleng == 1) {
				rh_error("user name missing");
			    }
			    uip = getuinam(yytext + 1);
			    if (uip == (user_info *) NULL) {
				rh_error("no such user");
			    }
			    rh_lval.value = uip->uid;
			    return NUMBER;
			}

"@@"			{
			    Line_pos += yyleng;
			    rh_lval.value = getgid();
			    return NUMBER;
			}

"@"[a-zA-Z0-9]*		{
			    group_info *gip;

			    Line_pos += yyleng;
			    if (yyleng == 1) {
				rh_error("group name missing");
			    }
			    gip = getginam(yytext + 1);
			    if (gip == (group_info *) NULL) {
				rh_error("no such group");
			    }
			    rh_lval.value = gip->gid;
			    return NUMBER;
			}

[1-9][0-9]*[KMGmhdw]?	{
			    char *end;

			    Line_pos += yyleng;
			    rh_lval.value = strtol(yytext, &end, 10);

			    switch (*end) {

			    case 'G':
				rh_lval.value *= 1024 * 1024 * 1024;
				break;

			    case 'K':
				rh_lval.value *= 1024;
				break;

			    case 'M':
				rh_lval.value *= 1024 * 1024;
				break;

			    case 'd':
				rh_lval.value *= 60 * 60 * 24;
				break;

			    case 'h':
				rh_lval.value *= 60 * 60;
				break;

			    case 'm':
				rh_lval.value *= 60;
				break;

			    case 'w':
				rh_lval.value *= 60 * 60 * 24 * 7;
				break;
			    }
			    return NUMBER;
			}

0[0-7]*			{
			    Line_pos += yyleng;
			    rh_lval.value = strtol(yytext, (char **) NULL, 0);
			    return NUMBER;
			}

0[xX][0-9a-fA-F]+	{
			    Line_pos += yyleng;
			    rh_lval.value = strtol(yytext, (char **) NULL, 0);
			    return NUMBER;
			}

[a-zA-Z_][a-zA-Z0-9_]*	{
			    Line_pos += yyleng;
			    return lookup(yytext);
			}

\"			{
			    Start_line_pos = Line_pos + 1;
			    Line_pos += yyleng;
			    BEGIN(FILE_NAME);
			}

\[[\t ]*		{
			    char *p;

			    Start_line_pos = Line_pos + 1;
			    for (p = yytext; *p != '\0'; p++) {
				if (*p == '\t') {
				    Line_pos += (8 - (Line_pos + 1) % 8);
				}
				else {
				    Line_pos++;
				}
			    }
			    BEGIN(DATE_SPEC);
			}

"/*"			{
			    Start_line_pos = Line_pos + 1;
			    Start_line_num = Line_num;
			    Start_line = SAVESTR(Line);
			    Line_pos += yyleng;
			    BEGIN(COMMENT);
			}

"//"[^\n]*		{ }	/* simple comment - ignore to end of line */

"`"[a-zA-Z0-9.]+	{
			    Line_pos += yyleng;
			    rh_lval.str = SAVESTR(yytext);
			    return FSTYPE;
			}

[\t\f ]+		{			/* ignore white space */
			    char *p;

			    for (p = yytext; *p != '\0'; p++) {
				if (*p == '\f') {
				    Line_num++;
				    Line_pos = 0;
				    *Line = '\0';
				}
				else if (*p == ' ') {
				    Line_pos++;
				}
				else {
				    Line_pos += (8 - (Line_pos + 1) % 8);
				}
			    }
			}

"#"			{
			    Start_line_pos = Line_pos + 1;
			    Line_pos += yyleng;
			    BEGIN(FILE_TIME);
			}

\n			{ OldSC = INITIAL; BEGIN(NEW_LINE); }

.			{ Line_pos += yyleng; return yytext[0]; }

<<EOF>>			{ yyterminate(); }

%%
 
static void lex_error(const char *line, int line_num, const char *msg)
{
    (void) fprintf(stderr, "%s: %s: %d: %s\n",
		   Program_name, Input_name, line_num, msg);
    (void) fprintf(stderr, "Line: %s\nError: %*s\n",
		   line, Start_line_pos, "^");
    exit(1);
    return;
}
 
void rh_lex_finish(void)
{
    if (Line) {
	free(Line);
	Line = (char *) NULL;
	Line_len = Line_num = Line_pos = 0;
    }
    First_line = 1;
    yy_delete_buffer(YY_CURRENT_BUFFER);
    return;
}
 
static symbol *insertname(const char *s,
			  int	t,
			  long	val
			  )
{
    symbol *	sym;


    sym = ALLOCATE(sizeof(symbol));
    sym->name = SAVESTR(s);
    sym->type = t;
    sym->value = val;
    sym->req_stat = FALSE;
    sym->func = UNUSED_FUNC;
    sym->ref_count = 0;
    sym->num_params = 0;
    sym->tree = (node *) NULL;

    sym->next = Symbol_table;
    Symbol_table = sym;
    
    return (sym);
}
 
static symbol *locatename(const char *s)
{
    symbol *	p;


    for (p = Symbol_table; p != (symbol *) NULL; p = p->next) {
	if (strcmp(s, p->name) == 0) {
	    return (p);
	}
    }
    return ((symbol *) NULL);
}
 
static int lookup(const char *str)
{
    symbol *	sym;

#if defined(DEBUG2)
    (void) fprintf(stderr, "lookup(%s)\n", str);
#endif

    sym = locatename(str);
    if (sym == (symbol *) NULL) {
	sym = insertname(str, IDENTIFIER, 0L);
    }
    if (sym->type == NUMBER) {
	rh_lval.value = sym->value;
    }
    else {
	rh_lval.sym = sym;
    }
    return sym->type;
}
 
static void remove_escapes(char *str)
{
    char *from;
    char *to;
    
    for (to = from = str; *from; /*void*/) {
	if (*from == '\\') {
	    if (*++from) {
		*to++ = *from++;
	    }
	    else {
		*to++ = '\\';
	    }
	}
	else {
	    *to++ = *from++;
	}
    }

    *to = *from;

    return;
}
 
static int rh_read(char *buf,
		   int max_size
		   )
{
    int result;

    if (Input_file == -1) {
	result = (max_size < (Input_expr_end - Input_expr_ptr))
	    ? max_size : (Input_expr_end - Input_expr_ptr);
	if (result > 0) {
	    (void) memcpy(buf, Input_expr_ptr, result);
	    Input_expr_ptr += result;
	}
#if defined(DEBUG)
	else {
	    assert(result == 0);
	}
#endif
    }
    else {
	result = read(Input_file, buf, max_size);
	if (result < 0) {
	    error("read from '%s' failed : %m", Input_name);
	}
    }

    return result;
}
 
static date_info get_file_time(char *str)
{
    int		field;
    date_info	file_time;
    char *	from;
    int		stat_error;
    struct stat statb;
    char *	to;
    char *	type = (char *) NULL;

    for (to = from = str; *from != '\0'; /* void */) {
	if (*from == '\\') {
	    if (*++from) {
		*to++ = *from++;
	    }
	    else {
		*to++ = '\\';
	    }
	}
	else if (*from == '(') {
	    for (type = ++from; *from != '\0' && *from != ')'; ++from) {
		;	/* empty loop */
	    }
	    if (*from != ')'
		|| *(from + 1) != '\0') {
		lex_error(Line, Line_num, "invalid file time operator");
	    }
	    *from++ = '\0';		/* terminate type */
	    
	    if (*from != '\0') {	/* must be at end of str */
	    }
	    break;
	}
	else {
	    *to++ = *from++;
	}
    }

    if (type == (str + 1)) {
	lex_error(Line, Line_num, "no file specified with file time operator");
    }	

    *to = '\0';

    if (type == (char *) NULL) {
	field = RH_MTIME;
    }
    else {
	if (strcasecmp(type, "atime") == 0) {
	    field = RH_ATIME;
	}
	else if (strcasecmp(type, "ctime") == 0) {
	    field = RH_CTIME;
	}
	else if (strcasecmp(type, "mtime") == 0) {
	    field = RH_MTIME;
	}
	else {
	    lex_error(Line, Line_num, "invalid file time field");
	}
	*type = '\0';
    }

    if (stat(str, &statb) == -1) {
	stat_error = errno;
	(void) fprintf(stderr, "%s: %s: %d: file time operator file - '%s'\n",
		       Program_name, Input_name, Line_num,
		       strerror(stat_error));
	(void) fprintf(stderr, " Line: %s\nError: %*s\n",
		       Line, Start_line_pos + 1, "^");
	exit(1);
    }

    switch (field) {

    case RH_ATIME:
	file_time.seconds = statb.st_atime;
	break;

    case RH_CTIME:
	file_time.seconds = statb.st_ctime;
	break;

    case RH_MTIME:
	file_time.seconds = statb.st_mtime;
	break;
    }

    return file_time;
}
int yywrap(void)
{
    return 1;
}

Added rhrc.1.











































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// rcsid: $Id: rhrc.1,v 1.1 1994/01/12 19:39:50 rick Exp $

dir() {
	return( (mode & IFMT) == IFDIR );
}

plain()
{
	return( (mode & IFMT) == IFREG );
}

link()
{
	return( (mode & IFMT) == IFLNK );
}

socket()
{
	return( (mode & IFMT) == IFSOCK );
}

fifo()
{
	return( (mode & IFMT) == IFIFO );
}

months
{
	return days*30;
}

nlinks { return nlink; }

ago(d)
{
	return( NOW - d );
}

writable()
{
	return mode & 022;
}

readable()
{
	return mode & 004;
}

executable()
{
	return ((mode & IFMT) == IFREG) && (mode & (IXUSR | IXGRP | IXOTH));
}

MINE
{
	return( uid == $$ );
}

bad()
{
	return( "core" || ("a.out" && mtime <= ago(2*days) ) ||
		"*.BAK" || "*.CKP" || writable);
}

csrc()
{
	return("*.c" || "*.h" || "[Mm]akefile" );
}

changed()
{
	return( mtime > NOW-1*hours );

	/* ALTERNATELY:
	 * (using the ago() function)
	 *
	 * 	return( mtime > ago(1*hours) );
	 *
	 */
}

KK()
{
	return 1024;
}

Added rhrc.2.













































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// rcsid: $Id: rhrc.2,v 1.1 1994/01/12 19:39:55 rick Exp $

megs { return KK<<10; }

recursion(n)
{
	return (n>0) ? recursion(n-1) : 0;
}

sqrt1(n,odd) { return( (n <= 0 ) ? 0 : sqrt1(n-odd,odd+2)+1 ); }
sqrt(n) { return( sqrt1(n,1) ); }

fact(n) { return (n<=0) ? 1 : fact(n-1)*n; }

new()
{
	return( mtime > NOW-1h || ctime > NOW-1h );
}

setuid()
{
	return( mode & ISUID );
}

setgid()
{
	return( mode & ISGID );
}

setid()
{
	return( mode & ( ISUID | ISGID) );
}

dump()
{
	return ( "xdump" );
}

Added strtol.c.











































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* 
 * strtol.c --
 *
 *	Source code for the "strtol" library procedure.
 *
 * Copyright (c) 1988 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

#if !defined(lint)
static char rcsid[] = "$Header: /home/rick/work/rh/rh-3.0/t/RCS/strtol.c,v 1.1 1994/01/12 19:40:06 rick Exp $ SPRITE (Berkeley)";
#endif /* not lint */

#include <ctype.h>

 
/*
 *----------------------------------------------------------------------
 *
 * strtol --
 *
 *	Convert an ASCII string into an integer.
 *
 * Results:
 *	The return value is the integer equivalent of string.  If endPtr
 *	is non-NULL, then *endPtr is filled in with the character
 *	after the last one that was part of the integer.  If string
 *	doesn't contain a valid integer value, then zero is returned
 *	and *endPtr is set to string.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

long int
strtol(string, endPtr, base)
    char *string;		/* String of ASCII digits, possibly
				 * preceded by white space.  For bases
				 * greater than 10, either lower- or
				 * upper-case digits may be used.
				 */
    char **endPtr;		/* Where to store address of terminating
				 * character, or NULL. */
    int base;			/* Base for conversion.  Must be less
				 * than 37.  If 0, then the base is chosen
				 * from the leading characters of string:
				 * "0x" means hex, "0" means octal, anything
				 * else means decimal.
				 */
{
    register char *p;
    int result;
    extern unsigned long strtoul(char *, char **, int);


    /*
     * Skip any leading blanks.
     */

    p = string;
    while (isspace(*p)) {
	p += 1;
    }

    /*
     * Check for a sign.
     */

    if (*p == '-') {
	p += 1;
	result = -(strtoul(p, endPtr, base));
    } else {
	if (*p == '+') {
	    p += 1;
	}
	result = strtoul(p, endPtr, base);
    }
    if ((result == 0) && (endPtr != 0) && (*endPtr == p)) {
	*endPtr = string;
    }
    return result;
}

Added strtoul.c.















































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* 
 * strtoul.c --
 *
 *	Source code for the "strtoul" library procedure.
 *
 * Copyright (c) 1988 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

#if !defined(lint)
static char rcsid[] = "$Header: /home/rick/work/rh/rh-3.0/t/RCS/strtoul.c,v 1.1 1994/01/12 19:40:20 rick Exp $ SPRITE (Berkeley)";
#endif /* not lint */

#include <ctype.h>

/*
 * The table below is used to convert from ASCII digits to a
 * numerical equivalent.  It maps from '0' through 'z' to integers
 * (100 for non-digit characters).
 */

static char cvtIn[] = {
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,		/* '0' - '9' */
    100, 100, 100, 100, 100, 100, 100,		/* punctuation */
    10, 11, 12, 13, 14, 15, 16, 17, 18, 19,	/* 'A' - 'Z' */
    20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
    30, 31, 32, 33, 34, 35,
    100, 100, 100, 100, 100, 100,		/* punctuation */
    10, 11, 12, 13, 14, 15, 16, 17, 18, 19,	/* 'a' - 'z' */
    20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
    30, 31, 32, 33, 34, 35};
 
/*
 *----------------------------------------------------------------------
 *
 * strtoul --
 *
 *	Convert an ASCII string into an integer.
 *
 * Results:
 *	The return value is the integer equivalent of string.  If endPtr
 *	is non-NULL, then *endPtr is filled in with the character
 *	after the last one that was part of the integer.  If string
 *	doesn't contain a valid integer value, then zero is returned
 *	and *endPtr is set to string.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

unsigned long int
strtoul(string, endPtr, base)
    char *string;		/* String of ASCII digits, possibly
				 * preceded by white space.  For bases
				 * greater than 10, either lower- or
				 * upper-case digits may be used.
				 */
    char **endPtr;		/* Where to store address of terminating
				 * character, or NULL. */
    int base;			/* Base for conversion.  Must be less
				 * than 37.  If 0, then the base is chosen
				 * from the leading characters of string:
				 * "0x" means hex, "0" means octal, anything
				 * else means decimal.
				 */
{
    register char *p;
    register unsigned long int result = 0;
    register unsigned digit;
    int anyDigits = 0;

    /*
     * Skip any leading blanks.
     */

    p = string;
    while (isspace(*p)) {
	p += 1;
    }

    /*
     * If no base was provided, pick one from the leading characters
     * of the string.
     */
    
    if (base == 0)
    {
	if (*p == '0') {
	    p += 1;
	    if (*p == 'x') {
		p += 1;
		base = 16;
	    } else {

		/*
		 * Must set anyDigits here, otherwise "0" produces a
		 * "no digits" error.
		 */

		anyDigits = 1;
		base = 8;
	    }
	}
	else base = 10;
    } else if (base == 16) {

	/*
	 * Skip a leading "0x" from hex numbers.
	 */

	if ((p[0] == '0') && (p[1] == 'x')) {
	    p += 2;
	}
    }

    /*
     * Sorry this code is so messy, but speed seems important.  Do
     * different things for base 8, 10, 16, and other.
     */

    if (base == 8) {
	for ( ; ; p += 1) {
	    digit = *p - '0';
	    if (digit > 7) {
		break;
	    }
	    result = (result << 3) + digit;
	    anyDigits = 1;
	}
    } else if (base == 10) {
	for ( ; ; p += 1) {
	    digit = *p - '0';
	    if (digit > 9) {
		break;
	    }
	    result = (10*result) + digit;
	    anyDigits = 1;
	}
    } else if (base == 16) {
	for ( ; ; p += 1) {
	    digit = *p - '0';
	    if (digit > ('z' - '0')) {
		break;
	    }
	    digit = cvtIn[digit];
	    if (digit > 15) {
		break;
	    }
	    result = (result << 4) + digit;
	    anyDigits = 1;
	}
    } else {
	for ( ; ; p += 1) {
	    digit = *p - '0';
	    if (digit > ('z' - '0')) {
		break;
	    }
	    digit = cvtIn[digit];
	    if (digit >= base) {
		break;
	    }
	    result = result*base + digit;
	    anyDigits = 1;
	}
    }

    /*
     * See if there were any digits at all.
     */

    if (!anyDigits) {
	p = string;
    }

    if (endPtr != 0) {
	*endPtr = p;
    }

    return result;
}

Added sysinfo.c.

























































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * sysinfo.c
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 * 
 * This file contains functions for getting information about
 * users, groups, and file systems. Results are cached to speed
 * subsequent queries.
 *
 * Each cache is a simple linked list with new entries added
 * to the beginning of the list.
 *
 * Global Functions: getfidev, getgigid, getginam, getuinam,
 *		     getuiuid, mounted_fstype
 *
 * Static Functions: init_file_systems,
 */

#if !defined(lint)
static char rcsid[] = "$Id: sysinfo.c,v 1.1 1994/01/12 19:40:33 rick Exp $";
#endif

#include <sys/types.h>
#include <string.h>
#include <grp.h>
#include <pwd.h>
#if defined(sun)
#include <mntent.h>
#endif

#include "alloc.h"
#include "sysinfo.h"

static group_info *	groups = (group_info *) NULL;
static user_info *	users = (user_info *) NULL;
 
#if defined(sun)

extern long strtol();

static fs_info *file_systems = (fs_info *) NULL;

/*
 * init_file_systems - initialize file systems cache with
 * information on all mounted file systems.
 */

static void init_file_systems(void)
{
    FILE *		mtab;
    char *		c_p;
    char *		dev_str;
    dev_t		device;
    fs_info *		p;
    fs_info *		prev;
    struct mntent *	info;
    
    if ((mtab = setmntent(MOUNTED, "r")) == (FILE *) NULL) {
	return;
    }
    while ((info = getmntent(mtab)) != (struct mntent *) NULL) {
	if ((dev_str = hasmntopt(info, "dev=")) == (char *) NULL) {
	    continue;
	}
	if (*dev_str == '0'
	    && (*(dev_str + 1) == 'x' || *(dev_str + 1) == 'X')) {
	    dev_str += 6;
	}
	else {
	    dev_str += 4;
	}
	device = (dev_t) strtol(dev_str, &c_p, 16);
	if (device == 0 || (c_p - dev_str) != 4) {
	    continue;
	}
	p = ALLOCATE(sizeof(fs_info));
	p->fs_type = SAVESTR(info->mnt_type);
	p->fs_dir = SAVESTR(info->mnt_dir);
	p->fs_dev = device;
	p->next = (fs_info *) NULL;
	if (file_systems == (fs_info *) NULL) {
	    file_systems = p;
	}
	else {
	    prev->next = p;
	}
	prev = p;
    }	
    (void) endmntent(mtab);
    return;
}

int mounted_fstype(type)
char *	type;
{
    fs_info *	p;
    
    if (file_systems == (fs_info *) NULL) {
	init_file_systems();
    }
    
    for (p = file_systems; p != (fs_info *) NULL; p = p->next) {
	if (strcmp(type, p->fs_type) == 0) {
	    return 1;
	}
    }
    
    return 0;
}

fs_info *getfidev(fs_dev)
dev_t	fs_dev;
{
    fs_info *	p;
    
    if (file_systems == (fs_info *) NULL) {
	init_file_systems();
    }
    
    for (p = file_systems; p != (fs_info *) NULL; p = p->next) {
	if (fs_dev == p->fs_dev) {
	    return p;
	}
    }
    
    return (fs_info *) NULL;
}
#endif
 
/*
 * getginam - get group information by group name
 */

group_info *getginam(char *group_name)
{
    struct group *	info;
    group_info *	p = groups;
    
    /* first check the group cache for the group name */
    
    while (p != (group_info *) NULL) {
	if (strcmp(group_name, p->name) == 0) {
	    return p;
	}
	p = p->next;
    }
    
    /*
     * The desired group name isn't in the group cache so, get group
     * information from the system and add it to the cache if
     * successful.
     */
    
    if ((info = getgrnam(group_name)) == (struct group *) NULL) {
	return (group_info *) NULL;
    }
    p = ALLOCATE(sizeof(group_info));
    p->name = SAVESTR(info->gr_name);
    p->gid = info->gr_gid;
    p->next = groups;
    groups = p;
    return p;
}
 
/*
 * getgigid - get group information by group id
 */

group_info *getgigid(gid_t gid)
{
    struct group *	info;
    group_info *		p = groups;
    
    /* first check the group cache for the group id */
    
    while (p != (group_info *) NULL) {
	if (gid == p->gid) {
	    return p;
	}
	p = p->next;
    }
    
    /*
     * The desired group id isn't in the group cache so, get group
     * information from the system and add it to the cache if
     * successful.
     */
    
    if ((info = getgrgid(gid)) == (struct group *) NULL) {
	return (group_info *) NULL;
    }
    p = ALLOCATE(sizeof(group_info));
    p->name = SAVESTR(info->gr_name);
    p->gid = info->gr_gid;
    p->next = groups;
    groups = p;
    return p;
}
 
/*
 * getuinam - get user information by user name
 */

user_info *getuinam(char *user_name)
{
    struct passwd *	info;
    user_info *		p = users;
    
    /* first check the user cache for the user name */
    
    while (p != (user_info *) NULL) {
	if (strcmp(user_name, p->name) == 0) {
	    return p;
	}
	p = p->next;
    }
    
    /*
     * The desired user name isn't in the user cache so, get user
     * information from the system and add it to the cache if
     * successful.
     */
    
    if ((info = getpwnam(user_name)) == (struct passwd *) NULL) {
	return (user_info *) NULL;
    }
    p = ALLOCATE(sizeof(user_info));
    p->name = SAVESTR(info->pw_name);
    p->uid = info->pw_uid;
    p->next = users;
    users = p;
    return p;
}
 
/*
 * getuiuid - get user information by user id
 */

user_info *getuiuid(uid_t uid)
{
    struct passwd *	info;
    user_info *		p = users;
    
    /* first check the user cache for the user id */
    
    while (p != (user_info *) NULL) {
	if (uid == p->uid) {
	    return p;
	}
	p = p->next;
    }
    
    /*
     * The desired user id isn't in the user cache so, get user
     * information from the system and add it to the cache if
     * successful.
     */
    
    if ((info = getpwuid(uid)) == (struct passwd *) NULL) {
	return (user_info *) NULL;
    }
    p = ALLOCATE(sizeof(user_info));
    p->name = SAVESTR(info->pw_name);
    p->uid = info->pw_uid;
    p->next = users;
    users = p;
    return p;
}

Added sysinfo.h.





































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * sysinfo.h
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 *
 * rcsid: $Id: sysinfo.h,v 1.1 1994/01/12 19:40:52 rick Exp $
 */

#if !defined(__SYSINFO_H__)
#define __SYSINFO_H__

typedef struct _group_info group_info;

struct _group_info {
   char *	name;
   gid_t	gid;
   group_info *	next;
};


typedef struct _user_info user_info;

struct _user_info {
   char *	name;
   uid_t	uid;
   user_info *	next;
};

extern group_info *	getgigid(gid_t gid);
extern group_info *	getginam(char *group_name);
extern user_info *	getuinam(char *user_name);
extern user_info *	getuiuid(uid_t uid);

#if defined(sun)

typedef struct _fs_info fs_info;

struct _fs_info {
    char *	fs_type;
    dev_t	fs_dev;
    char *	fs_dir;
    fs_info *	next;
};

extern fs_info *	getfidev(dev_t fs_dev);
extern int		mounted_fstype(char *type);

#endif	/* sun */

#endif	/* __SYSINFO_H__ */

Added tests.dat.



































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// rcsid: $Id: tests.dat,v 1.1 1994/01/12 19:41:22 rick Exp $

a(b,c) { return 6; }

d() { return a(1 + 2, 3 * 4); }

e() { return "xyz" }

f() { return e; }

g { return e ? 10 : 20 }

h() { return g }

i(j) { return j * h; }

l(m) { return m * d; }

n(o, p) { return p + o }

q { return n(5, 7) }

r(s) { s }

t{ r(33) }	// generates c_param 0 instead of c_number 33

u(v, w) { w }

x() { return u(66, 77); }

y() { x }

// this is a test

Added wildmat.c.

















































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
** wildmat.c
**  $Revision: 1.1 $
**
**  Do shell-style pattern matching for ?, \, [], and * characters.
**  Might not be robust in face of malformed patterns; e.g., "foo[a-"
**  could cause a segmentation violation.  It is 8bit clean.
**
**  Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
**  Rich $alz is now <rsalz@osf.org>.
**  April, 1991:  Replaced mutually-recursive calls with in-line code
**  for the star character.
**
**  Special thanks to Lars Mathiesen <thorinn@diku.dk> for the ABORT code.
**  This can greatly speed up failing wildcard patterns.  For example:
**	pattern: -*-*-*-*-*-*-12-*-*-*-m-*-*-*
**	text 1:	 -adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1
**	text 2:	 -adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1
**  Text 1 matches with 51 calls, while text 2 fails with 54 calls.  Without
**  the ABORT code, it takes 22310 calls to fail.  Ugh.  The following
**  explanation is from Lars:
**  The precondition that must be fulfilled is that DoMatch will consume
**  at least one character in text.  This is true if *p is neither '*' nor
**  '\0'.)  The last return has ABORT instead of FALSE to avoid quadratic
**  behaviour in cases like pattern "*a*b*c*d" with text "abcxxxxx".  With
**  FALSE, each star-loop has to run to the end of the text; with ABORT
**  only the last one does.
**
**  Once the control of one instance of DoMatch enters the star-loop, that
**  instance will return either TRUE or ABORT, and any calling instance
**  will therefore return immediately after (without calling recursively
**  again).  In effect, only one star-loop is ever active.  It would be
**  possible to modify the code to maintain this context explicitly,
**  eliminating all recursive calls at the cost of some complication and
**  loss of clarity (and the ABORT stuff seems to be unclear enough by
**  itself).  I think it would be unwise to try to get this into a
**  released version unless you have a good test data base to try it out
**  on.
*/

#if !defined(lint)
static char rcsid[] = "$Id: wildmat.c,v 1.1 1994/01/12 19:41:27 rick Exp $";
#endif

#define TRUE			1
#define FALSE			0
#define ABORT			-1


    /* What character marks an inverted character class? */
#define NEGATE_CLASS		'^'
    /* Is "*" a common pattern? */
#define OPTIMIZE_JUST_STAR
    /* Do tar(1) matching rules, which ignore a trailing slash? */
#undef MATCH_TAR_PATTERN


static unsigned char charmap[] = {
    '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
    '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
    '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
    '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
    '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
    '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
    '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
    '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
    '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
    '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
    '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
    '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
    '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
    '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
    '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
    '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
    '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
    '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
    '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
    '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
    '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
    '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
    '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
    '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
    '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
    '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
    '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
    '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
    '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
    '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
    '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
    '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
};


/*
**  Match text and p, return TRUE, FALSE, or ABORT.
*/
static int
DoMatch(register unsigned char	*text,
	register unsigned char	*pattern,
	int			 igncase)
{
    register int		 last;
    register int 	 	 matched;
    register int 	 	 reverse;
    register unsigned char	*tp;
    register unsigned char	*class_end;
    register unsigned char	*cmap = charmap;
    
    for ( ; *pattern != '\0'; text++, pattern++) {
	
	if (*text == '\0') {
	    return (*pattern == '*' && *++pattern == '\0') ? TRUE : ABORT;
	}

	switch (*pattern) {
	    
	case '\\':
	    /* Literal match with following character. */
	    if (*(pattern + 1)) {
		pattern++;
	    }
	    /* FALLTHROUGH */
	default:
	    if (igncase) {
		if (cmap[*text] != cmap[*pattern]) {
		    return FALSE;
		}
	    }
	    else if (*text != *pattern) {
		return FALSE;
	    }
	    continue;
	    
	case '?':
	    /* Match anything. */
	    continue;
	    
	case '*':
	    while (*++pattern == '*') {
		/* Consecutive stars act just like one. */
		continue;
	    }
	    if (*pattern == '\0') {
		/* Trailing star matches everything. */
		return TRUE;
	    }
	    while (*text) {
		if ((matched = DoMatch(text++, pattern, igncase)) != FALSE) {
		    return matched;
		}
	    }
	    return ABORT;
	    
	case '[':
	    for (tp = ++pattern; *tp != '\0' && *tp != ']'; tp++) {
		if (*tp == '\\') {
		    if (*++tp == '\0') {
			break;
		    }
		}
	    }
	    if (*tp == '\0' || tp == pattern) {
		/* Invalid character class so just do match against '[' */
		if (*text != '[') {
		    return FALSE;
		}
		pattern--;
		continue;
	    }
	    class_end = tp;
	    
	    /* inverse character class. */
	    if (*pattern == NEGATE_CLASS && *(pattern + 1) != ']') {
		reverse = TRUE;
		pattern++;
	    }
	    else {
		reverse = FALSE;
	    }
	    
	    for (tp = pattern, last = *pattern, matched = FALSE;
		 *pattern != '\0' && *pattern != ']';
		 last = *pattern, pattern++) {
		if (*pattern == '-'
		    && pattern != tp
		    && *(pattern + 1) != ']') {
		    if (*++pattern == '\\') {
			pattern++;
		    }
		    if (igncase) {
			if (cmap[*text] <= cmap[*pattern]
			    && cmap[*text] >= cmap[last]) {
			    pattern = class_end;
			    matched = TRUE;
			    break;
			}
		    }
		    else if (*text <= *pattern && *text >= last) {
			pattern = class_end;
			matched = TRUE;
			break;
		    }
		}
		else {
		    if (*pattern == '\\') {
			pattern++;
		    }
		    if (igncase) {
			if (cmap[*text] == cmap[*pattern]) {
			    pattern = class_end;
			    matched = TRUE;
			    break;
			}
		    }
		    else if (*text == *pattern) {
			pattern = class_end;
			matched = TRUE;
			break;
		    }
		}
	    }
	    
	    if (matched == reverse) {
		return FALSE;
	    }
	    continue;
	}
    }
    
#if defined(MATCH_TAR_PATTERN)
    if (*text == '/') {
	return TRUE;
    }
#endif	/* MATCH_TAR_PATTERN */
    return *text == '\0';
}


/*
**  User-level routine.  Returns TRUE or FALSE.
*/
int
wildmat(unsigned char	*text,
	unsigned char	*pattern,
	int		 dotspec,
	int		 igncase)
{
#if defined(OPTIMIZE_JUST_STAR)
    if (pattern[0] == '*' && pattern[1] == '\0') {
	return (dotspec && *text == '.') ? FALSE : TRUE;
    }
#endif	/* OPTIMIZE_JUST_STAR */
    if (dotspec && *text == '.' && *pattern != '.') {
	return FALSE;
    }
    else {
	return DoMatch(text, pattern, igncase) == TRUE;
    }
}

 

#if defined(TEST)

#include <stdio.h>

int
main(int	 argc,
     char	*argv[])
{
    int		 dotspec = 0;
    int		 igncase = 0;
    char	 p[80];
    char	 text[80];

    (void) printf("Wildmat tester.\n"
		  "Enter pattern, then strings to test.\n"
		  "A blank line gets prompts for a new pattern\n"
		  "A blank pattern exits the program.\n"
		  );

    for ( ; ; ) {
	(void) printf("\nEnter pattern:  ");
	(void) fflush(stdout);
	if (fgets(p, sizeof(p), stdin) == NULL || p[0] == '\n') {
	    break;
	}
	if (p[strlen(p) - 1] == '\n') {
	    p[strlen(p) - 1] == '\0';
	}
	for ( ; ; ) {
	    (void) printf("Enter text:  ");
	    (void) fflush(stdout);
	    if (fgets(text, sizeof(text), stdin) == NULL) {
		exit(0);
	    }
	    if (text[0] == '\n') {
		/* Blank line; go back and get a new pattern. */
		break;
	    }
	    if (text[strlen(text) - 1] == '\n') {
		text[strlen(text) - 1] == '\0';
	    }
	    (void) printf("      %s\n",
			  wildmat(text, p, dotspec, igncase) ? "YES" : "NO");
	}
    }

    return 0;
}

#endif	/* defined(TEST) */