Alien-Judy

 view release on metacpan or  search on metacpan

src/judy-1.0.5/tool/jhton.c  view on Meta::CPAN

// The translation is still complex enough to merit writing a parser that first
// builds a tree representation of the structured (HTML) document.  I would use
// yacc and lex if I knew them...
//
// This program is written for simplicity, cleanliness, and robustness, and not
// necessarily for speed.
//
// FLEXIBILITY:  It should be possible to teach this program new HTML tags; see
// data structures below.  The program might also be useful for other HTML
// manual entries, so long as they follow the simple conventions used in the
// Judy entries; see the comments before EmitNroffHeader().  You can also
// discover the format by trial and error -- this program issues verbose error
// messages.
//
// CONVENTIONS:
//
// - Global variables start with "g_" except option_*.
// - Global constants start with "gc_".
//
// - Pointer variable names start with one "P" per level of indirection.
//
// - Exception:  (char *) and (char[]) types, that is, strings, do not
//   necessarily start with "P".  A generic char pointer is named "Pch".
//   Variables of type (char **) start with a single P.
//
// - Exception:  the well known name "char ** argv".
//
// - Pointers to first elements of serial linked lists of structures have names
//   ending in "Phead".
//
// - Line lengths are less than 80 columns.  Message parameters to Error()
//   begin on the same line for easy finding.
//
// - Error messages might exceed one line when emitted, but no effort is made
//   to wrap them nicely.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>		// for varargs.
#include <string.h>		// for str*().
#include <errno.h>
#include <ctype.h>		// for isspace(), etc.
#include <time.h>
#include <assert.h>

#define	ISSPACE(Char) isspace((int) (Char))	// for convenience with lint.
#define	ISDIGIT(Char) isdigit((int) (Char))
#define	ISUPPER(Char) isupper((int) (Char))

#define	PUTS(String) (void) fputs(String, stdout)	// for convenience.
#define	PUTC(Char)   (void) putc((int) (Char), stdout)

#ifndef DEBUG
#define NDEBUG			// turn off assertions by default.
#endif

// Shorthand notation to avoid #ifdefs for single-line conditional statement:
//
// Warning:  This cannot be used around compiler directives, such as
// "#include", nor in the case where Code contains a comma other than nested
// within parentheses or quotes.

#ifndef DEBUG
#define	DBGCODE(Code) // null.
#else
#define	DBGCODE(Code) Code
#endif


// ****************************************************************************
// MISCELLANEOUS GLOBAL VALUES:

#define	FUNCTION			// null; easy to find functions.
#define	FALSE	0
#define	TRUE	1
#define	CHNULL	('\0')
#define	PCNULL	((char *) NULL)
typedef int bool_t;			// for clarity with Boolean values.

char * gc_usage[] = {
    "usage: %s filename.htm[l]",
    "",
    "Reads restricted (Judy-specific) HTML from filename.htm[l] and emits",
    "equivalent nroff -man to stdout.",
    PCNULL,
};

char * gc_myname;			// how program was invoked.

#define	OKEXIT	0
#define	NOEXIT	0			// values for Error().
#define	ERREXIT	1
#define	USAGE	2
#define	NOERRNO	0

// Prefix for printf formats:

#define	FILELINE "File \"%s\", line %d: "

// Common error string:

char * FmtErrLineEnds = FILELINE "Input line ends within an HTML tag; for this "
			"translator, all tags must be on a single input line";

// Macros for skipping whitespace or non-whitespace; in the latter case,
// stopping at end of line or end of tag:

#define	SKIPSPACE(Pch)	   { while (ISSPACE(*(Pch))) ++(Pch); }

#define	SKIPNONSPACE(Pch)  { while ((! ISSPACE(*(Pch))) \
				 && (*(Pch) != CHNULL)  \
				 && (*(Pch) != '>')) ++(Pch); }

// Highest line number + 1, and last input line number that caused output:

int g_linenumlim;
int g_prevlinenum = 0;

// <PRE> block equivalents in nroff need some special handling for bold font
// and for continuing a tagged paragraph; these are bit flags:

src/judy-1.0.5/tool/jhton.c  view on Meta::CPAN

};

// Regarding dnt_nest:  If an HTML tag type is marked as nesting, that means
// it is required not to be a singleton in this context; it must have a closing
// tag, and when the tree is built, the intervening text is nested as a child.
// Otherwise, intervening text is a sibling; a closing tag is allowed (whether
// or not this makes sense), but is not required; however, if present, it must
// match.

