File: main.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (924 lines) | stat: -rw-r--r-- 23,335 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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/*
 * main.c --
 *
 * The topmost module of the Magic VLSI tool.  This module
 * initializes the other modules and then calls the 'textio'
 * module to read and execute commands.
 *
 *     ********************************************************************* 
 *     * 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-7.5/utils/main.c,v 1.6 2008/02/07 17:33:00 tim Exp $";
#endif  /* not lint */

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

#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>

#include "utils/main.h"
#include "utils/magic.h"
#include "utils/malloc.h"
#include "utils/magsgtty.h"
#include "utils/hash.h"
#include "utils/macros.h"
#include "textio/textio.h"
#include "utils/geometry.h"
#include "textio/txcommands.h"
#include "tiles/tile.h"
#include "utils/tech.h"
#include "database/database.h"
#include "drc/drc.h"
#include "windows/windows.h"
#include "graphics/graphics.h"
#include "dbwind/dbwind.h"
#include "commands/commands.h"
#include "utils/signals.h"
#include "utils/utils.h"
#include "utils/runstats.h"
#include "cif/cif.h"
#ifdef ROUTE_MODULE
#include "router/router.h"
#endif
#ifdef LEF_MODULE
#include "lef/lef.h"
#endif
#include "extract/extract.h"
#include "utils/undo.h"
#include "netmenu/netmenu.h"
#include "plow/plow.h"
#include "utils/paths.h"
#include "wiring/wiring.h"
#ifdef PLOT_MODULE
#include "plot/plot.h"
#endif
#include "sim/sim.h"
#include "utils/list.h"
#ifdef ROUTE_MODULE
#include "mzrouter/mzrouter.h"
#endif
#include "lisp/lisp.h"
#ifdef THREE_D
#include "graphics/wind3d.h"
#endif


/*
 * Global data structures
 *
 */

global char	*Path = NULL;		/* Search path */
global char	*CellLibPath = NULL;	/* Used to find cells. */
global char	*SysLibPath = NULL;	/* Used to find color maps, styles, */
					/* technologies, etc. */

/*
 * Flag that tells if various options have been set on the command line
 * (see utils.h for explanation of individual flags).
 */

#ifdef MAGIC_WRAPPER
global short RuntimeFlags = MAIN_MAKE_WINDOW | MAIN_TK_CONSOLE;
#else
global short RuntimeFlags = MAIN_MAKE_WINDOW;
#endif

/*
 * See the file main.h for a description of the information kept
 * pertaining to the edit cell.
 */

global CellUse	*EditCellUse = NULL;
global CellDef	*EditRootDef = NULL;
global Transform EditToRootTransform;
global Transform RootToEditTransform;


/*
 * data structures local to main.c
 *
 */

/* the filename specified on the command line */
static char *MainFileName = NULL;	

/* RC file specified on the command line */
static char *RCFileName = NULL;	

/* the first filename specified on the command line */
typedef struct filename {
    char *fn;
    struct filename *fn_prev;
} FileName;
FileName *CurrentName;

/* the filename for the graphics and mouse ports */
global char *MainGraphicsFile = NULL;
global char *MainMouseFile = NULL;

/* information about the color display. */
global char *MainDisplayType = NULL;
global char *MainMonType = NULL;


/* Copyright notice for the binary file. */
global char *MainCopyright = "\n--- MAGIC: Copyright (C) 1985, 1990 "
		"Regents of the University of California.  ---\n";

/* Forward declarations */
char *mainArg();


/*
 * ----------------------------------------------------------------------------
 * MainExit:
 *
 *	Magic's own exit procedure
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	We exit.
 * ----------------------------------------------------------------------------
 */

void
MainExit(errNum)
    int errNum;
{
#ifdef	MOCHA
    MochaExit(errNum);
#endif
    if (GrClosePtr != NULL) /* We are not guarenteed that everthing will
			     * be initialized already!
			     */
	GrClose();

    DBRemoveBackup();

    TxFlush();
    TxResetTerminal();

    exit(errNum);
}

/*
 * ----------------------------------------------------------------------------
 *
 * mainDoArgs:
 *
 *	Process command line arguments
 *
 * Results:
 *	Return 0 on success, 1 on failure
 *
 * Side effects:
 *	Global variables are modified
 *
 * Notes:
 *	In order to work properly with the -F flag, we need to
 *	use StrDup() to make copies of any arguments we want
 *	to be visible when we restart a frozen file.
 *
 * ----------------------------------------------------------------------------
 */

