summaryrefslogtreecommitdiffstats
path: root/src/kmk/makeint.h
blob: 9924cb9a598b202378b5b17439f9d6fe872a0e69 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
/* Miscellaneous global declarations and portability cruft for GNU Make.
Copyright (C) 1988-2016 Free Software Foundation, Inc.
This file is part of GNU Make.

GNU Make is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* We use <config.h> instead of "config.h" so that a compilation
   using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
   (which it would do because makeint.h was found in $srcdir).  */
#include <config.h>
#undef  HAVE_CONFIG_H
#define HAVE_CONFIG_H 1

#include <k/kDefs.h> /* bird */

/* Specify we want GNU source code.  This must be defined before any
   system headers are included.  */

#define _GNU_SOURCE 1

/* AIX requires this to be the first thing in the file.  */
#if HAVE_ALLOCA_H
# include <alloca.h>
#else
# ifdef _AIX
 #pragma alloca
# else
#  if !defined(__GNUC__) && !defined(WINDOWS32)
#   ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
#   endif
#  endif
# endif
#endif

/* Disable assert() unless we're a maintainer.
   Some asserts are compute-intensive.  */
#ifndef MAKE_MAINTAINER_MODE
# define NDEBUG 1
#endif

/* Include the externally-visible content.
   Be sure to use the local one, and not one installed on the system.
   Define GMK_BUILDING_MAKE for proper selection of dllexport/dllimport
   declarations for MS-Windows.  */
#ifdef WINDOWS32
# define GMK_BUILDING_MAKE
#endif
#include "gnumake.h"

#ifdef  CRAY
/* This must happen before #include <signal.h> so
   that the declaration therein is changed.  */
# define signal bsdsignal
#endif

/* If we're compiling for the dmalloc debugger, turn off string inlining.  */
#if defined(HAVE_DMALLOC_H) && defined(__GNUC__)
# define __NO_STRING_INLINES
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdio.h>
#include <ctype.h>

#ifdef HAVE_SYS_TIMEB_H
/* SCO 3.2 "devsys 4.2" has a prototype for 'ftime' in <time.h> that bombs
   unless <sys/timeb.h> has been included first.  */
# include <sys/timeb.h>
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <errno.h>

#ifndef errno
extern int errno;
#endif

#ifdef __VMS
/* In strict ANSI mode, VMS compilers should not be defining the
   VMS macro.  Define it here instead of a bulk edit for the correct code.
 */
# ifndef VMS
#  define VMS
# endif
#endif

#ifdef  HAVE_UNISTD_H
# include <unistd.h>
/* Ultrix's unistd.h always defines _POSIX_VERSION, but you only get
   POSIX.1 behavior with 'cc -YPOSIX', which predefines POSIX itself!  */
# if defined (_POSIX_VERSION) && !defined (ultrix) && !defined (VMS)
#  define POSIX 1
# endif
#endif

/* Some systems define _POSIX_VERSION but are not really POSIX.1.  */
#if (defined (butterfly) || defined (__arm) || (defined (__mips) && defined (_SYSTYPE_SVR3)) || (defined (sequent) && defined (i386)))
# undef POSIX
#endif

#if !defined (POSIX) && defined (_AIX) && defined (_POSIX_SOURCE)
# define POSIX 1
#endif

#ifndef RETSIGTYPE
# define RETSIGTYPE     void
#endif

#ifndef sigmask
# define sigmask(sig)   (1 << ((sig) - 1))
#endif

#ifndef HAVE_SA_RESTART
# define SA_RESTART 0
#endif

#ifdef HAVE_VFORK_H
# include <vfork.h>
#endif

#ifdef  HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef  HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif

#ifndef PATH_MAX
# ifndef POSIX
#  define PATH_MAX      MAXPATHLEN
# endif
#endif
#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif

#ifdef  PATH_MAX
# define GET_PATH_MAX   PATH_MAX
# define PATH_VAR(var)  char var[PATH_MAX]
#else
# define NEED_GET_PATH_MAX 1
# define GET_PATH_MAX   (get_path_max ())
# define PATH_VAR(var)  char *var = alloca (GET_PATH_MAX)
unsigned int get_path_max (void);
#endif

#if defined (KMK) || defined (CONFIG_WITH_VALUE_LENGTH) \
 || defined (CONFIG_WITH_ALLOC_CACHES) \
 || defined (CONFIG_WITH_STRCACHE2)
# ifdef _MSC_VER
#  define MY_INLINE     _inline static
# elif defined(__GNUC__)
#  define MY_INLINE     static __inline__
# else
#  define MY_INLINE     static
# endif

# ifdef __GNUC__
#  define MY_PREDICT_TRUE(expr)  __builtin_expect(!!(expr), 1)
#  define MY_PREDICT_FALSE(expr) __builtin_expect(!!(expr), 0)
# else
#  define MY_PREDICT_TRUE(expr)  (expr)
#  define MY_PREDICT_FALSE(expr) (expr)
# endif
#endif

#if defined (KMK) || defined (CONFIG_WITH_VALUE_LENGTH) \
 || defined (CONFIG_WITH_STRCACHE2)
