| 1 |
/* |
| 2 |
* Copyright (c) 2021-2025 Symas Corporation |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of the Symas Corporation nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
#ifndef _UTIL_H_ |
| 32 |
#define _UTIL_H_ |
| 33 |
|
| 34 |
void cbl_message(int fd, const char *format_string, ...) |
| 35 |
ATTRIBUTE_PRINTF_2; |
| 36 |
[[noreturn]] void cbl_internal_error(const char *format_string, ...) |
| 37 |
ATTRIBUTE_GCOBOL_DIAG(1, 2); |
| 38 |
|
| 39 |
void cbl_err(const char *format_string, ...) ATTRIBUTE_GCOBOL_DIAG(1, 2); |
| 40 |
void cbl_errx(const char *format_string, ...) ATTRIBUTE_GCOBOL_DIAG(1, 2); |
| 41 |
|
| 42 |
bool fisdigit(int c); |
| 43 |
bool fisspace(int c); |
| 44 |
int ftolower(int c); |
| 45 |
int ftoupper(int c); |
| 46 |
bool fisprint(int c); |
| 47 |
|
| 48 |
void cobol_set_pp_option(int opt); |
| 49 |
|
| 50 |
void cobol_filename_restore(); |
| 51 |
const char * cobol_lineno( int ); |
| 52 |
int cobol_lineno(void); |
| 53 |
|
| 54 |
unsigned long gb4( size_t input ); |
| 55 |
|
| 56 |
template <typename P> |
| 57 |
static inline const void * |
| 58 |
as_voidp( P p ) { |
| 59 |
return static_cast<const void *>(p); |
| 60 |
} |
| 61 |
|
| 62 |
/* |
| 63 |
* The default source format, whether free or fixed, is determined |
| 64 |
* heuristically by examining the PROGRAM-ID line, if it exists, in the first |
| 65 |
* input file. If that file does not have such a line, the default is free |
| 66 |
* format. Else the format is set to fixed if anything appears on that line |
| 67 |
* that would prohibit parsing it as free format, |
| 68 |
*/ |
| 69 |
class source_format_t { |
| 70 |
bool first_file, explicitly; |
| 71 |
int left, right; |
| 72 |
public: |
| 73 |
source_format_t() |
| 74 |
: first_file(true), explicitly(false), left(0), right(0) |
| 75 |
{} |
| 76 |
void indicator_column_set( int column ) { |
| 77 |
explicitly = true; |
| 78 |
if( column == 0 ) right = 0; |
| 79 |
if( column < 0 ) { |
| 80 |
column = -column; |
| 81 |
right = 73; |
| 82 |
} |
| 83 |
left = column; |
| 84 |
} |
| 85 |
|
| 86 |
bool inference_pending() { |
| 87 |
bool tf = first_file && !explicitly; |
| 88 |
first_file = false; |
| 89 |
return tf; |
| 90 |
} |
| 91 |
|
| 92 |
void infer( const char *bol, bool want_reference_format ); |
| 93 |
|
| 94 |
inline bool is_fixed() const { return left == 7; } |
| 95 |
inline bool is_reffmt() const { return is_fixed() && right == 73; } |
| 96 |
inline bool is_free() const { return ! is_fixed(); } |
| 97 |
|
| 98 |
const char * description() const { |
| 99 |
if( is_reffmt() ) return "REFERENCE"; |
| 100 |
if( is_fixed() ) return "FIXED"; |
| 101 |
if( is_free() ) return "FREE"; |
| 102 |
gcc_unreachable(); |
| 103 |
} |
| 104 |
|
| 105 |
inline int left_margin() { |
| 106 |
return left == 0? left : left - 1; |
| 107 |
} |
| 108 |
inline int right_margin() { |
| 109 |
return right == 0? right : right - 1; |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
void cdf_push(); |
| 115 |
void cdf_push_call_convention(); |
| 116 |
void cdf_push_current_tokens(); |
| 117 |
void cdf_push_dictionary(); |
| 118 |
void cdf_push_enabled_exceptions(); |
| 119 |
void cdf_push_source_format(); |
| 120 |
|
| 121 |
void cdf_pop(); |
| 122 |
void cdf_pop_call_convention(); |
| 123 |
void cdf_pop_current_tokens(); |
| 124 |
void cdf_pop_dictionary(); |
| 125 |
void cdf_pop_source_format(); |
| 126 |
void cdf_pop_enabled_exceptions(); |
| 127 |
|
| 128 |
#endif |