home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BLKIO10.ZIP
/
MANX10.ZIP
/
MANX.C
next >
Wrap
Text File
|
1989-08-30
|
10KB
|
345 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "manx.c 1.1 - 89/08/29" */
#include <errno.h>
#include <limits.h>
#include <stdio.h>
/* #include <stdlib.h> */
void exit(); /* delete if you have stdlib.h */
#define EXIT_SUCCESS (0)
#define EXIT_FAILURE (1)
/*#include <string.h>*/
char *strstr(); /* delete if you have string.h */
int strncmp();
int strcmp();
unsigned strlen();
#define PAGELEN (60) /* default page length */
#define BACKSLASH ('\\') /* char to begin escape sequence */
#define PAGINATOR ("\f") /* string to use as page separator */
#define MAXLINE (256) /* maximum line length */
/* command syntax */
#define USAGE ("Usage: manx [language] [page-length]")
/* language type definition */
typedef struct {
char option[11]; /* language command line option */
char begin[21]; /* marker for beginning of manual entry */
char end[21]; /* marker for end of manual entry */
char comment[11]; /* character sequence that begins comment */
int flags; /* bit flags */
} lang_t;
/* lang_t flag bits */
#define SINGLELINE (1)
#define MULTILINE (2)
lang_t langs[] = {
{"a", "--man", "--end", "--", SINGLELINE}, /* Ada */
{"c", "/*man", "*/", "/*", MULTILINE}, /* C */
{"f", "Cman", "Cend", "C", SINGLELINE}, /* Fortran */
{"l", ";man", ";end", ";", SINGLELINE}, /* Lisp */
{"m", "(*man", "*)", "(*", MULTILINE}, /* Modula-2 */
{"p", "{man", "}", "{", MULTILINE}, /* Pascal */
{"pl", "\"man", "\"", "\"", MULTILINE}, /* Prolog */
{"st", "\"man", "\"", "\"", MULTILINE} /* Smalltalk */
};
/* index into langs array for each language */
#define ADA (1)
#define C (2)
#define FORTRAN (3)
#define LISP (4)
#define MODULA_2 (5)
#define PASCAL (6)
#define PROLOG (7)
#define SMALLTALK (8)
#define LANG C /* default language */
static int manputs(/*const char *cs, FILE *stream*/);
/*man---------------------------------------------------------------------------
NAME
manx - manual entry extracter
SYNOPSIS
manx [language] [page-length]
DESCRIPTION
The manx command extracts manual entries from source files. The
files are read from stdin and the manual entries are written to
stdout. Each individual manual entry is broken into pages
separated by form feeds and terminated with a form feed.
The language option specifies which language the source file is
written in. The languages supported are:
Ada -a
C -c
Fortran -f
Lisp -l
Modula-2 -m
Pascal -p
Prolog -pl
Smalltalk -st
The default language is C.
The page-length argument can be used to set the page length.
Pagination may be disabled by specifying a page length of 0.
The default page length is 60.
The beginning of a manual entry is marked by the character
sequence (language dependent) to start a comment immediately
followed by the characters man. This marker must appear in the
leftmost column allowed by the language. For multiple-line
comments, the end of the manual entry is marked by the character
sequence terminating the comment. For single-line comments, the
end of a manual entry is marked by the character sequence to end
a comment immediately followed by the characters end. All lines
between but not including these constitute the manual entry.
The following escape sequences can be used within a manual entry:
audible alert BEL \\a
backspace BS \\b
carriage return CR \\r
horizontal tab HT \\t
backslash \\ \\\\
EXAMPLE
The following command would extract the manual pages from all C
files in the current UNIX directory, paginate them every 24
lines, and place them in a file called manual.
cat *.c | manx -c 24 > manual
------------------------------------------------------------------------------*/
/*int main(int argc, char *argv[])*/
int main(argc, argv)
int argc;
char * argv[];
{
int lang = LANG; /* default language */
long pagelen = PAGELEN; /* default page length */
int page = 0; /* page number */
long line = 0; /* line number */
int i = 0; /* index variable */
char s[MAXLINE] /*= ""*/; /* line storage */
char * endptr = NULL; /* pointer for strtol fct */
/* process command line options and arguments */
/* language option */
if ((argc > 1) && (*argv[1] == '-')) {
argc--;
argv++;
lang = 0;
for (i = 0; i < sizeof(langs)/sizeof(langs[0]); i++) {
if (strcmp(*argv + 1, langs[i].option) == 0) {
lang = i + 1;
break;
}
}
if (lang == 0) {
printf("manx: invalid language option\n");
printf("%s\n", USAGE);
exit(EXIT_FAILURE);
}
}
/* page length argument */
if (argc > 1) {
argc--;
argv++;
errno = 0;
pagelen = strtol(*argv, &endptr, 10);
if (errno == ERANGE) {
printf("manx: page length argument out of range\n");
printf("%s\n", USAGE);
exit(EXIT_FAILURE);
}
if (endptr != NULL) {
if (endptr[0] != '\0') {
printf("%s\n", USAGE);
exit(EXIT_FAILURE);
}
}
if (pagelen < 0) {
printf("%s\n", USAGE);
exit(EXIT_FAILURE);
}
}
if (argc != 1) {
printf("%s\n", USAGE);
exit(EXIT_FAILURE);
}
/* main loop */
while (1) {
/* read next line of input */
if (fgets(s, sizeof(s), stdin) == NULL) {
if (ferror(stdin) != 0) {
fprintf(stderr, "*** Error reading standard input. Exiting.\n");
exit(EXIT_FAILURE);
}
break;
}
/* check for manual entry marker at beginning of line */
if (strncmp(s, langs[lang - 1].begin, strlen(langs[lang - 1].begin)) != 0) {
continue;
}
if (langs[lang - 1].flags & MULTILINE) {
if (strstr(s, langs[lang - 1].end) != NULL) {
continue;
}
}
/* inner loop */
line = 0;
page = 1;
while (1) {
line++;
/* read next line of manual entry */
if (fgets(s, sizeof(s), stdin) == NULL) {
if (ferror(stdin) != 0) {
fprintf(stderr, "*** Error reading standard input. Exiting.\n");
exit(EXIT_FAILURE);
}
break;
}
/* check for end of entry marker */
if (langs[lang - 1].flags & SINGLELINE) {
if (strncmp(s, langs[lang - 1].end, strlen(langs[lang - 1].end)) == 0) {
break;
}
if (strncmp(s, langs[lang - 1].comment, strlen(langs[lang - 1].comment)) != 0) {
break;
}
} else {
if (strstr(s, langs[lang - 1].end) != NULL) {
break;
}
}
if (s[strlen(s) - 1] != '\n') {
fprintf(stderr, "*** Warning. Maximum line length of %d exceeded. Page %ld, line %ld.\n", (int)(sizeof(s) - 2), page, line);
s[strlen(s) - 1] = '\n';
}
if (langs[lang - 1].flags & SINGLELINE) {
if (manputs(s + strlen(langs[lang - 1].comment) , stdout) == EOF) {
exit(EXIT_FAILURE);
}
} else {
if (manputs(s, stdout) == EOF) {
exit(EXIT_FAILURE);
}
}
if ((line >= pagelen) && (pagelen != 0)) {
if (fputs(PAGINATOR, stdout) == EOF) {
exit(EXIT_FAILURE);
}
line = 0;
page++;
}
}
/* write last paginator */
if ((line != 1) && (pagelen != 0)) {
if (fputs(PAGINATOR, stdout) == EOF) {
exit(EXIT_FAILURE);
}
page++;
}
/* check if end of file */
if (feof(stdin) != 0) {
break;
}
}
exit(EXIT_SUCCESS);
}
/*------------------------------------------------------------------------------
DESCRIPTION
The manputs function writes the null-terminated string pointed to by
cs to the named output stream. Any manx escape sequence found in
the string is converted to the character it represents before being
output.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise, a
value of EOF is returned.
------------------------------------------------------------------------------*/
/*static int manputs(const char *cs, FILE *stream)*/
static int manputs(cs, stream)
char *cs;
FILE *stream;
{
char t[MAXLINE] /*= ""*/; /* target string */
int i = 0; /* index into target string */
char c = '\0';
/* convert string to output format */
for (i = 0; *cs != '\0'; i++) {
if (i > sizeof(t)) {
return EOF;
}
c = *cs++;
/* check for escape sequence */
if ((c == BACKSLASH) && (*cs != '\0')) {
c = *cs++;
switch (c) {
case 'a': /* audible alert */
c = '\a';
break; /* case 'a': */
case 'b': /* backspace */
c = '\b';
break; /* case 'b': */
case 'r': /* carriage return */
c = '\r';
break; /* case 'r': */
case 't': /* horizontal tab */
c = '\t';
break; /* case 't': */
case '\\': /* backslash */
c = '\\';
break; /* case '\\': */
default: /* ignore backslash */
break; /* default: */
}
}
t[i] = c;
}
t[i] = '\0';
/* write converted string to stream */
if (fputs(t, stream) == EOF) {
return EOF;
}
return 0;
}
/* delete this function if it is included in your string library */
char *strstr(cs, ct)
char *cs;
char *ct;
{
unsigned int ctlen = 0;
if ((cs == NULL) || (ct == NULL)) {
return NULL;
}
ctlen = strlen(ct);
while (*cs != '\0') {
if (strncmp(cs, ct, ctlen) == 0) {
return cs;
}
cs++;
}
return NULL;
}