1278 |
mateusz.vi |
1 |
/* This file is part of the svarlang project and is published under the terms
|
|
|
2 |
* of the MIT license.
|
|
|
3 |
*
|
|
|
4 |
* Copyright (C) 2021-2023 Mateusz Viste
|
|
|
5 |
*
|
|
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
7 |
* copy of this software and associated documentation files (the "Software"),
|
|
|
8 |
* to deal in the Software without restriction, including without limitation
|
|
|
9 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
10 |
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
11 |
* Software is furnished to do so, subject to the following conditions:
|
|
|
12 |
*
|
|
|
13 |
* The above copyright notice and this permission notice shall be included in
|
|
|
14 |
* all copies or substantial portions of the Software.
|
|
|
15 |
*
|
|
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
22 |
* DEALINGS IN THE SOFTWARE.
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
#include <stdlib.h> /* _psp */
|
|
|
26 |
#include <string.h> /* _fmemcpy() */
|
|
|
27 |
#include <i86.h> /* MK_FP() */
|
|
|
28 |
|
|
|
29 |
#include "svarlang.h"
|
|
|
30 |
|
|
|
31 |
int svarlang_autoload_exepath(const char *lang) {
|
|
|
32 |
unsigned short far *psp_envseg = MK_FP(_psp, 0x2c); /* pointer to my env segment field in the PSP */
|
|
|
33 |
char far *myenv = MK_FP(*psp_envseg, 0);
|
|
|
34 |
unsigned short len;
|
|
|
35 |
char orig_ext[3];
|
|
|
36 |
int res;
|
|
|
37 |
|
|
|
38 |
/* who am i? look into my own environment, at the end of it should be my EXEPATH string */
|
|
|
39 |
while (*myenv != 0) {
|
|
|
40 |
/* consume a NULL-terminated string */
|
|
|
41 |
while (*myenv != 0) myenv++;
|
|
|
42 |
/* move to next string */
|
|
|
43 |
myenv++;
|
|
|
44 |
}
|
|
|
45 |
myenv++; /* skip the nul terminator */
|
|
|
46 |
|
|
|
47 |
/* check next word, if 1 then EXEPATH follows */
|
|
|
48 |
if (*myenv != 1) return(-1);
|
|
|
49 |
myenv++;
|
|
|
50 |
if (*myenv != 0) return(-1);
|
|
|
51 |
myenv++;
|
|
|
52 |
|
|
|
53 |
/* myenv contains my full name, find end of string now */
|
|
|
54 |
for (len = 0; myenv[len] != 0; len++);
|
|
|
55 |
|
|
|
56 |
/* must be at least 5 bytes long and 4th char from end must be a dot (like "a.exe") */
|
|
|
57 |
if ((len < 5) || (myenv[len - 4] != '.')) return(-1);
|
|
|
58 |
|
|
|
59 |
/* copy extension to buffer and replace it with "lng" */
|
|
|
60 |
orig_ext[0] = myenv[len - 3];
|
|
|
61 |
orig_ext[1] = myenv[len - 2];
|
|
|
62 |
orig_ext[2] = myenv[len - 1];
|
|
|
63 |
|
|
|
64 |
myenv[len - 3] = 'L';
|
|
|
65 |
myenv[len - 2] = 'N';
|
|
|
66 |
myenv[len - 1] = 'G';
|
|
|
67 |
|
|
|
68 |
/* try loading it now */
|
|
|
69 |
res = svarlang_load(myenv, lang); /* TODO FIXME myenv is a far pointer, while svarlang_load() in small or medium memory models expects a near ptr */
|
|
|
70 |
|
|
|
71 |
/* restore the original filename and quit */
|
|
|
72 |
myenv[len - 3] = orig_ext[0];
|
|
|
73 |
myenv[len - 2] = orig_ext[1];
|
|
|
74 |
myenv[len - 1] = orig_ext[2];
|
|
|
75 |
|
|
|
76 |
return(res);
|
|
|
77 |
}
|