# ifdef _MSC_VER
#  define MY_DBGBREAK   __debugbreak()
# elif defined(__GNUC__)
#  if defined(__i386__) || defined(__x86_64__)
#   define MY_DBGBREAK  __asm__ __volatile__ ("int3")
#  else
#   define MY_DBGBREAK  assert(0)
#  endif
# else
#  define MY_DBGBREAK   assert(0)
# endif
# ifndef NDEBUG
#  define MY_ASSERT_MSG(expr, printfargs) \
    do { if (!(expr)) { printf printfargs; MY_DBGBREAK; } } while (0)
# else
#  define MY_ASSERT_MSG(expr, printfargs)   do { } while (0)
# endif
#endif

#ifdef KMK
/** @todo measure performance diff here!   */
# if 0 /* See if this speeds things up (Windows is doing this anyway, so,
          we might as well try be consistent in speed + features).  */
#  define MY_IS_BLANK(ch)           ((ch) == ' ' || (ch) == '\t')
#  define MY_IS_BLANK_OR_EOS(ch)    ((ch) == ' ' || (ch) == '\t' || (ch) == '\0')
# else
#  define MY_IS_BLANK(ch)           ISBLANK ((ch))
#  define MY_IS_BLANK_OR_EOS(ch)    (ISBLANK ((ch)) || (ch) == '\0')
# endif
#endif

#ifdef CONFIG_WITH_MAKE_STATS
extern long make_stats_allocations;
extern long make_stats_reallocations;
extern unsigned long make_stats_allocated;
extern unsigned long make_stats_ht_lookups;
extern unsigned long make_stats_ht_collisions;

# ifdef __APPLE__
#  include <malloc/malloc.h>
#  define SIZE_OF_HEAP_BLOCK(ptr)   malloc_size(ptr)

# elif defined(__linux__) /* glibc */
#  include <malloc.h>
#  define SIZE_OF_HEAP_BLOCK(ptr)   malloc_usable_size(ptr)

# elif defined(_MSC_VER) || defined(__OS2__)
#  define SIZE_OF_HEAP_BLOCK(ptr)   _msize(ptr)

# else
#  include <stdlib.h>
#  define SIZE_OF_HEAP_BLOCK(ptr)   0
#endif

# define MAKE_STATS_3(expr) do { expr; } while (0)
# define MAKE_STATS_2(expr) do { expr; } while (0)
# define MAKE_STATS(expr)   do { expr; } while (0)
#else
# define MAKE_STATS_3(expr) do { } while (0)
# define MAKE_STATS_2(expr) do { } while (0)
# define MAKE_STATS(expr)   do { } while (0)
#endif

/* bird - start */
#ifdef _MSC_VER
# include <intrin.h>
# define CURRENT_CLOCK_TICK() __rdtsc()
#else
# define CURRENT_CLOCK_TICK() 0
#endif

#define COMMA ,
#ifdef CONFIG_WITH_VALUE_LENGTH
# define IF_WITH_VALUE_LENGTH(a_Expr)           a_Expr
# define IF_WITH_VALUE_LENGTH_PARAM(a_Expr)     , a_Expr
#else
# define IF_WITH_VALUE_LENGTH(a_Expr)
# define IF_WITH_VALUE_LENGTH_PARAM(a_Expr)
#endif

#ifdef CONFIG_WITH_ALLOC_CACHES
# define IF_WITH_ALLOC_CACHES(a_Expr)           a_Expr
# define IF_WITH_ALLOC_CACHES_PARAM(a_Expr)     , a_Expr
#else
# define IF_WITH_ALLOC_CACHES(a_Expr)
# define IF_WITH_ALLOC_CACHES_PARAM(a_Expr)
#endif

#ifdef CONFIG_WITH_COMMANDS_FUNC
# define IF_WITH_COMMANDS_FUNC(a_Expr)          a_Expr
# define IF_WITH_COMMANDS_FUNC_PARAM(a_Expr)    , a_Expr
#else
# define IF_WITH_COMMANDS_FUNC(a_Expr)
# define IF_WITH_COMMANDS_FUNC_PARAM(a_Expr)
#endif

/* bird - end */


#ifndef CHAR_BIT
# define CHAR_BIT 8
#endif

#ifndef USHRT_MAX
# define USHRT_MAX 65535
#endif

/* Nonzero if the integer type T is signed.
   Use <= to avoid GCC warnings about always-false expressions.  */
#define INTEGER_TYPE_SIGNED(t) ((t) -1 <= 0)

/* The minimum and maximum values for the integer type T.
   Use ~ (t) 0, not -1, for portability to 1's complement hosts.  */
#define INTEGER_TYPE_MINIMUM(t) \
  (! INTEGER_TYPE_SIGNED (t) ? (t) 0 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))
#define INTEGER_TYPE_MAXIMUM(t) (~ (t) 0 - INTEGER_TYPE_MINIMUM (t))

#ifndef CHAR_MAX
# define CHAR_MAX INTEGER_TYPE_MAXIMUM (char)
#endif

#ifdef STAT_MACROS_BROKEN
# ifdef S_ISREG
#  undef S_ISREG
# endif
# ifdef S_ISDIR
#  undef S_ISDIR
# endif
#endif  /* STAT_MACROS_BROKEN.  */

#ifndef S_ISREG
# define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
#endif
#ifndef S_ISDIR
# define S_ISDIR(mode)  (((mode) & S_IFMT) == S_IFDIR)
#endif

