/*
* "$Id: rfctohtml.c 6649 2007-07-11 21:46:42Z mike $"
*
* RFC file to HTML conversion program.
*
* Copyright 2007-2009 by Apple Inc.
* Copyright 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/".
*
* Contents:
*
* main() - Convert a man page to HTML.
* put_entity() - Put a single character, using entities as needed.
* put_line() - Put a whole string for a line.
*/
/*
* Include necessary headers.
*/
#include
#include
#include
/*
* Local functions...
*/
void put_entity(cups_file_t *fp, int ch);
void put_line(cups_file_t *fp, const char *line);
/*
* 'main()' - Convert a man page to HTML.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line args */
char *argv[]) /* I - Command-line arguments */
{
cups_file_t *infile, /* Input file */
*outfile; /* Output file */
char line[1024], /* Line from file */
*lineptr, /* Pointer into line */
title[2048], /* Title string */
*titleptr, /* Pointer into title */
name[1024], /* Heading anchor name */
*nameptr; /* Pointer into anchor name */
int rfc, /* RFC # */
inheading, /* Inside a heading? */
inpre, /* Inside preformatted text? */
intoc, /* Inside table-of-contents? */
toclevel, /* Current table-of-contents level */
linenum; /* Current line on page */
/*
* Check arguments...
*/
if (argc > 3)
{
fputs("Usage: rfctohtml [rfcNNNN.txt [rfcNNNN.html]]\n", stderr);
return (1);
}
/*
* Open files as needed...
*/
if (argc > 1)
{
if ((infile = cupsFileOpen(argv[1], "r")) == NULL)
{
perror(argv[1]);
return (1);
}
}
else
infile = cupsFileOpenFd(0, "r");
if (argc > 2)
{
if ((outfile = cupsFileOpen(argv[2], "w")) == NULL)
{
perror(argv[2]);
cupsFileClose(infile);
return (1);
}
}
else
outfile = cupsFileOpenFd(1, "w");
/*
* Read from input and write the output...
*/
cupsFilePuts(outfile,
"\n"
"\n"
"\n"
"\n"
"\t\n");
/*
* Skip the initial header stuff (working group ID, RFC #, authors, and
* copyright...
*/
linenum = 0;
rfc = 0;
while (cupsFileGets(infile, line, sizeof(line)))
{
linenum ++;
if (line[0])
break;
}
while (cupsFileGets(infile, line, sizeof(line)))
{
linenum ++;
if (!line[0])
break;
else if (!strncasecmp(line, "Request for Comments:", 21))
rfc = atoi(line + 21);
}
/*
* Read the document title...
*/
while (cupsFileGets(infile, line, sizeof(line)))
{
linenum ++;
if (line[0])
break;
}
for (lineptr = line; isspace(*lineptr & 255); lineptr ++);
snprintf(title, sizeof(title), "RFC %d: %s", rfc, lineptr);
titleptr = title + strlen(title);
while (cupsFileGets(infile, line, sizeof(line)))
{
linenum ++;
if (!line[0])
break;
else
{
for (lineptr = line; isspace(*lineptr & 255); lineptr ++);
snprintf(titleptr, sizeof(title) - (titleptr - title), " %s", lineptr);
titleptr += strlen(titleptr);
}
}
cupsFilePrintf(outfile, "\t%s\n"
"\n"
"\n"
"
%s
\n", title, title);
/*
* Read the rest of the file...
*/
inheading = 0;
inpre = 0;
intoc = 0;
toclevel = 0;
while (cupsFileGets(infile, line, sizeof(line)))
{
linenum ++;
if (!line[0])
{
if (linenum > 50)
continue;
if (inpre)
{
cupsFilePuts(outfile, "\n");
inpre = 0;
}
if (inheading)
{
if (inheading < 0)
cupsFilePuts(outfile, "\n");
else
cupsFilePrintf(outfile, "\n", inheading);
inheading = 0;
}
}
else if ((line[0] == ' ' ||
(!isupper(line[0] & 255) && !isdigit(line[0] & 255) &&
!strstr(line, "[Page "))) && !inheading)
{
if (inheading)
{
if (inheading < 0)
cupsFilePuts(outfile, "\n");
else
cupsFilePrintf(outfile, "\n", inheading);
inheading = 0;
}
for (lineptr = line; *lineptr == ' '; lineptr ++);
if (intoc)
{
char *temp; /* Temporary pointer into line */
int level; /* Heading level */
if (isdigit(*lineptr & 255))
{
strlcpy(name, lineptr, sizeof(name));
for (nameptr = name, level = -1; *nameptr;)
if (isdigit(*nameptr & 255))
{
while (isdigit(*nameptr & 255))
nameptr ++;
level ++;
}
else if (*nameptr == ' ')
{
*nameptr = '\0';
break;
}
else
nameptr ++;
while (toclevel > level)
{
cupsFilePuts(outfile, "\n");
toclevel --;
}
while (toclevel < level)
{
cupsFilePuts(outfile, "\n