int
mainDoArgs(argc, argv)
    int argc;
    char **argv;
{
    bool haveDashI = FALSE;
    char *cp;

    /* Startup filename (may be changed with the "-rcfile" option or	*/
    /* the "-norcfile" option).						*/

    RCFileName = StrDup((char **) NULL, ".magicrc");

    argc--;
    while (argc-- > 0)
    {
	argv++;
	if (**argv == '-')
	{
	    switch (argv[0][1])
	    {
		case 'g':
		    if ((cp = mainArg(&argc, &argv, "tty name")) == NULL)
			return 1;
		    MainGraphicsFile = StrDup((char **) NULL, cp);
		    if (!haveDashI)
			MainMouseFile = MainGraphicsFile;
		    break;

		case 'i':
		    haveDashI = TRUE;
		    if ((cp = mainArg(&argc, &argv, "tty name")) == NULL)
			return 1;
		    MainMouseFile = StrDup((char **) NULL, cp);
		    break;

		case 'd':
		    if ((cp = mainArg(&argc, &argv, "display type")) ==NULL)
			return 1;
		    MainDisplayType = StrDup((char **) NULL, cp);
		    break;

		case 'm':
		    if ((cp = mainArg(&argc, &argv, "monitor type")) ==NULL)
			return 1;
		    MainMonType = StrDup((char **) NULL, cp);
		    break;

		/*
		 * Declare the technology.
		 */
		case 'T':
		    if ((cp = mainArg(&argc, &argv, "technology")) == NULL)
			return 1;
		    TechDefault = StrDup((char **) NULL, cp);
		    TechOverridesDefault = TRUE;
		    break;

		/*
		 * -r or -re or -recover: Recover a crash file.
		 * -rc or -rcfile: Declare a specific startup file to read.
		 */
		case 'r':
		    if ((strlen(argv[0]) <= 2) || argv[0][2] == 'e')
			RuntimeFlags |= MAIN_RECOVER;
		    else if ((argc > 1) && (argv[0][2] == 'c'))
		    {
			argv[0][2] = '\0';
		        if ((cp = mainArg(&argc, &argv, "startup file")) == NULL)
			    return 1;
			RCFileName = StrDup((char **) NULL, cp);
		    }
		    else
		    {
			TxError("Unknown option: '%s'\n", *argv);
			return 1;
		    }
		    break;

		/* 
		 * We are being debugged.
		 */
		case 'D':
		    RuntimeFlags |= MAIN_DEBUG;
		    break;

#ifdef MAGIC_WRAPPER
		/*
		 * "-w" for wrapper * implies -nowindow (no initial window)
		 */
		case 'w':
		    RuntimeFlags &= ~MAIN_MAKE_WINDOW;
		    break;
		/*
		 * No initial window / no console options / no startup file read.
		 */
		case 'n':
		    if (strlen(argv[0]) < 4)
		    {
			TxError("Ambiguous option %s:  use -nowindow, -noconsole, "
				"or -norcfile\n", argv[0]);
			return 1;
		    }
		    else if (argv[0][3] == 'c')
			RuntimeFlags &= ~MAIN_TK_CONSOLE;
		    else if (argv[0][3] == 'w')
			RuntimeFlags &= ~MAIN_MAKE_WINDOW;
		    else if (argv[0][3] == 'r')
		    {
			freeMagic(RCFileName);
			RCFileName = NULL;
		    }
		    else
		    {
			TxError("Unknown option: '%s'\n", *argv);
			return 1;
		    }
		    break;
#endif
		default:
		    TxError("Unknown option: '%s'\n", *argv);
		    TxError("Usage:  magic [-g gPort] [-d devType] [-m monType] "
				"[-i tabletPort] [-D] [-F objFile saveFile]\n"
				"[-T technology] [-rcfile startupFile | -norcfile]"
#ifdef MAGIC_WRAPPER
				"[-noconsole] [-nowindow] [-wrapper] "
#endif
				"[file]\n");
		    return 1;
	    }
	}
	else if (MakeMainWindow)
	{
	    if (MainFileName == NULL) {
		MainFileName = StrDup((char **) NULL, *argv);
		CurrentName = (FileName *) mallocMagic(sizeof(FileName));
		CurrentName->fn = MainFileName;
		CurrentName->fn_prev = (FileName *) NULL;
	    }
	    else
	    {
		FileName *temporary;

		temporary = (FileName *) mallocMagic(sizeof(FileName));
		temporary->fn = StrDup((char **) NULL, *argv);
		temporary->fn_prev = CurrentName;
		CurrentName = temporary;
	    }

	    /* Remove suffix if the file name already has it */
	    {
		char *c,*d;

		for(c = CurrentName->fn; (*c) != '\0'; c++);
		for(d = DBSuffix; (*d) != '\0'; d++);
		while( (*c) == (*d) ) {
		    if (c == MainFileName) break;
		    if (d == DBSuffix) {
			(*c) = '\0';
			break;
		    }
		    c--;
		    d--;
		}
	    }
	}
    }

    return 0;

badfopt:
    TxError("-F requires load and save file names, e.g.,");
    TxError(" \"-F magic magicsave\"\n");
exitnow:
    return 1;
}