#ifdef VMS
# include <fcntl.h>
# include <types.h>
# include <unixlib.h>
# include <unixio.h>
# include <perror.h>
/* Needed to use alloca on VMS.  */
# include <builtins.h>

extern int vms_use_mcr_command;
extern int vms_always_use_cmd_file;
extern int vms_gnv_shell;
extern int vms_comma_separator;
extern int vms_legacy_behavior;
extern int vms_unix_simulation;
#endif

#ifndef __attribute__
/* This feature is available in gcc versions 2.5 and later.  */
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
#  define __attribute__(x)
# endif
/* The __-protected variants of 'format' and 'printf' attributes
   are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
#  define __format__ format
#  define __printf__ printf
# endif
#endif
#define UNUSED  __attribute__ ((unused))

#if defined (STDC_HEADERS) || defined (__GNU_LIBRARY__)
# include <stdlib.h>
# include <string.h>
# define ANSI_STRING 1
#else   /* No standard headers.  */
# ifdef HAVE_STRING_H
#  include <string.h>
#  define ANSI_STRING 1
# else
#  include <strings.h>
# endif
# ifdef HAVE_MEMORY_H
#  include <memory.h>
# endif
# ifdef HAVE_STDLIB_H
#  include <stdlib.h>
# else
void *malloc (int);
void *realloc (void *, int);
void free (void *);

void abort (void) __attribute__ ((noreturn));
void exit (int) __attribute__ ((noreturn));
# endif /* HAVE_STDLIB_H.  */

#endif /* Standard headers.  */

/* These should be in stdlib.h.  Make sure we have them.  */
#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
# define EXIT_FAILURE 1
#endif

#ifndef  ANSI_STRING

/* SCO Xenix has a buggy macro definition in <string.h>.  */
#undef  strerror
#if !defined(__DECC)
char *strerror (int errnum);
#endif

#endif  /* !ANSI_STRING.  */
#undef  ANSI_STRING

#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#if HAVE_STDINT_H
# include <stdint.h>
#endif
#define FILE_TIMESTAMP uintmax_t

#if !defined(HAVE_STRSIGNAL)
char *strsignal (int signum);
#endif

/* ISDIGIT offers the following features:
   - Its arg may be any int or unsigned int; it need not be an unsigned char.
   - It's guaranteed to evaluate its argument exactly once.
      NOTE!  Make relies on this behavior, don't change it!
   - It's typically faster.
   POSIX 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
   only '0' through '9' are digits.  Prefer ISDIGIT to isdigit() unless
   it's important to use the locale's definition of 'digit' even when the
   host does not conform to POSIX.  */
#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)

/* Test if two strings are equal. Is this worthwhile?  Should be profiled.  */
#define streq(a, b) \
   ((a) == (b) || \
    (*(a) == *(b) && (*(a) == '\0' || !strcmp ((a) + 1, (b) + 1))))

/* Test if two strings are equal, but match case-insensitively on systems
   which have case-insensitive filesystems.  Should only be used for
   filenames!  */
#ifdef HAVE_CASE_INSENSITIVE_FS
# define patheq(a, b) \
    ((a) == (b) \
     || (tolower((unsigned char)*(a)) == tolower((unsigned char)*(b)) \
         && (*(a) == '\0' || !strcasecmp ((a) + 1, (b) + 1))))
#else
# define patheq(a, b) streq(a, b)
#endif

#define strneq(a, b, l) (strncmp ((a), (b), (l)) == 0)

#if (defined(__GNUC__) || defined(ENUM_BITFIELDS)) && !defined(NO_ENUM_BITFIELDS) /* bird: NO_ENUM_BITFIELDS */
# define ENUM_BITFIELD(bits)    :bits
#else
# define ENUM_BITFIELD(bits)
#endif

/* Handle gettext and locales.  */

#if HAVE_LOCALE_H
# include <locale.h>
#else
# define setlocale(category, locale)
#endif

#include <gettext.h>

#define _(msgid)            gettext (msgid)
#define N_(msgid)           gettext_noop (msgid)
#define S_(msg1,msg2,num)   ngettext (msg1,msg2,num)

/* This is needed for getcwd() and chdir(), on some W32 systems.  */
#if defined(HAVE_DIRECT_H)
# include <direct.h>
#endif

#ifdef WINDOWS32
# include <fcntl.h>
# include <malloc.h>
# define pipe(_p)        _pipe((_p), 512, O_BINARY)
# ifndef CONFIG_NEW_WIN_CHILDREN /* (only used by commands.c) */
# define kill(_pid,_sig) w32_kill((_pid),(_sig))
# endif
/* MSVC and Watcom C don't have ftruncate.  */
# if defined(_MSC_VER) || defined(__WATCOMC__)
#  define ftruncate(_fd,_len) _chsize(_fd,_len)
# endif
/* MinGW64 doesn't have _S_ISDIR.  */
# ifndef _S_ISDIR
#  define _S_ISDIR(m)  S_ISDIR(m)
# endif

void sync_Path_environment (void);
# ifndef CONFIG_NEW_WIN_CHILDREN /* (only used by commands.c) */
int w32_kill (pid_t pid, int sig);
# endif
int find_and_set_default_shell (const char *token);

