summaryrefslogtreecommitdiffstats
path: root/mantools/srctoman
blob: 9ea45cd8d60bc537caf90913a77a6561cb9f4136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/sh

# srctoman - extract manual page from source file comment

# @(#) srctoman.sh 1.2 11/4/89 15:56:22

LANG=

# process arguments

while :
do
    case $1 in
 [0-9]) SECT=$1;;
     -) LANG=$1; B='[#:]';;
  -awk) LANG=$1; B='#';;
    -c) LANG=$1; B='\/\*';;
    -f) LANG=$1; B='[Cc]';;
   -mk) LANG=$1; B='#';;
 -n|-t) LANG=$1; B='\\"';;
    -p) LANG=$1; B='{';;
    -r) LANG=$1; B='#';;
    -C) LANG=$1; B=$2; shift;;
    -*) ERROR="unknown option: $1"; break;;
    "") ERROR="missing file argument"; break;;
     *) break;;
    esac
    shift
done

# check error status

case $ERROR in
"") ;;
 *) echo "$0: $ERROR" 1>&2
    echo "usage: $0 [-|-awk|-c|-f|-mk|-n|-p|-t|-r|-C] [section] file(s)" 1>&2; exit 1;;
esac

# set up for file suffix processing

case $LANG in
"") sh='[:#]';	r='#';	rh=$r;	awk='#'; mk='#';
    c='\/\*';	h=$c;	y=$c;	l=$c;
    f='[Cc]';	fh=$f;	p='{';	ph=$p;
    ms='\\"';	nr=$ms;	mn=$ms;	man=$ms;
esac

# extract comments

for i in $*
do
    case $LANG in
    "") eval B\="\$`expr $i : '.*\.\([^.]*\)$'`"
	test "$B" || { echo "$0: unknown suffix: $i; assuming c" 1>&2; B=$c; }
    esac
    sed '
    /^'"$B"'++/,/^'"$B"'--/!d
    /^'"$B"'++/d
    /^'"$B"'--/d
    s/[ 	]*$//
    /^'"$B"' \([A-Z]\)/{
	s//\1/
	/^NAME[ 	]*$/{
	    N
	    s/^.*\n'"$B"'[ 	]*//
	    h
	    y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
	    s/^.*$/.TH & '"$SECT"'\
.ad\
.fi\
.SH NAME/
	    p
	    g
	    s/[ 	][ 	]*[0-9][ 	]*$//
	    a\
\\-
	    p
	    d
	}
	/^SUMMARY/d
	/^DESCRIPTION/s//.SH &\
.ad\
.fi/
	/^BUGS/s//.SH &\
.ad\
.fi/
	/^DIAGNOSTICS/s//.SH &\
.ad\
.fi/
	/^HISTORY/s//.SH &\
.ad\
.fi/
	/^[A-Z][A-Z][A-Z][^a-z]*$/s//.SH "&"\
.na\
.nf/
	p
	d
    }
    s/^'"$B"' *//
    s/^	//
    s/^[ 	]*$//
    /^\\"/d
    /^\./{
	s/\([^ ]\)-/\1\\-/g
    }
    /^[^.]/{
	s/-/\\-/g
    }
' $i
done | expand

exit

#++
# NAME
#	srctoman 1
# SUMMARY
#	extract manual page from source file comment
# PACKAGE
#	sdetools
# SYNOPSIS
#	srctoman [-|-awk|-c|-f|-mk|-m|-n|-p|-t|-r|-C] [section] file(s)
# DESCRIPTION
#	\fIsrctoman\fR converts comments in various programming languages to
#	UNIX-style manual pages.
#	The command processes comments in the style of newsrc(1);
#	its standard output is suitable for formatting with nroff(1) or 
#	troff(1) using the "-man" macro package.  
#	Typically, srctoman is invoked from make(1) scripts.
#
#	Source files are processed in the indicated order; if no
#	files are specified the command produces no output.
#
#	The source file language can be specified through a command-line
#	option, or can be implied by the filename suffix.
#	The expected start-of-comment symbol is shown in the last column.
#
# .nf
# .ft C
	option	language	comment

	-	shell		[:#]
	-awk	awk		#
	-c	c		/*
	-f	fortran		[Cc]
	-mk	make		#
	-n	nroff		\\"
	-p	pascal		{
	-t	troff		\\"
	-r	ratfor		#
	-C	any language	next argument
#
	suffix	language	comment

	.awk	awk		#
	.c	c		/*
	.f	fortran		[Cc]
	.fh	fortran		[Cc]
	.h	c		/*
	.l	lex		/*
	.man	nroff,troff	\\"
	.mk	make		#
	.me	nroff,troff	\\"
	.ms	nroff,troff	\\"
	.nr	nroff,troff	\\"
	.p	pascal		{
	.ph	pascal		{
	.r	ratfor		#
	.rh	ratfor		#
	.sh	shell		[:#]
	.y	yacc		/*
# .ft
# .PP
# .fi
#
#	The required format of comments is discussed below, where SOC
#	stands for the start-of-comment symbol of the language being used.
# .IP o
#	Start of manual: SOC, followed by `++'.
# .IP o
#	Section heading: SOC, blank, section name in upper case.
# .IP o
#	All other text: SOC and subsequent blanks or tabs are removed.
#	Lines that do not start with SOC are left unchanged (useful for 
#	inclusion of program text).
# .IP o
#	End of manual: SOC, followed by `--'.
#	An end-of-comment may follow if the source file language requires this.
# .PP
#	The following manual sections receive a special treatment:
#	NAME and SUMMARY should appear at the beginning and in
#	this order; DESCRIPTION, DIAGNOSTICS and BUGS will be
#	right-margin adjusted.
#	Other sections may be added freely without confusing srctoman.
# COMMANDS
#	sh(1), sed(1), expand(1)
# SEE ALSO
#	newsrc(1)
# DIAGNOSTICS
#	The program complains if an unknown language is specified
#	of if the language cannot be deduced from the file suffix.
# AUTHOR(S)
#	W.Z. Venema
#	Eindhoven University of Technology
#	Department of Mathematics and Computer Science
#	Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
# CREATION DATE
#	Fri Jan 17 22:59:27 MET 1986
# STATUS
#	srctoman.sh 1.2 11/4/89 15:56:22 (draft)
#--