Skip to content

Commit

Permalink
executable in (liii sys)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Nov 24, 2024
1 parent 6f22f5b commit 6027c40
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 22 deletions.
234 changes: 222 additions & 12 deletions Goldfish.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -15243,6 +15243,108 @@
\;
</scm-chunk>

<chapter|(liii sys)>

<section|许可证>

<\goldfish-chunk|goldfish/liii/sys.scm|false|true>
;

; Copyright (C) 2024 The Goldfish Scheme Authors

;

; Licensed under the Apache License, Version 2.0 (the "License");

; you may not use this file except in compliance with the License.

; You may obtain a copy of the License at

;

; http://www.apache.org/licenses/LICENSE-2.0

;

; Unless required by applicable law or agreed to in writing, software

; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT

; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the

; License for the specific language governing permissions and limitations

; under the License.

;

\;
</goldfish-chunk>

<section|接口>

<\goldfish-chunk|goldfish/liii/sys.scm|true|true>
(define-library (liii sys)

(export argv executable)

(import (scheme process-context))

(begin

\;
</goldfish-chunk>

<section|测试>

<\goldfish-chunk|tests/goldfish/liii/sys-test.scm|false|false>
(import (liii check)

\ \ \ \ \ \ \ \ (liii sys))

\;

(display (executable))

(newline)

\;

(check-report)

\;
</goldfish-chunk>

<section|实现>

<paragraph|argv>

<\goldfish-chunk|goldfish/liii/sys.scm|true|true>
(define (argv) (command-line))

\;
</goldfish-chunk>

<paragraph|executable><index|executable><typehint|() =\<gtr\> string?>

具体实现见<smart-ref|paragraph:g_executable>。

<\goldfish-chunk|goldfish/liii/sys.scm|true|true>
(define (executable) (g_executable))

\;
</goldfish-chunk>

<section|结尾>

<\goldfish-chunk|goldfish/liii/sys.scm|true|false>
) ; end of begin

) ; end of define-library

\;
</goldfish-chunk>

<chapter|(liii os)>

<section|协议>
Expand Down Expand Up @@ -17298,11 +17400,15 @@

#include \<less\>windows.h\<gtr\>

#else
#elif TB_CONFIG_OS_MACOSX

#include \<less\>pwd.h\<gtr\>
#include \<less\>limits.h\<gtr\>

#include \<less\>unistd.h\<gtr\>
#include \<less\>mach-o/dyld.h\<gtr\>

#else

#include \<less\>linux/limits.h\<gtr\>

#endif

Expand All @@ -17312,6 +17418,10 @@

#include \<less\>errno.h\<gtr\>

#include \<less\>pwd.h\<gtr\>

#include \<less\>unistd.h\<gtr\>

#include \<less\>wordexp.h\<gtr\>

#endif
Expand Down Expand Up @@ -17657,6 +17767,108 @@
\;
</cpp-chunk>

<subsubsection|(liii sys)的胶水代码>

<paragraph|g_executable><index|g_executable><label|paragraph:g_executable>

<\cpp-chunk|src/goldfish.hpp|true|true>
string

goldfish_exe () {

#ifdef TB_CONFIG_OS_WINDOWS

\ \ char buffer[GOLDFISH_PATH_MAXN];

\ \ GetModuleFileName (NULL, buffer, GOLDFISH_PATH_MAXN);

\ \ return string (buffer);

#elif TB_CONFIG_OS_MACOSX

\ \ char \ \ \ \ \ \ \ buffer[PATH_MAX];

\ \ uint32_t \ \ \ size= sizeof (buffer);

\ \ if (_NSGetExecutablePath (buffer, &size) == 0) {

\ \ \ \ char real_path[GOLDFISH_PATH_MAXN];

\ \ \ \ if (realpath (buffer, real_path) != NULL) {

\ \ \ \ \ \ return string (real_path);

\ \ \ \ }

\ \ }

\ \ return "";

#elif TB_CONFIG_OS_LINUX

\ \ char \ \ \ buffer[GOLDFISH_PATH_MAXN];

\ \ ssize_t len= readlink ("/proc/self/exe", buffer, sizeof (buffer) - 1);

\ \ if (len != -1) {

\ \ \ \ buffer[len]= '\\0';

\ \ \ \ return std::string (buffer);

\ \ }

\ \ return "";

#endif

}

\;
</cpp-chunk>

<\cpp-chunk|src/goldfish.hpp|true|true>
static s7_pointer

f_executable (s7_scheme* sc, s7_pointer args) {

\ \ string exe_path= goldfish_exe ();

\ \ return s7_make_string (sc, exe_path.c_str ());

}

\;
</cpp-chunk>

<paragraph|glue_liii_sys>

<\cpp-chunk|src/goldfish.hpp|true|true>
inline void

glue_liii_sys (s7_scheme* sc) {

\ \ s7_pointer cur_env= s7_curlet (sc);

\;

\ \ const char* s_executable= "g_executable";

\ \ const char* d_executable= "(g_executable) =\<gtr\> string";

\;

\ \ s7_define (sc, cur_env, s7_make_symbol (sc, s_executable),

\ \ \ \ \ \ \ \ \ \ \ \ \ s7_make_typed_function (sc, s_executable, f_executable, 0, 0,

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ false, d_executable, NULL));

}

\;
</cpp-chunk>

<subsubsection|(liii os)的胶水代码>

<paragraph|g_os-type><index|g_os-type>
Expand Down Expand Up @@ -17728,7 +17940,7 @@

#else

\ \ wordexp_t \ \ p;
\ \ wordexp_t p;

\ \ ret= wordexp (cmd_c, &p, 0);

Expand Down Expand Up @@ -18366,6 +18578,8 @@

\ \ glue_scheme_process_context (sc);

\ \ glue_liii_sys (sc);

\ \ glue_liii_os (sc);

\ \ glue_liii_uuid (sc);
Expand Down Expand Up @@ -18618,21 +18832,17 @@
<\cpp-chunk|src/goldfish.hpp|true|true>
string

find_goldfish_library (char** argv) {

\ \ tb_char_t \ \ \ \ \ \ \ data_goldfish[TB_PATH_MAXN]= {0};

\ \ tb_char_t const* goldfish=
find_goldfish_library () {

\ \ \ \ \ \ tb_path_absolute (argv[0], data_goldfish, sizeof (data_goldfish));
\ \ string exe_path= goldfish_exe ();

\;

\ \ tb_char_t \ \ \ \ \ \ \ data_bin[TB_PATH_MAXN]= {0};

\ \ tb_char_t const* ret_bin=

\ \ \ \ \ \ tb_path_directory (goldfish, data_bin, sizeof (data_bin));
\ \ \ \ \ \ tb_path_directory (exe_path.c_str (), data_bin, sizeof (data_bin));

\;

Expand Down Expand Up @@ -18728,7 +18938,7 @@

repl_for_community_edition (int argc, char** argv) {

\ \ string \ \ \ \ \ gf_lib_dir \ = find_goldfish_library (argv);
\ \ string \ \ \ \ \ gf_lib_dir \ = find_goldfish_library ();

\ \ const char* gf_lib \ \ \ \ \ = gf_lib_dir.c_str ();

Expand Down
5 changes: 4 additions & 1 deletion goldfish/liii/sys.scm
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
;

(define-library (liii sys)
(export argv)
(export argv executable)
(import (scheme process-context))
(begin

(define (argv) (command-line))

(define (executable) (g_executable))

) ; end of begin
) ; end of define-library

Loading

0 comments on commit 6027c40

Please sign in to comment.