-
Notifications
You must be signed in to change notification settings - Fork 0
/
links.h
5192 lines (4428 loc) · 147 KB
/
links.h
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
/* links.h
* (c) 2002 Mikulas Patocka, Karel 'Clock' Kulhavy, Petr 'Brain' Kulhavy,
* Martin 'PerM' Pergel
* This file is a part of the Links program, released under GPL.
*/
/*
* WARNING: this file MUST be C++ compatible. this means:
* no implicit conversions from void *:
* BAD: unsigned char *c = mem_alloc(4);
* GOOD: unsigned char *c = (unsigned char *)mem_alloc(4);
* no implicit char * -> unsigned char * conversions:
* BAD: unsigned char *c = stracpy("A");
* GOOD: unsigned char *c = stracpy((unsigned char *)"A");
* no implicit unsigned char * -> char * conversions:
* BAD: unsigned char *x, *y, *z; z = strcpy(x, y);
* BAD: l = strlen(x);
* GOOD: unsigned char *x, *y; z = (unsigned char *)strcpy((char *)x, (char *)y);
* GOOD: l = strlen((char *)x);
* don't use C++ keywords (like template)
* if there is struct X, you cannot use variable X or typedef X
* (this applies to typedef ip as well -- don't use it!)
*
* IF YOU WRITE ANYTHING NEW TO THIS FILE, try compiling this file in c++
* to make sure that you didn't break anything:
* g++ -DHAVE_CONFIG_H -x c++ links.h
*/
#ifndef LINKS_H
#define LINKS_H
#include "cfg.h"
#include "com-defs.h"
#define LINKS_COPYRIGHT "(C) 1999 - 2021 Mikulas Patocka\n(C) 2000 - 2021 Petr Kulhavy, Karel Kulhavy, Martin Pergel"
#define LINKS_COPYRIGHT_8859_1 "(C) 1999 - 2021 Mikul\341s Patocka\n(C) 2000 - 2021 Petr Kulhav\375, Karel Kulhav\375, Martin Pergel"
#define LINKS_COPYRIGHT_8859_2 "(C) 1999 - 2021 Mikul\341\271 Pato\350ka\n(C) 2000 - 2021 Petr Kulhav\375, Karel Kulhav\375, Martin Pergel"
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
#if ((__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))) && \
!(defined(__clang__) || defined(__llvm__) || defined(__ICC) || defined(__OPEN64__) || defined(__PATHSCALE__) || defined(__PGI) || defined(__PGIC__)) && \
defined(__OPTIMIZE__) && !defined(__OPTIMIZE_SIZE__) && \
!(defined(__arm__) && defined(__thumb__) && !defined(__thumb2__)) /* avoid gcc bug */
#pragma GCC optimize ("-ftree-vectorize", "-ffast-math")
#endif
#endif
#include "os_dep.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H_X
#include <stdlib.h>
#endif
#include <stdarg.h>
#include <stddef.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_BSD_STRING_H
#include <bsd/string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <errno.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#include <sys/types.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifndef __USE_XOPEN
#define U_X
#define __USE_XOPEN
#endif
#ifndef _XOPEN_SOURCE
#define X_S
#define _XOPEN_SOURCE 5 /* The 5 is a kludge to get a strptime() prototype in NetBSD */
#endif
#ifdef TIME_WITH_SYS_TIME
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#else
#if defined(TM_IN_SYS_TIME) && defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#elif defined(HAVE_TIME_H)
#include <time.h>
#endif
#endif
#ifdef X_S
#undef _XOPEN_SOURCE
#endif
#ifdef U_X
#undef __USE_XOPEN
#endif
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if defined(HAVE_LINUX_FALLOC_H) && !defined(FALLOC_FL_KEEP_SIZE)
#include <linux/falloc.h>
#endif
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#if defined(HAVE_DIRENT_H) || defined(__CYGWIN__)
#if defined(__CYGWIN__) && !defined(NAME_MAX)
#define NAME_MAX 255
#endif
#include <dirent.h>
#elif defined(HAVE_SYS_NDIR_H)
#include <sys/ndir.h>
#elif defined(HAVE_SYS_DIR_H)
#include <sys/dir.h>
#ifndef dirent
#define dirent direct
#endif
#elif defined(HAVE_NDIR_H)
#include <ndir.h>
#endif
#include <signal.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_SYS_CYGWIN_H
#include <sys/cygwin.h>
#endif
#ifdef HAVE_UWIN_H
#include <uwin.h>
#endif
#ifdef HAVE_INTERIX_INTERIX_H
#include <interix/interix.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#ifdef HAVE_PROCESS_H
#include <process.h>
#endif
#ifdef HAVE_CYGWIN_PROCESS_H
#include <cygwin/process.h>
#endif
#ifdef HAVE_CYGWIN_VERSION_H
#include <cygwin/version.h>
#endif
#ifdef HAVE_UNIXLIB_H
#include <unixlib.h>
#endif
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#ifdef HAVE_SEARCH_H
#include <search.h>
#endif
#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#else
#ifdef HAVE_NETINET_IN_SYSTEM_H
#include <netinet/in_system.h>
#endif
#endif
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef HAVE_NETINET_IP_H
#include <netinet/ip.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_SSL
#ifdef HAVE_OPENSSL
#if !defined(NO_SSL_CERTIFICATES) && defined(HAVE_OPENSSL_X509V3_H) && defined(HAVE_ASN1_STRING_TO_UTF8)
#define HAVE_SSL_CERTIFICATES
#endif
#if defined(OPENVMS) && defined(__VAX)
#define OPENSSL_NO_SHA512
#endif
#include <openssl/ssl.h>
#include <openssl/rand.h>
#ifdef HAVE_SSL_CERTIFICATES
#include <openssl/x509v3.h>
#endif
#include <openssl/err.h>
#include <openssl/crypto.h>
#ifdef HAVE_SSL_GET1_SESSION
#define SSL_SESSION_RESUME
#endif
#if defined(HAVE_CONFIG_VMS_H) && defined(OPENSSL_VERSION_NUMBER)
#if OPENSSL_VERSION_NUMBER >= 0x10100002L
#define HAVE_CRYPTO_SET_MEM_FUNCTIONS_2
#endif
#define HAVE_CRYPTO_SET_MEM_FUNCTIONS_1
#endif
#endif
#ifdef HAVE_NSS
#include <nss_compat_ossl/nss_compat_ossl.h>
#endif
#endif
#if defined(HAVE_CRYPTO_SET_MEM_FUNCTIONS_1) && defined(HAVE_CRYPTO_SET_MEM_FUNCTIONS_2)
#undef HAVE_CRYPTO_SET_MEM_FUNCTIONS_1
#undef HAVE_CRYPTO_SET_MEM_FUNCTIONS_2
#endif
#if defined(HAVE_CRYPTO_SET_MEM_FUNCTIONS_1) || defined(HAVE_CRYPTO_SET_MEM_FUNCTIONS_2)
#define HAVE_CRYPTO_SET_MEM_FUNCTIONS
#endif
#if defined(G)
#if defined(HAVE_PNG_H)
#define PNG_THREAD_UNSAFE_OK
#include <png.h>
#elif defined(HAVE_LIBPNG_PNG_H)
#define PNG_THREAD_UNSAFE_OK
#include <libpng/png.h>
#endif /* #if defined(HAVE_PNG_H) */
#ifndef png_jmpbuf
#define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
#endif
#endif /* #if defined(G) */
#ifdef HAVE_SETJMP_H
#ifndef _SETJMP_H
#include <setjmp.h>
#endif /* _SETJMP_H */
#endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#elif defined(HAVE_SGTTY_H)
#include <sgtty.h>
#endif
#if defined(HAVE_POLL_H) && defined(HAVE_POLL) && !defined(INTERIX) && !defined(__HOS_AIX__)
#define USE_POLL
#include <poll.h>
#endif
#if (defined(HAVE_EVENT_H) || defined(HAVE_EV_EVENT_H)) && (defined(HAVE_LIBEVENT) || defined(HAVE_LIBEV)) && !defined(OPENVMS) && !defined(BEOS) && !defined(OPENVMS) && !defined(DOS)
#if defined(HAVE_EVENT_H)
#include <event.h>
#else
#include <ev-event.h>
#endif
#define USE_LIBEVENT
#endif
#ifdef HAVE_OPENMP
#include <omp.h>
#define SMP_ALIGN 256
#else
#define omp_get_num_threads() 1
#define omp_get_thread_num() 0
#define SMP_ALIGN 1
#endif
#if (defined(__alpha__) || defined(__alpha)) && !defined(__alpha_bwx__)
#define OPENMP_NONATOMIC 1
#else
#define OPENMP_NONATOMIC 0
#endif
#ifdef HAVE_LONG_LONG
#define longlong long long
#define ulonglong unsigned long long
#else
#define longlong double
#define ulonglong double
#endif
#if defined(__INTERIX) && defined(HAVE_STRTOQ)
extern quad_t
#if defined(__cdecl) || defined(_MSC_VER)
__cdecl
#endif
strtoq(const char *, char **, int);
#endif
#define stringify_internal_error(arg) #arg
#define stringify(arg) stringify_internal_error(arg)
#define array_elements(a) (sizeof(a) / sizeof(*a))
#include "os_depx.h"
#include "setup.h"
#define LINKS_2
#ifdef HAVE_POINTER_COMPARISON_BUG
#define DUMMY ((void *)1L)
#else
#define DUMMY ((void *)-1L)
#endif
#define cast_const_char (const char *)
#define cast_char (char *)
#define cast_uchar (unsigned char *)
/* avoid unreachable code warnings */
static inline int value_0(void)
{
return 0;
}
static inline int value_1(void)
{
return 1;
}
#if defined(C_LITTLE_ENDIAN)
#define big_endian (value_0())
#elif defined(C_BIG_ENDIAN)
#define big_endian (value_1())
#else
#define big_endian (htons(0x1234) == 0x1234)
#endif
#ifdef OPENVMS
#define RET_OK 1 /* SS$_NORMAL */
#define RET_ERROR 3586 /* SS$_LINEABRT */
#define RET_SIGNAL 2096 /* SS$_CANCEL */
#define RET_SYNTAX 20 /* SS$_BADPARAM */
#define RET_FATAL 44 /* SS$_ABORT */
#define RET_INTERNAL 44 /* SS$_ABORT */
#else
#define RET_OK 0
#define RET_ERROR 1
#define RET_SIGNAL 2
#define RET_SYNTAX 3
#define RET_FATAL 4
#define RET_INTERNAL 127
#endif
#ifndef HAVE_SNPRINTF
int my_snprintf(char *, int n, char *format, ...) PRINTF_FORMAT(3, 4);
#define snprintf my_snprintf
#endif
#ifndef HAVE_RAISE
int raise(int);
#endif
#ifndef HAVE_GETTIMEOFDAY
struct timeval {
long tv_sec;
long tv_usec;
};
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
int gettimeofday(struct timeval *tv, struct timezone *tz);
#endif
#ifndef HAVE_TEMPNAM
char *tempnam(const char *dir, const char *pfx);
#endif
#ifndef HAVE_STRDUP
char *strdup(const char *s);
#endif
#ifndef HAVE_STRTOL
long strtol(const char *, char **, int);
#endif
#ifndef HAVE_STRTOUL
unsigned long strtoul(const char *, char **, int);
#endif
#ifndef HAVE_STRTOD
double strtod(const char *nptr, char **endptr);
#endif
#ifndef HAVE_STRLEN
size_t strlen(const char *s);
#endif
#ifndef HAVE_STRCPY
char *strcpy(char *dst, const char *src);
#endif
#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t max);
#endif
#ifndef HAVE_STRNCPY
char *strncpy(char *dst, const char *src, size_t len);
#endif
#ifndef HAVE_STRCHR
char *strchr(const char *s, int c);
#endif
#ifndef HAVE_STRRCHR
char *strrchr(const char *s, int c);
#endif
#ifndef HAVE_STRCMP
int strcmp(const char *s1, const char *s2);
#endif
#ifndef HAVE_STRNCMP
int strncmp(const char *s1, const char *s2, size_t n);
#endif
#ifndef HAVE_STRCSPN
size_t strcspn(const char *s, const char *reject);
#endif
#ifndef HAVE_STRSPN
size_t strspn(const char *s, const char *accept);
#endif
#ifndef HAVE_STRSTR
char *strstr(const char *haystack, const char *needle);
#endif
#ifndef HAVE_MEMCHR
void *memchr(const void *s, int c, size_t length);
#endif
#ifndef HAVE_MEMRCHR
void *memrchr(const void *s, int c, size_t length);
#endif
#ifndef HAVE_MEMCMP
int memcmp(const void *, const void *, size_t);
#endif
#ifndef HAVE_MEMCPY
void *memcpy(void *, const void *, size_t);
#endif
#ifndef HAVE_MEMMOVE
void *memmove(void *, const void *, size_t);
#endif
#ifndef HAVE_MEMSET
void *memset(void *, int, size_t);
#endif
#ifndef HAVE_MEMMEM
void *memmem(const void *haystack, size_t hs, const void *needle, size_t ns);
#endif
#ifndef HAVE_STRERROR
char *strerror(int);
#endif
#define EINTRLOOPX(ret_, call_, x_) \
do { \
(ret_) = (call_); \
} while ((ret_) == (x_) && errno == EINTR)
#define EINTRLOOP(ret_, call_) EINTRLOOPX(ret_, call_, -1)
#define ENULLLOOP(ret_, call_) \
do { \
errno = 0; \
(ret_) = (call_); \
} while (!(ret_) && errno == EINTR)
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(__BIONIC__)
static inline int do_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
int r;
r = pthread_sigmask(how, set, oset);
if (r) {
errno = r;
return -1;
}
return 0;
}
#elif defined(HAVE_SIGPROCMASK)
#define do_sigprocmask sigprocmask
#else
#ifdef sigset_t
#undef sigset_t
#endif
#define sigset_t int
#ifndef SIG_BLOCK
#define SIG_BLOCK 0
#endif
#ifndef SIG_SETMASK
#define SIG_SETMASK 2
#endif
static inline int do_sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
sigset_t old = 0;
#if defined(HAVE_SIGBLOCK) && defined(HAVE_SIGSETMASK)
switch (how) {
case SIG_BLOCK:
old = sigblock(*set);
break;
case SIG_SETMASK:
old = sigsetmask(*set);
break;
}
#endif
if (oset) *oset = old;
return 0;
}
#ifdef sigdelset
#undef sigdelset
#endif
#define sigdelset(x, s) (*(x) &= ~(1 << (s)), 0)
#ifndef HAVE_SIGDELSET
#define HAVE_SIGDELSET 1
#endif
#ifdef HAVE_SIGFILLSET
#undef HAVE_SIGFILLSET
#endif
#endif
#ifdef HAVE_SIGFILLSET
static inline void sig_fill_set(sigset_t *set)
{
sigfillset(set);
}
#else
static inline void sig_fill_set(sigset_t *set)
{
memset(set, -1, sizeof(sigset_t));
}
#endif
#define option option_dirty_workaround_for_name_clash_with_include_on_cygwin
#define table table_dirty_workaround_for_name_clash_with_libraries_on_macos
#define scroll scroll_dirty_workaround_for_name_clash_with_libraries_on_macos
#define list list_dirty_workaround_for_name_clash_in_stl_with_class_list
#ifndef G
#define F 0
#else
extern int F;
#endif
#if defined(DEBUG)
#if defined(G)
#define NO_GFX do {if (F) internal_error("call to text-only function");} while (0)
#define NO_TXT do {if (!F) internal_error("call to graphics-only function");} while (0)
#else
#define NO_GFX do {} while (0)
#define NO_TXT this_should_not_be_compiled
#endif
#else
#define NO_GFX do {} while (0)
#define NO_TXT do {} while (0)
#endif
#ifndef G
#define gf_val(x, y) (x)
#define GF(x)
#else
#define gf_val(x, y) (F ? (y) : (x))
#define GF(x) if (F) {x;}
#endif
#define MAX_STR_LEN 1024
#define BIN_SEARCH(entries, eq, ab, key, result) \
{ \
int s_ = 0, e_ = (entries) - 1; \
(result) = -1; \
while (s_ <= e_) { \
int m_ = (int)(((unsigned)s_ + (unsigned)e_) / 2); \
if (eq((m_), (key))) { \
(result) = m_; \
break; \
} \
if (ab((m_), (key))) e_ = m_ - 1; \
else s_ = m_ + 1; \
} \
} \
/* error.c */
#if defined(DOS)
#define ANSI_BELL "\007"
#define ANSI_SET_BOLD ""
#define ANSI_CLEAR_BOLD ""
#else
#define ANSI_BELL "\007"
#define ANSI_SET_BOLD "\033[1m"
#define ANSI_CLEAR_BOLD "\033[0m"
#endif
#ifdef LEAK_DEBUG
extern unsigned alloc_overhead;
#else
#define alloc_overhead 0
#endif
void *do_not_optimize_here(void *p);
void init_heap(void);
void check_memory_leaks(void);
void error(const char *, ...) PRINTF_FORMAT(1, 2);
void fatal_exit(const char *, ...) PRINTF_FORMAT(1, 2) ATTR_NORETURN;
void debug_msg(const char *, ...) PRINTF_FORMAT(1, 2);
void int_error(const char *, ...) PRINTF_FORMAT(1, 2)
#ifndef NO_IE
ATTR_NORETURN
#endif
;
extern int errline;
extern unsigned char *errfile;
#define internal_error errfile = cast_uchar __FILE__, errline = __LINE__, int_error
#define debug errfile = cast_uchar __FILE__, errline = __LINE__, debug_msg
void fatal_tty_exit(void);
#ifdef __ICC
/* ICC OpenMP bug */
#define overalloc_condition 0
#else
#define overalloc_condition 1
#endif
#define overalloc_at(f, l) \
do { \
fatal_exit("ERROR: attempting to allocate too large block at %s:%d", f, l);\
} while (overalloc_condition) /* while (1) is not a typo --- it's here to allow the compiler
that doesn't know that fatal_exit doesn't return to do better
optimizations */
#define overalloc() overalloc_at(__FILE__, __LINE__)
#define overflow() \
do { \
fatal_exit("ERROR: arithmetic overflow at %s:%d", __FILE__, __LINE__);\
} while (1)
static inline int test_int_overflow(int x, int y, int *result)
{
#ifdef HAVE___BUILTIN_ADD_OVERFLOW
return __builtin_add_overflow(x, y, result);
#else
int z = *result = (int)((unsigned)(x) + (unsigned)(y));
return ~((unsigned)(x) ^ (unsigned)(y)) & ((unsigned)(x) ^ ((unsigned)(z))) & (1U << (sizeof(unsigned) * 8 - 1));
#endif
}
static inline int safe_add_function(int x, int y, unsigned char *file, int line)
{
int result;
#if !defined(HAVE___BUILTIN_ADD_OVERFLOW) && defined(HAVE_GCC_ASSEMBLER) && (defined(__i386__) || defined(__x86_64__))
unsigned char ovf;
__asm__ ("addl %2, %0; seto %1":"=r"(result),
#if defined(__PATHSCALE__) || defined(__OPEN64__)
"=q" /* a bug in the PathScale and Open64 compiler */
#else
"=qm"
#endif
(ovf):"g"(y),"0"(x));
if (ovf)
fatal_exit("ERROR: arithmetic overflow at %s:%d: %d + %d", file, line, (x), (y));
return result;
#endif
if (test_int_overflow(x, y, &result))
fatal_exit("ERROR: arithmetic overflow at %s:%d: %d + %d", file, line, (x), (y));
return result;
}
#define safe_add(x, y) safe_add_function(x, y, (unsigned char *)__FILE__, __LINE__)
#ifdef HAVE_GCC_ASSEMBLER
static inline void *memory_barrier(void *p)
{
void *o;
__asm__ volatile("" : "=r"(o) : "0"(p) : "memory");
return o;
}
#else
#define memory_barrier(p) do_not_optimize_here(p)
#endif
#ifdef LEAK_DEBUG
extern my_uintptr_t mem_amount;
extern my_uintptr_t mem_blocks;
#endif
#ifdef LEAK_DEBUG
void *debug_mem_alloc(unsigned char *, int, size_t, int);
void *debug_mem_calloc(unsigned char *, int, size_t, int);
void debug_mem_free(unsigned char *, int, void *);
void *debug_mem_realloc(unsigned char *, int, void *, size_t, int);
void set_mem_comment(void *, unsigned char *, int);
unsigned char *get_mem_comment(void *);
#define mem_alloc(x) debug_mem_alloc((unsigned char *)__FILE__, __LINE__, x, 0)
#define mem_calloc(x) debug_mem_calloc((unsigned char *)__FILE__, __LINE__, x, 0)
#define mem_free(x) debug_mem_free((unsigned char *)__FILE__, __LINE__, x)
#define mem_realloc(x, y) debug_mem_realloc((unsigned char *)__FILE__, __LINE__, x, y, 0)
#define mem_alloc_mayfail(x) debug_mem_alloc((unsigned char *)__FILE__, __LINE__, x, 1)
#define mem_calloc_mayfail(x) debug_mem_calloc((unsigned char *)__FILE__, __LINE__, x, 1)
#define mem_realloc_mayfail(x, y) debug_mem_realloc((unsigned char *)__FILE__, __LINE__, x, y, 1)
#else
void *mem_alloc_(size_t size, int mayfail);
void *mem_calloc_(size_t size, int mayfail);
void mem_free(void *p);
void *mem_realloc_(void *p, size_t size, int mayfail);
#define mem_alloc(x) mem_alloc_(x, 0)
#define mem_calloc(x) mem_calloc_(x, 0)
#define mem_realloc(x, y) mem_realloc_(x, y, 0)
#define mem_alloc_mayfail(x) mem_alloc_(x, 1)
#define mem_calloc_mayfail(x) mem_calloc_(x, 1)
#define mem_realloc_mayfail(x, y) mem_realloc_(x, y, 1)
static inline void *debug_mem_alloc(unsigned char *f, int l, size_t s, int mayfail) { return mem_alloc_(s, mayfail); }
static inline void *debug_mem_calloc(unsigned char *f, int l, size_t s, int mayfail) { return mem_calloc_(s, mayfail); }
static inline void debug_mem_free(unsigned char *f, int l, void *p) { mem_free(p); }
static inline void *debug_mem_realloc(unsigned char *f, int l, void *p, size_t s, int mayfail) { return mem_realloc_(p, s, mayfail); }
static inline void set_mem_comment(void *p, unsigned char *c, int l) {}
static inline unsigned char *get_mem_comment(void *p){return (unsigned char *)"";}
#endif
#if !(defined(LEAK_DEBUG) && defined(LEAK_DEBUG_LIST))
unsigned char *memacpy(const unsigned char *src, size_t len);
unsigned char *stracpy(const unsigned char *src);
#else
unsigned char *debug_memacpy(unsigned char *f, int l, const unsigned char *src, size_t len);
#define memacpy(s, l) debug_memacpy((unsigned char *)__FILE__, __LINE__, s, l)
unsigned char *debug_stracpy(unsigned char *f, int l, const unsigned char *src);
#define stracpy(s) debug_stracpy((unsigned char *)__FILE__, __LINE__, s)
#endif
#if defined(LEAK_DEBUG) && defined(LEAK_DEBUG_LIST)
#define GTM_MOST_ALLOCATED 0
#define GTM_LARGEST_BLOCKS 1
unsigned char *get_top_memory(int mode, unsigned n);
#endif
#if !defined(HAVE_SIGSETJMP) || !defined(HAVE_SETJMP_H)
#ifdef OOPS
#undef OOPS
#endif
#endif
#ifndef OOPS
#define pr(code) if (1) {code;} else
static inline void nopr(void) {}
static inline void xpr(void) {}
#else
sigjmp_buf *new_stack_frame(void);
void xpr(void);
#define pr(code) if (!sigsetjmp(*new_stack_frame(), 1)) {do {code;} while (0); xpr();} else
void nopr(void);
#endif
/* inline */
#define ALLOC_GR 0x040 /* must be power of 2 */
#define get_struct_(ptr, struc, entry) ((struc *)((char *)(ptr) - offsetof(struc, entry)))
#define get_struct(ptr, struc, entry) ((void)(&get_struct_(ptr, struc, entry)->entry == (ptr)), get_struct_(ptr, struc, entry))
struct list_head {
struct list_head *next;
struct list_head *prev;
};
#ifdef DEBUG
static inline void corrupted_list_entry(unsigned char *file, int line, const char *msg, struct list_head *l, struct list_head *a)
{
errfile = file;
errline = line;
int_error("%s %p, %p, %p, %p, %p, %p", msg, l, l->next, l->prev, l->next->prev, l->prev->next, a);
}
#define verify_list_entry(e) ((e)->next->prev != (e) || (e)->prev->next != (e) ? corrupted_list_entry(cast_uchar __FILE__, __LINE__, "corrupted link pointers", e, NULL) : (void)0)
#define verify_double_add(l, e) ((l) == (e) || (l)->next == (e) || (l)->prev == (e) ? corrupted_list_entry(cast_uchar __FILE__, __LINE__, "double add", l, e) : (void)0)
#else
#define verify_list_entry(e) ((void)0)
#define verify_double_add(l, e) ((void)0)
#endif
#define list_struct(ptr, struc) get_struct(ptr, struc, list_entry)
#define init_list(x) do { (x).next = &(x); (x).prev = &(x); } while (0)
#define list_empty(x) (verify_list_entry(&x), (x).next == &(x))
#define del_list_entry(x) do { verify_list_entry(x); (x)->next->prev = (x)->prev; (x)->prev->next = (x)->next; (x)->prev = (x)->next = NULL; } while (0)
#define del_from_list(x) del_list_entry(&(x)->list_entry)
#define add_after_list_entry(p, x) do { verify_double_add(p, x); verify_list_entry(p); (x)->next = (p)->next; (x)->prev = (p); (p)->next = (x); (x)->next->prev = (x); } while (0)
#define add_before_list_entry(p, x) do { verify_double_add(p, x); verify_list_entry(p); (x)->prev = (p)->prev; (x)->next = (p); (p)->prev = (x); (x)->prev->next = (x); } while (0)
#define add_to_list(l, x) add_after_list_entry(&(l), &(x)->list_entry)
#define add_to_list_end(l, x) add_before_list_entry(&(l), &(x)->list_entry)
#define add_after_pos(p, x) add_after_list_entry(&(p)->list_entry, &(x)->list_entry)
#define add_before_pos(p, x) add_before_list_entry(&(p)->list_entry, &(x)->list_entry)
#define fix_list_after_realloc(x) do { (x)->list_entry.prev->next = &(x)->list_entry; (x)->list_entry.next->prev = &(x)->list_entry; } while (0)
#define foreachfrom(struc, e, h, l, s) for ((h) = (s); verify_list_entry(h), (h) == &(l) ? 0 : ((e) = list_struct(h, struc), 1); (h) = (h)->next)
#define foreach(struc, e, h, l) foreachfrom(struc, e, h, l, (l).next)
#define foreachbackfrom(struc, e, h, l, s) for ((h) = (s); verify_list_entry(h), (h) == &(l) ? 0 : ((e) = list_struct(h, struc), 1); (h) = (h)->prev)
#define foreachback(struc, e, h, l) foreachbackfrom(struc, e, h, l, (l).prev)
#define free_list(struc, l) do { while (!list_empty(l)) { struc *a__ = list_struct((l).next, struc); del_from_list(a__); mem_free(a__); } } while (0)
static inline unsigned long list_size(struct list_head *l)
{
struct list_head *e;
unsigned long n = 0;
for (e = l->next; e != l; e = e->next) n++;
return n;
}
#ifdef DEBUG
#define REORDER_LIST_ENTRIES
#endif
#ifndef REORDER_LIST_ENTRIES
#define list_entry_1st struct list_head list_entry;
#define list_entry_last
#define init_list_1st(x) { (x), (x) },
#define init_list_last(x)
#else
#define list_entry_1st
#define list_entry_last struct list_head list_entry;
#define init_list_1st(x)
#define init_list_last(x) { (x), (x) },
#endif
#define WHITECHAR(x) ((x) == 9 || (x) == 10 || (x) == 12 || (x) == 13 || (x) == ' ')
#define U(x) ((x) == '"' || (x) == '\'')
#define CI_BYTES 1
#define CI_FILES 2
#define CI_LOCKED 3
#define CI_LOADING 4
#define CI_TIMERS 5
#define CI_TRANSFER 6
#define CI_CONNECTING 7
#define CI_KEEP 8
/* string.c */
int snprint(unsigned char *s, int n, my_uintptr_t num);
int snzprint(unsigned char *s, int n, off_t num);
void add_to_strn(unsigned char **s, unsigned char *a);
void extend_str(unsigned char **s, int n);
#define init_str() init_str_x((unsigned char *)__FILE__, __LINE__)
static inline unsigned char *init_str_x(unsigned char *file, int line)
{
unsigned char *p;
p=(unsigned char *)debug_mem_calloc(file, line, 1L, 0);
return p;
}
void add_bytes_to_str(unsigned char **s, int *l, unsigned char *a, size_t ll);
void add_to_str(unsigned char **s, int *l, unsigned char *a);
void add_chr_to_str(unsigned char **s, int *l, unsigned char a);
void add_unsigned_num_to_str(unsigned char **s, int *l, off_t n);
void add_unsigned_long_num_to_str(unsigned char **s, int *l, my_uintptr_t n);
void add_num_to_str(unsigned char **s, int *l, off_t n);
void add_knum_to_str(unsigned char **s, int *l, off_t n);
long strtolx(unsigned char *c, unsigned char **end);
#if defined(HAVE_STRTOLL) || defined(HAVE_STRTOQ) || defined(HAVE_STRTOIMAX)
#define my_strtoll_t longlong
#else
#define my_strtoll_t long
#endif
my_strtoll_t my_strtoll(unsigned char *string, unsigned char **end);
void safe_strncpy(unsigned char *dst, const unsigned char *src, size_t dst_size);
#ifdef JS
void skip_nonprintable(unsigned char *txt);
#endif
/* case insensitive compare of 2 strings */
/* comparison ends after len (or less) characters */
/* return value: 1=strings differ, 0=strings are same */
static inline unsigned upcase(unsigned a)
{
if (a >= 'a' && a <= 'z') a -= 0x20;
return a;
}
static inline unsigned locase(unsigned a)
{
if (a >= 'A' && a <= 'Z') a += 0x20;
return a;
}
static inline int srch_cmp(unsigned char c1, unsigned char c2)
{
return upcase(c1) != upcase(c2);
}
int casestrcmp(const unsigned char *s1, const unsigned char *s2);
int casecmp(const unsigned char *c1, const unsigned char *c2, size_t len);
int casestrstr(const unsigned char *h, const unsigned char *n);
unsigned hash_string(unsigned char *s);
static inline int xstrcmp(const unsigned char *s1, const unsigned char *s2)
{
if (!s1 && !s2) return 0;
if (!s1) return -1;
if (!s2) return 1;
return strcmp(cast_const_char s1, cast_const_char s2);
}
static inline int cmpbeg(const unsigned char *str, const unsigned char *b)
{
while (*str && upcase(*str) == upcase(*b)) str++, b++;
return !!*b;
}
/* os_dep.c */
#ifdef HAVE_LONG_LONG
typedef unsigned long long uttime;
typedef unsigned long long tcount;
#else
typedef unsigned long uttime;
typedef unsigned long tcount;
#endif
extern int page_size;
#if defined(HAVE_GPM_H) && defined(HAVE_LIBGPM)
#define USE_GPM
#endif
#if defined(OS2) && defined(HAVE_UMALLOC_H) && defined(HAVE__MSIZE) && defined(HAVE__UCREATE) && defined(HAVE__UOPEN) && defined(HAVE__UDEFAULT) && defined(HAVE_BEGINTHREAD)
#define OS2_ADVANCED_HEAP
void *virtual_alloc(size_t len);
void virtual_free(void *ptr, size_t len);
void *os2_orig_malloc(size_t len);
#define MEMORY_BIGALLOC
extern unsigned long mem_bigalloc;
extern unsigned long blocks_bigalloc;
#define MEMORY_REQUESTED
extern unsigned long mem_requested;
extern unsigned long blocks_requested;
#endif
#ifdef OPENVMS
#define VMS_ADVANCED_HEAP
void *virtual_alloc(size_t len);
void virtual_free(void *ptr, size_t len);
#define MEMORY_BIGALLOC
extern unsigned long mem_bigalloc;
extern unsigned long blocks_bigalloc;
#endif
struct terminal;
struct open_in_new {
unsigned char *text;
unsigned char *hk;
int (* const *open_window_fn)(struct terminal *, unsigned char *, unsigned char *);
};
void close_fork_tty(void);
int get_system_env(void);
int is_twterm(void);
int is_screen(void);
int is_xterm(void);
void get_terminal_size(int *, int *);
void handle_terminal_resize(void (*)(int, int), int *x, int *y);
void unhandle_terminal_resize(void);
void set_nonblock(int);
int c_pipe(int [2]);
int c_dup(int oh);
int c_socket(int, int, int);
int c_accept(int, struct sockaddr *, socklen_t *);
int c_open(unsigned char *, int);
int c_open3(unsigned char *, int, int);
DIR *c_opendir(unsigned char *);
#ifdef HAVE_OPEN_PREALLOC
int open_prealloc(unsigned char *, int, int, off_t);