C-style.htm 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Ghostscript C coding guidelines</title>
  5. <!-- $Id: C-style.htm,v 1.21.2.2 2002/02/01 05:31:24 raph Exp $ -->
  6. <!-- Originally: c-style.txt -->
  7. <link rel="stylesheet" type="text/css" href="gs.css" title="Ghostscript Style">
  8. </head>
  9. <body>
  10. <!-- [1.0 begin visible header] ============================================ -->
  11. <!-- [1.1 begin headline] ================================================== -->
  12. <h1>Ghostscript C coding guidelines</h1>
  13. <!-- [1.1 end headline] ==================================================== -->
  14. <!-- [1.2 begin table of contents] ========================================= -->
  15. <h2>Table of contents</h2>
  16. <blockquote><ul>
  17. <li><a href="#Introduction">Introduction</a>
  18. <li><a href="#C_language">C language do's and don'ts</a>
  19. <ul>
  20. <li>Preprocessor:
  21. <a href="#Conditionals">Conditionals</a>,
  22. <a href="#Macros">Macros</a>,
  23. <a href="#Preprocessor_other">Other</a>
  24. <li><a href="#Lexical_elements">Lexical elements</a>
  25. <li><a href="#Scoping">Scoping</a>
  26. <li>Data types:
  27. <a href="#Scalars">Scalars</a>,
  28. <a href="#Arrays">Arrays</a>,
  29. <a href="#Typedefs">Typedefs</a>,
  30. <a href="#Structures">Structures</a>,
  31. <a href="#Unions">Unions</a>
  32. <li><a href="#Expressions">Expressions</a>
  33. <li><a href="#Statements">Statements</a>
  34. <li><a href="#Procedures">Procedures</a> (prototypes and definitions)
  35. <li><a href="#Standard_library">Standard library</a>
  36. </ul>
  37. <li><a href="#Language_extensions">Language extensions</a>
  38. <li><a href="#Stylistic_conventions">Stylistic conventions</a>
  39. <ul>
  40. <li>Formatting:
  41. <a href="#Indentation">Indentation</a>,
  42. <a href="#Spaces">Spaces</a>,
  43. <a href="#Parentheses">Parentheses</a>
  44. <li><a href="#Preprocessor_style">Preprocessor</a>
  45. <li><a href="#Naming">Naming</a>
  46. <li><a href="#Types">Types</a>
  47. <li><a href="#Procedures_style">Procedures</a>,
  48. <li>Miscellany:
  49. <a href="#Local_variables">Local variables</a>,
  50. <a href="#Compiler_warnings">Compiler warnings</a>
  51. </ul>
  52. <li><a href="#File_structuring">File structuring and naming</a>
  53. <ul>
  54. <li><a href="#All_files">All files</a>
  55. <li><a href="#Makefiles">Makefiles</a>
  56. <li><a href="#General_C_code">General C Code</a>
  57. <li><a href="#Headers">Headers (<b><tt>.h</tt></b> files)</a>
  58. <li><a href="#Source">Source (<b><tt>.c</tt></b> files)</a>
  59. </ul>
  60. <li><a href="#Conventions">Ghostscript conventions</a>
  61. <ul>
  62. <li><a href="#Specific_names">Specific names</a>:
  63. <a href="#code"><b><tt>code</tt></b></a>,
  64. <a href="#status"><b><tt>status</tt></b></a>
  65. <li><a href="#Structure_type_descriptors">Structure type descriptors</a>
  66. <li><a href="#Objects">"Objects"</a>
  67. <li><a href="#Error_handling">Error handling</a>
  68. </ul>
  69. </ul></blockquote>
  70. <!-- [1.2 end table of contents] =========================================== -->
  71. <!-- [1.3 begin hint] ====================================================== -->
  72. <p>
  73. For other information, see the <a href="Readme.htm">Ghostscript
  74. overview</a>.
  75. <!-- [1.3 end hint] ======================================================== -->
  76. <hr>
  77. <!-- [1.0 end visible header] ============================================== -->
  78. <!-- [2.0 begin contents] ================================================== -->
  79. <h2><a name="Introduction"></a>Introduction</h2>
  80. <p>
  81. This document describes Ghostscript's C coding conventions. It is primarily
  82. <em>prescriptive</em>, documenting what developers should do when writing
  83. new code; the companion developer documentation (<a
  84. href="Develop.htm">Develop.htm</a>) is primarily <em>descriptive</em>,
  85. documenting the way things are.
  86. <p>
  87. We encourage following the general language usage and stylistic rules for
  88. any code that will be integrated with Ghostscript, even if the code doesn't
  89. use Ghostscript's run-time facilities or have anything to do with
  90. PostScript, PDF, or page description languages. Ghostscript itself follows
  91. some additional conventions; these are documented separately under "<a
  92. href="#Conventions">Ghostscript conventions</a>" below.
  93. <hr>
  94. <h2><a name="C_language"></a>C language do's and don'ts</h2>
  95. <p>
  96. There are several different versions of the C language, and even of the ANSI
  97. C standard. Ghostscript versions through 7.0 were designed to compile on
  98. pre-ANSI compilers as well as on many compilers that implemented the ANSI
  99. standard with varying faithfulness. Ghostscript versions since 7.0 do not
  100. cater for pre-ANSI compilers: they must conform to the ANSI 1989 standard
  101. (ANS X3.159-1989), with certain restrictions and a few conventional
  102. additions.
  103. <h3>Preprocessor</h3>
  104. <h4><a name="Conditionals"></a>Conditionals</h4>
  105. Restrictions:
  106. <ul>
  107. <li>Don't assume that <b><tt>#if</tt></b> will treat undefined names as 0.
  108. While the ANSI standard requires this, it may produce a warning.
  109. <li>In <b><tt>.c</tt></b> files, don't use preprocessor conditionals that
  110. test for individual platforms or compilers. Use them only in header files
  111. named xxx<b><tt>_.h</tt></b>.
  112. </ul>
  113. <h4><a name="Macros"></a>Macros</h4>
  114. <p>
  115. Restrictions:
  116. <ul>
  117. <li>Don't redefine a macro, even with the same definition, without using
  118. <b><tt>#undef</tt></b>.
  119. <li><b><tt>CAPITALIZE</tt></b> macro names unless there is a good reason not
  120. to.
  121. <li>Don't use a macro call within a macro argument if the call expands to a
  122. token sequence that includes any commas not within parentheses: this
  123. produces different results depending on whether the compiler expands the
  124. inner call before or after the argument is substituted into the macro body.
  125. (The ANSI standard says that calls must be expanded after substitution, but
  126. some compilers do it the other way.)
  127. <li>Don't use macro names, even inadvertently, in string constants. Some
  128. compilers erroneously try to expand them.
  129. <li>Don't use macros to define shorthands for casted pointers. For
  130. instance, avoid
  131. <blockquote><b><tt>
  132. #define fdev ((gx_device_fubar *)dev)
  133. </tt></b></blockquote>
  134. <p>
  135. and instead use
  136. <blockquote><b><tt>
  137. gx_device_fubar * const fdev = (gx_device_fubar *)dev;
  138. </tt></b></blockquote>
  139. <p>
  140. The use of <b><tt>const</tt></b> alerts the reader that this is effectively
  141. a synonym.
  142. <li>If a macro generates anything larger than a single expression (that is,
  143. one or more statements), surround it with <b><tt>BEGIN</tt></b> and
  144. <b><tt>END</tt></b>. These work around the fact that simple statements and
  145. compound statements in C can't be substituted for each other syntactically.
  146. </ul>
  147. <h3><a name="Preprocessor_other"></a>Other</h3>
  148. <p>
  149. Restrictions:
  150. <ul>
  151. <li>Only use <b><tt>#pragma</tt></b> in files that are explicitly identified
  152. as being platform-dependent. Many compilers complain if this is used at
  153. all, and some complain if they don't recognize the specific pragma being
  154. requested (both incorrect according to the ANSI standard).
  155. </ul>
  156. <h3><a name="Lexical_elements"></a>Lexical elements</h3>
  157. <p>
  158. Do not use:
  159. <ul>
  160. <li>ANSI trigraphs (??x)
  161. <li>Nested comments (/* /* */ */) (not ANSI compliant, but often accepted)
  162. <li>Multi-character character constants ('abc')
  163. <li>Wide-character character or string constants (L'x', L"x")
  164. </ul>
  165. <p>
  166. Restrictions:
  167. <ul>
  168. <li>Procedure and static variable names must be 31 characters or less.
  169. <li>Externally visible procedure and variable names must be unique in the
  170. first 23 characters.
  171. </ul>
  172. <h3><a name="Scoping"></a>Scoping (extern, static, ...)</h3>
  173. <p>
  174. Do not use:
  175. <ul>
  176. <li><b><tt>register</tt></b>
  177. </ul>
  178. <p>
  179. Restrictions:
  180. <ul>
  181. <li>Do not allow a global variable (constant) to have more than one
  182. non-<b><tt>extern</tt></b> definition, even though some ANSI C compilers
  183. allow this. Every global constant should have exactly one definition, in a
  184. <b><tt>.c</tt></b> file, and preferably just one <b><tt>extern</tt></b>
  185. declaration, in a header file.
  186. <li>Use <b><tt>private</tt></b> instead of <b><tt>static</tt></b> for
  187. procedures and variables declared at the outermost scope of a file. This
  188. allows making such constructs either visible or invisible to profilers by
  189. changing a single <b><tt>#define</tt></b>.
  190. <li><b><tt>static</tt></b> or <b><tt>private</tt></b> variables must be
  191. <b><tt>const</tt></b> and initialized: non-<b><tt>const</tt></b> statically
  192. allocated variables are incompatible with reentrancy, and we're in the
  193. process of eliminating all of them.
  194. <li>Do not use <b><tt>extern</tt></b> in <b><tt>.c</tt></b> files, only in
  195. <b><tt>.h</tt></b> files, unless you have a very good reason for it (e.g.,
  196. as in <a href="../src/iconf.c">iconf.c</a>). There are too many such
  197. <b><tt>extern</tt></b>s in the code now: we are eliminating them over time.
  198. <li>Do not declare the same name as both <b><tt>static</tt></b>
  199. (<b><tt>private</tt></b>) and non-<b><tt>static</tt></b> within the same
  200. compilation. (Some compilers complain, some do not.) This is especially a
  201. problem for procedures: it is easy to declare a procedure as
  202. <b><tt>private</tt></b> near the beginning of a file and accidentally not
  203. declare it <b><tt>private</tt></b> where it is defined later in the file.
  204. <li>Even though the ANSI standard allows initialized external declarations
  205. (<b><tt>extern&nbsp;int&nbsp;x&nbsp;=&nbsp;0</tt></b>), don't use them.
  206. </ul>
  207. <h3><a name="Scalars"></a>Scalars</h3>
  208. <p>
  209. Restrictions:
  210. <ul>
  211. <li>Avoid using <b><tt>char</tt></b>, except for <b><tt>char&nbsp;*</tt></b>
  212. for a pointer to a string. Don't assume that <b><tt>char</tt></b> is
  213. signed; also don't assume it is unsigned.
  214. <li>Never cast a <b><tt>float</tt></b> to a <b><tt>double</tt></b>
  215. explicitly. ANSI compilers in their default mode do all floating point
  216. computations in double precision, and handle such casts automatically.
  217. <li>Don't use <b><tt>long long</tt></b>: even though it is in the ANSI
  218. standard, not all compilers support it. Use <b><tt>bits64</tt></b> instead
  219. (see below under "<a href="#Language_extensions">Language extensions</a>").
  220. <li>Don't assume anything about whether <b><tt>sizeof(long)</tt></b> is less
  221. than, equal to, or greater than <b><tt>sizeof(ptr)</tt></b>. (However, you
  222. can make such comparisons in preprocessor conditionals using
  223. <b><tt>ARCH_SIZEOF_LONG</tt></b> and <b><tt>ARCH_SIZEOF_PTR</tt></b>.)
  224. </ul>
  225. <h3><a name="Arrays"></a>Arrays</h3>
  226. <p>
  227. Restrictions:
  228. <ul>
  229. <li>Don't declare arrays of size 0. (The newer ANSI standard allows this,
  230. but the older one doesn't.)
  231. <li>Don't declare an array of size 1 at the end of a structure to indicate
  232. that a variable-size array follows.
  233. <li>Don't declare initialized <b><tt>auto</tt></b> arrays.
  234. </ul>
  235. <h3><a name="Typedefs"></a>Typedefs</h3>
  236. <p>
  237. Restrictions:
  238. <ul>
  239. <li>Don't use <b><tt>typedef</tt></b> for function types, such as
  240. <blockquote>
  241. <b><tt>typedef int proc_xyz_t(double, int *);</tt></b>
  242. </blockquote>
  243. <p>Many compilers don't handle this correctly -- they will give errors, or
  244. do the wrong thing, when declaring variables of type
  245. <b><tt>proc_xyz_t</tt></b> and/or <b><tt>proc_xyz_t *</tt></b>. Instead, do
  246. this:
  247. <blockquote>
  248. <b><tt>#define PROC_XYZ(proc) int proc(double, int *)<br>
  249. PROC_XYZ(some_proc); /* declare a procedure of this type */<br>
  250. typedef PROC_XYZ((*proc_xyz_ptr_t)); /* define a type for procedure ptrs */<br>
  251. <br>
  252. proc_xyz_ptr_t pp; /* pointer to procedure */</tt></b>
  253. </blockquote>
  254. <li>Don't redefine <b><tt>typedef</tt></b>'ed names, even with the same
  255. definition. Some compilers complain about this, and the standard doesn't
  256. allow it.
  257. </ul>
  258. <h3><a name="Structures"></a>Structures</h3>
  259. <p>
  260. Restrictions:
  261. <li>Don't use anonymous structures if you can possibly avoid it, except
  262. occasionally as components of other structures. Ideally, use the
  263. <b><tt>struct</tt></b> keyword only for declaring named structure types,
  264. like this:
  265. <blockquote>
  266. <b><tt>typedef struct xxx_s {</tt></b><br>
  267. &nbsp;&nbsp;&nbsp;... members ...<br>
  268. <b><tt>} xxx_t;</tt></b>
  269. </blockquote>
  270. <li>Use <b><tt>struct</tt></b> only when declaring structure types, never
  271. for referring to them (e.g., never declare a variable as type
  272. <b><tt>struct&nbsp;xxx_s&nbsp;*</tt></b>).
  273. <li>Don't assume that the compiler will (or won't) insert padding in
  274. structures to align members for best performance. To preserve alignment,
  275. only declare structure members that are narrower than an <b><tt>int</tt></b>
  276. if there will be a lot of instances of that structure in memory. For such
  277. structures, insert <b><tt>byte</tt></b> and/or <b><tt>short</tt></b> padding
  278. members as necessary to re-establish <b><tt>int</tt></b> alignment.
  279. <li>Don't declare initialized <b><tt>auto</tt></b> structures.
  280. </ul>
  281. <h3><a name="Unions"></a>Unions</h3>
  282. <p>
  283. Restrictions:
  284. <ul>
  285. <li>Use unions only as components of structures, not as typedefs in their
  286. own right.
  287. <li>Don't attempt to initialize unions: not all compilers support this, even
  288. though it is in the 1989 ANSI standard.
  289. </ul>
  290. <h3><a name="Expressions"></a>Expressions</h3>
  291. <p>
  292. Restrictions:
  293. <ul>
  294. <li>Don't assign a larger integer data type to a smaller one without a cast
  295. (<b><tt>int_x&nbsp;=&nbsp;long_y</tt></b>).
  296. <li>It's OK to use the address of a structure or array element
  297. (<b><tt>&p->e</tt></b>, <b><tt>&a[i]</tt></b>) locally, or pass it to a
  298. procedure that won't store it, but don't store such an address in allocated
  299. storage unless you're very sure of what you're doing.
  300. <li>Don't use conditional expressions with structure or union values.
  301. (Pointers to structures or unions are OK.)
  302. <li>For calling a variable or parameter procedure, use
  303. <b><tt>ptr-&gt;func(...)</tt></b>. Some old code uses explicit indirection,
  304. <b><tt>(*ptr-&gt;func)(...)</tt></b>: don't use this in new code.
  305. <li>Don't write expressions that depend on order of evaluation, unless the
  306. order is created explicitly by use of <b><tt>||</tt></b>,
  307. <b><tt>&amp;&amp;</tt></b>, <b><tt>?:</tt></b>, <b><tt>,</tt></b>, or
  308. function nesting (the arguments of a function must be evaluated before the
  309. function is called). In particular, don't assume that the arguments of a
  310. function will be evaluated left-to-right, or that the left side of an
  311. assignment will be evaluated before the right.
  312. <li>Don't mix integer and enumerated types ad lib: treat enumerated types as
  313. distinct from integer types, and use casts to convert between the two.
  314. (Some compilers generate warnings if you do not do this.)
  315. </ul>
  316. <h3><a name="Statements"></a>Statements</h3>
  317. <p>
  318. Restrictions:
  319. <ul>
  320. <li>If you use an expression as a statement, other than an assignment or a
  321. function call with <b><tt>void</tt></b> return value, enclose it explicitly
  322. in <b><tt>DISCARD()</tt></b>.
  323. <li>The type of the operand of a <b><tt>switch</tt></b> must match the type
  324. of the case labels, whether the labels are <b><tt>int</tt></b>s or the
  325. members of an <b><tt>enum</tt></b> type. (Use a cast if necessary.)
  326. <li>It is OK for one case of a switch to "fall through" into another (i.e.,
  327. for the statement just before a case label not to be a control transfer),
  328. but a comment <b><tt>/*&nbsp;falls&nbsp;through&nbsp;*/</tt></b> is
  329. required.
  330. <li>If you are returning an error code specified explicitly (e.g.,
  331. <b><tt>return&nbsp;gs_error_rangecheck</tt></b> or
  332. <b><tt>return&nbsp;e_rangecheck</tt></b>), use
  333. <b><tt>return_error()</tt></b> rather than plain <b><tt>return</tt></b>.
  334. However, if the program is simply propagating an error code generated
  335. elsewhere, as opposed to generating the error, use <b><tt>return</tt></b>
  336. (e.g., <b><tt>if&nbsp;(code&nbsp;<&nbsp;0)&nbsp;return&nbsp;code</tt></b>).
  337. </ul>
  338. <h3><a name="Procedures"></a>Procedures</h3>
  339. <p>
  340. Restrictions:
  341. <ul>
  342. <li>Provide a prototype for every procedure, and make sure the prototype is
  343. available at every call site. If the procedure is local to a file
  344. (<b><tt>private</tt></b>), the prototype should precede the procedure, in
  345. the same file; if the procedure is global, the prototype should be in a
  346. header file.
  347. <li>If a procedure parameter is itself a procedure, do list its parameter
  348. types rather than just using <b><tt>()</tt></b>. For example,
  349. <blockquote><b><tt>
  350. int foo(int (*callback)(int, int));
  351. </tt></b></blockquote>
  352. <p>
  353. rather than just
  354. <blockquote><b><tt>
  355. int foo(int (*callback)());
  356. </tt></b></blockquote>
  357. <li>Don't use the <b><tt>P</tt></b>* macros in new code. (See the
  358. Procedures section of <a href="#Language_extensions">Language extensions</a>
  359. below for more information.)
  360. <li>Always provide an explicit return type for procedures, in both the
  361. prototype and the definition: don't rely on the implicit declaration as
  362. <b><tt>int</tt></b>.
  363. <li>Don't use <b><tt>float</tt></b> as the return type of a procedure,
  364. unless there's a special reason. Floating point hardware typically does
  365. everything in double precision internally and has to do extra work to
  366. convert between double and single precision.
  367. <li>Don't declare parameters as being of type <b><tt>float</tt></b>,
  368. <b><tt>short</tt></b>, or <b><tt>char</tt></b>. If you do this and forget
  369. to include the prototype at a call site, ANSI compilers will generate
  370. incompatible calling sequences. Use <b><tt>floatp</tt></b> (a synonym for
  371. <b><tt>double</tt></b>, mnemonic for "float parameter") instead of
  372. <b><tt>float</tt></b>, and use <b><tt>int</tt></b> or <b><tt>uint</tt></b>
  373. instead of <b><tt>short</tt></b> or <b><tt>char</tt></b>.
  374. </ul>
  375. <h3><a name="Standard_library"></a>Standard library</h3>
  376. <p>
  377. Restrictions:
  378. <ul>
  379. <li>Only use library features that are documented in the established ANSI
  380. standard (e.g., Harbison & Steele's book). Do not use procedures that are
  381. "standards" promulgated by Microsoft (e.g., <b><tt>stricmp</tt></b>), or
  382. originate in BSD Unix (e.g., <b><tt>strcasecmp</tt></b>), or were added in
  383. later versions of the standard such as C 9X.
  384. <li>Do not use any features from <b><tt>stdio.h</tt></b> that assume the
  385. existence of <b><tt>stdin</tt></b>, <b><tt>stdout</tt></b>, or
  386. <b><tt>stderr</tt></b>. See <a href="../src/gsio.h">gsio.h</a> for the full
  387. list. Instead, use <b><tt>gs_stdin</tt></b> et al.
  388. </ul>
  389. <hr>
  390. <h2><a name="Language_extensions"></a>Language extensions</h2>
  391. <h3>Scoping</h3>
  392. <dl>
  393. <dt><b><tt>inline</tt></b>
  394. <dd><b><tt>inline</tt></b> is available even if the compiler does not
  395. support it. Be aware, however, that it may have no effect. In particular,
  396. do not use <b><tt>inline</tt></b> in header files. Instead, use the
  397. <b><tt>extern_inline</tt></b> facility described just below.
  398. <dt><b><tt>extern_inline</tt></b>
  399. <dd>Compilers that do support <b><tt>inline</tt></b> vary in how they decide
  400. whether to (also) compile a closed-code copy of the procedure. Because of
  401. this, putting an <b><tt>inline</tt></b> procedure in a header file may
  402. produce multiple closed copies, causing duplicate name errors at link time.
  403. <b><tt>extern_inline</tt></b> provides a safe way to put
  404. <b><tt>inline</tt></b> procedures in header files, regardless of compiler.
  405. Unfortunately, the only way we've found to make this fully portable involves
  406. a fair amount of boilerplate. For details, please see <a
  407. href="../src/stdpre.h">stdpre.h</a>.
  408. <dt><b><tt>private</tt></b>
  409. <dd><b>Use <tt>private</tt></b> instead of <b><tt>static</tt></b> for all
  410. file-local procedures, and also for file-local variables defined at the
  411. outermost level. However, use <b><tt>static</tt></b>, not
  412. <b><tt>private</tt></b>, for variables defined within a procedure.
  413. <p>
  414. <b><tt>private</tt></b> is normally #define'd as <b><tt>static</tt></b>.
  415. However, it can also be #define'd as empty, which allows profilers to
  416. measure all procedures, and 'nm' to list all interesting statically
  417. allocated variables, not just public ones.
  418. </dl>
  419. <h3>Scalar types</h3>
  420. <dl>
  421. <dt><b><tt>bool, true, false</tt></b>
  422. <dd><b><tt>bool</tt></b> is intended as a Boolean type, with canonical
  423. values <b><tt>true</tt></b> and <b><tt>false</tt></b>. In a more reasonable
  424. language, such as Java, <b><tt>bool</tt></b> is an enumerated type requiring
  425. an explicit cast to or from <b><tt>int</tt></b>; however, because C's
  426. conditionals are defined as producing <b><tt>int</tt></b> values, we can't
  427. even define <b><tt>bool</tt></b> as a C <b><tt>enum</tt></b> without
  428. provoking compiler warnings.
  429. <p>
  430. Even though <b><tt>bool</tt></b> is a synonym for <b><tt>int</tt></b>, treat
  431. them as conceptually different types:
  432. <ul>
  433. <li>Initialize or set <b><tt>bool</tt></b> variables to <b><tt>true</tt></b>
  434. or <b><tt>false</tt></b>, not 0 or 1.
  435. <li>Use the Boolean operators <b><tt>!</tt></b>, <b><tt>&&</tt></b>,
  436. and <b><tt>||</tt></b> only with Booleans. Don't use the idiom
  437. <b><tt>!!x</tt></b> to create a Boolean that is true iff <b><tt>x</tt></b>
  438. != 0: use <b><tt>x != 0</tt></b>.
  439. <li>Use an explicit <b><tt>(int)</tt></b> cast to convert a Boolean to an
  440. integer.
  441. </ul>
  442. <dt><b><tt>byte, ushort, uint, ulong</tt></b>
  443. <dd>These types are simply shorthands for <b><tt>unsigned char, short, int,
  444. long</tt></b>.
  445. <dt><b><tt>floatp</tt></b>
  446. <dd>This is a synonym for <b><tt>double</tt></b>. It should be used for,
  447. and only for, procedure parameters that would otherwise be
  448. <b><tt>float</tt></b>. (As noted above, procedure parameters should not be
  449. declared as <b><tt>float</tt></b>.)
  450. <dt><b><tt>bits8, bits16, bits32</tt></b>
  451. <dd>These are unsigned integer types of the given width. Use them wherever
  452. the actual width matters: do <em>not</em>, for example, use
  453. <b><tt>short</tt></b> assuming that it is 16 bits wide.
  454. <dt><b><tt>bits64</tt></b>
  455. <dd><strong>****** NOT IMPLEMENTED YET ******</strong>
  456. This is an unsigned 64-bit integer type, but it may not be available on all
  457. platforms. Any code that uses this type should be surrounded by
  458. <b><tt>#if&nbsp;ARCH_HAS_BITS64</tt></b>.
  459. </dl>
  460. <h3>Procedures</h3>
  461. <dl>
  462. <dt><b><tt>P0(), P1(p1), ..., P16(p1, ..., p16)</tt></b> [deprecated]
  463. <dd>Nearly all existing code uses these macros for the parameter lists of
  464. procedure prototypes, e.g.,
  465. <b><tt>int&nbsp;proc(P2(int&nbsp;a,&nbsp;int&nbsp;b))</tt></b>, to provide
  466. compatibility with pre-ANSI, a.k.a. "traditional" or "K &amp; R", compilers.
  467. Since only ANSI compilers are now supported, don't use these macros in new
  468. code: use the ANSI standard syntax
  469. <b><tt>int&nbsp;proc(int&nbsp;a,&nbsp;int&nbsp;b)</tt></b>.
  470. </dl>
  471. <hr>
  472. <h2><a name="Stylistic_conventions"></a>Stylistic conventions</h2>
  473. <p>
  474. Ghostscript's coding rules cover not only the use of the language, but also
  475. many stylistic issues like the choice of names and the use of whitespace.
  476. The stylistic rules are meant to produce code that is easy to read. It's
  477. important to observe them as much as possible in order to maintain a
  478. consistent style, but if you find these rules getting in your way or
  479. producing ugly-looking results once in a while, it's OK to break it.
  480. <h3><a name="Formatting"></a>Formatting</h3>
  481. <h4><a name="Indentation"></a>Indentation</h4>
  482. <p>
  483. We've formatted all of our code using the GNU <b><tt>indent</tt></b> program.
  484. <blockquote><b><tt>
  485. indent&nbsp;-bad&nbsp;-nbap&nbsp;-nsob&nbsp;-br&nbsp;-ce&nbsp;-cli4&nbsp;-npcs&nbsp;-ncs&nbsp;\<br>
  486. &nbsp;&nbsp;&nbsp;-i4&nbsp;-di0&nbsp;-psl&nbsp;-lp&nbsp;-lps&nbsp;somefile.c
  487. </tt></b></blockquote>
  488. <p>
  489. does a 98% accurate job of producing our preferred style. Unfortunately,
  490. there are bugs in all versions of GNU <b><tt>indent</tt></b>, requiring
  491. both pre- and post-processing of the code. The <b><tt>gsindent</tt></b>
  492. script in the Ghostscript fileset contains the necessary workarounds.
  493. <p>
  494. Put indentation points every 4 spaces, with 8 spaces = 1 tab stop.
  495. <p>
  496. For assignments (including chain assignments), put the entire statement on
  497. one line if it will fit; if not, break it after a <b><tt>=</tt></b> and
  498. indent all the following lines. I.e., format like this:
  499. <blockquote>
  500. var1&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
  501. var1&nbsp;<b><tt>=</tt></b>&nbsp;var2&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
  502. var1&nbsp;<b><tt>=</tt></b><br>
  503. &nbsp;&nbsp;&nbsp;&nbsp;value<b><tt>;</tt></b><br>
  504. var1&nbsp;<b><tt>=</tt></b><br>
  505. &nbsp;&nbsp;&nbsp;&nbsp;var2&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
  506. var1&nbsp;<b><tt>=</tt></b>&nbsp;var2&nbsp;<b><tt>=</tt></b><br>
  507. &nbsp;&nbsp;&nbsp;&nbsp;value<b><tt>;</tt></b>
  508. </blockquote>
  509. <p>
  510. But not like this:
  511. <blockquote>
  512. var1&nbsp;<b><tt>=</tt></b><br>
  513. var2&nbsp;<b><tt>=</tt></b> value<b><tt>;</tt></b>
  514. </blockquote>
  515. <p>
  516. Indent in-line blocks thus:
  517. <blockquote>
  518. <b><tt>{</tt></b><br>
  519. &nbsp;&nbsp;&nbsp;... declarations ...<br>
  520. &nbsp;&nbsp;&nbsp;{{ blank line if any declarations above }}<br>
  521. &nbsp;&nbsp;&nbsp;... statements ...<br>
  522. <b><tt>}</tt></b>
  523. </blockquote>
  524. <p>
  525. Similarly, indent procedures thus:
  526. <blockquote>
  527. return_type<br>
  528. proc_name(... arguments ...)<br>
  529. <b><tt>{</tt></b><br>
  530. &nbsp;&nbsp;&nbsp;... declarations ...<br>
  531. &nbsp;&nbsp;&nbsp;{{ blank line if any declarations above }}<br>
  532. &nbsp;&nbsp;&nbsp;... statements ...<br>
  533. <b><tt>}</tt></b>
  534. </blockquote>
  535. <p>
  536. If a control construct (<b><tt>if</tt></b>, <b><tt>do</tt></b>,
  537. <b><tt>while</tt></b>, or <b><tt>for</tt></b>) has a one-line body, use
  538. this:
  539. <blockquote>
  540. ... control construct ...<br>
  541. &nbsp;&nbsp;&nbsp;... subordinate simple statement ...
  542. </blockquote>
  543. <p>
  544. If it has a multi-line body, use this:
  545. <blockquote>
  546. ... control construct ... <b><tt>{</tt></b><br>
  547. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  548. <b><tt>}</tt></b>
  549. </blockquote>
  550. <p>
  551. If the subordinate code has declarations, see blocks above.
  552. <p>
  553. For if-else statements, do this:
  554. <blockquote>
  555. <b><tt>if (</tt></b> ...<b><tt> ) {</tt></b><br>
  556. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  557. <b><tt>} else if (</tt></b> ...<b><tt> ) {</tt></b><br>
  558. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  559. <b><tt>} else {</tt></b><br>
  560. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  561. <b><tt>}</tt></b>
  562. </blockquote>
  563. <p>
  564. When there are more than two alternatives, as in the example above, use the
  565. above ("parallel") syntax rather than the following ("nested") syntax:
  566. <blockquote>
  567. <b><tt>if (</tt></b> ...<b><tt> ) {</tt></b><br>
  568. &nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  569. <b><tt>} else {</tt></b><br>
  570. <b><tt>&nbsp;&nbsp;&nbsp;if (</tt></b> ...<b><tt> ) {</tt></b><br>
  571. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  572. <b><tt>&nbsp;&nbsp;&nbsp;} else {</tt></b><br>
  573. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
  574. <b><tt>&nbsp;&nbsp;&nbsp;}</tt></b><br>
  575. <b><tt>}</tt></b>
  576. </blockquote>
  577. <p>
  578. Similarly, for do-while statements, do this:
  579. <blockquote>
  580. <b><tt>do {</tt></b><br>
  581. &nbsp;&nbsp;&nbsp;... body ...<br>
  582. <b><tt>} while (</tt></b> ... condition ... <b><tt>);</tt></b>
  583. </blockquote>
  584. <h4><a name="Spaces"></a>Spaces</h4>
  585. <p>
  586. Do put a space:
  587. <ul>
  588. <li>after every comma and semicolon, unless it ends a line;
  589. <li>around every binary operator other than "<b><tt>-&gt;</tt></b>" and
  590. "<b><tt>.</tt></b>", although you can omit the spaces around the innermost
  591. operator in a nested expression if you like;
  592. <li>on both sides of the parentheses of an <b><tt>if</tt></b>, <b><tt>for</tt></b>, or <b><tt>while</tt></b>.
  593. </ul>
  594. <p>
  595. Don't put a space:
  596. <ul>
  597. <li>at the end of a line;
  598. <li>before a comma or semicolon;
  599. <li>after unary prefix operators;
  600. <li>before the parenthesis of a macro or procedure call.
  601. </ul>
  602. <h4><a name="Parentheses"></a>Parentheses</h4>
  603. <p>
  604. Parentheses are important in only a few places:
  605. <ul>
  606. <li>Around the inner subexpressions in expressions that mix
  607. <b><tt>&amp;&amp;</tt></b> and <b><tt>||</tt></b>, even if they are not
  608. required by precedence, for example
  609. <blockquote><b><tt>
  610. (xx &amp;&amp; yy) || zz
  611. </tt></b></blockquote>
  612. <li>Similarly around inner subexpressions in expressions that mix
  613. <b><tt>&amp;</tt></b>, <b><tt>|</tt></b>, or shifts, especially if mixing
  614. these with other operators, for instance
  615. <blockquote><b><tt>
  616. (x &lt;&lt; 3) | (y &gt;&gt; 5)
  617. </tt></b></blockquote>
  618. <li>In macro definitions around every use of an argument that logically
  619. could be an expression, for example
  620. <blockquote><b><tt>
  621. ((x) * (x) + (y) * (y))
  622. </tt></b></blockquote>
  623. </ul>
  624. <p>
  625. Anywhere else, given the choice, use fewer parentheses.
  626. <p>
  627. For stylistic consistency with the existing Ghostscript code, put
  628. parentheses around conditional expressions even if they aren't
  629. syntactically required, unless you really dislike doing this. Note that
  630. the parentheses should go around the entire expression, not the condition.
  631. For instance, instead of
  632. <blockquote><b><tt>
  633. hpgl_add_point_to_path(pgls, arccoord_x, arccoord_y,<br>
  634. &nbsp;&nbsp;&nbsp;(pgls-&gt;g.pen_down) ? gs_lineto : gs_moveto);
  635. </tt></b></blockquote>
  636. <p>
  637. use
  638. <blockquote><b><tt>
  639. hpgl_add_point_to_path(pgls, arccoord_x, arccoord_y,<br>
  640. &nbsp;&nbsp;&nbsp;(pgls-&gt;g.pen_down ? gs_lineto : gs_moveto));
  641. </tt></b></blockquote>
  642. <h3><a name="Preprocessor_style"></a>Preprocessor</h3>
  643. <h4>Conditionals</h4>
  644. <p>
  645. Using preprocessor conditionals can easily lead to unreadable code, since
  646. the eye really wants to read linearly rather than having to parse the
  647. conditionals just to figure out what code is relevant. It's OK to use
  648. conditionals that have small scope and that don't change the structure or
  649. logic of the program (typically, they select between different sets of
  650. values depending on some configuration parameter), but where possible, break
  651. up source modules rather than use conditionals that affect the structure or
  652. logic.
  653. <h4>Macros</h4>
  654. <p>
  655. Ghostscript code uses macros heavily to effectively extend the rather
  656. weak abstraction capabilities of the C language, specifically in the area of
  657. memory management and garbage collection: in order to read Ghostscript code
  658. effectively, you simply have to learn some of these macros as though they
  659. were part of the language. The current code also uses macros heavily for
  660. other purposes, but we are trying to phase these out as rapidly as possible,
  661. because they make the code harder to read and debug, and to use the
  662. rules that follow consistently in new code.
  663. <p>
  664. Define macros in the smallest scope you can manage (procedure, file, or
  665. <b><tt>.h</tt></b> file), and <b><tt>#undef</tt></b> them at the end of
  666. that scope: that way, someone reading the code can see the definitions
  667. easily when reading the uses. If that isn't appropriate, define them in as
  668. large a scope as possible, so that they effectively become part of the
  669. language. This places an additional burden on the reader, but it can be
  670. amortized over reading larger amounts of code.
  671. <p>
  672. Try hard to use procedures instead of macros. Use "<b><tt>inline</tt></b>"
  673. if you really think the extra speed is needed, but only within a
  674. <b><tt>.c</tt></b> file: don't put inline procedures in <b><tt>.h</tt></b>
  675. files, because most compilers don't honor "<b><tt>inline</tt></b>" and
  676. you'll wind up with a copy of the procedure in every <b><tt>.c</tt></b>
  677. file that includes the <b><tt>.h</tt></b> file.
  678. <p>
  679. If you define a macro that looks like a procedure, make sure it will work
  680. wherever a procedure will work. In particular, put parentheses around every
  681. use of an argument within the macro body, so that the macro will parse
  682. correctly if some of the arguments are expressions, and put parentheses
  683. around the entire macro body. (This is still subject to the problem that an
  684. argument may be evaluated more than once, but there is no way around this in
  685. C, since C doesn't provide for local variables within expressions.)
  686. <p>
  687. If you define macros for special loop control structures, make their uses
  688. look somewhat like ordinary loops, for instance:
  689. <blockquote>
  690. <b><tt>BEGIN_RECT(xx, yy) {</tt></b><br>
  691. &nbsp;&nbsp;... body indented one position ...<br>
  692. <b><tt>} END_RECT(xx, yy);</tt></b>
  693. </blockquote>
  694. <p>
  695. If at all possible, don't use free variables in macros -- that is, variables
  696. that aren't apparent as arguments of the macro. If you must use free
  697. variables, list them all in a comment at the point where the macro is
  698. defined.
  699. <h3><a name="Comments"></a>Comments</h3>
  700. <p>
  701. The most important descriptive comments are ones in header files that
  702. describe structures, including invariants; but every procedure or structure
  703. declaration, or group of other declarations, should have a comment. Don't
  704. spend a lot of time commenting executable code unless something unusual or
  705. subtle is going on.
  706. <h3><a name="Naming"></a>Naming</h3>
  707. <p>
  708. Use fully spelled-out English words in names, rather than contractions.
  709. This is most important for procedure and macro names, global variables and
  710. constants, values of <b><tt>#define</tt></b> and <b><tt>enum</tt></b>,
  711. <b><tt>struct</tt></b> and other <b><tt>typedef</tt></b> names, and
  712. structure member names, and for argument and variable names which have
  713. uninformative types like <b><tt>int</tt></b>. It's not very important for
  714. arguments or local variables of distinctive types, or for local index or
  715. count variables.
  716. <p>
  717. Avoid names that run English words together:
  718. "<b><tt>hpgl_compute_arc_center</tt></b>" is better than
  719. "<b><tt>hpgl_compute_arccenter</tt></b>". However, for terms drawn from
  720. some predefined source, like the names of PostScript operators, use a term
  721. in its well-known form (for instance, <b><tt>gs_setlinewidth</tt></b>
  722. rather than <b><tt>gs_set_line_width</tt></b>).
  723. <p>
  724. Procedures, variables, and structures visible outside a single
  725. <b><tt>.c</tt></b> file should generally have prefixes that indicate what
  726. subsystem they belong to (in the case of Ghostscript, <b><tt>gs_</tt></b>
  727. or <b><tt>gx_</tt></b>). This rule isn't followed very consistently.
  728. <h3><a name="Types"></a>Types</h3>
  729. <p>
  730. Many older structure names don't have <b><tt>_t</tt></b> on the end, but
  731. this suffix should be used in all new code. (The <b><tt>_s</tt></b>
  732. structure name is needed only to satisfy some debuggers. No code other than
  733. the structure declaration should refer to it.)
  734. <p>
  735. Declare structure types that contain pointers to other instances of
  736. themselves like this:
  737. <blockquote>
  738. <b><tt>typedef struct xxx_s xxx_t;</tt></b><br>
  739. <b><tt>struct xxx_s {</tt></b><br>
  740. &nbsp;&nbsp;&nbsp;... members ...<br>
  741. &nbsp;&nbsp;&nbsp;<b><tt>xxx_t *</tt></b>ptr_member_name;<br>
  742. &nbsp;&nbsp;&nbsp;... members ...<br>
  743. <b><tt>};</tt></b>
  744. </blockquote>
  745. <p>
  746. If, to maintain data abstraction and avoid including otherwise unnecessary
  747. header files, you find that you want the type <b><tt>xxx_t</tt></b> to be
  748. available in a header file that doesn't include the definition of the
  749. structure <b><tt>xxx_s</tt></b>, use this approach:
  750. <blockquote>
  751. <b><tt>#ifndef xxx_DEFINED</tt></b><br>
  752. <b><tt>#&nbsp;&nbsp;define xxx_DEFINED</tt></b><br>
  753. <b><tt>typedef struct xxx_s xxx_t;</tt></b><br>
  754. <b><tt>#endif</tt></b><br>
  755. <b><tt>struct xxx_s {</tt></b><br>
  756. &nbsp;&nbsp;&nbsp;... members ...<br>
  757. <b><tt>};</tt></b>
  758. </blockquote>
  759. <p>
  760. You can then copy the first 4 lines in other header files. (Don't ever
  761. include them in an executable code file.)
  762. <p>
  763. Don't bother using <b><tt>const</tt></b> for anything other than with
  764. pointers as described below. However, in those places where it is necessary
  765. to cast a pointer of type <b><tt>const&nbsp;T&nbsp;*</tt></b> to type
  766. <b><tt>T&nbsp;*</tt></b>, always include a comment that explains why you are
  767. "breaking const".
  768. <h4>Pointers</h4>
  769. <p>
  770. Use <b><tt>const</tt></b> for pointer referents (that is,
  771. <b><tt>const&nbsp;T&nbsp;*</tt></b>) wherever possible and appropriate.
  772. <p>
  773. If you find yourself wanting to use <b><tt>void&nbsp;*</tt></b>, try to
  774. find an alternative using unions or (in the case of super- and subclasses)
  775. casts, unless you're writing something like a memory manager that really
  776. treats memory as opaque.
  777. <h3><a name="Procedures_style"></a>Procedures</h3>
  778. <p>
  779. In general, don't create procedures that are private and only called from
  780. one place. However, if a compound statement (especially an arm of a
  781. conditional) is too long for the eye easily to match its enclosing braces
  782. "<b><tt>{...}</tt></b>" -- that is, longer than 10 or 15 lines -- and it
  783. doesn't use or set a lot of state held in outer local variables, it may be
  784. more readable if you put it in a procedure.
  785. <h3>Miscellany</h3>
  786. <h4><a name="Local_variables"></a>Local variables</h4>
  787. <p>
  788. Don't assign new values to procedure parameters. It makes debugging very
  789. confusing when the parameter values printed for a procedure are not the
  790. ones actually supplied by the caller. Instead use a separate local
  791. variable initialized to the value of the parameter.
  792. <p>
  793. If a local variable is only assigned a value once, assign it that value at
  794. its declaration, if possible. For example,
  795. <blockquote>
  796. <b><tt>int x = </tt></b>some expression <b><tt>;</tt></b>
  797. </blockquote>
  798. <p>
  799. rather than
  800. <blockquote>
  801. <b><tt>int x;</tt></b><br>
  802. ...<br>
  803. <b><tt>x = </tt></b> some expression <b><tt>;</tt></b>
  804. </blockquote>
  805. <p>
  806. Use a local pointer variable like this to "narrow" pointer types:
  807. <blockquote>
  808. <b><tt>int</tt></b><br>
  809. someproc(... <b><tt>gx_device *dev</tt></b> ...)<br>
  810. <b><tt>{<br>
  811. &nbsp;&nbsp;&nbsp;gx_device_printer *const pdev = (gx_device_printer *)dev;</tt></b><br>
  812. &nbsp;&nbsp;&nbsp;...<br>
  813. <b><tt>}</tt></b>
  814. </blockquote>
  815. <h4><a name="Compiler_warnings"></a>Compiler warnings</h4>
  816. <p>
  817. The following section refers to the warnings produced by <b><tt>gcc</tt></b>:
  818. your compiler may produce different ones.
  819. <p>
  820. It's OK if compilation produces the following warnings:
  821. <ul>
  822. <li><b><tt>&lt;name&gt; might be used uninitialized in this function</tt></b>
  823. <li><b><tt>cast discards `const' from pointer target type</tt></b>
  824. </ul>
  825. <p>
  826. The first of these often occurs because the compiler isn't aware of control
  827. flow restrictions that guarantee the variable will be initialized before
  828. use: if it occurs in new code, check the code carefully, but don't worry
  829. about the message. The second is often unavoidable in code that initializes
  830. or frees a structure that is otherwise <b><tt>const</tt></b> during its
  831. lifetime.
  832. <p>
  833. Do work hard to eliminate all warnings other than these,
  834. since they often indicate the possible presence of coding errors.
  835. In particular, get rid of warnings about parameter passing or
  836. initialization that discards <b><tt>const</tt></b>,
  837. by using explicit casts.
  838. <hr>
  839. <h2><a name="File_structuring"></a>File structuring</h2>
  840. <h3><a name="All_files"></a>All files</h3>
  841. <p>
  842. Keep file names within the "8.3" format for portability:
  843. <ul>
  844. <li>Use only letters, digits, dash, and underscore in file names.
  845. <li>Don't assume upper and lower case letters are distinct.
  846. <li>Put no more than 8 characters before the ".", if any.
  847. <li>If there is a ".", put between 1 and 3 characters after the ".".
  848. </ul>
  849. <p>
  850. For files other than documentation files, use only lower case letters
  851. in the names; for HTML documentation files, capitalize the first letter.
  852. <p>
  853. Every code file should start with comments containing
  854. <ol>
  855. <li>a copyright notice,
  856. <li>the name of the file in the form of an RCS Id:
  857. <blockquote><b><tt>
  858. /*Id$: filename.ext $*/
  859. </tt></b></blockquote>
  860. <p>
  861. (using the comment convention appropriate to the language of the file), and
  862. <li>a summary, no more than one line, of what the file contains.
  863. </ol>
  864. <p>
  865. If you create a file by copying the beginning of another file, be sure to
  866. update the copyright year and change the file name.
  867. <h3><a name="Makefiles"></a>Makefiles</h3>
  868. <p>
  869. Use the extension <b><tt>.mak</tt></b> for makefiles.
  870. <p>
  871. For each
  872. <blockquote><b><tt>
  873. #include "xxx.h"
  874. </tt></b></blockquote>
  875. <p>
  876. make sure there is a dependency on <b><tt>$(xxx_h)</tt></b> in the
  877. makefile. If xxx ends with a "<b><tt>_</tt></b>", this rule still holds,
  878. so that if you code
  879. <blockquote><b><tt>
  880. #include "math_.h"
  881. </tt></b></blockquote>
  882. <p>
  883. the makefile must contain a dependency on "<b><tt>$(math__h)</tt></b>"
  884. (note the two underscores "<b><tt>__</tt></b>").
  885. <p>
  886. List the dependencies bottom-to-top, like the <b><tt>#include</tt></b>
  887. statements themselves; within each level, list them alphabetically. Do
  888. this also with <b><tt>#include</tt></b> statements themselves whenever
  889. possible (but sometimes there are inter-header dependencies that require
  890. bending this rule).
  891. <p>
  892. For compatibility with the build utilities on OpenVMS, always put a space
  893. before the colon that separates the target(s) of a rule from the dependents.
  894. <h3><a name="General_C_code"></a>General C code</h3>
  895. <p>
  896. List <b><tt>#include</tt></b> statements from "bottom" to "top", that is,
  897. in the following order:
  898. <blockquote><ol>
  899. <li>System includes (<b><tt>"xxx_.h"</tt></b>)
  900. <li><b><tt>gs*.h</tt></b>
  901. <li><b><tt>gx*.h</tt></b> (yes, <b><tt>gs</tt></b> and <b><tt>gx</tt></b>
  902. are in the wrong order.)
  903. <li><b><tt>s*.h</tt></b>
  904. <li><b><tt>i*.h</tt></b> (or other interpreter headers that don't start
  905. with "<b><tt>i</tt></b>")
  906. </ol></blockquote>
  907. <h3><a name="Headers"></a>Headers (<b><tt>.h</tt></b> files)</h3>
  908. <p>
  909. In header files, always use the following at the beginning of a header file
  910. to prevent double inclusion:
  911. <blockquote>
  912. {{ Copyright notice etc. }}<br><br>
  913. <b><tt>#ifndef </tt></b>&lt;filename&gt;<b><tt>_INCLUDED</tt></b><br>
  914. <b><tt>#define </tt></b>&lt;filename&gt;<b><tt>_INCLUDED</tt></b><br><br>
  915. {{ The contents of the file }}<br><br>
  916. <b><tt>#endif /* </tt></b>&lt;filename&gt;<b><tt>_INCLUDED */</tt></b>
  917. </blockquote>
  918. <p>
  919. The header file is the first place that a reader goes for
  920. information about procedures, structures, constants, etc., so ensure that
  921. every procedure and structure has a comment that says what it does. Divide
  922. procedures into meaningful groups set off by some distinguished form of
  923. comment.
  924. <h3><a name="Source"></a>Source (<b><tt>.c</tt></b> files)</h3>
  925. <p>
  926. After the initial comments, arrange C files in the following order:
  927. <blockquote><ol>
  928. <li><b><tt>#include</tt></b> statements
  929. <li>Exported data declarations
  930. <li>Explicit externs (if necessary)
  931. <li>Forward declarations of procedures
  932. <li>Private data declarations
  933. <li>Exported procedures
  934. <li>Private procedures
  935. </ol></blockquote>
  936. <p>
  937. Be flexible about the order of the declarations if necessary to improve
  938. readability. Many older files don't follow this order, often without good
  939. reason.
  940. <hr>
  941. <h2><a name="Conventions"></a>Ghostscript conventions</h2>
  942. <h3><a name="Specific_names"></a>Specific names</h3>
  943. <p>
  944. The Ghostscript code uses certain names consistently for certain kinds of
  945. values. Some of the commonest and least obvious are these two:
  946. <h4><a name="code"></a><b><tt>code</tt></b></h4>
  947. <blockquote>
  948. A value to be returned from a procedure:
  949. <table cellpadding=0 cellspacing=0>
  950. <tr valign=top> <td align=right>&lt; 0
  951. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  952. <td>An error code defined in
  953. <a href="../src/gserrors.h">gserrors.h</a>
  954. (or <a href="../src/errors.h">errors.h</a>)
  955. <tr valign=top> <td align=right>0
  956. <td>&nbsp;
  957. <td>Normal return
  958. <tr valign=top> <td align=right>&gt; 0
  959. <td>&nbsp;
  960. <td>A non-standard but successful return (which must be documented, preferably with the procedure's prototype)
  961. </table>
  962. </blockquote>
  963. <h4><a name="status"></a><b><tt>status</tt></b></h4>
  964. <blockquote>
  965. A value returned from a stream procedure:
  966. <table cellpadding=0 cellspacing=0>
  967. <tr valign=top> <td align=right>&lt; 0
  968. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  969. <td>An exceptional condition as defined in
  970. <a href="../src/scommon.h">scommon.h</a>
  971. <tr valign=top> <td align=right>0
  972. <td>&nbsp;
  973. <td>Normal return (or, from the "<b><tt>process</tt></b>" procedure, means that more input is needed)
  974. <tr valign=top> <td align=right>1
  975. <td>&nbsp;
  976. <td>More output space is needed (from the "<b><tt>process</tt></b>" procedure)
  977. </table>
  978. </blockquote>
  979. <h3><a name="Structure_type_descriptors"></a>Structure type descriptors</h3>
  980. <p>
  981. The Ghostscript memory manager requires run-time type information for every
  982. structure. (We don't document this in detail here: see the <a
  983. href="Develop.htm#Structure_descriptors">Structure descriptors</a> section
  984. of the developer documentation for details.) Putting the descriptor for a
  985. structure next to the structure definition will help keep the two
  986. consistent, so immediately after the definition of a structure
  987. <b><tt>xxx_s</tt></b>, define its structure descriptor:
  988. <blockquote>
  989. <b><tt>struct xxx_s {</tt></b><br>
  990. &nbsp;&nbsp;&nbsp;... members ...<br>
  991. <b><tt>};</tt></b><br>
  992. <b><tt>#define private_st_xxx()&nbsp;&nbsp;/* in </tt></b>&lt;filename&gt;<tt><b>.c */\</tt></b><br>
  993. <b><tt>&nbsp;&nbsp;gs_private_st_</tt></b>&lt;whatever&gt;<b><tt>(st_xxx, xxx_t,\</tt></b><br>
  994. <b><tt>&nbsp;&nbsp;&nbsp;&nbsp;"xxx_t", xxx_enum_ptrs, xxx_reloc_ptrs,\</tt></b><br>
  995. <b><tt>&nbsp;&nbsp;&nbsp;&nbsp;</tt></b>... additional parameters as needed ...<b><tt>)</tt></b>
  996. </blockquote>
  997. <p>
  998. The file that implements operations on this structure
  999. (&lt;filename&gt;<b><tt>.c</tt></b>) should then include, near the
  1000. beginning, the line:
  1001. <blockquote>
  1002. <b><tt>private_st_xxx();</tt></b>
  1003. </blockquote>
  1004. <p>
  1005. In much existing code, structure descriptors are declared as
  1006. <b><tt>public</tt></b>, which allows clients to allocate instances of the
  1007. structure directly. We now consider this bad design. Instead, structure
  1008. descriptors should always be <b><tt>private</tt></b>; the implementation
  1009. file should provide one or more procedures for allocating instances, e.g.,
  1010. <blockquote>
  1011. <b><tt>xxx_t *gs_xxx_alloc(P1(gs_memory_t *mem));</tt></b>
  1012. </blockquote>
  1013. <p>
  1014. If it is necessary to make a structure descriptor public, it should be
  1015. declared in its clients as
  1016. <blockquote>
  1017. <b><tt>extern_st(st_xxx);</tt></b>
  1018. </blockquote>
  1019. <h3><a name="Objects"></a>"Objects"</h3>
  1020. <p>
  1021. Ghostscript makes heavy use of object-oriented constructs, including
  1022. analogues of classes, instances, subclassing, and class-associated
  1023. procedures. However, these constructs are implemented in C rather than C++,
  1024. for two reasons:
  1025. <ul>
  1026. <li>The first Ghostscript code was written in 1986, long before C++ was
  1027. codified or was well supported by tools. Even today, C++ tools rarely
  1028. support C++ as well as C tools support C.
  1029. <li>C++ imposes its own implementations for virtual procedures, inheritance,
  1030. run-time type information, and (to some extent) memory management.
  1031. Ghostscript requires use of its own memory manager, and also sometimes
  1032. requires the ability to change the virtual procedures of an object
  1033. dynamically.
  1034. </ul>
  1035. <h4>Classes</h4>
  1036. <p>
  1037. The source code representation of a class is simply a
  1038. <b><tt>typedef</tt></b> for a C <b><tt>struct</tt></b>. See <a
  1039. href="C-style.htm#Structures">Structures</a>, above, for details.
  1040. <h4>Procedures</h4>
  1041. <p>
  1042. Ghostscript has no special construct for non-virtual procedures associated
  1043. with a class. In some cases, the <b><tt>typedef</tt></b> for the class is
  1044. in a header file but the <b><tt>struct</tt></b> declaration is in the
  1045. implementation code file: this provides an extra level of opaqueness, since
  1046. clients then can't see the representation and must make all accesses through
  1047. procedures. You should use this approach in new code, if it doesn't
  1048. increase the size of the code too much or require procedure calls for very
  1049. heavily used accesses.
  1050. <p>
  1051. Ghostscript uses three different approaches for storing and accessing
  1052. virtual procedures, plus a fourth one that is recommended but not currently
  1053. used. For exposition, we assume the class (type) is named
  1054. <b><tt>xxx_t</tt></b>, it has a virtual procedure
  1055. <b><tt>void&nbsp;(*virtu)(P1(xxx_t&nbsp;*))</tt></b>, and we have a variable
  1056. declared as <b><tt>xxx_t&nbsp;*pxx</tt></b>.
  1057. <ol>
  1058. <li>The procedures are stored in a separate, constant structure of type
  1059. <b><tt>xxx_procs</tt></b>, of which <b><tt>virtu</tt></b> is a member. The
  1060. structure definition of <b><tt>xxx_t</tt></b> includes a member defined as
  1061. <b><tt>const&nbsp;xxx_procs&nbsp;*procs</tt></b> (always named
  1062. <b><tt>procs</tt></b>). The construct for calling the virtual procedure is
  1063. <b><tt>pxx->procs->virtu(pxx)</tt></b>.
  1064. <li>The procedures are defined in a structure of type
  1065. <b><tt>xxx_procs</tt></b> as above. The structure definition of
  1066. <b><tt>xxx_t</tt></b> includes a member defined as
  1067. <b><tt>xxx_procs&nbsp;procs</tt></b> (always named <b><tt>procs</tt></b>).
  1068. The construct for calling the virtual procedure is
  1069. <b><tt>pxx->procs.virtu(pxx)</tt></b>.
  1070. <li>The procedures are not defined in a separate structure: each procedure
  1071. is a separate member of <b><tt>xxx_t</tt></b>. The construct for calling
  1072. the virtual procedure is <b><tt>pxx->virtu(pxx)</tt></b>.
  1073. <li>The procedures are defined in a structure of type
  1074. <b><tt>xxx_procs</tt></b> as above. The structure definition of
  1075. <b><tt>xxx_t</tt></b> includes a member defined as
  1076. <b><tt>xxx_procs&nbsp;procs[1]</tt></b> (always named
  1077. <b><tt>procs</tt></b>). The construct for calling the virtual procedure is
  1078. again <b><tt>pxx->procs->virtu(pxx)</tt></b>.
  1079. </ol>
  1080. <p>
  1081. Note that in approach 1, the procedures are in a shared constant structure;
  1082. in approaches 2 - 4, they are in a per-instance structure that can be
  1083. changed dynamically, which is sometimes important.
  1084. <p>
  1085. In the present Ghostscript code, approach 1 is most common, followed by 2
  1086. and 3; 4 is not used at all. For new code, you should use 1 or 4: that way,
  1087. all virtual procedure calls have the same form, regardless of whether the
  1088. procedures are shared and constant or per-instance and mutable.
  1089. <h4>Subclassing</h4>
  1090. <p>
  1091. Ghostscript's class mechanism allows for subclasses that can add data
  1092. members, or can add procedure members if approach 1 or 3 (above) is used.
  1093. Since C doesn't support subclassing, we use a convention to accomplish it.
  1094. In the example below, <b><tt>gx_device</tt></b> is the root class; it has a
  1095. subclass <b><tt>gx_device_forward</tt></b>, which in turn has a subclass
  1096. <b><tt>gx_device_null</tt></b>. First we define a macro for all the members
  1097. of the root class, and the root class type. (As for structures in general,
  1098. classes need a structure descriptor, as discussed in <a
  1099. href="#Structures">Structures</a> above: we include these in the examples
  1100. below.)
  1101. <blockquote><b><tt>
  1102. #define gx_device_common\<br>
  1103. &nbsp;&nbsp;&nbsp;&nbsp;type1 member1;\<br>
  1104. &nbsp;&nbsp;&nbsp;&nbsp;</tt></b>...<b><tt><br>
  1105. &nbsp;&nbsp;&nbsp;&nbsp;typeN memberN<br>
  1106. <br>
  1107. typedef struct gx_device_s {<br>
  1108. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_common;<br>
  1109. } gx_device;<br>
  1110. <br>
  1111. #define private_st_gx_device()&nbsp;&nbsp;/* in gsdevice.c */\<br>
  1112. &nbsp;&nbsp;gs_private_st_</tt></b>&lt;whatever&gt;<b><tt>(st_gx_device, gx_device,\<br>
  1113. &nbsp;&nbsp;&nbsp;&nbsp;"gx_device", device_enum_ptrs, device_reloc_ptrs,\<br>
  1114. &nbsp;&nbsp;&nbsp;&nbsp;</tt></b>... additional parameters as needed ...<b><tt>)</tt></b>
  1115. </tt></b></blockquote>
  1116. <p>
  1117. We then define a similar macro and type for the subclass.
  1118. <blockquote><b><tt>
  1119. #define gx_device_forward_common\<br>
  1120. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_common;\<br>
  1121. &nbsp;&nbsp;&nbsp;&nbsp;gx_device *target<br>
  1122. <br>
  1123. typedef struct gx_device_forward_s {<br>
  1124. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward_common;<br>
  1125. } gx_device_forward;<br>
  1126. <br>
  1127. #define private_st_device_forward()&nbsp;&nbsp;/* in gsdevice.c */\<br>
  1128. &nbsp;&nbsp;gs_private_st_suffix_add1(st_device_forward, gx_device_forward,\<br>
  1129. &nbsp;&nbsp;&nbsp;&nbsp;"gx_device_forward", device_forward_enum_ptrs, device_forward_reloc_ptrs,\<br>
  1130. &nbsp;&nbsp;&nbsp;&nbsp;gx_device, target)
  1131. </tt></b></blockquote>
  1132. <p>
  1133. Finally, we define a leaf class, which doesn't need a macro because we don't
  1134. currently subclass it. (We can create the macro later if needed, with no
  1135. changes anywhere else.) In this particular case, the leaf class has no
  1136. additional data members, but it could have some.
  1137. <blockquote><b><tt>
  1138. typedef struct gx_device_null_s {<br>
  1139. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward_common;<br>
  1140. };<br>
  1141. <br>
  1142. #define private_st_device_null()&nbsp;&nbsp;/* in gsdevice.c */\<br>
  1143. &nbsp;&nbsp;gs_private_st_suffix_add0_local(st_device_null, gx_device_null,\<br>
  1144. &nbsp;&nbsp;&nbsp;&nbsp;"gx_device_null", device_null_enum_ptrs, device_null_reloc_ptrs,\<br>
  1145. &nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward)
  1146. </tt></b></blockquote>
  1147. <p>
  1148. Note that the above example is <strong>not</strong> the actual definition of
  1149. the <b><tt>gx_device</tt></b> structure type: the actual type has some
  1150. additional complications because it has a finalization procedure. See <a
  1151. href="../src/gxdevcli.h">src/gxdevcli.h</a> for the details.
  1152. <p>
  1153. If you add members to a root class (such as <b><tt>gx_device</tt></b> in
  1154. this example), or change existing members, do this in the
  1155. <b><tt>gx_device_common</tt></b> macro, not the <b><tt>gx_device</tt></b>
  1156. structure definition. Similarly, to change the
  1157. <b><tt>gx_device_forward</tt></b> class, modify the
  1158. <b><tt>gx_device_forward_common</tt></b> macro, not the structure
  1159. definition. Only change the structure definition if the class is a leaf
  1160. class (one with no <b><tt>_common</tt></b> macro and no possibility of
  1161. subclassing), like <b><tt>gx_device_null</tt></b>.
  1162. <h3><a name="Error_handling"></a>Error handling</h3>
  1163. <p>
  1164. Every caller should check for error returns and, in general, propagate them
  1165. to <b>its</b> callers. By convention, nearly every procedure returns an
  1166. <b><tt>int</tt></b> to indicate the outcome of the call:
  1167. <blockquote><table cellpadding=0 cellspacing=0>
  1168. <tr valign=top> <td align=right>&lt; 0
  1169. <td>&nbsp;&nbsp;&nbsp;&nbsp;
  1170. <td>Error return
  1171. <tr valign=top> <td align=right>0
  1172. <td>&nbsp;
  1173. <td>Normal return
  1174. <tr valign=top> <td align=right>&gt; 0
  1175. <td>&nbsp;
  1176. <td>Non-error return other than the normal case
  1177. </table></blockquote>
  1178. <p>
  1179. To make a procedure generate an error and return it, as opposed to
  1180. propagating an error generated by a lower procedure, you should use
  1181. <blockquote>
  1182. <b><tt>return_error(</tt></b><em>error_number</em><b><tt>);</tt></b>
  1183. </blockquote>
  1184. <p>
  1185. Sometimes it is more convenient to generate the error in one place and
  1186. return it in another. In this case, you should use
  1187. <blockquote>
  1188. <b><tt>code = gs_note_error(</tt></b><em>error_number</em><b><tt>);</tt></b><br>
  1189. ...<br>
  1190. <b><tt>return code;</tt></b>
  1191. </blockquote>
  1192. <p>
  1193. In executables built for debugging, the <b><tt>-E</tt></b> (or
  1194. <b><tt>-Z#</tt></b>) command line switch causes <b><tt>return_error</tt></b>
  1195. and <b><tt>gs_note_error</tt></b> to print the error number and the source
  1196. file and line: this is often helpful for identifying the original cause of
  1197. an error.
  1198. <p>
  1199. See the file <a href="../src/gserrors.h">src/gserrors.h</a> for the error
  1200. return codes used by the graphics library, most of which correspond directly
  1201. to PostScript error conditions.
  1202. <!-- [2.0 end contents] ==================================================== -->
  1203. <!-- [3.0 begin visible trailer] =========================================== -->
  1204. <hr>
  1205. <p>
  1206. <small>Copyright &copy; 1996, 1997, 1998 Aladdin Enterprises.
  1207. Copyright &copy; 2001 artofcode LLC.
  1208. All rights reserved.</small>
  1209. <p>
  1210. <small>This file is part of AFPL Ghostscript. See the <a
  1211. href="Public.htm">Aladdin Free Public License</a> (the "License") for full
  1212. details of the terms of using, copying, modifying, and redistributing AFPL
  1213. Ghostscript.</small>
  1214. <p>
  1215. <small>Ghostscript version 7.04, 31 January 2002
  1216. <!-- [3.0 end visible trailer] ============================================= -->
  1217. </body>
  1218. </html>