1 |
/* This file is part of the svarlang project and is published under the terms
|
1 |
/* This file is part of the svarlang project and is published under the terms
|
2 |
* of the MIT license.
|
2 |
* of the MIT license.
|
3 |
*
|
3 |
*
|
4 |
* Copyright (C) 2021-2023 Mateusz Viste
|
4 |
* Copyright (C) 2021-2023 Mateusz Viste
|
5 |
*
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
7 |
* copy of this software and associated documentation files (the "Software"),
|
7 |
* copy of this software and associated documentation files (the "Software"),
|
8 |
* to deal in the Software without restriction, including without limitation
|
8 |
* to deal in the Software without restriction, including without limitation
|
9 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
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
|
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:
|
11 |
* Software is furnished to do so, subject to the following conditions:
|
12 |
*
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
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,
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
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
|
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
|
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
|
21 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
22 |
* DEALINGS IN THE SOFTWARE.
|
22 |
* DEALINGS IN THE SOFTWARE.
|
23 |
*/
|
23 |
*/
|
24 |
|
24 |
|
25 |
#include <stdlib.h> /* _psp */
|
25 |
#include <stdlib.h> /* NULL */
|
26 |
#include <string.h> /* _fmemcpy() */
|
26 |
#include <stdio.h>
|
27 |
#include <i86.h> /* MK_FP() */
|
- |
|
28 |
|
27 |
|
29 |
#include "svarlang.h"
|
28 |
#include "svarlang.h"
|
30 |
|
29 |
|
31 |
int svarlang_autoload_exepath(const char *lang) {
|
30 |
int svarlang_autoload_exepath(const char *selfexe, 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;
|
31 |
unsigned short selflen;
|
- |
|
32 |
char self_ext_backup[3];
|
35 |
char orig_ext[3];
|
33 |
char *self_ext_ptr;
|
36 |
int res;
|
34 |
int res;
|
37 |
|
35 |
|
38 |
/* who am i? look into my own environment, at the end of it should be my EXEPATH string */
|
36 |
/* validate selfexe: must be at least 5 bytes long and 4th char from end must
|
39 |
while (*myenv != 0) {
|
- |
|
40 |
/* consume a NULL-terminated string */
|
37 |
* be a dot (like "a.exe" or "c:\b.com" or "..\..\test\run.exe") */
|
41 |
while (*myenv != 0) myenv++;
|
38 |
if (selfexe == NULL) return(-1);
|
42 |
/* move to next string */
|
- |
|
43 |
myenv++;
|
- |
|
44 |
}
|
- |
|
45 |
myenv++; /* skip the nul terminator */
|
- |
|
46 |
|
- |
|
47 |
/* check next word, if 1 then EXEPATH follows */
|
39 |
for (selflen = 0; selfexe[selflen] != 0; selflen++);
|
48 |
if (*myenv != 1) return(-1);
|
- |
|
49 |
myenv++;
|
- |
|
50 |
if (*myenv != 0) return(-1);
|
40 |
if ((selflen < 5) || (selfexe[selflen - 4] != '.')) return(-2);
|
51 |
myenv++;
|
- |
|
52 |
|
41 |
|
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") */
|
42 |
self_ext_ptr = (char *)selfexe + selflen - 3; /* disregard CONST (I revert original content later, so the caller won't notice */
|
57 |
if ((len < 5) || (myenv[len - 4] != '.')) return(-1);
|
- |
|
58 |
|
43 |
|
59 |
/* copy extension to buffer and replace it with "lng" */
|
44 |
/* copy extension to buffer and replace it with "lng" */
|
60 |
orig_ext[0] = myenv[len - 3];
|
45 |
self_ext_backup[0] = self_ext_ptr[0];
|
61 |
orig_ext[1] = myenv[len - 2];
|
46 |
self_ext_backup[1] = self_ext_ptr[1];
|
62 |
orig_ext[2] = myenv[len - 1];
|
47 |
self_ext_backup[2] = self_ext_ptr[2];
|
63 |
|
48 |
|
64 |
myenv[len - 3] = 'L';
|
49 |
self_ext_ptr[0] = 'L';
|
65 |
myenv[len - 2] = 'N';
|
50 |
self_ext_ptr[1] = 'N';
|
66 |
myenv[len - 1] = 'G';
|
51 |
self_ext_ptr[2] = 'G';
|
67 |
|
52 |
|
68 |
/* try loading it now */
|
53 |
/* 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 */
|
54 |
res = svarlang_load(selfexe, lang);
|
70 |
|
55 |
|
71 |
/* restore the original filename and quit */
|
56 |
/* restore the original filename and quit */
|
72 |
myenv[len - 3] = orig_ext[0];
|
57 |
self_ext_ptr[0] = self_ext_backup[0];
|
73 |
myenv[len - 2] = orig_ext[1];
|
58 |
self_ext_ptr[1] = self_ext_backup[1];
|
74 |
myenv[len - 1] = orig_ext[2];
|
59 |
self_ext_ptr[2] = self_ext_backup[2];
|
75 |
|
60 |
|
76 |
return(res);
|
61 |
return(res);
|
77 |
}
|
62 |
}
|
78 |
|
63 |
|