File: DBtechtype.c

package info (click to toggle)
magic 8.3.105%2Bds.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 17,128 kB
  • sloc: ansic: 175,615; sh: 7,634; tcl: 4,536; lisp: 2,554; makefile: 946; cpp: 587; python: 389; csh: 148; awk: 140
file content (829 lines) | stat: -rw-r--r-- 21,464 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
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
/*
 * DBtechtype.c --
 *
 * Creation of tile and plane types and their names.
 * Lookup procedures are in DBtechname.c
 *
 *     *********************************************************************
 *     * Copyright (C) 1985, 1990 Regents of the University of California. *
 *     * Permission to use, copy, modify, and distribute this              *
 *     * software and its documentation for any purpose and without        *
 *     * fee is hereby granted, provided that the above copyright          *
 *     * notice appear in all copies.  The University of California        *
 *     * makes no representations about the suitability of this            *
 *     * software for any purpose.  It is provided "as is" without         *
 *     * express or implied warranty.  Export of this software outside     *
 *     * of the United States of America may require an export license.    *
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/database/DBtechtype.c,v 1.2 2008/09/05 13:56:25 tim Exp $";
#endif  /* not lint */

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "utils/magic.h"
#include "utils/geometry.h"
#include "utils/utils.h"
#include "tiles/tile.h"
#include "utils/hash.h"
#include "database/database.h"
#include "database/databaseInt.h"
#include "utils/tech.h"
#include "textio/textio.h"
#include "utils/malloc.h"

    /* Types and their names */
int DBNumTypes;
char *DBTypeLongNameTbl[NT];
int DBTypePlaneTbl[NT];		/* Normally accessed as macro "DBPlane(x)" */
NameList dbTypeNameLists = {NULL, NULL, NULL, (ClientData)0, FALSE};
HashTable DBTypeAliasTable;

    /* Planes and their names */
int DBNumPlanes;
char *DBPlaneLongNameTbl[PL_MAXTYPES];
NameList dbPlaneNameLists = {NULL, NULL, NULL, (ClientData)0, FALSE};


    /*
     * Sets of types.
     * These are generated after the "types" section of the
     * technology file has been read, but before any automatically
     * generated types (contact images) are created.
     */
int DBNumUserLayers;
TileTypeBitMask DBZeroTypeBits;
TileTypeBitMask DBAllTypeBits;
TileTypeBitMask DBBuiltinLayerBits;
TileTypeBitMask DBAllButSpaceBits;
TileTypeBitMask DBAllButSpaceAndDRCBits;
TileTypeBitMask DBSpaceBits;
TileTypeBitMask DBUserLayerBits;
TileTypeBitMask DBActiveLayerBits;	/* Layers that are locked */
TileTypeBitMask DBTechActiveLayerBits;	/* Layers marked locked in the techfile */


/* Table of default, builtin planes */
DefaultPlane dbTechDefaultPlanes[] =
{
    PL_ROUTER,		"router",
    PL_DRC_ERROR,	"designRuleError",
    PL_DRC_CHECK,	"designRuleCheck",
    PL_M_HINT,		"mhint",
    PL_F_HINT,		"fhint",
    PL_R_HINT,		"rhint",
    0,			0,	0
};

/* Table of default, builtin types */
DefaultType dbTechDefaultTypes[] =
{
    TT_SPACE,		-1,		"space",		FALSE,
    TT_CHECKPAINT,	PL_DRC_CHECK,	"checkpaint,CP",	FALSE,
    TT_CHECKSUBCELL,	PL_DRC_CHECK,	"checksubcell,CS",	FALSE,
    TT_ERROR_P,		PL_DRC_ERROR,	"error_p,EP",		FALSE,
    TT_ERROR_S,		PL_DRC_ERROR,	"error_s,ES",		FALSE,
    TT_ERROR_PS,	PL_DRC_ERROR,	"error_ps,EPS",		FALSE,
    TT_MAGNET,		PL_M_HINT,	"magnet,mag",		TRUE,
    TT_FENCE,		PL_F_HINT,	"fence,f",		TRUE,
    TT_ROTATE,		PL_R_HINT,	"rotate,r",		TRUE,
    0,			0,		NULL,			0
};

