File: boolexp.cpp

package info (click to toggle)
tinymux 2.10.1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,212 kB
  • ctags: 8,535
  • sloc: cpp: 111,587; sh: 5,867; ansic: 141; makefile: 139
file content (719 lines) | stat: -rw-r--r-- 16,987 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
/*! \file boolexp.cpp
 * \brief Boolean Expressions for \@locks
 *
 * $Id: boolexp.cpp 4107 2009-01-18 17:24:43Z brazilofmux $
 *
 * The functions here evaluate and organize boolean expressions used in
 * locks.  Lock evaluation contains one of server's the three parsers.  The
 * other two parsers are for functions (see eval.cpp) and commands (see
 * command.cpp).
 */

#include "copyright.h"
#include "autoconf.h"
#include "config.h"
#include "externs.h"

#include "attrs.h"
#include "mathutil.h"

static bool parsing_internal = false;

/* ---------------------------------------------------------------------------
 * check_attr: indicate if attribute ATTR on player passes key when checked by
 * the object lockobj
 */

static bool check_attr(dbref player, dbref lockobj, ATTR *attr, UTF8 *key)
{
    dbref aowner;
    int aflags;
    bool bCheck = false;

    UTF8 *buff = atr_pget(player, attr->number, &aowner, &aflags);

    if (attr->number == A_LENTER)
    {
        // We can see enterlocks... else we'd break zones.
        //
        bCheck = true;
    }
    else if (See_attr(lockobj, player, attr))
    {
        bCheck = true;
    }
    else if (attr->number == A_NAME)
    {
        bCheck = true;
    }

    if (  bCheck
       && !wild_match(key, buff))
    {
        bCheck = false;
    }
    free_lbuf(buff);
    return bCheck;
}

bool eval_boolexp(dbref player, dbref thing, dbref from, BOOLEXP *b)
{
    if (b == TRUE_BOOLEXP)
    {
        return true;
    }

    dbref aowner, obj, source;
    int aflags;
    UTF8 *buff, *buff2, *bp;
    UTF8 *key;
    ATTR *a;
    bool bCheck, c;

    switch (b->type)
    {
    case BOOLEXP_AND:
        return   eval_boolexp(player, thing, from, b->sub1)
              && eval_boolexp(player, thing, from, b->sub2);

    case BOOLEXP_OR:
        return   eval_boolexp(player, thing, from, b->sub1)
              || eval_boolexp(player, thing, from, b->sub2);

    case BOOLEXP_NOT:
        return !eval_boolexp(player, thing, from, b->sub1);

    case BOOLEXP_INDIR:

        // BOOLEXP_INDIR (i.e. @) is a unary operation which is replaced at
        // evaluation time by the lock of the object whose number is the
        // argument of the operation.
        //
        mudstate.lock_nest_lev++;
        if (mudstate.lock_nest_lev >= mudconf.lock_nest_lim)
        {
            if (mudstate.bStandAlone)
            {
                Log.WriteString(T("Lock exceeded recursion limit." ENDLINE));
            }
            else
            {
                STARTLOG(LOG_BUGS, "BUG", "LOCK");
                log_name_and_loc(player);
                log_text(T(": Lock exceeded recursion limit."));
                ENDLOG;
                notify(player, T("Sorry, broken lock!"));
            }
            mudstate.lock_nest_lev--;
            return false;
        }
        if (  b->sub1->type != BOOLEXP_CONST
           || b->sub1->thing < 0)
        {
            if (mudstate.bStandAlone)
            {
                Log.WriteString(T("Broken lock." ENDLINE));
            }
            else
            {
                STARTLOG(LOG_BUGS, T("BUG"), T("LOCK"));
                log_name_and_loc(player);
                buff = alloc_mbuf("eval_boolexp.LOG.indir");
                mux_sprintf(buff, MBUF_SIZE, T(": Lock had bad indirection (%c, type %d)"),
                    INDIR_TOKEN, b->sub1->type);
                log_text(buff);
                free_mbuf(buff);
                ENDLOG;
                notify(player, T("Sorry, broken lock!"));
            }
            mudstate.lock_nest_lev--;
            return false;
        }
        key = atr_get("boolexp.130", b->sub1->thing, A_LOCK, &aowner, &aflags);
        c = eval_boolexp_atr(player, b->sub1->thing, from, key);
        free_lbuf(key);
        mudstate.lock_nest_lev--;
        return c;

    case BOOLEXP_CONST:
        return   b->thing == player
              || member(b->thing, Contents(player));

    case BOOLEXP_ATR:
        a = atr_num(b->thing);
        if (!a)
        {
            // No such attribute.
            //
            return false;
        }

        // First check the object itself, then its contents.
        //
        if (check_attr(player, from, a, (UTF8 *)b->sub1))
        {
            return true;
        }
        DOLIST(obj, Contents(player))
        {
            if (check_attr(obj, from, a, (UTF8 *)b->sub1))
            {
                return true;
            }
        }
        return false;

    case BOOLEXP_EVAL:

        a = atr_num(b->thing);
        if (  !a
           || MuxAlarm.bAlarmed)
        {
            // No such attribute.
            //
            return false;
        }
        source = from;
        buff = atr_pget(from, a->number, &aowner, &aflags);
        if (!buff || !*buff)
        {
            free_lbuf(buff);
            buff = atr_pget(thing, a->number, &aowner, &aflags);
            source = thing;
        }
        bCheck = false;

        if (  a->number == A_NAME
           || a->number == A_LENTER)
        {
            bCheck = true;
        }
        else if (bCanReadAttr(source, source, a, false))
        {
            bCheck = true;
        }

        if (bCheck)
        {
            reg_ref **preserve = NULL;
            preserve = PushRegisters(MAX_GLOBAL_REGS);
            save_global_regs(preserve);

            buff2 = bp = alloc_lbuf("eval_boolexp");
            mux_exec(buff, LBUF_SIZE-1, buff2, &bp, source, player, player,
                AttrTrace(aflags, EV_FIGNORE|EV_EVAL|EV_FCHECK|EV_TOP),
                NULL, 0);
            *bp = '\0';

            restore_global_regs(preserve);
            PopRegisters(preserve, MAX_GLOBAL_REGS);

            bCheck = !string_compare(buff2, (UTF8 *)b->sub1);
            free_lbuf(buff2);
        }
        free_lbuf(buff);
        return bCheck;

    case BOOLEXP_IS:

        // If an object check, do that.
        //
        if (b->sub1->type == BOOLEXP_CONST)
        {
            return (b->sub1->thing == player);
        }

        // Nope, do an attribute check
        //
        a = atr_num(b->sub1->thing);
        if (!a)
        {
            return false;
        }
        return check_attr(player, from, a, (UTF8 *)(b->sub1)->sub1);

    case BOOLEXP_CARRY:

        // If an object check, do that
        //
        if (b->sub1->type == BOOLEXP_CONST)
        {
            return member(b->sub1->thing, Contents(player));
        }

        // Nope, do an attribute check
        //
        a = atr_num(b->sub1->thing);
        if (!a)
        {
            return false;
        }
        DOLIST(obj, Contents(player))
        {
            if (check_attr(obj, from, a, (UTF8 *)(b->sub1)->sub1))
            {
                return true;
            }
        }
        return false;

    case BOOLEXP_OWNER:

        return (Owner(b->sub1->thing) == Owner(player));

    default:

        // Bad type
        //
        mux_assert(0);
        return false;
    }
}

