In addition to the customary UNIX text-based tool set, Mac OS X supports many UNIX programs that developers have ported to the Aqua user interface. This means you can use some of your favorite UNIX programs with new, Aqua-based interfaces.
4.4.1 UNIX-based editors
As you saw in the previous section, all the familiar UNIX editing tools are available under Mac OS X, including notable favorites like emacs and vi. Within the Mac OS X Aqua environment, you have several choices of non-UNIX, Aqua-based pro- gramming editors.
Project Builder editor
Let’s begin with the editor that comes with Project Builder. The Project Builder editor provides most of the basic editing features you would expect, as well as some advanced features such as emacs-style key-mappings, syntax highlighting, and indentation options. You can customize the editor’s behavior through the Project Builder Preferences dialog (located under Project Builder→Preferences) using the Text Editing, Syntax, and Indentation items (see figure 4.2). These options offer most of the common customization features you will require for basic editing tasks, but certainly do not offer the breadth of customization
options supported by UNIX editors like emacs. Let’s look at some of the more interesting options.
The Text Editing item enables you to set various editing options that affect how the editor treats, formats, and saves information:
■ Preserve Resource Forks—Permits you to save files with or without their resource fork. Because many Classic mode programs expect files to have resource forks, this option is useful if you are editing shared files from the Mac OS X and Classic environments.2
■ Line Endings—Useful if you are editing a set of files for a cross-platform project and you need to preserve file formats between platforms. These options are helpful if your code base and primary development environ- ment are UNIX, but you also plan to do development from the same code base under Mac OS X and Project Builder. In this case, you would set the For Existing Files menu item to Preserve, to ensure that Project Builder maintains the UNIX line endings.
The Syntax Coloring pane item permits you to set the font, colors, and styles Project Builder applies to a source file (see figure 4.3):
2 Not all Classic programs expect a resource fork, but many do. For example, the Codewarrior IDE and the BBEdit text editor use the resource fork for storing font and size information.
Figure 4.2 The Project Builder Preferences dialog enables you to set many customization options for Project Builder’s integrated editor.
■ Allow Separate Fonts—Enables the editor to display different fonts for dif- ferent language elements. Imagine you like string constants italicized. You select the Strings item from the pop-up menu (below the checkbox), check the Allow Separate Fonts checkbox to enable the Font text item (located at the bottom of the dialog box), and click Set. Then, select the appropriate font and typeface and click OK. The editor now displays all strings as italicized.
Deselecting the checkbox will remove the font and typeface highlighting.
Contrast this interface with emacs or vim, where you specify typeface styles and options in an initialization file. Doing this through an interface makes the job faster, but is not as extendable.
■ Show Colors When Printing—Prints source code listings with stylized fonts and font types. You can also get this functionality with the UNIX tool trueprint, but having the feature available with Project Builder is a real time saver.
Indentation options enable you to specify rules for how the editor indents your code (see figure 4.4). These rules are similar to emacs language modes and hooks:
Figure 4.3 You set font color and style options using Project Builder’s Syntax Coloring preferences.
■ Solo “{“ Indent—Holds the number of spaces the brace is indented.
■ Auto-Insert “}”—Automatically adds a closing brace after you type an open- ing brace. If you have ever tried to set up this functionality under emacs, you know it is a welcome feature.
■ Auto-Indent Characters—Indents the corresponding character if it is not entered at the correct indentation level.
Collectively, these editor options provide core functionality and should make you feel right at home within the editing environment.
One interesting consideration is how you know whether applying a set of for- matting options to your code has changed the behavior of the program. Granted, the formatting changes Project Builder applies are minor compared to such tools such as GNU indent, but it is still an important question. Intuitively, source code formatting should not alter the operation of a program; it simply reformats code by inserting whitespace into a source file. As Peter van der Linden’s excellent book Expert C Programming points out, this is not always the case.3 Nevertheless, how
3 Peter van der Linden, Expert C Programming, 10–11.
Figure 4.4 Source code indentation options are set under the Indentation preferences. These options are similar in spirit to the options available under most editors such as emacs and vi.
do you convince yourself this is true? One technique is to compare the binary file generated from the original source code with the binary file generated from the newly formatted code:
1 Compile the original program (before reformatting), reformat the code, and recompile the program to a different name.
2 Compare the two binaries using the cmp command:
cmp –l [first-file] [second-file]
The cmp command should not produce output if the files are the same. This method seems intuitively correct, but under some conditions it may not produce repeatable results. For example, some compilers contain enhancements that ran- domize the stack layout (insert random values into the stack) at compile time in an attempt to prevent buffer overflow attacks.4 In this case, there is no guarantee that compiling a program many times will produce the same object code each time.
NOTE Sometimes it is useful to view a binary file in hex. The program xxd can be used for this purpose. The following command will produce a text file (in hex) of the binary program:
% xxd binary-file > hex-file.txt
emacs and vi can also be used to generate hex files from binary programs.
Another approach is to compile each program to assembly and compare the assembly listings, first using diff and then manually if necessary. Yet another approach is to use regression testing to verify that reformatting has not changed the behavior of the program. This technique involves creating a regression test for the program: first the program behavior is baselined by running the regression test; then, after reformatting, you rerun the regression test. Because a regression test is probably already part of the project, no additional coding is necessary to verify reformatting. However, testing is only as sound as your test cases, and pro- viding complete test coverage is a difficult task.
4 StackGuard, now part of Immunix 7.0 (http://immunix.org), is an example of this technology. Stack- Guard was originally developed under a DARPA-funded Information Survivability research project at the Oregon Graduate Institute of Science & Technology.
External editors
Unfortunately, Project Builder does not currently support using external editors.
However, this should not stop you from exploring and using other Mac OS X editing tools for development. You can use any external editor to edit code, and use Project Builder to build and debug the software. If Project Builder detects the file has changed from the version on disk, it will automatically reload the file. This approach has limitations, but if you prefer another editor, it may be worth it.
4.4.2 Mac OS X-based editors
The Macintosh platform has always supported many fine editing tools, and Mac OS X is no exception. One of the most popular Macintosh programming editors is BBEdit, from Bare Bones Software (http://www.barebones.com). BBEdit is a great editor with a loyal following, it has an uncomplicated interface, and it is rock solid. It is available in two versions: the full-featured commercial product and a freeware version that contains a subset of the full version’s features. The freeware version does not contain features such as the HTML tools, Unix scripting and command integration, and extensible syntax coloring, to name a few, but it does include the core editing features, making it a very good choice as a free pro- gramming editor.
Another popular editor for the Macintosh is Alpha. Alpha is a very good editor that uses Tool Command Language (Tcl) as its extension language. Alpha supports language-based syntax highlighting and many other features for programming and general text editing operations; it’s also a favorite editor of users composing LaTeX documents on the Macintosh. The current (as of this writing) public release of Alpha runs only under the Classic environment, but by the time you read this it should be ported to Mac OS X. According to the Alpha developers, the Mac OS X version will add some new functionality, including direct integration with the sys- tem’s Tcl library, to provide better performance and upgradeability. In addition, it will support seamless editing of files on remote hosts.