Custom file struct
This commit is contained in:
@@ -178,9 +178,8 @@ Turns into
|
|||||||
!dlrow olleH
|
!dlrow olleH
|
||||||
```
|
```
|
||||||
|
|
||||||
Things between an `@n` and `@m` tag get processed twice. A double escape char `@@`
|
Things between an `@n` and `@m` tag get processed twice. Defining variables in a
|
||||||
gets escaped into a single escape char `@`. Defining variables in a nest is
|
nest is illegal, so
|
||||||
illegal, so
|
|
||||||
|
|
||||||
```
|
```
|
||||||
@n @@= dynamic_variable @$./generate_variable_value.sh@ @@ @m
|
@n @@= dynamic_variable @$./generate_variable_value.sh@ @@ @m
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
#define NOMINIFY_CHAR '&'
|
#define NOMINIFY_CHAR '&'
|
||||||
#define SEPARATOR_CHAR ','
|
#define SEPARATOR_CHAR ','
|
||||||
#define SHELL_CHAR '$'
|
#define SHELL_CHAR '$'
|
||||||
|
#define NEST_START 'n'
|
||||||
|
#define NEST_END 'm'
|
||||||
|
|
||||||
#define ALLOW_SHELL
|
#define ALLOW_SHELL
|
||||||
|
|
||||||
|
|||||||
16
src/include/ncdgfile.h
Normal file
16
src/include/ncdgfile.h
Normal 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
75
src/ncdgfile.c
Normal 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);
|
||||||
|
}
|
||||||
@@ -157,6 +157,10 @@ autoescapeend:
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
case NEST_START:
|
||||||
|
return 0;
|
||||||
|
case NEST_END:
|
||||||
|
return 0;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Error in expansion phase: Unknown escape %c%c\n", ESCAPE_CHAR, data->data[i]);
|
fprintf(stderr, "Error in expansion phase: Unknown escape %c%c\n", ESCAPE_CHAR, data->data[i]);
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user