/* Forward declarations */
char *dbTechNameAdd();
NameList *dbTechNameAddOne();

/*
 * ----------------------------------------------------------------------------
 *
 * DBTechInitPlane --
 *
 * Initialize the default plane information.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Initializes DBNumPlanes to PL_TECHDEPBASE.
 *	Initializes DBPlaneLongNameTbl[] for builtin planes.
 *
 * ----------------------------------------------------------------------------
 */

void
DBTechInitPlane()
{
    DefaultPlane *dpp;
    char *cp;

    /* Clear out any old information */
    if (dbPlaneNameLists.sn_next != NULL)
    {
	NameList *tbl;

	for (tbl = dbPlaneNameLists.sn_next; tbl != &dbPlaneNameLists;
			tbl = tbl->sn_next)
	{
	    freeMagic(tbl->sn_name);
	    freeMagic(tbl);
	}
    }

    /* Tables of short names */
    dbPlaneNameLists.sn_next = &dbPlaneNameLists;
    dbPlaneNameLists.sn_prev = &dbPlaneNameLists;

    for (dpp = dbTechDefaultPlanes; dpp->dp_names; dpp++)
    {
	cp = dbTechNameAdd(dpp->dp_names, (ClientData) dpp->dp_plane,
			&dbPlaneNameLists, FALSE);
	if (cp == NULL)
	{
	    TxError("DBTechInit: can't add plane names %s\n", dpp->dp_names);
	    niceabort();
	}
	DBPlaneLongNameTbl[dpp->dp_plane] = cp;
    }

    DBNumPlanes = PL_TECHDEPBASE;
}


/*
 * ----------------------------------------------------------------------------
 *
 * DBTypeInit --
 *
 * Initialization routine for the types database on startup (runs only
 * once).
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Hash table initialization.
 *
 * ----------------------------------------------------------------------------
 */

void
DBTypeInit()
{
    HashInit(&DBTypeAliasTable, 8, HT_STRINGKEYS);
}

/*
 * ----------------------------------------------------------------------------
 *
 * DBTechInitType --
 *
 * Add the names and planes of the builtin types.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	See above.
 *	Initializes DBNumTypes to TT_TECHDEPBASE.
 *
 * ----------------------------------------------------------------------------
 */

void
DBTechInitType()
{
    DefaultType *dtp;
    char *cp;

    /* Clear out any old information */
    if (dbTypeNameLists.sn_next != NULL)
    {
	NameList *tbl;

	for (tbl = dbTypeNameLists.sn_next; tbl != &dbTypeNameLists;
		tbl = tbl->sn_next)
	{
	    freeMagic(tbl->sn_name);
	    freeMagic(tbl);
	}
    }

    /* Tables of short names */
    dbTypeNameLists.sn_next = &dbTypeNameLists;
    dbTypeNameLists.sn_prev = &dbTypeNameLists;

    /*
     * Add the type names to the list of known names, and set
     * the default plane for each type.
     */
    for (dtp = dbTechDefaultTypes; dtp->dt_names; dtp++)
    {
	cp = dbTechNameAdd(dtp->dt_names, (ClientData) dtp->dt_type,
			&dbTypeNameLists, FALSE);
	if (cp == NULL)
	{
	    TxError("DBTechInit: can't add type names %s\n", dtp->dt_names);
	    niceabort();
	}
	DBTypeLongNameTbl[dtp->dt_type] = cp;
	DBPlane(dtp->dt_type) = dtp->dt_plane;
        TTMaskSetOnlyType(&DBLayerTypeMaskTbl[dtp->dt_type], dtp->dt_type);
    }

    /* Zero the active layers (this mask is inverted later) */
    TTMaskZero(&DBActiveLayerBits);

    /* Hash table of layer aliases, free'ing the allocated type masks */
    HashFreeKill(&DBTypeAliasTable);
    HashInit(&DBTypeAliasTable, 8, HT_STRINGKEYS);

    DBNumTypes = TT_TECHDEPBASE;
}

