Print this page
Update to 1.12.3.

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/mandoc/html.c
          +++ new/usr/src/cmd/mandoc/html.c
   1      -/*      $Id: html.c,v 1.150 2011/10/05 21:35:17 kristaps Exp $ */
        1 +/*      $Id: html.c,v 1.152 2013/08/08 20:07:47 schwarze Exp $ */
   2    2  /*
   3    3   * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
   4      - * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
        4 + * Copyright (c) 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
   5    5   *
   6    6   * Permission to use, copy, modify, and distribute this software for any
   7    7   * purpose with or without fee is hereby granted, provided that the above
   8    8   * copyright notice and this permission notice appear in all copies.
   9    9   *
  10   10   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11   11   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12   12   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13   13   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14   14   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
↓ open down ↓ 213 lines elided ↑ open up ↑
 228  228          switch (deco) {
 229  229          case (ESCAPE_FONTPREV):
 230  230                  font = h->metal;
 231  231                  break;
 232  232          case (ESCAPE_FONTITALIC):
 233  233                  font = HTMLFONT_ITALIC;
 234  234                  break;
 235  235          case (ESCAPE_FONTBOLD):
 236  236                  font = HTMLFONT_BOLD;
 237  237                  break;
      238 +        case (ESCAPE_FONTBI):
      239 +                font = HTMLFONT_BI;
      240 +                break;
 238  241          case (ESCAPE_FONT):
 239  242                  /* FALLTHROUGH */
 240  243          case (ESCAPE_FONTROMAN):
 241  244                  font = HTMLFONT_NONE;
 242  245                  break;
 243  246          default:
 244  247                  abort();
 245  248                  /* NOTREACHED */
 246  249          }
 247  250  
 248  251          if (h->metaf) {
 249  252                  print_tagq(h, h->metaf);
 250  253                  h->metaf = NULL;
 251  254          }
 252  255  
 253  256          h->metal = h->metac;
 254  257          h->metac = font;
 255  258  
 256      -        if (HTMLFONT_NONE != font)
 257      -                h->metaf = HTMLFONT_BOLD == font ?
 258      -                        print_otag(h, TAG_B, 0, NULL) :
 259      -                        print_otag(h, TAG_I, 0, NULL);
      259 +        switch (font) {
      260 +        case (HTMLFONT_ITALIC):
      261 +                h->metaf = print_otag(h, TAG_I, 0, NULL);
      262 +                break;
      263 +        case (HTMLFONT_BOLD):
      264 +                h->metaf = print_otag(h, TAG_B, 0, NULL);
      265 +                break;
      266 +        case (HTMLFONT_BI):
      267 +                h->metaf = print_otag(h, TAG_B, 0, NULL);
      268 +                print_otag(h, TAG_I, 0, NULL);
      269 +                break;
      270 +        default:
      271 +                break;
      272 +        }
 260  273  }
 261  274  
 262  275  int
 263  276  html_strlen(const char *cp)
 264  277  {
 265      -        int              ssz, sz;
 266      -        const char      *seq, *p;
      278 +        size_t           rsz;
      279 +        int              skip, sz;
 267  280  
 268  281          /*
 269  282           * Account for escaped sequences within string length
 270  283           * calculations.  This follows the logic in term_strlen() as we
 271  284           * must calculate the width of produced strings.
 272  285           * Assume that characters are always width of "1".  This is
 273  286           * hacky, but it gets the job done for approximation of widths.
 274  287           */
 275  288  
 276  289          sz = 0;
 277      -        while (NULL != (p = strchr(cp, '\\'))) {
 278      -                sz += (int)(p - cp);
 279      -                ++cp;
 280      -                switch (mandoc_escape(&cp, &seq, &ssz)) {
      290 +        skip = 0;
      291 +        while (1) {
      292 +                rsz = strcspn(cp, "\\");
      293 +                if (rsz) {
      294 +                        cp += rsz;
      295 +                        if (skip) {
      296 +                                skip = 0;
      297 +                                rsz--;
      298 +                        }
      299 +                        sz += rsz;
      300 +                }
      301 +                if ('\0' == *cp)
      302 +                        break;
      303 +                cp++;
      304 +                switch (mandoc_escape(&cp, NULL, NULL)) {
 281  305                  case (ESCAPE_ERROR):
 282  306                          return(sz);
 283  307                  case (ESCAPE_UNICODE):
 284  308                          /* FALLTHROUGH */
 285  309                  case (ESCAPE_NUMBERED):
 286  310                          /* FALLTHROUGH */
 287  311                  case (ESCAPE_SPECIAL):
 288      -                        sz++;
      312 +                        if (skip)
      313 +                                skip = 0;
      314 +                        else
      315 +                                sz++;
 289  316                          break;
      317 +                case (ESCAPE_SKIPCHAR):
      318 +                        skip = 1;
      319 +                        break;
 290  320                  default:
 291  321                          break;
 292  322                  }
 293  323          }
 294      -
 295      -        assert(sz >= 0);
 296      -        return(sz + strlen(cp));
      324 +        return(sz);
 297  325  }
 298  326  
 299  327  static int
 300  328  print_encode(struct html *h, const char *p, int norecurse)
 301  329  {
 302  330          size_t           sz;
 303  331          int              c, len, nospace;
 304  332          const char      *seq;
 305  333          enum mandoc_esc  esc;
 306  334          static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
 307  335  
 308  336          nospace = 0;
 309  337  
 310  338          while ('\0' != *p) {
      339 +                if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
      340 +                        h->flags &= ~HTML_SKIPCHAR;
      341 +                        p++;
      342 +                        continue;
      343 +                }
      344 +
 311  345                  sz = strcspn(p, rejs);
 312  346  
 313  347                  fwrite(p, 1, sz, stdout);
 314  348                  p += (int)sz;
 315  349  
 316  350                  if ('\0' == *p)
 317  351                          break;
 318  352  
 319  353                  switch (*p++) {
 320  354                  case ('<'):
↓ open down ↓ 10 lines elided ↑ open up ↑
 331  365                          continue;
 332  366                  default:
 333  367                          break;
 334  368                  }
 335  369  
 336  370                  esc = mandoc_escape(&p, &seq, &len);
 337  371                  if (ESCAPE_ERROR == esc)
 338  372                          break;
 339  373  
 340  374                  switch (esc) {
      375 +                case (ESCAPE_FONT):
      376 +                        /* FALLTHROUGH */
      377 +                case (ESCAPE_FONTPREV):
      378 +                        /* FALLTHROUGH */
      379 +                case (ESCAPE_FONTBOLD):
      380 +                        /* FALLTHROUGH */
      381 +                case (ESCAPE_FONTITALIC):
      382 +                        /* FALLTHROUGH */
      383 +                case (ESCAPE_FONTBI):
      384 +                        /* FALLTHROUGH */
      385 +                case (ESCAPE_FONTROMAN):
      386 +                        if (0 == norecurse)
      387 +                                print_metaf(h, esc);
      388 +                        continue;
      389 +                case (ESCAPE_SKIPCHAR):
      390 +                        h->flags |= HTML_SKIPCHAR;
      391 +                        continue;
      392 +                default:
      393 +                        break;
      394 +                }
      395 +
      396 +                if (h->flags & HTML_SKIPCHAR) {
      397 +                        h->flags &= ~HTML_SKIPCHAR;
      398 +                        continue;
      399 +                }
      400 +
      401 +                switch (esc) {
 341  402                  case (ESCAPE_UNICODE):
 342  403                          /* Skip passed "u" header. */
 343  404                          c = mchars_num2uc(seq + 1, len - 1);
 344  405                          if ('\0' != c)
 345  406                                  printf("&#x%x;", c);
 346  407                          break;
 347  408                  case (ESCAPE_NUMBERED):
 348  409                          c = mchars_num2char(seq, len);
 349  410                          if ('\0' != c)
 350  411                                  putchar(c);
 351  412                          break;
 352  413                  case (ESCAPE_SPECIAL):
 353  414                          c = mchars_spec2cp(h->symtab, seq, len);
 354  415                          if (c > 0)
 355  416                                  printf("&#%d;", c);
 356  417                          else if (-1 == c && 1 == len)
 357  418                                  putchar((int)*seq);
 358  419                          break;
 359      -                case (ESCAPE_FONT):
 360      -                        /* FALLTHROUGH */
 361      -                case (ESCAPE_FONTPREV):
 362      -                        /* FALLTHROUGH */
 363      -                case (ESCAPE_FONTBOLD):
 364      -                        /* FALLTHROUGH */
 365      -                case (ESCAPE_FONTITALIC):
 366      -                        /* FALLTHROUGH */
 367      -                case (ESCAPE_FONTROMAN):
 368      -                        if (norecurse)
 369      -                                break;
 370      -                        print_metaf(h, esc);
 371      -                        break;
 372  420                  case (ESCAPE_NOSPACE):
 373  421                          if ('\0' == *p)
 374  422                                  nospace = 1;
 375  423                          break;
 376  424                  default:
 377  425                          break;
 378  426                  }
 379  427          }
 380  428  
 381  429          return(nospace);
↓ open down ↓ 122 lines elided ↑ open up ↑
 504  552                  /* Manage keeps! */
 505  553                  if ( ! (HTML_KEEP & h->flags)) {
 506  554                          if (HTML_PREKEEP & h->flags)
 507  555                                  h->flags |= HTML_KEEP;
 508  556                          putchar(' ');
 509  557                  } else
 510  558                          printf("&#160;");
 511  559          }
 512  560  
 513  561          assert(NULL == h->metaf);
 514      -        if (HTMLFONT_NONE != h->metac)
 515      -                h->metaf = HTMLFONT_BOLD == h->metac ?
 516      -                        print_otag(h, TAG_B, 0, NULL) :
 517      -                        print_otag(h, TAG_I, 0, NULL);
      562 +        switch (h->metac) {
      563 +        case (HTMLFONT_ITALIC):
      564 +                h->metaf = print_otag(h, TAG_I, 0, NULL);
      565 +                break;
      566 +        case (HTMLFONT_BOLD):
      567 +                h->metaf = print_otag(h, TAG_B, 0, NULL);
      568 +                break;
      569 +        case (HTMLFONT_BI):
      570 +                h->metaf = print_otag(h, TAG_B, 0, NULL);
      571 +                print_otag(h, TAG_I, 0, NULL);
      572 +                break;
      573 +        default:
      574 +                break;
      575 +        }
 518  576  
 519  577          assert(word);
 520  578          if ( ! print_encode(h, word, 0)) {
 521  579                  if ( ! (h->flags & HTML_NONOSPACE))
 522  580                          h->flags &= ~HTML_NOSPACE;
 523  581          } else
 524  582                  h->flags |= HTML_NOSPACE;
 525  583  
 526  584          if (h->metaf) {
 527  585                  print_tagq(h, h->metaf);
↓ open down ↓ 172 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX