Guide to Ghostscript source code

Table of contents

For other information, see the Ghostscript overview and the documents on how to build Ghostscript from source, Aladdin's C coding guidelines, drivers, the Ghostscript library and how to install Ghostscript.

Conceptual overview

The Ghostscript source code is divided conceptually as follows:

PostScript interpreter:
     PostScript operators      z*.h and z*.c
  Other interpreter code   i*.h and i*.c
  PostScript code   gs_*.ps
PDF interpreter:
  PostScript code   pdf_*.ps
Graphics library:
  Main library code   g*.h and g*.c
  Streams   s*.h and s*.c
  Device drivers   gdev*.h and gdev*.c
  Platform-specific code   gp*.h and gp*.c

PostScript Interpreter

gs.c is the main program for the interactive language interpreter; gserver.c is an alternative main program that is a rudimentary server. If you configure Ghostscript as a server rather than an interactive program, you will use gserver.c instead of gs.c.

Files named z*.c are Ghostscript operator files. The names of the files generally follow the section headings of the operator summary in section 6.2 of the PostScript Language Reference Manual. Each operator XXX is implemented by a procedure named zXXX, for example, zfill and zarray.

Files named i*.c, and *.h other than g*.h, are the rest of the interpreter. See the makefile for a little more information on how the files are divided functionally.

The main loop of the PostScript interpreter is the interp procedure in interp.c. When the interpreter is reading from an input file, it calls the token scanner in iscan*.c.

idebug.c contains a lot of debugger-callable routines useful for printing PostScript objects when debugging.

PDF interpreter

The PDF interpreter is written entirely in PostScript. Its main loop is the .pdfrun procedure in When the PDF interpreter is configured into the build, it redefines the "run" operator to test whether the file is a PDF file. This redefinition is near the beginning of

Graphics library

Files beginning with gs, gx, or gz (both .c and .h), other than gs.c and gserver.c, are the Ghostscript library. Files beginning with gdev are device drivers or related code, also part of the library. Other files beginning with g are library files that don't fall neatly into either the kernel or the driver category.

Files named s*.c and s*.h are a flexible stream package, including the Level 2 PostScript "filters" supported by Ghostscript. See stream.h, scommon.h, and strimpl.h for all the details.

Device drivers

The interface between the graphics library and device drivers is the only really well documented one in all of Ghostscript: see the documentation on drivers.

In addition to many real device and file format drivers listed in devs.mak and contrib.mak, a number of drivers are used for internal purposes. You can search lib.mak for files named gdev*.c to find almost all of them.

Drivers are divided into "printer" drivers, which support banding, and non-printer drivers, which don't. The decision whether banding is required is made (by default on the basis of how much memory is available) in the procedure gdev_prn_alloc in gdevprn.c: it implements this decision by filling the virtual procedure table for the printer device in one of two different ways.

A good simple "printer" (bandable) driver to read is gdevmiff.c: it's less than 100 lines, of which much is boilerplate. There are no simple non-printer drivers that actually drive devices: probably the simplest non-printer driver for reading is gdevm8.c, which implements 8-bit-deep devices that only store the bits in memory.

Platform-specific code

There are very few platform dependencies in Ghostscript. Ghostscript deals with them in three ways:

The platform-specific implementations of the gp*.h interfaces have names of the form "gp_{platform}.c, specifically (this list may be out of date):

Platform-specific interfaces
Routine      Platform

gp_dosfb.c   DOS
gp_dosfs.c   DOS and MS Windows
gp_itbc.c   DOS, Borland compilers
gp_iwatc.c   DOS, Watcom or Microsoft compiler
gp_msdos.c   DOS and MS Windows
gp_ntfs.c   MS-Windows Win32s and Windows NT
gp_os2.c   OS/2
gp_os9.c   OS-9
gp_unifs.c   Unix, OS-9, and QNX
gp_unix.c   Unix and QNX
gp_sysv.c   System V Unix
gp_vms.c   VMS
gp_win32.c   MS-Windows Win32s and Windows NT

If you are going to extend Ghostscript to new machines or operating systems, check the *_.h files for ifdef on things other than DEBUG. You should probably plan to make a new makefile and a new gp_XXX.c file.

Copyright © 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.

This file is part of GNU Ghostscript. See the GNU General Public License (the "License") for full details of the terms of using, copying, modifying, and redistributing GNU Ghostscript.

Ghostscript version 5.50, 16 September 1998