/* indicates whether or not we have Bourne shell */
extern int no_default_sh_exe;

/* is default_shell unixy? */
extern int unixy_shell;

/* We don't have a preferred fixed value for LOCALEDIR.  */
# ifndef LOCALEDIR
#  define LOCALEDIR NULL
# endif

/* Include only the minimal stuff from windows.h.   */
# define WIN32_LEAN_AND_MEAN
#endif  /* WINDOWS32 */

#define ANY_SET(_v,_m)  (((_v)&(_m)) != 0)
#define NONE_SET(_v,_m) (! ANY_SET ((_v),(_m)))

#define MAP_NUL         0x0001
#define MAP_BLANK       0x0002
#define MAP_NEWLINE     0x0004
#define MAP_COMMENT     0x0008
#define MAP_SEMI        0x0010
#define MAP_EQUALS      0x0020
#define MAP_COLON       0x0040
#define MAP_PERCENT     0x0080
#define MAP_PIPE        0x0100
#define MAP_DOT         0x0200
#define MAP_COMMA       0x0400

/* These are the valid characters for a user-defined function.  */
#define MAP_USERFUNC    0x2000
/* This means not only a '$', but skip the variable reference.  */
#define MAP_VARIABLE    0x4000
/* The set of characters which are directory separators is OS-specific.  */
#define MAP_DIRSEP      0x8000

#ifdef VMS
# define MAP_VMSCOMMA   MAP_COMMA
#else
# define MAP_VMSCOMMA   0x0000
#endif

#define MAP_SPACE       (MAP_BLANK|MAP_NEWLINE)

/* Handle other OSs.
   To overcome an issue parsing paths in a DOS/Windows environment when
   built in a unix based environment, override the PATH_SEPARATOR_CHAR
   definition unless being built for Cygwin. */
#if defined(HAVE_DOS_PATHS) && !defined(__CYGWIN__)
# undef PATH_SEPARATOR_CHAR
# define PATH_SEPARATOR_CHAR ';'
# define MAP_PATHSEP    MAP_SEMI
#elif !defined(PATH_SEPARATOR_CHAR)
# if defined (VMS)
#  define PATH_SEPARATOR_CHAR (vms_comma_separator ? ',' : ':')
#  define MAP_PATHSEP    (vms_comma_separator ? MAP_COMMA : MAP_SEMI)
# else
#  define PATH_SEPARATOR_CHAR ':'
#  define MAP_PATHSEP    MAP_COLON
# endif
#elif PATH_SEPARATOR_CHAR == ':'
# define MAP_PATHSEP     MAP_COLON
#elif PATH_SEPARATOR_CHAR == ';'
# define MAP_PATHSEP     MAP_SEMI
#elif PATH_SEPARATOR_CHAR == ','
# define MAP_PATHSEP     MAP_COMMA
#else
# error "Unknown PATH_SEPARATOR_CHAR"
#endif

#define STOP_SET(_v,_m) ANY_SET(stopchar_map[(unsigned char)(_v)],(_m))

#define ISBLANK(c)      STOP_SET((c),MAP_BLANK)
#define ISSPACE(c)      STOP_SET((c),MAP_SPACE)
#define NEXT_TOKEN(s)   while (ISSPACE (*(s))) ++(s)
#define END_OF_TOKEN(s) while (! STOP_SET (*(s), MAP_SPACE|MAP_NUL)) ++(s)

#if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
# define SET_STACK_SIZE
#endif
#ifdef SET_STACK_SIZE
# include <sys/resource.h>
extern struct rlimit stack_limit;
#endif

#include "glob.h" /* bird double quotes */

#define NILF ((floc *)0)

#define CSTRLEN(_s)           (sizeof (_s)-1)
#define STRING_SIZE_TUPLE(_s) (_s), CSTRLEN(_s) /* The number of bytes needed to represent the largest integer as a string.  */
#define INTSTR_LENGTH         CSTRLEN ("18446744073709551616")

#define DEFAULT_TTYNAME "true"
#ifdef HAVE_TTYNAME
#  define TTYNAME(_f) ttyname (_f)
#else
#  define TTYNAME(_f) DEFAULT_TTYNAME
#endif



/* Specify the location of elements read from makefiles.  */
typedef struct
  {
    const char *filenm;
    unsigned long lineno;
    unsigned long offset;
  } floc;

#if defined (CONFIG_WITH_MATH) /* bird start */ \
 || defined (CONFIG_WITH_NANOTS) \
 || defined (CONFIG_WITH_FILE_SIZE) \
 || defined (CONFIG_WITH_PRINT_TIME_SWITCH)
# ifdef _MSC_VER
typedef __int64 big_int;
#  define BIG_INT_C(c)      (c ## LL)
typedef unsigned __int64 big_uint;
#  define BIG_UINT_C(c)     (c ## ULL)
# else
#  include <stdint.h>
typedef int64_t big_int;
#  define BIG_INT_C(c)      INT64_C(c)
typedef uint64_t big_uint;
#  define BIG_UINT_C(c)     UINT64_C(c)
# endif
#endif /* bird end */

const char *concat (unsigned int, ...);
void message (int prefix, size_t length, const char *fmt, ...)
              __attribute__ ((__format__ (__printf__, 3, 4)));