/*
 * ----------------------------------------------------------------------------
 *
 * DBTechAddPlane --
 *
 * Define a tile plane type for the new technology.
 *
 * Results:
 *	TRUE if successful, FALSE on error
 *
 * Side effects:
 *	Updates the database technology variables.
 *	In particular, updates the number of known tile planes.
 *
 * ----------------------------------------------------------------------------
 */

    /*ARGSUSED*/
bool
DBTechAddPlane(sectionName, argc, argv)
    char *sectionName;
    int argc;
    char *argv[];
{
    char *cp;

    if (DBNumPlanes >= PL_MAXTYPES)
    {
	TechError("Too many tile planes (max=%d)\n", PL_MAXTYPES);
	return FALSE;
    }

    if (argc != 1)
    {
	TechError("Line must contain names for plane\n");
	return FALSE;
    }

    cp = dbTechNameAdd(argv[0], (ClientData) DBNumPlanes, &dbPlaneNameLists, FALSE);
    if (cp == NULL)
	return FALSE;
    DBPlaneLongNameTbl[DBNumPlanes++] = cp;
    return TRUE;
}

/*
 * ----------------------------------------------------------------------------
 *
 * DBTechAddNameToType --
 *
 * Add a new name (alias) to an existing tile type.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Adds "newname" to dbTypeNameLists.  May change the primary name
 *	of the type stored in the pointer array DBTypeLongNameTbl[].
 *
 * ----------------------------------------------------------------------------
 */

void
DBTechAddNameToType(newname, ttype, canonical)
    char *newname;
    TileType ttype;
    bool canonical;
{
    char *cp;

    cp = dbTechNameAdd(newname, (ClientData) ttype, &dbTypeNameLists, TRUE);
    if (canonical)
	DBTypeLongNameTbl[ttype] = cp;
}

/*
 * ----------------------------------------------------------------------------
 *
 * DBTechAddAlias --
 *
 * Define an alias representing a mask of one or more tiletypes
 *
 * Results:
 *	TRUE if successful, FALSE on error.  Returns TRUE on some
 *	errors that may not necessarily be fatal.
 *
 * Side effects:
 *	Updates the database technology variables.
 *	In particular, updates the alias hash table.
 *
 * ----------------------------------------------------------------------------
 */

bool
DBTechAddAlias(sectionName, argc, argv)
    char *sectionName;
    int argc;
    char *argv[];
{
    char *cp;
    int pNum;
    HashEntry *he;
    TileType atype;
    TileTypeBitMask *amask, tmask;

    if (argc < 2)
    {
	TechError("Line must contain at least 2 fields\n");
	return TRUE;
    }

    /* First check that the alias name does not shadow an existing type */
    if (DBTechNameTypeExact(argv[0]) >= 0)
    {
	TechError("Type alias \"%s\" shadows a defined type\n", argv[0]);
	return TRUE;
    }

    /* Next, check the type list.  If there is only one type, then	*/
    /* add the name to the type's names as usual, instead of adding	*/
    /* it to the alias hash table, which is meant for names that	*/
    /* map to multiple types.						*/

    DBTechNoisyNameMask(argv[1], &tmask);
    if ((atype = DBTechNameType(argv[1])) >= 0)
	if (TTMaskEqual(&DBLayerTypeMaskTbl[atype], &tmask))
	{
	    DBTechAddNameToType(argv[0], atype, FALSE);
	    return TRUE;
        }

    he = HashFind(&DBTypeAliasTable, argv[0]);
    amask = (TileTypeBitMask *)HashGetValue(he);
    if (amask == NULL)
    {
	amask = (TileTypeBitMask *)mallocMagic(sizeof(TileTypeBitMask));
	TTMaskZero(amask);
	TTMaskSetMask(amask, &tmask);
	HashSetValue(he, amask);
    }
    else
    {
	TechError("Type \"%s\" is already defined and cannot be an alias\n",
			argv[0]);
	return TRUE;
    }
    return TRUE;
}


