summaryrefslogtreecommitdiffstats
path: root/generic/regc_color.c
blob: 003f5fcbeede15a201255b76a0df29327a51f806 (plain)
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
/*
 * colorings of characters
 * This file is #included by regcomp.c.
 *
 * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
 *
 * Development of this software was funded, in part, by Cray Research Inc.,
 * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
 * Corporation, none of whom are responsible for the results. The author
 * thanks all of them.
 *
 * Redistribution and use in source and binary forms -- with or without
 * modification -- are permitted for any purpose, provided that
 * redistributions in source form retain this entire copyright notice and
 * indicate the origin and nature of any modifications.
 *
 * I'd appreciate being given credit for this package in the documentation of
 * software which uses it, but that is not a requirement.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Note that there are some incestuous relationships between this code and NFA
 * arc maintenance, which perhaps ought to be cleaned up sometime.
 */

#define	CISERR()	VISERR(cm->v)
#define	CERR(e)		VERR(cm->v, (e))

/*
 - initcm - set up new colormap
 ^ static VOID initcm(struct vars *, struct colormap *);
 */
static void
initcm(
    struct vars *v,
    struct colormap *cm)
{
    int i;
    int j;
    union tree *t;
    union tree *nextt;
    struct colordesc *cd;

    cm->magic = CMMAGIC;
    cm->v = v;

    cm->ncds = NINLINECDS;
    cm->cd = cm->cdspace;
    cm->max = 0;
    cm->free = 0;

    cd = cm->cd;		/* cm->cd[WHITE] */
    cd->sub = NOSUB;
    cd->arcs = NULL;
    cd->flags = 0;
    cd->nchrs = CHR_MAX - CHR_MIN + 1;

    /*
     * Upper levels of tree.
     */

    for (t=&cm->tree[0], j=NBYTS-1 ; j>0 ; t=nextt, j--) {
	nextt = t + 1;
	for (i=BYTTAB-1 ; i>=0 ; i--) {
	    t->tptr[i] = nextt;
	}
    }

    /*
     * Bottom level is solid white.
     */

    t = &cm->tree[NBYTS-1];
    for (i=BYTTAB-1 ; i>=0 ; i--) {
	t->tcolor[i] = WHITE;
    }
    cd->block = t;
}

/*
 - freecm - free dynamically-allocated things in a colormap
 ^ static VOID freecm(struct colormap *);
 */
static void
freecm(
    struct colormap *cm)
{
    size_t i;
    union tree *cb;

    cm->magic = 0;
    if (NBYTS > 1) {
	cmtreefree(cm, cm->tree, 0);
    }
    for (i=1 ; i<=cm->max ; i++) {	/* skip WHITE */
	if (!UNUSEDCOLOR(&cm->cd[i])) {
	    cb = cm->cd[i].block;
	    if (cb != NULL) {
		FREE(cb);
	    }
	}
    }
    if (cm->cd != cm->cdspace) {
	FREE(cm->cd);
    }
}

/*
 - cmtreefree - free a non-terminal part of a colormap tree
 ^ static VOID cmtreefree(struct colormap *, union tree *, int);
 */
static void
cmtreefree(
    struct colormap *cm,
    union tree *tree,
    int level)			/* level number (top == 0) of this block */
{
    int i;
    union tree *t;
    union tree *fillt = &cm->tree[level+1];
    union tree *cb;

    assert(level < NBYTS-1);	/* this level has pointers */
    for (i=BYTTAB-1 ; i>=0 ; i--) {
	t = tree->tptr[i];
	assert(t != NULL);
	if (t != fillt) {
	    if (level < NBYTS-2) {	/* more pointer blocks below */
		cmtreefree(cm, t, level+1);
		FREE(t);
	    } else {		/* color block below */
		cb = cm->cd[t->tcolor[0]].block;
		if (t != cb) {	/* not a solid block */
		    FREE(t);
		}
	    }
	}
    }
}

/*
 - setcolor - set the color of a character in a colormap
 ^ static color setcolor(struct colormap *, pchr, pcolor);
 */