/*
 * ----------------------------------------------------------------------------
 *
 * mainArg --
 *
 * Pull off an argument from the (argc, argv) pair and also check
 * to make sure it's not another flag (i.e, it doesn't begin with
 * a '-').
 *
 * Results:
 *	Return pointer to the argument string.
 *
 * Side effects:
 *	See the comments in ArgStr() in the utils module -- they
 *	apply here.
 *
 * ----------------------------------------------------------------------------
 */

char *
mainArg(pargc, pargv, mesg)
    int *pargc;
    char ***pargv;
    char *mesg;
{
    char option, *cp;

    option = (*pargv)[0][1];
    cp = ArgStr(pargc, pargv, mesg);
    if (cp == NULL)
	return (char *) NULL;

    if (*cp == '-')
    {
	TxError("Bad name after '-%c' option: '%s'\n", option, cp);
	return (char *) NULL;
    }
    return cp;
}


/*
 * ----------------------------------------------------------------------------
 * mainInitBeforeArgs:
 *
 *	Initializes things before argument processing.
 *
 * Results:
 *	0 on success.  As written, there are no failure modes.
 *
 * Side effects:
 *	All sorts of initialization.  Most initialization, however, is done
 *	in 'mainInitAfterArgs'.
 * ----------------------------------------------------------------------------
 */

int
mainInitBeforeArgs(argc, argv)
    int argc;
    char *argv[];
{
    TechOverridesDefault = FALSE;
    if (Path == NULL)
	Path = StrDup((char **) NULL, ".");

    /* initialize text display */
    TxInit();
    TxSetTerminal();

#ifdef SCHEME_INTERPRETER
    /* Initialize Lisp module. (rajit@cs.caltch.edu) */
    LispInit();
#endif

    /* Identify version first thing */
    TxPrintf("\nMagic %s revision %s - Compiled on %s.\n", MagicVersion,
		MagicRevision, MagicCompileTime);

    /*
     * Get preliminary info on the graphics display.
     * This may be overriden later.
     */
    GrGuessDisplayType(&MainGraphicsFile, &MainMouseFile, 
	&MainDisplayType, &MainMonType);
    FindDisplay((char *)NULL, "displays", CAD_LIB_PATH, &MainGraphicsFile,
	&MainMouseFile, &MainDisplayType, &MainMonType);

    return 0;
}


/*
 * ----------------------------------------------------------------------------
 * mainInitAfterArgs:
 *
 *	Initializes things after argument processing.
 *
 * Results:
 *	Status: 0 = success, 1 = failure to set graphics display,
 *	2 = failure to load technology file.
 *
 * Side effects:
 *	All sorts of initialization.
 * ----------------------------------------------------------------------------
 */

