forked from endlesssoftware/mmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
1374 lines (1259 loc) · 30.9 KB
/
misc.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: Miscellaneous routines for MMK.
**
** MODULE DESCRIPTION:
**
** Miscellaneous utility routines.
**
** 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: 20-AUG-1992
**
** MODIFICATION HISTORY:
**
** 20-AUG-1992 V1.0 Madison Initial coding.
** 01-SEP-1992 V1.1 Madison Comments.
** 12-JAN-1993 V1.1-1 Madison Fix make_object_name.
** 04-JUN-1993 V1.2 Madison Add default rule support.
** 17-OCT-1993 V1.3 Madison Add strneql_case_blind.
** 20-OCT-1993 V1.4 Madison Add ctrlt_ast routines.
** 28-OCT-1993 V1.4-1 Madison Fix behavior of .SUFFIXES directive.
** 09-DEC-1993 V1.4-2 Madison Fix extract_name to use $FILESCAN.
** 04-APR-1994 V1.4-3 Madison Fix extract_name again!
** 14-APR-1994 V1.4-4 Madison Fix extract_name again!
** 11-JUL-1994 V1.4-5 Madison Have find_rule return 0 if none found.
** 14-JUL-1994 V1.5 Madison New extract routines, scan_rule_list.
** 11-AUG-1994 V1.5-1 Madison Convert suffixes to upper case.
** 27-JUN-1995 V1.6 Madison Add extract_nametype.
** 06-NOV-1995 V1.6-1 Madison Fix behavior of scan_rule_list.
** 27-DEC-1998 V1.7 Madison General cleanup.
** 20-JAN-2001 V1.8 Madison More fixes for rule application, from
** Chuck Lane.
** 17-DEC-2010 V1.9 Sneddon Add cat.
** 10-FEB-2011 V1.10 Sneddon Add itoa.
** 11-APR-2011 V1.10-1 Sneddon Minor change to help cat cope with
** null source strings.
** 12-APR-2011 V1.11 Sneddon Add trim.
** 02-JUL-2012 V1.12 Sneddon Change find_char to find first out of
** a list of characters.
** 29-AUG-2012 V1.13 Sneddon Improve cat.
** 09-JUN-2014 V1.14 Sneddon Add length argument to find_suffix.
** 10-JUN-2014 V1.14-1 Sneddon make find_suffix match case-insensitive
** 12-JUN-2014 V1.15 Sneddon Add create_suffix. Fix find_suffix to
** match length before compare.
**--
*/
#pragma module MISC "V1.15"
#include "mmk.h"
#include "globals.h"
#include <lnmdef.h>
#include <iodef.h>
#include <dvidef.h>
#include <fscndef.h>
#include <builtins.h>
#include <stdarg.h>
#include <stdio.h>
/*
** Forward declarations
*/
void Build_Suffix_List(char *, int);
char *itoa(int);
char *cat(char *, ...);
char *trim(char *);
char *find_char(char *, char *, char *);
void upcase(char *);
int extract_name(char *, char *);
int extract_prefix(char *, char *);
int extract_filetype(char *, char *);
int extract_filename(char *, char *);
int extract_nametype(char *, char *);
static int split_path(char *, char *, unsigned int);
int prefix_match(char *, char *);
int create_suffix(char *, int, struct SFX *);
struct SFX *find_suffix(char *, int);
struct RULE *find_rule(char *, char *);
struct RULE *find_rule_with_prefixes(struct OBJECT *, struct OBJECT *);
struct RULE *scan_rule_list(struct RULE *, char *, int);
int make_object_name(char *, struct OBJECT *);
int logical_present(char *);
int get_logical(char *, char *, int);
int strneql_case_blind(char *, char *, int);
void set_ctrlt_ast(unsigned int (*)(void *), void *);
void clear_ctrlt_ast(void);
static unsigned int ctrlt_ast(void);
unsigned int find_image_symbol(char *, char *, void *);
static unsigned int x_find_image_symbol(struct dsc$descriptor *,
struct dsc$descriptor *, void *);
/*
** Local statics
*/
static unsigned short sysinput_chan = 0;
static unsigned int (*ctrlt_ast_rtn)(void *) = 0;
static void *ctrlt_ast_arg = 0;
/*
**++
** ROUTINE: Build_Suffix_List
**
** FUNCTIONAL DESCRIPTION:
**
** Builds the queue of suffixes from the right-hand side
** of a .SUFFIXES directive.
**
** RETURNS: void
**
** PROTOTYPE:
**
** Build_Suffix_List(char *line, int linelen)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
void Build_Suffix_List (char *line, int linelen) {
struct SFX *sfx;
char *lp, *lpmax, *sp;
int i;
if (linelen == 0) {
while (queue_remove(suffixes.flink, &sfx)) mem_free_sfx(sfx);
} else {
lp = line;
lpmax = line+linelen;
while (1) {
while (lp < lpmax && isspace(*lp)) lp++;
if (lp >= lpmax) break;
sp = lp;
while (lp < lpmax && !isspace(*lp)) lp++;
/*
** The behaviour here is different from .SUFFIXES_AFTER and
** .SUFFIXES_BEFORE to retain compatibility with previous versions
** of MMK. However, beware that if the suffix is already in the
** list, it will NOT be appended to the end as in previous
** versions. However, that said the functionality will not change
** as the list is scanned from suffixes.flink, so duplicate
** entries will never be reached anyway.
*/
create_suffix(sp, lp-sp, suffixes.blink);
}
}
set_mmssuffixes();
} /* Build_Suffix_List */
/*
**++
** ROUTINE: itoa
**
** FUNCTIONAL DESCRIPTION:
**
** Converts an integer into its string representation. Anything
** returned by this function will need to be free()'d by the caller.
**
** RETURNS: pointer to char
**
** PROTOTYPE:
**
** itoa(int i)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** 0: memory allocation error
** non-0: pointer to the character
**
** SIDE EFFECTS: None.
**
**--
*/
char *itoa(int i) {
char s[12];
sprintf(s, "%d", i);
return strdup(s);
}
/*
**++
** ROUTINE: cat
**
** FUNCTIONAL DESCRIPTION:
**
** Concatenates strings, dynamically. Arguments following the input
** string (which must be a pointer of zero or to a null-terminated, malloc'd
** string) must be a string, length pair. If the length is -1, the string
** is assumed to be null-terminated and the length taken from the string
** with strlen.
**
** There is a special case that if the last argument is odd (as int,
** no length) it is assumed that the last string is already null-terminated
** and the length is taken with strlen.
**
** RETURNS: pointer to char
**
** PROTOTYPE:
**
** cat(char *in, [ char *, int , ... [ char *])
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** 0: memory allocation error
** non-0: pointer to the character
**
** SIDE EFFECTS: Uses malloc/realloc to allocate all storage, the
** caller needs to free this themselves using free.
**
**--
*/
char *cat(char *in, ...) {
int actualcount;
va_list ap;
char *str, *out, *outp;
int i, inlen, len, outlen;
outlen = inlen = (in == (char *)0) ? 0 : strlen(in);
va_count(actualcount);
va_start(ap, in);
i = 1;
while (i < actualcount) {
str = va_arg(ap, char *);
if (++i < actualcount) {
i++;
len = va_arg(ap, int);
outlen += (len != -1) ? len : strlen(str);
} else {
outlen += strlen(str);
}
}
va_end(ap);
out = (in == 0) ? malloc(outlen+1) : realloc(in, outlen+1);
va_start(ap, in);
i = 1;
outp = out + inlen;
while (i < actualcount) {
str = va_arg(ap, char *);
if (++i < actualcount) {
i++;
len = va_arg(ap, int);
if (len == -1) len = strlen(str);
} else {
len = strlen(str);
}
memcpy(outp, str, len);
outp += len;
}
va_end(ap);
*outp = '\0';
return out;
}
/*
**++
** ROUTINE: trim
**
** FUNCTIONAL DESCRIPTION:
**
** Trims the leading and trailing space characters of of a
** NUL-terminated string.
**
** RETURNS: pointer to char
**
** PROTOTYPE:
**
** trim(char *s)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** 0: Illegal string address.
** non-0: pointer to the trimmed string.
**
** SIDE EFFECTS:
** This function edits the string in place. To preserve
** the original string, use strdup (or something similar).
** However, beware that the pointer returned by this function
** may not necessarily be the same as the pointer in.
**
**--
*/
char *trim(char *s) {
char *sp, *tp;
if (!s) return NULL;
for (sp = s; isspace(*sp) && (*sp != '\0'); sp++);
for (tp = sp + strlen(sp) - 1; isspace(*tp) && (tp > sp); tp--);
*++tp = '\0';
return sp;
}
/*
**++
** ROUTINE: find_char
**
** FUNCTIONAL DESCRIPTION:
**
** Locates a character in a string, given the start and end
** address of the string.
**
** RETURNS: pointer to char
**
** PROTOTYPE:
**
** find_char(char *base, char *end, char *str)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** 0: not found
** non-0: pointer to the character
**
** SIDE EFFECTS: None.
**
**--
*/
char *find_char (char *base, char *end, char *charset) {
register char *cp, *csp;
for (cp = base; cp < end; cp++) {
for (csp = charset; *csp; csp++) {
if (*cp == *csp) return cp;
}
}
return (char *) 0;
}
/*
**++
** ROUTINE: upcase
**
** FUNCTIONAL DESCRIPTION:
**
** Converts a string in-place to upper case.
**
** RETURNS: void
**
** PROTOTYPE:
**
** upcase(char *str)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
void upcase (char *str) {
register char *cp;
for (cp = str; *cp; cp++) {
*cp = islower(*cp) ? toupper(*cp) : *cp;
}
return;
}
/*
**++
** ROUTINE: extract_name
**
** FUNCTIONAL DESCRIPTION:
**
** Given a VMS file specification, returns everything up to,
** but not including, the file type.
**
** RETURNS: int
**
** PROTOTYPE:
**
** extract_name(char *dest, char *src)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
int extract_name (char *dest, char *src) {
return split_path(dest, src, FSCN$M_DEVICE|FSCN$M_ROOT|FSCN$M_DIRECTORY|FSCN$M_NAME);
} /* extract_name */
/*
**++
** ROUTINE: extract_prefix
**
** FUNCTIONAL DESCRIPTION:
**
** Given a VMS file specification, returns the device and directory.
**
** RETURNS: int
**
** PROTOTYPE:
**
** extract_prefix(char *dest, char *src)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
int extract_prefix (char *dest, char *src) {
return split_path(dest, src, FSCN$M_DEVICE|FSCN$M_ROOT|FSCN$M_DIRECTORY);
} /* extract_prefix */
/*
**++
** ROUTINE: extract_filetype
**
** FUNCTIONAL DESCRIPTION:
**
** Given a VMS file specification, returns just the file type.
**
** RETURNS: int
**
** PROTOTYPE:
**
** extract_filetype(char *dest, char *src)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
int extract_filetype (char *dest, char *src) {
return split_path(dest, src, FSCN$M_TYPE);
} /* extract_filetype */
/*
**++
** ROUTINE: extract_filename
**
** FUNCTIONAL DESCRIPTION:
**
** Given a VMS file specification, returns just the file name.
**
** RETURNS: int
**
** PROTOTYPE:
**
** extract_filename(char *dest, char *src)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
int extract_filename (char *dest, char *src) {
return split_path(dest, src, FSCN$M_NAME);
} /* extract_filename */
/*
**++
** ROUTINE: extract_nametype
**
** FUNCTIONAL DESCRIPTION:
**
** Given a VMS file specification, returns just the file name and type.
**
** RETURNS: int
**
** PROTOTYPE:
**
** extract_nametype(char *dest, char *src)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
int extract_nametype (char *dest, char *src) {
return split_path(dest, src, FSCN$M_NAME|FSCN$M_TYPE);
} /* extract_nametype */
/*
**++
** ROUTINE: split_path
**
** FUNCTIONAL DESCRIPTION:
**
** Workhorse routine for the extract_xxx routines. Uses the
** $FILESCAN system service to parse a file specification, and
** copies in just the requested parts.
**
** RETURNS: int
**
** PROTOTYPE:
**
** split_path(char *dest, char *src, unsigned int flags)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
static int split_path (char *dest, char *src, unsigned int flags) {
static struct {
unsigned short len, code;
char *ptr;
} itmlst[] = {0, FSCN$_DEVICE, 0,
0, FSCN$_ROOT, 0,
0, FSCN$_DIRECTORY, 0,
0, FSCN$_NAME, 0,
0, FSCN$_TYPE, 0,
0, 0, 0};
static unsigned int part[] = {FSCN$M_DEVICE, FSCN$M_ROOT,
FSCN$M_DIRECTORY, FSCN$M_NAME, FSCN$M_TYPE};
unsigned int status;
struct dsc$descriptor fdsc;
char *cp;
int i;
INIT_SDESC(fdsc, strlen(src), src);
status = sys$filescan(&fdsc, itmlst, 0);
cp = dest;
if (OK(status)) {
for (i = 0; i < sizeof(part)/sizeof(unsigned int); i++) {
if ((flags & part[i]) && itmlst[i].len > 0) {
if (part[i] == FSCN$M_TYPE) {
if (itmlst[i].ptr[itmlst[i].len] == '~') itmlst[i].len++;
}
memcpy(cp, itmlst[i].ptr, itmlst[i].len);
cp += itmlst[i].len;
}
}
}
*cp = '\0';
return (cp - dest);
} /* split_path */
/*
**++
** ROUTINE: prefix_match
**
** FUNCTIONAL DESCRIPTION:
**
** Given a "prefix" (i.e., a device+directory specification) and
** a file specification, checks to see if the file spec has the given
** prefix.
**
** RETURNS: int
**
** PROTOTYPE:
**
** prefix_match(char *pfx, char *fspec)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES: None.
**
** SIDE EFFECTS: None.
**
**--
*/
int prefix_match(char *pfx, char *fspec) {
char tmp[256];
int len;
len = split_path(tmp, fspec, FSCN$M_DEVICE|FSCN$M_ROOT|FSCN$M_DIRECTORY);
if (len != strlen(pfx)) return 0;
return strneql_case_blind(tmp, pfx, len);
} /* prefix_match */
/*
**++
** ROUTINE: create_suffix
**
** FUNCTIONAL DESCRIPTION:
**
** Create a suffix in the suffix queue (at the specified position).
**
** RETURNS: longword status code
**
** PROTOTYPE:
**
** create_suffix(char *str, int, struct SFX *)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** 1: suffix was inserted into the queue.
** 0: suffix already exists.
**
** SIDE EFFECTS: None.
**--
*/
int create_suffix (char *name, int len, struct SFX *pos) {
struct SFX *sfx;
if (len == -1) len = strlen(name);
len = len > MMK_S_SFX ? MMK_S_SFX : len;
sfx = find_suffix(name, len);
if (sfx != 0) return 0;
sfx = mem_get_sfx();
memcpy(sfx->value, name, len);
sfx->value[len] = '\0';
upcase(sfx->value);
queue_insert(sfx, pos);
return 1;
}
/*
**++
** ROUTINE: find_suffix
**
** FUNCTIONAL DESCRIPTION:
**
** Locates a suffix in the suffix queue.
**
** RETURNS: pointer to a SFX structure
**
** PROTOTYPE:
**
** find_suffix(char *str, int)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** non-0: address of entry in suffixes queue.
** 0: not found
**
** SIDE EFFECTS: None.
**
**--
*/
struct SFX *find_suffix (char *name, int len) {
struct SFX *sfx;
if (len == -1) len = strlen(name);
for (sfx = suffixes.flink; sfx != &suffixes; sfx = sfx->flink) {
if ((len == strlen(sfx->value))
&& (strncasecmp(name, sfx->value, len) == 0)) {
return sfx;
}
}
return (struct SFX *) 0;
}
/*
**++
** ROUTINE: find_rule
**
** FUNCTIONAL DESCRIPTION:
**
** Given source and target suffix strings, locates the base default
** build rule for those suffixes.
**
** RETURNS: pointer to struct RULE
**
** PROTOTYPE:
**
** find_rule(char *target, char *source)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** non-0: pointer to rule entry
** 0: not found
**
** SIDE EFFECTS: None.
**
**--
*/
struct RULE *find_rule (char *trg, char *src) {
struct RULE *r;
for (r = rules.flink; r != &rules; r = r->flink) {
if (strcmp(trg, r->trg) == 0 && strcmp(src, r->src) == 0) return r;
}
return 0;
} /* find_rule */
/*
**++
** ROUTINE: find_rule_with_prefixes
**
** FUNCTIONAL DESCRIPTION:
**
** Given two objects, locates a build rule for those objects,
** taking including scanning for prefixes.
**
** RETURNS: pointer to struct RULE
**
** PROTOTYPE:
**
** find_rule_with_prefixes(struct OBJECT *target, struct OBJECT *source)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** non-0: pointer to rule entry
** 0: not found
**
** SIDE EFFECTS: None.
**
**--
*/
struct RULE *find_rule_with_prefixes (struct OBJECT *trg, struct OBJECT *src) {
struct RULE *xr, *r;
for (xr = rules.flink; xr != &rules; xr = xr->flink) {
for (r = xr; r != 0; r = r->next) {
if (strcmp(trg->sfx, r->trg) == 0 && strcmp(src->sfx, r->src) == 0) {
if (prefix_match(r->trgpfx, trg->name) &&
prefix_match(r->srcpfx, src->name)) return r;
}
}
for (r = xr; r != 0; r = r->next) {
if (strcmp(trg->sfx, r->trg) == 0 && strcmp(src->sfx, r->src) == 0) {
if (r->trgpfx[0] == '\0' && r->srcpfx[0] == '\0') return r;
}
}
}
return 0;
} /* find_rule_with_prefixes */
static unsigned int
object_or_file_exists (const char *fspec)
{
struct OBJECT tobj;
memset(&tobj, 0, sizeof(tobj));
strcpy(tobj.name, fspec);
tobj.type = MMK_K_OBJ_FILE;
if (Find_Object(&tobj) != NULL)
return SS$_NORMAL;
return file_exists((char *)fspec, 0);
}
/*
**++
** ROUTINE: scan_rule_list
**
** FUNCTIONAL DESCRIPTION:
**
** Given a base rule from a find_rule() call, scans
** the list of rules (with prefixes, possibly) hanging off
** the base, looking for a rule that might be used to
** build a target.
**
** RETURNS: struct RULE *
**
** PROTOTYPE:
**
** scan_rule_list(struct RULE *base, char *target_name, int generalize)
**
** IMPLICIT INPUTS: None.
**
** IMPLICIT OUTPUTS: None.
**
** COMPLETION CODES:
** 0: No rule found.
** non-0: Matching rule found.
**
** SIDE EFFECTS: None.
**
**--
*/
struct RULE *scan_rule_list (struct RULE *base, char *target_name, int generalize) {
TIME rdt ;
struct RULE *r, *tmpr, *r_maybe[128], *tmpr_maybe[128];
struct SFX *s;
char trgnam[MMK_S_FILE], tmpsfx[MMK_S_SFX], fspec[MMK_S_FILE], *cp;
unsigned int status;
int nofileok, pass, passmax, check_cms, trgnamlen, maybes, i;
nofileok = (generalize & 0x10) != 0;
generalize = generalize & 0x0F;
maybes = 0;
/*
** When there's just one rule for this suffix pair, we want to avoid
** checking it twice. Hence this rather convoluted-looking logic,
** which prevents us from doing a prefix-based scan plus a non-prefix-based
** scan in the 1-rule case.
**
** This is further complicated by our recursive inference check for files
** residing in CMS. When we call with generalize == 1, we want to check
** _just_ the prefixed rules. When we call with generalize == 2, we
** want to check _just_ the non-prefixed rules.
**
** When the NOFILEOK flag is set (generalize & 0x10), we don't care
** if the file exists.
**
** Phew! Now I understand why NMAKE doesn't have generic rules that
** work across directories.
*/
if (base->next == 0) {
if (base->trgpfx[0] == '\0' && base->srcpfx[0] == '\0') {
pass = 1;
passmax = 2;
} else {
pass = 0;
passmax = 1;
}
} else {
pass = generalize < 2 ? 0 : 1;
passmax = (generalize & 1) ? 1 : 2;
}
/*
** Now scan the base rule and any rules hanging off it. We may do two
** passes over the list -- one with prefixes being used, the other without.
*/
for (r = base; pass < passmax; pass++, r = base) {
while (r) {
/*
** The first pass is the prefix-based scan. For
** this, we use just the file name part of the target.
*/
if (pass == 0) {
if (!prefix_match(r->trgpfx, target_name)) {
r = r->next;
continue;
}
trgnamlen = extract_filename(trgnam, target_name);
/*
** The second pass is the non-prefix-based scan.
** For this, we check to see if the source file is in the
** same directory as the target file.
*/
} else {
if (r->trgpfxlen != 0 || r->srcpfxlen != 0) {
r = r->next;
continue;
}
trgnamlen = extract_name(trgnam, target_name);
}
check_cms = r->src[strlen(r->src)-1] == '~';
memcpy(fspec, r->srcpfx, r->srcpfxlen);
memcpy(fspec+r->srcpfxlen, trgnam, trgnamlen);
strcpy(fspec+(r->srcpfxlen+trgnamlen), r->src);
if (check_cms) status = cms_get_rdt(fspec, 0, &rdt);
else if (nofileok) status = SS$_NORMAL;
else status = object_or_file_exists(fspec);
if (OK(status)) break;
/*
** OK, so the source file doesn't exist. If we're using
** CMS, let's see if we can infer the existence of the
** source we want from its presence in the CMS library.
*/
if (!check_cms && use_cms) {
strcpy(tmpsfx, r->src);
strcat(tmpsfx, "~");
s = find_suffix(tmpsfx, -1);
if (s != 0) {
tmpr = find_rule(r->src, s->value);
if (tmpr != 0) {
if (scan_rule_list(tmpr, fspec, 1)) break;
/*
** We schedule a later check on the generic rules if the rule we found
** has both prefixed and generic ones.
*/
if (tmpr->next != 0) {
r_maybe[maybes] = r;
tmpr_maybe[maybes++] = tmpr;
}
}
}
}
r = r->next;
}
if (r != 0) break;
}
if ((pass >= passmax || r == 0) && maybes > 0) {
for (i = 0; i < maybes; i++) {
r = r_maybe[i];
tmpr = tmpr_maybe[i];
memcpy(fspec, r->srcpfx, r->srcpfxlen);
memcpy(fspec+r->srcpfxlen, trgnam, trgnamlen);