void error (const floc *flocp, size_t length, const char *fmt, ...)
            __attribute__ ((__format__ (__printf__, 3, 4)));
void fatal (const floc *flocp, size_t length, const char *fmt, ...)
            __attribute__ ((noreturn, __format__ (__printf__, 3, 4)));

#define O(_t,_a,_f)           _t((_a), 0, (_f))
#define OS(_t,_a,_f,_s)       _t((_a), strlen (_s), (_f), (_s))
#define OSS(_t,_a,_f,_s1,_s2) _t((_a), strlen (_s1) + strlen (_s2), \
                                 (_f), (_s1), (_s2))
#define OSSS(_t,_a,_f,_s1,_s2,_s3) _t((_a), strlen (_s1) + strlen (_s2) + strlen (_s3), \
                                      (_f), (_s1), (_s2), (_s3))
#define ON(_t,_a,_f,_n)       _t((_a), INTSTR_LENGTH, (_f), (_n))
#define ONN(_t,_a,_f,_n1,_n2) _t((_a), INTSTR_LENGTH*2, (_f), (_n1), (_n2))

#define OSN(_t,_a,_f,_s,_n)   _t((_a), strlen (_s) + INTSTR_LENGTH, \
                                 (_f), (_s), (_n))
#define ONS(_t,_a,_f,_n,_s)   _t((_a), INTSTR_LENGTH + strlen (_s), \
                                 (_f), (_n), (_s))

/* bird: more wrappers */
#define OSNN(_t,_a,_f,_s,_n1,_n2)   _t((_a), strlen (_s) + INTSTR_LENGTH + INTSTR_LENGTH, \
                                       (_f), (_s), (_n1), (_n2))                    /* bird */
#define OSNS(_t,_a,_f,_s1,_n,_s2)   _t((_a), strlen (_s1) + strlen (_s2) + INTSTR_LENGTH, \
                                      (_f), (_s1), (_n), (_s2))                     /* bird */
#define OSSSS(_t,_a,_f,_s1,_s2,_s3,_s4) _t((_a), strlen (_s1) + strlen (_s2) + strlen (_s3) + strlen (_s4), \
                                           (_f), (_s1), (_s2), (_s3), (_s4))        /* bird */
#define OSSN(_t,_a,_f,_s1,_s2,_n)       _t((_a), strlen (_s1) + strlen (_s2) + INTSTR_LENGTH, \
                                          (_f), (_s1), (_s2), (_n))                 /* bird */
#define OSSNS(_t,_a,_f,_s1,_s2,_n,_s3) _t((_a), strlen (_s1) + strlen (_s2) + strlen (_s3) + INTSTR_LENGTH, \
                                          (_f), (_s1), (_s2), (_n), (_s3))          /* bird */
#define ONNS(_t,_a,_f,_n1,_n2,_s1)     _t((_a), INTSTR_LENGTH * 2 + strlen (_s1), \
                                          (_f), (_n1), (_n2), (_s1))                /* bird */
#define ONNNS(_t,_a,_f,_n1,_n2,_n3,_s1) _t((_a), INTSTR_LENGTH * 3 + strlen (_s1), \
                                          (_f), (_n1), (_n2), (_n3), (_s1))         /* bird */

#define OUT_OF_MEM() O (fatal, NILF, _("virtual memory exhausted"))

void die (int) __attribute__ ((noreturn));
struct output;
void die_with_job_output (int, struct output *) __attribute__ ((noreturn));
void pfatal_with_name (const char *) __attribute__ ((noreturn));
void perror_with_name (const char *, const char *);
#define xstrlen(_s) ((_s)==NULL ? 0 : strlen (_s))
void *xmalloc (unsigned int);
void *xcalloc (unsigned int);
void *xrealloc (void *, unsigned int);
char *xstrdup (const char *);
char *xstrndup (const char *, unsigned int);
#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
void print_heap_stats (void);
#endif
char *find_next_token (const char **, unsigned int *);
char *next_token (const char *);
char *end_of_token (const char *);
#ifdef KMK
char *find_next_token_eos (const char **ptr, const char *eos, unsigned int *lengthptr);
char *find_next_file_token (const char **ptr, const char *eos, unsigned int *lengthptr);
#endif
#ifndef CONFIG_WITH_VALUE_LENGTH
void collapse_continuations (char *);
#else
char *collapse_continuations (char *, unsigned int);
#endif
#ifdef CONFIG_WITH_OPTIMIZATION_HACKS /* memchr is usually compiler intrinsic, thus faster. */
# define lindex(s, limit, c) ((char *)memchr((s), (c), (limit) - (s)))
#else
char *lindex (const char *, const char *, int);
#endif
int alpha_compare (const void *, const void *);
void print_spaces (unsigned int);
char *find_percent (char *);
const char *find_percent_cached (const char **);

#ifndef NO_ARCHIVES
int ar_name (const char *);
void ar_parse_name (const char *, char **, char **);
int ar_touch (const char *);
time_t ar_member_date (const char *);

typedef long int (*ar_member_func_t) (int desc, const char *mem, int truncated,
                                      long int hdrpos, long int datapos,
                                      long int size, long int date, int uid,
                                      int gid, unsigned int mode,
                                      const void *arg);

long int ar_scan (const char *archive, ar_member_func_t function, const void *arg);
int ar_name_equal (const char *name, const char *mem, int truncated);
#ifndef VMS
int ar_member_touch (const char *arname, const char *memname);
#endif
#endif

int dir_file_exists_p (const char *, const char *);
int file_exists_p (const char *);
int file_impossible_p (const char *);
void file_impossible (const char *);
const char *dir_name (const char *);
void print_dir_data_base (void);
void dir_setup_glob (glob_t *);
void hash_init_directories (void);
#if defined (KMK) && defined (KBUILD_OS_WINDOWS)
int utf16_regular_file_p(const wchar_t *pwszPath);
#endif

void define_default_variables (void);
void undefine_default_variables (void);
void set_default_suffixes (void);
void install_default_suffix_rules (void);
void install_default_implicit_rules (void);

void build_vpath_lists (void);
void construct_vpath_list (char *pattern, char *dirpath);
const char *vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr,
                          unsigned int* vpath_index, unsigned int* path_index);
int gpath_search (const char *file, unsigned int len);

void construct_include_path (const char **arg_dirs);

void user_access (void);
void make_access (void);
void child_access (void);

char *strip_whitespace (const char **begpp, const char **endpp);

void show_goal_error (void);


#ifdef CONFIG_WITH_ALLOC_CACHES /* bird start */
/* alloccache (misc.c) */

struct alloccache_free_ent
{
  struct alloccache_free_ent *next;
};

struct alloccache
{
  char *free_start;
  char *free_end;
  struct alloccache_free_ent *free_head;
  unsigned int size;
  unsigned int total_count;
  unsigned long alloc_count;
  unsigned long free_count;
  const char *name;
  struct alloccache *next;
  void *grow_arg;
  void *(*grow_alloc)(void *grow_arg, unsigned int size);
};

void alloccache_init (struct alloccache *cache, unsigned int size, const char *name,
                      void *(*grow_alloc)(void *grow_arg, unsigned int size), void *grow_arg);
void alloccache_term (struct alloccache *cache,
                      void (*term_free)(void *term_arg, void *ptr, unsigned int size), void *term_arg);
void alloccache_join (struct alloccache *cache, struct alloccache *eat);
void alloccache_print (struct alloccache *cache);
void alloccache_print_all (void);
struct alloccache_free_ent *alloccache_alloc_grow (struct alloccache *cache);
void alloccache_free (struct alloccache *cache, void *item);

/* Allocate an item. */
MY_INLINE void *
alloccache_alloc (struct alloccache *cache)
{
  struct alloccache_free_ent *f;
# ifndef CONFIG_WITH_ALLOCCACHE_DEBUG
  f = cache->free_head;
  if (f)
    cache->free_head = f->next;
  else if (cache->free_start != cache->free_end)
    {
      f = (struct alloccache_free_ent *)cache->free_start;
      cache->free_start += cache->size;
    }
  else
# endif
    f = alloccache_alloc_grow (cache);
  MAKE_STATS(cache->alloc_count++;);
  return f;
}

/* Allocate a cleared item. */
MY_INLINE void *
alloccache_calloc (struct alloccache *cache)
{
  void *item = alloccache_alloc (cache);
  memset (item, '\0', cache->size);
  return item;
}


/* the alloc caches */
extern struct alloccache dep_cache;
extern struct alloccache goaldep_cache;
extern struct alloccache nameseq_cache;
extern struct alloccache file_cache;
extern struct alloccache commands_cache;
extern struct alloccache variable_cache;
extern struct alloccache variable_set_cache;
extern struct alloccache variable_set_list_cache;

#endif /* CONFIG_WITH_ALLOC_CACHES - bird end*/


/* String caching  */
void strcache_init (void);
void strcache_print_stats (const char *prefix);
#ifndef CONFIG_WITH_STRCACHE2 /* bird */
int strcache_iscached (const char *str);
const char *strcache_add (const char *str);
const char *strcache_add_len (const char *str, unsigned int len);
#else  /* CONFIG_WITH_STRCACHE2 */

# include "strcache2.h"
extern struct strcache2 file_strcache;
extern const char *suffixes_strcached;

# define strcache_iscached(str)     strcache2_is_cached(&file_strcache, str)
# define strcache_add(str)          strcache2_add_file(&file_strcache, str, strlen (str))
# define strcache_add_len(str, len) strcache2_add_file(&file_strcache, str, len)
# define strcache_get_len(str)      strcache2_get_len(&file_strcache, str) /* FIXME: replace this and related checks ... */

#endif /* CONFIG_WITH_STRCACHE2 */

/* Guile support  */
int guile_gmake_setup (const floc *flocp);

/* Loadable object support.  Sets to the strcached name of the loaded file.  */
typedef int (*load_func_t)(const floc *flocp);
int load_file (const floc *flocp, const char **filename, int noerror);
void unload_file (const char *name);

/* We omit these declarations on non-POSIX systems which define _POSIX_VERSION,
   because such systems often declare them in header files anyway.  */

#if !defined (__GNU_LIBRARY__) && !defined (POSIX) && !defined (_POSIX_VERSION) && !defined(WINDOWS32)

long int atol ();
# ifndef VMS
long int lseek ();
# endif

