Update kaem test to new kaem.c

This commit is contained in:
fosslinux 2020-02-02 09:18:27 +11:00
parent 510d4203ec
commit 831de6bc4a
No known key found for this signature in database
GPG Key ID: 7D7996D0C25B63A3
6 changed files with 176 additions and 92 deletions

View File

@ -145,10 +145,10 @@ ed1eb588d8b14a370729d1426f8c1b1cc9cbfeaffe80012558ab74dadd82fbf8 test/results/t
91f8415a8358a26aa99706d990a35761cd5e4f313a32ef45ab5bc49a47435249 test/results/test24-armv7l-binary 91f8415a8358a26aa99706d990a35761cd5e4f313a32ef45ab5bc49a47435249 test/results/test24-armv7l-binary
44b37b8d45d8c0a6e983168e5646ef652deab2fcd327cb255bc2a71289f471ff test/results/test24-knight-posix-binary 44b37b8d45d8c0a6e983168e5646ef652deab2fcd327cb255bc2a71289f471ff test/results/test24-knight-posix-binary
29216c9761435396912cb03a414ac74cde165a522cf19a7cccac212b06d3a597 test/results/test24-x86-binary 29216c9761435396912cb03a414ac74cde165a522cf19a7cccac212b06d3a597 test/results/test24-x86-binary
9c3b36e8d0c41ac4fd3c650c01c26c42f2e53b23703242d3e0f96b64f50414f2 test/results/test25-aarch64-binary 4daca656e721fbc7f0ce040b9ed7c86f6183122ed6ae8b83f2c359a43cdf6196 test/results/test25-aarch64-binary
d21614c94b45087430a034c1b971f2e9953a4b71f46ec2de00f8549e3b82f5db test/results/test25-amd64-binary 0506117d8b028c1e5e23ecd0493116278e94ec32a55ff314dee1a2f4e2b243a5 test/results/test25-amd64-binary
f414d86f296579ce1f9b6d21734c447ffe7327d324d43dfed5f4c1d20b322915 test/results/test25-armv7l-binary 8af23bb361cce0d7031d78a3f1f88a51d6786df4cc2f5dc0bf09ff67947d30ea test/results/test25-armv7l-binary
74ba43bffc60656eb030e229ba5bb0b36dbae093cdbb3f59c9586b16d56002e5 test/results/test25-x86-binary e1fca774a067de5edb7eb1165619573993d2a5e5647c05568a68f3a53a7990cd test/results/test25-x86-binary
19316443ba9fd25fa7079902aa4fc6261628cc0600f2b94dbf436e3848102370 test/results/test26-aarch64-binary 19316443ba9fd25fa7079902aa4fc6261628cc0600f2b94dbf436e3848102370 test/results/test26-aarch64-binary
60206125928d77a758cd73ec8e85f8592423ce678570ebbe7876b71dc38aaeaf test/results/test26-amd64-binary 60206125928d77a758cd73ec8e85f8592423ce678570ebbe7876b71dc38aaeaf test/results/test26-amd64-binary
649349e01a90ce93ba72ba1b9bb81a9bd9d79b580ddff366173c98835c10b976 test/results/test26-armv7l-binary 649349e01a90ce93ba72ba1b9bb81a9bd9d79b580ddff366173c98835c10b976 test/results/test26-armv7l-binary

View File

@ -60,7 +60,7 @@ then
# Verify that the compiled program returns the correct result # Verify that the compiled program returns the correct result
out=$(./test/results/test25-aarch64-binary --version 2>&1 ) out=$(./test/results/test25-aarch64-binary --version 2>&1 )
[ 0 = $? ] || exit 5 [ 0 = $? ] || exit 5
[ "$out" = "kaem version 0.1" ] || exit 6 [ "$out" = "kaem version 0.6.0" ] || exit 6
# Verify that the resulting file works # Verify that the resulting file works
out=$(./test/results/test25-aarch64-binary --file test/test25/kaem.run) out=$(./test/results/test25-aarch64-binary --file test/test25/kaem.run)

