aboutsummaryrefslogtreecommitdiff
path: root/badroff.c
diff options
context:
space:
mode:
Diffstat (limited to 'badroff.c')
-rw-r--r--badroff.c41
1 files changed, 38 insertions, 3 deletions
diff --git a/badroff.c b/badroff.c
index adf3e41..ed84760 100644
--- a/badroff.c
+++ b/badroff.c
@@ -15,6 +15,12 @@ FILE* getfile(int argc, char** argv){
return stdin;
}
+char* chrmult(char c, size_t ct){
+ char* str = malloc(sizeof(char)*(ct+1)); str[ct] = 0;
+ memset(str, c, ct);
+ return str;
+}
+
// file read & buffer fill helpers
FILE* in;
@@ -48,9 +54,9 @@ char* center(char* txt){
int max = width/2 + len/2; // excluding terminator
int min = max-len+1; // first index of *txt
char* str = malloc(sizeof(char)*(max + 1));
- str[max] = 0;
- for (int i=0; i < min; i++) str[i] = ' ';
+ memset(str, ' ', min);
memcpy(str+min, txt, len);
+ str[max] = 0;
return str;
}
char* setwidth(char* txt){
@@ -80,8 +86,37 @@ char* cmd(void){
// normal typesetting
+char* cleanup(char* line){ // cleans up trailing whitespace && ^\n$
+ for (ws = len-1; line[ws] == ' '; ws--, len--); // rethink
+ if (ws < len-1){
+ line[ws] = '\n'
+ line[ws+1] = '\0';
+ } else if (line[len-1] != '\n'){
+ len++;
+ line = realloc(line, len);
+ }
+ if (line[len-1] != '\n'){
+ line[len-1] = '\n';
+ line[len] = '\0';
+ }
+ return line;
+}
+
+char* fold(char* line){ // inserts \n to limit line length
+
+}
+
char* typeset(void){
- return popstrbuf(inbuf, chrfill('\n'));
+ size_t len; size_t nl;
+ for (nl = 0; (len = chrfill('\n')) == 1; nl++)
+ popstrbuf(inbuf, len);
+ if (nl) return chrmult('\n', nl+1);
+ char* line = popstrbuf(inbuf, len);
+ size_t ws;
+ line = cleanup(line);
+ if (brk && line[len-1] == '\n') line[len-1] = ' ';
+ if (brk) for (; *line == ' '; line++); //remove preceding whitespace
+ return fold(line);
}
// a parser to choose when to typeset and when to run a command