struct docnode_type {
	char *	dnt_tag;		// HTML tag.
	bool_t	dnt_savetag;		// flag: save HTML tag.
	bool_t	dnt_nest;		// flag: see comments above.
	int	dnt_type;		// corresponding number.
} g_dntype[] = {

// Note:  HTML is case-insensitive, but for expediency this program is
// case-sensitive.  Tags must be as shown below.

    { "",	FALSE, FALSE, DN_TYPE_TEXT,  },	// special, see above.

    { "HTML",	FALSE, TRUE,  DN_TYPE_HTML,  },
    { "HEAD",	FALSE, TRUE,  DN_TYPE_HEAD,  },
    { "TITLE",	FALSE, TRUE,  DN_TYPE_TITLE, },
    { "BODY",	FALSE, TRUE,  DN_TYPE_BODY,  },

    { "!--",	TRUE,  FALSE, DN_TYPE_COMM,  },	// comments are singleton tags.

    { "TABLE",	FALSE, TRUE,  DN_TYPE_TABLE, },	// limited understanding!
    { "TR",	FALSE, TRUE,  DN_TYPE_TR,    },
    { "TD",	TRUE,  TRUE,  DN_TYPE_TD,    },

    { "DL",	FALSE, TRUE,  DN_TYPE_DL,    },
    { "DT",	FALSE, TRUE,  DN_TYPE_DT,    },
    { "DD",	FALSE, FALSE, DN_TYPE_DD,    },	// </DD> not req in our manuals.

    { "A",	TRUE,  TRUE,  DN_TYPE_A,     },	// either "name" or "href" type.
    { "B",	FALSE, TRUE,  DN_TYPE_B,     },
    { "I",	FALSE, TRUE,  DN_TYPE_I,     },

    { "PRE",	FALSE, TRUE,  DN_TYPE_PRE,   },

    { "P",	FALSE, FALSE, DN_TYPE_P,     },	// </P>  not req in our manuals.
    { "BR",	FALSE, FALSE, DN_TYPE_BR,    },	// </BR> not req in our manuals.

    { PCNULL,	FALSE, FALSE, 0,	     },	// end of list.
};

// Convenience macros:

#define	TAG(DN_Type)	 (g_dntype[DN_Type].dnt_tag)
#define	SAVETAG(DN_Type) (g_dntype[DN_Type].dnt_savetag)
#define	NEST(DN_Type)	 (g_dntype[DN_Type].dnt_nest)


// ****************************************************************************
// DOCUMENT NODE DATA STRUCTURES:
//
// Document nodes are saved in a doubly-linked tree of docnodes.  Each docnode
// points sideways to a doubly-linked list of sibling docnodes for
// previous/successive unnested document objects, plus points to its parent and
// to the first of a sideways doubly-linked child list of nested objects.  All
// data lives in malloc'd memory.
//
// The dn_text field is null for a tag node unless the tag text is worth
// saving.  The field is non-null for non-tag (document) text.

typedef	struct docnode * Pdn_t;

struct docnode {
	int	dn_type;	// node type, index in g_dntype[].
	int	dn_linenum;	// where introduced, for reconstructing.
	bool_t	dn_closed;	// flag: closing tag was seen.
	bool_t	dn_noemit;	// flag: skip on output, for marking ahead.
	bool_t	dn_bold;	// flag: for <PRE>, whole section is bold.

	char *	dn_text;	// node text; see above.

	Pdn_t	dn_Pprev;	// previous node in sibling list.
	Pdn_t	dn_Pnext;	// next     node in sibling list.
	Pdn_t	dn_Pparent;	// up-link to parent node, if any.
	Pdn_t	dn_Pchild;	// down-link to first node in child subtree.
};

#define	PDNNULL	((Pdn_t) NULL)

Pdn_t g_Pdnhead = PDNNULL;	// head of docnode tree.


// ****************************************************************************
// FUNCTION SIGNATURES (forward declarations):

int	main(int argc, char ** argv);

void	ReadInputFile( char * Filename, FILE * PFile);
void	CheckNesting(Pdn_t Pdn);
void	EmitNroffHeader(char * Filename, char ** PPageName);
void	EmitNroffBody(Pdn_t Pdn, int DLLevel, int InPRE, char * PageName);

void	ExtractHeader(  Pdn_t Pdn, char ** PFileRev,
		        char ** PPageName, char ** PPageSection,
			char *  PLcLetter, char ** PRevision);
char *	ExtractText(    Pdn_t Pdn);
void	ExtractPageInfo(Pdn_t Pdn, char * Pch,
			char ** PPageName, char ** PPageSection,
			char *  PLcLetter);

int	TagType(char * Tag, bool_t * isclosing, char * Filename, int Linenum);
Pdn_t	AppDocNode( Pdn_t Pdn,	     int linenum);
Pdn_t	NewDocNode( Pdn_t dn_Pparent, int linenum);
char *	SaveDocNode(Pdn_t Pdn, int DN_Type, char * Pch,
		    char * Filename, int Linenum);
bool_t	ParentPre(Pdn_t Pdn, bool_t BoldOnly);

void	MarkNoEmit(   Pdn_t Pdn, bool_t Font);
void	EmitText(     char * Pch, int InPRE, int Linenum);
void	EmitTextPRE(  char * Pch, int InPRE);
void	EmitTextBS(   char * Pch);
bool_t	NoWhiteSpace( char * Pch);
int	CountNewlines(char * Pch);

char *	StrSave(  char * String);
char *	StrSaveN( char * String, ...);
void *	Malloc(	  size_t Size);

void	Usage(void);
void    Error(int Exitvalue, int MyErrno, char * Message, ...);

DBGCODE(void DumpTree(Pdn_t Pdn, int Depth, bool_t Separator);)


// ****************************************************************************
// M A I N

