aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--badroff.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/badroff.c b/badroff.c
index 3e2bd12..7dff4f1 100644
--- a/badroff.c
+++ b/badroff.c
@@ -64,7 +64,6 @@ size_t chrnfill(char chr, size_t sz){//fills to first of chr or sz
// typesetting config and commands to change.
int width = 80;
-bool brk = false; // not implemented
char* center(char* txt){
size_t len = strlen(txt);
@@ -81,9 +80,15 @@ char* setwidth(char* txt){
sscanf(txt, "%d", &width);
return NULL;
}
+char* fillline(char* txt){
+ char* str = malloc(sizeof(char)*(width+2));
+ str[width] = '\n'; str[width+1] = 0;
+ memset(str, txt[0], width);
+ return str;
+}
-char* cmds[] = {"CT ", "W "};
-char* (*call[])(char* txt) = {center, setwidth};
+char* cmds[] = {"CT ", "FIL ", "W "}; // MUST be sorted alphabetically
+char* (*call[])(char* txt) = {center, fillline, setwidth};
char* cmd(void){
char* dat = popstrbuf(inbuf, chrfill('\n'));
@@ -118,7 +123,7 @@ size_t nextws(size_t start){//searches for \n or [^ \n]-1 in buffer
size_t i;
for (i = 0; i<len-start; i++){
if (str[i] == '\n') return start+i;
- if (str[i] != ' ') return start+i-1;
+ else if (str[i] != ' ') return start+i-1;
}
free(str);
for (int c = 0; c != '\n' && ( c = addchr() ) != EOF && c == ' ';
@@ -126,19 +131,25 @@ size_t nextws(size_t start){//searches for \n or [^ \n]-1 in buffer
return len;
}
-char* typeset(void){ // brk is completely unimplemented
+char* typeset(void){
size_t len = chrnfill('\n', width+1); //+1 for \n on 81-char lines
char* next = peekstrbuf(inbuf, 0, len);
size_t wsend = 0;
- if (next[--len] == '\n') wsend = len;
- if (next[len] == ' ') wsend = nextws(len);
- for (; next[len] == ' ' && wsend; len--){
- if (!wsend && next[len] == ' ') wsend = len;
- } // len should be last letter
+ if (next[--len] == '\n'){
+ wsend = len;
+ } else if (next[len] == ' '){
+ wsend = nextws(len);
+ }
+ else for (wsend = len; next[wsend] != ' '; wsend--);
+ // wsend is last index with a piece of whitespace
+ for (len = min(wsend,len-1); next[len] == ' '; len--);
+ // check, within the string, behind the whitespace for a letter.
+ // len should be *index of* last letter
if (peekchrbuf(inbuf, wsend) == '\n') cont_typst = false;
else cont_typst = true;
free(next);
- char* ln = popstrbuf(inbuf, wsend+1); ln[len] = '\n'; ln[len+1] = 0;
+ char* ln = popstrbuf(inbuf, wsend+1);
+ ln[len+1] = '\n'; ln[len+2] = 0;
return ln;
}