/*
 * ----------------------------------------------------------------------------
 *
 * DBTechAddType --
 *
 * Define a tile type for the new technology.
 *
 * Results:
 *	TRUE if successful, FALSE on error.  Returns TRUE on some
 *	errors that may not necessarily be fatal.
 *
 * Side effects:
 *	Updates the database technology variables.
 *	In particular, updates the number of known tile types.
 *
 * ----------------------------------------------------------------------------
 */

    /*ARGSUSED*/
bool
DBTechAddType(sectionName, argc, argv)
    char *sectionName;
    int argc;
    char *argv[];
{
    char *cp;
    int pNum;

    if (DBNumTypes >= TT_MAXTYPES-TT_RESERVEDTYPES)
    {
	TechError("Too many tile types (max=%d)\n",
		TT_MAXTYPES-TT_RESERVEDTYPES);
	return FALSE;
    }

    if (argc < 2)
    {
	TechError("Line must contain at least 2 fields\n");
	return TRUE;
    }

    if (!strcmp(argv[0], "alias"))
    {
	/* Check that we have not used "*" in the alias list, since if	*/
	/* we define aliases in the "types" section (which is allowed	*/
	/* for backwards compatibility), the contacts have not yet been	*/
	/* defined.							*/

	if (strchr(argv[2], '*') != NULL)
	{
	    TechError("Type alias \"%s\" contains the wildcard character "
			"\"*\" (alias ignored).\n"
			"Perhaps you want to define aliases in "
			"the \"alias\" section?\n", argv[2]);
	    return TRUE;
	}
	return (DBTechAddAlias(sectionName, argc - 1, argv + 1));
    }
    else
    {
	cp = dbTechNameAdd(argv[1], (ClientData) DBNumTypes, &dbTypeNameLists, FALSE);
	if (cp == NULL)
	    return FALSE;

	/* Set the lock layer bit if the plane name begins with '-' */
	if (*argv[0] == LOCKLAYERCHAR)
	{
	    TTMaskSetType(&DBActiveLayerBits, DBNumTypes);
	    argv[0]++;
	}

	pNum = DBTechNoisyNamePlane(argv[0]);
	if (pNum < 0)
	    return FALSE;

	DBTypeLongNameTbl[DBNumTypes] = cp;
	DBPlane(DBNumTypes) = pNum;
	TTMaskSetOnlyType(&DBLayerTypeMaskTbl[DBNumTypes], DBNumTypes);
	DBNumTypes++;
    }

    return TRUE;
}



/*
 * ----------------------------------------------------------------------------
 *  dbTechNewStackedType --
 *
 *	Generate a new tiletype for a stacked layer (generally speaking,
 *	a stacked contact).  Return the new tile type for the stacked
 *	contact type.
 *
 *  Results:
 *	The new tile type
 *
 *  Side effects:
 *	Updates the name tables with the new type name.  Note that other
 *	tables, such as the home plane table, are not affected by this
 *	routine.
 * ----------------------------------------------------------------------------
 */

TileType
dbTechNewStackedType(type1, type2)
    TileType type1, type2;
{
    char buf[1024], *cp;

    if (DBNumTypes >= TT_MAXTYPES - TT_RESERVEDTYPES)
    {
	TechError("Too many types to generate a new contact.  Maximum=%d\n",
			TT_MAXTYPES - TT_RESERVEDTYPES);
	return (TileType) -1;
    }

    sprintf(buf, "%s+%s", DBTypeShortName(type1), DBTypeShortName(type2));
    cp = dbTechNameAdd(buf, (ClientData) DBNumTypes, &dbTypeNameLists, FALSE);
    if (cp == NULL)
    {
	TechError("Couldn't generate new stacking type %s\n", buf);
	return (TileType) -1;
    }

    DBTypeLongNameTbl[DBNumTypes] = cp;

    return DBNumTypes++;
}

/*
 * ----------------------------------------------------------------------------
 *
 * DBTechFinalType --
 *
 * After processing the types and planes sections, compute the
 * various derived type and plane masks and tables.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Initializes DBNumUserLayers to be DBNumTypes at the time
 *	    this procedure is called, since none of the automatically
 *	    generated plane images have yet been created.
 *	Initializes the following bit masks:
 *		DBAllTypeBits
 *		DBSpaceBits
 *		DBBuiltinLayerBits
 *		DBAllButSpaceBits
 *		DBAllButSpaceAndDRCBits
 *		DBUserLayerBits
 *
 * ----------------------------------------------------------------------------
 */

