LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2007, 01:39 AM   #1
duncan21
LQ Newbie
 
Registered: Mar 2007
Location: LA
Distribution: LFS
Posts: 4

Rep: Reputation: 0
What's a good way to embed C code into a man page?


For example, I'm looking to make a man page for the solution to an exercise from K&R about stripping comments from a C source file, and I would like to add a code example and have it show up as bold and offset. Using the .nf macro to insert the code as preformatted text seems pretty inelegant. The code sample I want to embed is in the EXAMPLES section of the man page.

Here's what the man page looks like, and following it is the source file (cstrip.1)

man cstrip
Code:
cstrip(1)                  K&R Programs                 cstrip(1)

NAME
       cstrip - a C-comment stripper

SYNOPSIS
       cstrip [-ahutV] [infile [outfile]]

DESCRIPTION
       cstrip  removes  comments  from  a C or a C++ source file.
       Note that C99 line comments (i.e., comments starting  with
       //)  are  stripped  by default. C89 processing can be done
       using the --ansi flag. Also, note that trigraph processing
       is  not supported by cstrip, and any source file that uses
       the trigraph ??/ will not be processed correctly.

       If infile is not specified, the C file is  read  from  the
       standard  input. If outfile is not specified, the stripped
       C file is written to the standard output.

COPYRIGHT
       cstrip is Copyright (C) 2007 by XXX

OPTIONS
       -a, --ansi
              Parse infile as ANSI C89, instead of the default of
              ISO C99/C++.

       -h, --help
              Print  a help message that briefly describes how to
              use the program.

       -t, --trigraphs
              Enable trigraph processing, which  is  disabled  by
              default.

       -u, --usage
              Prints  a  brief  usage  message and exits success-
              fully.

       -V, --version
              Prints information about the version  of  the  pro-
              gram, as well as license info.

EXAMPLES
       Note  that  using  ANSI C89 vs ISO C99 processing can very
       rarely affect the meaning of the code it is stripping com-
       ments  from. Thus, C89 stripping should never be used on a
       source file intended to  be  compiled  as  C99,  and  vice
       versa.  Consider the following (contrived) example:

            int  x = 8 //* pretty unlikely*/ 2
                   ;

       which, under C89 processing is translated to:

cstrip-1.1                April 19, 2007                        1

cstrip(1)                  K&R Programs                 cstrip(1)

            int  x = 8 /  2
                   ;

       under C99 processing, this example translates to:
            int  x = 8
                   ;

AUTHOR
       XXX <xxx@yyy.com>

SEE ALSO
       cpp(1)

BUGS
        trigraph ??/
              cstrip  will  not correctly strip comments from any
              file that contains the trigraph ??/ in normal  pro-
              gram code (i.e., outside of comments or string lit-
              erals). cpp should instead be used in this case.

cstrip-1.1                April 19, 2007                        2
cstrip.1
Code:
.TH "cstrip" 1 "April 19, 2007" "cstrip-1.1" "K&R Programs"
.SH NAME
cstrip \- a C-comment stripper
.SH SYNOPSIS
.B cstrip [-ahutV] [infile [outfile]]
.SH DESCRIPTION
cstrip removes comments from a C or a C++ source file. Note that C99 line
comments (i.e., comments starting with //) are stripped by default. C89
processing can be done using the --ansi flag. Also, note that trigraph
processing is not supported by cstrip, and any source file that uses the
trigraph ??/ will not be processed correctly.

If infile is not specified, the C file is read from the standard input. If
outfile is not specified, the stripped C file is written to the standard
output.
.SH COPYRIGHT
cstrip is Copyright (C) 2007 by XXX.
.SH OPTIONS
.TP
.B \-a, --ansi
Parse infile as ANSI C89, instead of the default of ISO C99/C++.
.TP
.B \-h, --help
Print a help message that briefly describes how to use the program.
.TP
.B \-t, --trigraphs
Enable trigraph processing, which is disabled by default.
.TP
.B \-u, --usage
Prints a brief usage message and exits successfully.
.TP
.B \-V, --version
Prints information about the version of the program, as well as license info.
.SH EXAMPLES
.PP
Note that using ANSI C89 vs ISO C99 processing can very rarely affect the
meaning of the code it is stripping comments from. Thus, C89 stripping should
never be used on a source file intended to be compiled as C99, and vice versa.
Consider the following (contrived) example:
.nf

     int  x = 8 //* pretty unlikely*/ 2
            ;

.fi
.PP
which, under C89 processing is translated to:
.nf
     int  x = 8 /  2
            ;

.fi
.PP
under C99 processing, this example translates to:
.nf
     int  x = 8
            ;

.fi
.SH AUTHOR
.B XXX    <xxx@yyy.com>
.SH "SEE ALSO"
.BR cpp(1)
.SH BUGS
.TP
.B \ trigraph ??/
cstrip will not correctly strip comments from any file that contains the
trigraph ??/ in normal program code (i.e., outside of comments or string
literals). cpp should instead be used in this case.
I know one part of my man page says I support trigraphs, and another doesn't. :
I'm working on that.

Last edited by duncan21; 04-21-2007 at 01:40 AM.
 
Old 04-21-2007, 06:38 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
The way I've always done it is to use left-justified code (at the margin) with tabs for the indents. Here's an example from a man page:
Code:
.IP
.ft CB
.nf
#!/bin/ksh
for file in *.unl
do
        #       get the name without the extension
        name=${file%%.unl}
        #       display what we're doing
        print "Working on ${name}..."
        #       execute dbload with appropriate arguments
        dbload -d bchahold -c ${name}.cmd -l ${name}.err -n 1000
done
.fi
.ft P
Just remember that the text is at the left margin with tabs used only for indenting the code example inside the .nf - .fi pair.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting out of a man page! naijaguy Linux - Newbie 11 04-10-2018 05:21 PM
man alsamixer not showing the man page nosaku Slackware 1 12-20-2004 08:52 AM
Is there a man page on how to use man page? jdruin Linux - Software 2 10-30-2004 09:29 AM
man page? chandan Linux - Newbie 1 07-16-2003 07:31 PM
Man Page Formatting nxny Linux - General 7 01-06-2003 07:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration