From 0673f87892e799437f25b5ba304073a15f5143f7 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 15 Jun 2020 14:29:30 -0400 Subject: added indentation mode --- badroff.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/badroff.c b/badroff.c index 6f767bb..2f66b29 100644 --- a/badroff.c +++ b/badroff.c @@ -271,12 +271,21 @@ static char* nomrg(char* txt){ join = NULL; return NULL; } +int indlevel = 0; +static char* indent(char* txt){ + int orig = indlevel; + if (isdigit(*txt)) sscanf(txt, "%d", &indlevel); else indlevel = 0; + // REPEATED + width += orig-indlevel; + return NULL; +} static char* cmds[] = // MUST be sorted alphabetically - {"CT", "EG", "FIL", "LD", "LS", "MRG", "NBSP", "NMRG", "V", "W"}; + {"CT", "EG", "FIL", "IND", "LD", "LS", "MRG", "NBSP", "NMRG", "V", + "W"}; static char* (*call[])(char* txt) = - {center, fingroup, fillline, leader, lineset, merge, nbrkspc, nomrg, - vert, setwidth}; + {center, fingroup, fillline, indent, leader, lineset, merge, + nbrkspc, nomrg, vert, setwidth}; static char* cmd(void){ char* dat = norm(); @@ -329,9 +338,23 @@ static void nbspclean(char* txt){ } } +static char* wsadd(char* txt, int lnct) { + int len = strlen(txt) + lnct*indlevel; + txt = realloc(txt, sizeof(char)*(len+1)); + char* end = txt + len; + char *nl, *last; nl = last = txt; + while ( (nl = memchr(nl, '\n', end-nl))[1] ) { + memmove(last+indlevel, last, nl-last); + memset(last, ' ', indlevel); + last = nl+1; + } + return txt; +} + char* wordset(char* txt){ char* orig = txt; // txt will be manipulated int ws = 0; // zero means do not cut + int ln = 0; for (int i=0; txt[i] != 0; i++){ // turn into a nested loop if (txt[i] == ' ' || txt[i] == '\n') ws = i; // nl's (multiline input) and spaces are proper linebreaks @@ -340,9 +363,11 @@ char* wordset(char* txt){ txt[0] = '\n'; ws = 0; i = 0; + ln++; } } + orig = wsadd(orig, ln); wsclean(orig); // remove trailing whitespace nbspclean(orig); @@ -401,7 +426,8 @@ int main(int argc, char** argv){ } char* out; while ( (out = line())[0] != '\0'){ - printf("%s",wordset(out)); + out = wordset(out); + printf("%s",out); free(out); } fclose(in); -- cgit