| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | // InfFile.cpp: implementation of the InfFile class. |
| 20 | // |
| 21 | ////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | #include "inffile.h"cxx . . $ `<file>/cfile/inffile.h`/ |
| 24 | |
| 25 | #include "cfile.h"cxx . . $ `<file>/cfile/cfile.h`/ |
| 26 | #include "pstring.h"cxx . . $ `<file>/misc/pstring.h`/ |
| 27 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 28 | |
| 29 | #include <string.h> |
| 30 | |
| 31 | #define INFFILE_NULLcxx . . $ `cfile/inffile.cpp:31:9`! (-1024) |
| 32 | |
| 33 | ////////////////////////////////////////////////////////////////////// |
| 34 | // Construction/Destruction |
| 35 | ////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | InfFilecxx . . $ InfFile#::InfFilecxx . . $ InfFile#InfFile(49f6e7a06ebc5aa8).() { m_fpcxx . . $ InfFile#m_fp. = nullptr; } |
| 38 | |
| 39 | InfFilecxx . . $ InfFile#::~cxx . . $ InfFile#`~InfFile`(ced63f7c635d850d).InfFilecxx . . $ InfFile#() { |
| 40 | // close file and free symbol lists |
| 41 | Closecxx . . $ InfFile#Close(49f6e7a06ebc5aa8).(); |
| 42 | } |
| 43 | |
| 44 | // symbol functions. |
| 45 | void InfFilecxx . . $ InfFile#::AddSymbolcxx . . $ InfFile#AddSymbol(2fa786934fe80e24).(const char *name, const char *text) { |
| 46 | tListNodecxx . . $ tListNode#<sym_infocxx . . $ InfFile#sym_info#> *symbol = new tListNodecxx . . $ tListNode#<sym_infocxx . . $ InfFile#sym_info#>; |
| 47 | |
| 48 | strcpy(symbol->tcxx . . $ tListNode#t..namecxx . . $ InfFile#sym_info#name., name); |
| 49 | symbol->tcxx . . $ tListNode#t..textcxx . . $ InfFile#sym_info#text. = new char[strlen(text) + 1]; |
| 50 | strcpy(symbol->tcxx . . $ tListNode#t..textcxx . . $ InfFile#sym_info#text., text); |
| 51 | |
| 52 | m_sym_listcxx . . $ InfFile#m_sym_list..linkcxx . . $ tList#link(fb0c6bc9adb09be2).(symbol); |
| 53 | } |
| 54 | |
| 55 | void InfFilecxx . . $ InfFile#::FreeSymbolscxx . . $ InfFile#FreeSymbols(49f6e7a06ebc5aa8).() { |
| 56 | tListNodecxx . . $ tListNode#<sym_infocxx . . $ InfFile#sym_info#> *sym; |
| 57 | |
| 58 | m_sym_listcxx . . $ InfFile#m_sym_list..startcxx . . $ tList#start(fe6b4b23a955992d).(); |
| 59 | |
| 60 | while ((sym = m_sym_listcxx . . $ InfFile#m_sym_list..startcxx . . $ tList#start(fe6b4b23a955992d).()) != 0) { |
| 61 | sym = m_sym_listcxx . . $ InfFile#m_sym_list..unlinkcxx . . $ tList#unlink(fe6b4b23a955992d).(); |
| 62 | if (sym->tcxx . . $ tListNode#t..textcxx . . $ InfFile#sym_info#text.) |
| 63 | delete sym->tcxx . . $ tListNode#t..textcxx . . $ InfFile#sym_info#text.; |
| 64 | delete sym; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const char *InfFilecxx . . $ InfFile#::GetSymbolTextcxx . . $ InfFile#GetSymbolText(fb58d2e09a2a1ce6).(const char *name) { |
| 69 | tListNodecxx . . $ tListNode#<sym_infocxx . . $ InfFile#sym_info#> *sym; |
| 70 | |
| 71 | m_sym_listcxx . . $ InfFile#m_sym_list..startcxx . . $ tList#start(fe6b4b23a955992d).(); |
| 72 | for (sym = m_sym_listcxx . . $ InfFile#m_sym_list..startcxx . . $ tList#start(fe6b4b23a955992d).(); sym; sym = sym->nextcxx . . $ tListNode#next.) { |
| 73 | if (strcmp(sym->tcxx . . $ tListNode#t..namecxx . . $ InfFile#sym_info#name., name) == 0) |
| 74 | return (const char *)sym->tcxx . . $ tListNode#t..textcxx . . $ InfFile#sym_info#text.; |
| 75 | } |
| 76 | |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | // opens an inf file, pass in a lexical analyzer that will return a command index. |
| 81 | // tag_check is a string that must appear at the very beginning of the file. |
| 82 | // lexfn is a function to match a command to a number. num <= 0 are taken. |
| 83 | bool InfFilecxx . . $ InfFile#::Opencxx . . $ InfFile#Open(a6c18f6c547e5631).(const char *filename, const char *tag_check, int (*lexfn)(const char *command)) { |
| 84 | if (m_fpcxx . . $ InfFile#m_fp.) |
| 85 | return false; |
| 86 | |
| 87 | m_fpcxx . . $ InfFile#m_fp. = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(filenamecxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d)., "rt"); |
| 88 | if (!m_fpcxx . . $ InfFile#m_fp.) |
| 89 | return false; |
| 90 | |
| 91 | // do tag checking. |
| 92 | if (tag_check) { |
| 93 | cf_ReadStringcxx . . $ cf_ReadString(38cd788e4423eff6).(m_linetextcxx . . $ InfFile#m_linetext., INFFILE_LINELENcxx . . $ INFFILE_LINELEN., m_fpcxx . . $ InfFile#m_fp.); |
| 94 | if (strcmp(tag_check, m_linetextcxx . . $ InfFile#m_linetext.) != 0) { |
| 95 | cfclosecxx . . $ cfclose(3639e799710acb68).(m_fpcxx . . $ InfFile#m_fp.); |
| 96 | m_fpcxx . . $ InfFile#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // okay we have a 'valid' inf file. |
| 102 | m_lexfncxx . . $ InfFile#m_lexfn. = lexfn; |
| 103 | m_lineptrcxx . . $ InfFile#m_lineptr. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 104 | m_endptrcxx . . $ InfFile#m_endptr. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 105 | m_linecxx . . $ InfFile#m_line. = 0; |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | // closes the file |
| 111 | void InfFilecxx . . $ InfFile#::Closecxx . . $ InfFile#Close(49f6e7a06ebc5aa8).() { |
| 112 | if (!m_fpcxx . . $ InfFile#m_fp.) |
| 113 | return; |
| 114 | |
| 115 | FreeSymbolscxx . . $ InfFile#FreeSymbols(49f6e7a06ebc5aa8).(); |
| 116 | m_sym_listcxx . . $ InfFile#m_sym_list..freecxx . . $ tList#free(49f6e7a06ebc5aa8).(); |
| 117 | cfclosecxx . . $ cfclose(3639e799710acb68).(m_fpcxx . . $ InfFile#m_fp.); |
| 118 | m_fpcxx . . $ InfFile#m_fp. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 119 | } |
| 120 | |
| 121 | // read a command line. returns false on eof. |
| 122 | bool InfFilecxx . . $ InfFile#::ReadLinecxx . . $ InfFile#ReadLine(c9857a73d68e59ba).() { |
| 123 | if (cfeofcxx . . $ cfeof(413716a422df0036).(m_fpcxx . . $ InfFile#m_fp.)) |
| 124 | return false; |
| 125 | |
| 126 | cf_ReadStringcxx . . $ cf_ReadString(38cd788e4423eff6).(m_linetextcxx . . $ InfFile#m_linetext., INFFILE_LINELENcxx . . $ INFFILE_LINELEN., m_fpcxx . . $ InfFile#m_fp.); |
| 127 | |
| 128 | // start line. |
| 129 | m_lineptrcxx . . $ InfFile#m_lineptr. = &m_linetextcxx . . $ InfFile#m_linetext.[0]; |
| 130 | m_endptrcxx . . $ InfFile#m_endptr. = m_lineptrcxx . . $ InfFile#m_lineptr. + strlen(m_lineptrcxx . . $ InfFile#m_lineptr.) + 1; |
| 131 | m_linecxx . . $ InfFile#m_line.++; |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | // parses a line of 'code'. return value from the lexfn |
| 137 | // a 0 for EOL (end of line). |
| 138 | // -1 for error. |
| 139 | // -2 for comment |
| 140 | int InfFilecxx . . $ InfFile#::ParseLinecxx . . $ InfFile#ParseLine(6abd07ddb1a912f0).(char *operand, int oprlen) { |
| 141 | if (!m_lineptrcxx . . $ InfFile#m_lineptr.) |
| 142 | return INFFILE_EOLcxx . . $ INFFILE_EOL.; |
| 143 | |
| 144 | // tokenize line. |
| 145 | char command[32]; // Command read from line. |
| 146 | char *opr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, *cmd; |
| 147 | int retval = INFFILE_NULLcxx . . $ `cfile/inffile.cpp:31:9`!; |
| 148 | |
| 149 | if (strlen(m_lineptrcxx . . $ InfFile#m_lineptr.) == 0) |
| 150 | return INFFILE_EOLcxx . . $ INFFILE_EOL.; |
| 151 | |
| 152 | // parse out command and operand (command=str) |
| 153 | // m_lineptr = command, opr = operand, nextcmd = next lineptr. |
| 154 | cmd = strtok(m_lineptrcxx . . $ InfFile#m_lineptr., " \t=:"); |
| 155 | if (cmd) |
| 156 | opr = strtok(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, ",;"); |
| 157 | else |
| 158 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 159 | |
| 160 | // clean out trailing and preceeding trash (tabs, spaces, etc) |
| 161 | CleanupStrcxx . . $ CleanupStr(55abe9014c6d0e39).(command, cmd, sizeof(command)); |
| 162 | if (opr) |
| 163 | CleanupStrcxx . . $ CleanupStr(55abe9014c6d0e39).(operand, opr, oprlen); |
| 164 | else |
| 165 | operand[0] = 0; |
| 166 | |
| 167 | if (strlen(command) == 0) { |
| 168 | if (strlen(operand) == 0) |
| 169 | return INFFILE_EOLcxx . . $ INFFILE_EOL.; |
| 170 | } |
| 171 | |
| 172 | // do predefined commands |
| 173 | if (command[0] == '@') |
| 174 | retval = INFFILE_COMMENTcxx . . $ INFFILE_COMMENT.; |
| 175 | |
| 176 | // do symbolic commands. |
| 177 | if (operand[0] == '#') { |
| 178 | const char *sym = GetSymbolTextcxx . . $ InfFile#GetSymbolText(fb58d2e09a2a1ce6).(&operand[1]); |
| 179 | if (sym) |
| 180 | strcpy(operand, sym); |
| 181 | else |
| 182 | retval = INFFILE_ERRORcxx . . $ INFFILE_ERROR.; |
| 183 | } |
| 184 | |
| 185 | if (retval == INFFILE_NULLcxx . . $ `cfile/inffile.cpp:31:9`!) { |
| 186 | if (command[0] == '#') { |
| 187 | AddSymbolcxx . . $ InfFile#AddSymbol(2fa786934fe80e24).(&command[1], operand); |
| 188 | retval = INFFILE_SYMBOLcxx . . $ INFFILE_SYMBOL.; |
| 189 | } else { |
| 190 | // do command through lex analyzer. if not preprocessed. |
| 191 | if (m_lexfncxx . . $ InfFile#m_lexfn.) |
| 192 | retval = (*m_lexfncxx . . $ InfFile#m_lexfn.)(command); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // adjust text pointers to prepare for next read |
| 197 | char *lastptr = (opr) ? opr : cmd; |
| 198 | m_lineptrcxx . . $ InfFile#m_lineptr. = lastptr + strlen(lastptr) + 1; |
| 199 | if (m_lineptrcxx . . $ InfFile#m_lineptr. >= m_endptrcxx . . $ InfFile#m_endptr.) |
| 200 | m_lineptrcxx . . $ InfFile#m_lineptr. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 201 | |
| 202 | // end |
| 203 | return retval; |
| 204 | } |