bool eval_boolexp_atr(dbref player, dbref thing, dbref from, UTF8 *key)
{
    bool ret_value;

    BOOLEXP *b = parse_boolexp(player, key, true);
    if (b == NULL)
    {
        ret_value = true;
    }
    else
    {
        ret_value = eval_boolexp(player, thing, from, b);
        free_boolexp(b);
    }
    return ret_value;
}

// If the parser returns TRUE_BOOLEXP, you lose
// TRUE_BOOLEXP cannot be typed in by the user; use @unlock instead
//
static const char *parsebuf;
static char parsestore[LBUF_SIZE];
static dbref parse_player;

static void skip_whitespace(void)
{
    while (mux_isspace(*parsebuf))
    {
        parsebuf++;
    }
}

// Defined below.
//
static BOOLEXP *parse_boolexp_E(void);

static BOOLEXP *test_atr(UTF8 *s)
{
    UTF8 *s1;
    int anum;
    boolexp_type locktype;

    UTF8 *buff = alloc_lbuf("test_atr");
    mux_strncpy(buff, s, LBUF_SIZE-1);
    for (s = buff; *s && (*s != ':') && (*s != '/'); s++)
    {
        ; // Nothing.
    }
    if (!*s)
    {
        free_lbuf(buff);
        return TRUE_BOOLEXP;
    }
    if (*s == '/')
    {
        locktype = BOOLEXP_EVAL;
    }
    else
    {
        locktype = BOOLEXP_ATR;
    }

    *s++ = '\0';

    // See if left side is valid attribute.  Access to attr is checked on eval
    // Also allow numeric references to attributes. It can't hurt us, and lets
    // us import stuff that stores attr locks by number instead of by name.
    //
    ATTR *attrib = atr_str(buff);
    if (!attrib)
    {
        // Only #1 can lock on numbers
        //
        if (!God(parse_player))
        {
            free_lbuf(buff);
            return TRUE_BOOLEXP;
        }
        for (s1 = buff; mux_isdigit(*s1); s1++)
        {
            ; // Nothing.
        }
        if (*s1)
        {
            free_lbuf(buff);
            return TRUE_BOOLEXP;
        }
        anum = mux_atol(buff);
        if (anum <= 0)
        {
            free_lbuf(buff);
            return TRUE_BOOLEXP;
        }
    }
    else
    {
        anum = attrib->number;
    }

    // made it now make the parse tree node
    //
    BOOLEXP *b = alloc_bool("test_str");
    b->type = locktype;
    b->thing = (dbref) anum;
    b->sub1 = (BOOLEXP *) StringClone(s);
    free_lbuf(buff);
    return b;
}

