⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch35_04.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 35] 35.4 recomment: Clean Up Program Comment Blocks </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:48:00Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch35_01.htm"TITLE="35. You Can't Quite Call This Editing"><LINKREL="prev"HREF="ch35_03.htm"TITLE="35.3 Alternatives to fmt "><LINKREL="next"HREF="ch35_05.htm"TITLE="35.5 Remove Mail/News Headers with behead "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch35_03.htm"TITLE="35.3 Alternatives to fmt "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 35.3 Alternatives to fmt "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 35<BR>You Can't Quite Call This Editing</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch35_05.htm"TITLE="35.5 Remove Mail/News Headers with behead "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 35.5 Remove Mail/News Headers with behead "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-4530">35.4 recomment: Clean Up Program Comment Blocks </A></H2><PCLASS="para">Lines in a program's comment block usually start with one or morespecial characters, like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># <CODECLASS="replaceable"><I>line 1 of the comment</I></CODE># <CODECLASS="replaceable"><I>line 2 of the comment</I></CODE># <CODECLASS="replaceable"><I>line 3 of the comment</I></CODE>    ...</PRE></BLOCKQUOTE></P><PCLASS="para">&#13;It can be a hassle to add more text to one of the comment lines in a blockbecause the line can get too long, which requires you to fold thatline onto the next line, which means you have to work around theleading comment character(s).<ACLASS="indexterm"NAME="AUTOID-40227"></A></P><PCLASS="para">The<SPANCLASS="link"><EMCLASS="emphasis">fmt</EM> (<ACLASS="linkend"HREF="ch35_02.htm"TITLE="Neatening Text with fmt ">35.2</A>)</SPAN>program neatens lines of a text file.But the standard <EMCLASS="emphasis">fmt</EM> won't help you&quot;neaten&quot; blocks of comments in a program:it mixes the comment characters from the starts of lines with the words.(If your <EMCLASS="emphasis">fmt</EM> has the <EMCLASS="emphasis">-p</EM> option, it handles this problem;there's an example below.)The <EMCLASS="emphasis">recomment</EM> script is <EMCLASS="emphasis">fmt</EM> for comment blocks.It's for people who write shell, <EMCLASS="emphasis">awk</EM>, C, or almost anyother kind of program with comment blocks several lines long.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="recomment">recomment</A><BR></TH><TDVALIGN="TOP">&#13;<EMCLASS="emphasis">recomment</EM> reads the lines that you feed its standard input.It looks at the first line and figures out what characters you're usingto comment the line (see the <CODECLASS="literal">$cchars</CODE> variable for a list&nbsp;- typicallySPACEs, TABs, <CODECLASS="literal">#</CODE>, or <CODECLASS="literal">*</CODE>).Then, <EMCLASS="emphasis">recomment</EM> strips those comment charactersoff each line, feeds the remaining block of text to the <EMCLASS="emphasis">fmt</EM>utility, and uses<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>)</SPAN>to add the comment characters again.<ACLASS="indexterm"NAME="AUTOID-40249"></A></TD></TR></TABLE><PCLASS="para">I usually use <EMCLASS="emphasis">recomment</EM> from inside <EMCLASS="emphasis">vi</EM>, with<SPANCLASS="link">filter-through (<ACLASS="linkend"HREF="ch30_22.htm"TITLE="Filtering Text Through a UNIX Command ">30.22</A>)</SPAN>commands like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">!}recomment <ICLASS="lineannotation">reformat to the next blank line</I>5!!recomment <ICLASS="lineannotation">reformat this line and the next 4</I></PRE></BLOCKQUOTE></P><PCLASS="para">Normally, <EMCLASS="emphasis">recomment</EM> lets <EMCLASS="emphasis">fmt</EM> choose the width of the comment block(72 characters, typically).To get another width, you can either:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Give the width on the command line, like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">recomment -50</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">Set an environment variable named <EMCLASS="emphasis">CBLKWID</EM>.Give the maximum width, in characters, for the comment text.For example, in the C shell, use:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>setenv CBLKWID 50</B></CODE></PRE></BLOCKQUOTE></P></LI></UL><PCLASS="para"><EMCLASS="emphasis">recomment</EM> isn't perfect, but it's usually much better than nothing!Here's the part of the script that does the work.The first two commands get the comment character(s) and count theirlength.The next three commands strip the comment characters,clean up the remaining comment text, and add the same comment charactersto the start of all reformatted lines:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch45_29.htm"TITLE="45.29 Testing Characters in a String with expr ">expr</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"># GET COMMENT CHARACTERS USED ON FIRST LINE; STORE IN $comment:comment=&quot;`sed -n \&quot;1s/^\([$cchars]*\).*/\1/p\&quot; $temp`&quot;# GET NUMBER OF CHARACTERS IN COMMENT CHARACTER STRING:cwidth=`expr &quot;$comment&quot; : '.*'`# RE-FORMAT THE COMMENT BLOCK.  IF $widopt SET, USE IT:colrm 1 $cwidth &lt; $temp |       # STRIP OFF COMMENT LEADER FROM LINESfmt $widopt |                   # RE-FORMAT THE TEXT, ANDsed &quot;s/^/$comment/&quot;             # PUT THE COMMENT CHARACTERS BACK</PRE></TD></TR></TABLE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-40280"></A>If your system doesn't have the<SPANCLASS="link"><EMCLASS="emphasis">colrm</EM> (<ACLASS="linkend"HREF="ch35_15.htm"TITLE="Cutting Columns with colrm ">35.15</A>)</SPAN>utility,change thethird-to-last line to use<SPANCLASS="link"><EMCLASS="emphasis">cut</EM> (<ACLASS="linkend"HREF="ch35_14.htm"TITLE="Cutting Columns or Fields with cut ">35.14</A>)</SPAN>instead:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">cut -c`expr $cwidth + 1`- &lt; $temp |  # STRIP OFF COMMENT LEADER</PRE></BLOCKQUOTE></P><PCLASS="para">That makes a command like <CODECLASS="literal">cut&nbsp;-c4-</CODE> instead of <CODECLASS="literal">colrm&nbsp;1&nbsp;3</CODE>.</P><PCLASS="para">Some versions of <EMCLASS="emphasis">fmt</EM> (like the one on the CD-ROM) havea <EMCLASS="emphasis">-p</EM> option that does the same thing.Unlike the automatic system in <EMCLASS="emphasis">recomment</EM>, you have to tell<EMCLASS="emphasis">fmt&nbsp;-p</EM> what the prefix characters are.For example, here's the start of a C program.The prefix character is <CODECLASS="literal">*</CODE>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat prog.c</B></CODE>/* * This file, load.cc, reads an input  * data file.  * Each input line is added to a new node * of type struct Node. */% <CODECLASS="userinput"><B>fmt -p '*' prog.c</B></CODE>/* * This file, load.cc, reads an input data file.  Each input line is * added to a new node of type struct Node. */</PRE></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch35_03.htm"TITLE="35.3 Alternatives to fmt "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 35.3 Alternatives to fmt "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch35_05.htm"TITLE="35.5 Remove Mail/News Headers with behead "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 35.5 Remove Mail/News Headers with behead "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">35.3 Alternatives to fmt </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">35.5 Remove Mail/News Headers with behead </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -