[BACK]Return to macro.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / docbook2mdoc

Diff for /docbook2mdoc/macro.c between version 1.7 and 1.15

version 1.7, 2019/04/07 15:06:56 version 1.15, 2019/04/23 21:09:42
Line 30 
Line 30 
 void  void
 macro_open(struct format *f, const char *name)  macro_open(struct format *f, const char *name)
 {  {
           if (f->parastate == PARA_WANT) {
                   if (f->linestate != LINE_NEW) {
                           putchar('\n');
                           f->linestate = LINE_NEW;
                   }
                   puts(".Pp");
           }
         switch (f->linestate) {          switch (f->linestate) {
           case LINE_MACRO:
                   if (f->flags & FMT_NOSPC) {
                           fputs(" Ns ", stdout);
                           break;
                   }
                   if (f->flags & (FMT_CHILD | FMT_IMPL)) {
                           putchar(' ');
                           break;
                   }
                   /* FALLTHROUGH */
         case LINE_TEXT:          case LINE_TEXT:
                 putchar('\n');                  putchar('\n');
                 /* FALLTHROUGH */                  /* FALLTHROUGH */
         case LINE_NEW:          case LINE_NEW:
                 putchar('.');                  putchar('.');
                 f->linestate = LINE_MACRO;                  f->linestate = LINE_MACRO;
                   f->flags = 0;
                 break;                  break;
         case LINE_MACRO:  
                 putchar(' ');  
                 if (f->spc == 0)  
                         fputs("Ns ", stdout);  
                 break;  
         }          }
         fputs(name, stdout);          fputs(name, stdout);
           f->flags &= FMT_IMPL;
           f->flags |= FMT_ARG;
           f->parastate = PARA_MID;
 }  }
   
 void  void
 macro_close(struct format *f)  macro_close(struct format *f)
 {  {
         if (f->linestate == LINE_NEW)          if (f->linestate != LINE_NEW)
                 return;                  putchar('\n');
         putchar('\n');  
         f->linestate = LINE_NEW;          f->linestate = LINE_NEW;
           f->flags = 0;
 }  }
   
 void  void