FUNCTION int main(
	int	argc,
	char **	argv)
{
	char *	filename;	// input file.
	FILE *	Pfile;		// open input file.
	char *	pagename;	// such as "Judy1".

	gc_myname = argv[0];
	if (argc != 2) Usage();
	filename = argv[1];

#ifdef DEBUG
// Assert that each dnt_type matches its index in the table, since the code
// depends on this:

	{
	    int dn_type;

	    for (dn_type = 0; TAG(dn_type) != PCNULL; ++dn_type)
		assert(g_dntype[dn_type].dnt_type == dn_type);
	}
#endif


// READ FROM LIST OF FILES OR STDIN; BUILD TREE:

	if ((Pfile = fopen(filename, "r")) == (FILE *) NULL)
	{
	    Error(ERREXIT, errno, "Cannot open file \"%s\" to read it",
		  filename);
	}

	ReadInputFile(filename, Pfile);

	if (ferror(Pfile))

src/judy-1.0.5/tool/jhton.c  view on Meta::CPAN


		Pch2 = Pch;
		SKIPNONSPACE(Pch2);  // find whitespace or end of line or tag.

		if (*Pch2 == CHNULL)
		    Error(ERREXIT, NOERRNO, FmtErrLineEnds, Filename, linenum);

		chold = *Pch2;
		*Pch2 = CHNULL;		// temporarily terminate.
		dn_type = TagType(Pch, &isclosing, Filename, linenum);
		*Pch2 = chold;


// HANDLE CLOSING TAG:
//
// First ensure the tag checks out OK.

		if (isclosing)
		{
		    if (*Pch2 != '>')
		    {
			Error(ERREXIT, NOERRNO, FILELINE "Closing HTML tag "
			      "\"%s\" must be followed immediately by \">\"; "
			      "this translator does not even allow whitespace",
			      Filename, linenum, Pch);
		    }

		    if (g_Pdnhead == PDNNULL)
		    {
			Error(ERREXIT, NOERRNO, FILELINE "Closing HTML tag "
			      "\"%s\" found before any opening tag in the file",
			      Filename, linenum, Pch);
		    }

		    tagname = Pch;		// for error reporting.
		    Pch = Pchp = Pch2 + 1;	// skip ">" in line.

// Check if the closing tag is an optional closing for the last unclosed,
// non-DN_TYPE_TEXT docnode in the current sibling list, if any:

		    for (Pdnprev  = Pdn;
			 Pdnprev != PDNNULL;
			 Pdnprev  = Pdnprev->dn_Pprev)
		    {
			if (((Pdnprev->dn_type) == DN_TYPE_TEXT)
			 || (Pdnprev->dn_closed))
			{
			    continue;		// skip text or closed tag.
			}

			if ((Pdnprev->dn_type) == dn_type)
			{
			    (Pdnprev->dn_closed) = TRUE;  // optional closing.
			    break;
			}
		    }

		    if (Pdnprev != PDNNULL) continue;	// matched closing.

// Otherwise check that the closing tag is the (required) closing tag for the
// (required) parent node (which must not have been closed yet):

		    if ((Pdn->dn_Pparent) == PDNNULL)
		    {
			Error(ERREXIT, NOERRNO, FILELINE "Closing HTML tag "
			      "\"%s\" does not match an opening tag",
			      Filename, linenum, tagname);
		    }

		    assert(! (Pdn->dn_Pparent->dn_closed));

		    if ((Pdn->dn_Pparent->dn_type) != dn_type)
		    {
			Error(ERREXIT, NOERRNO, FILELINE "Parent HTML tag "
			      "\"%s\" found on line %d requires a closing tag, "
			      "but \"%s\" does not match it; check for out-of-"
			      "order HTML tags", Filename, linenum,
			      TAG(Pdn->dn_Pparent->dn_type),
			      Pdn->dn_Pparent->dn_linenum, tagname);
		    }

// Go uplevel in the tree to the parent node:

		    Pdn = Pdn->dn_Pparent;
		    (Pdn->dn_closed) = TRUE;
		    continue;

		} // closing tag.


// NEW HTML TAG:  ADD SIBLING OR CHILD NODE TO TREE:
//
// Save appropriate information about the tag and move beyond its closing point
// in the input line.

		Pdn = AppDocNode(Pdn, linenum);
		Pch = Pch2;
		assert( Pch != PCNULL);
		assert(*Pch != CHNULL);
		Pch = Pchp = SaveDocNode(Pdn, dn_type, Pch, Filename, linenum);

	    } // while more on input line.
	} // while more in input file.

	g_linenumlim = linenum + 1;

} // ReadInputFile()


// ****************************************************************************
// C H E C K   N E S T I N G
//
// Given a docnode, recursively check that all nested HTML tags were closed.
// If not, error out.

FUNCTION void CheckNesting(
	Pdn_t Pdn)	// current docnode.
{
	if (NEST(Pdn->dn_type) && (! (Pdn->dn_closed)))
	{
	    Error(ERREXIT, NOERRNO, "No closing tag found for HTML tag \"%s\" "
		  "from input line %d", TAG(Pdn->dn_type), Pdn->dn_linenum);
	}

	if ((Pdn->dn_Pchild) != PDNNULL) CheckNesting(Pdn->dn_Pchild);
	if ((Pdn->dn_Pnext)  != PDNNULL) CheckNesting(Pdn->dn_Pnext);

} // CheckNesting()




// ****************************************************************************
// E M I T   N R O F F   H E A D E R
//
// Given the input filename, a pointer to a page name string to return, and the
// docnode tree under g_Pdnhead, extract header info and emit nroff header
// lines.

