From 4e44d053e0bc1de0fd9043d024d0952328d7cbc7 Mon Sep 17 00:00:00 2001 From: glenda Date: Fri, 31 Jan 2025 03:51:23 +0000 Subject: [PATCH] becoming a supervillain --- mkfile | 3 +- pres.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 pres.c diff --git a/mkfile b/mkfile index e17a54c0bb68ecaa70a7641a55421a602670f4ef..f47b18040c3eb6eb285f3e873e48085cafddaa12 100644 --- a/mkfile +++ b/mkfile @@ -4,7 +4,8 @@ bins=\ pong\ maze\ spiral\ - queuesrv + queuesrv\ + pres all:VQ: $bins diff --git a/pres.c b/pres.c new file mode 100644 index 0000000000000000000000000000000000000000..a988310e5cffa070c87799e9f8522d7dd3881ca2 --- /dev/null +++ b/pres.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include + +int checkonly; +char *file, *contents; + +typedef struct Slide { + char *cmd; + char *arg; + int nlines; + char *lines[]; +} Slide; + +char * +nextline(char **p) +{ + char *line; + while(1){ + line = *p; + while(**p != '\n' && **p != '\0') (*p)++; + if(**p == '\0') + return nil; + + **p = '\0'; + (*p)++; + return line; + } +} + +Slide * +nextslide(char **p) +{ + char *cmd, *arg, *line1, *line, *lineN; + int nlines, blines, i; + Slide *s; + + while(1){ + if((cmd = nextline(p)) == nil) + return nil; + if(cmd[0] != '%') + continue; + break; + } + + cmd++; + arg = cmd; + + while(*arg != ' ' && *arg != '\t' && *arg != '\0') arg++; + if(*arg == '\0') + arg = nil; + else + *(arg++) = '\0'; + + line1 = nextline(p); + if(line1 != nil && *line1 == '%'){ + *p = line1; + line1[strlen(line1)] = '\n'; + line1 = nil; + } + if(line1 == nil){ + s = malloc(sizeof(Slide)); + s->cmd = cmd; + s->arg = arg; + s->nlines = 0; + return s; + } + lineN = line1; + nlines = 1; + blines = 0; + + while((line = nextline(p)) != nil){ + if(*line == '\0'){ + blines++; + continue; + } + if(*line == '%'){ + *p = line; + line[strlen(line)] = '\n'; + break; + } + + nlines += blines; + blines = 0; + nlines++; + lineN = line; + } + + s = malloc(sizeof(Slide) + sizeof(char*) * nlines); + + s->cmd = cmd; + s->arg = arg; + s->nlines = nlines; + + line = line1; + for(i = 0; line != lineN; i++){ + s->lines[i] = line; + line += strlen(line) + 1; + } + s->lines[i] = lineN; + + return s; +} + +void +checkfile(void) +{ + int i; + char *c2, *p; + Slide *s; + + c2 = strdup(contents); + p = c2; + + while((s = nextslide(&p)) != nil){ + print("Slide: %s (%d lines)\n", s->cmd, s->nlines); + if(s->arg != nil) + print("\targ: %s\n", s->arg); + for(i = 0; i < s->nlines; i++) + print("\tline %d: %s\n", i, s->lines[i]); + } +} + +void +usage(void) +{ + fprint(2,"usage: %s [ -c ] filename\n", argv0); + exits("usage"); +} + +void +main(int argc, char *argv[]) +{ + int fd; + Dir *d; + + ARGBEGIN{ + case 'c': + checkonly = 1; + break; + default: + usage(); + }ARGEND + + if(argc != 1) + usage(); + + file = argv[0]; + + if((fd = open(file, OREAD)) < 0) + sysfatal("%r"); + if((d = dirfstat(fd)) == nil) + sysfatal("%r"); + + contents = malloc(d->length + 2); + if(readn(fd, contents, d->length) < 0) + sysfatal("%r"); + // cursed but makes the handling nicer above + contents[d->length] = '\n'; + contents[d->length + 1] = '\0'; + free(d); + close(fd); + + checkfile(); + + if(checkonly) + exits(nil); +}