Custom file struct

This commit is contained in:
2024-02-11 18:02:22 -06:00
parent f680fe177d
commit 72c117b388
5 changed files with 99 additions and 3 deletions

View File

@@ -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

View File

@@ -6,6 +6,8 @@
#define NOMINIFY_CHAR '&'
#define SEPARATOR_CHAR ','
#define SHELL_CHAR '$'
#define NEST_START 'n'
#define NEST_END 'm'
#define ALLOW_SHELL

16
src/include/ncdgfile.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef HAVE_NCDGFILE
#define HAVE_NCDGFILE
#include <stdio.h>
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

75
src/ncdgfile.c Normal file
View File

@@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ncdgfile.h>
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);
}

View File

@@ -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;