version 1.3, 2019/04/02 15:53:02 |
version 1.6, 2019/04/06 22:37:57 |
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <stdio.h> |
#include <stdio.h> |
|
#include <string.h> |
|
|
#include "node.h" |
#include "node.h" |
#include "macro.h" |
#include "macro.h" |
Line 39 macro_open(struct format *f, const char *name) |
|
Line 40 macro_open(struct format *f, const char *name) |
|
break; |
break; |
case LINE_MACRO: |
case LINE_MACRO: |
putchar(' '); |
putchar(' '); |
|
if (f->spc == 0) |
|
fputs("Ns ", stdout); |
break; |
break; |
} |
} |
fputs(name, stdout); |
fputs(name, stdout); |
Line 47 macro_open(struct format *f, const char *name) |
|
Line 50 macro_open(struct format *f, const char *name) |
|
void |
void |
macro_close(struct format *f) |
macro_close(struct format *f) |
{ |
{ |
assert(f->linestate == LINE_MACRO); |
if (f->linestate == LINE_NEW) |
|
return; |
putchar('\n'); |
putchar('\n'); |
f->linestate = LINE_NEW; |
f->linestate = LINE_NEW; |
} |
} |
Line 60 macro_line(struct format *f, const char *name) |
|
Line 64 macro_line(struct format *f, const char *name) |
|
} |
} |
|
|
/* |
/* |
* If the next node is a text node starting with closing punctuation, |
* At the end of a macro, decide whether the line needs to remain open |
* emit the closing punctuation as a trailing macro argument. |
* because the next node follows without intervening whitespace; |
|
* otherwise, close the line. |
*/ |
*/ |
void |
void |
macro_closepunct(struct format *f, struct pnode *pn) |
macro_closepunct(struct format *f, struct pnode *pn) |
{ |
{ |
if ((pn = TAILQ_NEXT(pn, child)) != NULL && |
char *cp; |
pn->node == NODE_TEXT && pn->bsz > 0 && |
|
(pn->b[0] == ',' || pn->b[0] == '.') && |
if ((pn = TAILQ_NEXT(pn, child)) != NULL && pn->spc == 0) { |
(pn->bsz == 1 || isspace((unsigned char)pn->b[1]))) { |
|
putchar(' '); |
/* |
putchar(pn->b[0]); |
* If a non-text node follows without intervening |
pn->b++; |
* whitespace, the handler of that node will decide |
pn->bsz--; |
* whether and how to suppress whitespace. To allow |
|
* that, the macro line needs to remain open. |
|
*/ |
|
|
|
if (pn->node != NODE_TEXT && pn->node != NODE_ESCAPE) |
|
return; |
|
|
|
/* |
|
* Give closing punctuation |
|
* in the form of trailing macro arguments. |
|
*/ |
|
|
|
while (*pn->b != '\0' && |
|
strchr("!),.:;?]", *pn->b) != NULL) { |
|
putchar(' '); |
|
putchar(*pn->b); |
|
pn->b++; |
|
pn->bsz--; |
|
} |
|
|
|
/* |
|
* Text follows without intervening whitespace. |
|
* Append the first word with .Ns. |
|
*/ |
|
|
|
if (*pn->b != '\0' && isspace((unsigned char)*pn->b) == 0) { |
|
fputs(" Ns", stdout); |
|
for (cp = pn->b; *cp != '\0'; cp++) |
|
if (isspace((unsigned char)*cp)) |
|
break; |
|
*cp = '\0'; |
|
macro_addarg(f, pn->b, ARG_SPACE); |
|
pn->bsz -= cp - pn->b; |
|
pn->b = cp; |
|
if (pn->bsz > 0) { |
|
pn->b++; |
|
pn->bsz--; |
|
pn->spc = 1; |
|
} |
|
} |
|
|
|
/* Skip whitespace after the first word. */ |
|
|
|
while (isspace((unsigned char)*pn->b)) { |
|
pn->b++; |
|
pn->bsz--; |
|
pn->spc = 1; |
|
} |
} |
} |
macro_close(f); |
macro_close(f); |
} |
} |
Line 152 macro_argline(struct format *f, const char *name, cons |
|
Line 204 macro_argline(struct format *f, const char *name, cons |
|
void |
void |
macro_addnode(struct format *f, struct pnode *pn, int flags) |
macro_addnode(struct format *f, struct pnode *pn, int flags) |
{ |
{ |
|
struct pnode *nc; |
int quote_now; |
int quote_now; |
|
|
assert(f->linestate == LINE_MACRO); |
assert(f->linestate == LINE_MACRO); |
Line 161 macro_addnode(struct format *f, struct pnode *pn, int |
|
Line 214 macro_addnode(struct format *f, struct pnode *pn, int |
|
* that text, letting macro_addarg() decide about quoting. |
* that text, letting macro_addarg() decide about quoting. |
*/ |
*/ |
|
|
if (pn->node == NODE_TEXT || pn->node == NODE_ESCAPE || |
while ((nc = TAILQ_FIRST(&pn->childq)) != NULL && |
((pn = TAILQ_FIRST(&pn->childq)) != NULL && |
TAILQ_NEXT(nc, child) == NULL) |
(pn->node == NODE_TEXT || pn->node == NODE_ESCAPE) && |
pn = nc; |
TAILQ_NEXT(pn, child) == NULL)) { |
|
|
if (pn->node == NODE_TEXT || pn->node == NODE_ESCAPE) { |
macro_addarg(f, pn->b, flags); |
macro_addarg(f, pn->b, flags); |
return; |
return; |
} |
} |
Line 193 macro_addnode(struct format *f, struct pnode *pn, int |
|
Line 247 macro_addnode(struct format *f, struct pnode *pn, int |
|
* inserting whitespace between nodes. |
* inserting whitespace between nodes. |
*/ |
*/ |
|
|
while (pn != NULL) { |
while (nc != NULL) { |
macro_addnode(f, pn, flags); |
macro_addnode(f, nc, flags); |
pn = TAILQ_NEXT(pn, child); |
nc = TAILQ_NEXT(nc, child); |
flags |= ARG_SPACE; |
flags |= ARG_SPACE; |
} |
} |
if (quote_now) |
if (quote_now) |