]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - man/mantohtml.c
Bump the default Timeout to 15 minutes as well (Issue #5570)
[thirdparty/cups.git] / man / mantohtml.c
index d498903b158a40075a0fb043e0cead6c17391761..2a9353a9f76500747dcae7660205ec427c0245e8 100644 (file)
@@ -1,16 +1,10 @@
 /*
- * "$Id$"
- *
  * Man page to HTML conversion program.
  *
- * Copyright 2007-2010, 2014 by Apple Inc.
+ * Copyright 2007-2017 by Apple Inc.
  * Copyright 2004-2006 by Easy Software Products.
  *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
@@ -53,12 +47,17 @@ main(int  argc,                             /* I - Number of command-line args */
                *outfile;               /* Output file */
   char         line[1024],             /* Line from file */
                *lineptr,               /* Pointer into line */
-               name[1024];             /* Man page name */
+               anchor[1024],           /* Anchor */
+               name[1024],             /* Man page name */
+               ddpost[256];            /* Tagged list post markup */
   int          section = -1,           /* Man page section */
                pre = 0,                /* Preformatted */
                font = 0,               /* Current font */
                linenum = 0;            /* Current line number */
-  const char   *list = NULL;           /* Current list, if any */
+  float                list_indent = 0.0f,     /* Current list indentation */
+               nested_indent = 0.0f;   /* Nested list indentation, if any */
+  const char   *list = NULL,           /* Current list, if any */
+               *nested = NULL;         /* Nested list, if any */
   const char   *post = NULL;           /* Text to add after the current line */
 
 
@@ -110,6 +109,8 @@ main(int  argc,                             /* I - Number of command-line args */
        "\t<link rel=\"stylesheet\" type=\"text/css\" "
        "href=\"../cups-printable.css\">\n", outfile);
 
+  anchor[0] = '\0';
+
   while (fgets(line, sizeof(line), infile))
   {
     size_t linelen = strlen(line);     /* Length of line */
@@ -172,13 +173,21 @@ main(int  argc,                           /* I - Number of command-line args */
        else
          fputs("<h3><a name=\"", outfile);
 
-        for (lineptr = line + 4; *lineptr; lineptr ++)
-         if (*lineptr  == '\"')
-           continue;
-         else if (isalnum(*lineptr & 255))
-           html_putc(*lineptr, outfile);
-         else
-           html_putc('_', outfile);
+        if (anchor[0])
+        {
+          fputs(anchor, outfile);
+          anchor[0] = '\0';
+        }
+        else
+        {
+         for (lineptr = line + 4; *lineptr; lineptr ++)
+           if (*lineptr  == '\"')
+             continue;
+           else if (isalnum(*lineptr & 255))
+             html_putc(*lineptr, outfile);
+           else
+             html_putc('_', outfile);
+        }
 
        fputs("\">", outfile);
 
@@ -217,7 +226,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 3, "b", "b", outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".I ", 3))
       {
@@ -228,7 +252,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 3, "i", "i", outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".BI ", 4))
       {
@@ -239,7 +278,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 4, "b", "i", outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".BR ", 4))
       {
@@ -250,7 +304,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 4, "b", NULL, outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".IB ", 4))
       {
@@ -261,7 +330,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 4, "i", "b", outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".IR ", 4))
       {
@@ -272,7 +356,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 4, "i", NULL, outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".RB ", 4))
       {
@@ -283,7 +382,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 4, NULL, "b", outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".RI ", 4))
       {
@@ -294,7 +408,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 4, NULL, "i", outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".SB ", 4))
       {
@@ -305,7 +434,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 4, "small", "b", outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strncmp(line, ".SM ", 4))
       {
@@ -316,7 +460,22 @@ main(int  argc,                            /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
+        if (anchor[0])
+          fprintf(outfile, "<a name=\"%s\">", anchor);
+
         html_alternate(line + 4, "small", "small", outfile);
+
+        if (anchor[0])
+        {
+          fputs("</a>", outfile);
+          anchor[0] = '\0';
+        }
+
+       if (post)
+       {
+         fputs(post, outfile);
+         post = NULL;
+       }
       }
       else if (!strcmp(line, ".LP") || !strcmp(line, ".PP") || !strcmp(line, ".P"))
       {
@@ -334,6 +493,12 @@ main(int  argc,                            /* I - Number of command-line args */
         }
 
        fputs("<p>", outfile);
+
+        if (anchor[0])
+        {
+          fprintf(outfile, "<a name=\"%s\"></a>", anchor);
+          anchor[0] = '\0';
+        }
       }
       else if (!strcmp(line, ".RS") || !strncmp(line, ".RS ", 4))
       {
@@ -344,18 +509,20 @@ main(int  argc,                           /* I - Number of command-line args */
        float amount = 3.0;             /* Indentation */
 
         if (line[3])
-          amount = atof(line + 4);
+          amount = (float)atof(line + 4);
 
        fputs(end_fonts[font], outfile);
        font = 0;
 
         if (list)
         {
-          fprintf(outfile, "</%s>\n", list);
-          list = NULL;
+          nested        = list;
+          list          = NULL;
+          nested_indent = list_indent;
+          list_indent   = 0.0f;
         }
 
-        fprintf(outfile, "<div style=\"margin-left: %.1fem;\">\n", amount);
+        fprintf(outfile, "<div style=\"margin-left: %.1fem;\">\n", amount - nested_indent);
       }
       else if (!strcmp(line, ".RE"))
       {
@@ -367,21 +534,28 @@ main(int  argc,                           /* I - Number of command-line args */
        font = 0;
 
         fputs("</div>\n", outfile);
+
+        if (nested)
+        {
+          list   = nested;
+          nested = NULL;
+
+          list_indent   = nested_indent;
+          nested_indent = 0.0f;
+        }
       }
-      else if (!strcmp(line, ".HP") || !strncmp(line, ".HP ", 4) ||
-               !strcmp(line, ".TP") || !strncmp(line, ".TP ", 4))
+      else if (!strcmp(line, ".HP") || !strncmp(line, ".HP ", 4))
       {
        /*
-        * Hanging paragraph/tagged list...
+        * Hanging paragraph...
         *
         * .HP i
-        * .TP i
        */
 
        float amount = 3.0;             /* Indentation */
 
         if (line[3])
-          amount = atof(line + 4);
+          amount = (float)atof(line + 4);
 
        fputs(end_fonts[font], outfile);
        font = 0;
@@ -394,9 +568,54 @@ main(int  argc,                            /* I - Number of command-line args */
 
         fprintf(outfile, "<p style=\"margin-left: %.1fem; text-indent: %.1fem\">", amount, -amount);
 
+        if (anchor[0])
+        {
+          fprintf(outfile, "<a name=\"%s\"></a>", anchor);
+          anchor[0] = '\0';
+        }
+
         if (line[1] == 'T')
           post = "<br>\n";
       }
+      else if (!strcmp(line, ".TP") || !strncmp(line, ".TP ", 4))
+      {
+       /*
+        * Tagged list...
+        *
+        * .TP i
+       */
+
+       float amount = 3.0;             /* Indentation */
+
+        if (line[3])
+          amount = (float)atof(line + 4);
+
+       fputs(end_fonts[font], outfile);
+       font = 0;
+
+        if (list && strcmp(list, "dl"))
+        {
+          fprintf(outfile, "</%s>\n", list);
+          list = NULL;
+        }
+
+        if (!list)
+        {
+          fputs("<dl class=\"man\">\n", outfile);
+          list        = "dl";
+          list_indent = amount;
+        }
+
+        fputs("<dt>", outfile);
+        snprintf(ddpost, sizeof(ddpost), "<dd style=\"margin-left: %.1fem\">", amount);
+       post = ddpost;
+
+        if (anchor[0])
+        {
+          fprintf(outfile, "<a name=\"%s\"></a>", anchor);
+          anchor[0] = '\0';
+        }
+      }
       else if (!strncmp(line, ".IP ", 4))
       {
        /*
@@ -457,7 +676,7 @@ main(int  argc,                             /* I - Number of command-line args */
           lineptr ++;
 
         if (isdigit(*lineptr & 255))
-          amount = atof(lineptr);
+          amount = (float)atof(lineptr);
 
         if (newlist && list && strcmp(newlist, list))
         {
@@ -479,6 +698,12 @@ main(int  argc,                            /* I - Number of command-line args */
           fprintf(outfile, "<li style=\"margin-left: %.1fem;\">", amount);
         else
           fprintf(outfile, "<p style=\"margin-left: %.1fem;\">", amount);
+
+        if (anchor[0])
+        {
+          fprintf(outfile, "<a name=\"%s\"></a>", anchor);
+          anchor[0] = '\0';
+        }
       }
       else if (!strncmp(line, ".br", 3))
       {
@@ -511,7 +736,7 @@ main(int  argc,                             /* I - Number of command-line args */
         * Ignore unused commands...
        */
       }
-      else if (!strncmp(line, ".Vb", 3) || !strncmp(line, ".nf", 3))
+      else if (!strncmp(line, ".Vb", 3) || !strncmp(line, ".nf", 3) || !strncmp(line, ".EX", 3))
       {
        /*
         * Start preformatted...
@@ -520,16 +745,16 @@ main(int  argc,                           /* I - Number of command-line args */
        fputs(end_fonts[font], outfile);
        font = 0;
 
-        if (list)
-       {
-         fprintf(outfile, "</%s>\n", list);
-         list = NULL;
-       }
+//        if (list)
+//     {
+//       fprintf(outfile, "</%s>\n", list);
+//       list = NULL;
+//     }
 
         pre = 1;
-       fputs("<pre>\n", outfile);
+       fputs("<pre class=\"man\">\n", outfile);
       }
-      else if (!strncmp(line, ".Ve", 3) || !strncmp(line, ".fi", 3))
+      else if (!strncmp(line, ".Ve", 3) || !strncmp(line, ".fi", 3) || !strncmp(line, ".EE", 3))
       {
        /*
         * End preformatted...
@@ -580,6 +805,14 @@ main(int  argc,                            /* I - Number of command-line args */
        */
       }
 #endif /* 0 */
+      else if (!strncmp(line, ".\\\"#", 4))
+      {
+       /*
+        * Anchor for HTML output...
+        */
+
+        strlcpy(anchor, line + 4, sizeof(anchor));
+      }
       else if (strncmp(line, ".\\\"", 3))
       {
        /*
@@ -735,7 +968,8 @@ html_alternate(const char *s,               /* I - String */
     {
       if (*s == '\"')
         quote = !quote;
-      else if (*s == '\\' && s[1])
+
+      if (*s == '\\' && s[1])
       {
         s ++;
         html_putc(*s++, fp);
@@ -763,9 +997,6 @@ html_alternate(const char *s,               /* I - String */
 
     while (isspace(*s & 255))
       s ++;
-
-    if (*s && *s != '(' && *s != '.' && *s != ',')
-      putc(' ', fp);
   }
 
   putc('\n', fp);
@@ -869,6 +1100,24 @@ html_fputs(const char *s,         /* I  - String */
               break;
         }
       }
+      else if (*s == '(')
+      {
+        if (!strncmp(s, "(em", 3))
+        {
+          fputs("&mdash;", fp);
+          s += 3;
+        }
+        else if (!strncmp(s, "(en", 3))
+        {
+          fputs("&ndash;", fp);
+          s += 3;
+        }
+        else
+        {
+          putc(*s, fp);
+          s ++;
+        }
+      }
       else if (*s == '[')
       {
        /*
@@ -900,8 +1149,11 @@ html_fputs(const char *s,         /* I  - String */
       }
       else
       {
-        if (*s != '\\' && *s == '\"' && *s == '\'' && *s == '-')
+        if (*s != '\\' && *s != '\"' && *s != '\'' && *s != '-')
+        {
           fprintf(stderr, "mantohtml: Unrecognized escape \"\\%c\" ignored.\n", *s);
+          html_putc('\\', fp);
+        }
 
         html_putc(*s++, fp);
       }
@@ -912,12 +1164,20 @@ html_fputs(const char *s,                /* I  - String */
       * Embed URL...
       */
 
+      char temp[1024];                 /* Temporary string */
       const char *end = s + 6;         /* End of URL */
 
       while (*end && !isspace(*end & 255))
        end ++;
 
-      fprintf(fp, "<a href=\"%*s\">%*s</a>", (int)(end - s), s, (int)(end - s), s);
+      if (end[-1] == ',' || end[-1] == '.' || end[-1] == ')')
+        end --;
+
+      strlcpy(temp, s, sizeof(temp));
+      if ((size_t)(end -s) < sizeof(temp))
+        temp[end - s] = '\0';
+
+      fprintf(fp, "<a href=\"%s\">%s</a>", temp, temp);
       s = end;
     }
     else
@@ -956,8 +1216,3 @@ strmove(char       *d,                     /* I - Destination */
 
   *d = '\0';
 }
-
-
-/*
- * End of "$Id$".
- */