View File

@ -58,7 +58,7 @@ then
# Verify that the compiled program returns the correct result # Verify that the compiled program returns the correct result
out=$(./test/results/test25-amd64-binary --version 2>&1 ) out=$(./test/results/test25-amd64-binary --version 2>&1 )
[ 0 = $? ] || exit 5 [ 0 = $? ] || exit 5
[ "$out" = "kaem version 0.1" ] || exit 6 [ "$out" = "kaem version 0.6.0" ] || exit 6
# Verify that the resulting file works # Verify that the resulting file works
out=$(./test/results/test25-amd64-binary --file test/test25/kaem.run) out=$(./test/results/test25-amd64-binary --file test/test25/kaem.run)

View File

@ -58,7 +58,7 @@ then
# Verify that the compiled program returns the correct result # Verify that the compiled program returns the correct result
out=$(./test/results/test25-armv7l-binary --version 2>&1 ) out=$(./test/results/test25-armv7l-binary --version 2>&1 )
[ 0 = $? ] || exit 5 [ 0 = $? ] || exit 5
[ "$out" = "kaem version 0.1" ] || exit 6 [ "$out" = "kaem version 0.6.0" ] || exit 6
# Verify that the resulting file works # Verify that the resulting file works
out=$(./test/results/test25-armv7l-binary --file test/test25/kaem.run) out=$(./test/results/test25-armv7l-binary --file test/test25/kaem.run)

View File

@ -58,7 +58,7 @@ then
# Verify that the compiled program returns the correct result # Verify that the compiled program returns the correct result
out=$(./test/results/test25-x86-binary --version 2>&1 ) out=$(./test/results/test25-x86-binary --version 2>&1 )
[ 0 = $? ] || exit 5 [ 0 = $? ] || exit 5
[ "$out" = "kaem version 0.1" ] || exit 6 [ "$out" = "kaem version 0.6.0" ] || exit 6
# Verify that the resulting file works # Verify that the resulting file works
out=$(./test/results/test25-x86-binary --file test/test25/kaem.run) out=$(./test/results/test25-x86-binary --file test/test25/kaem.run)

View File

