-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsymbols.c
3404 lines (3126 loc) · 79.3 KB
/
symbols.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
/*
**++
** FACILITY: MMK
**
** ABSTRACT: Symbol substitution routines
**
** MODULE DESCRIPTION:
**
** This module contains routines that deal with the
** MMK global and local symbol lists.
**
** AUTHOR: M. Madison
**
** Copyright (c) 2008, Matthew Madison.
** Copyright (c) 2014, Endless Software Solutions.
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** * Redistributions of source code must retain the above
** copyright notice, this list of conditions and the following
** disclaimer.
** * Redistributions in binary form must reproduce the above
** copyright notice, this list of conditions and the following
** disclaimer in the documentation and/or other materials provided
** with the distribution.
** * Neither the name of the copyright owner nor the names of any
** other contributors may be used to endorse or promote products
** derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "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 THE COPYRIGHT
** OWNER OR CONTRIBUTORS 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.
**
** CREATION DATE: 21-AUG-1998
**
** MODIFICATION HISTORY:
**
** 21-AUG-1992 V1.0 Madison Initial coding.
** 01-SEP-1992 V1.1 Madison Comments.
** 29-SEP-1992 V1.2 Madison Separate out command-line symbol defs.
** 14-OCT-1992 V1.2-1 Madison Support use of DCL symbols.
** 02-JUN-1993 V1.3 Madison Support use of $*, $<, etc.
** 07-JUN-1993 V1.4 Madison Add MMS$CHANGED_LIST.
** 16-SEP-1993 V1.4-1 Madison Fix make symbol substitution list.
** 17-OCT-1993 V1.5 Madison Allow $(var:subst-rule) notation.
** 17-OCT-1993 V1.6 Madison Symbol setup for libfile deletion.
** 28-OCT-1993 V1.6-1 Madison Fix redefinition of cmdline symbols.
** 02-DEC-1993 V1.6-2 Madison Allow symbol refs inside symbol refs.
** 12-DEC-1993 V1.7 Madison Support for $(MMS) macro.
** 02-MAR-1994 V1.7-1 Madison Fix non-resolvable specials.
** 14-APR-1994 V1.7-2 Madison Fix MMS$CHANGED_LIST creation.
** 01-JUL-1994 V1.8 Madison CMS support.
** 22-JUL-1994 V1.9 Madison Add MMS$TARGET_FNAME.
** 17-AUG-1994 V1.9-1 Madison Fix Define_Symbol for "MMS" override.
** 22-AUG-1994 V1.9-2 Madison Remove KILL_INTERMEDIATES stuff.
** 02-DEC-1994 V2.0 Madison Add default CMS generation stuff.
** 21-JUN-1995 V2.1 Madison Revamp for MMS parity.
** 03-OCT-1995 V2.1-1 Madison Fix handling of nested symbol refs.
** 29-MAY-1996 V2.1-2 Madison Add MMS$SOURCE_FNAME special symbol.
** 23-MAR-1997 V2.1-3 Madison Fix $(sym:sfx1=sfx) resolution.
** 27-DEC-1998 V2.2 Madison Support version macros; general cleanup.
** 29-JAN-2004 V2.2-1 Madison Don't define MMS$CMS_LIBRARY as "CMS$LIB".
** 07-APR-2010 V2.3 Sneddon Added append flag to Define_Symbol plus
** temporary symbol support in preparation
** for builtin function support.
** 02-JUL-2012 V2.4 Sneddon Change to find_char arguments.
** 25-JUL-2012 V3.0 Sneddon Add builtin function support.
** 28-AUG-2012 V3.0-1 Sneddon Add ERROR, INFO, WARN, WORDLIST.
** 29-AUG-2012 V3.0-2 Sneddon Improve WORD and WORDS range checking.
** Add FIRSTWORD and LASTWORD. WARNING
** alias for WARN. All builtin file
** operations.
** 31-AUG-2012 V3.1 Sneddon Add support for builtins that handle
** their own argument resolution.
** 04-SEP-2012 V3.2 Sneddon Add OR, AND and IF.
** 07-SEP-2012 V3.3 Sneddon Add CALL, reorganise temporary symbols.
** 27-SEP-2012 V3.3-2 Sneddon Add FOREACH.
** 08-OCT-2012 V3.3-3 Sneddon Add FINDSTRING, FILTER, FILTER-OUT,
** STRIP, COLLAPSE.
** 24-OCT-2012 V3.3-4 Sneddon Add JOIN.
** 25-OCT-2012 V3.3-5 Sneddon Add ADDPREFIX, ADDSUFFIX.
** 26-OCT-2012 V3.3-6 Sneddon Add SORT
** 12-NOV-2012 V3.3-7 Sneddon Add PATSUBST.
** 30-JAN-2013 V3.3-8 Sneddon Add SUBST.
** 05-FEB-2013 V3.3-9 Sneddon Final touches to builtin support.
** 20-FEB-2013 V3.3-10 Sneddon Fix issue #25 related to FILEVERSION.
** Fix issue #27 related to built in
** functions. Fix issue #29, FINDSTRING.
** Fixed IF argument mask.
** 21-FEB-2013 V3.3-11 Sneddon Fix calls to Resolve_Symbols by
** builtin handlers. Builtin handlers
** now receive call-specific argument
** stack. Fixes issue #31. Fix symbol
** types, issue #33.
** 22-FEB-2013 V3.3-12 Sneddon Fix WORDS, issue #39.
** 06-JAN-2014 V3.4 Sneddon Reviewed whitespace processing of
** all builtins.
** 31-MAY-2014 V3.4-1 Sneddon Further whitespace processing review.
** 12-JUN-2014 V3.5 Sneddon Add set_mmssuffixes.
** 13-JUN-2014 V3.6 Sneddon Changed how append works for the
** routine Define_Symbol.
**--
*/
#pragma module SYMBOLS "V3.6"
#include "mmk.h"
#include "globals.h"
#include <builtins.h>
#include <libvmdef.h>
#include <stdarg.h>
#include <string.h>
#include <strdef.h>
#include <ots$routines.h>
#include <str$routines.h>
#include <ctype.h>
#include <rms.h>
#define ARGMAX (sizeof(int) * 8)
/*
** Builtin function descriptor
*/
struct FUNCTION {
char *name;
int vararg, maxarg, mask;
int (*handler)(int, struct dsc$descriptor *, char **, int *);
};
/*
** Tree node for LIB$xxx_TREE
*/
struct LEAF {
void *left, *right;
int reserved;
struct dsc$descriptor str;
};
const static int LEAF_S_LEAFDEF = sizeof(struct LEAF);
/*
** Forward declarations
*/
struct SYMBOL *Lookup_Symbol(char *);
void Define_Symbol(SYMTYPE, char *, char *, int, ...);
int Resolve_Symbols(char *, int, char **, int *, int);
void Clear_Local_Symbols(void);
void Create_Local_Symbols(struct DEPEND *, struct OBJREF *, struct QUE *);
void set_mmssuffixes(void);
static void apply_subst_rule(char *, char *, char **, int *);
static void apply_full_subst_rule(char *, char *, char **, int *);
static char *apply_builtin (char *, char *, int, char **, int *, int *,
int);
static int apply_addsuffix(int, struct dsc$descriptor *, char **, int *);
static int apply_addprefix(int, struct dsc$descriptor *, char **, int *);
static int apply_and(int, struct dsc$descriptor *, char **, int *);
static int apply_basename(int, struct dsc$descriptor *, char **, int*);
static int apply_call(int, struct dsc$descriptor *, char **, int *);
static int apply_collapse(int, struct dsc$descriptor *, char **, int *);
static int apply_dir(int, struct dsc$descriptor *, char **, int *);
static int apply_directory(int, struct dsc$descriptor *, char **, int *);
static int apply_error(int, struct dsc$descriptor *, char **, int *);
static int apply_filename(int, struct dsc$descriptor *, char **, int *);
static int apply_filetype(int, struct dsc$descriptor *, char **, int *);
static int apply_fileversion(int, struct dsc$descriptor *, char **, int *);
static int apply_filter(int, struct dsc$descriptor *, char **, int *);
static int apply_filter_out(int, struct dsc$descriptor *, char **, int *);
static int apply_findstring(int, struct dsc$descriptor *, char **, int *);
static int apply_firstword(int, struct dsc$descriptor *, char **, int *);
static int apply_foreach(int, struct dsc$descriptor *, char **, int *);
static int apply_if(int, struct dsc$descriptor *, char **, int *);
static int apply_info(int, struct dsc$descriptor *, char **, int *);
static int apply_join(int, struct dsc$descriptor *, char **, int *);
static int apply_lastword(int, struct dsc$descriptor *, char **, int *);
static int apply_notdir(int, struct dsc$descriptor *, char **, int *);
static int apply_or(int, struct dsc$descriptor *, char **, int *);
static int apply_origin(int, struct dsc$descriptor *, char **, int *);
static int apply_patsubst(int, struct dsc$descriptor *, char **, int *);
static int apply_sort(int, struct dsc$descriptor *, char **, int *);
static int apply_sort_cmp(struct dsc$descriptor *, struct LEAF *, void *);
static int apply_sort_malloc(struct dsc$descriptor *, struct LEAF **, int);
static int apply_sort_cat(struct LEAF *, char **);
static int apply_strip(int, struct dsc$descriptor *, char **, int *);
static int apply_subst(int, struct dsc$descriptor *, char **, int *);
static int apply_warn(int, struct dsc$descriptor *, char **, int *);
static int apply_wildcard(int, struct dsc$descriptor *, char **, int *);
static int apply_word(int, struct dsc$descriptor *, char **, int *);
static int apply_wordlist(int, struct dsc$descriptor *, char **, int *);
static int apply_words(int, struct dsc$descriptor *, char **, int *);
/*
** Own storage
*/
static struct SYMTABLE dcl_symbols;
static int dcl_symbols_inited = 0;
static char *WHITESPACE = " \r\n\t\v\f";
static char SPECIALS[] = "@*<+?%&";
static char *SPECIAL_VAR[] = {"MMS$TARGET","MMS$TARGET_NAME",
"MMS$SOURCE","MMS$SOURCE_LIST",
"MMS$CHANGED_LIST","MMS$LIB_ELEMENT",
"MMS$CMS_GEN"};
static char *non_resolvables[] = {"MMS","MMSQUALIFIERS",
"MMS$TARGET","MMS$TARGET_NAME","MMS$TARGET_MODULE","MMS$LIB_ELEMENT",
"MMS$SOURCE","MMS$SOURCE_LIST","MMS$CHANGED_LIST",
"MMS$SOURCE_NAME","MMS$SOURCE_LIST_SPACES","MMS$CHANGED_LIST_SPACES",
"MMS$CMS_LIBRARY", "MMS$CMS_ELEMENT", "MMS$CMS_GEN",
"MMS$TARGET_FNAME","MMS$SOURCE_FNAME"};
static struct FUNCTION functions[] = {
{ "ADDPREFIX", 0, 2, 0x00000000, apply_addprefix, },
{ "ADDSUFFIX", 0, 2, 0x00000000, apply_addsuffix, },
{ "AND", 1, 1, 0xFFFFFFFF, apply_and, },
{ "BASENAME", 0, 1, 0x00000000, apply_basename, },
{ "CALL", 1, 1, 0x00000000, apply_call, },
{ "COLLAPSE", 0, 1, 0x00000000, apply_collapse, },
{ "DIR", 0, 1, 0x00000000, apply_dir, },
{ "DIRECTORY", 0, 1, 0x00000000, apply_directory, },
{ "ERROR", 1, 1, 0x00000000, apply_error, },
{ "FILENAME", 0, 1, 0x00000000, apply_filename, },
{ "FILETYPE", 0, 1, 0x00000000, apply_filetype, },
{ "FILEVERSION", 0, 1, 0x00000000, apply_fileversion, },
{ "FILTER", 0, 2, 0x00000000, apply_filter, },
{ "FILTER-OUT", 0, 2, 0x00000000, apply_filter_out, },
{ "FINDSTRING", 0, 2, 0x00000000, apply_findstring, },
{ "FIRSTWORD", 0, 1, 0x00000000, apply_firstword, },
{ "FOREACH", 0, 3, 0x00000004, apply_foreach, },
{ "JOIN", 0, 2, 0x00000000, apply_join, },
{ "IF", 1, 2, 0x00000006, apply_if, },
{ "INFO", 1, 1, 0x00000000, apply_info, },
{ "LASTWORD", 0, 1, 0x00000000, apply_lastword, },
{ "NOTDIR", 0, 1, 0x00000000, apply_notdir, },
{ "OR", 1, 1, 0xFFFFFFFF, apply_or, },
{ "ORIGIN", 0, 1, 0x00000000, apply_origin, },
{ "PATSUBST", 0, 3, 0x00000000, apply_patsubst, },
{ "SORT", 0, 1, 0x00000000, apply_sort, },
{ "STRIP", 0, 1, 0x00000000, apply_strip, },
{ "SUBST", 0, 3, 0x00000000, apply_subst, },
{ "WARN", 1, 1, 0x00000000, apply_warn, },
{ "WARNING", 1, 1, 0x00000000, apply_warn, },
{ "WILDCARD", 0, 1, 0x00000000, apply_wildcard, },
{ "WORD", 0, 2, 0x00000000, apply_word, },
{ "WORDLIST", 0, 3, 0x00000000, apply_wordlist, },
{ "WORDS", 0, 1, 0x00000000, apply_words, }, };
/*
**++
** ROUTINE: Lookup_Symbol
**
** FUNCTIONAL DESCRIPTION:
**
** Locates a symbol by name.
**
** RETURNS: pointer to struct SYMBOL
**
** PROTOTYPE:
**
** Lookup_Symbol(char *name)
**
** IMPLICIT INPUTS: all symbol tables
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** non-0: symbol found
** 0: symbol not found
**
** SIDE EFFECTS: None.
**
**--
*/
struct SYMBOL *Lookup_Symbol (char *name) {
struct SYMBOL *sym;
struct QUE *symq;
unsigned char *cp;
struct dsc$descriptor namdsc, valdsc;
unsigned int status, hash_value;
int i;
static struct SYMTABLE *normal_order[] = {
&local_symbols, &cmdline_symbols, &global_symbols,
&builtin_symbols, &dcl_symbols};
struct SYMTABLE *override_order[] = {
&local_symbols, &cmdline_symbols,
&dcl_symbols, &global_symbols, &builtin_symbols};
if (!dcl_symbols_inited) {
for (i = 0; i < MMK_K_SYMTABLE_SIZE; i++) {
INIT_QUEUE(dcl_symbols.symlist[i]);
}
dcl_symbols_inited = 1;
}
hash_value = 0;
for (cp = (unsigned char *) name, i = 0; *cp != '\0' && i < 4; cp++, i++) {
hash_value |= *cp;
}
hash_value &= 0xff;
if (temporary_symbols != 0) {
symq = &temporary_symbols->symlist[hash_value];
for (sym = symq->head; sym != (struct SYMBOL *) symq; sym = sym->flink) {
if (strcmp(name, sym->name) == 0) return sym;
}
}
for (i = 0; i < sizeof(normal_order)/sizeof(normal_order[0]); i++) {
symq = symbol_override ? &override_order[i]->symlist[hash_value]
: &normal_order[i]->symlist[hash_value];
for (sym = symq->head; sym != (struct SYMBOL *) symq; sym = sym->flink) {
if (strcmp(name, sym->name) == 0) return sym;
}
if ((symbol_override && override_order[i] == &dcl_symbols) ||
(!symbol_override && normal_order[i] == &dcl_symbols)) {
INIT_SDESC(namdsc, strlen(name), name);
INIT_DYNDESC(valdsc);
status = lib$get_symbol(&namdsc, &valdsc);
if (OK(status)) {
sym = mem_get_symbol();
strcpy(sym->name, name);
sym->type = MMK_K_SYM_CLI;
sym->value = malloc(valdsc.dsc$w_length+1);
memcpy(sym->value, valdsc.dsc$a_pointer, valdsc.dsc$w_length);
sym->value[valdsc.dsc$w_length] = '\0';
queue_insert(sym, dcl_symbols.symlist[hash_value].tail);
str$free1_dx(&valdsc);
return sym;
}
}
}
return (struct SYMBOL *) 0;
} /* Lookup_Symbol */
/*
**++
** ROUTINE: Define_Symbol
**
** FUNCTIONAL DESCRIPTION:
**
** Creates or re-defines a symbol in the global symbol
** table.
**
** The fifth (and optional) argument controls whether val is appended
** to the existing symbol value or not. If present, and non-zero, the
** fifth argument is a pointer to a separator string (use the string ""
** for no separator). A 0 pointer indicates that val should replace
** the symbols value.
**
** RETURNS: void
**
** PROTOTYPE:
**
** Define_Symbol(SYMTYPE symtype, char *name, char *val, int vallen, ...)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: global_symbols
**
**--
*/
void Define_Symbol (SYMTYPE symtype, char *name, char *val, int vallen, ...) {
struct SYMBOL *sym;
struct QUE *symq;
char *sep = 0, upname[MMK_S_SYMBOL+1];
unsigned char *cp;
unsigned int hash_value;
int actualcount, i;
va_list ap;
va_count(actualcount);
if (actualcount > 4) {
va_start(ap, vallen);
sep = va_arg(ap, char *);
va_end(ap);
}
strcpy(upname, name);
upcase(upname);
hash_value = 0;
for (cp = (unsigned char *) upname, i = 0; *cp != '\0' && i < 4; cp++, i++) {
hash_value |= *cp;
}
hash_value &= 0xff;
switch (symtype) {
case MMK_K_SYM_LOCAL:
symq = &local_symbols.symlist[hash_value];
break;
case MMK_K_SYM_DESCRIP:
symq = &global_symbols.symlist[hash_value];
break;
case MMK_K_SYM_CMDLINE:
symq = &cmdline_symbols.symlist[hash_value];
break;
case MMK_K_SYM_BUILTIN:
symq = &builtin_symbols.symlist[hash_value];
break;
case MMK_K_SYM_TEMPORARY:
symq = &temporary_symbols->symlist[hash_value];
break;
default:
symq = 0; /* this will cause an ACCVIO - should never happen */
break;
}
for (sym = symq->head; sym != (struct SYMBOL *) symq; sym = sym->flink) {
if (strcmp(upname, sym->name) == 0) break;
}
if (sym == (struct SYMBOL *) symq) {
sym = mem_get_symbol();
strcpy(sym->name, upname);
queue_insert(sym, symq->tail);
} else {
if (sep == 0) {
free(sym->value);
sym->value = 0;
}
}
sym->type = symtype;
if (vallen < 0) vallen = strlen(val);
if (sym->value) {
vallen += strlen(sym->value) + strlen(sep);
sym->value = realloc(sym->value, vallen+1);
if (sep != 0) strcat(sym->value, sep);
} else {
sym->value = malloc(vallen+1);
sym->value[0] = '\0';
}
strncat(sym->value, val, vallen);
} /* Define_Symbol */
/*
**++
** ROUTINE: Resolve_Symbols
**
** FUNCTIONAL DESCRIPTION:
**
** Performs symbol substitution in a string. Iterates until no
** further substitutions are performed.
**
** Symbol references appear as $(symbol-name) in an MMS description line.
**
** RETURNS: void
**
** PROTOTYPE:
**
** Resolve_Symbols(char *in, int inlen, char **out, int *outlen,
** int dont_resolve_unknowns, ...)
**
** The output string is allocated by this procedure using malloc
** and should be freed by the caller when done.
**
** Optional arguments are used internally by Resolve_Symbol when calling
** itself. They are not intended for general use.
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
int Resolve_Symbols (char *in, int inlen, char **out, int *outlen,
int dont_resolve_unknowns) {
char *cp, *inend, *dp, *pp, *tmp, *val, *colp;
struct SYMBOL *valsym;
char var[MMK_S_SYMBOL+1];
int len, curlen, tmplen, first, did_one = 0, free_val, i;
int actualcount, resolved_MMS_macro;
first = 1;
resolved_MMS_macro = 0;
do {
did_one = 0;
tmp = malloc(tmplen = inlen);
cp = in;
inend = in+inlen;
curlen = 0;
while (cp < inend) {
colp = 0;
free_val = 0;
val = 0;
/*
** Look for the beginning of $(...)
*/
dp = find_char(cp, inend, "$");
if (dp == (char *) 0) {
len = inend-cp;
} else {
len = dp-cp;
if ((dp == inend-1) || ((strchr(SPECIALS, *(dp+1)) == 0)
&& (*(dp+1) != '('))) {
len++;
dp = (char *) 0;
} else if (dp > inend-3 && *(dp+1) == '(') {
len++;
dp = (char *) 0;
}
}
/*
** Copy everything up to the "$(" into the output string, expanding its
** size if necessary.
*/
if (curlen+len > tmplen) {
tmplen = curlen+len+128;
tmp = realloc(tmp, tmplen);
}
memcpy(tmp+curlen, cp, len);
curlen += len;
/*
** If we found "$(", locate the closing ")" and extract the symbol name,
** then look it up and stuff the resulting value into the output string.
*/
if (dp == (char *) 0) {
cp += len;
} else {
int is_special = 0;
int builtin_called = 0;
dp++;
if (*dp == '(') {
dp++;
pp = find_char(dp, inend, ")");
/*
** Check for function calls, then embedded symbol references and lastly,
** check for $(var:<sfx>=<sfx>).
*/
if (pp != (char *) 0) {
colp = find_char(dp, pp, " :");
if (colp != 0 && *colp == ' ') {
int i;
pp = 0;
len = min(colp-dp, MMK_S_SYMBOL);
strncpy(var, dp, len);
var[len] = '\0';
upcase(var);
builtin_called = 1;
++colp;
cp = apply_builtin(var, colp,
inend-colp, &val, &len,
&resolved_MMS_macro,
dont_resolve_unknowns);
did_one = (dont_resolve_unknowns == 0);
free_val = (val != (char *)0);
} else {
colp = find_char(dp, pp, "$");
if (colp != 0) if (colp < pp-1 && (*(colp+1) == '('
|| strchr(SPECIALS, *(colp+1)) != 0)) {
tmp[curlen++] = '$';
cp += len + 1;
dp = colp = pp = 0;
continue;
}
colp = find_char(dp, pp, ":");
if (colp != 0) {
char *cp;
for (cp = colp; isspace(*(cp-1)); cp--);
len = min(cp-dp, MMK_S_SYMBOL);
} else {
len = min(pp-dp,MMK_S_SYMBOL);
}
strncpy(var, dp, len);
var[len] = '\0';
upcase(var);
}
}
} else {
pp = strchr(SPECIALS, *dp);
if (pp != 0) {
strcpy(var, SPECIAL_VAR[pp-SPECIALS]);
pp = dp;
is_special = 1;
}
}
if (!builtin_called) {
if (pp != 0) {
valsym = Lookup_Symbol(var);
if (valsym != (struct SYMBOL *) 0) {
did_one = 1;
if (strcmp(valsym->name, "MMS") == 0) resolved_MMS_macro = 1;
if (colp != 0) {
apply_subst_rule(valsym->value, colp+1, &val, &len);
free_val = 1;
} else {
val = valsym->value;
len = strlen(val);
}
cp = pp + 1;
} else {
/*
** If dont_resolve_unknowns is set and we didn't find the symbol in the
** symbol table, just copy the symbol reference into the output string.
** Otherwise, the symbol reference resolves to a null string.
**
** When dont_resolve_unknowns is set to 2, we resolve unknowns unless they
** are on the special "non_resolvables" list.
*/
if (dont_resolve_unknowns == 1) {
len = 1;
val = is_special ? dp-1 : dp-2;
cp = is_special ? dp : dp-1;
} else {
if (dont_resolve_unknowns == 2) {
for (i = 0; i < sizeof(non_resolvables)/
sizeof(non_resolvables[0]); i++) {
if (strcmp(var, non_resolvables[i]) == 0)
break;
}
if (i < sizeof(non_resolvables)/
sizeof(non_resolvables[0])) {
len = 1;
val = is_special ? dp-1 : dp-2;
cp = is_special ? dp : dp-1;
} else {
len = 0;
val = dp;
cp = pp + 1;
}
} else {
len = 0;
val = dp;
cp = pp + 1;
}
}
}
} else {
len = 1;
val = dp-2;
cp = dp-1;
}
}
if (curlen+len > tmplen) {
tmplen = curlen+len+128;
tmp = realloc(tmp, tmplen);
}
memcpy(tmp+curlen, val, len);
curlen += len;
if (free_val) free(val);
}
}
if (!first) free(in);
first = 0;
if (did_one) {
in = tmp;
inlen = curlen;
}
} while (did_one);
*out = tmp;
*outlen = curlen;
return resolved_MMS_macro;
} /* Resolve_Symbols */
/*
**++
** ROUTINE: Clear_Local_Symbols
**
** FUNCTIONAL DESCRIPTION:
**
** Deletes all of the symbols in the local symbol table.
**
** RETURNS: void
**
** PROTOTYPE:
**
** Clear_Local_Symbols()
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: local_symbols
**
**--
*/
void Clear_Local_Symbols (void) {
struct SYMBOL *sym;
int i;
for (i = 0; i < MMK_K_SYMTABLE_SIZE; i++) {
while (queue_remove(local_symbols.symlist[i].head, &sym)) {
mem_free_symbol(sym);
}
}
} /* Clear_Local_Symbols */
/*
**++
** ROUTINE: Create_Local_Symbols
**
** FUNCTIONAL DESCRIPTION:
**
** Given a dependency rule, the local symbols for commands
** invoked under that dependency are defined.
**
** RETURNS: void
**
** PROTOTYPE:
**
** Create_Local_Symbols(struct DEPEND *dependency_rule)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: local_symbols
**
**--
*/
void Create_Local_Symbols (struct DEPEND *dep, struct OBJREF *srcref, struct QUE *chgque) {
struct OBJREF *obj;
struct RULE *r;
char nam[256], *src, *cp;
int srclen, srcsize, i;
extract_name(nam, dep->target->name);
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$TARGET_NAME", nam, -1);
extract_filename(nam, dep->target->name);
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$TARGET_FNAME", nam, -1);
if (dep->target->type == MMK_K_OBJ_LIBMOD) {
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$TARGET", dep->target->libfile->name, -1);
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$TARGET_MODULE", dep->target->name, -1);
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$LIB_ELEMENT", dep->target->name, -1);
} else {
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$TARGET", dep->target->name, -1);
}
/*
** If we didn't get an explicit source reference, try and find the "best"
** one available. That's the one where there's a rule for building the
** target from one of the sources, or if there is no such rule, just the
** first source in the list.
*/
if (srcref == 0) {
for (obj = dep->sources.flink; obj != &dep->sources; obj = obj->flink) {
if (obj->obj->type == MMK_K_OBJ_LIBMOD) {
r = find_rule(dep->target->sfx, obj->obj->libfile->sfx);
} else {
r = find_rule(dep->target->sfx, obj->obj->sfx);
}
if (r) break;
}
if (obj == &dep->sources) obj = dep->sources.flink;
} else obj = srcref;
/*
** We still have an out if there were no sources in the list (in which case
** we just don't define the sources macros, except for MMS$CMS_GEN).
*/
if (obj != &dep->sources) {
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$SOURCE", obj->obj->name, -1);
if (obj->obj->type == MMK_K_OBJ_CMSFILE) {
char lib[256];
unsigned int status;
status = cms_parse_name(obj->obj->name, lib, sizeof(lib), 0,
nam, sizeof(nam), 0, 0);
if (OK(status)) {
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_ELEMENT", nam, -1);
if (lib[0] != '\0') Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_LIBRARY", lib, -1);
else if (strlen(cms$lib) != 7 || !strneql_case_blind(cms$lib, "CMS$LIB", 7))
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_LIBRARY", cms$lib, -1);
}
if (obj->obj->cms_gen[0] != '\0') {
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_GEN", obj->obj->cms_gen, -1);
} else if (cms_default_generation[0] != '\0') {
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_GEN", cms_default_generation, -1);
} else Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_GEN", "1+", 2);
} else if (cms_default_generation[0] != '\0') {
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_GEN", cms_default_generation, -1);
} else Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_GEN", "1+", 2);
extract_name(nam, obj->obj->name);
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$SOURCE_NAME", nam, -1);
extract_filename(nam, obj->obj->name);
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$SOURCE_FNAME", nam, -1);
srcsize = strlen(obj->obj->name)+256;
src = malloc(srcsize);
strcpy(src, obj->obj->name);
srclen = strlen(obj->obj->name);
for (obj = obj->flink; obj != &dep->sources; obj = obj->flink) {
i = make_object_name(nam, obj->obj);
if (srclen + i > srcsize-2) {
srcsize += i + 256;
src = realloc(src, srcsize);
}
*(src+(srclen++)) = ',';
strcpy(src+srclen, nam);
srclen += strlen(nam);
}
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$SOURCE_LIST", src, srclen);
for (cp = src; *cp; cp++) if (*cp == ',') *cp = ' ';
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$SOURCE_LIST_SPACES", src, srclen);
free(src);
} else if (cms_default_generation[0] != '\0') {
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_GEN", cms_default_generation, -1);
} else Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CMS_GEN", "1+", 2);
/*
** Now build the changed-source list
*/
srcsize = 256;
src = malloc(srcsize);
srclen = 0;
for (obj = chgque->head; obj != (struct OBJREF *) chgque; obj = obj->flink) {
i = make_object_name(nam, obj->obj);
if (srclen + i > srcsize-2) {
srcsize += i + 256;
src = realloc(src, srcsize);
}
if (srclen > 0) *(src+(srclen++)) = ',';
strcpy(src+srclen, nam);
srclen += strlen(nam);
}
src[srclen] = '\0';
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CHANGED_LIST", src, srclen);
for (cp = src; *cp; cp++) if (*cp == ',') *cp = ' ';
Define_Symbol(MMK_K_SYM_LOCAL, "MMS$CHANGED_LIST_SPACES", src, srclen);
free(src);
} /* Create_Local_Symbols */
/*
**++
** ROUTINE: set_mmssuffixes
**
** FUNCTIONAL DESCRIPTION:
**
** Define the value for MMSSUFFIXES.
**
** RETURNS: void
**
** PROTOTYPE:
**
** set_mmssuffixes(void)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
void set_mmssuffixes (void) {
static char mmssuffixes[] = "MMSSUFFIXES";
struct SFX *sfx;
Define_Symbol(MMK_K_SYM_BUILTIN, mmssuffixes, "", 0);
sfx = suffixes.flink;
while (sfx != (struct SFX *) &suffixes) {
Define_Symbol(MMK_K_SYM_BUILTIN, mmssuffixes, sfx->value, -1, " ");
sfx = sfx->flink;
}
} /* set_mmssuffixes */
/*
**++
** ROUTINE: apply_subst_rule
**
** FUNCTIONAL DESCRIPTION:
**
** tbs
**
** RETURNS: cond_value, longword (unsigned), write only, by value
**
** PROTOTYPE:
**
** tbs
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
**
**
** SIDE EFFECTS: None.
**
**--
*/
static void apply_subst_rule (char *orig, char *rule, char **xval, int *xlen) {
char lhs[256], rhs[256];
char *cp, *val, *cp1, *dp;
int i, len, curlen, add_rhs, lhslen, rhslen;
val = malloc(len = strlen(orig)+256);
if (*rule == ':') {
apply_full_subst_rule(orig, rule+1, xval, xlen);
return;
}
while (isspace(*rule)) rule++;
cp1 = lhs;
for (cp = rule; *cp != '\0' && *cp != '=' && *cp != ')' && !isspace(*cp); cp++) *cp1++ = *cp;
while (isspace(*cp)) cp++;
*cp1 = '\0';
lhslen = cp1-lhs;
cp1 = rhs;
if (*cp == '=') {
for (cp++; isspace(*cp); cp++);
while (*cp != '\0' && *cp != ')' && !isspace(*cp)) *cp1++ = *cp++;
}
*cp1 = '\0';
rhslen = cp1-rhs;
curlen = 0;
cp = orig;
while (*cp != '\0') {
add_rhs = 0;
for (cp1 = cp; *cp1 != '\0' && *cp1 != ',' && !isspace(*cp1); cp1++);
for (dp = cp1-1; dp >= cp && *dp != '.' && *dp != ']' && *dp != '>' && *dp != ':'; dp--);
if (dp < cp || *dp != '.') dp = 0;
if (dp == 0) {
if (lhslen == 0) {
dp = cp1;
add_rhs = 1;
}
} else if (cp1-dp == lhslen) {
add_rhs = strneql_case_blind(dp, lhs, lhslen);
}
i = add_rhs ? rhslen + (dp-cp) : cp1-cp;
if ((curlen + i) >= len) {
len = curlen + i + len + 256;
val = realloc(val, len);
}
if (add_rhs) {
memcpy(val+curlen, cp, dp-cp);
curlen += (dp-cp);
memcpy(val+curlen, rhs, rhslen);
curlen += rhslen;
} else {
memcpy(val+curlen, cp, cp1-cp);
curlen += (cp1-cp);
}
while (isspace(*cp1)) cp1++;
if (*cp1 == '\0') break;
if (*cp1 == ',') {
*(val+curlen) = ',';
curlen++;
for (cp = cp1+1; isspace(*cp); cp++);
} else {
*(val+curlen) = ' ';
curlen++;
cp = cp1;
}
}
*xval = val;
*xlen = curlen;
} /* apply_subst_rule */
/*
**++