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> /* getenv(), NULL */
|
- |
|
26 |
#include "svarlang.h"
|
25 |
#include "svarlang.h"
|
27 |
|
26 |
|
28 |
|
27 |
|
29 |
int svarlang_autoload_nlspath(const char *progname) {
|
28 |
int svarlang_autoload_pathlist(const char *progname, const char *pathlist, const char *lang) {
|
30 |
const char *lang;
|
- |
|
31 |
const char *nlspath;
|
- |
|
32 |
char buff[128];
|
29 |
char buff[128];
|
33 |
unsigned short i, ii;
|
30 |
unsigned short i, ii;
|
34 |
|
31 |
|
35 |
/* read and validate LANG */
|
32 |
/* read and validate LANG and pathlist */
|
36 |
lang = getenv("LANG");
|
- |
|
37 |
if ((lang == NULL) || (lang[0] == 0)) return(-1);
|
33 |
if ((!lang) || (lang[0] == 0) || (!pathlist)) return(-1);
|
38 |
|
- |
|
39 |
/* read and validate NLSPATH */
|
- |
|
40 |
nlspath = getenv("NLSPATH");
|
- |
|
41 |
if (nlspath == NULL) return(-2);
|
- |
|
42 |
|
34 |
|
43 |
/* look into every path in NLSPATH */
|
35 |
/* look into every path in NLSPATH */
|
44 |
while (*nlspath != 0) {
|
36 |
while (*pathlist != 0) {
|
45 |
|
37 |
|
46 |
/* skip any leading ';' separators */
|
38 |
/* skip any leading ';' separators */
|
47 |
while (*nlspath == ';') nlspath++;
|
39 |
while (*pathlist == ';') pathlist++;
|
48 |
|
40 |
|
49 |
if (*nlspath == 0) return(-3);
|
41 |
if (*pathlist == 0) return(-3);
|
50 |
|
42 |
|
51 |
/* copy nlspath to buff and remember len */
|
43 |
/* copy nlspath to buff and remember len */
|
52 |
for (i = 0; (nlspath[i] != 0) && (nlspath[i] != ';'); i++) buff[i] = nlspath[i];
|
44 |
for (i = 0; (pathlist[i] != 0) && (pathlist[i] != ';'); i++) buff[i] = pathlist[i];
|
53 |
nlspath += i;
|
45 |
pathlist += i;
|
54 |
|
46 |
|
55 |
/* add a trailing backslash if there is none (non-empty paths empty) */
|
47 |
/* add a trailing backslash if there is none (non-empty paths empty) */
|
56 |
if ((i > 0) && (buff[i - 1] != '\\')) buff[i++] = '\\';
|
48 |
if ((i > 0) && (buff[i - 1] != '\\')) buff[i++] = '\\';
|
57 |
|
49 |
|
58 |
/* append progname + ".LNG" to the path */
|
50 |
/* append progname + ".LNG" to the path */
|
59 |
for (ii = 0; progname[ii] != 0; ii++) buff[i++] = progname[ii];
|
51 |
for (ii = 0; progname[ii] != 0; ii++) buff[i++] = progname[ii];
|
60 |
buff[i++] = '.';
|
52 |
buff[i++] = '.';
|
61 |
buff[i++] = 'L';
|
53 |
buff[i++] = 'L';
|
62 |
buff[i++] = 'N';
|
54 |
buff[i++] = 'N';
|
63 |
buff[i++] = 'G';
|
55 |
buff[i++] = 'G';
|
64 |
buff[i] = 0;
|
56 |
buff[i] = 0;
|
65 |
|
57 |
|
66 |
if (svarlang_load(buff, lang) == 0) return(0);
|
58 |
if (svarlang_load(buff, lang) == 0) return(0);
|
67 |
}
|
59 |
}
|
68 |
/* failed to load anything */
|
60 |
/* failed to load anything */
|
69 |
return(-4);
|
61 |
return(-4);
|
70 |
}
|
62 |
}
|
71 |
|
63 |
|