rh

Check-in [ffcc6936f9]
Login

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

Overview
Comment:Mostly we don't change the syntax, so keep yacc/lex out of the picture by default.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:ffcc6936f9137face48c878a38eeb7adf87250c755dd9606e44c15294183e226
User & Date: ajv-899-334-8894@vsta.org 2016-08-10 21:03:30
Context
2016-08-10
21:04
Tidy up git output check-in: 32a60f8413 user: ajv-899-334-8894@vsta.org tags: master, trunk
21:03
Mostly we don't change the syntax, so keep yacc/lex out of the picture by default. check-in: ffcc6936f9 user: ajv-899-334-8894@vsta.org tags: master, trunk
21:01
Merge branch 'master' of https://github.com/vandys/rh check-in: f7d115da53 user: ajv-899-334-8894@vsta.org tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Deleted 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
%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.3 2014/07/31 21:58:09 vandys 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();

%}

%%
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 t;
    struct tm tm;

    /* Negative year?  WTF? */
    if (yy < 0) {
	yy = -yy;
    }

    /* Initialize exploded time, then fill in */
    time(&t);
    tm = *localtime(&t);
    tm.tm_year = yy;
    tm.tm_mon = mm-1;
    tm.tm_mday = dd;
    tm.tm_hour = h;
    tm.tm_min = m;
    tm.tm_sec = s;

    /* Convert */
    t = mktime(&tm);

    return (t);
}

static time_t
dayconv(ord, day, now)
int ord, day; time_t now;
{
	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)
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;
	char c;
	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];
	char *j, *k;
	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 hold/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
%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.3 2014/07/31 21:58:09 vandys 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();

%}

%%
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 t;
    struct tm tm;

    /* Negative year?  WTF? */
    if (yy < 0) {
	yy = -yy;
    }

    /* Initialize exploded time, then fill in */
    time(&t);
    tm = *localtime(&t);
    tm.tm_year = yy;
    tm.tm_mon = mm-1;
    tm.tm_mday = dd;
    tm.tm_hour = h;
    tm.tm_min = m;
    tm.tm_sec = s;

    /* Convert */
    t = mktime(&tm);

    return (t);
}

static time_t
dayconv(ord, day, now)
int ord, day; time_t now;
{
	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)
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;
	char c;
	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];
	char *j, *k;
	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 hold/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.1 2008/12/27 00:56:03 vandys 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 hold/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
/* -*-C-*- */

%{

/*
 * rhlex.l
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 */
#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;
}

Deleted 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.1 2008/12/27 00:56:03 vandys 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;
}
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<






























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Deleted 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
/* -*-C-*- */

%{

/*
 * rhlex.l
 *
 * Written by:	Rick Ohnemus	(rick@sterling.com)
 */
#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;
}
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<