~aleteoryx/gloss

c76a78c66b58c4efd80462f3b6ad42c5d5ecea7e — Aleteoryx 3 months ago cb36f07
don't leak memory
2 files changed, 26 insertions(+), 13 deletions(-)

M gloss.py
M markup.c
M gloss.py => gloss.py +5 -5
@@ 119,15 119,15 @@ def first_pass(slug, fp):

class Markup:
	def __init__(self, where, *, cfile='markup.c', bfile='markup'):
		self.cfile = str(Path(where, cfile))
		self.bfile = str(Path(where, bfile))
		self.cfile = Path(where, cfile)
		self.bfile = Path(where, bfile)

		if stat(self.cfile).st_mtime > stat(self.bfile).st_mtime:
		if not self.bfile.exists() or self.cfile.stat().st_mtime > self.bfile.stat().st_mtime:
			print("recompiling markup subsystem...")
			runcmd(['cc', self.cfile, '-o', self.bfile, '-Wall'], check=True)
			runcmd(['cc', str(self.cfile), '-o', str(self.bfile), '-Wall'], check=True)
			print("recompiled!")
		
		self.proc = Popen([self.bfile, 'convert'], stdin=PIPE, stdout=PIPE, text=True)
		self.proc = Popen([str(self.bfile), 'convert'], stdin=PIPE, stdout=PIPE, text=True)
	
	def process(self, text):
		self.proc.stdin.write(text+"\n")

M markup.c => markup.c +21 -8
@@ 22,7 22,7 @@
#define DEBUG 0
#endif

#define PLINE() fprintf(stderr, "on line %d in func %s\n", __LINE__, __FILE__)
#define PLINE() fprintf(stderr, "on line %d\n", __LINE__)

#define LEN(x) (sizeof(x) / sizeof(x[0]))



@@ 79,6 79,19 @@ new_segment(int type, size_t length, const char *text)

	return new;
}
static inline void
shift_segment(struct segment **seg)
{
	struct segment *next = (*seg)->next;
	free(*seg);
	*seg = next;
}
static inline void
free_segments(struct segment *seg)
{
	while(seg != NULL)
		shift_segment(&seg);
}

static inline void
push_segment(struct segment **seg, int type, size_t length, const char *text)


@@ 154,7 167,7 @@ segmentize(size_t length, const char *source)
	// p is the start of the most recent text segment
	const char *p = source, *i, *newi, *end = source + length;
	int in_italic = 0;
	struct segment *head = new_segment(SEG_HEAD, 0, NULL), *ret;
	struct segment *head = new_segment(SEG_HEAD, 0, NULL);
	struct segment *tail = head;
	struct segment *italic_head, *italic_tail;
	struct segment *link_head, *link_tail;


@@ 243,9 256,8 @@ segmentize(size_t length, const char *source)
		tail->next = new_segment(SEG_TEXT, end - p, p);
	}

	ret = head->next;
	free(head);
	return ret;
	shift_segment(&head);
	return head;
}

static const char *


@@ 284,16 296,17 @@ convert()
{
	size_t length;
	const char *s;
	struct segment *seg;
	struct segment *seg, *p;

	while((s = nextline()) != NULL){
		if(DEBUG)
			fprintf(stderr, "processing line: %s\n", s);
		length = strlen(s);
		seg = segmentize(length, s);
		for(; seg != NULL; seg = seg->next)
			print_segment(seg);
		for(p = seg; p != NULL; p = p->next)
			print_segment(p);
		puts("NEXT");
		free_segments(seg);
		fflush(stdout);
	}
}