// L -> (E); L -> object identifier
//
static BOOLEXP *parse_boolexp_L(void)
{
    BOOLEXP *b;
    UTF8    *p;
    UTF8    *buf;
    MSTATE  mstate;

    buf = NULL;
    skip_whitespace();

    switch (*parsebuf)
    {
    case '(':
        parsebuf++;
        b = parse_boolexp_E();
        skip_whitespace();
        if (  b == TRUE_BOOLEXP
           || *parsebuf++ != ')')
        {
            free_boolexp(b);
            return TRUE_BOOLEXP;
        }
        break;
    default:

        // Must have hit an object ref.  Load the name into our buffer.
        //
        buf = alloc_lbuf("parse_boolexp_L");
        p = buf;
        while (  *parsebuf
              && *parsebuf != AND_TOKEN
              && *parsebuf != OR_TOKEN
              && *parsebuf != ')'
              && p < buf + LBUF_SIZE)
        {
            *p++ = *parsebuf++;
        }

        // Strip trailing whitespace.
        //
        *p-- = '\0';
        while (mux_isspace(*p))
        {
            *p-- = '\0';
        }

        // Check for an attribute.
        //
        if ((b = test_atr(buf)) != NULL)
        {
            free_lbuf(buf);
            return (b);
        }
        b = alloc_bool("parse_boolexp_L");
        b->type = BOOLEXP_CONST;

        // do the match.
        //

        if (!mudstate.bStandAlone)
        {
            // If we are parsing a boolexp that was a stored lock then we
            // know that object refs are all dbrefs, so we skip the
            // expensive match code.
            //
            if (parsing_internal)
            {
                if (buf[0] != '#')
                {
                    free_lbuf(buf);
                    free_bool(b);
                    return TRUE_BOOLEXP;
                }
                b->thing = mux_atol(&buf[1]);
                if (!Good_dbref(b->thing))
                {
                    free_lbuf(buf);
                    free_bool(b);
                    return TRUE_BOOLEXP;
                }
            }
            else
            {
                save_match_state(&mstate);
                init_match(parse_player, buf, TYPE_THING);
                match_everything(MAT_EXIT_PARENTS);
                b->thing = match_result();
                restore_match_state(&mstate);
            }

            if (b->thing == NOTHING)
            {
                notify(parse_player, tprintf(T("I don\xE2\x80\x99t see %s here."), buf));
                free_lbuf(buf);
                free_bool(b);
                return TRUE_BOOLEXP;
            }
            if (b->thing == AMBIGUOUS)
            {
                notify(parse_player, tprintf(T("I don\xE2\x80\x99t know which %s you mean!"),
                    buf));
                free_lbuf(buf);
                free_bool(b);
                return TRUE_BOOLEXP;
            }
        }
        else
        {
            // Had better be #<num> or we're hosed.
            //
            if (buf[0] != '#')
            {
                free_lbuf(buf);
                free_bool(b);
                return TRUE_BOOLEXP;
            }
            b->thing = mux_atol(&buf[1]);
            if (b->thing < 0)
            {
                free_lbuf(buf);
                free_bool(b);
                return TRUE_BOOLEXP;
            }
        }
        free_lbuf(buf);
    }
    return b;
}