int
mainInitAfterArgs()
{
    int (*nullProc)() = 0;
    SectionID sec_tech, sec_planes, sec_types, sec_aliases;
    SectionID sec_styles;
    SectionID sec_connect, sec_contact, sec_compose;
    SectionID sec_cifinput, sec_cifoutput;
    SectionID sec_drc, sec_extract, sec_wiring, sec_router;
    SectionID sec_plow, sec_plot, sec_mzrouter;

    /* If no technology has been specified yet, try to read one from
     * the initial cell, or else assign a default.
     */

    if ((TechDefault == NULL) && (MainFileName != NULL))
	(void) StrDup(&TechDefault, DBGetTech(MainFileName));
    if (TechDefault == NULL)
	TechDefault = "scmos";

    DBTypeInit();
    MacroInit();

#ifdef SCHEME_INTERPRETER
    /* Pass technology name to Lisp interpreter (rajit@cs.caltech.edu) */
    LispSetTech (TechDefault);
#endif

#ifdef LEF_MODULE
    /* Pre-techfile-loading intialization of the LEF module */
    LefInit();
#endif

#ifdef OPENACCESS
    OAInit();
#endif

    /* TxPrintf("Using technology \"%s\".\n", TechDefault); */

    /*
     * Setup path names for system directory searches
     */

    StrDup(&SysLibPath, MAGIC_SYS_PATH);

    CellLibPath = (char *)mallocMagic(strlen(MAGIC_LIB_PATH) + strlen(TechDefault) - 1);
    (void) sprintf(CellLibPath, MAGIC_LIB_PATH, TechDefault);
    
    if (MainGraphicsFile == NULL) MainGraphicsFile = "/dev/null";
    if (MainMouseFile == NULL) MainMouseFile = MainGraphicsFile;

    /* catch signals, must come after mainDoArgs & before SigWatchFile */
    SigInit();

    /* set up graphics */
    if ( !GrSetDisplay(MainDisplayType, MainGraphicsFile, MainMouseFile) )
    {
	return 1;
    }

    /* initialize technology */
    TechInit();
    TechAddClient("tech", DBTechInit, DBTechSetTech, nullProc,
			(SectionID) 0, &sec_tech, FALSE);
    TechAddClient("version", DBTechInitVersion, DBTechSetVersion, nullProc,
			(SectionID) 0, (int *)0, TRUE);
    TechAddClient("planes",	DBTechInitPlane, DBTechAddPlane, nullProc,
			(SectionID) 0, &sec_planes, FALSE);
    TechAddClient("types", DBTechInitType, DBTechAddType, DBTechFinalType,
			sec_planes, &sec_types, FALSE);

    TechAddClient("styles", nullProc, DBWTechAddStyle, nullProc,
			sec_types, &sec_styles, FALSE);

    TechAddClient("contact", DBTechInitContact,
			DBTechAddContact, DBTechFinalContact,
			sec_types|sec_planes, &sec_contact, FALSE);

    TechAddAlias("contact", "images");
    TechAddClient("aliases", nullProc, DBTechAddAlias, nullProc,
			sec_planes|sec_types|sec_contact, &sec_aliases, TRUE);

    TechAddClient("compose", DBTechInitCompose,
			DBTechAddCompose, DBTechFinalCompose,
			sec_types|sec_planes|sec_contact, &sec_compose, FALSE);

    TechAddClient("connect", DBTechInitConnect,
			DBTechAddConnect, DBTechFinalConnect,
			sec_types|sec_planes|sec_contact, &sec_connect, FALSE);

#ifdef CIF_MODULE
    TechAddClient("cifoutput", CIFTechStyleInit, CIFTechLine, CIFTechFinal,
			(SectionID) 0, &sec_cifoutput, FALSE);
	
    TechAddClient("cifinput", CIFReadTechStyleInit, CIFReadTechLine,
		    CIFReadTechFinal, (SectionID) 0, &sec_cifinput, FALSE);
#else
    TechAddClient("cifoutput", nullProc,nullProc,nullProc,
			(SectionID) 0, &sec_cifoutput, FALSE);
	
    TechAddClient("cifinput", nullProc,nullProc,nullProc,
		     (SectionID) 0, &sec_cifinput, FALSE);
#endif
#ifdef ROUTE_MODULE
    TechAddClient("mzrouter", MZTechInit, MZTechLine, MZTechFinal,
			sec_types|sec_planes, &sec_mzrouter, TRUE);
#else
    TechAddClient("mzrouter", nullProc,nullProc,nullProc,
			sec_types|sec_planes, &sec_mzrouter, TRUE);
#endif
    TechAddClient("drc", DRCTechStyleInit, DRCTechLine, DRCTechFinal,
			sec_types|sec_planes, &sec_drc, FALSE);

#ifdef LEF_MODULE
    TechAddClient("lef", LefTechInit, LefTechLine, nullProc,
			sec_types|sec_planes, (SectionID *) 0, TRUE);
#endif

#ifdef NO_EXT
    TechAddClient("extract", nullProc, nullProc,nullProc,
			sec_types|sec_connect, &sec_extract, FALSE);
#else
    TechAddClient("extract", nullProc, ExtTechLine, ExtTechFinal,
			sec_types|sec_connect, &sec_extract, FALSE);
#endif
	
    TechAddClient("wiring", WireTechInit, WireTechLine, WireTechFinal,
			sec_types, &sec_wiring, TRUE);

#ifdef ROUTE_MODULE
    TechAddClient("router", RtrTechInit, RtrTechLine, RtrTechFinal,
			sec_types, &sec_router, TRUE);
#else
    TechAddClient("router", nullProc,nullProc,nullProc,
			sec_types, &sec_router, TRUE);
#endif
    TechAddClient("plowing", PlowTechInit, PlowTechLine, PlowTechFinal,
			sec_types|sec_connect|sec_contact, &sec_plow, TRUE);
#ifdef PLOT_MODULE
    TechAddClient("plot", PlotTechInit, PlotTechLine, PlotTechFinal,
			sec_types, &sec_plot, TRUE);
#else
    TechAddClient("plot", nullProc,nullProc,nullProc,
			sec_types, &sec_plot, TRUE);
#endif

    /* Load the technology file */

    if (!TechLoad(TechDefault, 0))
    {
	/* Don't give up just yet---user may have put a tech	*/
	/* load command in the RC file.  Load the default	*/
	/* technology, and only complain if that doesn't work	*/
	/* either.						*/

	if (!TechLoad("scmos", 0))
	{
	    TxError("Cannot load technology \"scmos\" or \"%s\"\n", TechDefault);
	    return 2;
	}
    }

    if (DBTechName != 0) {
	TxPrintf("Using technology \"%s\"", DBTechName);
	if (DBTechVersion != 0) TxPrintf(", version %s.", DBTechVersion);
	TxPrintf("\n");
    }
    if (DBTechDescription != 0) TxPrintf("%s\n", DBTechDescription);

    /* initialize the undo package */
    (void) UndoInit((char *) NULL, (char *) NULL);

    /* initialize windows */
    WindInit();

    /* initialize commands */
    CmdInit();

    /* Initalize the interface between windows and its clients */
    DBWinit();
#ifdef USE_READLINE
    TxInitReadline();
#endif
    CMWinit();
#ifdef THREE_D
    W3Dinit();
#endif

    /* Initialize the circuit extractor */
#ifndef NO_EXT
    ExtInit();
#endif

    /* Initialize plowing */
    PlowInit();

    /* Initialize selection */
    SelectInit();

    /* Initialize the wiring module */
    WireInit();

#ifdef ROUTE_MODULE
    /* Initialize the netlist menu */
    NMinit();
#endif

    /* Initialize the design-rule checker */
    DRCInit();

    /* Initialize the maze router */
#ifdef ROUTE_MODULE
    MZInit();

    /* Initialize the interactive router - 
     * NOTE the mzrouter must be initialized prior to the irouter
     * so that default parameters will be completely setup
     */
    IRDebugInit();
    IRAfterTech();
#endif

    PlowAfterTech();	/* Copies DRC rule information into plow database */

	/* Initialize the Sim Module (the part of it which involves (i)rsim) */
#if !defined(NO_SIM_MODULE) && defined(RSIM_MODULE)
    SimInit();
#endif

    TxSetPoint(GR_CURSOR_X, GR_CURSOR_Y, WIND_UNKNOWN_WINDOW);

    return 0;
}