# ifdef  HAVE_GETCWD
#  if !defined(VMS) && !defined(__DECC) && !defined(_MSC_VER) /* bird: MSC */
char *getcwd ();
#  endif
# else
char *getwd ();
#  define getcwd(buf, len)       getwd (buf)
# endif

#endif  /* Not GNU C library or POSIX.  */

#if !HAVE_STRCASECMP
# if HAVE_STRICMP
#  define strcasecmp stricmp
# elif HAVE_STRCMPI
#  define strcasecmp strcmpi
# else
/* Create our own, in misc.c */
int strcasecmp (const char *s1, const char *s2);
# endif
#endif

#if !HAVE_STRNCASECMP
# if HAVE_STRNICMP
#  define strncasecmp strnicmp
# elif HAVE_STRNCMPI
#  define strncasecmp strncmpi
# else
/* Create our own, in misc.c */
int strncasecmp (const char *s1, const char *s2, int n);
# endif
#endif

#define OUTPUT_SYNC_NONE    0
#define OUTPUT_SYNC_LINE    1
#define OUTPUT_SYNC_TARGET  2
#define OUTPUT_SYNC_RECURSE 3

#if !defined(_MSC_VER) /* bird */
/* Non-GNU systems may not declare this in unistd.h.  */
extern char **environ;
#endif

extern const floc *reading_file;
extern const floc **expanding_var;

extern unsigned short stopchar_map[];

extern int just_print_flag, silent_flag, ignore_errors_flag, keep_going_flag;
extern int print_data_base_flag, question_flag, touch_flag, always_make_flag;
extern int env_overrides, no_builtin_rules_flag, no_builtin_variables_flag;
extern int print_version_flag, print_directory_flag, check_symlink_flag;
extern int warn_undefined_variables_flag, trace_flag, posix_pedantic;
extern int not_parallel, second_expansion, clock_skew_detected;
extern int rebuilding_makefiles, one_shell, output_sync, verify_flag;

#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION /* bird start */
extern int second_target_expansion;
#endif
#ifdef CONFIG_PRETTY_COMMAND_PRINTING
extern int pretty_command_printing;
#endif
#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
extern int print_time_min, print_time_width;
#endif
#if defined (CONFIG_WITH_MAKE_STATS) || defined (CONFIG_WITH_MINIMAL_STATS)
extern int make_expensive_statistics;
#endif                                  /* bird end */

extern const char *default_shell;

/* can we run commands via 'sh -c xxx' or must we use batch files? */
extern int batch_mode_shell;

/* Resetting the command script introduction prefix character.  */
#define RECIPEPREFIX_NAME          ".RECIPEPREFIX"
#define RECIPEPREFIX_DEFAULT       '\t'
extern char cmd_prefix;

extern unsigned int job_slots;
#ifndef NO_FLOAT
extern double max_load_average;
#else
extern int max_load_average;
#endif

#ifdef WINDOWS32
extern char *program;
#else
extern const char *program;
#endif

#ifdef VMS
const char *vms_command (const char *argv0);
const char *vms_progname (const char *argv0);

void vms_exit (int);
# define _exit(foo) vms_exit(foo)
# define exit(foo) vms_exit(foo)

extern char *program_name;

void
set_program_name (const char *arv0);

int
need_vms_symbol (void);

int
create_foreign_command (const char *command, const char *image);

int
vms_export_dcl_symbol (const char *name, const char *value);

int
vms_putenv_symbol (const char *string);

void
vms_restore_symbol (const char *string);

#endif

void remote_setup (void);
void remote_cleanup (void);
int start_remote_job_p (int);
int start_remote_job (char **, char **, int, int *, int *, int *);
int remote_status (int *, int *, int *, int);
void block_remote_children (void);
void unblock_remote_children (void);
int remote_kill (int id, int sig);
void print_variable_data_base (void);
void print_vpath_data_base (void);

extern char *starting_directory;
extern unsigned int makelevel;
extern char *version_string, *remote_description, *make_host;

extern unsigned int commands_started;

extern int handling_fatal_signal;


#ifndef MIN
#define MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
#endif
#ifndef MAX
#define MAX(_a,_b) ((_a)>(_b)?(_a):(_b))
#endif


#define MAKE_SUCCESS 0
#define MAKE_TROUBLE 1
#define MAKE_FAILURE 2

/* Set up heap debugging library dmalloc.  */

#ifdef HAVE_DMALLOC_H
#include <dmalloc.h>
#endif

#ifndef initialize_main
# ifdef __EMX__
#  define initialize_main(pargc, pargv) \
                          { _wildcard(pargc, pargv); _response(pargc, pargv); }
# else
#  define initialize_main(pargc, pargv)
# endif
#endif

#ifdef __EMX__
# if !defined chdir
#  define chdir _chdir2
# endif
# if !defined getcwd
#  define getcwd _getcwd2
# endif

/* NO_CHDIR2 causes make not to use _chdir2() and _getcwd2() instead of
   chdir() and getcwd(). This avoids some error messages for the
   make testsuite but restricts the drive letter support. */
# ifdef NO_CHDIR2
#  warning NO_CHDIR2: usage of drive letters restricted
#  undef chdir
#  undef getcwd
# endif
#endif

#ifndef initialize_main
# define initialize_main(pargc, pargv)
#endif


/* Some systems (like Solaris, PTX, etc.) do not support the SA_RESTART flag
   properly according to POSIX.  So, we try to wrap common system calls with
   checks for EINTR.  Note that there are still plenty of system calls that
   can fail with EINTR but this, reportedly, gets the vast majority of
   failure cases.  If you still experience failures you'll need to either get
   a system where SA_RESTART works, or you need to avoid -j.  */

#define EINTRLOOP(_v,_c)   while (((_v)=_c)==-1 && errno==EINTR)

/* While system calls that return integers are pretty consistent about
   returning -1 on failure and setting errno in that case, functions that
   return pointers are not always so well behaved.  Sometimes they return
   NULL for expected behavior: one good example is readdir() which returns
   NULL at the end of the directory--and _doesn't_ reset errno.  So, we have
   to do it ourselves here.  */

#define ENULLLOOP(_v,_c)   do { errno = 0; (_v) = _c; } \
                           while((_v)==0 && errno==EINTR)


#if defined(__EMX__) && defined(CONFIG_WITH_OPTIMIZATION_HACKS) /* bird: saves 40-100ms on libc. */
static inline void *__my_rawmemchr (const void *__s, int __c);
#undef strchr
#define strchr(s, c) \
  (__extension__ (__builtin_constant_p (c)				      \
		  ? ((c) == '\0'					      \
		     ? (char *) __my_rawmemchr ((s), (c))			      \
		     : __my_strchr_c ((s), ((c) & 0xff) << 8))		      \
		  : __my_strchr_g ((s), (c))))
static inline char *__my_strchr_c (const char *__s, int __c)
{
  register unsigned long int __d0;
  register char *__res;
  __asm__ __volatile__
    ("1:\n\t"
     "movb	(%0),%%al\n\t"
     "cmpb	%%ah,%%al\n\t"
     "je	2f\n\t"
     "leal	1(%0),%0\n\t"
     "testb	%%al,%%al\n\t"
     "jne	1b\n\t"
     "xorl	%0,%0\n"
     "2:"
     : "=r" (__res), "=&a" (__d0)
     : "0" (__s), "1" (__c),
       "m" ( *(struct { char __x[0xfffffff]; } *)__s)
     : "cc");
  return __res;
}

static inline char *__my_strchr_g (__const char *__s, int __c)
{
  register unsigned long int __d0;
  register char *__res;
  __asm__ __volatile__
    ("movb	%%al,%%ah\n"
     "1:\n\t"
     "movb	(%0),%%al\n\t"
     "cmpb	%%ah,%%al\n\t"
     "je	2f\n\t"
     "leal	1(%0),%0\n\t"
     "testb	%%al,%%al\n\t"
     "jne	1b\n\t"
     "xorl	%0,%0\n"
     "2:"
     : "=r" (__res), "=&a" (__d0)
     : "0" (__s), "1" (__c),
       "m" ( *(struct { char __x[0xfffffff]; } *)__s)
     : "cc");
  return __res;
}

static inline void *__my_rawmemchr (const void *__s, int __c)
{
  register unsigned long int __d0;
  register unsigned char *__res;
  __asm__ __volatile__
    ("cld\n\t"
     "repne; scasb\n\t"
     : "=D" (__res), "=&c" (__d0)
     : "a" (__c), "0" (__s), "1" (0xffffffff),
       "m" ( *(struct { char __x[0xfffffff]; } *)__s)
     : "cc");
  return __res - 1;
}

#undef memchr
#define memchr(a,b,c) __my_memchr((a),(b),(c))
static inline void *__my_memchr (__const void *__s, int __c, size_t __n)
{
  register unsigned long int __d0;
  register unsigned char *__res;
  if (__n == 0)
    return NULL;
  __asm__ __volatile__
    ("repne; scasb\n\t"
     "je	1f\n\t"
     "movl	$1,%0\n"
     "1:"
     : "=D" (__res), "=&c" (__d0)
     : "a" (__c), "0" (__s), "1" (__n),
       "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
     : "cc");
  return __res - 1;
}

#endif /* __EMX__ (bird) */

#ifdef CONFIG_WITH_IF_CONDITIONALS
extern int expr_eval_if_conditionals(const char *line, const floc *flocp);
extern char *expr_eval_to_string(char *o, const char *expr);
#endif

#ifdef KMK
extern char *abspath(const char *name, char *apath);
extern char *func_breakpoint(char *o, char **argv, const char *funcname);
# ifdef KBUILD_OS_WINDOWS
extern void dir_cache_invalid_after_job (void);
extern void dir_cache_invalid_all (void);
extern void dir_cache_invalid_all_and_close_dirs (int including_root);
extern void dir_cache_invalid_missing (void);
extern int dir_cache_volatile_dir (const char *dir);
extern int dir_cache_deleted_directory(const char *pszDir);
# endif
#endif

#if defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_PRINT_TIME_SWITCH) || defined(CONFIG_WITH_KMK_BUILTIN_STATS)
/* misc.c */
extern big_int nano_timestamp (void);
extern int format_elapsed_nano (char *buf, size_t size, big_int ts);
#endif

#ifdef KMK
/* main.c */
extern mode_t g_fUMask;
#endif