1208963Srdivacky#!/bin/sh
2208963Srdivacky
3# srctoman - extract manual page from source file comment
4
5# @(#) srctoman.sh 1.2 11/4/89 15:56:22
6
7LANG=
8
9# process arguments
10
11while :
12do
13    case $1 in
14 [0-9]) SECT=$1;;
15     -) LANG=$1; B='[#:]';;
16  -awk) LANG=$1; B='#';;
17    -c) LANG=$1; B='\/\*';;
18    -f) LANG=$1; B='[Cc]';;
19   -mk) LANG=$1; B='#';;
20 -n|-t) LANG=$1; B='\\"';;
21    -p) LANG=$1; B='{';;
22    -r) LANG=$1; B='#';;
23    -C) LANG=$1; B=$2; shift;;
24    -*) ERROR="unknown option: $1"; break;;
25    "") ERROR="missing file argument"; break;;
26     *) break;;
27    esac
28    shift
29done
30
31# check error status
32
33case $ERROR in
34"") ;;
35 *) echo "$0: $ERROR" 1>&2
36    echo "usage: $0 [-|-awk|-c|-f|-mk|-n|-p|-t|-r|-C] [section] file(s)" 1>&2; exit 1;;
37esac
38
39# set up for file suffix processing
40
41case $LANG in
42"") sh='[:#]';	r='#';	rh=$r;	awk='#'; mk='#';
43    c='\/\*';	h=$c;	y=$c;	l=$c;
44    f='[Cc]';	fh=$f;	p='{';	ph=$p;
45    ms='\\"';	nr=$ms;	mn=$ms;	man=$ms;
46esac
47
48# extract comments
49
50for i in $*
51do
52    case $LANG in
53    "") eval B\="\$`expr $i : '.*\.\([^.]*\)$'`"
54	test "$B" || { echo "$0: unknown suffix: $i; assuming c" 1>&2; B=$c; }
55    esac
56    sed '
57    /^'"$B"'++/,/^'"$B"'--/!d
58    /^'"$B"'++/d
59    /^'"$B"'--/d
60    s/[ 	]*$//
61    /^'"$B"' \([A-Z]\)/{
62	s//\1/
63	/^NAME[ 	]*$/{
64	    N
65	    s/^.*\n'"$B"'[ 	]*//
66	    h
67	    y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
68	    s/^.*$/.TH & '"$SECT"'\
69.ad\
70.fi\
71.SH NAME/
72	    p
73	    g
74	    s/[ 	][ 	]*[0-9][ 	]*$//
75	    a\
76\\-
77	    p
78	    d
79	}
80	/^SUMMARY/d
81	/^DESCRIPTION/s//.SH &\
82.ad\
83.fi/
84	/^BUGS/s//.SH &\
85.ad\
86.fi/
87	/^DIAGNOSTICS/s//.SH &\
88.ad\
89.fi/
90	/^HISTORY/s//.SH &\
91.ad\
92.fi/
93	/^[A-Z][A-Z][A-Z][^a-z]*$/s//.SH "&"\
94.na\
95.nf/
96	p
97	d
98    }
99    s/^'"$B"' *//
100    s/^	//
101    s/^[ 	]*$//
102    /^\\"/d
103    /^\./{
104	s/\([^ ]\)-/\1\\-/g
105    }
106    /^'"'"'/{
107	s/^/\\\&/
108    }
109    /^[^.]/{
110	s/-/\\-/g
111    }
112' $i
113done | expand
114
115exit
116
117#++
118# NAME
119#	srctoman 1
120# SUMMARY
121#	extract manual page from source file comment
122# PACKAGE
123#	sdetools
124# SYNOPSIS
125#	srctoman [-|-awk|-c|-f|-mk|-m|-n|-p|-t|-r|-C] [section] file(s)
126# DESCRIPTION
127#	\fIsrctoman\fR converts comments in various programming languages to
128#	UNIX-style manual pages.
129#	The command processes comments in the style of newsrc(1);
130#	its standard output is suitable for formatting with nroff(1) or 
131#	troff(1) using the "-man" macro package.  
132#	Typically, srctoman is invoked from make(1) scripts.
133#
134#	Source files are processed in the indicated order; if no
135#	files are specified the command produces no output.
136#
137#	The source file language can be specified through a command-line
138#	option, or can be implied by the filename suffix.
139#	The expected start-of-comment symbol is shown in the last column.
140#
141# .nf
142# .ft C
143	option	language	comment
144
145	-	shell		[:#]
146	-awk	awk		#
147	-c	c		/*
148	-f	fortran		[Cc]
149	-mk	make		#
150	-n	nroff		\\"
151	-p	pascal		{
152	-t	troff		\\"
153	-r	ratfor		#
154	-C	any language	next argument
155#
156	suffix	language	comment
157
158	.awk	awk		#
159	.c	c		/*
160	.f	fortran		[Cc]
161	.fh	fortran		[Cc]
162	.h	c		/*
163	.l	lex		/*
164	.man	nroff,troff	\\"
165	.mk	make		#
166	.me	nroff,troff	\\"
167	.ms	nroff,troff	\\"
168	.nr	nroff,troff	\\"
169	.p	pascal		{
170	.ph	pascal		{
171	.r	ratfor		#
172	.rh	ratfor		#
173	.sh	shell		[:#]
174	.y	yacc		/*
175# .ft
176# .PP
177# .fi
178#
179#	The required format of comments is discussed below, where SOC
180#	stands for the start-of-comment symbol of the language being used.
181# .IP o
182#	Start of manual: SOC, followed by `++'.
183# .IP o
184#	Section heading: SOC, blank, section name in upper case.
185# .IP o
186#	All other text: SOC and subsequent blanks or tabs are removed.
187#	Lines that do not start with SOC are left unchanged (useful for 
188#	inclusion of program text).
189# .IP o
190#	End of manual: SOC, followed by `--'.
191#	An end-of-comment may follow if the source file language requires this.
192# .PP
193#	The following manual sections receive a special treatment:
194#	NAME and SUMMARY should appear at the beginning and in
195#	this order; DESCRIPTION, DIAGNOSTICS and BUGS will be
196#	right-margin adjusted.
197#	Other sections may be added freely without confusing srctoman.
198# COMMANDS
199#	sh(1), sed(1), expand(1)
200# SEE ALSO
201#	newsrc(1)
202# DIAGNOSTICS
203#	The program complains if an unknown language is specified
204#	of if the language cannot be deduced from the file suffix.
205# AUTHOR(S)
206#	W.Z. Venema
207#	Eindhoven University of Technology
208#	Department of Mathematics and Computer Science
209#	Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
210# CREATION DATE
211#	Fri Jan 17 22:59:27 MET 1986
212# STATUS
213#	srctoman.sh 1.2 11/4/89 15:56:22 (draft)
214#--
215