diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-31 22:30:29 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2019-12-31 22:30:29 -0500 |
commit | 263bd26d281a530b78b56d298c29ffa368b0b981 (patch) | |
tree | 0c739765dc9f917ce8725aa9aeb84799967562e7 | |
parent | 5126cdc3c7385f01fe0839d63e2621914812dd26 (diff) | |
download | spmc-263bd26d281a530b78b56d298c29ffa368b0b981.tar.gz |
Update README.md
* Add environment.c
-rw-r--r-- | README.md | 33 | ||||
-rw-r--r-- | include/spm.h | 5 | ||||
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/environment.c | 77 | ||||
-rw-r--r-- | src/spm.c | 2 |
5 files changed, 116 insertions, 3 deletions
@@ -69,7 +69,7 @@ _TODO_ ## Usage ```bash -$ ./spm --help +$ spm --help usage: spm [-hVvBIrLS] -h, --help show this help message -V, --version show version @@ -81,6 +81,37 @@ usage: spm [-hVvBIrLS] -S, --search search for a package ``` +### Example + +#### Install Python +```bash +$ spm --root ~/spmenv123 --install "python" # [...] +``` + +#### Export environment variables + +```bash +$ export PATH="~/spmenv123/bin:$PATH" +$ export MANPATH="~/spmenv123/share/man:$MANPATH" +$ hash -r # or "rehash" if your shell supports it +``` + +#### Use Python + +```bash +$ which python +/home/example/spmenv123/bin/python +$ which pip +/home/example/spmenv123/bin/pip +$ which git +/home/example/spmenv123/bin/git + +$ python -m venv ~/spmenv123/venv +$ source ~/spmenv123/venv/bin/activate +$ pip install https://github.com/spacetelescope/jwst.git#egg=jwst +# ... do work +``` + ## Building SPM Packages _TODO_ diff --git a/include/spm.h b/include/spm.h index eb6b95f..a8a4ab7 100644 --- a/include/spm.h +++ b/include/spm.h @@ -261,4 +261,9 @@ int file_is_binexec(const char *filename); // internal_cmd.c int internal_cmd(int argc, char **argv); +// environment.c +char **runtime_copy(char **env); +void runtime_export(char **env); +void runtime_free(char **env); + #endif //SPM_SPM_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8981122..0d53e9b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,7 +3,7 @@ include_directories( ${CMAKE_BINARY_DIR}/include ) -add_executable(spm spm.c config.c compat.c deps.c fs.c rpath.c find.c shell.c archive.c strings.c relocation.c install.c config_global.c manifest.c checksum.c extern/url.c version_spec.c spm_build.c mime.c internal_cmd.c) +add_executable(spm spm.c config.c compat.c deps.c fs.c rpath.c find.c shell.c archive.c strings.c relocation.c install.c config_global.c manifest.c checksum.c extern/url.c version_spec.c spm_build.c mime.c internal_cmd.c environment.c) target_link_libraries(spm rt crypto ssl curl) if(MSVC) diff --git a/src/environment.c b/src/environment.c new file mode 100644 index 0000000..d338e4a --- /dev/null +++ b/src/environment.c @@ -0,0 +1,77 @@ +/** + * @file environment.c + */ +#include "spm.h" + +/** + * + * @param env + */ +void runtime_export(char **env) { + char *borne[] = { + "bash", + "dash", + "zsh", + NULL, + }; + char *unborne[] = { + "csh" + "tcsh", + NULL, + }; + + char output[BUFSIZ]; + char export_command[7]; // export=6 and setenv=6... convenient + char *_sh = getenv("SHELL"); + char *sh = basename(_sh); + if (sh == NULL) { + fprintf(stderr, "echo SHELL environment variable is not defined"); + exit(1); + } + + for (size_t i = 0; borne[i] != NULL; i++) { + if (strcmp(sh, borne[i]) == 0) { + strcpy(export_command, "export"); + break; + } + } + for (size_t i = 0; unborne[i] != NULL; i++) { + if (strcmp(sh, unborne[i]) == 0) { + strcpy(export_command, "setenv"); + break; + } + } + + for (size_t i = 0; env[i] != NULL; i++) { + char **pair = split(env[i], "="); + sprintf(output, "%s %s=\"%s\"", export_command, pair[0], pair[1] ? pair[1] : ""); + puts(output); + split_free(pair); + } +} + +/** + * + * @param env + * @return + */ +char **runtime_copy(char **env) { + char **envp = NULL; + size_t env_count; + for (env_count = 0; env[env_count] != NULL; env_count++); + + envp = (char **)calloc(env_count + 1, sizeof(char *)); + for (size_t i = 0; i < env_count; i++) { + size_t len = strlen(env[i]); + envp[i] = (char *)calloc(len + 1, sizeof(char)); + memcpy(envp[i], env[i], len); + } + return envp; +} + +void runtime_free(char **env) { + for (size_t i = 0; env[i] != NULL; i++) { + free(env[i]); + } + free(env); +}
\ No newline at end of file @@ -24,7 +24,7 @@ void usage(const char *program_name) { , program_name); } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[], char *arge[]) { char program_name[strlen(argv[0]) + 1]; memset(program_name, '\0', sizeof(program_name) + 1); strcpy(program_name, basename(argv[0])); |