/*
 * ----------------------------------------------------------------------------
 * mainInitFinal:
 *
 *	Final initialization; reads startup files and any initial file
 *	specified.
 *
 * Results:
 *	Return 0 on success.  As written, there is no failure mode.
 *
 * Side effects:
 *	All sorts of initialization.
 * ----------------------------------------------------------------------------
 */

int
mainInitFinal()
{
    char *home;
    char startupFileName[100];
    FILE *f;

    /* Read in system startup file, if it exists. */
    f = PaOpen(MAGIC_SYS_DOT, "r", (char *) NULL, ".",
	    (char *) NULL, (char **) NULL);
    if (f != NULL)
    { 
	TxDispatch(f); 
	(void) fclose(f);
    }

    /*
     * Strive for a wee bit more parallelism; let the graphics
     * display run while we're reading in startup files & initial cell.
     */

    GrFlush();

    /* Ignore this whole section if we have received the -norc option */
    if (RCFileName != NULL)
    {

	/* Read in user's startup files, if there are any. */
	/* If the "-rcfile" option has been used, and it doesn't specify   */
	/* a full path, then look for this file in the home directory too. */

	home = getenv("HOME");
	if (home != NULL && (RCFileName[0] != '/'))
	{
	    (void) sprintf(startupFileName, "%s/%s", home, RCFileName);
	    f = PaOpen(startupFileName, "r", (char *) NULL, ".",
		(char *) NULL, (char **) NULL);

	    if ((f == NULL) && (!strcmp(RCFileName, ".magicrc")))
	    {
		/* Try the (deprecated) name ".magic" */
		(void) sprintf(startupFileName, "%s/.magic", home);
		f = PaOpen(startupFileName, "r", (char *) NULL, ".",
		    (char *) NULL, (char **) NULL);
		if (f != NULL)
		    TxPrintf("Note:  Use of the file name \"~/.magic\" is deprecated."
			"  Please change this to \"~/.magicrc\".\n");
	    }

	    if (f != NULL)
	    {
		TxDispatch(f); 
		(void) fclose(f);
	    }
	}

	/* Read in any startup file in the current directory, or one that was	*/
	/* specified on the commandline by the "-rcfile <name>" option.		*/

	f = PaOpen(RCFileName, "r", (char *) NULL, ".",
			(char *) NULL, (char **) NULL);

	/* Again, check for the deprecated name ".magic" */
	if (f == NULL)
	{
	    if (!strcmp(RCFileName, ".magicrc"))
	    {
		f = PaOpen(".magic", "r", (char *) NULL, ".",
			(char *) NULL, (char **) NULL);
		if (f != NULL)
		    TxPrintf("Note:  Use of the file name \"./.magic\" is deprecated."
				"  Please change this to \"./.magicrc\".\n");

		else
		    f = PaOpen("magic_setup", "r", (char *) NULL, ".",
				(char *) NULL, (char **) NULL);
	    }
	    else
		TxError("Startup file \"%s\" not found or unreadable!\n", RCFileName);
	}

	if (f != NULL)
	{
	    TxDispatch(f); 
	    fclose(f);
	}
    }

    /*
     * Recover crash files from the temp directory if we have specified
     * the -r option on the command line (non-tcl version.  Tcl version
     * uses the command-line command crashrecover.
     */

    if (mainRecover && MakeMainWindow)
    {
	DBFileRecovery();
    }

    /*
     * Bring in a new cell or cells to start up if one was given
     * on the command line
     */

    else if (MainFileName && MakeMainWindow)
    {
	FileName *temporary;

	while(CurrentName != NULL)
	{
	    temporary = CurrentName;
	    CurrentName = temporary->fn_prev;
	    DBWreload(temporary->fn);
	    freeMagic(temporary);
	}
    }
    
    /* Create an initial box. */

    if (MakeMainWindow && EditCellUse)
	DBWSetBox(EditCellUse->cu_def, &EditCellUse->cu_def->cd_bbox);

    /* Set the initial fence for undo-ing:  don't want to be able to
     * undo past this point.
     */
    UndoFlush();
    TxClearPoint();

    return 0;
}


