Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Enable vgrind for C/C++ print formatting UNIX
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 \
% 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 | 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...

  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)

Enable vgrind for C/C++ print formatting | 5 comments | Create New Account
Click here to return to the 'Enable vgrind for C/C++ print formatting' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
How to use vgrind once it's working
Authored by: semios on Jul 26, '04 03:09:40PM
I've never used vgrind. I fumbled around a bit after trying this hint, so I figured a little demonstration might be useful. You can try it after you've made the link as the hint says.
$ vgrind mycode.c >
$ open;  # Open with Preview.

[ Reply to This | # ]
How to use vgrind once it's working
Authored by: semios on Jul 26, '04 03:10:57PM

Oops, I guess I didn't read the rest of the hint before commenting. :)

[ Reply to This | # ]
Authored by: sveinbjornt on Jul 27, '04 09:27:33AM
No need to download GhostScript. Mac OS X ships with the /usr/bin/pstopdf command line program, which also converts PostScript files to PDF. It is, amongst other things, used by Preview.

You can get a nifty command line Source-To-PDF converter by doing the following:

# sudo pico /bin/src2pdf

and type in the following:

vgrind $1 | pstopdf -i -o $1.pdf

then save it by doing Ctl-X and then change the permissions to 755. You then have the command line utility src2pdf, which takes a single source file as argument and generates a pdf file with the same name (but of course, the .pdf suffix).

[ Reply to This | # ]
Authored by: sjk on Jul 27, '04 03:43:29PM

/usr/local/bin would be a more preferable location for such a script. About the /bin directory, the hier(7) man page says:

user utilities fundamental to both single-user and multi-user environments

[ Reply to This | # ]
Authored by: sveinbjornt on Jul 31, '04 01:49:45PM
I've created a droplet called "SourceGrinder", using Platypus, which processes files by dragging and dropping in the Finder, using 'vgrind' and 'pstopdf' to convert source files to elegantly formatted PDF or PostScript files.

You can download it here:

[ Reply to This | # ]