Enable vgrind for C/C++ print formatting

Jul 26, '04 11:10:00AM

Contributed by: paco

MacOS X ships with a shell script called vgrind (in /usr/bin) which helps format and print C, C++, and other kinds of code. I find it really useful. However, it's broken as shipped with Mac OS X (Panther). Here's a quick fix.

The simplest fix is to find the tmac.vgrind file and put it in the right directory. As it is, the file exists but it's in the wrong place. So when vgrind invokes troff, it isn't found and vgrind fails. To fix it, run this command from a Terminal window:

% sudo ln /usr/share/groff/tmac/tmac.vgrind /usr/share/groff/1.18.1/tmac/
Although that will get vgrind up and going it's not very good on C++ as is. There's another file called vgrindefs that tells vgrind how to recognize the syntax of various languages. I find that FreeBSD's vgrindefs file is better. I recommend downloading that file and copying it to /usr/share/misc. Here's how to do it from a Terminal window:
% /usr/bin/ftp -o vgrindefs \
'http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/\
usr.bin/vgrind/vgrindefs.src?content-type=text/plain'
% sudo cp vgrindefs /usr/share/misc

Using vgrind
To use vgrind, simply provide one or more code files on the command line. It spits out Postscript on standard out, meaning that you have to capture that output to a file. Then you probably want to convert it to PDF so that you can view it with Preview and print it. I do it this way in a Terminal window:

% vgrind wtf.cc | ps2pdf - wtf.pdf
Now I can open wtf.pdf and print from Preview. Note that I used the ps2pdf command there. That's part of Ghostscript, which you can get easily using Fink Commander. In theory, Preview can convert Postscript to PDF, but I've never seen it do a credible job. It either takes forever or it just hangs.

An Example

If you're interested, here's an example of some input and output:

Things I like:
  1. Keyword bolding
  2. Proportionately spaced fonts. (I find it easier to read, but it's a matter of opinion)
  3. The current function name is printed in the right-hand margin, and appears there from page to page if a function is split across multiple pages.

This particular example illustrates a bug in GCC that I found on the gcc mailing list. I've duplicated this behavior on Intel Linux (Fedora Core 1), but on no other platform. FreeBSD/AMD, OpenBSD Intel, MacOS X PPC, Solaris Sparc, etc. None exhibit this floating point issue. But that's beside the point...

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040722111032193