/*
 * ----------------------------------------------------------------------------
 * mainFinish:
 *
 *	Finish up things for Magic.  This routine is NOT called on an
 *	error exit.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Various things, such as stopping measurement gathering.
 * ----------------------------------------------------------------------------
 */

void
mainFinished()
{
    /* Close up things */
    MainExit(0);
}

/*---------------------------------------------------------------------------
 * magicMain:
 *
 *	Top-level procedure of the Magic Layout System.  There is purposely
 *	not much in here so that we have more flexibility.  Also, it is
 *	not called 'main' so that other programs that use Magic may do
 *	something else.
 *
 * Results:	
 *	None.
 *
 * Side Effects:
 *	None.
 *
 * Note:  Try not to add code to this procedure.  Add it instead to one of the
 *	procedures that it calls.
 *
 *----------------------------------------------------------------------------
 */

void
magicMain(argc, argv)
    int argc;
    char *argv[];
{
    int rstatus;

    if ((rstatus = mainInitBeforeArgs(argc, argv)) != 0) MainExit(rstatus);
    if ((rstatus = mainDoArgs(argc, argv)) != 0) MainExit(rstatus);
    if ((rstatus = mainInitAfterArgs()) != 0) MainExit(rstatus);
    if ((rstatus = mainInitFinal()) != 0) MainExit(rstatus);
    TxDispatch( (FILE *) NULL);
    mainFinished();
}