fix: move the new utils into utils.h/c
This commit is contained in:
parent
faf362c607
commit
e442800779
2
Makefile
2
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)
|
||||
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#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
|
@ -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
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef SCI_WHICH_H
|
||||
#define SCI_WHICH_H
|
||||
|
||||
int which(const char* program_name, char* out_full_program, int max_path);
|
||||
|
||||
#endif
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "argv_split.h"
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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"
|
@ -15,14 +15,12 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#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 <fcntl.h>
|
||||
#include <linux/limits.h>
|
||||
#include <spawn.h>
|
||||
@ -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]);
|
||||
|
91
src/util.c
91
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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "util.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
#include <ctype.h>
|
||||
#include <linux/limits.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
54
src/which.c
54
src/which.c
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "log.h"
|
||||
#include <assert.h>
|
||||
#include <linux/limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user