static color			/* previous color */
setcolor(
    struct colormap *cm,
    pchr c,
    pcolor co)
{
    uchr uc = c;
    int shift;
    int level;
    int b;
    int bottom;
    union tree *t;
    union tree *newt;
    union tree *fillt;
    union tree *lastt;
    union tree *cb;
    color prev;

    assert(cm->magic == CMMAGIC);
    if (CISERR() || co == COLORLESS) {
	return COLORLESS;
    }

    t = cm->tree;
    for (level=0, shift=BYTBITS*(NBYTS-1) ; shift>0; level++, shift-=BYTBITS){
	b = (uc >> shift) & BYTMASK;
	lastt = t;
	t = lastt->tptr[b];
	assert(t != NULL);
	fillt = &cm->tree[level+1];
	bottom = (shift <= BYTBITS) ? 1 : 0;
	cb = (bottom) ? cm->cd[t->tcolor[0]].block : fillt;
	if (t == fillt || t == cb) {	/* must allocate a new block */
	    newt = (union tree *) MALLOC((bottom) ?
		    sizeof(struct colors) : sizeof(struct ptrs));
	    if (newt == NULL) {
		CERR(REG_ESPACE);
		return COLORLESS;
	    }
	    if (bottom) {
		memcpy(newt->tcolor, t->tcolor, BYTTAB*sizeof(color));
	    } else {
		memcpy(newt->tptr, t->tptr, BYTTAB*sizeof(union tree *));
	    }
	    t = newt;
	    lastt->tptr[b] = t;
	}
    }

    b = uc & BYTMASK;
    prev = t->tcolor[b];
    t->tcolor[b] = (color) co;
    return prev;
}

/*
 - maxcolor - report largest color number in use
 ^ static color maxcolor(struct colormap *);
 */
static color
maxcolor(
    struct colormap *cm)
{
    if (CISERR()) {
	return COLORLESS;
    }

    return (color) cm->max;
}

/*
 - newcolor - find a new color (must be subject of setcolor at once)
 * Beware: may relocate the colordescs.
 ^ static color newcolor(struct colormap *);
 */
static color			/* COLORLESS for error */
newcolor(
    struct colormap *cm)
{
    struct colordesc *cd;
    size_t n;

    if (CISERR()) {
	return COLORLESS;
    }

    if (cm->free != 0) {
	assert(cm->free > 0);
	assert((size_t) cm->free < cm->ncds);
	cd = &cm->cd[cm->free];
	assert(UNUSEDCOLOR(cd));
	assert(cd->arcs == NULL);
	cm->free = cd->sub;
    } else if (cm->max < cm->ncds - 1) {
	cm->max++;
	cd = &cm->cd[cm->max];
    } else {
	struct colordesc *newCd;

	/*
	 * Oops, must allocate more.
	 */

	n = cm->ncds * 2;
	if (cm->cd == cm->cdspace) {
	    newCd = (struct colordesc *) MALLOC(n * sizeof(struct colordesc));
	    if (newCd != NULL) {
		memcpy(newCd, cm->cdspace,
			cm->ncds * sizeof(struct colordesc));
	    }
	} else {
	    newCd = (struct colordesc *)
		    REALLOC(cm->cd, n * sizeof(struct colordesc));
	}
	if (newCd == NULL) {
	    CERR(REG_ESPACE);
	    return COLORLESS;
	}
	cm->cd = newCd;
	cm->ncds = n;
	assert(cm->max < cm->ncds - 1);
	cm->max++;
	cd = &cm->cd[cm->max];
    }

    cd->nchrs = 0;
    cd->sub = NOSUB;
    cd->arcs = NULL;
    cd->flags = 0;
    cd->block = NULL;

    return (color) (cd - cm->cd);
}

/*
 - freecolor - free a color (must have no arcs or subcolor)
 ^ static VOID freecolor(struct colormap *, pcolor);
 */
static void
freecolor(
    struct colormap *cm,
    pcolor co)
{
    struct colordesc *cd = &cm->cd[co];
    color pco, nco;		/* for freelist scan */

    assert(co >= 0);
    if (co == WHITE) {
	return;
    }

    assert(cd->arcs == NULL);
    assert(cd->sub == NOSUB);
    assert(cd->nchrs == 0);
    cd->flags = FREECOL;
    if (cd->block != NULL) {
	FREE(cd->block);
	cd->block = NULL;	/* just paranoia */
    }

    if ((size_t) co == cm->max) {
	while (cm->max > WHITE && UNUSEDCOLOR(&cm->cd[cm->max])) {
	    cm->max--;
	}
	assert(cm->free >= 0);
	while ((size_t) cm->free > cm->max) {
	    cm->free = cm->cd[cm->free].sub;
	}
	if (cm->free > 0) {
	    assert(cm->free < cm->max);
	    pco = cm->free;
	    nco = cm->cd[pco].sub;
	    while (nco > 0) {
		if ((size_t) nco > cm->max) {
		    /*
		     * Take this one out of freelist.
		     */

		    nco = cm->cd[nco].sub;
		    cm->cd[pco].sub = nco;
		} else {
		    assert(nco < cm->max);
		    pco = nco;
		    nco = cm->cd[pco].sub;
		}
	    }
	}
    } else {
	cd->sub = cm->free;
	cm->free = (color) (cd - cm->cd);
    }
}