FUNCTION void EmitNroffHeader(
	char *	Filename,		// input file.
	char **	PPageName)		// such as "Judy1", to return.
{
	char *	filerev = PCNULL;	// from first comment in input.

src/judy-1.0.5/tool/jhton.c  view on Meta::CPAN

	(void) printf(".TA %c\n",    lcletter);
	(void) printf(".TH %s %s\n", *PPageName, pagesection);
	(void) puts(  ".ds )H Hewlett-Packard Company");
	(void) printf(".ds ]W %s\n", revision);

} // EmitNroffHeader()


// ****************************************************************************
// E M I T   N R O F F   B O D Y
//
// Given a current node in the docnodes tree, the current <DL> level, a flag
// whether below a <PRE> node, the manual entry page name, and in
// g_prevlinenum, the previous input line number that resulted in output,
// recursively emit nroff body text.  Translate the HTML docnodes as described
// in the comments prior to EmitNroffHeader(), and also translate certain HTML
// escaped chars back to literal form.  Hope the results are legal nroff
// without spurious unintended nroff commands embedded.
//
// Note:  This function recurses two ways; first, to the child subtree, and
// second, to the next sibling at the current level.

FUNCTION void EmitNroffBody(
	Pdn_t	Pdn,		// current top of subtree.
	int	DLLevel,	// <DL> level, top = 0.
	int	InPRE,		// bit flags for <PRE> handling.
	char *	PageName)	// such as "Judy1".
{
	int	DLcount = 0;	   // set to 1 if hit <DL> here.
	char *	suffix  = PCNULL;  // to print after children, before siblings.

// When about to emit text, if the previous output came from a lower input line
// number, start with a newline; otherwise do not, and let the text
// concatenate:
//
// Use CHECKPREV except when the text to be emitted is forced to a new line.

#ifdef CPPRINT		// for special debugging:
#define	CHECKPREVPRINT printf("\ncp %d %d\n", g_prevlinenum, Pdn->dn_linenum)
#else
#define	CHECKPREVPRINT // null
#endif

#define	CHECKPREV	\
	CHECKPREVPRINT;	\
	{ if (g_prevlinenum && (g_prevlinenum < (Pdn->dn_linenum))) PUTC('\n');}

// To support CHECKPREV, call SETPREV() after emitting text that might need a
// line break to a new line, or SETPREVNONL to ensure NO newline, that is, the
// next text concatenates on the same line:
//
// Note:  For a correct line number, SETPREV() must account for any newlines in
// the text just emitted.

#define	SETPREV(Text)	g_prevlinenum = (Pdn->dn_linenum) + CountNewlines(Text)
#define	SETPREVNONL	g_prevlinenum = g_linenumlim	// no newline.

// Check if under a lower-level <DL>, for continuing an indented paragraph:

#define	UNDER_DL ((DLLevel > 1)				\
	       && ((Pdn->dn_Pparent) != PDNNULL)	\
	       && ((Pdn->dn_Pparent->dn_type) == DN_TYPE_DL))


// SWITCH ON DOCNODE TYPE:

	if (Pdn->dn_noemit)	// upstream node said to skip this one.
	    goto NextNode;

	switch (Pdn->dn_type)
	{


// DOCUMENT TEXT:
//
// Just emit it with HTML escaped chars modified, with backslashes doubled,
// with no trailing newline, and if not within <PRE> text, with any leading
// whitespace deleted, so that, for example, something like "\fI text\fP" does
// not result.

	case DN_TYPE_TEXT:

	    assert((Pdn->dn_text) != PCNULL);
	    CHECKPREV;
	    EmitText(Pdn->dn_text, InPRE, Pdn->dn_linenum);
	    SETPREV(Pdn->dn_text);
	    break;


// IGNORE THESE TYPES:
//
// See EmitNroffHeader() for nroff equivalents already emitted in some cases.
// In some cases, mark all child nodes no-emit to ignore them.

	case DN_TYPE_HTML:
	case DN_TYPE_HEAD:
	case DN_TYPE_BODY:
	case DN_TYPE_COMM:	break;

	case DN_TYPE_TITLE:
	case DN_TYPE_TABLE:
	case DN_TYPE_TR:
	case DN_TYPE_TD:

	    MarkNoEmit(Pdn->dn_Pchild, /* Font = */ FALSE);
	    break;


// DESCRIPTIVE LIST:
//
// At the top level these represent manual entry sections, and any bold markers
// around the text are ignored.  Below the top level these translate to tagged
// paragraphs.  Here, just note the increment and continue the walk.

	case DN_TYPE_DL:

	    DLcount = 1;
	    break;


// DESCRIPTIVE LIST TAG:

src/judy-1.0.5/tool/jhton.c  view on Meta::CPAN

	} // case DN_TYPE_A


// BOLD TEXT:
//
// If the first child is <PRE>, use a "hard" font change; otherwise an in-line
// change.
//
// Note:  For <DT><B>, this node is already marked dn_noemit and not seen here.
//
// Note:  For <B><PRE>, nroff seems to reset font upon .PP, so mark the bold
// for later emission.

	case DN_TYPE_B:

	    if (((Pdn->dn_Pchild) != PDNNULL)
	     && ((Pdn->dn_Pchild->dn_type) == DN_TYPE_PRE))
	    {
		(Pdn->dn_Pchild->dn_bold) = TRUE;	// see above.
		break;
	    }

	    CHECKPREV;
	    PUTS("\\fB");			// bold font.
	    SETPREVNONL;
	    suffix = "\\fP";			// revert to previous font.
	    break;


