slidescript/src/main.c

114 lines
2.7 KiB
C
Raw Normal View History

2021-04-05 04:22:57 -07:00
/*
2021-04-06 01:32:15 -07:00
SlideScript - minimalistic top-down scripting language.
2022-05-17 19:24:54 -07:00
(C) Copyright 2014-2022 Chris Dorman - some rights reserved (GPLv2)
2021-04-05 04:22:57 -07:00
2021-04-06 01:32:15 -07:00
View README file supplied with this software for more details
2021-04-05 04:22:57 -07:00
*/
2021-04-07 15:15:53 -07:00
#define MY_GLOBAL
2021-04-15 18:06:30 -07:00
#define RES_UX3 1 /* resolve UX3 global variables */
2021-04-11 14:43:56 -07:00
#include "inc/deps.h"
2021-04-15 18:06:30 -07:00
#include "inc/eprintf.h"
2021-04-11 14:43:56 -07:00
#include "inc/util.h"
#include "inc/lexer.h"
2021-04-11 14:43:56 -07:00
#include "inc/pipe.h"
#include "inc/inset.h"
2021-04-11 14:43:56 -07:00
#include "inc/vars.h"
2021-04-05 04:22:57 -07:00
int main(int argc, char **argv)
{
int is_interactive = 0;
FILE *script;
char script_line[MAX_STRING_BUFSIZE+1];
2021-04-05 04:22:57 -07:00
// set environment variables
setenv("PATH", PATH, 0);
2021-04-05 04:22:57 -07:00
if (argc == 1)
{
script = stdin;
if (isatty (fileno (stdin))) is_interactive = 1;
2021-04-05 04:22:57 -07:00
}
else
{
parse_args (argc, argv);
script = fopen (argv [1], "r");
}
/* check if config opens successfully */
if(script == NULL) {
printf("Error: failed to open %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
/* Run while loop if file isnt empty. */
if (script != NULL)
2021-04-05 04:22:57 -07:00
{
while (1)
{
2021-04-08 02:19:12 -07:00
char *return_dat;
int pipechk;
int varc, ii;
if (is_interactive)
{
printf ("%s", ":ss:prompt: ");
fflush (stdout);
}
/* parse each line from the script in order. */
if (fgets (script_line,
sizeof(script_line), script) == NULL) break;
2021-04-08 02:19:12 -07:00
ss_piping(script_line);
2021-04-06 22:27:35 -07:00
2021-04-08 02:19:12 -07:00
pipechk = get_cmd_count();
return_dat = process_line(spipe[0].command);
2021-04-06 22:27:35 -07:00
// Blank line, getting up outta here.
if(return_dat == NULL) continue;
2021-04-06 22:27:35 -07:00
2021-04-08 02:19:12 -07:00
varc = get_var_count();
if(pipechk > 1)
2021-04-06 22:27:35 -07:00
{
2021-04-08 02:19:12 -07:00
for(ii = 1; ii < pipechk; ii++)
{
set_var(varc, "PIPE", return_dat);
// Process functions based on previous pipe return
return_dat = process_line(spipe[ii].command);
2021-04-15 17:09:54 -07:00
2021-04-08 02:19:12 -07:00
set_var(varc, "\0", "\0");
2021-05-09 17:48:26 -07:00
2021-04-08 02:19:12 -07:00
// Check to see if there's anything to even display
if(return_dat == NULL)
{
2021-05-09 17:48:26 -07:00
continue;
}
2021-04-08 02:19:12 -07:00
}
2021-04-06 22:27:35 -07:00
}
2021-04-08 02:19:12 -07:00
// Print the pipe line
if(return_dat != NULL)
2021-04-14 13:50:28 -07:00
{
2021-04-06 22:27:35 -07:00
printf("%s\n", return_dat);
2021-04-14 13:50:28 -07:00
fflush(stdout);
}
// Free used mallocs within QM_SS & QM_BQ
qflush(QM_SS);
qflush(QM_BQ);
2021-04-05 04:22:57 -07:00
} /* end of while */
} /* file null */
/* Free variables after:
a) scripts parsed or
b) user session is over */
qflush(QM_VARIABLES);
2021-04-05 04:22:57 -07:00
if (argc != 1 ) fclose(script);
exit(EXIT_SUCCESS);
}