Line 65  macro_line(struct format *f, const char *name)
Line 81  macro_line(struct format *f, const char *name)
 }  }
   
 /*  /*
  * At the end of a macro, decide whether the line needs to remain open  
  * because the next node follows without intervening whitespace;  
  * otherwise, close the line.  
  */  
 void  
 macro_closepunct(struct format *f, struct pnode *pn)  
 {  
         char            *cp;  
   
         if ((pn = TAILQ_NEXT(pn, child)) != NULL && pn->spc == 0) {  
   
                 /*  
                  * If a non-text node follows without intervening  
                  * whitespace, the handler of that node will decide  
                  * 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);  
 }  
   
 /*  
  * Print an argument string on a macro line, collapsing whitespace.   * Print an argument string on a macro line, collapsing whitespace.
  */   */
 void  void
 macro_addarg(struct format *f, const char *arg, int flags)  macro_addarg(struct format *f, const char *arg, int flags)
 {  {
         const char      *cp;          const char      *cp;
           int              quote_now;
   
         assert(f->linestate == LINE_MACRO);          assert(f->linestate == LINE_MACRO);
   
         /* Quote if requested and necessary. */          /* Quote if requested and necessary. */
   
           quote_now = 0;
         if ((flags & (ARG_SINGLE | ARG_QUOTED)) == ARG_SINGLE) {          if ((flags & (ARG_SINGLE | ARG_QUOTED)) == ARG_SINGLE) {
                 for (cp = arg; *cp != '\0'; cp++)                  for (cp = arg; *cp != '\0'; cp++)
                         if (isspace((unsigned char)*cp))                          if (isspace((unsigned char)*cp))
Line 154  macro_addarg(struct format *f, const char *arg, int fl
Line 105  macro_addarg(struct format *f, const char *arg, int fl
                         }                          }
                         putchar('"');                          putchar('"');
                         flags = ARG_QUOTED;                          flags = ARG_QUOTED;
                           quote_now = 1;
                 }                  }
         }          }
   
Line 171  macro_addarg(struct format *f, const char *arg, int fl
Line 123  macro_addarg(struct format *f, const char *arg, int fl
   
                 /* Escape us if we look like a macro. */                  /* Escape us if we look like a macro. */
   
                 if ((flags & ARG_QUOTED) == 0 &&                  if ((flags & (ARG_QUOTED | ARG_UPPER)) == 0 &&
                     (cp == arg || isspace((unsigned char)cp[-1])) &&                      (cp == arg || isspace((unsigned char)cp[-1])) &&
                     isupper((unsigned char)cp[0]) &&                      isupper((unsigned char)cp[0]) &&
                     islower((unsigned char)cp[1]) &&                      islower((unsigned char)cp[1]) &&
                     (cp[2] == '\0' || cp[2] == ' ' ||                      (cp[2] == '\0' || cp[2] == ' ' ||
                      (islower((unsigned char)cp[2]) &&                       ((cp[3] == '\0' || cp[3] == ' ') &&
                       (cp[3] == '\0' || cp[3] == ' '))))                        (strncmp(cp, "Brq", 3) == 0 ||
                          strncmp(cp, "Bro", 3) == 0 ||
                          strncmp(cp, "Brc", 3) == 0 ||
                          strncmp(cp, "Bsx", 3) == 0))))
                         fputs("\\&", stdout);                          fputs("\\&", stdout);
   
                 if (*cp == '"')                  if (*cp == '"')
Line 189  macro_addarg(struct format *f, const char *arg, int fl
Line 144  macro_addarg(struct format *f, const char *arg, int fl
                 if (*cp == '\\')                  if (*cp == '\\')
                         putchar('e');                          putchar('e');
         }          }
           if (quote_now)
                   putchar('"');
           f->parastate = PARA_MID;
 }  }
   
 void  void
Line 203  macro_argline(struct format *f, const char *name, cons
Line 161  macro_argline(struct format *f, const char *name, cons
  * Recursively append text from the children of a node to a macro line.   * Recursively append text from the children of a node to a macro line.
  */   */
 void  void
 macro_addnode(struct format *f, struct pnode *pn, int flags)  macro_addnode(struct format *f, struct pnode *n, int flags)
 {  {
         struct pnode    *nc;          struct pnode    *nc;
         int              quote_now;          int              is_text, quote_now;
   
         assert(f->linestate == LINE_MACRO);          assert(f->linestate == LINE_MACRO);
   
Line 215  macro_addnode(struct format *f, struct pnode *pn, int 
Line 173  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.
          */           */
   
         while ((nc = TAILQ_FIRST(&pn->childq)) != NULL &&          while ((nc = TAILQ_FIRST(&n->childq)) != NULL &&
             TAILQ_NEXT(nc, child) == NULL)              TAILQ_NEXT(nc, child) == NULL)
                 pn = nc;                  n = nc;
   
         if (pn->node == NODE_TEXT || pn->node == NODE_ESCAPE) {          if (n->node == NODE_TEXT || n->node == NODE_ESCAPE) {
                 macro_addarg(f, pn->b, flags);                  macro_addarg(f, n->b, flags);
                   f->parastate = PARA_MID;
                 return;                  return;
         }          }
   
Line 250  macro_addnode(struct format *f, struct pnode *pn, int 
Line 209  macro_addnode(struct format *f, struct pnode *pn, int 
   
         while (nc != NULL) {          while (nc != NULL) {
                 macro_addnode(f, nc, flags);                  macro_addnode(f, nc, flags);
                   is_text = pnode_class(nc->node) == CLASS_TEXT;
                 nc = TAILQ_NEXT(nc, child);                  nc = TAILQ_NEXT(nc, child);
                 flags |= ARG_SPACE;                  if (nc == NULL || pnode_class(nc->node) != CLASS_TEXT)
                           is_text = 0;
                   if (is_text && !nc->spc)
                           flags &= ~ARG_SPACE;
                   else
                           flags |= ARG_SPACE;
         }          }
         if (quote_now)          if (quote_now)
                 putchar('"');                  putchar('"');
           f->parastate = PARA_MID;
 }  }
   
 void  void
 macro_nodeline(struct format *f, const char *name, struct pnode *pn, int flags)  macro_nodeline(struct format *f, const char *name, struct pnode *n, int flags)
 {  {
         macro_open(f, name);          macro_open(f, name);
         macro_addnode(f, pn, ARG_SPACE | flags);          macro_addnode(f, n, ARG_SPACE | flags);
         macro_close(f);          macro_close(f);
 }  }
   
Line 271  macro_nodeline(struct format *f, const char *name, str
Line 237  macro_nodeline(struct format *f, const char *name, str
  * line otherwise.  The flag ARG_SPACE inserts spaces between words.   * line otherwise.  The flag ARG_SPACE inserts spaces between words.
  */   */
 void  void
 print_text(struct format *f, const char *word, int flags) {  print_text(struct format *f, const char *word, int flags)
   {
           if (f->parastate == PARA_WANT) {
                   if (f->linestate != LINE_NEW) {
                           putchar('\n');
                           f->linestate = LINE_NEW;
                   }
                   puts(".Pp");
           }
         switch (f->linestate) {          switch (f->linestate) {
         case LINE_NEW:          case LINE_NEW:
                 break;                  break;
Line 283  print_text(struct format *f, const char *word, int fla
Line 257  print_text(struct format *f, const char *word, int fla
                 macro_close(f);                  macro_close(f);
                 break;                  break;
         }          }
         fputs(word, stdout);          if (f->linestate == LINE_NEW && (*word == '.' || *word == '\''))
                   fputs("\\&", stdout);
           while (*word != '\0') {
                   putchar(*word);
                   if (*word++ == '\\')
                           putchar('e');
           }
         f->linestate = LINE_TEXT;          f->linestate = LINE_TEXT;
           f->parastate = PARA_MID;
           f->flags = 0;
 }  }
   
 /*  /*

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.15

CVSweb