@ -29,14 +29,15 @@
#define max_args 256 #define max_args 256
//CONSTANT max_args 256 //CONSTANT max_args 256
void file_print(char* s, FILE* f);
char* numerate_number(int a); char* numerate_number(int a);
int match(char* a, char* b); int match(char* a, char* b);
void file_print(char* s, FILE* f);
char** tokens; char** tokens;
int command_done; int command_done;
int VERBOSE; int VERBOSE;
int STRICT; int STRICT;
int envp_length;
/* Function for purging line comments */ /* Function for purging line comments */
void collect_comment(FILE* input) void collect_comment(FILE* input)
@ -137,6 +138,13 @@ char* copy_string(char* target, char* source)
return target; return target;
} }
int string_length(char* a)
{
int i = 0;
while(0 != a[i]) i = i + 1;
return i;
}
char* prepend_string(char* add, char* base) char* prepend_string(char* add, char* base)
{ {
char* ret = calloc(max_string, sizeof(char)); char* ret = calloc(max_string, sizeof(char));
@ -189,16 +197,16 @@ char* find_executable(char* name, char* PATH)
char* next = find_char(PATH, ':'); char* next = find_char(PATH, ':');
char* trial; char* trial;
FILE* try; FILE* t;
while(NULL != next) while(NULL != next)
{ {
next[0] = 0; next[0] = 0;
trial = prepend_string(PATH, prepend_string("/", name)); trial = prepend_string(PATH, prepend_string("/", name));
try = fopen(trial, "r"); t = fopen(trial, "r");
if(NULL != try) if(NULL != t)
{ {
fclose(try); fclose(t);
return trial; return trial;
} }
PATH = next + 1; PATH = next + 1;
@ -208,92 +216,153 @@ char* find_executable(char* name, char* PATH)
return NULL; return NULL;
} }
/* Function to check if the token is an envar and if it is get the pos of = */
int check_envar(char* token)
{
int j;
int equal_found;
equal_found = 0;
for(j = 0; j < string_length(token); j = j + 1)
{
if(token[j] == '=')
{ /* After = can be anything */
equal_found = 1;
break;
}
else
{ /* Should be A-z */
int found;
found = 0;
char c;
/* Represented numerically; 0 = 48 through 9 = 57 */
for(c = 48; c <= 57; c = c + 1)
{
if(token[j] == c)
{
found = 1;
}
}
/* Represented numerically; A = 65 through z = 122 */
for(c = 65; c <= 122; c = c + 1)
{
if(token[j] == c)
{
found = 1;
}
}
if(found == 0)
{ /* In all likelihood this isn't actually an environment variable */
return 1;
}
}
}
if(equal_found == 0)
{ /* Not an envar */
return 1;
}
return 0;
}
/* Function for executing our programs with desired arguments */ /* Function for executing our programs with desired arguments */
void execute_command(FILE* script, char** envp) void execute_commands(FILE* script, char** envp, int envp_length)
{ {
tokens = calloc(max_args, sizeof(char*)); while(1)
char* PATH = env_lookup("PATH=", envp);
if(NULL != PATH)
{ {
PATH = calloc(max_string, sizeof(char)); tokens = calloc(max_args, sizeof(char*));
copy_string(PATH, env_lookup("PATH=", envp)); char* PATH = env_lookup("PATH=", envp);
} if(NULL != PATH)
char* USERNAME = env_lookup("LOGNAME=", envp);
if((NULL == PATH) && (NULL == USERNAME))
{
PATH = calloc(max_string, sizeof(char));
copy_string(PATH, "/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
}
else if(NULL == PATH)
{
PATH = prepend_string("/home/", prepend_string(USERNAME,"/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"));
}
int i = 0;
int status = 0;
command_done = 0;
do
{
char* result = collect_token(script);
if(0 != result)
{ /* Not a comment string but an actual argument */
tokens[i] = result;
i = i + 1;
}
} while(0 == command_done);
if(VERBOSE && (0 < i))
{
file_print(" +> ", stdout);
int j;
for(j = 0; j < i; j = j + 1)
{ {
file_print(tokens[j], stdout); PATH = calloc(max_string, sizeof(char));
fputc(' ', stdout); copy_string(PATH, env_lookup("PATH=", envp));
} }
file_print("\n", stdout);
}
if(0 < i) char* USERNAME = env_lookup("LOGNAME=", envp);
{ /* Not a line comment */ if((NULL == PATH) && (NULL == USERNAME))
char* program = find_executable(tokens[0], PATH);
if(NULL == program)
{ {
file_print(tokens[0], stderr); PATH = calloc(max_string, sizeof(char));
file_print("Some weird shit went down with: ", stderr); copy_string(PATH, "/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
file_print("\n", stderr);
exit(EXIT_FAILURE);
} }
else if(NULL == PATH)
int f = fork();
if (f == -1)
{ {
file_print("fork() failure", stderr); PATH = prepend_string("/home/", prepend_string(USERNAME,"/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"));
exit(EXIT_FAILURE);
}
else if (f == 0)
{ /* child */
/* execve() returns only on error */
execve(program, tokens, envp);
/* Prevent infinite loops */
_exit(EXIT_SUCCESS);
} }
/* Otherwise we are the parent */ int i = 0;
/* And we should wait for it to complete */ int status = 0;
waitpid(f, &status, 0); command_done = 0;
do
{
char* result = collect_token(script);
if(0 != result)
{ /* Not a comment string but an actual argument */
tokens[i] = result;
i = i + 1;
}
} while(0 == command_done);
if(STRICT && (0 != status)) if(VERBOSE && (0 < i))
{ /* Clearly the script hit an issue that should never have happened */ {
file_print("Subprocess error ", stderr); file_print(" +> ", stdout);
file_print(numerate_number(status), stderr); int j;
file_print("\nABORTING HARD\n", stderr); for(j = 0; j < i; j = j + 1)
/* stop to prevent damage */ {
exit(EXIT_FAILURE); file_print(tokens[j], stdout);
fputc(' ', stdout);
}
file_print("\n", stdout);
}
if(0 < i)
{ /* Not a line comment */
int is_envar;
is_envar = 0;
if(check_envar(tokens[0]) == 0)
{ /* It's an envar! */
is_envar = 1;
envp[envp_length] = tokens[0]; /* Since arrays are 0 indexed */
envp_length = envp_length + 1;
}
if(is_envar == 0)
{ /* Stuff to exec */
char* program = find_executable(tokens[0], PATH);
if(NULL == program)
{
file_print(tokens[0], stderr);
file_print("Some weird shit went down with: ", stderr);
file_print("\n", stderr);
exit(EXIT_FAILURE);
}
int f = fork();
if (f == -1)
{
file_print("fork() failure", stderr);
exit(EXIT_FAILURE);
}
else if (f == 0)
{ /* child */
/* execve() returns only on error */
execve(program, tokens, envp);
/* Prevent infinite loops */
_exit(EXIT_SUCCESS);
}
/* Otherwise we are the parent */
/* And we should wait for it to complete */
waitpid(f, &status, 0);
if(STRICT && (0 != status))
{ /* Clearly the script hit an issue that should never have happened */
file_print("Subprocess error ", stderr);
file_print(numerate_number(status), stderr);
file_print("\nABORTING HARD\n", stderr);
/* stop to prevent damage */
exit(EXIT_FAILURE);
}
}
/* Then go again */
} }
/* Then go again */
} }
} }
@ -305,7 +374,25 @@ int main(int argc, char** argv, char** envp)
char* filename = "kaem.run"; char* filename = "kaem.run";
FILE* script = NULL; FILE* script = NULL;
int i = 1; /* Get envp_length */
envp_length = 1;
while(envp[envp_length] != NULL)
{
envp_length = envp_length + 1;
}
char** nenvp = calloc(envp_length + max_args, sizeof(char*));
int i;
for(i = 0; i < envp_length; i = i + 1)
{
nenvp[i] = envp[i];
}
for(i = envp_length; i < (envp_length + max_args); i = i + 1)
{
nenvp[i] = "";
}
i = 1;
while(i <= argc) while(i <= argc)
{ {
if(NULL == argv[i]) if(NULL == argv[i])
@ -314,7 +401,7 @@ int main(int argc, char** argv, char** envp)
} }
else if(match(argv[i], "-h") || match(argv[i], "--help")) else if(match(argv[i], "-h") || match(argv[i], "--help"))
{ {
file_print("kaem only accepts --help, --version, --verbose or no arguments\n", stdout); file_print("kaem only accepts --help, --version, --file, --verbose, --nightmare-mode or no arguments\n", stdout);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else if(match(argv[i], "-f") || match(argv[i], "--file")) else if(match(argv[i], "-f") || match(argv[i], "--file"))
@ -330,7 +417,7 @@ int main(int argc, char** argv, char** envp)
} }
else if(match(argv[i], "-V") || match(argv[i], "--version")) else if(match(argv[i], "-V") || match(argv[i], "--version"))
{ {
file_print("kaem version 0.1\n", stdout); file_print("kaem version 0.6.0\n", stdout);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else if(match(argv[i], "--verbose")) else if(match(argv[i], "--verbose"))
@ -360,10 +447,7 @@ int main(int argc, char** argv, char** envp)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while(1) execute_commands(script, nenvp, envp_length);
{
execute_command(script, envp);
}
fclose(script); fclose(script);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }