-
Notifications
You must be signed in to change notification settings - Fork 24
/
files.c
7224 lines (6147 loc) · 222 KB
/
files.c
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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-----------------------------------------------------------------------*/
/* files.c --- file handling routines for xcircuit */
/* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
/*-----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef XC_WIN32
#include <pwd.h>
#endif
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef XC_WIN32
#include <unistd.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#else
#include <winsock2.h>
#endif
#ifdef TCL_WRAPPER
#include <tk.h>
#else
#ifndef XC_WIN32
#include "Xw/TextEdit.h" /* for XwTextCopyBuffer() */
#endif
#endif
/*------------------------------------------------------------------------*/
/* Local includes */
/*------------------------------------------------------------------------*/
#include "colordefs.h"
#include "xcircuit.h"
/*----------------------------------------------------------------------*/
/* Function prototype declarations */
/*----------------------------------------------------------------------*/
#include "prototypes.h"
#ifdef ASG
extern void Route(XCWindowData *, Boolean);
extern int ReadSpice(FILE *);
#endif
/*------------------------------------------------------------------------*/
/* Useful (local) defines */
/*------------------------------------------------------------------------*/
#define OUTPUTWIDTH 80 /* maximum text width of output */
#define S_OBLIQUE 13 /* position of Symbol-Oblique in font array */
/*------------------------------------------------------------------------*/
/* External Variable definitions */
/*------------------------------------------------------------------------*/
#ifdef TCL_WRAPPER
extern Tcl_Interp *xcinterp;
#endif
extern char _STR2[250], _STR[150];
extern Globaldata xobjs;
extern XCWindowData *areawin;
extern fontinfo *fonts;
extern short fontcount;
extern Cursor appcursors[NUM_CURSORS];
extern XtAppContext app;
extern Display *dpy;
extern Window win;
extern short beeper;
extern int number_colors;
extern colorindex *colorlist;
/*------------------------------------------------------*/
/* Global variable definitions */
/*------------------------------------------------------*/
Boolean load_in_progress = False;
char version[20];
/* Structure for remembering what names refer to the same object */
aliasptr aliastop;
/*------------------------------------------------------*/
/* Utility routine---compare version numbers */
/* Given version strings v1 and v2, return -1 if */
/* version v1 < v2, +1 if version v1 > v2, and 0 if */
/* they are equal. */
/*------------------------------------------------------*/
int compare_version(char *v1, char *v2)
{
int vers1, subvers1, vers2, subvers2;
sscanf(v1, "%d.%d", &vers1, &subvers1);
sscanf(v2, "%d.%d", &vers2, &subvers2);
if (vers1 < vers2) return -1;
else if (vers1 > vers2) return 1;
else {
if (subvers1 < subvers2) return -1;
if (subvers1 > subvers2) return 1;
else return 0;
}
}
/*------------------------------------------------------*/
/* Simple utility---get rid of newline character */
/*------------------------------------------------------*/
char *ridnewline(char *sptr)
{
char *tstrp;
for (tstrp = sptr; *tstrp != '\0' && *tstrp != '\n'; tstrp++);
if (*tstrp == '\n') *tstrp = '\0';
return tstrp;
}
/*----------------------------------------------------------------------*/
/* Check if two filenames are equivalent. This requires separately */
/* checking any absolute or relative pathnames in front of the filename */
/* as well as the filename itself. */
/*----------------------------------------------------------------------*/
#define PATHSEP '/'
int filecmp(char *filename1, char *filename2)
{
char *root1, *root2, *path1, *path2, *end1, *end2;
int rval;
struct stat statbuf;
ino_t inode1;
const char *cwdname = ".";
if (filename1 == NULL || filename2 == NULL) return 1;
if (!strcmp(filename1, filename2)) return 0; /* exact match */
root1 = strrchr(filename1, PATHSEP);
root2 = strrchr(filename2, PATHSEP);
if (root1 == NULL) {
path1 = (char *)cwdname;
end1 = NULL;
root1 = filename1;
}
else {
path1 = filename1;
end1 = root1;
root1++;
}
if (root2 == NULL) {
path2 = (char *)cwdname;
end2 = NULL;
root2 = filename2;
}
else {
path2 = filename2;
end2 = root2;
root2++;
}
if (strcmp(root1, root2)) return 1; /* root names don't match */
/* If we got here, one or both filenames specify a directory */
/* path, and the directory paths are different strings. */
/* However, one may be an absolute path and the other a */
/* relative path, so we check the inodes of the paths for */
/* equivalence. Note that the file itself is not assumed to */
/* exist. */
rval = 1;
if (end1 != NULL) *end1 = '\0';
if (stat(path1, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
inode1 = statbuf.st_ino;
if (end2 != NULL) *end2 = '\0';
if (stat(path2, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
if (inode1 == statbuf.st_ino)
rval = 0;
}
if (end2 != NULL) *end2 = PATHSEP;
}
if (end1 != NULL) *end1 = PATHSEP;
return rval;
}
/*--------------------------------------------------------------*/
/* Make sure that a string (object or parameter name) is a */
/* valid PostScript name. We do this by converting illegal */
/* characters to the PostScript \ooo (octal value) form. */
/* */
/* This routine does not consider whether the name might be a */
/* PostScript numeric value. This problem is taken care of by */
/* having the load/save routines prepend '@' to parameters and */
/* a technology namespace to object names. */
/* */
/* If "need_prefix" is TRUE, then prepend "@" to the result */
/* string, unless teststring is a numerical parameter name */
/* (p_...). */
/*--------------------------------------------------------------*/
char *create_valid_psname(char *teststring, Boolean need_prefix)
{
int i, isize, ssize;
static char *optr = NULL;
char *sptr, *pptr;
Boolean prepend = need_prefix;
char illegalchars[] = {'/', '}', '{', ']', '[', ')', '(', '<', '>', ' ', '%'};
/* Check for illegal characters which have syntactical meaning in */
/* PostScript, and the presence of nonprintable characters or */
/* whitespace. */
ssize = strlen(teststring);
isize = ssize;
if (need_prefix && !strncmp(teststring, "p_", 2))
prepend = FALSE;
else
isize++;
for (sptr = teststring; *sptr != '\0'; sptr++) {
if ((!isprint(*sptr)) || isspace(*sptr))
isize += 3;
else {
for (i = 0; i < sizeof(illegalchars); i++) {
if (*sptr == illegalchars[i]) {
isize += 3;
break;
}
}
}
}
if (isize == ssize) return teststring;
isize++;
if (optr == NULL)
optr = (char *)malloc(isize);
else
optr = (char *)realloc(optr, isize);
pptr = optr;
if (prepend) *pptr++ = '@';
for (sptr = teststring; *sptr != '\0'; sptr++) {
if ((!isprint(*sptr)) || isspace(*sptr)) {
sprintf(pptr, "\\%03o", *sptr);
pptr += 4;
}
else {
for (i = 0; i < sizeof(illegalchars); i++) {
if (*sptr == illegalchars[i]) {
sprintf(pptr, "\\%03o", *sptr);
pptr += 4;
break;
}
}
if (i == sizeof(illegalchars))
*pptr++ = *sptr;
}
}
*pptr++ = '\0';
return optr;
}
/*------------------------------------------------------*/
/* Turn a PostScript string with possible backslash */
/* escapes into a normal character string. */
/* */
/* if "spacelegal" is TRUE, we are parsing a PostScript */
/* string in parentheses () where whitespace is legal. */
/* If FALSE, we are parsing a PostScript name where */
/* whitespace is illegal, and any whitespace should be */
/* considered the end of the name. */
/* */
/* "dest" is ASSUMED to be large enough to hold the */
/* result. "dest" is always equal to or smaller than */
/* "src" in length. "size" should be the maximum size */
/* of the string, or 1 less that the allocated memory, */
/* allowing for a final NULL byte to be added. */
/* */
/* The fact that "dest" is always smaller than or equal */
/* to "src" means that parse_ps_string(a, a, ...) is */
/* legal. */
/* */
/* Return 0 if the result is empty, 1 otherwise. */
/*------------------------------------------------------*/
int parse_ps_string(char *src, char *dest, int size, Boolean spacelegal, Boolean strip)
{
char *sptr = src;
char *tptr = dest;
int tmpdig, rval = 0;
/* Strip leading "@", inserted by XCircuit to */
/* prevent conflicts with PostScript reserved */
/* keywords or numbers. */
if (strip && (*sptr == '@')) sptr++;
for (;; sptr++) {
if ((*sptr == '\0') || (isspace(*sptr) && !spacelegal)) {
*tptr = '\0';
break;
}
else {
if (*sptr == '\\') {
sptr++;
if (*sptr >= '0' && *sptr < '8') {
sscanf(sptr, "%3o", &tmpdig);
*tptr++ = (u_char)tmpdig;
sptr += 2;
}
else
*tptr++ = *sptr;
}
else
*tptr++ = *sptr;
rval = 1;
}
if ((int)(tptr - dest) > size) {
Wprintf("Warning: Name \"%s\" in input exceeded buffer length!\n", src);
*tptr = '\0';
return rval;
}
}
return rval;
}
/*------------------------------------------------------*/
/* Free memory allocated to a label string */
/*------------------------------------------------------*/
void freelabel(stringpart *string)
{
stringpart *strptr = string, *tmpptr;
while (strptr != NULL) {
if (strptr->type == TEXT_STRING || strptr->type == PARAM_START)
free(strptr->data.string);
tmpptr = strptr->nextpart;
free(strptr);
strptr = tmpptr;
}
}
/*------------------------------------------------------*/
/* Free memory for a single element */
/*------------------------------------------------------*/
void free_single(genericptr genobj)
{
objinstptr geninst;
oparamptr ops, fops;
if (IS_POLYGON(genobj)) free(((polyptr)(genobj))->points);
else if (IS_LABEL(genobj)) freelabel(((labelptr)(genobj))->string);
else if (IS_GRAPHIC(genobj)) freegraphic((graphicptr)(genobj));
else if (IS_PATH(genobj)) free(((pathptr)(genobj))->plist);
else if (IS_OBJINST(genobj)) {
geninst = (objinstptr)genobj;
ops = geninst->params;
while (ops != NULL) {
/* Don't try to free data from indirect parameters */
/* (That's not true---all data are copied by epsubstitute) */
/* if (find_indirect_param(geninst, ops->key) == NULL) { */
switch(ops->type) {
case XC_STRING:
freelabel(ops->parameter.string);
break;
case XC_EXPR:
free(ops->parameter.expr);
break;
}
/* } */
free(ops->key);
fops = ops;
ops = ops->next;
free(fops);
}
}
free_all_eparams(genobj);
}
/*---------------------------------------------------------*/
/* Reset an object structure by freeing all alloc'd memory */
/*---------------------------------------------------------*/
void reset(objectptr localdata, short mode)
{
/* short i; (jdk) */
if (localdata->polygons != NULL || localdata->labels != NULL)
destroynets(localdata);
localdata->valid = False;
if (localdata->parts > 0) {
genericptr *genobj;
if (mode != SAVE) {
for (genobj = localdata->plist; genobj < localdata->plist
+ localdata->parts; genobj++)
/* (*genobj == NULL) only on library pages */
/* where the instances are kept in the library */
/* definition, and are only referenced on the page. */
if (*genobj != NULL) {
free_single(*genobj);
free(*genobj);
}
}
free(localdata->plist);
removeparams(localdata);
initmem(localdata);
if (mode == DESTROY)
free(localdata->plist);
}
}
/*---------------------------------------------------------*/
void pagereset(short rpage)
{
/* free alloc'd filename */
if (xobjs.pagelist[rpage]->filename != NULL)
free(xobjs.pagelist[rpage]->filename);
xobjs.pagelist[rpage]->filename = (char *)NULL;
if (xobjs.pagelist[rpage]->background.name != NULL)
free(xobjs.pagelist[rpage]->background.name);
xobjs.pagelist[rpage]->background.name = (char *)NULL;
clearselects();
/* New pages pick up their properties from page 0, which can be changed */
/* from the .xcircuitrc file on startup (or loaded from a script). */
/* Thanks to Norman Werner ([email protected]) for */
/* pointing out this more obvious way of doing the reset, and providing */
/* a patch. */
xobjs.pagelist[rpage]->wirewidth = xobjs.pagelist[0]->wirewidth;
xobjs.pagelist[rpage]->orient = xobjs.pagelist[0]->orient;
xobjs.pagelist[rpage]->pmode = xobjs.pagelist[0]->pmode;
xobjs.pagelist[rpage]->outscale = xobjs.pagelist[0]->outscale;
xobjs.pagelist[rpage]->drawingscale.x = xobjs.pagelist[0]->drawingscale.x;
xobjs.pagelist[rpage]->drawingscale.y = xobjs.pagelist[0]->drawingscale.y;
xobjs.pagelist[rpage]->gridspace = xobjs.pagelist[0]->gridspace;
xobjs.pagelist[rpage]->snapspace = xobjs.pagelist[0]->snapspace;
xobjs.pagelist[rpage]->coordstyle = xobjs.pagelist[0]->coordstyle;
xobjs.pagelist[rpage]->margins = xobjs.pagelist[0]->margins;
if (xobjs.pagelist[rpage]->coordstyle == CM) {
xobjs.pagelist[rpage]->pagesize.x = 595;
xobjs.pagelist[rpage]->pagesize.y = 842; /* A4 */
}
else {
xobjs.pagelist[rpage]->pagesize.x = 612; /* letter */
xobjs.pagelist[rpage]->pagesize.y = 792;
}
}
/*---------------------------------------------------------*/
void initmem(objectptr localdata)
{
localdata->parts = 0;
localdata->plist = (genericptr *)malloc(sizeof(genericptr));
localdata->hidden = False;
localdata->changes = 0;
localdata->params = NULL;
localdata->viewscale = 0.5;
/* Object should not reference the window: this needs to be rethunk! */
if (areawin != NULL) {
localdata->pcorner.x = -areawin->width;
localdata->pcorner.y = -areawin->height;
}
localdata->bbox.width = 0;
localdata->bbox.height = 0;
localdata->bbox.lowerleft.x = 0;
localdata->bbox.lowerleft.y = 0;
localdata->highlight.netlist = NULL;
localdata->highlight.thisinst = NULL;
localdata->schemtype = PRIMARY;
localdata->symschem = NULL;
localdata->netnames = NULL;
localdata->polygons = NULL;
localdata->labels = NULL;
localdata->ports = NULL;
localdata->calls = NULL;
localdata->valid = False;
localdata->infolabels = False;
localdata->traversed = False;
}
/*--------------------------------------------------------------*/
/* Exhaustively compare the contents of two objects and return */
/* true if equivalent, false if not. */
/*--------------------------------------------------------------*/
Boolean elemcompare(genericptr *compgen, genericptr *gchk)
{
Boolean bres;
switch(ELEMENTTYPE(*compgen)) {
case(ARC):
bres = (TOARC(compgen)->position.x == TOARC(gchk)->position.x &&
TOARC(compgen)->position.y == TOARC(gchk)->position.y &&
TOARC(compgen)->style == TOARC(gchk)->style &&
TOARC(compgen)->width == TOARC(gchk)->width &&
abs(TOARC(compgen)->radius) == abs(TOARC(gchk)->radius) &&
TOARC(compgen)->yaxis == TOARC(gchk)->yaxis &&
TOARC(compgen)->angle1 == TOARC(gchk)->angle1 &&
TOARC(compgen)->angle2 == TOARC(gchk)->angle2);
break;
case(SPLINE):
bres = (TOSPLINE(compgen)->style == TOSPLINE(gchk)->style &&
TOSPLINE(compgen)->width == TOSPLINE(gchk)->width &&
TOSPLINE(compgen)->ctrl[0].x == TOSPLINE(gchk)->ctrl[0].x &&
TOSPLINE(compgen)->ctrl[0].y == TOSPLINE(gchk)->ctrl[0].y &&
TOSPLINE(compgen)->ctrl[1].x == TOSPLINE(gchk)->ctrl[1].x &&
TOSPLINE(compgen)->ctrl[1].y == TOSPLINE(gchk)->ctrl[1].y &&
TOSPLINE(compgen)->ctrl[2].x == TOSPLINE(gchk)->ctrl[2].x &&
TOSPLINE(compgen)->ctrl[2].y == TOSPLINE(gchk)->ctrl[2].y &&
TOSPLINE(compgen)->ctrl[3].x == TOSPLINE(gchk)->ctrl[3].x &&
TOSPLINE(compgen)->ctrl[3].y == TOSPLINE(gchk)->ctrl[3].y);
break;
case(POLYGON): {
int i;
if (TOPOLY(compgen)->style == TOPOLY(gchk)->style &&
TOPOLY(compgen)->width == TOPOLY(gchk)->width &&
TOPOLY(compgen)->number == TOPOLY(gchk)->number) {
for (i = 0; i < TOPOLY(compgen)->number; i++) {
if (TOPOLY(compgen)->points[i].x != TOPOLY(gchk)->points[i].x
|| TOPOLY(compgen)->points[i].y != TOPOLY(gchk)->points[i].y)
break;
}
bres = (i == TOPOLY(compgen)->number);
}
else bres = False;
}break;
}
return bres;
}
/*--------------------------------------------------------------*/
/* Compare any element with any other element. */
/*--------------------------------------------------------------*/
Boolean compare_single(genericptr *compgen, genericptr *gchk)
{
Boolean bres = False;
if ((*gchk)->type == (*compgen)->type) {
switch(ELEMENTTYPE(*compgen)) {
case(OBJINST):{
objinst *newobj = TOOBJINST(compgen);
objinst *oldobj = TOOBJINST(gchk);
bres = (newobj->position.x == oldobj->position.x &&
newobj->position.y == oldobj->position.y &&
newobj->rotation == oldobj->rotation &&
newobj->scale == oldobj->scale &&
newobj->style == oldobj->style &&
newobj->thisobject == oldobj->thisobject);
} break;
case(LABEL):
bres = (TOLABEL(compgen)->position.x == TOLABEL(gchk)->position.x &&
TOLABEL(compgen)->position.y == TOLABEL(gchk)->position.y &&
TOLABEL(compgen)->rotation == TOLABEL(gchk)->rotation &&
TOLABEL(compgen)->scale == TOLABEL(gchk)->scale &&
TOLABEL(compgen)->anchor == TOLABEL(gchk)->anchor &&
TOLABEL(compgen)->pin == TOLABEL(gchk)->pin &&
!stringcomp(TOLABEL(compgen)->string, TOLABEL(gchk)->string));
break;
case(PATH): /* elements *must* be in same order for a path */
bres = (TOPATH(compgen)->parts == TOPATH(gchk)->parts &&
TOPATH(compgen)->style == TOPATH(gchk)->style &&
TOPATH(compgen)->width == TOPATH(gchk)->width);
if (bres) {
genericptr *pathchk, *gpath;
for (pathchk = TOPATH(compgen)->plist, gpath =
TOPATH(gchk)->plist; pathchk < TOPATH(compgen)->plist
+ TOPATH(compgen)->parts; pathchk++, gpath++) {
if (!elemcompare(pathchk, gpath)) bres = False;
}
}
break;
case(ARC): case(SPLINE): case(POLYGON):
bres = elemcompare(compgen, gchk);
break;
}
}
return bres;
}
/*--------------------------------------------------------------------*/
short objcompare(objectptr obja, objectptr objb)
{
genericptr *compgen, *glist, *gchk, *remg;
short csize;
Boolean bres;
/* quick check on equivalence of number of objects */
if (obja->parts != objb->parts) return False;
/* check equivalence of parameters. Parameters need not be in any */
/* order; they must only match by key and value. */
if (obja->params == NULL && objb->params != NULL) return False;
else if (obja->params != NULL && objb->params == NULL) return False;
else if (obja->params != NULL || objb->params != NULL) {
oparamptr opsa, opsb;
for (opsa = obja->params; opsa != NULL; opsa = opsa->next) {
opsb = match_param(objb, opsa->key);
if (opsb == NULL) return False;
else if (opsa->type != opsb->type) return False;
switch (opsa->type) {
case XC_STRING:
if (stringcomp(opsa->parameter.string, opsb->parameter.string))
return False;
break;
case XC_EXPR:
if (strcmp(opsa->parameter.expr, opsb->parameter.expr))
return False;
break;
case XC_INT: case XC_FLOAT:
if (opsa->parameter.ivalue != opsb->parameter.ivalue)
return False;
break;
}
}
}
/* For the exhaustive check we must match component for component. */
/* Best not to assume that elements are in same order for both. */
csize = obja->parts;
glist = (genericptr *)malloc(csize * sizeof(genericptr));
for (compgen = objb->plist; compgen < objb->plist + csize; compgen++)
(*(glist + (int)(compgen - objb->plist))) = *compgen;
for (compgen = obja->plist; compgen < obja->plist + obja->parts;
compgen++) {
bres = False;
for (gchk = glist; gchk < glist + csize; gchk++) {
if ((*compgen)->color == (*gchk)->color)
bres = compare_single(compgen, gchk);
if (bres) {
csize--;
for (remg = gchk; remg < glist + csize; remg++)
*remg = *(remg + 1);
break;
}
}
}
free(glist);
if (csize != 0) return False;
/* Both objects cannot attempt to set an associated schematic/symbol to */
/* separate objects, although it is okay for one to make the association */
/* and the other not to. */
if (obja->symschem != NULL && objb->symschem != NULL)
if (obja->symschem != objb->symschem)
return False;
return(True);
}
/*------------------------*/
/* scale renormalization */
/*------------------------*/
float getpsscale(float value, short page)
{
if (xobjs.pagelist[page]->coordstyle != CM)
return (value * INCHSCALE);
else
return (value * CMSCALE);
}
/*---------------------------------------------------------------*/
/* Keep track of columns of output and split lines when too long */
/*---------------------------------------------------------------*/
void dostcount(FILE *ps, short *count, short addlength)
{
*count += addlength;
if (*count > OUTPUTWIDTH) {
*count = addlength;
fprintf(ps, "\n");
}
}
/*----------------------------------------------------------------------*/
/* Write a numerical value as a string to _STR, making a parameter */
/* substitution if appropriate. */
/* Return 1 if a parameter substitution was made, 0 if not. */
/*----------------------------------------------------------------------*/
Boolean varpcheck(FILE *ps, short value, objectptr localdata, int pointno,
short *stptr, genericptr thiselem, u_char which)
{
oparamptr ops;
eparamptr epp;
Boolean done = False;
for (epp = thiselem->passed; epp != NULL; epp = epp->next) {
if ((epp->pdata.pointno != -1) && (epp->pdata.pointno != pointno)) continue;
ops = match_param(localdata, epp->key);
if (ops != NULL && (ops->which == which)) {
sprintf(_STR, "%s ", epp->key);
done = True;
break;
}
}
if (!done) {
if (pointno == -1) return done;
sprintf(_STR, "%d ", (int)value);
}
else if ((epp->pdata.pointno == -1) && (pointno >= 0)) {
sprintf(_STR, "%d ", (int)value - ops->parameter.ivalue);
}
dostcount (ps, stptr, strlen(_STR));
fputs(_STR, ps);
return done;
}
/*----------------------------------------------------------------------*/
/* like varpcheck(), but without pointnumber */
/*----------------------------------------------------------------------*/
void varcheck(FILE *ps, short value, objectptr localdata,
short *stptr, genericptr thiselem, u_char which)
{
varpcheck(ps, value, localdata, 0, stptr, thiselem, which);
}
/*----------------------------------------------------------------------*/
/* like varcheck(), but for floating-point values */
/*----------------------------------------------------------------------*/
void varfcheck(FILE *ps, float value, objectptr localdata, short *stptr,
genericptr thiselem, u_char which)
{
oparamptr ops;
eparamptr epp;
Boolean done = False;
for (epp = thiselem->passed; epp != NULL; epp = epp->next) {
ops = match_param(localdata, epp->key);
if (ops != NULL && (ops->which == which)) {
sprintf(_STR, "%s ", epp->key);
done = True;
break;
}
}
if (!done)
sprintf(_STR, "%3.3f ", value);
dostcount (ps, stptr, strlen(_STR));
fputs(_STR, ps);
}
/*----------------------------------------------------------------------*/
/* Like varpcheck(), for path types only. */
/*----------------------------------------------------------------------*/
Boolean varpathcheck(FILE *ps, short value, objectptr localdata, int pointno,
short *stptr, genericptr *thiselem, pathptr thispath, u_char which)
{
oparamptr ops;
eparamptr epp;
Boolean done = False;
for (epp = thispath->passed; epp != NULL; epp = epp->next) {
if ((epp->pdata.pathpt[0] != -1) && (epp->pdata.pathpt[1] != pointno)) continue;
if ((epp->pdata.pathpt[0] != -1) && (epp->pdata.pathpt[0] !=
(short)(thiselem - thispath->plist))) continue;
ops = match_param(localdata, epp->key);
if (ops != NULL && (ops->which == which)) {
sprintf(_STR, "%s ", epp->key);
done = True;
break;
}
}
if (!done) {
if (pointno == -1) return done;
sprintf(_STR, "%d ", (int)value);
}
else if ((epp->pdata.pathpt[0] == -1) && (pointno >= 0)) {
sprintf(_STR, "%d ", (int)value - ops->parameter.ivalue);
}
dostcount (ps, stptr, strlen(_STR));
fputs(_STR, ps);
return done;
}
/* Structure used to hold data specific to each load mode. See */
/* xcircuit.h for the list of load modes (enum loadmodes) */
typedef struct _loaddata {
void (*func)(); /* Routine to run to load the file */
char *prompt; /* Substring name of action, for prompting */
char *filext; /* Default extention of file to load */
} loaddata;
/*-------------------------------------------------------*/
/* Load a PostScript or Python (interpreter script) file */
/*-------------------------------------------------------*/
void getfile(xcWidget button, pointertype mode, caddr_t nulldata)
{
static loaddata loadmodes[LOAD_MODES] = {
{normalloadfile, "load", "ps"}, /* mode NORMAL */
{importfile, "import", "ps"}, /* mode IMPORT */
{loadbackground, "render", "ps"}, /* mode PSBKGROUND */
#ifdef HAVE_PYTHON
{execscript, "execute", "py"},
#else
{execscript, "execute", ""}, /* mode SCRIPT */
#endif
{crashrecover, "recover", "ps"}, /* mode RECOVER */
#ifdef ASG
{importspice, "import", "spice"}, /* mode IMPORTSPICE */
#endif
#ifdef HAVE_CAIRO
{importgraphic, "import", "ppm"}, /* mode IMPORTGRAPHIC */
#endif
};
buttonsave *savebutton = NULL;
char *promptstr = NULL;
/* char strext[10]; (jdk) */
int idx = (int)mode;
if (is_page(topobject) == -1) {
Wprintf("Can only read file into top-level page!");
return;
}
else if (idx >= LOAD_MODES) {
Wprintf("Unknown mode passed to routine getfile()\n");
return;
}
#ifndef TCL_WRAPPER
savebutton = getgeneric(button, getfile, (void *)mode);
#endif
if (idx == RECOVER) {
char *cfile = getcrashfilename();
promptstr = (char *)malloc(18 + ((cfile == NULL) ? 9 : strlen(cfile)));
sprintf(promptstr, "Recover file \'%s\'?", (cfile == NULL) ? "(unknown)" : cfile);
popupprompt(button, promptstr, NULL, loadmodes[idx].func, savebutton, NULL);
if (cfile) free(cfile);
}
else {
promptstr = (char *)malloc(18 + strlen(loadmodes[idx].prompt));
sprintf(promptstr, "Select file to %s:", loadmodes[idx].prompt);
popupprompt(button, promptstr, "\0", loadmodes[idx].func,
savebutton, loadmodes[idx].filext);
}
free(promptstr);
}
/*--------------------------------------------------------------*/
/* Tilde ('~') expansion in file name. Assumes that filename */
/* is a static character array of size "nchars". */
/*--------------------------------------------------------------*/
Boolean xc_tilde_expand(char *filename, int nchars)
{
#ifndef _MSC_VER
struct passwd *passwd;
char *username = NULL, *expanded, *sptr;
if (*filename == '~') {
sptr = filename + 1;
if (*sptr == '/' || *sptr == ' ' || *sptr == '\0')
username = getenv("HOME");
else {
for (; *sptr != '/' && *sptr != '\0'; sptr++);
if (*sptr == '\0') *(sptr + 1) = '\0';
*sptr = '\0';
passwd = getpwnam(filename + 1);
if (passwd != NULL)
username = passwd->pw_dir;
*sptr = '/';
}
if (username != NULL) {
expanded = (char *)malloc(strlen(username) +
strlen(filename));
strcpy(expanded, username);
strcat(expanded, sptr);
strncpy(filename, expanded, nchars);
free(expanded);
}
return True;
}
return False;
#else
return False;
#endif
}
/*--------------------------------------------------------------*/
/* Variable ('$') expansion in file name */
/*--------------------------------------------------------------*/
Boolean xc_variable_expand(char *filename, int nchars)
{
char *expanded, *sptr, tmpchar, *varpos, *varsub;
if ((varpos = strchr(filename, '$')) != NULL) {
for (sptr = varpos; *sptr != '/' && *sptr != '\0'; sptr++);
if (*sptr == '\0') *(sptr + 1) = '\0';
tmpchar = *sptr;
*sptr = '\0';
#ifdef TCL_WRAPPER
/* Interpret as a Tcl variable */
varsub = (char *)Tcl_GetVar(xcinterp, varpos + 1, TCL_NAMESPACE_ONLY);
#else
/* Interpret as an environment variable */
varsub = (char *)getenv((const char *)(varpos + 1));
#endif
if (varsub != NULL) {
*varpos = '\0';
expanded = (char *)malloc(strlen(varsub) + strlen(filename) +
strlen(sptr + 1) + 2);
strcpy(expanded, filename);
strcat(expanded, varsub);
*sptr = tmpchar;
strcat(expanded, sptr);
strncpy(filename, expanded, nchars);
free(expanded);
}
else
*sptr = tmpchar;
return True;
}
return False;
}
/*--------------------------------------------------------------*/
/* Attempt to find a file and open it. */
/*--------------------------------------------------------------*/
FILE *fileopen(char *filename, char *suffix, char *name_return, int nchars)
{
FILE *file = NULL;
char inname[250], expname[250], *sptr, *cptr, *iptr, *froot;
int slen;
sscanf(filename, "%249s", expname);
xc_tilde_expand(expname, 249);
while (xc_variable_expand(expname, 249));
sptr = xobjs.filesearchpath;
while (1) {
if ((xobjs.filesearchpath == NULL) || (expname[0] == '/')) {
strcpy(inname, expname);
iptr = inname;
}
else {
strcpy(inname, sptr);
cptr = strchr(sptr, ':');
slen = (cptr == NULL) ? strlen(sptr) : (int)(cptr - sptr);
sptr += (slen + ((cptr == NULL) ? 0 : 1));
iptr = inname + slen;
if (*(iptr - 1) != '/') strcpy(iptr++, "/");
strcpy(iptr, expname);
}
/* Attempt to open the filename with a suffix */