slidescript now processes programs using popen if no built-in found.

This commit is contained in:
Chris Dorman 2022-05-18 04:06:25 -07:00
parent 273382b200
commit 5ffbec0ce0
4 changed files with 53 additions and 64 deletions

View File

@ -28,10 +28,6 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
// For tar containers
#if !defined(__APPLE__)
#include <sys/sysmacros.h>
#endif
// For file handling
#include <assert.h>
#include <sys/stat.h>
@ -79,6 +75,8 @@
#define ENCSTEPODD 2
#define ENCSTEPEVEN 3
#define PATH "/bin/"
// HELP PRINTOUT
#define FUNCTION_HELP "-[Main Functions]-\n Need function information? Use: help <function> \n list:\n" \
@ -100,7 +98,7 @@
"read, " \
"write, " \
"cat, " \
"exec, unset \n " \
"unset \n " \
"\n-[Variables, Pipes, and Backquoting]-\nExample:\n" \
" FORK=`isfile \"index.html\"` -> returns 1 on find\n" \
" write \"port.txt\" \"8888\" -> writes '8888' to port.txt\n" \

View File

@ -42,3 +42,4 @@ char *ss_time();
void gen_random_string(char *s, const int len);
char *remove_extension(char *string);
int check_for_var(char *string);
int ss_exec(char **argv);

View File

@ -26,6 +26,12 @@ char *process_line (char *line)
char *tok_srch, *retbuf, *filebuf, *compbuf, *loopbuf;
//printf("%s", &line[strlen(line)-2]);
static char pathbuf[MAXPATHLENGTH+1];
// Needed for strncpy
static char line_save[MAXPATHLENGTH+1];
// save full line if popen is used, and no function is found
bzero(line_save, sizeof(line_save));
strcpy(line_save, line);
// Check to see if we're dealing with a
// variable (for the end if the lexer)
@ -437,7 +443,7 @@ char *process_line (char *line)
}
// Print directory
else if (strncmp("showdir", tok_srch, 7) == 0 || strncmp("ls", tok_srch, 2) == 0)
else if (strncmp("showdir", tok_srch, 7) == 0)
{
char *dirname;
@ -1112,38 +1118,6 @@ char *process_line (char *line)
return retbuf;
}
/* system execute function */
else if (strncmp("exec", tok_srch, 4) == 0 || strncmp("~", tok_srch, 1) == 0)
{
char *cmd_line;
int return_val;
tok_srch = strtok_next ("\"");
if (tok_srch == NULL)
{
x_warn("ss:warn:exec syntax error, missing quote?");
return NULL;
}
if (strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0)
{
x_warn("ss:warn:exec syntax error, missing execution switch?");
return NULL;
}
if (strtok_next ("\"") == NULL)
{
x_warn("ss:warn:exec syntax error, missing quote?");
return NULL;
}
cmd_line = parse_vars(tok_srch);
return_val = system(cmd_line);
if (return_val != 0)
x_warn("ss:warn:exec returned error code");
return NULL;
}
else if (strncmp("search",tok_srch,6) == 0)
{
char filename[MAX_FILENAME_BUFSIZE];
@ -1495,39 +1469,39 @@ char *process_line (char *line)
// Set variables if needed.
else if (strcmp(tok_srch, "\n") != 0 && strcmp(tok_srch, "") != 0)
{
if (tok_srch[strlen(tok_srch)-1] == '\n')
if(isVar == 1)
{
return NULL;
int varc = get_var_count();
char *varname_tmp = tok_srch;
char *bq_check;
tok_srch = strtok_next ("=");
// Check for back quotes, return string with backquotes processed
bq_check = strip_nl(parse_vars(parse_bq(tok_srch)));
// Don't check if variable is blank, if so let it fly!
set_var(varc, varname_tmp, bq_check);
printf("ss: var '%s' -> %s\n", varname_tmp, get_var_data(varname_tmp));
}
else
{
if(isVar == 1)
{
int varc = get_var_count();
char *varname_tmp = tok_srch;
char *bq_check;
tok_srch = strtok_next ("=");
FILE* p = popen(line_save, "r");
// Check for back quotes, return string with backquotes processed
bq_check = strip_nl(parse_vars(parse_bq(tok_srch)));
// Don't check if variable is blank, if so let it fly!
set_var(varc, varname_tmp, bq_check);
printf("ss: var '%s' -> %s\n", varname_tmp, get_var_data(varname_tmp));
}
else
{
if(tok_srch != NULL)
{
printf("ss:warn:unknown function: %s\n", tok_srch);
}
else
{
printf("ss:warn:unknown function\n");
}
return NULL;
}
if(!p)
{
printf("ss:warn:command not found: %s\n", line_save);
}
else
{
char buff[1024];
while (fgets(buff, sizeof(buff), p)) {
printf("%s", buff);
}
}
pclose(p);
}
}
return NULL;
}

View File

@ -159,3 +159,19 @@ int check_for_var(char *string)
return 0;
}
}
// Execute - Run programs found within the *nix system
int ss_exec(char **argv)
{
int exec_ret = execve(argv[0], (char **)argv, NULL);
if ((exec_ret) == -1)
{
printf("ss:warn:failed to execute program [%d]", exec_ret);
return exec_ret;
}
else
{
return exec_ret;
}
}