/*
 - pseudocolor - allocate a false color, to be managed by other means
 ^ static color pseudocolor(struct colormap *);
 */
static color
pseudocolor(
    struct colormap *cm)
{
    color co;

    co = newcolor(cm);
    if (CISERR()) {
	return COLORLESS;
    }
    cm->cd[co].nchrs = 1;
    cm->cd[co].flags = PSEUDO;
    return co;
}

/*
 - subcolor - allocate a new subcolor (if necessary) to this chr
 ^ static color subcolor(struct colormap *, pchr c);
 */
static color
subcolor(
    struct colormap *cm,
    pchr c)
{
    color co;			/* current color of c */
    color sco;			/* new subcolor */

    co = GETCOLOR(cm, c);
    sco = newsub(cm, co);
    if (CISERR()) {
	return COLORLESS;
    }
    assert(sco != COLORLESS);

    if (co == sco) {		/* already in an open subcolor */
	return co;		/* rest is redundant */
    }
    cm->cd[co].nchrs--;
    cm->cd[sco].nchrs++;
    setcolor(cm, c, sco);
    return sco;
}

/*
 - newsub - allocate a new subcolor (if necessary) for a color
 ^ static color newsub(struct colormap *, pcolor);
 */
static color
newsub(
    struct colormap *cm,
    pcolor co)
{
    color sco;			/* new subcolor */

    sco = cm->cd[co].sub;
    if (sco == NOSUB) {		/* color has no open subcolor */
	if (cm->cd[co].nchrs == 1) {	/* optimization */
	    return co;
	}
	sco = newcolor(cm);	/* must create subcolor */
	if (sco == COLORLESS) {
	    assert(CISERR());
	    return COLORLESS;
	}
	cm->cd[co].sub = sco;
	cm->cd[sco].sub = sco;	/* open subcolor points to self */
    }
    assert(sco != NOSUB);

    return sco;
}

/*
 - subrange - allocate new subcolors to this range of chrs, fill in arcs
 ^ static VOID subrange(struct vars *, pchr, pchr, struct state *,
 ^ 	struct state *);
 */
static void
subrange(
    struct vars *v,
    pchr from,
    pchr to,
    struct state *lp,
    struct state *rp)
{
    uchr uf;
    int i;

    assert(from <= to);

    /*
     * First, align "from" on a tree-block boundary
     */

    uf = (uchr) from;
    i = (int) (((uf + BYTTAB - 1) & (uchr) ~BYTMASK) - uf);
    for (; from<=to && i>0; i--, from++) {
	newarc(v->nfa, PLAIN, subcolor(v->cm, from), lp, rp);
    }
    if (from > to) {		/* didn't reach a boundary */
	return;
    }

    /*
     * Deal with whole blocks.
     */

    for (; to-from>=BYTTAB ; from+=BYTTAB) {
	subblock(v, from, lp, rp);
    }

    /*
     * Clean up any remaining partial table.
     */

    for (; from<=to ; from++) {
	newarc(v->nfa, PLAIN, subcolor(v->cm, from), lp, rp);
    }
}

/*
 - subblock - allocate new subcolors for one tree block of chrs, fill in arcs
 ^ static VOID subblock(struct vars *, pchr, struct state *, struct state *);
 */
static void
subblock(
    struct vars *v,
    pchr start,			/* first of BYTTAB chrs */
    struct state *lp,
    struct state *rp)
{
    uchr uc = start;
    struct colormap *cm = v->cm;
    int shift;
    int level;
    int i;
    int b;
    union tree *t;
    union tree *cb;
    union tree *fillt;
    union tree *lastt;
    int previ;
    int ndone;
    color co;
    color sco;

    assert((uc % BYTTAB) == 0);

    /*
     * Find its color block, making new pointer blocks as needed.
     */

    t = cm->tree;
    fillt = NULL;
    for (level=0, shift=BYTBITS*(NBYTS-1); shift>0; level++, shift-=BYTBITS) {
	b = (uc >> shift) & BYTMASK;
	lastt = t;
	t = lastt->tptr[b];
	assert(t != NULL);
	fillt = &cm->tree[level+1];
	if (t == fillt && shift > BYTBITS) {	/* need new ptr block */
	    t = (union tree *) MALLOC(sizeof(struct ptrs));
	    if (t == NULL) {
		CERR(REG_ESPACE);
		return;
	    }
	    memcpy(t->tptr, fillt->tptr, BYTTAB*sizeof(union tree *));
	    lastt->tptr[b] = t;
	}
    }

