From 72c117b3881088f7360c4da4d66e7e105441882c Mon Sep 17 00:00:00 2001 From: Nate Choe Date: Sun, 11 Feb 2024 18:02:22 -0600 Subject: [PATCH] Custom file struct --- README.md | 5 ++- src/include/config.h | 2 ++ src/include/ncdgfile.h | 16 +++++++++ src/ncdgfile.c | 75 ++++++++++++++++++++++++++++++++++++++++++ src/parse.c | 4 +++ 5 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 src/include/ncdgfile.h create mode 100644 src/ncdgfile.c diff --git a/README.md b/README.md index 09aba4f..6f72e90 100644 --- a/README.md +++ b/README.md @@ -178,9 +178,8 @@ Turns into !dlrow olleH ``` -Things between an `@n` and `@m` tag get processed twice. A double escape char `@@` -gets escaped into a single escape char `@`. Defining variables in a nest is -illegal, so +Things between an `@n` and `@m` tag get processed twice. Defining variables in a +nest is illegal, so ``` @n @@= dynamic_variable @$./generate_variable_value.sh@ @@ @m diff --git a/src/include/config.h b/src/include/config.h index e9f334b..d4a9f20 100644 --- a/src/include/config.h +++ b/src/include/config.h @@ -6,6 +6,8 @@ #define NOMINIFY_CHAR '&' #define SEPARATOR_CHAR ',' #define SHELL_CHAR '$' +#define NEST_START 'n' +#define NEST_END 'm' #define ALLOW_SHELL diff --git a/src/include/ncdgfile.h b/src/include/ncdgfile.h new file mode 100644 index 0000000..c7ccf5e --- /dev/null +++ b/src/include/ncdgfile.h @@ -0,0 +1,16 @@ +#ifndef HAVE_NCDGFILE +#define HAVE_NCDGFILE + +#include + +struct ncdgfile { + int (*putc)(struct ncdgfile *file, int c); + int (*puts)(struct ncdgfile *file, char *s); + void (*free)(struct ncdgfile *file); + void *handle; +}; + +struct ncdgfile *file2ncdg(FILE *file); +struct ncdgfile *stringfile(void); + +#endif diff --git a/src/ncdgfile.c b/src/ncdgfile.c new file mode 100644 index 0000000..76131f0 --- /dev/null +++ b/src/ncdgfile.c @@ -0,0 +1,75 @@ +#include +#include + +#include +#include + +static int fileputc(struct ncdgfile *file, int c); +static int fileputs(struct ncdgfile *file, char *s); +static void filefree(struct ncdgfile *file); + +static int stringputc(struct ncdgfile *file, int c); +static int stringputs(struct ncdgfile *file, char *s); +static void stringfree(struct ncdgfile *file); + +struct ncdgfile *file2ncdg(FILE *file) { + struct ncdgfile *ret; + if ((ret = malloc(sizeof *ret)) == NULL) { + return NULL; + } + ret->putc = fileputc; + ret->puts = fileputs; + ret->free = filefree; + ret->handle = (void *) file; + return ret; +} + +struct ncdgfile *stringfile(void) { + struct ncdgfile *ret; + if ((ret = malloc(sizeof *ret)) == NULL) { + goto error1; + } + if ((ret->handle = (void *) newstring()) == NULL) { + goto error2; + } + ret->putc = stringputc; + ret->puts = stringputs; + ret->free = stringfree; + return ret; +error2: + free(ret); +error1: + return NULL; +} + +static int fileputc(struct ncdgfile *file, int c) { + return fputc(c, (FILE *) file->handle) == EOF ? -1 : 0; +} + +static int fileputs(struct ncdgfile *file, char *s) { + return fputs(s, (FILE *) file->handle) == EOF ? -1 : 0; +} + +static void filefree(struct ncdgfile *file) { + fclose((FILE *) file->handle); + free(file); +} + +static int stringputc(struct ncdgfile *file, int c) { + return appendchar((struct string *) file->handle, c) ? -1 : 0; +} + +static int stringputs(struct ncdgfile *file, char *s) { + long i; + for (i = 0; s[i] != '\0'; ++i) { + if (appendchar((struct string *) file->handle, s[i])) { + return -1; + } + } + return 0; +} + +static void stringfree(struct ncdgfile *file) { + freestring((struct string *) file->handle); + free(file); +} diff --git a/src/parse.c b/src/parse.c index d93cbbd..f6aaf2a 100644 --- a/src/parse.c +++ b/src/parse.c @@ -157,6 +157,10 @@ autoescapeend: break; } #endif + case NEST_START: + return 0; + case NEST_END: + return 0; default: fprintf(stderr, "Error in expansion phase: Unknown escape %c%c\n", ESCAPE_CHAR, data->data[i]); return 1;