Rework main loop function for piping support

This commit is contained in:
Pentium44 2021-04-06 17:37:13 -07:00
parent f639381e7c
commit 304c47a682
3 changed files with 25 additions and 21 deletions

View File

@ -12,7 +12,7 @@
#include "enc.h" #include "enc.h"
#include "md5.h" #include "md5.h"
int process_line(char *line) char *process_line(char *line)
{ {
char *tok_srch; char *tok_srch;
@ -27,7 +27,7 @@ int process_line(char *line)
/* if line starts with a comment, skip */ /* if line starts with a comment, skip */
if(strncmp("#",tok_srch,1) == 0) if(strncmp("#",tok_srch,1) == 0)
{ {
return 0; return NULL;
} }
/* print function */ /* print function */
@ -41,7 +41,7 @@ int process_line(char *line)
if(strtok(NULL, "\"") == NULL) if(strtok(NULL, "\"") == NULL)
syn_error("ss:error:print syntax error, missing end quote"); syn_error("ss:error:print syntax error, missing end quote");
printf("%s\n", parsed); return parsed;
} }
else else
{ {
@ -63,6 +63,7 @@ int process_line(char *line)
tok_srch[strlen(tok_srch)-1] = 0; tok_srch[strlen(tok_srch)-1] = 0;
} }
sleep(atoi(tok_srch)); sleep(atoi(tok_srch));
return NULL;
} }
/* ss encrypt function */ /* ss encrypt function */
@ -79,7 +80,7 @@ int process_line(char *line)
{ {
char *encrp; char *encrp;
encrp = ss_encrypt(var_conv); encrp = ss_encrypt(var_conv);
printf("%s\n", encrp); return encrp;
} }
} }
@ -95,9 +96,9 @@ int process_line(char *line)
var_conv = parse_vars(tok_srch); var_conv = parse_vars(tok_srch);
if(var_conv != NULL) if(var_conv != NULL)
{ {
char *encrp; char *decrp;
encrp = ss_decrypt(var_conv); decrp = ss_decrypt(var_conv);
printf("%s\n", encrp); return decrp;
} }
} }
@ -113,6 +114,7 @@ int process_line(char *line)
file_line = parse_vars(tok_srch); file_line = parse_vars(tok_srch);
file_md5_val = md5_file(file_line); file_md5_val = md5_file(file_line);
printf("ss:md5: %s, %s\n", file_md5_val, file_line); printf("ss:md5: %s, %s\n", file_md5_val, file_line);
return NULL;
} }
/* system execute function */ /* system execute function */
@ -131,6 +133,7 @@ int process_line(char *line)
{ {
printf("ss:warning:%s exited with error code %d\n", cmd_line, return_val); printf("ss:warning:%s exited with error code %d\n", cmd_line, return_val);
} }
return NULL;
} }
/* write */ /* write */
@ -152,8 +155,7 @@ int process_line(char *line)
/* Check if file exists and can be opened */ /* Check if file exists and can be opened */
if(write_file == NULL) if(write_file == NULL)
{ {
printf("ss:error:cannot write to '%s'.\n", tok_srch); syn_error("ss:error:write, cannot write to file specified");
return 1;
} }
/* strtok to the content that will be written to file */ /* strtok to the content that will be written to file */
@ -171,6 +173,7 @@ int process_line(char *line)
file_content = parse_vars(tok_srch); file_content = parse_vars(tok_srch);
fprintf(write_file, "%s\n", file_content); fprintf(write_file, "%s\n", file_content);
fclose(write_file); fclose(write_file);
return NULL;
} /* write function */ } /* write function */
/* read function */ /* read function */
@ -196,8 +199,7 @@ int process_line(char *line)
/* Check if file was opened successfully */ /* Check if file was opened successfully */
if(read_file == NULL) if(read_file == NULL)
{ {
printf("ss: error: failed to open '%s'.\n", tok_srch); syn_error("ss:error:read, failed to read from file");
return 1;
} }
while(fgets(read_line, sizeof(read_line), read_file) != NULL) while(fgets(read_line, sizeof(read_line), read_file) != NULL)
@ -206,6 +208,7 @@ int process_line(char *line)
} }
fclose(read_file); fclose(read_file);
return NULL;
} /* read function */ } /* read function */
else if(strncmp("delete", tok_srch, 6) == 0) { else if(strncmp("delete", tok_srch, 6) == 0) {
@ -222,25 +225,23 @@ int process_line(char *line)
if(access(del_filename, W_OK) == 0) if(access(del_filename, W_OK) == 0)
{ {
remove(del_filename); remove(del_filename);
return 0; return NULL;
} }
else else
{ {
printf("ss: error: '%s' is not accessible", del_filename); syn_error("ss:error:delete, file is not accessible");
return 1;
} }
} }
else else
{ {
printf("ss: error: '%s' not found", del_filename); syn_error("ss:error:delete, file not found");
return 1;
} }
} }
else if(strcmp(tok_srch, "\n") != 0 && strcmp(tok_srch, "") != 0) { else if(strcmp(tok_srch, "\n") != 0 && strcmp(tok_srch, "") != 0) {
if(tok_srch[strlen(tok_srch)-1] == '\n') if(tok_srch[strlen(tok_srch)-1] == '\n')
{ {
return 1; return NULL;
} }
else else
{ {
@ -253,5 +254,5 @@ int process_line(char *line)
} }
} }
return 0; return NULL;
} }

View File

@ -5,4 +5,4 @@
View README file supplied with this software for more details View README file supplied with this software for more details
*/ */
int process_line(char *line); char *process_line(char *line);

View File

@ -34,11 +34,14 @@ int main(int argc, char **argv)
/* Run while loop if file is empty. */ /* Run while loop if file is empty. */
if(script != NULL) if(script != NULL)
{ {
/* parse each line from the script in order. */ /* parse each line from the script in order. */
while(fgets(script_line, sizeof(script_line), script) != NULL) while(fgets(script_line, sizeof(script_line), script) != NULL)
{ {
process_line(script_line); char *return_dat;
return_dat = process_line(script_line);
if(return_dat == NULL) continue;
// If return is not null, provide the return
printf("%s\n", return_dat);
} /* end of while */ } /* end of while */
} /* file null */ } /* file null */