| 1 | // |
| 2 | // ACME - a crossassembler for producing 6502/65c02/65816 code. |
| 3 | // Copyright (C) 1998-2006 Marco Baye |
| 4 | // Have a look at "acme.c" for further info |
| 5 | // |
| 6 | // Character encoding stuff |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include "alu.h" |
| 11 | #include "acme.h" |
| 12 | #include "dynabuf.h" |
| 13 | #include "encoding.h" |
| 14 | #include "global.h" |
| 15 | #include "output.h" |
| 16 | #include "input.h" |
| 17 | #include "tree.h" |
| 18 | |
| 19 | |
| 20 | // Encoder function type definition |
| 21 | typedef char (*encoder_t)(char) ; |
| 22 | |
| 23 | |
| 24 | // Constants |
| 25 | static const char s_pet[] = "pet"; |
| 26 | static const char s_raw[] = "raw"; |
| 27 | static const char s_scr[] = "scr"; |
| 28 | |
| 29 | |
| 30 | // Variables |
| 31 | static char outermost_table[256]; // space for encoding table... |
| 32 | static char* loaded_table = outermost_table; // ...loaded from file |
| 33 | // predefined stuff |
| 34 | static node_t* encoder_tree = NULL; // tree to hold encoders |
| 35 | |
| 36 | |
| 37 | // Functions |
| 38 | |
| 39 | // convert character using current encoding |
| 40 | // Conversion function pointer. No init needed: gets set before each pass. |
| 41 | char (*Encoding_encode_char)(char); |
| 42 | |
| 43 | // Insert string(s) |
| 44 | static enum eos_t encode_string(encoder_t inner_encoder, char eor) { |
| 45 | encoder_t outer_encoder = Encoding_encode_char;// buffer encoder |
| 46 | |
| 47 | // make given encoder the current one (for ALU-parsed values) |
| 48 | Encoding_encode_char = inner_encoder; |
| 49 | do { |
| 50 | if(GotByte == '"') { |
| 51 | // read initial character |
| 52 | GetQuotedByte(); |
| 53 | // send characters until closing quote is reached |
| 54 | while(GotByte && (GotByte != '"')) { |
| 55 | Output_8b(eor ^ Encoding_encode_char(GotByte)); |
| 56 | GetQuotedByte(); |
| 57 | } |
| 58 | if(GotByte == CHAR_EOS) |
| 59 | return(AT_EOS_ANYWAY); |
| 60 | // after closing quote, proceed with next char |
| 61 | GetByte(); |
| 62 | } else { |
| 63 | // Parse value. No problems with single characters |
| 64 | // because the current encoding is |
| 65 | // temporarily set to the given one. |
| 66 | Output_8b(ALU_any_int()); |
| 67 | } |
| 68 | } while(Input_accept_comma()); |
| 69 | Encoding_encode_char = outer_encoder; // reactivate buffered encoder |
| 70 | return(ENSURE_EOS); |
| 71 | } |
| 72 | |
| 73 | // Insert text string (default format) |
| 74 | static enum eos_t PO_text(void) { |
| 75 | return(encode_string(Encoding_encode_char, 0)); |
| 76 | } |
| 77 | |
| 78 | // convert raw to raw (do not convert at all) |
| 79 | static char encoder_raw(char byte) { |
| 80 | return(byte); |
| 81 | } |
| 82 | |
| 83 | // Insert raw string |
| 84 | static enum eos_t PO_raw(void) { |
| 85 | return(encode_string(encoder_raw, 0)); |
| 86 | } |
| 87 | |
| 88 | // convert raw to petscii |
| 89 | static char encoder_pet(char byte) { |
| 90 | if((byte >= 'A') && (byte <= 'Z')) |
| 91 | return((char) (byte | 0x80)); // FIXME - check why SAS-C |
| 92 | if((byte >= 'a') && (byte <= 'z')) // wants these casts. |
| 93 | return((char)(byte - 32)); // There are more below. |
| 94 | return(byte); |
| 95 | } |
| 96 | |
| 97 | // Insert PetSCII string |
| 98 | static enum eos_t PO_pet(void) { |
| 99 | return(encode_string(encoder_pet, 0)); |
| 100 | } |
| 101 | |
| 102 | // convert raw to C64 screencode |
| 103 | static char encoder_scr(char byte) { |
| 104 | if((byte >= 'a') && (byte <= 'z')) |
| 105 | return((char)(byte - 96)); // shift uppercase down |
| 106 | if((byte >= '[') && (byte <= '_')) |
| 107 | return((char)(byte - 64)); // shift [\]^_ down |
| 108 | if(byte == '`') |
| 109 | return(64); // shift ` down |
| 110 | if(byte == '@') |
| 111 | return(0); // shift @ down |
| 112 | return(byte); |
| 113 | } |
| 114 | |
| 115 | // Insert screencode string |
| 116 | static enum eos_t PO_scr(void) { |
| 117 | return(encode_string(encoder_scr, 0)); |
| 118 | } |
| 119 | |
| 120 | // Insert screencode string, EOR'd |
| 121 | static enum eos_t PO_scrxor(void) { |
| 122 | intval_t num = ALU_any_int(); |
| 123 | |
| 124 | if(Input_accept_comma()) |
| 125 | return(encode_string(encoder_scr, num)); |
| 126 | Throw_error(exception_syntax); |
| 127 | return(SKIP_REMAINDER); |
| 128 | } |
| 129 | |
| 130 | // Switch to CBM mode ("!cbm" pseudo opcode) |
| 131 | static enum eos_t PO_cbm(void) { |
| 132 | Encoding_encode_char = encoder_pet; |
| 133 | // output deprecation warning |
| 134 | Throw_first_pass_warning("\"!cbm\" is deprecated; use \"!ct pet\" instead."); |
| 135 | return(ENSURE_EOS); |
| 136 | } |
| 137 | |
| 138 | // |
| 139 | static char encoder_file(char byte) { |
| 140 | return(loaded_table[(unsigned char) byte]); |
| 141 | } |
| 142 | |
| 143 | // read encoding table from file |
| 144 | static enum eos_t user_defined_encoding(void) { |
| 145 | FILE* fd; |
| 146 | char local_table[256], |
| 147 | *buffered_table = loaded_table; |
| 148 | encoder_t buffered_encoder = Encoding_encode_char; |
| 149 | |
| 150 | // if file name is missing, don't bother continuing |
| 151 | if(Input_read_filename(TRUE)) |
| 152 | return(SKIP_REMAINDER); |
| 153 | fd = fopen(GLOBALDYNABUF_CURRENT, FILE_READBINARY); |
| 154 | if(fd) { |
| 155 | if(fread(local_table, sizeof(char), 256, fd) != 256) |
| 156 | Throw_error("Conversion table incomplete."); |
| 157 | fclose(fd); |
| 158 | } else |
| 159 | Throw_error(exception_cannot_open_input_file); |
| 160 | Encoding_encode_char = encoder_file; // activate new encoding |
| 161 | loaded_table = local_table; // activate local table |
| 162 | // If there's a block, parse that and then restore old values |
| 163 | if(Parse_optional_block()) |
| 164 | Encoding_encode_char = buffered_encoder; |
| 165 | else |
| 166 | // if there's *no* block, the table must be used from now on. |
| 167 | // copy the local table to the "outer" table |
| 168 | memcpy(buffered_table, local_table, 256); |
| 169 | // re-activate "outer" table (it might have been changed by memcpy()) |
| 170 | loaded_table = buffered_table; |
| 171 | return(ENSURE_EOS); |
| 172 | } |
| 173 | |
| 174 | // use one of the pre-defined encodings (raw, pet, scr) |
| 175 | static enum eos_t predefined_encoding(void) { |
| 176 | void* node_body; |
| 177 | char local_table[256], |
| 178 | *buffered_table = loaded_table; |
| 179 | encoder_t buffered_encoder = Encoding_encode_char; |
| 180 | |
| 181 | // use one of the pre-defined encodings |
| 182 | if(Input_read_and_lower_keyword()) { |
| 183 | // search for tree item |
| 184 | if(Tree_easy_scan(encoder_tree, &node_body, GlobalDynaBuf)) |
| 185 | Encoding_encode_char = (encoder_t) node_body;// activate new encoder |
| 186 | else |
| 187 | Throw_error("Unknown encoding."); |
| 188 | } |
| 189 | loaded_table = local_table; // activate local table |
| 190 | // If there's a block, parse that and then restore old values |
| 191 | if(Parse_optional_block()) |
| 192 | Encoding_encode_char = buffered_encoder; |
| 193 | // re-activate "outer" table |
| 194 | loaded_table = buffered_table; |
| 195 | return(ENSURE_EOS); |
| 196 | } |
| 197 | |
| 198 | // Set current encoding ("!convtab" pseudo opcode) |
| 199 | static enum eos_t PO_convtab(void) { |
| 200 | if((GotByte == '<') || (GotByte == '"')) |
| 201 | return(user_defined_encoding()); |
| 202 | else |
| 203 | return(predefined_encoding()); |
| 204 | } |
| 205 | |
| 206 | // pseudo opcode table |
| 207 | static node_t pseudo_opcodes[] = { |
| 208 | PREDEFNODE(s_cbm, PO_cbm), |
| 209 | PREDEFNODE("ct", PO_convtab), |
| 210 | PREDEFNODE("convtab", PO_convtab), |
| 211 | PREDEFNODE(s_pet, PO_pet), |
| 212 | PREDEFNODE(s_raw, PO_raw), |
| 213 | PREDEFNODE(s_scr, PO_scr), |
| 214 | PREDEFNODE(s_scrxor, PO_scrxor), |
| 215 | PREDEFNODE("text", PO_text), |
| 216 | PREDEFLAST("tx", PO_text), |
| 217 | // ^^^^ this marks the last element |
| 218 | }; |
| 219 | |
| 220 | // keywords for "!convtab" pseudo opcode |
| 221 | static node_t encoders[] = { |
| 222 | PREDEFNODE(s_pet, encoder_pet), |
| 223 | PREDEFNODE(s_raw, encoder_raw), |
| 224 | PREDEFLAST(s_scr, encoder_scr), |
| 225 | // ^^^^ this marks the last element |
| 226 | }; |
| 227 | |
| 228 | |
| 229 | // Exported functions |
| 230 | |
| 231 | // register pseudo opcodes and build keyword tree for encoders |
| 232 | void Encoding_init(void) { |
| 233 | Tree_add_table(&encoder_tree, encoders); |
| 234 | Tree_add_table(&pseudo_opcode_tree, pseudo_opcodes); |
| 235 | } |
| 236 | |
| 237 | // Set "raw" as default encoding |
| 238 | void Encoding_passinit(void) { |
| 239 | Encoding_encode_char = encoder_raw; |
| 240 | } |