Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_shell_bridge.c
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Invokes Pharos shell runtime, build, and status command surfaces for native callers.
4 */
5
7
9#include "native_support.h"
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15#ifndef _WIN32
16#include <unistd.h>
17#endif
18
19static char *runtime_script_path_native(const char *repo_root) {
20 return path_join(repo_root, "scripts/pharos_runtime.sh");
21}
22
23#ifndef _WIN32
24#ifndef __wasm__
25int exec_runtime_script_native(const char *repo_root, int argc, char **argv) {
26 char *script = runtime_script_path_native(repo_root);
27 char **child_argv = (char **)calloc((size_t)argc + 1, sizeof(char *));
28 int index;
29 if (script == NULL || child_argv == NULL) {
30 free(script);
31 free(child_argv);
32 return 69;
33 }
34 child_argv[0] = script;
35 for (index = 1; index < argc; index++) {
36 child_argv[index] = argv[index];
37 }
38 child_argv[argc] = NULL;
39 execv(script, child_argv);
40 perror("execv");
41 free(child_argv);
42 free(script);
43 return 69;
44}
45#else
46int exec_runtime_script_native(const char *repo_root, int argc, char **argv) {
47 (void)repo_root;
48 (void)argc;
49 (void)argv;
50 return 78;
51}
52#endif
53#else
54int exec_runtime_script_native(const char *repo_root, int argc, char **argv) {
55 char *script = runtime_script_path_native(repo_root);
56 char command[16384];
57 int offset = 0;
58 int index;
59 if (script == NULL) {
60 return 69;
61 }
62 offset += snprintf(command + offset, sizeof(command) - (size_t)offset, "\"%s\"", script);
63 for (index = 1; index < argc && offset < (int)sizeof(command) - 4; index++) {
64 offset += snprintf(command + offset, sizeof(command) - (size_t)offset, " \"%s\"", argv[index]);
65 }
66 free(script);
67 return system(command);
68}
69#endif
70
71int shell_runtime_command_capture_native(const char *repo_root, int argc, char *const argv[], char **output) {
72 char *script = runtime_script_path_native(repo_root);
73 char **argv_list;
74 int index;
75 int code;
76 if (script == NULL) {
77 return 69;
78 }
79 argv_list = (char **)calloc((size_t)argc + 2, sizeof(char *));
80 if (argv_list == NULL) {
81 free(script);
82 return 69;
83 }
84 argv_list[0] = script;
85 for (index = 0; index < argc; index++) {
86 argv_list[index + 1] = argv[index];
87 }
88 argv_list[argc + 1] = NULL;
89 {
90 const char *env_keys[1] = {"PHAROS_DISABLE_NATIVE_DELEGATION"};
91 const char *env_values[1] = {"1"};
92 code = capture_execv_output_env_native(argv_list, env_keys, env_values, 1, output, NULL);
93 }
94 free(argv_list);
95 free(script);
96 return code;
97}
98
99int shell_status_or_build_native(const char *repo_root, const ServeConfig *config, int use_build, char **output) {
100 char *argv_list[31];
101 int argc = 0;
102 argv_list[argc++] = use_build ? "build" : "status";
103 argv_list[argc++] = config->kind;
104 argv_list[argc++] = config->target_id;
105 if (config->conf_path != NULL) {
106 argv_list[argc++] = "--conf";
107 argv_list[argc++] = config->conf_path;
108 }
109 if (config->app_dir != NULL) {
110 argv_list[argc++] = "--app-dir";
111 argv_list[argc++] = config->app_dir;
112 }
113 if (config->build_dir != NULL) {
114 argv_list[argc++] = "--build-dir";
115 argv_list[argc++] = config->build_dir;
116 }
117 if (config->state_dir != NULL) {
118 argv_list[argc++] = "--state-dir";
119 argv_list[argc++] = config->state_dir;
120 }
121 if (config->state_path != NULL) {
122 argv_list[argc++] = "--state-path";
123 argv_list[argc++] = config->state_path;
124 }
125 if (config->base_path != NULL) {
126 argv_list[argc++] = "--base-path";
127 argv_list[argc++] = config->base_path;
128 }
129 if (config->log_path != NULL) {
130 argv_list[argc++] = "--log-path";
131 argv_list[argc++] = config->log_path;
132 }
133 if (config->debug != NULL) {
134 argv_list[argc++] = "--debug";
135 argv_list[argc++] = config->debug;
136 }
137 argv_list[argc++] = "--text";
138 argv_list[argc] = NULL;
139 return shell_runtime_command_capture_native(repo_root, argc, argv_list, output);
140}
int capture_execv_output_env_native(char *const argv[], const char *env_keys[], const char *env_values[], size_t env_count, char **output, size_t *output_len)
Executes a process with injected environment values and captures its output.
Declares child-process capture helpers.
static char * runtime_script_path_native(const char *repo_root)
int exec_runtime_script_native(const char *repo_root, int argc, char **argv)
Executes the shared Pharos runtime script with the supplied arguments.
int shell_status_or_build_native(const char *repo_root, const ServeConfig *config, int use_build, char **output)
Runs the shell-side status or build flow for one native serve target.
int shell_runtime_command_capture_native(const char *repo_root, int argc, char *const argv[], char **output)
Executes a runtime shell command and captures its combined output.
Declares bridge helpers for invoking Pharos shell command surfaces.
char * path_join(const char *left, const char *right)
Joins two path segments using the native path separator.
Declares shared low-level support helpers used across the Pharos native runtime.
Parsed native serve configuration used to bootstrap the runtime listener.