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 "md5.h"
int process_line(char *line)
char *process_line(char *line)
{
char *tok_srch;
@ -27,7 +27,7 @@ int process_line(char *line)
/* if line starts with a comment, skip */
if(strncmp("#",tok_srch,1) == 0)
{
return 0;
return NULL;
}
/* print function */
@ -41,7 +41,7 @@ int process_line(char *line)
if(strtok(NULL, "\"") == NULL)
syn_error("ss:error:print syntax error, missing end quote");
printf("%s\n", parsed);
return parsed;
}
else
{
@ -63,6 +63,7 @@ int process_line(char *line)
tok_srch[strlen(tok_srch)-1] = 0;
}
sleep(atoi(tok_srch));
return NULL;
}
/* ss encrypt function */
@ -79,7 +80,7 @@ int process_line(char *line)
{
char *encrp;
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);
if(var_conv != NULL)
{
char *encrp;
encrp = ss_decrypt(var_conv);
printf("%s\n", encrp);
char *decrp;
decrp = ss_decrypt(var_conv);
return decrp;
}
}
@ -113,6 +114,7 @@ int process_line(char *line)
file_line = parse_vars(tok_srch);
file_md5_val = md5_file(file_line);
printf("ss:md5: %s, %s\n", file_md5_val, file_line);
return NULL;
}
/* 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);
}
return NULL;
}
/* write */
@ -152,8 +155,7 @@ int process_line(char *line)
/* Check if file exists and can be opened */
if(write_file == NULL)
{
printf("ss:error:cannot write to '%s'.\n", tok_srch);
return 1;
syn_error("ss:error:write, cannot write to file specified");
}
/* 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);
fprintf(write_file, "%s\n", file_content);
fclose(write_file);
return NULL;
} /* write function */
/* read function */
@ -196,8 +199,7 @@ int process_line(char *line)
/* Check if file was opened successfully */
if(read_file == NULL)
{
printf("ss: error: failed to open '%s'.\n", tok_srch);
return 1;
syn_error("ss:error:read, failed to read from file");
}
while(fgets(read_line, sizeof(read_line), read_file) != NULL)
@ -206,6 +208,7 @@ int process_line(char *line)
}
fclose(read_file);
return NULL;
} /* read function */
else if(strncmp("delete", tok_srch, 6) == 0) {
@ -222,25 +225,23 @@ int process_line(char *line)
if(access(del_filename, W_OK) == 0)
{
remove(del_filename);
return 0;
return NULL;
}
else
{
printf("ss: error: '%s' is not accessible", del_filename);
return 1;
syn_error("ss:error:delete, file is not accessible");
}
}
else
{
printf("ss: error: '%s' not found", del_filename);
return 1;
syn_error("ss:error:delete, file not found");
}
}
else if(strcmp(tok_srch, "\n") != 0 && strcmp(tok_srch, "") != 0) {
if(tok_srch[strlen(tok_srch)-1] == '\n')
{
return 1;
return NULL;
}
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
*/
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. */
if(script != NULL)
{
/* parse each line from the script in order. */
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 */
} /* file null */