void
DBTechFinalType()
{
    TileType i;
    int pNum;

    DBNumUserLayers = DBNumTypes;

    for (i = 0; i < TT_MAXTYPES; i++)
    {
	if (i >= TT_SELECTBASE)
	    TTMaskSetType(&DBAllButSpaceAndDRCBits, i);

	if (i < TT_TECHDEPBASE)
	    TTMaskSetType(&DBBuiltinLayerBits, i);
	else if (i < DBNumUserLayers)
	    TTMaskSetType(&DBUserLayerBits, i);
    }

    TTMaskCom2(&DBAllTypeBits, &DBZeroTypeBits);
    TTMaskSetOnlyType(&DBSpaceBits, TT_SPACE);
    TTMaskCom2(&DBAllButSpaceBits, &DBSpaceBits);

    /* UserLayerBits includes space */
    TTMaskSetType(&DBUserLayerBits, TT_SPACE);

    /* Set locked layer bitmask */
    TTMaskCom(&DBActiveLayerBits);
    TTMaskAndMask(&DBActiveLayerBits, &DBAllButSpaceAndDRCBits);
    /* Save this mask for later reference */
    TTMaskZero(&DBTechActiveLayerBits);
    TTMaskSetMask(&DBTechActiveLayerBits, &DBActiveLayerBits);

    /* Space is visible on all planes except the router */
    TTMaskZero(&DBPlaneTypes[PL_ROUTER]);
    for (pNum = PL_PAINTBASE;  pNum < PL_MAXTYPES;  pNum++)
	TTMaskSetOnlyType(&DBPlaneTypes[pNum], TT_SPACE);
}

/*
 * ----------------------------------------------------------------------------
 *
 * dbTechNameLookupExact --
 *
 * Lookup a type or plane name.
 * Case is significant.
 *
 * Results:
 *	Returns the ClientData associated with the given name.
 *	If the name was not found, we return -2.  Unlike dbTechNameLookup,
 *	this routine requires an exact name match, and never returns -1
 *	(which indicates ambiguity in dbTechNameLookup()).
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

ClientData
dbTechNameLookupExact(str, table)
    char *str;		/* The name to be looked up */
    NameList *table;	/* Table of names to search */
{
    NameList *top;

    top = table;
    while ((top = top->sn_next) != table)
	if (!strcmp(top->sn_name, str))
	    return (top->sn_value);

    return ((ClientData) -2);
}


/*
 * ----------------------------------------------------------------------------
 *
 * dbTechNameLookup --
 *
 * Lookup a type or plane name.
 * Case is significant.
 *
 * Results:
 *	Returns the ClientData associated with the given name.
 *	If the name was not found, we return -2; if it was ambiguous,
 *	we return -1.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

ClientData
dbTechNameLookup(str, table)
    char *str;		/* The name to be looked up */
    NameList *table;	/* Table of names to search */
{
    /*
     * The search is carried out by using two pointers, one which moves
     * forward through list from its start, and one which moves backward
     * through table from its end.  The two pointers mark the range of
     * strings that match the portion of str that we have scanned.  When
     * all of the characters of str have been scanned, then the two
     * pointers better be identical, or one of the strings in the range
     * between the two pointers better match 'str' exactly.
     */
    NameList *bot, *top;
    char currentchar;
    int indx;

    bot = table->sn_next;
    top = table->sn_prev;
    if (top == bot) return ((ClientData) -2);

    for (indx = 0; ; indx++)
    {
	/* Check for the end of string */
	currentchar = str[indx];
	if (currentchar == '\0')
	{
	    if (bot == top)
		return (bot->sn_value);

	    /*
	     * Several entries match this one up to the last character
	     * of the string.  If one is an exact match, we allow it;
	     * otherwise, we claim the string was ambiguous.
	     */
	    for ( ; bot != top; bot = bot->sn_next)
		if (bot->sn_name[indx] == '\0')
		    return (bot->sn_value);

	    return ((ClientData) -1);
	}

	/*
	 * Move bot up until the string it points to matches str in the
	 * indx'th position.  Make match refer to the indx of bot in table.
	 */
	while (bot->sn_name[indx] != currentchar)
	{
	    if (bot == top) return((ClientData) -2);
	    bot = bot->sn_next;
	}

	/* Move top down until it matches */
	while (top->sn_name[indx] != currentchar)
	{
	    if (bot == top) return((ClientData) -2);
	    top = top->sn_prev;
	}
    }

    /*NOTREACHED*/
}

/*
 * ----------------------------------------------------------------------------
 *
 * dbTechNameAdd --
 *
 * Add several names to a name table.
 * The shortest name is marked as the standard "short" name
 * for this cdata value.
 *
 * Results:
 *	Returns a pointer to the first name added if successful,
 *	or NULL if not.  In this latter case, we print error messages.
 *
 * Side effects:
 *	Adds new entries to the table pointed to by *ptable, and
 *	modifies *ptable if necessary.
 *
 * ----------------------------------------------------------------------------
 */

char *
dbTechNameAdd(name, cdata, ptable, alias)
    char *name;	/* Comma-separated list of names to be added */
    ClientData cdata;		/* Value to be stored with each name above */
    NameList *ptable;		/* Table to which we will add names */
    int alias;			/* 1 if this is an alias (never make primary) */
{
    char *cp;
    char onename[BUFSIZ];
    char *first;
    int shortestLength, length;
    NameList *primary, *current;

    if (name == NULL)
	return (NULL);

    first = NULL;
    shortestLength = INFINITY;
    primary = NULL;
    while (*name)
    {
	if (*name == ',')
	{
	    name++;
	    continue;
	}
	for (cp = onename; *name && *name != ','; *cp++ = *name++)
	    /* Nothing */;
	*cp = '\0';
	if (*(cp = onename))
	{
	    if ((current = dbTechNameAddOne(cp, cdata, FALSE, alias, ptable)) == NULL)
		return (NULL);
	    if (first == NULL)
		first = current->sn_name;
	    length = strlen(onename);
	    if (length < shortestLength)
		shortestLength = length, primary = current;
	}
    }

    if (primary && (alias == 0))
	primary->sn_primary = TRUE;
    return (first);
}

/*
 * ----------------------------------------------------------------------------
 *
 * dbTechNameAddOne --
 *
 * Add a single name to the table.
 *
 * Results:
 *	Returns a pointer to the new entry if succesful,
 *	or NULL if the name was already in the table.
 *
 * Side effects:
 *	See above.
 *
 * ----------------------------------------------------------------------------
 */

NameList *
dbTechNameAddOne(name, cdata, isPrimary, isAlias, ptable)
    char *name;	/* Name to be added */
    ClientData cdata;		/* Client value associated with this name */
    bool isPrimary;		/* TRUE if this is the primary abbreviation */
    bool isAlias;		/* TRUE if this name is an alias */
    NameList *ptable;		/* Table of names to which we're adding this */
{
    int cmp;
    NameList *tbl, *new;

    /* Sort the name into the existing list */
    for (tbl = ptable->sn_next ;tbl != ptable; tbl = tbl->sn_next)
	if ((cmp = strcmp(name, tbl->sn_name)) == 0)
	{
	    TechError("Duplicate name: %s\n", name);
	    return (NULL);
	}
	else if (cmp < 0)
	    break;

    /* Create a new name */
    new = (NameList *) mallocMagic((unsigned) (sizeof (NameList)));
    new->sn_name = StrDup((char **) NULL, name);
    new->sn_value = cdata;
    new->sn_primary = isPrimary;
    new->sn_alias = isAlias;

    /* Link this entry in to the list before 'tbl' */
    new->sn_next = tbl;
    new->sn_prev = tbl->sn_prev;
    tbl->sn_prev->sn_next = new;
    tbl->sn_prev = new;
    return (new);
}