| 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 |
#ifdef _COPYBOOK_H |
| 32 |
#pragma message __FILE__ " included twice" |
| 33 |
#else |
| 34 |
#define _COPYBOOK_H |
| 35 |
|
| 36 |
FILE * copy_mode_start(); |
| 37 |
|
| 38 |
const char * cobol_filename(); |
| 39 |
bool cobol_filename( const char *name, ino_t inode ); |
| 40 |
|
| 41 |
void scanner_parsing( int token, bool tf ); |
| 42 |
void scanner_parsing_toggle(); |
| 43 |
void scanner_parsing_pop(); |
| 44 |
|
| 45 |
/* |
| 46 |
* COPY support On encountering a COPY statement, the parser continues |
| 47 |
* to parse, collecting the replacement values, if any. At statement |
| 48 |
* end (at the period), the system rearranges input to apply the |
| 49 |
* replacements before the input text is read by the lexer. |
| 50 |
*/ |
| 51 |
|
| 52 |
enum replace_type_t { string_e, token_e, pseudo_e }; |
| 53 |
|
| 54 |
struct copybook_replace_t { |
| 55 |
replace_type_t type; |
| 56 |
const char *src, *tgt; |
| 57 |
}; |
| 58 |
class copybook_t; |
| 59 |
|
| 60 |
class copybook_elem_t { |
| 61 |
friend copybook_t; |
| 62 |
struct copybook_loc_t { |
| 63 |
YYLTYPE loc; |
| 64 |
const char *name; |
| 65 |
copybook_loc_t() : loc(), name(NULL) {} |
| 66 |
} source, library; |
| 67 |
bool suppress; |
| 68 |
static std::list<const char *> suffixes; |
| 69 |
public: |
| 70 |
struct { bool source, library; } literally; |
| 71 |
int fd; |
| 72 |
size_t nsubexpr; |
| 73 |
std::deque<copybook_replace_t> replacements; |
| 74 |
|
| 75 |
copybook_elem_t() |
| 76 |
: suppress(false) |
| 77 |
, literally() |
| 78 |
, fd(-1) |
| 79 |
, nsubexpr(0) |
| 80 |
, regex_text(NULL) |
| 81 |
{} |
| 82 |
|
| 83 |
void clear() { |
| 84 |
suppress = false; |
| 85 |
nsubexpr = 0; |
| 86 |
if( fd ) close(fd); |
| 87 |
fd = -1; |
| 88 |
// TODO: free src & tgt |
| 89 |
replacements.clear(); |
| 90 |
} |
| 91 |
|
| 92 |
int open_file( const char dir[], bool literally = false ); |
| 93 |
|
| 94 |
static inline bool is_quote( const char ch ) { |
| 95 |
return ch == '\'' || ch == '"'; |
| 96 |
} |
| 97 |
static inline bool quoted( const char name[] ) { |
| 98 |
gcc_assert(name); |
| 99 |
return is_quote(name[0]); |
| 100 |
} |
| 101 |
static char * dequote( const char orig[] ) { |
| 102 |
gcc_assert(quoted(orig)); |
| 103 |
auto name = static_cast<char*>(xcalloc(1, strlen(orig))); |
| 104 |
gcc_assert(name); |
| 105 |
char *tgt = name; |
| 106 |
|
| 107 |
// For a literal name, we de-quote it and try to open it in the |
| 108 |
// current working directory. The COBOL literal could include |
| 109 |
// (escaped) doubled quotes, which we reduce to one. |
| 110 |
for( const char *src = orig; src < orig + strlen(orig); ) { |
| 111 |
if( is_quote(src[0]) ) { |
| 112 |
if( src[0] == src[1] ) { |
| 113 |
*tgt++ = *src++; // copy one of doubled quote |
| 114 |
} |
| 115 |
src++; // skip quote |
| 116 |
continue; |
| 117 |
} |
| 118 |
*tgt++ = *src++; |
| 119 |
} |
| 120 |
*tgt = '\0'; |
| 121 |
|
| 122 |
return name; |
| 123 |
} |
| 124 |
|
| 125 |
private: |
| 126 |
char *regex_text; |
| 127 |
}; |
| 128 |
|
| 129 |
class uppername_t { |
| 130 |
std::string upper; |
| 131 |
public: |
| 132 |
explicit uppername_t( const std::string& input ) : upper(input) { |
| 133 |
std::transform(input.begin(), input.end(), upper.begin(), |
| 134 |
[]( char ch ) { return TOUPPER(ch); } ); |
| 135 |
} |
| 136 |
const char *data() const { return upper.data(); } |
| 137 |
}; |
| 138 |
|
| 139 |
class copybook_t { |
| 140 |
std::list<const char *> directories; |
| 141 |
copybook_elem_t book; |
| 142 |
|
| 143 |
// Take copybook name from the environment, if defined, else use it verbatim. |
| 144 |
static const char * transform_name( const char name[] ) { |
| 145 |
uppername_t uname(name); |
| 146 |
const char *value = getenv(name); |
| 147 |
if( !value ) { |
| 148 |
value = getenv(uname.data()); // try uppercase of envar name |
| 149 |
if( !value ) value = name; // keep original unmodified |
| 150 |
} |
| 151 |
return xstrdup(value); |
| 152 |
} |
| 153 |
|
| 154 |
public: |
| 155 |
copybook_t() { directories.push_back(NULL); } |
| 156 |
|
| 157 |
void suppress( bool tf = true ) { book.suppress = tf; } |
| 158 |
bool suppressed() { return book.suppress; } |
| 159 |
void source( const YYLTYPE& loc, const char name[] ) { |
| 160 |
book.source.loc = loc; |
| 161 |
book.literally.source = copybook_elem_t::quoted(name); |
| 162 |
book.source.name = book.literally.source? |
| 163 |
copybook_elem_t::dequote(name) : transform_name(name); |
| 164 |
} |
| 165 |
void library( const YYLTYPE& loc, const char name[] ) { |
| 166 |
book.library.loc = loc; |
| 167 |
book.literally.library = copybook_elem_t::quoted(name); |
| 168 |
book.library.name = book.literally.library? |
| 169 |
copybook_elem_t::dequote(name) : transform_name(name); |
| 170 |
} |
| 171 |
void replacement( replace_type_t type, const char src[], const char tgt[] ) { |
| 172 |
copybook_replace_t elem = { type, src, tgt }; |
| 173 |
book.replacements.push_back(elem); |
| 174 |
} |
| 175 |
|
| 176 |
copybook_elem_t *current() { return &book; } |
| 177 |
const char *source() const { return book.source.name; } |
| 178 |
const char *library() const { return book.library.name; } |
| 179 |
|
| 180 |
int open(YYLTYPE loc, const char name[]) { |
| 181 |
int fd = -1; |
| 182 |
book.clear(); |
| 183 |
this->source(loc, name); |
| 184 |
|
| 185 |
for( auto dir : directories ) { |
| 186 |
dbgmsg("copybook_t::open '%s' OF '%s' %s", |
| 187 |
book.source.name, |
| 188 |
dir? dir: ".", |
| 189 |
book.literally.source? ", literally" : "" ); |
| 190 |
if( (fd = book.open_file(dir, book.literally.source)) != -1 ) break; |
| 191 |
} |
| 192 |
return fd; |
| 193 |
} |
| 194 |
|
| 195 |
const char * directory_add( const char name[] ) { |
| 196 |
directories.push_back(name); |
| 197 |
return name; |
| 198 |
} |
| 199 |
void extensions_add( const char ext[], const char alt[] ); |
| 200 |
}; |
| 201 |
|
| 202 |
extern copybook_t copybook; |
| 203 |
|
| 204 |
#endif |