Added search function for grep like activities
This commit is contained in:
parent
3f25d81908
commit
e5c0aba078
2
Makefile
2
Makefile
@ -13,7 +13,7 @@ CPPFLAGS += -DVERSION=$(VERSION) -D_FORTIFY_SOURCE=2
|
||||
LDFLAGS += -Wl,-O1
|
||||
BIN ?= slidescript
|
||||
|
||||
OBJECTS = src/main.o src/functions.o src/util.o src/vars.o src/enc.o src/md5.o src/pipe.o src/network.o src/math.o src/inset.o
|
||||
OBJECTS = src/main.o src/functions.o src/util.o src/vars.o src/enc.o src/md5.o src/pipe.o src/network.o src/math.o src/inset.o src/search.o
|
||||
|
||||
all: main
|
||||
|
||||
|
@ -51,6 +51,9 @@ at this moment!
|
||||
* Sleep (Zzz) example (sleeps for 2 seconds):
|
||||
* sleep 2
|
||||
|
||||
* Search functions, example:
|
||||
* search "README.txt" "SlideScript" -> returns each line that 'SlideScript' is found on
|
||||
|
||||
* Variable setting and passing
|
||||
* filename=file.txt -> filename
|
||||
* filedata=File '%filename%' is being moved to moo -> %filename% is populated as file.txt
|
||||
|
@ -26,6 +26,8 @@
|
||||
#include <netinet/in.h>
|
||||
// For checking if file exists
|
||||
#include <sys/stat.h>
|
||||
// For string searching
|
||||
#include <regex.h>
|
||||
|
||||
#define MAX_VAR_NAME_LEN 512
|
||||
#define MAX_VAR_NAME_BUFSIZE (MAX_VAR_NAME_LEN + 1)
|
||||
@ -47,6 +49,12 @@
|
||||
// For Expr
|
||||
#define MAX_EXPR_ARGS 128
|
||||
#define MAX_EXPR_LEN 512
|
||||
// For search
|
||||
#define MAX_SEARCH_LEN 65536
|
||||
#define MAX_SEARCH_BUFSIZE (MAX_SEARCH_LEN + 1)
|
||||
// For files
|
||||
#define MAX_FILENAME_LEN 1024
|
||||
#define MAX_FILENAME_BUFSIZE (MAX_FILENAME_LEN + 1)
|
||||
// END
|
||||
#define TOKEN '%'
|
||||
#define TOKEN_STR "%"
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "math.h"
|
||||
#include "md5.h"
|
||||
#include "network.h"
|
||||
#include "search.h"
|
||||
#include "inset.h"
|
||||
|
||||
char *process_line(char *line)
|
||||
@ -519,6 +520,82 @@ char *process_line(char *line)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
else if(strncmp("search",tok_srch,6) == 0)
|
||||
{
|
||||
char filename[MAX_FILENAME_BUFSIZE];
|
||||
char *search_str, *retval;
|
||||
FILE* search_file = NULL;
|
||||
/* strtok to filename of function */
|
||||
|
||||
tok_srch = strtok(NULL, "\"");
|
||||
if(tok_srch == NULL)
|
||||
{
|
||||
syn_warn("ss:warn:search syntax error, missing quote?");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Check to see if syntax is correct */
|
||||
if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0)
|
||||
{
|
||||
syn_warn("ss:warn:search syntax error, missing filename");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* open file */
|
||||
if(MAX_FILENAME_LEN < atoi(parse_vars(tok_srch)))
|
||||
{
|
||||
syn_warn("ss:warn:search, filename too long");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcat(filename, parse_vars(tok_srch));
|
||||
search_file = fopen(filename, "r");
|
||||
/* Check if file exists and can be opened */
|
||||
if(search_file == NULL)
|
||||
{
|
||||
fclose(search_file);
|
||||
syn_warn("ss:warn:search, failed to open file");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* strtok to the content that will be written to file */
|
||||
tok_srch = strtok(NULL, "\"");
|
||||
if(tok_srch == NULL)
|
||||
{
|
||||
fclose(search_file);
|
||||
syn_warn("ss:warn:search syntax error, missing quote?");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tok_srch = strtok(NULL, "\"");
|
||||
if(tok_srch == NULL)
|
||||
{
|
||||
fclose(search_file);
|
||||
syn_warn("ss:warn:search syntax error, missing quote?");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0)
|
||||
{
|
||||
fclose(search_file);
|
||||
syn_warn("ss:warn:search syntax error, missing write data?");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(strtok(NULL, "\"") == NULL)
|
||||
{
|
||||
fclose(search_file);
|
||||
syn_warn("ss:warn:search syntax error, missing end quote?");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
search_str = parse_vars(tok_srch);
|
||||
retval = strip_nl(ss_search(filename, search_file, search_str));
|
||||
fclose(search_file);
|
||||
return retval;
|
||||
} /* search function */
|
||||
|
||||
|
||||
/* write */
|
||||
else if(strncmp("write",tok_srch,5) == 0)
|
||||
{
|
||||
@ -547,6 +624,7 @@ char *process_line(char *line)
|
||||
/* Check if file exists and can be opened */
|
||||
if(write_file == NULL)
|
||||
{
|
||||
fclose(write_file);
|
||||
syn_warn("ss:warn:write, failed to open file");
|
||||
return NULL;
|
||||
}
|
||||
@ -555,6 +633,7 @@ char *process_line(char *line)
|
||||
tok_srch = strtok(NULL, "\"");
|
||||
if(tok_srch == NULL)
|
||||
{
|
||||
fclose(write_file);
|
||||
syn_warn("ss:warn:write syntax error, missing quote?");
|
||||
return NULL;
|
||||
}
|
||||
@ -562,6 +641,7 @@ char *process_line(char *line)
|
||||
tok_srch = strtok(NULL, "\"");
|
||||
if(tok_srch == NULL)
|
||||
{
|
||||
fclose(write_file);
|
||||
syn_warn("ss:warn:write syntax error, missing quote?");
|
||||
return NULL;
|
||||
}
|
||||
@ -575,6 +655,7 @@ char *process_line(char *line)
|
||||
|
||||
if(strtok(NULL, "\"") == NULL)
|
||||
{
|
||||
fclose(write_file);
|
||||
syn_warn("ss:warn:write syntax error, missing end quote?");
|
||||
return NULL;
|
||||
}
|
||||
@ -698,6 +779,7 @@ char *process_line(char *line)
|
||||
/* strtok to the content that will be written to file */
|
||||
if(read_file == NULL)
|
||||
{
|
||||
fclose(read_file);
|
||||
syn_warn("ss:warn:cat, failed to open file");
|
||||
return NULL;
|
||||
}
|
||||
@ -705,6 +787,7 @@ char *process_line(char *line)
|
||||
tok_srch = strtok(NULL, "\"");
|
||||
if(tok_srch == NULL)
|
||||
{
|
||||
fclose(read_file);
|
||||
syn_warn("ss:warn:cat syntax error, missing quote?");
|
||||
return NULL;
|
||||
}
|
||||
@ -712,18 +795,21 @@ char *process_line(char *line)
|
||||
tok_srch = strtok(NULL, "\"");
|
||||
if(tok_srch == NULL)
|
||||
{
|
||||
fclose(read_file);
|
||||
syn_warn("ss:warn:cat syntax error, missing quote?");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(strcmp(tok_srch, "\n") == 0 || strcmp(tok_srch, " \n") == 0)
|
||||
{
|
||||
fclose(read_file);
|
||||
syn_warn("ss:warn:cat syntax error, missing write data?");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(strtok(NULL, "\"") == NULL)
|
||||
{
|
||||
fclose(read_file);
|
||||
syn_warn("ss:warn:cat syntax error, missing end quote?");
|
||||
return NULL;
|
||||
}
|
||||
@ -752,6 +838,7 @@ char *process_line(char *line)
|
||||
else
|
||||
{
|
||||
free(dynfile);
|
||||
fclose(read_file);
|
||||
syn_warn("ss:warn:cat, file too large (2MB max)");
|
||||
return NULL;
|
||||
}
|
||||
|
53
src/search.c
Normal file
53
src/search.c
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
SlideScript - minimalistic top-down scripting language.
|
||||
(C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2)
|
||||
|
||||
View README file supplied with this software for more details
|
||||
*/
|
||||
|
||||
#include "deps.h"
|
||||
#include "search.h"
|
||||
#include "util.h"
|
||||
|
||||
static char retbuf[MAX_SEARCH_BUFSIZE];
|
||||
|
||||
char *ss_search(const char *filename, FILE *fp, char *pat)
|
||||
{
|
||||
char *s_buf = NULL;
|
||||
size_t size = 0;
|
||||
int ret;
|
||||
char tmpbuf[TMPBUF_LEN+1];
|
||||
int flags = REG_NOSUB; /* don't need where-matched info */
|
||||
|
||||
regcomp(&pattern, pat, flags);
|
||||
|
||||
while(getline(&s_buf, &size, fp) != -1)
|
||||
{
|
||||
ret = regexec(&pattern, s_buf, 0, NULL, 0);
|
||||
if (ret != 0)
|
||||
{
|
||||
if (ret != REG_NOMATCH) {
|
||||
syn_warn("ss:warn:search, pattern not found");
|
||||
free(s_buf);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(strlen(s_buf) < MAX_SEARCH_LEN)
|
||||
{
|
||||
sprintf(tmpbuf, "ss: %s: %s", filename, s_buf);
|
||||
strcat(retbuf, tmpbuf);
|
||||
bzero(tmpbuf, TMPBUF_LEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
syn_warn("ss:warn:search, static buffer maxed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(s_buf);
|
||||
|
||||
return retbuf;
|
||||
}
|
12
src/search.h
Normal file
12
src/search.h
Normal file
@ -0,0 +1,12 @@
|
||||
/*
|
||||
SlideScript - minimalistic top-down scripting language.
|
||||
(C) Copyright 2014-2021 Chris Dorman - some rights reserved (GPLv2)
|
||||
|
||||
View README file supplied with this software for more details
|
||||
*/
|
||||
|
||||
#define TMPBUF_LEN 2048
|
||||
|
||||
regex_t pattern;
|
||||
|
||||
char *ss_search(const char *filename, FILE *fd, char *pat);
|
Loading…
x
Reference in New Issue
Block a user