From e44280077995c506a1de382da2023043f6ab7043 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Sun, 25 Aug 2024 15:04:04 +0200 Subject: [PATCH] fix: move the new utils into utils.h/c --- Makefile | 2 - include/argv_split.h | 24 ------------ include/util.h | 7 ++++ include/which.h | 23 ----------- src/argv_split.c | 81 --------------------------------------- src/main.c | 4 +- src/util.c | 91 +++++++++++++++++++++++++++++++++++++++++++- src/which.c | 54 -------------------------- 8 files changed, 98 insertions(+), 188 deletions(-) delete mode 100644 include/argv_split.h delete mode 100644 include/which.h delete mode 100644 src/argv_split.c delete mode 100644 src/which.c diff --git a/Makefile b/Makefile index cabe7ea..4bf0d8d 100644 --- a/Makefile +++ b/Makefile @@ -37,8 +37,6 @@ OBJ += out/obj/util.o OBJ += out/obj/pipeline.o OBJ += out/obj/threadlist.o OBJ += out/obj/threadpool.o -OBJ += out/obj/argv_split.o -OBJ += out/obj/which.o out/bin/sci: $(OBJ) | $(BINDIR) $(CC) -o $@ $^ $(CFLAGS) diff --git a/include/argv_split.h b/include/argv_split.h deleted file mode 100644 index f8de895..0000000 --- a/include/argv_split.h +++ /dev/null @@ -1,24 +0,0 @@ -/** - * sci - a simple ci system - Copyright (C) 2024 Asger Gitz-Johansen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - */ -#ifndef SCI_ARGV_SPLIT_H -#define SCI_ARGV_SPLIT_H - -char** create_argv_shell(const char* str, int* argc_out); -void argv_free(char** argv); - -#endif diff --git a/include/util.h b/include/util.h index 57095ab..d85d8e2 100644 --- a/include/util.h +++ b/include/util.h @@ -41,4 +41,11 @@ void per_line(const char* file, line_handler handler); char* join(const char* a, const char* b); +const char* skip_arg(const char* cp); +char* skip_spaces(const char* str); +int count_argc(const char* str); +char** argv_split(const char* str, int* argc_out); +void argv_free(char** argv); +int which(const char* program_name, char* out_full_program, int max_path); + #endif diff --git a/include/which.h b/include/which.h deleted file mode 100644 index c76ac45..0000000 --- a/include/which.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * sci - a simple ci system - Copyright (C) 2024 Asger Gitz-Johansen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - */ -#ifndef SCI_WHICH_H -#define SCI_WHICH_H - -int which(const char* program_name, char* out_full_program, int max_path); - -#endif diff --git a/src/argv_split.c b/src/argv_split.c deleted file mode 100644 index 66751a3..0000000 --- a/src/argv_split.c +++ /dev/null @@ -1,81 +0,0 @@ -/** - * sci - a simple ci system - Copyright (C) 2024 Asger Gitz-Johansen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - */ -#include "argv_split.h" -#include -#include -#include -#include - -static const char* skip_arg(const char* cp) { - while(*cp && !isspace(*cp)) - cp++; - return cp; -} - -static char* skip_spaces(const char* str) { - while(isspace(*str)) - str++; - return(char*)str; -} - -static int count_argc(const char* str) { - int count = 0; - while(*str) { - str = skip_spaces(str); - if(!*str) - continue; - count++; - str = skip_arg(str); - } - return count; -} - -void argv_free(char** argv) { - for(char** p = argv; *p; p++) { - free(*p); - *p = NULL; - } - free(argv); -} - -char** create_argv_shell(const char* str, int* argc_out) { - int argc = count_argc(str); - char** result = calloc(argc+1, sizeof(*result)); - if(result == NULL) - return result; - if(argc_out) - *argc_out = argc+1; - char** argvp = result; - while(*str) { - str = skip_spaces(str); - if(!*str) - continue; - const char* p = str; - str = skip_arg(str); - char* t = strndup(p, str-p); - if(t == NULL) { - perror("strndup"); - argv_free(result); - return NULL; - } - *argvp++ = t; - } - *argvp = NULL; - return result; -} -// sci-release https://git.gtz.dk/agj/sci/archive/main.tar.gz manual "scripts/wget-and-sci.sh sci" diff --git a/src/main.c b/src/main.c index 1833fa0..aa8fd3f 100644 --- a/src/main.c +++ b/src/main.c @@ -15,14 +15,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "argv_split.h" #include "cli.h" #include "log.h" #include "notify.h" #include "pipeline.h" #include "threadpool.h" #include "util.h" -#include "which.h" #include #include #include @@ -85,7 +83,7 @@ void executor(void* data) { char* id = join("SCI_PIPELINE_ID=", pipeline_id); char* envp[] = { path, name, url, trigger, id, NULL }; int argc; - char** argv = create_argv_shell(e->command, &argc); + char** argv = argv_split(e->command, &argc); log_trace("executing pipeline %s with argv:", e->name); for(int i = 0; i < argc; i++) log_trace(" \"%s\"", argv[i]); diff --git a/src/util.c b/src/util.c index e49ac71..bc08701 100644 --- a/src/util.c +++ b/src/util.c @@ -15,9 +15,10 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "util.h" #include "log.h" +#include "util.h" #include +#include #include char* trim(const char* const str) { @@ -61,3 +62,91 @@ char* join(const char* a, const char* b) { sprintf(result, "%s%s", a, b); return result; } + +const char* skip_arg(const char* cp) { + while(*cp && !isspace(*cp)) + cp++; + return cp; +} + +char* skip_spaces(const char* str) { + while(isspace(*str)) + str++; + return(char*)str; +} + +int count_argc(const char* str) { + int count = 0; + while(*str) { + str = skip_spaces(str); + if(!*str) + continue; + count++; + str = skip_arg(str); + } + return count; +} + +void argv_free(char** argv) { + for(char** p = argv; *p; p++) { + free(*p); + *p = NULL; + } + free(argv); +} + +char** argv_split(const char* str, int* argc_out) { + int argc = count_argc(str); + char** result = calloc(argc+1, sizeof(*result)); + if(result == NULL) + return result; + if(argc_out) + *argc_out = argc+1; + char** argvp = result; + while(*str) { + str = skip_spaces(str); + if(!*str) + continue; + const char* p = str; + str = skip_arg(str); + char* t = strndup(p, str-p); + if(t == NULL) { + perror("strndup"); + argv_free(result); + return NULL; + } + *argvp++ = t; + } + *argvp = NULL; + return result; +} + +int which(const char* program_name, char* out_full_program, int max_path) { + assert(out_full_program); + assert(max_path > 0); + // sanity check - maybe program_name is actually a full-path to begin with + if(access(program_name, X_OK) == 0) { + snprintf(out_full_program, max_path, "%s", program_name); + return 0; + } + char* path = getenv("PATH"); + if (path == NULL) { + log_error("PATH environment variable not found."); + return -1; + } + char* path_cpy = strdup(path); + char* dir = strtok(path_cpy, ":"); + char full_path[PATH_MAX]; + while(dir != NULL) { + snprintf(full_path, sizeof(full_path), "%s/%s", dir, program_name); + if(access(full_path, X_OK) == 0) { + snprintf(out_full_program, max_path, "%s", full_path); + free(path_cpy); + return 0; + } + dir = strtok(NULL, ":"); + } + log_error("'%s' not found in PATH", program_name); + free(path_cpy); + return -1; +} diff --git a/src/which.c b/src/which.c deleted file mode 100644 index 7e7c8b4..0000000 --- a/src/which.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * sci - a simple ci system - Copyright (C) 2024 Asger Gitz-Johansen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - */ -#include "log.h" -#include -#include -#include -#include -#include -#include - -int which(const char* program_name, char* out_full_program, int max_path) { - assert(out_full_program); - assert(max_path > 0); - // sanity check - maybe program_name is actually a full-path to begin with - if(access(program_name, X_OK) == 0) { - snprintf(out_full_program, max_path, "%s", program_name); - return 0; - } - char* path = getenv("PATH"); - if (path == NULL) { - log_error("PATH environment variable not found."); - return -1; - } - char* path_cpy = strdup(path); - char* dir = strtok(path_cpy, ":"); - char full_path[PATH_MAX]; - while(dir != NULL) { - snprintf(full_path, sizeof(full_path), "%s/%s", dir, program_name); - if(access(full_path, X_OK) == 0) { - snprintf(out_full_program, max_path, "%s", full_path); - free(path_cpy); - return 0; - } - dir = strtok(NULL, ":"); - } - log_error("'%s' not found in PATH", program_name); - free(path_cpy); - return -1; -}