    /*
     * Special cases: fill block or solid block.
     */
    co = t->tcolor[0];
    cb = cm->cd[co].block;
    if (t == fillt || t == cb) {
	/*
	 * Either way, we want a subcolor solid block.
	 */

	sco = newsub(cm, co);
	t = cm->cd[sco].block;
	if (t == NULL) {	/* must set it up */
	    t = (union tree *) MALLOC(sizeof(struct colors));
	    if (t == NULL) {
		CERR(REG_ESPACE);
		return;
	    }
	    for (i=0 ; i<BYTTAB ; i++) {
		t->tcolor[i] = sco;
	    }
	    cm->cd[sco].block = t;
	}

	/*
	 * Find loop must have run at least once.
	 */

	lastt->tptr[b] = t;
	newarc(v->nfa, PLAIN, sco, lp, rp);
	cm->cd[co].nchrs -= BYTTAB;
	cm->cd[sco].nchrs += BYTTAB;
	return;
    }

    /*
     * General case, a mixed block to be altered.
     */

    i = 0;
    while (i < BYTTAB) {
	co = t->tcolor[i];
	sco = newsub(cm, co);
	newarc(v->nfa, PLAIN, sco, lp, rp);
	previ = i;
	do {
	    t->tcolor[i++] = sco;
	} while (i < BYTTAB && t->tcolor[i] == co);
	ndone = i - previ;
	cm->cd[co].nchrs -= ndone;
	cm->cd[sco].nchrs += ndone;
    }
}

/*
 - okcolors - promote subcolors to full colors
 ^ static VOID okcolors(struct nfa *, struct colormap *);
 */
static void
okcolors(
    struct nfa *nfa,
    struct colormap *cm)
{
    struct colordesc *cd;
    struct colordesc *end = CDEND(cm);
    struct colordesc *scd;
    struct arc *a;
    color co;
    color sco;

    for (cd=cm->cd, co=0 ; cd<end ; cd++, co++) {
	sco = cd->sub;
	if (UNUSEDCOLOR(cd) || sco == NOSUB) {
	    /*
	     * Has no subcolor, no further action.
	     */
	} else if (sco == co) {
	    /*
	     * Is subcolor, let parent deal with it.
	     */
	} else if (cd->nchrs == 0) {
	    /*
	     * Parent empty, its arcs change color to subcolor.
	     */

	    cd->sub = NOSUB;
	    scd = &cm->cd[sco];
	    assert(scd->nchrs > 0);
	    assert(scd->sub == sco);
	    scd->sub = NOSUB;
	    while ((a = cd->arcs) != NULL) {
		assert(a->co == co);
		/* uncolorchain(cm, a); */
		cd->arcs = a->colorchain;
		a->co = sco;
		/* colorchain(cm, a); */
		a->colorchain = scd->arcs;
		scd->arcs = a;
	    }
	    freecolor(cm, co);
	} else {
	    /*
	     * Parent's arcs must gain parallel subcolor arcs.
	     */

	    cd->sub = NOSUB;
	    scd = &cm->cd[sco];
	    assert(scd->nchrs > 0);
	    assert(scd->sub == sco);
	    scd->sub = NOSUB;
	    for (a=cd->arcs ; a!=NULL ; a=a->colorchain) {
		assert(a->co == co);
		newarc(nfa, a->type, sco, a->from, a->to);
	    }
	}
    }
}

/*
 - colorchain - add this arc to the color chain of its color
 ^ static VOID colorchain(struct colormap *, struct arc *);
 */
static void
colorchain(
    struct colormap *cm,
    struct arc *a)
{
    struct colordesc *cd = &cm->cd[a->co];

    a->colorchain = cd->arcs;
    cd->arcs = a;
}

/*
 - uncolorchain - delete this arc from the color chain of its color
 ^ static VOID uncolorchain(struct colormap *, struct arc *);
 */
static void
uncolorchain(
    struct colormap *cm,
    struct arc *a)
{
    struct colordesc *cd = &cm->cd[a->co];
    struct arc *aa;

    aa = cd->arcs;
    if (aa == a) {		/* easy case */
	cd->arcs = a->colorchain;
    } else {
	assert(aa != NULL);
	for (; aa->colorchain!=a ; aa=aa->colorchain) {
	    assert(aa->colorchain != NULL);
	    continue;
	}
	aa->colorchain = a->colorchain;
    }
    a->colorchain = NULL;	/* paranoia */
}