// ITALIC TEXT:

	case DN_TYPE_I:

	    CHECKPREV;
	    PUTS("\\fI");			// italic font.
	    SETPREVNONL;
	    suffix = "\\fP";			// revert to previous font.
	    break;


// PREFORMATTED TEXT:
//
// Emit prefix/suffix directives based on example in strchr(3C).

	case DN_TYPE_PRE:

	    PUTS(UNDER_DL ? "\n.IP\n.nf\n.ps +1\n" : "\n.PP\n.nf\n.ps +1\n");
	    if (Pdn->dn_bold) PUTS(".ft B\n");	// deferred bold.
	    SETPREVNONL;
	    suffix = ((Pdn->dn_bold) ? "\n.ft P\n.ps\n.fi\n" : "\n.ps\n.fi\n");

	    // set for all children:
	    InPRE = INPRE_BLOCK
		  | ((Pdn->dn_bold) ? INPRE_BOLD   : 0)
		  | (UNDER_DL ?	      INPRE_INDENT : 0);
	    break;


// PARAGRAPH BREAK:
//
// If the parent is a <DL> below the top level, use .IP to continue a .TP
// (tagged paragraph); otherwise emit a standard .PP.

	case DN_TYPE_P:

	    PUTS(UNDER_DL ? "\n.IP\n" : "\n.PP\n");
	    SETPREVNONL;
	    break;


// LINE BREAK:

	case DN_TYPE_BR: PUTS("\n.br\n"); SETPREVNONL; break;


// UNRECOGNIZED DOCNODE TYPE:

	default:
	    Error(ERREXIT, NOERRNO, "Internal error: Unexpected docnode type "
		  "%d in docnodes tree", Pdn->dn_type);

	} // end switch on dn_type


// VISIT CHILD AND SIBLING DOCNODES:
//
// If this was a <DL> here, pass an incremented value to child nodes, but not
// to sibling nodes.

NextNode:
	if ((Pdn->dn_Pchild) != PDNNULL)
	    EmitNroffBody(Pdn->dn_Pchild, DLLevel + DLcount, InPRE, PageName);

	if (suffix != PCNULL) PUTS(suffix);

	if ((Pdn->dn_Pnext) != PDNNULL)
	    EmitNroffBody(Pdn->dn_Pnext, DLLevel, InPRE, PageName);

} // EmitNroffBody()


// ****************************************************************************
// E X T R A C T   H E A D E R
//
// Given a current docnode and pointers to values to return, walk the entire
// docnode tree once, recursively, in-order (parent then child then sibling) to
// extract nroff header information.  Find the first comment line, insist it
// contain "@\(#)", and put this in *PFileRev.  Also find exactly one
// DN_TYPE_TABLE, containing exactly one DN_TYPE_TR, containing one DN_TYPE_TD
// containing "align=\"left\"" and one DN_TYPE_TD containing
// "align=\"center\"", and extract from these the nroff .TH pagename,
// pagesection, and lcletter, and nroff ]W variable revision string,
// respectively.  Error out if anything goes wrong.
//
// Note:  Some of the returned strings are in separate malloc'd memory and
// others are not; treat them as read-only.

FUNCTION void ExtractHeader(
	Pdn_t	Pdn,		// current docnode.
	char **	PFileRev,	// from first comment in input.
	char **	PPageName,	// such as "Judy1".
	char **	PPageSection,	// such as "3X".
	char *	PLcLetter,	// manual tab section, such as "j".
	char **	PRevision)	// from centered table datum.
{
static	bool_t	found_filerev  = FALSE;
static	bool_t	found_table    = FALSE;
static	bool_t	found_tr       = FALSE;
static	bool_t	found_tdleft   = FALSE;
static	bool_t	found_tdcenter = FALSE;

	char *	text;		// from text node.

// Note:  The following are used for both 0 and >= 2 instances, so they don't
// include a line number because there is none for the 0 case:

#define	ERR_TABLE	"This translator expects exactly one HTML table " \
			"(\"TABLE\" tag) in the input file"
#define	ERR_TR		"This translator expects exactly one HTML table row " \
			"(\"TR\" tag) in the input file"
#define	ERR_TDLEFT	"This translator expects exactly one HTML table row " \
			"datum (\"TD\" tag) in the input containing " \
			"'align=\"left\"'"
#define	ERR_TDCENTER	"This translator expects exactly one HTML table row " \
			"datum (\"TD\" tag) in the input containing " \
			"'align=\"center\"'"


// CHECK CURRENT DOCNODE TYPE:

	switch (Pdn->dn_type)
	{
	case DN_TYPE_COMM:

	    if (found_filerev) break;	// already done.
	    found_filerev = TRUE;

// Hide the whatstring markers here from what(1) itself:

	    if (strstr(Pdn->dn_text, "@" "(#)") == PCNULL)
	    {
		Error(ERREXIT, NOERRNO, "First HTML comment line in input, "
		      "found at line %d, must contain a whatstring, marked by "
		      "\"@" "(#)\"", Pdn->dn_linenum);
	    }

src/judy-1.0.5/tool/jhton.c  view on Meta::CPAN


	    if (strstr(Pdn->dn_text, "align=\"left\"") != PCNULL)
	    {
		if (found_tdleft) Error(ERREXIT, NOERRNO, ERR_TDLEFT);
		found_tdleft = TRUE;

		text = StrSave(ExtractText(Pdn));
		ExtractPageInfo(Pdn, text, PPageName, PPageSection, PLcLetter);
	    }
	    else if (strstr(Pdn->dn_text, "align=\"center\"") != PCNULL)
	    {
		if (found_tdcenter) Error(ERREXIT, NOERRNO, ERR_TDCENTER);
		found_tdcenter = TRUE;

		*PRevision = ExtractText(Pdn);
	    }
	    // else ignore line.

	} // switch on dn_type


// VISIT CHILD AND SIBLING DOCNODES:
//
// Note:  Do this even though it seems redundant, to ensure no duplicates.

	if ((Pdn->dn_Pchild) != PDNNULL)
	{
	    ExtractHeader(Pdn->dn_Pchild, PFileRev,
			  PPageName, PPageSection, PLcLetter, PRevision);
	}

	if ((Pdn->dn_Pnext) != PDNNULL)
	{
	    ExtractHeader(Pdn->dn_Pnext, PFileRev,
			  PPageName, PPageSection, PLcLetter, PRevision);
	}


// AT TOP OF TREE, CHECK FOR SUCCESS:
//
// Note:  If you read the fine print, it's clear the ERR_TR here is impossible
// to hit.

	if (Pdn != g_Pdnhead) return;

	if (! found_table)	Error(ERREXIT, NOERRNO, ERR_TABLE);
	if (! found_tr)		Error(ERREXIT, NOERRNO, ERR_TR);
	if (! found_tdleft)	Error(ERREXIT, NOERRNO, ERR_TDLEFT);
	if (! found_tdcenter)	Error(ERREXIT, NOERRNO, ERR_TDCENTER);

} // ExtractHeader()


