aboutsummaryrefslogtreecommitdiff
path: root/include/status.h
blob: 198cf5704a5786b8f7b3e4f946e783d1e59766d4 (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
/*
 * *****************************************************************************
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
 *
 * 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.
 *
 * 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 HOLDER 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.
 *
 * *****************************************************************************
 *
 * All bc status codes and cross-platform portability.
 *
 */

#ifndef BC_STATUS_H
#define BC_STATUS_H

#ifdef _WIN32
#include <Windows.h>
#include <BaseTsd.h>
#include <stdio.h>
#include <io.h>
#endif // _WIN32

#include <stdint.h>
#include <sys/types.h>

// This is used by configure.sh to test for OpenBSD.
#ifdef BC_TEST_OPENBSD
#ifdef __OpenBSD__
#error On OpenBSD without _BSD_SOURCE
#endif // __OpenBSD__
#endif // BC_TEST_OPENBSD

// This is used by configure.sh to test for FreeBSD.
#ifdef BC_TEST_FREEBSD
#ifdef __FreeBSD__
#error On FreeBSD with _POSIX_C_SOURCE
#endif // __FreeBSD__
#endif // BC_TEST_FREEBSD

// Windows has deprecated isatty() and the rest of these. Or doesn't have them.
// So these are just fixes for Windows.
#ifdef _WIN32

// This one is special. Windows did not like me defining an
// inline function that was not given a definition in a header
// file. This suppresses that by making inline functions non-inline.
#define inline

#define restrict __restrict
#define strdup _strdup
#define write(f, b, s) _write((f), (b), (unsigned int) (s))
#define read(f, b, s) _read((f), (b), (unsigned int) (s))
#define close _close
#define open(f, n, m) \
	_sopen_s((f), (n), (m) | _O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE)
#define sigjmp_buf jmp_buf
#define sigsetjmp(j, s) setjmp(j)
#define siglongjmp longjmp
#define isatty _isatty
#define STDIN_FILENO _fileno(stdin)
#define STDOUT_FILENO _fileno(stdout)
#define STDERR_FILENO _fileno(stderr)
#define S_ISDIR(m) ((m) & (_S_IFDIR))
#define O_RDONLY _O_RDONLY
#define stat _stat
#define fstat _fstat
#define BC_FILE_SEP '\\'

#else // _WIN32
#define BC_FILE_SEP '/'
#endif // _WIN32

#ifndef BC_ENABLED
#define BC_ENABLED (1)
#endif // BC_ENABLED

#ifndef DC_ENABLED
#define DC_ENABLED (1)
#endif // DC_ENABLED

#ifndef BC_ENABLE_EXTRA_MATH
#define BC_ENABLE_EXTRA_MATH (1)
#endif // BC_ENABLE_EXTRA_MATH

#ifndef BC_ENABLE_LIBRARY
#define BC_ENABLE_LIBRARY (0)
#endif // BC_ENABLE_LIBRARY

#ifndef BC_ENABLE_HISTORY
#define BC_ENABLE_HISTORY (1)
#endif // BC_ENABLE_HISTORY

#ifndef BC_ENABLE_EDITLINE
#define BC_ENABLE_EDITLINE (0)
#endif // BC_ENABLE_EDITLINE

#ifndef BC_ENABLE_READLINE
#define BC_ENABLE_READLINE (0)
#endif // BC_ENABLE_READLINE

#ifndef BC_ENABLE_NLS
#define BC_ENABLE_NLS (0)
#endif // BC_ENABLE_NLS

#ifdef __OpenBSD__
#if BC_ENABLE_READLINE
#error Cannot use readline on OpenBSD
#endif // BC_ENABLE_READLINE
#endif // __OpenBSD__

#if BC_ENABLE_EDITLINE && BC_ENABLE_READLINE
#error Must enable only one of editline or readline, not both.
#endif // BC_ENABLE_EDITLINE && BC_ENABLE_READLINE

#if BC_ENABLE_EDITLINE || BC_ENABLE_READLINE
#define BC_ENABLE_LINE_LIB (1)
#else // BC_ENABLE_EDITLINE || BC_ENABLE_READLINE
#define BC_ENABLE_LINE_LIB (0)
#endif // BC_ENABLE_EDITLINE || BC_ENABLE_READLINE

// This is error checking for fuzz builds.
#if BC_ENABLE_AFL
#ifndef __AFL_HAVE_MANUAL_CONTROL
#error Must compile with afl-clang-fast or afl-clang-lto for fuzzing
#endif // __AFL_HAVE_MANUAL_CONTROL
#endif // BC_ENABLE_AFL

#ifndef BC_ENABLE_MEMCHECK
#define BC_ENABLE_MEMCHECK (0)
#endif // BC_ENABLE_MEMCHECK

/**
 * Mark a variable as unused.
 * @param e  The variable to mark as unused.
 */
#define BC_UNUSED(e) ((void) (e))

// If users want, they can define this to something like __builtin_expect(e, 1).
// It might give a performance improvement.
#ifndef BC_LIKELY

/**
 * Mark a branch expression as likely.
 * @param e  The expression to mark as likely.
 */
#define BC_LIKELY(e) (e)

#endif // BC_LIKELY

// If users want, they can define this to something like __builtin_expect(e, 0).
// It might give a performance improvement.
#ifndef BC_UNLIKELY

/**
 * Mark a branch expression as unlikely.
 * @param e  The expression to mark as unlikely.
 */
#define BC_UNLIKELY(e) (e)

#endif // BC_UNLIKELY

/**
 * Mark a branch expression as an error, if true.
 * @param e  The expression to mark as an error, if true.
 */
#define BC_ERR(e) BC_UNLIKELY(e)

/**
 * Mark a branch expression as not an error, if true.
 * @param e  The expression to mark as not an error, if true.
 */
#define BC_NO_ERR(s) BC_LIKELY(s)

// Disable extra debug code by default.
#ifndef BC_DEBUG_CODE
#define BC_DEBUG_CODE (0)
#endif // BC_DEBUG_CODE

#if defined(__clang__)
#define BC_CLANG (1)
#else // defined(__clang__)
#define BC_CLANG (0)
#endif // defined(__clang__)

#if defined(__GNUC__) && !BC_CLANG
#define BC_GCC (1)
#else // defined(__GNUC__) && !BC_CLANG
#define BC_GCC (0)
#endif // defined(__GNUC__) && !BC_CLANG

// We want to be able to use _Noreturn on C11 compilers.
#if __STDC_VERSION__ >= 201112L

#include <stdnoreturn.h>
#define BC_NORETURN _Noreturn
#define BC_C11 (1)

#else // __STDC_VERSION__

#if BC_CLANG
#if __has_attribute(noreturn)
#define BC_NORETURN __attribute((noreturn))
#else // __has_attribute(noreturn)
#define BC_NORETURN
#endif // __has_attribute(noreturn)

#else // BC_CLANG

#define BC_NORETURN

#endif // BC_CLANG

#define BC_MUST_RETURN
#define BC_C11 (0)

#endif // __STDC_VERSION__

#define BC_HAS_UNREACHABLE (0)
#define BC_HAS_COMPUTED_GOTO (0)

// GCC and Clang complain if fallthroughs are not marked with their special
// attribute. Jerks. This creates a define for marking the fallthroughs that is
// nothing on other compilers.
#if BC_CLANG || BC_GCC

#if defined(__has_attribute)

#if __has_attribute(fallthrough)
#define BC_FALLTHROUGH __attribute__((fallthrough));
#else // __has_attribute(fallthrough)
#define BC_FALLTHROUGH
#endif // __has_attribute(fallthrough)

#if BC_GCC

#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
#undef BC_HAS_UNREACHABLE
#define BC_HAS_UNREACHABLE (1)
#endif // __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)

#else // BC_GCC

#if __clang_major__ >= 4
#undef BC_HAS_UNREACHABLE
#define BC_HAS_UNREACHABLE (1)
#endif // __clang_major__ >= 4

#endif // BC_GCC

#else // defined(__has_attribute)
#define BC_FALLTHROUGH
#endif // defined(__has_attribute)
#else // BC_CLANG || BC_GCC
#define BC_FALLTHROUGH
#endif // BC_CLANG || BC_GCC

#if BC_HAS_UNREACHABLE

#define BC_UNREACHABLE __builtin_unreachable();

#else // BC_HAS_UNREACHABLE

#ifdef _WIN32

#define BC_UNREACHABLE __assume(0);

#else // _WIN32

#define BC_UNREACHABLE

#endif // _WIN32

#endif // BC_HAS_UNREACHABLE

#if BC_GCC

#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)

#undef BC_HAS_COMPUTED_GOTO
#define BC_HAS_COMPUTED_GOTO (1)

#endif // __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)

#endif // BC_GCC

#if BC_CLANG

#if __clang_major__ >= 4

#undef BC_HAS_COMPUTED_GOTO
#define BC_HAS_COMPUTED_GOTO (1)

#endif // __clang_major__ >= 4

#endif // BC_CLANG

#ifdef BC_NO_COMPUTED_GOTO

#undef BC_HAS_COMPUTED_GOTO
#define BC_HAS_COMPUTED_GOTO (0)

#endif // BC_NO_COMPUTED_GOTO

#if BC_GCC
#ifdef __OpenBSD__
// The OpenBSD GCC doesn't like inline.
#define inline
#endif // __OpenBSD__
#endif // BC_GCC

// Workarounds for AIX's POSIX incompatibility.
#ifndef SIZE_MAX
#define SIZE_MAX __SIZE_MAX__
#endif // SIZE_MAX
#ifndef UINTMAX_C
#define UINTMAX_C __UINTMAX_C
#endif // UINTMAX_C
#ifndef UINT32_C
#define UINT32_C __UINT32_C
#endif // UINT32_C
#ifndef UINT_FAST32_MAX
#define UINT_FAST32_MAX __UINT_FAST32_MAX__
#endif // UINT_FAST32_MAX
#ifndef UINT16_MAX
#define UINT16_MAX __UINT16_MAX__
#endif // UINT16_MAX
#ifndef SIG_ATOMIC_MAX
#define SIG_ATOMIC_MAX __SIG_ATOMIC_MAX__
#endif // SIG_ATOMIC_MAX

// Yes, this has to be here.
#include <bcl.h>

// All of these set defaults for settings.

#if BC_ENABLED

#ifndef BC_DEFAULT_BANNER
#define BC_DEFAULT_BANNER (0)
#endif // BC_DEFAULT_BANNER

#endif // BC_ENABLED

#ifndef BC_DEFAULT_SIGINT_RESET
#define BC_DEFAULT_SIGINT_RESET (1)
#endif // BC_DEFAULT_SIGINT_RESET

#ifndef BC_DEFAULT_TTY_MODE
#define BC_DEFAULT_TTY_MODE (1)
#endif // BC_DEFAULT_TTY_MODE

#ifndef BC_DEFAULT_PROMPT
#define BC_DEFAULT_PROMPT BC_DEFAULT_TTY_MODE
#endif // BC_DEFAULT_PROMPT

#ifndef BC_DEFAULT_EXPR_EXIT
#define BC_DEFAULT_EXPR_EXIT (1)
#endif // BC_DEFAULT_EXPR_EXIT

#ifndef BC_DEFAULT_DIGIT_CLAMP
#define BC_DEFAULT_DIGIT_CLAMP (0)
#endif // BC_DEFAULT_DIGIT_CLAMP

// All of these set defaults for settings.
#ifndef DC_DEFAULT_SIGINT_RESET
#define DC_DEFAULT_SIGINT_RESET (1)
#endif // DC_DEFAULT_SIGINT_RESET

#ifndef DC_DEFAULT_TTY_MODE
#define DC_DEFAULT_TTY_MODE (0)
#endif // DC_DEFAULT_TTY_MODE

#ifndef DC_DEFAULT_HISTORY
#define DC_DEFAULT_HISTORY DC_DEFAULT_TTY_MODE
#endif // DC_DEFAULT_HISTORY

#ifndef DC_DEFAULT_PROMPT
#define DC_DEFAULT_PROMPT DC_DEFAULT_TTY_MODE
#endif // DC_DEFAULT_PROMPT

#ifndef DC_DEFAULT_EXPR_EXIT
#define DC_DEFAULT_EXPR_EXIT (1)
#endif // DC_DEFAULT_EXPR_EXIT

#ifndef DC_DEFAULT_DIGIT_CLAMP
#define DC_DEFAULT_DIGIT_CLAMP (0)
#endif // DC_DEFAULT_DIGIT_CLAMP

/// Statuses, which mark either which category of error happened, or some other
/// status that matters.
typedef enum BcStatus
{
	/// Normal status.
	BC_STATUS_SUCCESS = 0,

	/// Math error.
	BC_STATUS_ERROR_MATH,

	/// Parse (and lex) error.
	BC_STATUS_ERROR_PARSE,

	/// Runtime error.
	BC_STATUS_ERROR_EXEC,

	/// Fatal error.
	BC_STATUS_ERROR_FATAL,

	/// EOF status.
	BC_STATUS_EOF,

	/// Quit status. This means that bc/dc is in the process of quitting.
	BC_STATUS_QUIT,

} BcStatus;

/// Errors, which are more specific errors.
typedef enum BcErr
{
	// Math errors.

	/// Negative number used when not allowed.
	BC_ERR_MATH_NEGATIVE,

	/// Non-integer used when not allowed.
	BC_ERR_MATH_NON_INTEGER,

	/// Conversion to a hardware integer would overflow.
	BC_ERR_MATH_OVERFLOW,

	/// Divide by zero.
	BC_ERR_MATH_DIVIDE_BY_ZERO,

	// Fatal errors.

	/// An allocation or reallocation failed.
	BC_ERR_FATAL_ALLOC_ERR,

	/// I/O failure.
	BC_ERR_FATAL_IO_ERR,

	/// File error, such as permissions or file does not exist.
	BC_ERR_FATAL_FILE_ERR,

	/// File is binary, not text, error.
	BC_ERR_FATAL_BIN_FILE,

	/// Attempted to read a directory as a file error.
	BC_ERR_FATAL_PATH_DIR,

	/// Invalid option error.
	BC_ERR_FATAL_OPTION,

	/// Option with required argument not given an argument.
	BC_ERR_FATAL_OPTION_NO_ARG,

	/// Option with no argument given an argument.
	BC_ERR_FATAL_OPTION_ARG,

	/// Option argument is invalid.
	BC_ERR_FATAL_ARG,

	// Runtime errors.

	/// Invalid ibase value.
	BC_ERR_EXEC_IBASE,

	/// Invalid obase value.
	BC_ERR_EXEC_OBASE,

	/// Invalid scale value.
	BC_ERR_EXEC_SCALE,

	/// Invalid expression parsed by read().
	BC_ERR_EXEC_READ_EXPR,

	/// read() used within an expression given to a read() call.
	BC_ERR_EXEC_REC_READ,

	/// Type error.
	BC_ERR_EXEC_TYPE,

	/// Stack has too few elements error.
	BC_ERR_EXEC_STACK,

	/// Register stack has too few elements error.
	BC_ERR_EXEC_STACK_REGISTER,

	/// Wrong number of arguments error.
	BC_ERR_EXEC_PARAMS,

	/// Undefined function error.
	BC_ERR_EXEC_UNDEF_FUNC,

	/// Void value used in an expression error.
	BC_ERR_EXEC_VOID_VAL,

	// Parse (and lex) errors.

	/// EOF encountered when not expected error.
	BC_ERR_PARSE_EOF,

	/// Invalid character error.
	BC_ERR_PARSE_CHAR,

	/// Invalid string (no ending quote) error.
	BC_ERR_PARSE_STRING,

	/// Invalid comment (no end found) error.
	BC_ERR_PARSE_COMMENT,

	/// Invalid token encountered error.
	BC_ERR_PARSE_TOKEN,

#if BC_ENABLED

	/// Invalid expression error.
	BC_ERR_PARSE_EXPR,

	/// Expression is empty error.
	BC_ERR_PARSE_EMPTY_EXPR,

	/// Print statement is invalid error.
	BC_ERR_PARSE_PRINT,

	/// Function definition is invalid error.
	BC_ERR_PARSE_FUNC,

	/// Assignment is invalid error.
	BC_ERR_PARSE_ASSIGN,

	/// No auto identifiers given for an auto statement error.
	BC_ERR_PARSE_NO_AUTO,

	/// Duplicate local (parameter or auto) error.
	BC_ERR_PARSE_DUP_LOCAL,

	/// Invalid block (within braces) error.
	BC_ERR_PARSE_BLOCK,

	/// Invalid return statement for void functions.
	BC_ERR_PARSE_RET_VOID,

	/// Reference attached to a variable, not an array, error.
	BC_ERR_PARSE_REF_VAR,

	// POSIX-only errors.

	/// Name length greater than 1 error.
	BC_ERR_POSIX_NAME_LEN,

	/// Non-POSIX comment used error.
	BC_ERR_POSIX_COMMENT,

	/// Non-POSIX keyword error.
	BC_ERR_POSIX_KW,

	/// Non-POSIX . (last) error.
	BC_ERR_POSIX_DOT,

	/// Non-POSIX return error.
	BC_ERR_POSIX_RET,

	/// Non-POSIX boolean operator used error.
	BC_ERR_POSIX_BOOL,

	/// POSIX relation operator used outside if, while, or for statements error.
	BC_ERR_POSIX_REL_POS,

	/// Multiple POSIX relation operators used in an if, while, or for statement
	/// error.
	BC_ERR_POSIX_MULTIREL,

	/// Empty statements in POSIX for loop error.
	BC_ERR_POSIX_FOR,

	/// POSIX's grammar does not allow a function definition right after a
	/// semicolon.
	BC_ERR_POSIX_FUNC_AFTER_SEMICOLON,

	/// Non-POSIX exponential (scientific or engineering) number used error.
	BC_ERR_POSIX_EXP_NUM,

	/// Non-POSIX array reference error.
	BC_ERR_POSIX_REF,

	/// Non-POSIX void error.
	BC_ERR_POSIX_VOID,

	/// Non-POSIX brace position used error.
	BC_ERR_POSIX_BRACE,

	/// String used in expression.
	BC_ERR_POSIX_EXPR_STRING,

#endif // BC_ENABLED

	// Number of elements.
	BC_ERR_NELEMS,

#if BC_ENABLED

	/// A marker for the start of POSIX errors.
	BC_ERR_POSIX_START = BC_ERR_POSIX_NAME_LEN,

	/// A marker for the end of POSIX errors.
	BC_ERR_POSIX_END = BC_ERR_POSIX_EXPR_STRING,

#endif // BC_ENABLED

} BcErr;

// The indices of each category of error in bc_errs[], and used in bc_err_ids[]
// to associate actual errors with their categories.

/// Math error category.
#define BC_ERR_IDX_MATH (0)

/// Parse (and lex) error category.
#define BC_ERR_IDX_PARSE (1)

/// Runtime error category.
#define BC_ERR_IDX_EXEC (2)

/// Fatal error category.
#define BC_ERR_IDX_FATAL (3)

/// Number of categories.
#define BC_ERR_IDX_NELEMS (4)

// If bc is enabled, we add an extra category for POSIX warnings.
#if BC_ENABLED

/// POSIX warning category.
#define BC_ERR_IDX_WARN (BC_ERR_IDX_NELEMS)

#endif // BC_ENABLED

/**
 * The mode bc is in. This is basically what input it is processing.
 */
typedef enum BcMode
{
	/// Expressions mode.
	BC_MODE_EXPRS,

	/// File mode.
	BC_MODE_FILE,

	/// stdin mode.
	BC_MODE_STDIN,

} BcMode;

/// Do a longjmp(). This is what to use when activating an "exception", i.e., a
/// longjmp(). With debug code, it will print the name of the function it jumped
/// from.
#if BC_DEBUG_CODE
#define BC_JMP bc_vm_jmp(__func__)
#else // BC_DEBUG_CODE
#define BC_JMP bc_vm_jmp()
#endif // BC_DEBUG_CODE

#if !BC_ENABLE_LIBRARY

/// Returns true if an exception is in flight, false otherwise.
#define BC_SIG_EXC(vm) \
	BC_UNLIKELY((vm)->status != (sig_atomic_t) BC_STATUS_SUCCESS || (vm)->sig)

/// Returns true if there is *no* exception in flight, false otherwise.
#define BC_NO_SIG_EXC(vm) \
	BC_LIKELY((vm)->status == (sig_atomic_t) BC_STATUS_SUCCESS && !(vm)->sig)

#ifndef _WIN32
#define BC_SIG_INTERRUPT(vm) \
	BC_UNLIKELY((vm)->sig != 0 && (vm)->sig != SIGWINCH)
#else // _WIN32
#define BC_SIG_INTERRUPT(vm) BC_UNLIKELY((vm)->sig != 0)
#endif // _WIN32

#if BC_DEBUG

/// Assert that signals are locked. There are non-async-signal-safe functions in
/// bc, and they *must* have signals locked. Other functions are expected to
/// *not* have signals locked, for reasons. So this is a pre-built assert
/// (no-op in non-debug mode) that check that signals are locked.
#define BC_SIG_ASSERT_LOCKED  \
	do                        \
	{                         \
		assert(vm->sig_lock); \
	}                         \
	while (0)

/// Assert that signals are unlocked. There are non-async-signal-safe functions
/// in bc, and they *must* have signals locked. Other functions are expected to
/// *not* have signals locked, for reasons. So this is a pre-built assert
/// (no-op in non-debug mode) that check that signals are unlocked.
#define BC_SIG_ASSERT_NOT_LOCKED   \
	do                             \
	{                              \
		assert(vm->sig_lock == 0); \
	}                              \
	while (0)

#else // BC_DEBUG

/// Assert that signals are locked. There are non-async-signal-safe functions in
/// bc, and they *must* have signals locked. Other functions are expected to
/// *not* have signals locked, for reasons. So this is a pre-built assert
/// (no-op in non-debug mode) that check that signals are locked.
#define BC_SIG_ASSERT_LOCKED

/// Assert that signals are unlocked. There are non-async-signal-safe functions
/// in bc, and they *must* have signals locked. Other functions are expected to
/// *not* have signals locked, for reasons. So this is a pre-built assert
/// (no-op in non-debug mode) that check that signals are unlocked.
#define BC_SIG_ASSERT_NOT_LOCKED

#endif // BC_DEBUG

/// Locks signals.
#define BC_SIG_LOCK               \
	do                            \
	{                             \
		BC_SIG_ASSERT_NOT_LOCKED; \
		vm->sig_lock = 1;         \
	}                             \
	while (0)

/// Unlocks signals. If a signal happened, then this will cause a jump.
#define BC_SIG_UNLOCK         \
	do                        \
	{                         \
		BC_SIG_ASSERT_LOCKED; \
		vm->sig_lock = 0;     \
		if (vm->sig) BC_JMP;  \
	}                         \
	while (0)

/// Locks signals, regardless of if they are already locked. This is really only
/// used after labels that longjmp() goes to after the jump because the cleanup
/// code must have signals locked, and BC_LONGJMP_CONT will unlock signals if it
/// doesn't jump.
#define BC_SIG_MAYLOCK    \
	do                    \
	{                     \
		vm->sig_lock = 1; \
	}                     \
	while (0)

/// Unlocks signals, regardless of if they were already unlocked. If a signal
/// happened, then this will cause a jump.
#define BC_SIG_MAYUNLOCK     \
	do                       \
	{                        \
		vm->sig_lock = 0;    \
		if (vm->sig) BC_JMP; \
	}                        \
	while (0)

/**
 * Locks signals, but stores the old lock state, to be restored later by
 * BC_SIG_TRYUNLOCK.
 * @param v  The variable to store the old lock state to.
 */
#define BC_SIG_TRYLOCK(v) \
	do                    \
	{                     \
		v = vm->sig_lock; \
		vm->sig_lock = 1; \
	}                     \
	while (0)

/**
 * Restores the previous state of a signal lock, and if it is now unlocked,
 * initiates an exception/jump.
 * @param v  The old lock state.
 */
#define BC_SIG_TRYUNLOCK(v)          \
	do                               \
	{                                \
		vm->sig_lock = (v);          \
		if (!(v) && vm->sig) BC_JMP; \
	}                                \
	while (0)

/// Stops a stack unwinding. Technically, a stack unwinding needs to be done
/// manually, but it will always be done unless certain flags are cleared. This
/// clears the flags.
#define BC_LONGJMP_STOP  \
	do                   \
	{                    \
		vm->sig_pop = 0; \
		vm->sig = 0;     \
	}                    \
	while (0)

/**
 * Sets a jump like BC_SETJMP, but unlike BC_SETJMP, it assumes signals are
 * locked and will just set the jump. This does *not* have a call to
 * bc_vec_grow() because it is assumed that BC_SETJMP_LOCKED(l) is used *after*
 * the initializations that need the setjmp().
 * param l  The label to jump to on a longjmp().
 */
#define BC_SETJMP_LOCKED(vm, l)           \
	do                                    \
	{                                     \
		sigjmp_buf sjb;                   \
		BC_SIG_ASSERT_LOCKED;             \
		if (sigsetjmp(sjb, 0))            \
		{                                 \
			assert(BC_SIG_EXC(vm));       \
			goto l;                       \
		}                                 \
		bc_vec_push(&vm->jmp_bufs, &sjb); \
	}                                     \
	while (0)

/// Used after cleanup labels set by BC_SETJMP and BC_SETJMP_LOCKED to jump to
/// the next place. This is what continues the stack unwinding. This basically
/// copies BC_SIG_UNLOCK into itself, but that is because its condition for
/// jumping is BC_SIG_EXC, not just that a signal happened.
#define BC_LONGJMP_CONT(vm)                          \
	do                                               \
	{                                                \
		BC_SIG_ASSERT_LOCKED;                        \
		if (!vm->sig_pop) bc_vec_pop(&vm->jmp_bufs); \
		vm->sig_lock = 0;                            \
		if (BC_SIG_EXC(vm)) BC_JMP;                  \
	}                                                \
	while (0)

#else // !BC_ENABLE_LIBRARY

#define BC_SIG_LOCK
#define BC_SIG_UNLOCK
#define BC_SIG_MAYLOCK
#define BC_SIG_TRYLOCK(lock)
#define BC_SIG_TRYUNLOCK(lock)
#define BC_SIG_ASSERT_LOCKED

/// Returns true if an exception is in flight, false otherwise.
#define BC_SIG_EXC(vm) \
	BC_UNLIKELY(vm->status != (sig_atomic_t) BC_STATUS_SUCCESS)

/// Returns true if there is *no* exception in flight, false otherwise.
#define BC_NO_SIG_EXC(vm) \
	BC_LIKELY(vm->status == (sig_atomic_t) BC_STATUS_SUCCESS)

/// Used after cleanup labels set by BC_SETJMP and BC_SETJMP_LOCKED to jump to
/// the next place. This is what continues the stack unwinding. This basically
/// copies BC_SIG_UNLOCK into itself, but that is because its condition for
/// jumping is BC_SIG_EXC, not just that a signal happened.
#define BC_LONGJMP_CONT(vm)         \
	do                              \
	{                               \
		bc_vec_pop(&vm->jmp_bufs);  \
		if (BC_SIG_EXC(vm)) BC_JMP; \
	}                               \
	while (0)

#endif // !BC_ENABLE_LIBRARY

/**
 * Sets a jump, and sets it up as well so that if a longjmp() happens, bc will
 * immediately goto a label where some cleanup code is. This one assumes that
 * signals are not locked and will lock them, set the jump, and unlock them.
 * Setting the jump also includes pushing the jmp_buf onto the jmp_buf stack.
 * This grows the jmp_bufs vector first to prevent a fatal error from happening
 * after the setjmp(). This is done because BC_SETJMP(l) is assumed to be used
 * *before* the actual initialization calls that need the setjmp().
 * param l  The label to jump to on a longjmp().
 */
#define BC_SETJMP(vm, l)                  \
	do                                    \
	{                                     \
		sigjmp_buf sjb;                   \
		BC_SIG_LOCK;                      \
		bc_vec_grow(&vm->jmp_bufs, 1);    \
		if (sigsetjmp(sjb, 0))            \
		{                                 \
			assert(BC_SIG_EXC(vm));       \
			goto l;                       \
		}                                 \
		bc_vec_push(&vm->jmp_bufs, &sjb); \
		BC_SIG_UNLOCK;                    \
	}                                     \
	while (0)

/// Unsets a jump. It always assumes signals are locked. This basically just
/// pops a jmp_buf off of the stack of jmp_bufs, and since the jump mechanism
/// always jumps to the location at the top of the stack, this effectively
/// undoes a setjmp().
#define BC_UNSETJMP(vm)            \
	do                             \
	{                              \
		BC_SIG_ASSERT_LOCKED;      \
		bc_vec_pop(&vm->jmp_bufs); \
	}                              \
	while (0)

#if BC_ENABLE_LIBRARY

#define BC_SETJMP_LOCKED(vm, l) BC_SETJMP(vm, l)

// Various convenience macros for calling the bc's error handling routine.

/**
 * Call bc's error handling routine.
 * @param e    The error.
 * @param l    The line of the script that the error happened.
 * @param ...  Extra arguments for error messages as necessary.
 */
#define bc_error(e, l, ...) (bc_vm_handleError((e)))

/**
 * Call bc's error handling routine.
 * @param e  The error.
 */
#define bc_err(e) (bc_vm_handleError((e)))

/**
 * Call bc's error handling routine.
 * @param e  The error.
 */
#define bc_verr(e, ...) (bc_vm_handleError((e)))

#else // BC_ENABLE_LIBRARY

// Various convenience macros for calling the bc's error handling routine.

/**
 * Call bc's error handling routine.
 * @param e    The error.
 * @param l    The line of the script that the error happened.
 * @param ...  Extra arguments for error messages as necessary.
 */
#if BC_DEBUG
#define bc_error(e, l, ...) \
	(bc_vm_handleError((e), __FILE__, __LINE__, (l), __VA_ARGS__))
#else // BC_DEBUG
#define bc_error(e, l, ...) (bc_vm_handleError((e), (l), __VA_ARGS__))
#endif // BC_DEBUG

/**
 * Call bc's error handling routine.
 * @param e  The error.
 */
#if BC_DEBUG
#define bc_err(e) (bc_vm_handleError((e), __FILE__, __LINE__, 0))
#else // BC_DEBUG
#define bc_err(e) (bc_vm_handleError((e), 0))
#endif // BC_DEBUG

/**
 * Call bc's error handling routine.
 * @param e  The error.
 */
#if BC_DEBUG
#define bc_verr(e, ...) \
	(bc_vm_handleError((e), __FILE__, __LINE__, 0, __VA_ARGS__))
#else // BC_DEBUG
#define bc_verr(e, ...) (bc_vm_handleError((e), 0, __VA_ARGS__))
#endif // BC_DEBUG

#endif // BC_ENABLE_LIBRARY

/**
 * Returns true if status @a s is an error, false otherwise.
 * @param s  The status to test.
 * @return   True if @a s is an error, false otherwise.
 */
#define BC_STATUS_IS_ERROR(s) \
	((s) >= BC_STATUS_ERROR_MATH && (s) <= BC_STATUS_ERROR_FATAL)

// Convenience macros that can be placed at the beginning and exits of functions
// for easy marking of where functions are entered and exited.
#if BC_DEBUG_CODE
#define BC_FUNC_ENTER                                               \
	do                                                              \
	{                                                               \
		size_t bc_func_enter_i;                                     \
		for (bc_func_enter_i = 0; bc_func_enter_i < vm->func_depth; \
		     ++bc_func_enter_i)                                     \
		{                                                           \
			bc_file_puts(&vm->ferr, bc_flush_none, "  ");           \
		}                                                           \
		vm->func_depth += 1;                                        \
		bc_file_printf(&vm->ferr, "Entering %s\n", __func__);       \
		bc_file_flush(&vm->ferr, bc_flush_none);                    \
	}                                                               \
	while (0);

#define BC_FUNC_EXIT                                                \
	do                                                              \
	{                                                               \
		size_t bc_func_enter_i;                                     \
		vm->func_depth -= 1;                                        \
		for (bc_func_enter_i = 0; bc_func_enter_i < vm->func_depth; \
		     ++bc_func_enter_i)                                     \
		{                                                           \
			bc_file_puts(&vm->ferr, bc_flush_none, "  ");           \
		}                                                           \
		bc_file_printf(&vm->ferr, "Leaving %s\n", __func__);        \
		bc_file_flush(&vm->ferr, bc_flush_none);                    \
	}                                                               \
	while (0);
#else // BC_DEBUG_CODE
#define BC_FUNC_ENTER
#define BC_FUNC_EXIT
#endif // BC_DEBUG_CODE

#endif // BC_STATUS_H