| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/TADS2/cmap.c,v 1.2 1999/05/17 02:52:14 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | cmap.c - character mapping implementation |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 05/31/98 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "os.h" |
| 26 | #include "cmap.h" |
| 27 | #include "err.h" |
| 28 | #include "tio.h" |
| 29 | |
| 30 | |
| 31 | /* ------------------------------------------------------------------------ */ |
| 32 | /* |
| 33 | * Global variables for character mapping tables |
| 34 | */ |
| 35 | |
| 36 | unsigned char G_cmap_input[256]; |
| 37 | unsigned char G_cmap_output[256]; |
| 38 | char G_cmap_id[5]; |
| 39 | char G_cmap_ldesc[CMAP_LDESC_MAX_LEN + 1]; |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * static variables |
| 44 | */ |
| 45 | |
| 46 | /* |
| 47 | * flag: true -> a character set has been explicitly loaded, so we |
| 48 | * should ignore any game character set setting |
| 49 | */ |
| 50 | static int S_cmap_loaded; |
| 51 | |
| 52 | |
| 53 | /* ------------------------------------------------------------------------ */ |
| 54 | /* |
| 55 | * Initialize the default character mappings |
| 56 | */ |
| 57 | void cmap_init_default(void) |
| 58 | { |
| 59 | size_t i; |
| 60 | |
| 61 | /* initialize the input table */ |
| 62 | for (i = 0 ; i < sizeof(G_cmap_input)/sizeof(G_cmap_input[0]) ; ++i) |
| 63 | G_cmap_input[i] = (unsigned char)i; |
| 64 | |
| 65 | /* initialize the output table */ |
| 66 | for (i = 0 ; i < sizeof(G_cmap_output)/sizeof(G_cmap_output[0]) ; ++i) |
| 67 | G_cmap_output[i] = (unsigned char)i; |
| 68 | |
| 69 | /* we have a null ID */ |
| 70 | memset(G_cmap_id, 0, sizeof(G_cmap_id)); |
| 71 | |
| 72 | /* indicate that it's the default */ |
| 73 | strcpy(G_cmap_ldesc, "(native/no mapping)"); |
| 74 | |
| 75 | /* note that we have no character set loaded */ |
| 76 | S_cmap_loaded = FALSE; |
| 77 | } |
| 78 | |
| 79 | /* ------------------------------------------------------------------------ */ |
| 80 | /* |
| 81 | * Internal routine to load a character map from a file |
| 82 | */ |
| 83 | static int cmap_load_internal(char *filename) |
| 84 | { |
| 85 | osfildef *fp; |
| 86 | static char sig1[] = CMAP_SIG_S100; |
| 87 | char buf[256]; |
| 88 | uchar lenbuf[2]; |
| 89 | size_t len; |
| 90 | int sysblk; |
| 91 | |
| 92 | /* if there's no mapping file, use the default mapping */ |
| 93 | if (filename == 0) |
| 94 | { |
| 95 | /* initialize with the default mapping */ |
| 96 | cmap_init_default(); |
| 97 | |
| 98 | /* return success */ |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* open the file */ |
| 103 | fp = osfoprb(filename, OSFTCMAP); |
| 104 | if (fp == 0) |
| 105 | return 1; |
| 106 | |
| 107 | /* check the signature */ |
| 108 | if (osfrb(fp, buf, sizeof(sig1)) |
| 109 | || memcmp(buf, sig1, sizeof(sig1)) != 0) |
| 110 | { |
| 111 | osfcls(fp); |
| 112 | return 2; |
| 113 | } |
| 114 | |
| 115 | /* load the ID */ |
| 116 | G_cmap_id[4] = '\0'; |
| 117 | if (osfrb(fp, G_cmap_id, 4)) |
| 118 | { |
| 119 | osfcls(fp); |
| 120 | return 3; |
| 121 | } |
| 122 | |
| 123 | /* load the long description */ |
| 124 | if (osfrb(fp, lenbuf, 2) |
| 125 | || (len = osrp2(lenbuf)) > sizeof(G_cmap_ldesc) |
| 126 | || osfrb(fp, G_cmap_ldesc, len)) |
| 127 | { |
| 128 | osfcls(fp); |
| 129 | return 4; |
| 130 | } |
| 131 | |
| 132 | /* load the two tables - input, then output */ |
| 133 | if (osfrb(fp, G_cmap_input, sizeof(G_cmap_input)) |
| 134 | || osfrb(fp, G_cmap_output, sizeof(G_cmap_output))) |
| 135 | { |
| 136 | osfcls(fp); |
| 137 | return 5; |
| 138 | } |
| 139 | |
| 140 | /* read the next section header */ |
| 141 | if (osfrb(fp, buf, 4)) |
| 142 | { |
| 143 | osfcls(fp); |
| 144 | return 6; |
| 145 | } |
| 146 | |
| 147 | /* if it's "SYSI", read the system information string */ |
| 148 | if (!memcmp(buf, "SYSI", 4)) |
| 149 | { |
| 150 | /* read the length prefix, then the string */ |
| 151 | if (osfrb(fp, lenbuf, 2) |
| 152 | || (len = osrp2(lenbuf)) > sizeof(buf) |
| 153 | || osfrb(fp, buf, len)) |
| 154 | { |
| 155 | osfcls(fp); |
| 156 | return 7; |
| 157 | } |
| 158 | |
| 159 | /* we have a system information block */ |
| 160 | sysblk = TRUE; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | /* there's no system information block */ |
| 165 | sysblk = FALSE; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * call the OS code, so that it can do any system-dependent |
| 170 | * initialization for the new character mapping |
| 171 | */ |
| 172 | os_advise_load_charmap(G_cmap_id, G_cmap_ldesc, sysblk ? buf : ""); |
| 173 | |
| 174 | /* read the next section header */ |
| 175 | if (sysblk && osfrb(fp, buf, 4)) |
| 176 | { |
| 177 | osfcls(fp); |
| 178 | return 8; |
| 179 | } |
| 180 | |
| 181 | /* see if we have an entity list */ |
| 182 | if (!memcmp(buf, "ENTY", 4)) |
| 183 | { |
| 184 | /* read the entities */ |
| 185 | for (;;) |
| 186 | { |
| 187 | size_t len; |
| 188 | unsigned int cval; |
| 189 | char expansion[CMAP_MAX_ENTITY_EXPANSION]; |
| 190 | |
| 191 | /* read the next item's length and character value */ |
| 192 | if (osfrb(fp, buf, 4)) |
| 193 | { |
| 194 | osfcls(fp); |
| 195 | return 9; |
| 196 | } |
| 197 | |
| 198 | /* decode the values */ |
| 199 | len = osrp2(buf); |
| 200 | cval = osrp2(buf+2); |
| 201 | |
| 202 | /* if we've reached the zero marker, we're done */ |
| 203 | if (len == 0 && cval == 0) |
| 204 | break; |
| 205 | |
| 206 | /* read the string */ |
| 207 | if (len > CMAP_MAX_ENTITY_EXPANSION |
| 208 | || osfrb(fp, expansion, len)) |
| 209 | { |
| 210 | osfcls(fp); |
| 211 | return 10; |
| 212 | } |
| 213 | |
| 214 | /* tell the output code about the expansion */ |
| 215 | tio_set_html_expansion(cval, expansion, len); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * ignore anything else we find - if the file format is updated to |
| 221 | * include extra information in the future, and this old code tries |
| 222 | * to load an updated file, we'll just ignore the new information, |
| 223 | * which should always be placed after the "SYSI" block (if present) |
| 224 | * to ensure compatibility with past versions (such as this code) |
| 225 | */ |
| 226 | |
| 227 | /* no problems - close the file and return success */ |
| 228 | osfcls(fp); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /* ------------------------------------------------------------------------ */ |
| 234 | /* |
| 235 | * Explicitly load a character set from a file. This character set |
| 236 | * mapping will override any implicit character set mapping that we read |
| 237 | * from a game file. This should be called when the player explicitly |
| 238 | * loads a character set (via a command line option or similar action). |
| 239 | */ |
| 240 | int cmap_load(char *filename) |
| 241 | { |
| 242 | int err; |
| 243 | |
| 244 | /* try loading the file */ |
| 245 | if ((err = cmap_load_internal(filename)) != 0) |
| 246 | return err; |
| 247 | |
| 248 | /* |
| 249 | * note that we've explicitly loaded a character set, if they named |
| 250 | * a character set (if not, this simply establishes the default |
| 251 | * setting, so we haven't explicitly loaded anything) |
| 252 | */ |
| 253 | if (filename != 0) |
| 254 | S_cmap_loaded = TRUE; |
| 255 | |
| 256 | /* success */ |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /* ------------------------------------------------------------------------ */ |
| 262 | /* |
| 263 | * Explicitly override any game character set and use no character set |
| 264 | * instead. |
| 265 | */ |
| 266 | void cmap_override(void) |
| 267 | { |
| 268 | /* apply the default mapping */ |
| 269 | cmap_init_default(); |
| 270 | |
| 271 | /* |
| 272 | * pretend we have a character map loaded, so that we don't try to |
| 273 | * load another one if the game specifies a character set |
| 274 | */ |
| 275 | S_cmap_loaded = TRUE; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /* ------------------------------------------------------------------------ */ |
| 280 | /* |
| 281 | * Set the game's internal character set. This is called when a game is |
| 282 | * loaded, and the game specifies a character set. |
| 283 | */ |
| 284 | void cmap_set_game_charset(errcxdef *ec, |
| 285 | char *internal_id, char *internal_ldesc, |
| 286 | char *argv0) |
| 287 | { |
| 288 | char filename[OSFNMAX]; |
| 289 | |
| 290 | /* |
| 291 | * If a character set is already explicitly loaded, ignore the |
| 292 | * game's character set - the player asked us to use a particular |
| 293 | * mapping, so ignore what the game wants. (This will probably |
| 294 | * result in incorrect display of non-ASCII character values, but |
| 295 | * the player is most likely to use this to avoid errors when an |
| 296 | * appropriate mapping file for the game is not available. In this |
| 297 | * case, the player informs us by setting the option that he or she |
| 298 | * knows and accepts that the game will not look exactly right.) |
| 299 | */ |
| 300 | if (S_cmap_loaded) |
| 301 | return; |
| 302 | |
| 303 | /* |
| 304 | * ask the operating system to name the mapping file -- this routine |
| 305 | * will determine, if possible, the current native character set, |
| 306 | * and apply a system-specific naming convention to tell us what |
| 307 | * mapping file we should open |
| 308 | */ |
| 309 | os_gen_charmap_filename(filename, internal_id, argv0); |
| 310 | |
| 311 | /* try loading the mapping file */ |
| 312 | if (cmap_load_internal(filename)) |
| 313 | errsig2(ec, ERR_CHRNOFILE, |
| 314 | ERRTSTR, errstr(ec, filename, strlen(filename)), |
| 315 | ERRTSTR, errstr(ec, internal_ldesc, strlen(internal_ldesc))); |
| 316 | |
| 317 | /* |
| 318 | * We were successful - the game's internal character set is now |
| 319 | * mapped to the current native character set. Even though we |
| 320 | * loaded an ldesc from the mapping file, forget that and store the |
| 321 | * internal ldesc that the game specified. The reason we do this is |
| 322 | * that it's possible that the player will dynamically switch native |
| 323 | * character sets in the future, at which point we'll need to |
| 324 | * re-load the mapping table, which could raise an error if a |
| 325 | * mapping file for the new character set isn't available. So, we |
| 326 | * may need to provide the same explanation later that we needed to |
| 327 | * provide here. Save the game's character set ldesc for that |
| 328 | * eventuality, since it describes exactly what the *game* wanted. |
| 329 | */ |
| 330 | strcpy(G_cmap_ldesc, internal_ldesc); |
| 331 | } |
| 332 |