/*
 - rainbow - add arcs of all full colors (but one) between specified states
 ^ static VOID rainbow(struct nfa *, struct colormap *, int, pcolor,
 ^ 	struct state *, struct state *);
 */
static void
rainbow(
    struct nfa *nfa,
    struct colormap *cm,
    int type,
    pcolor but,			/* COLORLESS if no exceptions */
    struct state *from,
    struct state *to)
{
    struct colordesc *cd;
    struct colordesc *end = CDEND(cm);
    color co;

    for (cd=cm->cd, co=0 ; cd<end && !CISERR(); cd++, co++) {
	if (!UNUSEDCOLOR(cd) && (cd->sub != co) && (co != but)
		&& !(cd->flags&PSEUDO)) {
	    newarc(nfa, type, co, from, to);
	}
    }
}

/*
 - colorcomplement - add arcs of complementary colors
 * The calling sequence ought to be reconciled with cloneouts().
 ^ static VOID colorcomplement(struct nfa *, struct colormap *, int,
 ^ 	struct state *, struct state *, struct state *);
 */
static void
colorcomplement(
    struct nfa *nfa,
    struct colormap *cm,
    int type,
    struct state *of,		/* complements of this guy's PLAIN outarcs */
    struct state *from,
    struct state *to)
{
    struct colordesc *cd;
    struct colordesc *end = CDEND(cm);
    color co;

    assert(of != from);
    for (cd=cm->cd, co=0 ; cd<end && !CISERR() ; cd++, co++) {
	if (!UNUSEDCOLOR(cd) && !(cd->flags&PSEUDO)) {
	    if (findarc(of, PLAIN, co) == NULL) {
		newarc(nfa, type, co, from, to);
	    }
	}
    }
}

#ifdef REG_DEBUG
/*
 ^ #ifdef REG_DEBUG
 */

/*
 - dumpcolors - debugging output
 ^ static VOID dumpcolors(struct colormap *, FILE *);
 */
static void
dumpcolors(
    struct colormap *cm,
    FILE *f)
{
    struct colordesc *cd;
    struct colordesc *end;
    color co;
    chr c;
    char *has;

    fprintf(f, "max %ld\n", (long) cm->max);
    if (NBYTS > 1) {
	fillcheck(cm, cm->tree, 0, f);
    }
    end = CDEND(cm);
    for (cd=cm->cd+1, co=1 ; cd<end ; cd++, co++) {	/* skip 0 */
	if (!UNUSEDCOLOR(cd)) {
	    assert(cd->nchrs > 0);
	    has = (cd->block != NULL) ? "#" : "";
	    if (cd->flags&PSEUDO) {
		fprintf(f, "#%2ld%s(ps): ", (long) co, has);
	    } else {
		fprintf(f, "#%2ld%s(%2d): ", (long) co, has, cd->nchrs);
	    }

	    /*
	     * It's hard to do this more efficiently.
	     */

	    for (c=CHR_MIN ; c<CHR_MAX ; c++) {
		if (GETCOLOR(cm, c) == co) {
		    dumpchr(c, f);
		}
	    }
	    assert(c == CHR_MAX);
	    if (GETCOLOR(cm, c) == co) {
		dumpchr(c, f);
	    }
	    fprintf(f, "\n");
	}
    }
}

/*
 - fillcheck - check proper filling of a tree
 ^ static VOID fillcheck(struct colormap *, union tree *, int, FILE *);
 */
static void
fillcheck(
    struct colormap *cm,
    union tree *tree,
    int level,			/* level number (top == 0) of this block */
    FILE *f)
{
    int i;
    union tree *t;
    union tree *fillt = &cm->tree[level+1];

    assert(level < NBYTS-1);	/* this level has pointers */
    for (i=BYTTAB-1 ; i>=0 ; i--) {
	t = tree->tptr[i];
	if (t == NULL) {
	    fprintf(f, "NULL found in filled tree!\n");
	} else if (t == fillt) {
	    /* empty body */
	} else if (level < NBYTS-2) {	/* more pointer blocks below */
	    fillcheck(cm, t, level+1, f);
	}
    }
}

/*
 - dumpchr - print a chr
 * Kind of char-centric but works well enough for debug use.
 ^ static VOID dumpchr(pchr, FILE *);
 */
static void
dumpchr(
    pchr c,
    FILE *f)
{
    if (c == '\\') {
	fprintf(f, "\\\\");
    } else if (c > ' ' && c <= '~') {
	putc((char) c, f);
    } else {
	fprintf(f, "\\u%04lx", (long) c);
    }
}

/*
 ^ #endif
 */
#endif				/* ifdef REG_DEBUG */

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */