aboutsummaryrefslogtreecommitdiff
path: root/badroff.c
diff options
context:
space:
mode:
Diffstat (limited to 'badroff.c')
-rw-r--r--badroff.c40
1 files changed, 28 insertions, 12 deletions
diff --git a/badroff.c b/badroff.c
index 8cf8961..323cba2 100644
--- a/badroff.c
+++ b/badroff.c
@@ -1,6 +1,8 @@
#include <stdio.h>
#include "sb.h"
#include "buf.h"
+#include <string.h>
+#include <stdlib.h>
typedef enum {
false,
@@ -17,9 +19,8 @@ FILE* getfile(int argc, char** argv){
FILE* in;
buf* inbuf;
-int width;
-bool brk;
-
+int width = 80;
+bool brk = false; // not implemented
size_t fillbuf(size_t len){ // returns real len
int c;
@@ -29,24 +30,38 @@ size_t fillbuf(size_t len){ // returns real len
return set;
}
-size_t nlfill(){
+size_t chrfill(char chr){
size_t line;
size_t len = buflen(inbuf);
char* str = peekstrbuf(inbuf, 0, len);
for (line = 0; line < len; line++)
- if (str[line] == '\n') return line+1;
- for (int c = 0; c != '\n' && (c = fgetc(in)) != EOF; line++)
+ if (str[line] == chr) return line+1;
+ for (int c = 0; c != chr && (c = fgetc(in)) != EOF; line++)
inschrbuf(inbuf, c);
return line;
}
-void cmd(void){
- int n = nlfill();
- popstrbuf(inbuf, n);
+char* center(char* text){
+ size_t len = strlen(text);
+ int max = width/2 + len/2; // excluding terminator
+ int min = max-len+1; // first index of *text
+ char* str = malloc(sizeof(char)*(max + 1));
+ str[max] = 0;
+ for (int i=0; i < min; i++) str[i] = ' ';
+ memcpy(str+min, text, len);
+ return str;
+}
+
+char* cmd(void){
+ char* dat = popstrbuf(inbuf, chrfill('\n'));
+ if (!strncmp(dat, "CT ", 3)){
+ return center(dat+3);
+ }
+ return "";
}
char* typeset(void){
- return popstrbuf(inbuf, nlfill());
+ return popstrbuf(inbuf, chrfill('\n'));
}
char* line(void){
@@ -58,8 +73,9 @@ char* line(void){
if (twobytes[0] == '.' && twobytes[1] == '\n') return typeset();
if (twobytes[0] == '.') popchrbuf(inbuf);
if (twobytes[0] == '.' && twobytes[1] != '.'){
- cmd();
- return line();
+ char* data = cmd();
+ if (data[0] != '\0') return data;
+ else return line();
} else {
return typeset();
}