Debian Codegraph @

src/execenv.c

raw
1 /*
2 * execenv.c - housekeeping execution environment
3 *
4 * Copyright (c) 2020-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Sets up various bindings and parameters to access runtime environment
36 */
37
38
39 #define LIBGAUCHE_BODY
40 #include "gauche.h"
41 #include "gauche/priv/configP.h"
42
43 static const ScmPrimitiveParameter *command_line = NULL;
44 static const ScmPrimitiveParameter *os_command_line = NULL;
45 static const ScmPrimitiveParameter *script_file = NULL;
46
47 /*=============================================================
48 * Command line arguments
49 */
50
51 /*
52 * Scm_InitCommandLine is to be called by an application to initialize
53 * *program-name*, *argv* and (command-line). argv[0] becomes *program-name*,
54 * and *argv* gets the rest of arguments.
55 *
56 * Note that this is likely to be called before VM loop starts.
57 * Scm_Error isn't much useful yet.
58 */
59 #if GAUCHE_API_VERSION < 98
60 ScmObj Scm_InitCommandLine(int argc, const char *argv[])
61 {
62 return Scm_InitCommandLine2(argc, argv, SCM_COMMAND_LINE_SCRIPT);
63 }
64 ScmObj Scm_InitCommandLine2(int argc, const char *argv[], int kind)
65 #else /* GAUCHE_API_VERSION >= 98 */
66 ScmObj Scm_InitCommandLine(int argc, const char *argv[], int kind)
67 #endif /* GAUCHE_API_VERSION >= 98 */
68 {
69 SCM_ASSERT(argc > 0);
70 ScmObj args = Scm_CStringArrayToList(argv, argc, SCM_STRING_IMMUTABLE);
71
72 if (kind & SCM_COMMAND_LINE_OS) {
73 Scm_PrimitiveParameterSet(Scm_VM(), os_command_line, args);
74 }
75 if (kind & SCM_COMMAND_LINE_SCRIPT) {
76 Scm_PrimitiveParameterSet(Scm_VM(), command_line, args);
77
78 /* For the backward compatibility */
79 SCM_DEFINE(Scm_UserModule(), "*program-name*",
80 SCM_NULLP(args)? SCM_FALSE : SCM_CAR(args));
81 SCM_DEFINE(Scm_UserModule(), "*argv*",
82 SCM_NULLP(args)? SCM_NIL : SCM_CDR(args));
83
84 }
85 return args;
86 }
87
88 /*=============================================================
89 * Runtime introspection
90 */
91
92 #define PATH_ALLOC(n) SCM_MALLOC_ATOMIC(n)
93 #define PATH_ERROR(...) Scm_Error(__VA_ARGS__)
94 #include "paths.c"
95 #include "paths_arch.c" /* generated by genconfig */
96
97 const char *Scm_HostArchitecture()
98 {
99 return gauche_arch;
100 }
101
102 ScmObj Scm_LibraryDirectory()
103 {
104 static ScmObj dir = SCM_UNBOUND;
105 if (SCM_UNBOUNDP(dir)) {
106 dir = SCM_MAKE_STR_IMMUTABLE(replace_install_dir(gauche_lib_dir));
107 }
108 return dir;
109 }
110
111 ScmObj Scm_ArchitectureDirectory()
112 {
113 static ScmObj dir = SCM_UNBOUND;
114 if (SCM_UNBOUNDP(dir)) {
115 dir = SCM_MAKE_STR_IMMUTABLE(replace_install_dir(gauche_arch_dir));
116 }
117 return dir;
118 }
119
120 ScmObj Scm_SiteLibraryDirectory()
121 {
122 static ScmObj dir = SCM_UNBOUND;
123 if (SCM_UNBOUNDP(dir)) {
124 dir = SCM_MAKE_STR_IMMUTABLE(replace_install_dir(gauche_site_lib_dir));
125 }
126 return dir;
127 }
128
129 ScmObj Scm_SiteArchitectureDirectory()
130 {
131 static ScmObj dir = SCM_UNBOUND;
132 if (SCM_UNBOUNDP(dir)) {
133 dir = SCM_MAKE_STR_IMMUTABLE(replace_install_dir(gauche_site_arch_dir));
134 }
135 return dir;
136 }
137
138 ScmObj Scm_RuntimeDirectory()
139 {
140 static ScmObj dir = SCM_UNBOUND;
141 if (SCM_UNBOUNDP(dir)) {
142 const char *d = get_install_dir();
143 if (d == NULL) dir = SCM_FALSE;
144 else dir = SCM_MAKE_STR_IMMUTABLE(d);
145 }
146 return dir;
147 }
148
149 ScmObj Scm_LibgauchePath()
150 {
151 static ScmObj path = SCM_UNBOUND;
152 if (SCM_UNBOUNDP(path)) {
153 const char *p = get_libgauche_path();
154 if (p == NULL) path = SCM_FALSE;
155 else path = SCM_MAKE_STR_IMMUTABLE(p);
156 }
157 return path;
158 }
159
160 ScmObj Scm_ExecutablePath()
161 {
162 static ScmObj path = SCM_UNBOUND;
163 if (SCM_UNBOUNDP(path)) {
164 const char *p = get_executable_path();
165 if (p == NULL) path = SCM_FALSE;
166 else path = SCM_MAKE_STR_IMMUTABLE(p);
167 }
168 return path;
169 }
170
171 /*
172 * Initialization
173 */
174
175 #include "gauche/priv/parameterP.h"
176
177 void Scm__InitExecenv(void)
178 {
179 /* (command-line) is R7RS. We realize it as a a parameter. */
180 ScmObj defaultval = SCM_LIST1(SCM_MAKE_STR_IMMUTABLE(""));
181 command_line = Scm_BindPrimitiveParameter(Scm_GaucheModule(),
182 "command-line",
183 defaultval, 0);
184 os_command_line = Scm_BindPrimitiveParameter(Scm_GaucheModule(),
185 "os-command-line",
186 defaultval, 0);
187 /* script-file is set by 'load'. */
188 script_file = Scm_BindPrimitiveParameter(Scm_GaucheModule(),
189 "script-file",
190 SCM_FALSE, 0);
191 }