// F -> !F; F -> @L; F -> =L; F -> +L; F -> $L
// The argument L must be type BOOLEXP_CONST
//
static BOOLEXP *parse_boolexp_F(void)
{
    BOOLEXP *b2;

    skip_whitespace();
    switch (*parsebuf)
    {
    case NOT_TOKEN:

        parsebuf++;
        b2 = alloc_bool("parse_boolexp_F.not");
        b2->type = BOOLEXP_NOT;
        if ((b2->sub1 = parse_boolexp_F()) == TRUE_BOOLEXP)
        {
            free_boolexp(b2);
            return (TRUE_BOOLEXP);
        }
        else
        {
            return (b2);
        }

        // NOTREACHED
        //

    case INDIR_TOKEN:

        parsebuf++;
        b2 = alloc_bool("parse_boolexp_F.indir");
        b2->type = BOOLEXP_INDIR;
        b2->sub1 = parse_boolexp_L();
        if ((b2->sub1) == TRUE_BOOLEXP)
        {
            free_boolexp(b2);
            return (TRUE_BOOLEXP);
        }
        else if ((b2->sub1->type) != BOOLEXP_CONST)
        {
            free_boolexp(b2);
            return (TRUE_BOOLEXP);
        }
        else
        {
            return (b2);
        }

        // NOTREACHED
        //

    case IS_TOKEN:

        parsebuf++;
        b2 = alloc_bool("parse_boolexp_F.is");
        b2->type = BOOLEXP_IS;
        b2->sub1 = parse_boolexp_L();
        if (b2->sub1 == TRUE_BOOLEXP)
        {
            free_boolexp(b2);
            return (TRUE_BOOLEXP);
        }
        else if (  b2->sub1->type != BOOLEXP_CONST
                && b2->sub1->type != BOOLEXP_ATR)
        {
            free_boolexp(b2);
            return TRUE_BOOLEXP;
        }
        else
        {
            return (b2);
        }

        // NOTREACHED
        //

    case CARRY_TOKEN:

        parsebuf++;
        b2 = alloc_bool("parse_boolexp_F.carry");
        b2->type = BOOLEXP_CARRY;
        b2->sub1 = parse_boolexp_L();
        if (b2->sub1 == TRUE_BOOLEXP)
        {
            free_boolexp(b2);
            return TRUE_BOOLEXP;
        }
        else if (  b2->sub1->type != BOOLEXP_CONST
                && b2->sub1->type != BOOLEXP_ATR)
        {
            free_boolexp(b2);
            return TRUE_BOOLEXP;
        }
        else
        {
            return b2;
        }

        // NOTREACHED
        //

    case OWNER_TOKEN:

        parsebuf++;
        b2 = alloc_bool("parse_boolexp_F.owner");
        b2->type = BOOLEXP_OWNER;
        b2->sub1 = parse_boolexp_L();
        if (b2->sub1 == TRUE_BOOLEXP)
        {
            free_boolexp(b2);
            return TRUE_BOOLEXP;
        }
        else if (b2->sub1->type != BOOLEXP_CONST)
        {
            free_boolexp(b2);
            return TRUE_BOOLEXP;
        }
        else
        {
            return b2;
        }

        // NOTREACHED
        //

    default:
        return parse_boolexp_L();
    }
}

// T -> F; T -> F & T
//
static BOOLEXP *parse_boolexp_T(void)
{
    BOOLEXP *b, *b2;

    if ((b = parse_boolexp_F()) != TRUE_BOOLEXP)
    {
        skip_whitespace();
        if (*parsebuf == AND_TOKEN)
        {
            parsebuf++;

            b2 = alloc_bool("parse_boolexp_T");
            b2->type = BOOLEXP_AND;
            b2->sub1 = b;
            if ((b2->sub2 = parse_boolexp_T()) == TRUE_BOOLEXP)
            {
                free_boolexp(b2);
                return TRUE_BOOLEXP;
            }
            b = b2;
        }
    }
    return b;
}

// E -> T; E -> T | E
//
static BOOLEXP *parse_boolexp_E(void)
{
    BOOLEXP *b, *b2;

    if ((b = parse_boolexp_T()) != TRUE_BOOLEXP)
    {
        skip_whitespace();
        if (*parsebuf == OR_TOKEN)
        {
            parsebuf++;

            b2 = alloc_bool("parse_boolexp_E");
            b2->type = BOOLEXP_OR;
            b2->sub1 = b;
            if ((b2->sub2 = parse_boolexp_E()) == TRUE_BOOLEXP)
            {
                free_boolexp(b2);
                return TRUE_BOOLEXP;
            }
            b = b2;
        }
    }
    return b;
}

BOOLEXP *parse_boolexp(dbref player, const UTF8 *buf, bool internal)
{
    if (  NULL == buf
       || '\0' == buf[0])
    {
        return TRUE_BOOLEXP;
    }

    size_t n = strlen((char *)buf);
    if (n > sizeof(parsestore)-1)
    {
        n = sizeof(parsestore)-1;
    }
    memcpy(parsestore, buf, n+1);
    parsebuf = parsestore;
    parse_player = player;
    if (!mudstate.bStandAlone)
    {
        parsing_internal = internal;
    }
    return parse_boolexp_E();
}