// ****************************************************************************
// E X T R A C T   T E X T
//
// Given a non-null docnode, return the non-null dn_text field from its first
// child (directly, not a copy).  Error out if anything goes wrong.

FUNCTION char * ExtractText(
	Pdn_t Pdn)	// parent node.
{
	assert(Pdn != PDNNULL);

#define	ERR_NULLTEXT "Node for HTML tag \"%s\", found at line %d, lacks a " \
		     "child \"text\" node containing a non-null text string " \
		     "as required"

// TBD:  This does not report a case of a text string containing only
// whitespace, but some callers do that themselves:

	if (((Pdn->dn_Pchild) == PDNNULL)
	 || ((Pdn->dn_Pchild->dn_type) != DN_TYPE_TEXT)
	 || ((Pdn->dn_Pchild->dn_text) == PCNULL)
	 || ((Pdn->dn_Pchild->dn_text[0]) == CHNULL))
	{
	    Error(ERREXIT, NOERRNO, ERR_NULLTEXT,
		  TAG(Pdn->dn_type), Pdn->dn_linenum);
	}

	return(Pdn->dn_Pchild->dn_text);

} // ExtractText()


// ****************************************************************************
// E X T R A C T   P A G E   I N F O
//
// Given a docnode, a non-null, modifiable string alleged to contain an nroff
// -man header, such as "Judy(3X)", and pointers to return values, break out
// and return the pieces.  Error out if anything goes wrong.
//
// Note:  Returned strings are in separate malloc'd memory.

FUNCTION void ExtractPageInfo(
	Pdn_t	Pdn,		// for error reporting.
	char *	Pch,		// string to decipher.
	char **	PPageName,	// such as "Judy1".
	char **	PPageSection,	// such as "3X".
	char *	PLcLetter)	// manual tab section, such as "j".
{
	char *	Pch2;		// second place in string.
	char *	Pch3 = PCNULL;	// third  place in string; init for gcc -Wall.

// Find start of string:

	assert(Pch != PCNULL);
	SKIPSPACE(Pch);

	if (*Pch == CHNULL)		// nothing but whitepace.
	{
	    Error(ERREXIT, NOERRNO, ERR_NULLTEXT,
		  TAG(Pdn->dn_type), Pdn->dn_linenum);
	}

// Find "(":

	if ((Pch2 = strchr(Pch, '(')) == PCNULL)
	{
	    Error(ERREXIT, NOERRNO, "Node for HTML tag \"%s\", found at "
		  "line %d, has a child \"text\" node whose text lacks a '('",

src/judy-1.0.5/tool/jhton.c  view on Meta::CPAN

	assert( Tag != PCNULL);
	assert(*Tag != CHNULL);

// Check for closing tag (yes, even for types that don't really allow it):

	if ((*Pisclosing = (*Tag == '/')))	// (()) for gcc.
	{
	    ++Tag;
	    SKIPSPACE(Tag);

	    if (*Tag == CHNULL)
		Error(ERREXIT, NOERRNO, FmtErrLineEnds, Filename, Linenum);
	}

// Translate comment tag to known type:

	mytag = (strncmp(Tag, "!--", 3) ? Tag : "!--");	 // see above.

// Look up tag:
//
// Note:  Main code already asserted dnt_type == dn_type for each entry.

	for (dn_type = 0; TAG(dn_type) != PCNULL; ++dn_type)
	    if (strcmp(mytag, TAG(dn_type)) == 0) return(dn_type);

	Error(ERREXIT, NOERRNO, FILELINE "Unrecognized HTML tag \"%s\"; "
	      "see program source file for recognized types; this is a "
	      "limited, special-purpose translator", Filename, Linenum, Tag);

	/*NOTREACHED*/
	return(0);		// make some compilers happy.

} // TagType()


// ****************************************************************************
// A P P   D O C   N O D E
//
// Given a current docnode tree node, the input file line number, and
// g_Pdnhead, create a new docnode, append it to the tree in the right place,
// and return a pointer to it, with g_Pdnhead updated if required:
//
// * If empty tree, insert new as head of tree.
//
// * Otherwise if current node nests and is not closed, insert as its child.
//
// * Otherwise insert as a sibling of the current node.
//
// Note:  Most HTML tags are non-singletons and hence nest, but if the nesting
// doesn't make sense, too bad, it's not detected, at least not here.

FUNCTION Pdn_t AppDocNode(
	Pdn_t Pdn,		// current docnode tree node.
	int   Linenum)		// in input file.
{
// No current tree, insert first node:

	if (g_Pdnhead == PDNNULL)
	    return(g_Pdnhead = NewDocNode(PDNNULL, Linenum));

// Insert new node as child, with parent set to current node:

	if (NEST(Pdn->dn_type) && (! (Pdn->dn_closed)))
	    return((Pdn->dn_Pchild) = NewDocNode(Pdn, Linenum));

// Insert new node as sibling with same parent:

	(Pdn->dn_Pnext) = NewDocNode(Pdn->dn_Pparent, Linenum);
	(Pdn->dn_Pnext->dn_Pprev) = Pdn;
	return(Pdn->dn_Pnext);

} // AppDocNode()


// ****************************************************************************
// N E W   D O C   N O D E
//
// Malloc() a new docnode and initialize its fields except dn_type, with error
// checking.  Set its parent to the given value.

FUNCTION Pdn_t NewDocNode(
	Pdn_t dn_Pparent,	// parent to record.
	int   Linenum)		// in input file.
{
	Pdn_t Pdn = (Pdn_t) Malloc(sizeof(struct docnode));

	(Pdn -> dn_linenum) = Linenum;
	(Pdn -> dn_closed)  = FALSE;
	(Pdn -> dn_noemit)  = FALSE;
	(Pdn -> dn_bold)    = FALSE;
	(Pdn -> dn_text)    = PCNULL;
	(Pdn -> dn_Pprev)   = PDNNULL;
	(Pdn -> dn_Pnext)   = PDNNULL;
	(Pdn -> dn_Pparent) = dn_Pparent;
	(Pdn -> dn_Pchild)  = PDNNULL;

	return(Pdn);

} // NewDocNode()


// ****************************************************************************
// S A V E   D O C   N O D E
//
// Given a pointer to a docnode, the docnode type, a string for the current
// location (past tag name at whitespace or ">"), and a filename and line
// number for error reporting, save the docnode type in the node, and also save
// the tag text if appropriate; then find the end of the tag (">") and return
// past that location (possibly before more whitespace).  Error out in case of
// syntax error.

FUNCTION char * SaveDocNode(
	Pdn_t	Pdn,		// docnode to modify.
	int	DN_Type,	// new type to save.
	char *	Pch,		// current location past tagname.
	char *	Filename,	// for error reporting.
	int	Linenum)	// for error reporting.
{
	char *	Pch2 = PCNULL;	// second location; init for gcc -Wall.

	assert( Pch != PCNULL);
	assert(*Pch != CHNULL);

// Save type:

	(Pdn->dn_type) = DN_Type;

// Pass whitespace and then find the end of the tag:

	SKIPSPACE(Pch);

	if ((*Pch == CHNULL) || ((Pch2 = strchr(Pch, '>')) == PCNULL))
	    Error(ERREXIT, NOERRNO, FmtErrLineEnds, Filename, Linenum);

// Optionally save tag text:

	if (SAVETAG(DN_Type))
	{
	    *Pch2 = CHNULL;			// temporarily terminate.
	    (Pdn->dn_text) = StrSave(Pch);
	    *Pch2 = '>';
	}

	return(Pch2 + 1);

} // SaveDocNode()


// ****************************************************************************
// P A R E N T   P R E
//
// Given a docnode (can be null) and a flag whether only bold <PRE> is of
// interest, return TRUE if any of its parents is a <PRE> (marked for bold
// text), that is, DN_TYPE_PRE (with dn_bold set); otherwise return FALSE.

FUNCTION bool_t ParentPre(
	Pdn_t  Pdn,		// starting node.
	bool_t BoldOnly)	// flag: only care about bold <PRE>.
{
	if (Pdn == PDNNULL) return (FALSE);	// no parent.

	for (Pdn = Pdn->dn_Pparent; Pdn != PDNNULL; Pdn = Pdn->dn_Pparent)
	{
	    if (((Pdn->dn_type) == DN_TYPE_PRE)
	     && ((! BoldOnly) || (Pdn->dn_bold)))
	    {
		return(TRUE);
	    }
	}

	return(FALSE);

} // ParentPre()


// ****************************************************************************
// M A R K   N O   E M I T
//
// Given a docnode (can be null), and a flag, recursively mark the node and all
// children and siblings as do-not-emit, unless the flag is set, only mark font
// docnodes.

FUNCTION void MarkNoEmit(
	Pdn_t  Pdn,	// top node to mark.
	bool_t Font)	// flag: only mark font docnodes.
{
	if (Pdn == PDNNULL) return;

	if ((! Font)
	 || ((Pdn->dn_type) == DN_TYPE_B)
	 || ((Pdn->dn_type) == DN_TYPE_I))
	{
	    (Pdn->dn_noemit) = TRUE;
	}

	if ((Pdn->dn_Pchild) != PDNNULL) MarkNoEmit(Pdn->dn_Pchild, Font);
	if ((Pdn->dn_Pnext)  != PDNNULL) MarkNoEmit(Pdn->dn_Pnext,  Font);

} // MarkNoEmit()


// ****************************************************************************
// E M I T   T E X T
//
// Given a text string, a bitflag for <PRE> status, and an input line number
// for error reporting, copy the text string to stdout with no added newlines,
// but translating selected HTML escape codes to simple characters, doubling
// any backslashes, and if InPRE, inserting .IP (if INPRE_INDENT) or .PP at
// blank lines (between successive newlines), and if INPRE_BOLD, putting back
// bold font since .IP/.PP seems to reset the font.  Warn about unrecognized
// escape codes.

struct et_list {
	char * et_escape;	// expected text.
	size_t et_len;		// of expected text.
	char   et_emit;		// equivalent char.
} et_list[] = {
	{ "amp;", 4, '&', },
	{ "gt;",  3, '>', },
	{ "lt;",  3, '<', },
	{ PCNULL, 0, ' ', },	// end of list.
};

src/judy-1.0.5/tool/jhton.c  view on Meta::CPAN

} // Usage()


// ****************************************************************************
// E R R O R
//
// Given an exit value (NOEXIT, ERREXIT, or USAGE), an errno value (NOERRNO if
// none), a message (printf) string, and zero or more argument strings, print
// an error message to stderr and, if exitvalue is NOEXIT, return; if USAGE,
// print a pointer to the program's usage message; otherwise exit with the
// value.
//
// Message is preceded by "<myname>: " using global gc_myname, and by
// "Warning: " for NOEXIT, and followed by a period and newline.  If myerrno
// (system error number) is not NOERRNO, a relevant message is appended before
// the period.

FUNCTION void Error(
	int	Exitvalue,	// or NOEXIT for warning.
	int	MyErrno,	// system errno if relevant.
	char *	Message, ...)
{
	va_list Parg;		// place in arg list.

	(void) fprintf(stderr, "%s: ", gc_myname);
	if (Exitvalue == NOEXIT) (void) fputs("Warning: ", stderr);

	va_start(Parg, Message);
	(void) vfprintf(stderr, Message, Parg);
	va_end(Parg);

	if (MyErrno != NOERRNO)
	{
	    (void) fprintf(stderr, ": %s (errno = %d)", strerror(MyErrno),
			   MyErrno);
	}

	(void) putc('.',  stderr);
	(void) putc('\n', stderr);

	if (Exitvalue == USAGE)
	{
	    (void) fprintf(stderr, "For a usage summary, run %s -?\n",
			   gc_myname);
	}

	DBGCODE(DumpTree(g_Pdnhead, /* Depth = */ 0, /* Separator = */ FALSE);)

	if (Exitvalue != NOEXIT)
	    exit(Exitvalue);

} // Error()


#ifdef DEBUG

// ****************************************************************************
// D U M P   T R E E
//
// Dump to stdout a representation of the docnode tree under g_Pdnhead.
// Recursively traverse the tree in-order (parent then child then sibling).

FUNCTION void DumpTree(
	Pdn_t  Pdn,		// first node of current sibling list.
	int    Depth,		// current depth.
	bool_t Separator)	// print a separator line after a long dump.
{
	int   indent;		// for counting to Depth.

// Check if enabled:

	if (getenv("DUMP") == PCNULL)
	{
	    PUTS(".\\\" $DUMP not set; DumpTree() disabled.\n");
	    return;
	}

// Check for empty tree:

	if ((Depth == 0) && (Pdn == PDNNULL))
	{
	    PUTS("Head pointer is null.\n");
	    return;
	}

// Print siblings and each of their children, indented to Depth after the node
// address:

	while (Pdn != PDNNULL)
	{
	    (void) printf("%lx ", (unsigned long) Pdn);

	    for (indent = 0; indent <= Depth; ++indent) PUTC('.');

	    (void) printf(" %-5s %3d %c %lx %lx \"%s\"\n",
			  ((Pdn -> dn_type) == DN_TYPE_TEXT) ?
			      "text" : TAG(Pdn -> dn_type),
			  Pdn -> dn_linenum,
			  (Pdn->dn_closed) ? 'c' : 'o',
			  Pdn -> dn_Pparent,
			  Pdn -> dn_Pprev,
			  Pdn -> dn_text);

	    if ((Pdn -> dn_Pchild) != PDNNULL)
		DumpTree(Pdn -> dn_Pchild, Depth + 1, Separator);

	    Pdn = Pdn -> dn_Pnext;
	}

// Print separator line:

	if ((Depth == 0) && Separator)
	    PUTS("=======================================================\n");

} // DumpTree()

#endif // DEBUG



( run in 1.284 second using v1.01-cache-2.11-cpan-2398b32b56e )