home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft_Programmers_Library.7z / MPL / msc / mscadv.txt < prev    next >
Encoding:
Text File  |  2013-11-08  |  874.2 KB  |  22,196 lines

  1.  Microsoft  C - Advanced Programming Techniques
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  ────────────────────────────────────────────────────────────────────────────
  11.               Microsoft (R) C - Advanced Programming Techniques
  12.  
  13.                         FOR MS (R) OS/2 AND MS-DOS (R)
  14.                               OPERATING SYSTEMS
  15.  ────────────────────────────────────────────────────────────────────────────
  16.  
  17.  
  18.                             MICROSOFT CORPORATION
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  Information in this document is subject to change without notice and does
  28.  not represent a commitment on the part of Microsoft Corporation. The
  29.  software described in this document is furnished under a license agreement
  30.  or nondisclosure agreement. The software may be used or copied only in
  31.  accordance with the terms of the agreement. It is against the law to copy
  32.  the software on any medium except as specifically allowed in the license or
  33.  nondisclosure agreement. No part of this manual may be reproduced or trans-
  34.  mitted in any form or by any means, electronic or mechanical, including
  35.  photocopying and recording, for any purpose without the express written
  36.  permission of Microsoft.
  37.  
  38.  (C) Copyright Microsoft Corporation, 1990. All rights reserved.
  39.  
  40.  Printed and bound in the United States of America.
  41.  
  42.  Microsoft, MS, MS-DOS, CodeView, InPort, and XENIX are
  43.  registered trademarks and Windows is a trademark of Microsoft Corporation.
  44.  
  45.  Apple and Macintosh are registered trademarks and Finder
  46.  is a trademark of Apple Computer, Inc.
  47.  
  48.  AT&T is a registered trademark of American Telephone
  49.  and Telegraph Company.
  50.  
  51.  Hercules is a registered trademark and InColor is a trademark
  52.  of Hercules Computer Technology.
  53.  
  54.  IBM is a registered trademark of International Business
  55.  Machines Corporation.
  56.  
  57.  Intel is a registered trademark of Intel Corporation.
  58.  
  59.  Olivetti is a registered trademark of Ing. C. Olivetti.
  60.  
  61.  PDP-11 and VAX-11 are registered trademarks of Digital
  62.  Equipment Corporation.
  63.  
  64.  WANG is a registered trademark of Wang Laboratories.
  65.  
  66.  Z8000 is a registered trademark of Zilog, Inc.
  67.  
  68.  Document No. LN06514-1189 OEMO711-6Z
  69.  10 9 8 7 6 5 4 3 2 1
  70.  
  71.  
  72.  
  73.  
  74.  Table of Contents
  75.  ────────────────────────────────────────────────────────────────────────────
  76.  
  77.  
  78.  
  79.  Introduction
  80.       Scope of This Book
  81.       Document Conventions
  82.  
  83.  
  84.  PART I  Improving Program Performance
  85.  ────────────────────────────────────────────────────────────────────────────
  86.  
  87.  
  88.  Chapter 1  Optimizing C Programs
  89.  
  90.       1.1   Controlling Optimization from the Programmer's WorkBench
  91.       1.2   Controlling Optimization from the Command Line
  92.       1.3   Controlling Optimization with Pragmas
  93.       1.4   Default Optimization
  94.              1.4.1    Common Subexpression Elimination
  95.              1.4.2    Dead-Store Elimination
  96.              1.4.3    Constant Propagation
  97.       1.5   Customizing Your Optimizations
  98.              1.5.1    Choosing Speed or Size (/Ot and /Os)
  99.              1.5.2    Generating Intrinsic Functions (/Oi)
  100.              1.5.3    Assuming No Aliasing (/Oa and /Ow)
  101.              1.5.4    Performing Loop Optimizations (/Ol)
  102.              1.5.5    Disabling Unsafe Loop Optimizations (/On)
  103.              1.5.6    Enabling Aggressive Optimizations (/Oz)
  104.              1.5.7    Removing Stack Probes (/Gs)
  105.              1.5.8    Enabling Global Register Allocation (/Oe)
  106.              1.5.9    Enabling Common Subexpression Optimization (/Oc and
  107.                          /Og)
  108.              1.5.10   Achieving Consistent Floating-Point Results (/Op)
  109.              1.5.11   Using the 80186, 80188, or 80286 Processor (/G0, /G1,
  110.                          /G2)
  111.              1.5.12   Optimizing for Maximum Efficiency (/Ox)
  112.       1.6   Linker (LINK) Options that Control Optimization
  113.              1.6.1    Enabling Far Call Optimization (/FARCALLTRANSLATION)
  114.              1.6.2    Packing Code (/PACKCODE)
  115.              1.6.3    Packing Data (/PACKDATA)
  116.              1.6.4    Packing the Executable File (/EXEPACK)
  117.       1.7   Optimizing in Different Environments
  118.              1.7.1    Optimizing in DOS
  119.              1.7.2    Optimizing in OS/2
  120.              1.7.3    Optimizing in Microsoft Windows(tm)
  121.       1.8   Choosing Function-Calling Conventions
  122.              1.8.1    The C Calling Convention (/Gd)
  123.              1.8.2    The FORTRAN/Pascal Calling Convention (/Gc)
  124.              1.8.3    The Register Calling Convention (/Gr)
  125.              1.8.4    The _fastcall Calling Convention
  126.  
  127.  Chapter 2  Managing Memory
  128.  
  129.       2.1   Pointer Sizes
  130.              2.1.1    Pointers and 64K Segments
  131.              2.1.2    Near Pointers
  132.              2.1.3    Far Pointers
  133.              2.1.4    Huge Pointers
  134.              2.1.5    Based Addressing
  135.       2.2   Selecting a Standard Memory Model
  136.              2.2.1    The Six Standard Memory Models
  137.              2.2.2    Limitations on Code Size and Data Size
  138.              2.2.3    The Tiny Memory Model
  139.              2.2.4    The Huge Memory Model
  140.              2.2.5    Null Pointers
  141.              2.2.6    Specifying a Memory Model
  142.       2.3   Mixing Memory Models
  143.              2.3.1    Pointer Problems
  144.              2.3.2    Declaring Near, Far, Huge, and Based Variables
  145.              2.3.3    Declaring Near and Far Functions
  146.              2.3.4    Pointer Conversions
  147.       2.4   Customizing Memory Models
  148.              2.4.1    Setting a Size for Code Pointers
  149.              2.4.2    Setting a Size for Data Pointers
  150.              2.4.3    Setting Up Segments
  151.              2.4.4    Library Support for Customized Memory Models
  152.              2.4.5    Setting the Data Threshold
  153.              2.4.6    Naming Modules and Segments
  154.  
  155.  Chapter 3  Using the In-Line Assembler
  156.  
  157.       3.1   Advantages of In-Line Assembly
  158.       3.2   The _asm Keyword
  159.       3.3   Using Assembly Language in _asm Blocks
  160.       3.4   Using C in _asm Blocks
  161.              3.4.1    Using Operators
  162.              3.4.2    Using C Symbols
  163.              3.4.3    Accessing C Data
  164.              3.4.4    Writing Functions
  165.       3.5   Using and Preserving Registers
  166.       3.6   Jumping to Labels
  167.       3.7   Calling C Functions
  168.       3.8   Defining _asm Blocks as C Macros
  169.       3.9   Optimizing
  170.  
  171.  Chapter 4  Controlling Floating-Point Math Operations
  172.  
  173.       4.1   Declaring Floating-Point Types
  174.              4.1.1    Declaring Variables as Floating-Point Types
  175.              4.1.2    Declaring Functions that Return Floating-Point Types
  176.       4.2   C Run-Time Library Support of Type long double
  177.       4.3   Summary of Math Packages
  178.              4.3.1    Emulator Package
  179.              4.3.2    Math Coprocessor Package
  180.              4.3.3    Alternate Math Package
  181.       4.4   Selecting Floating-Point Options (/FP)
  182.              4.4.1    In-Line Emulator Option (/FPi)
  183.              4.4.2    In-Line Math Coprocessor Instructions Option (/FPi87)
  184.              4.4.3    Calls to Emulator Option (/FPc)
  185.              4.4.4    Calls to Math Coprocessor Option (/FPc87)
  186.              4.4.5    Use Alternate Math Option (/FPa)
  187.       4.5   Library Considerations for Floating-Point Options
  188.              4.5.1    Using One Standard Library for Linking
  189.              4.5.2    In-Line Instructions or Calls
  190.       4.6   Compatibility between Floating-Point Options
  191.       4.7   Using the NO87 Environment Variable
  192.       4.8   Incompatibility Issues
  193.  
  194.  
  195.  PART II  Improving Programmer Productivity
  196.  ────────────────────────────────────────────────────────────────────────────
  197.  
  198.  
  199.  Chapter 5  Compiling and Linking Quickly
  200.  
  201.       5.1   Compiling Quickly
  202.              5.1.1    Quick Compiler
  203.              5.1.2    Incremental Compile Option
  204.       5.2   Linking Quickly with ILINK
  205.              5.2.1    Preparing for Incremental Linking
  206.              5.2.2    Incremental Violations
  207.  
  208.  Chapter 6  Managing Development Projects with NMAKE
  209.  
  210.       6.1   Overview of NMAKE
  211.       6.2   The NMAKE Command
  212.       6.3   NMAKE Description Files
  213.              6.3.1    Description Blocks
  214.              6.3.2    Comments
  215.              6.3.3    Macros
  216.              6.3.4    Inference Rules
  217.              6.3.5    Directives
  218.              6.3.6    Pseudotargets
  219.              6.3.7    PWB's extmake Syntax
  220.       6.4   Command-Line Options
  221.       6.5   NMAKE Command Files
  222.       6.6   The TOOLS.INI File
  223.       6.7   In-Line Files
  224.       6.8   NMAKE Operations Sequence
  225.       6.9   Differences between NMAKE and MAKE
  226.  
  227.  Chapter 7  Creating Help Files with HELPMAKE
  228.  
  229.       7.1   Structure and Contents of a Help Database
  230.              7.1.1    Contents of a Help File
  231.              7.1.2    Help File Formats
  232.       7.2   Invoking HELPMAKE
  233.       7.3   HELPMAKE Options
  234.              7.3.1    Options for Encoding
  235.              7.3.2    Options for Decoding
  236.       7.4   Creating a Help Database
  237.       7.5   Help Text Conventions
  238.              7.5.1    Structure of the Help Text File
  239.              7.5.2    Local Contexts
  240.              7.5.3    Context Prefixes
  241.              7.5.4    Hyperlinks
  242.       7.6   Using Help Database Formats
  243.              7.6.1    QuickHelp Format
  244.              7.6.2    Minimally Formatted ASCII Format
  245.              7.6.3    Rich Text Format (RTF)
  246.  
  247.  Chapter 8  Customizing the Microsoft Programmer's WorkBench
  248.  
  249.       8.1   Setting Switches
  250.              8.1.1    Editing the <assign> Pseudofile
  251.              8.1.2    Editing the TOOLS.INI Initialization File
  252.       8.2   Assigning Keystrokes
  253.       8.3   Writing Macros
  254.              8.3.1    Macro Syntax
  255.              8.3.2    Macro Responses
  256.              8.3.3    Macro Arguments
  257.              8.3.4    Macro Conditionals
  258.              8.3.5    Temporary Macros
  259.              8.3.6    Macro Recordings
  260.       8.4   Writing and Building C Extensions
  261.              8.4.1    Building Real-Mode Extensions
  262.              8.4.2    Building Protected-Mode Extensions
  263.              8.4.3    Describing Functions and Switches
  264.              8.4.4    Initializing Functions
  265.              8.4.5    Prototyping Functions
  266.              8.4.6    Receiving Parameters
  267.              8.4.7    Calling PWB Functions
  268.              8.4.8    Calling C Library Functions
  269.  
  270.  Chapter 9  Debugging C Programs with CodeView
  271.  
  272.       9.1   Understanding CodeView Windows
  273.       9.2   Overview of Debugging Techniques
  274.       9.3   Viewing and Modifying Program Data
  275.              9.3.1    Displaying Variables in the Watch Window
  276.              9.3.2    Displaying Expressions in the Watch Window
  277.              9.3.3    Displaying Arrays and Structures
  278.              9.3.4    Displaying Array Elements Dynamically
  279.              9.3.5    Using Quick Watch
  280.              9.3.6    Displaying Memory
  281.              9.3.7    Displaying the Processor Registers
  282.              9.3.8    Modifying the Values of Variables, Registers,
  283.                          and Memory
  284.       9.4   Controlling Execution
  285.              9.4.1    Continuous Execution
  286.              9.4.2    Single-Stepping
  287.       9.5   Replaying a Debug Session
  288.       9.6   Advanced CodeView Techniques
  289.       9.7   Controlling CodeView with Command-Line Options
  290.       9.8   Customizing CodeView with the TOOLS.INI FILE
  291.  
  292.  
  293.  PART III  Special Environments
  294.  ────────────────────────────────────────────────────────────────────────────
  295.  
  296.  
  297.  Chapter 10  Communicating with Graphics
  298.  
  299.       10.1  Video Modes
  300.              10.1.1    Sample Low-Level Graphics Program
  301.              10.1.2    Setting a Video Mode
  302.              10.1.3    Reading the videoconfig Structure
  303.              10.1.4    Maximizing Resolution or Color
  304.              10.1.5    Selecting Your Own Video Modes
  305.       10.2  Mixing Colors and Changing Palettes
  306.              10.2.1    CGA Palettes
  307.              10.2.2    Olivetti(R) Palettes
  308.              10.2.3    VGA Palettes
  309.              10.2.4    MCGA Palettes
  310.              10.2.5    EGA Palettes
  311.              10.2.6    Symbolic Constants
  312.       10.3  Specifying Points within Coordinate Systems
  313.              10.3.1    Physical Coordinates
  314.              10.3.2    Viewport Coordinates
  315.              10.3.3    Window Coordinates
  316.              10.3.4    Screen Locations
  317.              10.3.5    Bounding Rectangles
  318.              10.3.6    The Pixel Cursor
  319.       10.4  Graphics Functions
  320.              10.4.1    Controlling Video Modes
  321.              10.4.2    Changing Colors
  322.              10.4.3    Drawing Points, Lines, and Shapes
  323.              10.4.4    Defining Patterns
  324.              10.4.5    Manipulating Images
  325.       10.5  Using Graphic Fonts
  326.              10.5.1    Using the C Font Library
  327.              10.5.2    Registering the Fonts
  328.              10.5.3    Setting the Current Font
  329.              10.5.4    Displaying Text
  330.              10.5.5    A Sample Program
  331.              10.5.6    Using Fonts Effectively
  332.  
  333.  Chapter 11  Creating Charts and Graphs
  334.  
  335.       11.1  Overview of Presentation Graphics
  336.       11.2  Parts of a Graph
  337.       11.3  Writing a Presentation Graphics Program
  338.              11.3.1    Pie Chart
  339.              11.3.2    Bar, Column, and Line Charts
  340.              11.3.3    Scatter Diagram
  341.       11.4  Manipulating Colors and Patterns
  342.              11.4.1    Color Pool
  343.              11.4.2    Style Pool
  344.              11.4.3    Pattern Pool
  345.              11.4.4    Character Pool
  346.       11.5  Customizing the Chart Environment
  347.              11.5.1    titletype Structures
  348.              11.5.2    axistype Structures
  349.              11.5.3    windowtype Structures
  350.              11.5.4    legendtype Structures
  351.              11.5.5    chartenv Structures
  352.  
  353.  Chapter 12  Programming with Mixed Languages
  354.  
  355.       12.1  Making Mixed-Language Calls
  356.       12.2  Language Convention Requirements
  357.              12.2.1    Naming Convention Requirement
  358.              12.2.2    Calling Convention Requirement
  359.              12.2.3    Parameter-Passing Requirement
  360.       12.3  Compiling and Linking
  361.              12.3.1    Compiling with Correct Memory Models
  362.              12.3.2    Linking with Language Libraries
  363.       12.4  C Calls to High-Level Languages
  364.       12.5  C Calls to BASIC
  365.       12.6  C Calls to FORTRAN
  366.              12.6.1    Calling a FORTRAN Subroutine from C
  367.              12.6.2    Calling a FORTRAN Function from C
  368.       12.7  C Calls to Pascal
  369.              12.7.1    Calling a Pascal Procedure from C
  370.              12.7.2    Calling a Pascal Function from C
  371.       12.8  C Calls to Assembly Language
  372.              12.8.1    Writing the Assembly-Language Procedure
  373.              12.8.2    Setting Up the Procedure
  374.              12.8.3    Entering the Procedure
  375.              12.8.4    Allocating Local Data
  376.              12.8.5    Preserving Register Values
  377.              12.8.6    Accessing Parameters
  378.              12.8.7    Returning a Value
  379.              12.8.8    Exiting the Procedure
  380.       12.9  Handling Data in Mixed-Language Programming
  381.              12.9.1    Default Naming and Calling Conventions
  382.              12.9.2    Numeric Data Representation
  383.              12.9.3    Strings
  384.              12.9.4    Arrays
  385.              12.9.5    Array Declaration and Indexing
  386.              12.9.6    Structures, Records, and User-Defined Types
  387.              12.9.7    External Data
  388.              12.9.8    Pointers and Address Variables
  389.              12.9.9    Common Blocks
  390.              12.9.10   Using a Varying Number of Parameters
  391.  
  392.  Chapter 13  Writing Portable Programs
  393.  
  394.       13.1  Assumptions about Hardware
  395.              13.1.1    Size of Basic Types
  396.              13.1.2    Storage Order and Alignment
  397.              13.1.3    Byte Order in a Word
  398.              13.1.4    Reading and Writing Structures
  399.              13.1.5    Bit Fields in Structures
  400.              13.1.6    Processor Arithmetic Mode
  401.              13.1.7    Pointers
  402.              13.1.8    Address Space
  403.              13.1.9    Character Set
  404.       13.2  Assumptions about the Compiler
  405.              13.2.1    Sign Extension
  406.              13.2.2    Length and Case of Identifiers
  407.              13.2.3    Register Variables
  408.              13.2.4    Functions with a Variable Number of Arguments
  409.              13.2.5    Evaluation Order
  410.              13.2.6    Function and Macro Arguments with Side Effects
  411.              13.2.7    Environment Differences
  412.       13.3  Portability of Data Files
  413.       13.4  Portability Concerns Specific to Microsoft C
  414.       13.5  Microsoft C Byte Ordering
  415.  
  416.  
  417.  PART IV  OS/2 Support
  418.  ────────────────────────────────────────────────────────────────────────────
  419.  
  420.  
  421.  Chapter 14  Building OS/2 Applications
  422.  
  423.       14.1  The OS/2 Applications Program Interface
  424.              14.1.1    Calling the OS/2 API
  425.              14.1.2    Including the OS/2 Header Files
  426.              14.1.3    Creating Dual-Mode Programs as Family Applications
  427.       14.2  Compile Options for the CL Command
  428.              14.2.1    The Link Mode Options (/Lp, /Lr, and /Lc)
  429.              14.2.2    Creating Bound Programs Option (/Fb)
  430.              14.2.3    Library Selection Options (/MT, /ML, /MD, /Zl)
  431.              14.2.4    Memory-Model Options (/Ax)
  432.       14.3  Module-Definition Files and Import Libraries
  433.              14.3.1    Adding a Module-Definition File to the LINK Command
  434.              14.3.2    Creating Dynamic-Link Libraries (DLLs)
  435.              14.3.3    Creating Programs with I/O Privileges
  436.              14.3.4    Creating Presentation Manager Applications
  437.              14.3.5    Creating Import Libraries with the IMPLIB Utility
  438.       14.4  Link Command-Line Options
  439.       14.5  The BIND Utility
  440.  
  441.  Chapter 15  Creating Multithread OS/2 Applications
  442.  
  443.       15.1  Multithread Programs
  444.              15.1.1    Library Support
  445.              15.1.2    Include Files
  446.              15.1.3    C Run-Time Library Functions for Thread Control
  447.       15.2  Sample Multithread C Program
  448.       15.3  Writing a Multithread Program
  449.       15.4  Compiling and Linking
  450.       15.5  Avoiding Problem Areas
  451.       15.6  Using the Protected-Mode CodeView Debugger
  452.              15.6.1    Compiling with the /Zi Option
  453.              15.6.2    Prompt for Thread Number
  454.              15.6.3    Thread Commands
  455.              15.6.4    Screen Groups Used by CodeView
  456.  
  457.  Chapter 16  Dynamic Linking with OS/2
  458.  
  459.       16.1  Overview of Dynamic Linking
  460.              16.1.1    Load-Time and Run-Time Linking
  461.              16.1.2    Application Programs and DLLs
  462.              16.1.3    DLLs and Microsoft C Run-Time Libraries
  463.       16.2  Designing and Writing DLLs
  464.              16.2.1    Floating-Point Math Requirements
  465.              16.2.2    Initialization and Termination Requirements
  466.              16.2.3    Making the DLL Re-Entrant
  467.              16.2.4    Signal Handling
  468.              16.2.5    Using Microsoft C Keywords
  469.              16.2.6    Compile Options for Dynamic-Link Libraries
  470.       16.3  Building DLLs with Microsoft C
  471.              16.3.1    DLLs with Static C Run-Time Library Functions
  472.              16.3.2    DLLs without C Run-Time Library Functions
  473.              16.3.3    Programs and DLLs with a C Run-Time DLL
  474.              16.3.4    Using CodeView to Debug Dynamic-Link Libraries
  475.  
  476.  Appendix A  Using Exit Codes
  477.  
  478.       A.1   The exit Function
  479.       A.2   Testing Exit Codes from Command and Batch Files
  480.       A.3   Accessing Exit Codes from Other Programs
  481.  
  482.  Appendix B  Differences between C Versions 5.1 and 6.0
  483.  
  484.       B.1   Modifications for ANSI Compatibility
  485.              B.1.1    ANSI-Mandated New Features
  486.              B.1.2    Integer Promotion Rules
  487.              B.1.3    Defining NULL as a Pointer
  488.              B.1.4    Shift Operators
  489.              B.1.5    Pointers to Typedefs
  490.              B.1.6    Identifying Nonstandard Keywords
  491.              B.1.7    Trigraphs
  492.              B.1.8    ANSI Nonconformance
  493.       B.2   New Keywords and Functions
  494.              B.2.1    In-Line Assembler
  495.              B.2.2    Based Pointers and Objects
  496.              B.2.3    Based Heap Allocation Support
  497.              B.2.4    Releasing Unused Heap Memory
  498.              B.2.5    Making Static Data Available to the Heap
  499.              B.2.6    Long Doubles
  500.              B.2.7    Long Double Functions
  501.              B.2.8    Model-Independent String and Memory Functions
  502.              B.2.9    Mixed-Model Memory Allocation Support
  503.              B.2.10   The _fastcall Attribute (/Gr Option)
  504.              B.2.11   Drive and Directory Functions
  505.              B.2.12   Text Output Functions for OS/2
  506.       B.3   New Features
  507.              B.3.1    Strings and Macros
  508.              B.3.2    CL Options
  509.              B.3.3    Tiny Memory Model (.COM Files)
  510.              B.3.4    The Optimize Pragma
  511.              B.3.5    Nameless Structures and Unions
  512.              B.3.6    Unsized Arrays as the Last Member of a Structure
  513.              B.3.7    Improved Warnings
  514.              B.3.8    Macros
  515.              B.3.9    Improved Multithread Support in OS/2
  516.              B.3.10   Pipe Support in OS/2
  517.       B.4   Differences in Code Generation
  518.              B.4.1    Speed and Space Improvements
  519.              B.4.2    Code Quality
  520.              B.4.3    Floating-Point Code Generation
  521.              B.4.4    Intrinsic Functions
  522.       B.5   Changes and Deletions
  523.              B.5.1    Deleted Features
  524.              B.5.2    Evaluation of Real Expressions
  525.              B.5.3    Default Optimizations
  526.              B.5.4    Sign Extension of char Arguments
  527.              B.5.5    Conditional Compilation and Signed Values
  528.              B.5.6    The const and volatile Qualifiers
  529.              B.5.7    Memory Allocation
  530.              B.5.8    Memory Used by Command-Line Arguments
  531.              B.5.9    Format Specifiers in printf
  532.              B.5.10   Functions that Return Float Values
  533.  
  534.  Appendix C  Implementation-Defined Behavior
  535.  
  536.       C.1   Translation
  537.              C.1.1    Diagnostics
  538.       C.2   Environment
  539.              C.2.1    Arguments to main
  540.              C.2.2    Interactive Devices
  541.       C.3   Identifiers
  542.              C.3.1    Significant Characters without External Linkage
  543.              C.3.2    Significant Characters with External Linkage
  544.              C.3.3    Upper- and Lowercase
  545.       C.4   Characters
  546.              C.4.1    The ASCII Character Set
  547.              C.4.2    Multibyte Characters
  548.              C.4.3    Bits per Character
  549.              C.4.4    Character Sets
  550.              C.4.5    Unrepresented Character Constants
  551.              C.4.6    Wide Characters
  552.              C.4.7    Converting Multibyte Characters
  553.              C.4.8    Range of char Values
  554.       C.5   Integers
  555.              C.5.1    Range of Integer Values
  556.              C.5.2    Demotion of Integers
  557.              C.5.3    Signed Bitwise Operations
  558.              C.5.4    Remainders
  559.              C.5.5    Right Shifts
  560.       C.6   Floating-Point Math
  561.              C.6.1    Values
  562.              C.6.2    Casting Integers to Floating-Point Values
  563.              C.6.3    Truncation of Floating-Point Values
  564.       C.7   Arrays and Pointers
  565.              C.7.1    Largest Array Size
  566.              C.7.2    Casting Pointers
  567.              C.7.3    Pointer Subtraction
  568.       C.8   Registers
  569.              C.8.1    Availability of Registers
  570.       C.9   Structures, Unions, Enumerations, and Bit Fields
  571.              C.9.1    Improper Access to a Union
  572.              C.9.2    Sign of Bit Fields
  573.              C.9.3    Storage of Bit Fields
  574.              C.9.4    Alignment of Bit Fields
  575.              C.9.5    The enum Type
  576.       C.10  Qualifiers
  577.              C.10.1   Access to Volatile Objects
  578.       C.11  Declarators
  579.              C.11.1   Maximum Number
  580.       C.12  Statements
  581.              C.12.1   Limits on Switch Statements
  582.       C.13  Preprocessing Directives
  583.              C.13.1   Character Constants and Conditional Inclusion
  584.              C.13.2   Including Bracketed File Names
  585.              C.13.3   Including Quoted File Names
  586.              C.13.4   Character Sequences
  587.              C.13.5   Pragmas
  588.              C.13.6   Default Date and Time
  589.       C.14  Library Functions
  590.              C.14.1   NULL Macro
  591.              C.14.2   Diagnostic Printed by the assert Function
  592.              C.14.3   Character Testing
  593.              C.14.4   Domain Errors
  594.              C.14.5   Underflow of Floating-Point Values
  595.              C.14.6   The fmod Function
  596.              C.14.7   The signal Function
  597.              C.14.8   Default Signals
  598.              C.14.9   The SIGILL Signal
  599.              C.14.10  Terminating Newline Characters
  600.              C.14.11  Blank Lines
  601.              C.14.12  Null Characters
  602.              C.14.13  File Position in Append Mode
  603.              C.14.14  Truncation of Text Files
  604.              C.14.15  File Buffering
  605.              C.14.16  Zero-Length Files
  606.              C.14.17  File Names
  607.              C.14.18  File Access Limits
  608.              C.14.19  Deleting Open Files
  609.              C.14.20  Renaming with a Name that Exists
  610.              C.14.21  Printing Pointer Values
  611.              C.14.22  Reading Pointer Values
  612.              C.14.23  Reading Ranges
  613.              C.14.24  File Position Errors
  614.              C.14.25  Messages Generated by the perror Function
  615.              C.14.26  Allocating Zero Memory
  616.              C.14.27  The abort Function
  617.              C.14.28  The atexit Function
  618.              C.14.29  Environment Names
  619.              C.14.30  The system Function
  620.              C.14.31  The strerror Function
  621.              C.14.32  The Time Zone
  622.              C.14.33  The clock Function
  623.  
  624.  Index
  625.  
  626.  
  627.  
  628.  
  629.  Introduction
  630.  ────────────────────────────────────────────────────────────────────────────
  631.  
  632.  Advanced Programming Techniques describes how to get the most out of the
  633.  Microsoft(R) C Professional Development System with its new integrated
  634.  development environment─the Microsoft Programmer's WorkBench─and
  635.  source-level debugging tool─the CodeView(R) debugger.
  636.  
  637.  In this manual, you will see how all the components of the Microsoft C
  638.  Professional Development System work together to provide you with the most
  639.  powerful development environment available. A key element in the power of
  640.  the Professional Development System is your ability to customize it to suit
  641.  your individual needs as a programmer.
  642.  
  643.  Because this book is arranged by topic, it answers questions about using
  644.  Microsoft C version 6.0, rather than providing lists of options. If you have
  645.  specific questions about menu items in the CodeView debugger, the
  646.  Programmer's WorkBench, or any of the command-line utilities included in the
  647.  Professional Development System, you can use the Microsoft C Advisor
  648.  (on-line help) or the C Reference manual.
  649.  
  650.  Advanced Programming Techniques shows you how tools and utilities all fit
  651.  together.
  652.  
  653.  
  654.  Scope of This Book
  655.  
  656.  Advanced Programming Techniques is divided into four parts. Part 1,
  657.  "Improving Program Performance," helps you write more efficient programs. It
  658.  provides specific information about optimizing─when and why to use various
  659.  optimizing options. It also explains new memory management options and when
  660.  to use them. For example, Chapter 3 describes the in-line assembler, a new
  661.  feature that lets you mix assembly language with your C source code.
  662.  
  663.  Part 2, "Improving Programmer Productivity," will help you perform
  664.  programming tasks more quickly and efficiently. Chapter 8 explains the
  665.  different ways you can customize the new Programmer's WorkBench (PWB)─an
  666.  editor and integrated development environment that allows you to
  667.  
  668.  
  669.    ■   Create new programs
  670.  
  671.    ■   Modify existing programs
  672.  
  673.    ■   Browse source files
  674.  
  675.    ■   Obtain help about PWB, the C language, and the C run-time libraries
  676.  
  677.    ■   Set program build lists
  678.  
  679.    ■   Build programs
  680.  
  681.    ■   Debug programs with the CodeView debugger
  682.  
  683.  
  684.  Chapter 8 also describes how to change PWB behavior to suit your programming
  685.  style by making keyboard assignments, recording or writing macros, and
  686.  writing C extensions.
  687.  
  688.  Also in Part 2 is a chapter about the Microsoft Program Maintenance Utility,
  689.  NMAKE. NMAKE is a new program maintenance facility that allows you to use
  690.  program lists as input, which provides extra flexibility in your program
  691.  build process. It is a superset of the Microsoft XENIX(R) MAKE utility and
  692.  is substantially more powerful than previous versions of MAKE.
  693.  
  694.  Chapter 9 in Part 2 describes the CodeView debugger, which is even more
  695.  powerful than in previous releases. With CodeView version 3.0, you get many
  696.  new features, including the ability to record a debugging session, then play
  697.  it back (history and dynamic replay).
  698.  
  699.  Part 3, "Special Environments," describes new graphics capabilities. It also
  700.  shows how to program in mixed languages and offers tips to make your
  701.  programs more portable. Microsoft C helps you create graphics applications
  702.  easily. The Microsoft C run-time libraries contain graphics functions for
  703.  low-level graphics operations, such as drawing lines, rectangles, and
  704.  circles. The libraries also contain functions for creating presentation
  705.  graphics, such as pie charts and bar charts.
  706.  
  707.  Part 4, "OS/2 Support," describes how the Professional Development System
  708.  helps you build OS/2 applications. The three chapters in Part 4 provide
  709.  information about dual-mode applications, creating multithread applications,
  710.  and creating dynamic-link libraries.
  711.  
  712.  A postage-paid documentation feedback card is at the end of this manual.
  713.  After you have had a chance to become familiar with Microsoft C 6.0 and its
  714.  documentation, please give us your opinion. Your ideas will help us as we
  715.  develop future documentation. Also at the end of this book is a Product
  716.  Assistance Request form. If you need to call Microsoft for assistance, use
  717.  this form first to compile and organize pertinent information.
  718.  
  719.  
  720.  Document Conventions
  721.  
  722.  ────────────────────────────────────────────────────────────────────────────
  723.  NOTE
  724.  
  725.  The pages that follow use the term "OS/2" to refer to the OS/2
  726.  systems─Microsoft Operating System/2 (MS(R) OS/2) and IBM(R) OS/2.
  727.  Similarly, the term "DOS" refers to both the MS-DOS(R) and IBM Personal
  728.  Computer DOS operating systems. The name of a specific operating system is
  729.  used when it is necessary to note features that are unique to the system.
  730.  
  731.  
  732.  ────────────────────────────────────────────────────────────────────────────
  733.  
  734.  Example                           Description
  735.  ────────────────────────────────────────────────────────────────────────────
  736.  STDIO.H                           Uppercase letters indicate file names,
  737.                                    segment names, registers, and terms used
  738.                                    at the DOS- or OS/2-command level.
  739.  
  740.  _cdecl                            Boldface letters indicate C keywords,
  741.                                    operators, language-specific characters,
  742.                                    and library functions, as well as OS/2
  743.                                    functions. Within discussions of syntax,
  744.                                    bold type indicates that the text must
  745.                                    be entered exactly as shown.
  746.  
  747.  expression                        Words in italics indicate placeholders
  748.                                    for information you must supply, such as
  749.                                    a file name. Italics are also
  750.                                    occasionally used for emphasis in the
  751.                                    text.
  752.  
  753.  «option»                          Items inside double square brackets are
  754.                                    optional.
  755.  
  756.  #pragma pack {1|2}                Braces and a vertical bar indicate a
  757.                                    choice among two or more items. You must
  758.                                    choose one of these items unless double
  759.                                    square brackets surround the braces.
  760.  
  761.  CL A.C B.C C.OBJ                  This font is used for examples, user
  762.                                    input, program output, and error
  763.                                    messages in text.
  764.  
  765.  CL options « files...»            A horizontal ellipsis following an item
  766.                                    indicates that more items having the
  767.                                    same form may follow.
  768.  
  769.  while(   )                        A vertical ellipsis tells you that part
  770.  {                                 of the example program has been
  771.     .                              intentionally omitted.
  772.     .
  773.     .
  774.  }
  775.  
  776.  CTRL+ENTER                        Small capital letters are used for the
  777.                                    names of keys on the keyboard. When you
  778.                                    see a plus sign (+) between two key
  779.                                    names, you should hold down the first
  780.                                    key while pressing the second.
  781.  
  782.                                    The carriage-return key (sometimes
  783.                                    appearing as a bent arrow on the
  784.                                    keyboard) is called ENTER.
  785.  
  786.                                    The cursor-movement keys (sometimes
  787.                                    called direction keys) are called the
  788.                                    ARROW keys. Individual keys are referred
  789.                                    to by their direction (LEFT, UP) or by
  790.                                    the name on the key (PGUP).
  791.  
  792.  "argument"                        Quotation marks enclose a new term the
  793.                                    first time it is defined in text.
  794.  
  795.  Enhanced Graphics Adapter (EGA)   The first time an acronym is used, it is
  796.                                    often spelled out.
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  PART I  Improving Program Performance
  804.  ────────────────────────────────────────────────────────────────────────────
  805.  
  806.  The Microsoft C Professional Development System helps you create the
  807.  fastest, smallest applications using its sophisticated optimizer and
  808.  enhanced memory management capabilities.
  809.  
  810.  Chapter 1 tells when to use certain optimizations and describes how
  811.  Microsoft C generates code that is efficient in execution speed and size.
  812.  Chapter 2 explains the sophisticated tools Microsoft C gives you to allocate
  813.  and manage program memory, including the new _based type. For cases where
  814.  your program requires localized optimization, you can use the in-line
  815.  assembler, described in Chapter 3, to introduce the tightest possible code.
  816.  If your application requires floating-point math computations, you will find
  817.  Chapter 4 helpful in explaining the options in the Microsoft C math
  818.  packages; it explains which floating-point options yield the fastest,
  819.  smallest, and most flexible code.
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  Chapter 1  Optimizing C Programs
  827.  ────────────────────────────────────────────────────────────────────────────
  828.  
  829.  The Microsoft C compiler translates C source statements into
  830.  machineexecutable instructions. In addition, the compiler rewrites or
  831.  "optimizes"  parts of your program to make it more efficient in ways that
  832.  are not apparent at the source level.
  833.  
  834.  The compiler performs three general types of optimization:
  835.  
  836.  
  837.    1.  It modifies or moves sections of code so that fewer instructions are
  838.        used, or so that the instructions used make more efficient use of the
  839.        processor.
  840.  
  841.    2.  It moves code and combines operations to maximize use of registers
  842.        because operations on data stored in processor registers are far
  843.        faster than the same operations on data stored in memory.
  844.  
  845.    3.  It eliminates sections of code that are redundant or unused.
  846.  
  847.  
  848.  This chapter explains the various ways you can control how the Microsoft C
  849.  compiler optimizes your code.
  850.  
  851.  
  852.  1.1  Controlling Optimization from the Programmer's WorkBench
  853.  
  854.  The Programmer's WorkBench (PWB) is an integrated development environment
  855.  for editing, building, and debugging applications written in Microsoft C.
  856.  For more information on the PWB, see Installing and Using the Microsoft C
  857.  Professional Development System.
  858.  
  859.  There are two ways to compile from inside the Programmer's WorkBench:
  860.  
  861.  
  862.    1.  Debug compile. In a default debug compile, the compiler performs no
  863.        optimizations at all.
  864.  
  865.    2.  Release compile. In a default release compile, the compiler performs
  866.        most optimizations.
  867.  
  868.  
  869.  By modifying the settings in C Global Build Options, C Debug Build Options,
  870.  and C Release Build Options (on the Options menu), you can fine-tune
  871.  optimization by individually enabling or disabling any of the optimizations
  872.  the compiler performs.
  873.  
  874.  The optimizations in each of the Build Options dialog boxes correspond to a
  875.  command-line option to CL. (In fact, the PWB constructs a command line from
  876.  your input and passes it to CL.)
  877.  
  878.  ────────────────────────────────────────────────────────────────────────────
  879.  NOTE
  880.  
  881.  In this chapter, optimization options are discussed in terms of the effect
  882.  of the optimization, the command-line option to invoke the optimization, and
  883.  pragmas that control the optimization. All of these optimizations can be
  884.  controlled at the compilation-unit (file) level using the Build Options
  885.  dialog boxes.
  886.  ────────────────────────────────────────────────────────────────────────────
  887.  
  888.  
  889.  1.2  Controlling Optimization from the Command Line
  890.  
  891.  Controlling optimization from the command line requires that you determine
  892.  which optimizations you need for your application. You then specify those
  893.  optimizations using command-line options that begin with /O (and in some
  894.  cases /G).
  895.  
  896.  If there is any conflict between options, the compiler uses the last option
  897.  specified on the command line. The command line
  898.  
  899.    CL /Oa /Ol /Ot TEST.C
  900.  
  901.  compiles the program TEST.C. It specifies that the compiler can
  902.  
  903.  
  904.    ■   Optimize on the assumption that you are doing no aliasing (/Oa)
  905.  
  906.    ■   Perform loop optimization (/Ol)
  907.  
  908.    ■   Perform other general speed-enhancing optimizations (/Ot)
  909.  
  910.  
  911.  The preceding command line can also be written
  912.  
  913.    CL /Oalt TEST.C
  914.  
  915.  
  916.  1.3  Controlling Optimization with Pragmas
  917.  
  918.  Occasionally you will need to exercise a fine level of control over compiler
  919.  optimizations. Command-line options allow you to control optimization over
  920.  an entire compilation unit (file). In addition, Microsoft C supports several
  921.  pragmas that allow you to exercise such control on a per-function basis.
  922.  
  923.  The pragmas that control optimization are described in this chapter under
  924.  the type of optimization they affect.
  925.  
  926.   The optimize pragma is new to version 6.0.
  927.  
  928.  In version 6.0, you can control each of the following optimization
  929.  parameters on a function-by-function basis using the optimize pragma:
  930.  
  931.  
  932.    ■   Behavior of code with respect to aliasing (a and w)
  933.  
  934.    ■   Reduction of local common subexpressions (c)
  935.  
  936.    ■   Reduction of global common subexpressions (g)
  937.  
  938.    ■   Global register allocation (e)
  939.  
  940.    ■   Loop optimization (l)
  941.  
  942.    ■   Aggressiveness of optimizations (z)
  943.  
  944.    ■   Disabling of unsafe optimizations (n)
  945.  
  946.    ■   Achieving consistent floating-point results (p)
  947.  
  948.    ■   Optimizing for smaller code size or for faster execution speed (t)
  949.  
  950.  
  951.  Any optimization or combination of options can be enabled or disabled using
  952.  the optimize pragma. For example, if you have one function that uses aliases
  953.  heavily, you need to inhibit optimizations that could cause problems with
  954.  aliases. You do not, however, want to inhibit these optimizations for code
  955.  that does not do aliasing. To do this, use the optimize pragma as follows:
  956.  
  957.    /* Function(s) that do not do aliasing. */
  958.    .
  959.    .
  960.    .
  961.    #pragma optimize( "a", off )
  962.    /* Function(s) that do aliasing. */
  963.    .
  964.    .
  965.    .
  966.    #pragma optimize( "a", on )
  967.    /* More function(s) that do not do aliasing. */
  968.  
  969.  The parameters to the optimize pragma can be combined in a string to enable
  970.  or disable multiple options at once. For example,
  971.  
  972.    #pragma optimize( "lge", off )
  973.  
  974.  disables loop optimization, global common subexpression optimization, and
  975.  global register allocation.
  976.  
  977.  
  978.  1.4  Default Optimization
  979.  
  980.  Many optimizations are not explicitly disabled by any command-line option
  981.  except /Od (disable optimizations). These optimizations are small in scope
  982.  and are almost always helpful. They include
  983.  
  984.  
  985.    ■   Short range common subexpression elimination
  986.  
  987.    ■   Dead-store elimination
  988.  
  989.    ■   Constant propagation
  990.  
  991.  
  992.  
  993.  1.4.1  Common Subexpression Elimination
  994.  
  995.  In common subexpression elimination, the compiler finds code containing
  996.  repeated subexpressions and produces modified code in which the
  997.  subexpressions are evaluated only once. Subexpression elimination is usually
  998.  done with temporary variables as shown in the following example:
  999.  
  1000.    a = b + c * d;
  1001.    x = c * d / y;
  1002.  
  1003.  The preceding two lines contain the common subexpression  c * d. This code
  1004.  can be modified to evaluate  c * d  only once; the result is placed in a
  1005.  temporary variable (usually a register):
  1006.  
  1007.    tmp = c * d;
  1008.    a = b + tmp;
  1009.    x = tmp / y;
  1010.  
  1011.  
  1012.  1.4.2  Dead-Store Elimination
  1013.  
  1014.  Dead-store elimination is an extension of common subexpression elimination.
  1015.  Variables that contain the same value in a short piece of code can be
  1016.  combined into a single temporary variable.
  1017.  
  1018.  In the following code fragment, the compiler detects that the expression
  1019.  func( x )  is equivalent to  func( a + b ):
  1020.  
  1021.    x = a + b;
  1022.    x = func( x );
  1023.  
  1024.  Thus, the compiler can rewrite the code as follows:
  1025.  
  1026.    x = func( a + b);
  1027.  
  1028.  
  1029.  1.4.3  Constant Propagation
  1030.  
  1031.  When doing constant propagation, the compiler analyzes variable assignments
  1032.  and determines if they can be changed to constant assignments. In the
  1033.  following example, the variable  i  must have a value of  7  when it is
  1034.  assigned to  j:
  1035.  
  1036.    i = 7;
  1037.    j = i;
  1038.  
  1039.  Instead of assigning  i  to  j, the constant  7  can be assigned to  j:
  1040.  
  1041.    i = 7;
  1042.    j = 7;
  1043.  
  1044.  While you could make any of these changes in the source file, doing so might
  1045.  reduce the readability of the program. In many cases, optimizations not only
  1046.  increase the efficiency of the program but allow you to write more readable
  1047.  code without any actual efficiency loss.
  1048.  
  1049.   Remove optimization before using a symbolic debugger.
  1050.  
  1051.  In some cases, you might want to disable even the default optimizations.
  1052.  Because optimizations may rearrange code in the object file, it can become
  1053.  difficult to recognize parts of your code during debugging. It is usually
  1054.  best to remove all optimization before using a symbolic debugger. You can
  1055.  remove all optimization with the /Od (disable optimizations) option.
  1056.  
  1057.  You can disable all optimizations for a function by including the statement
  1058.  #pragma optimize( "", off ). To restore optimization to its former state,
  1059.  use the statement  #pragma optimize( "", on ).
  1060.  
  1061.  
  1062.  1.5  Customizing Your Optimizations
  1063.  
  1064.  The default optimizations are sufficient for many applications, but you may
  1065.  want to tune your programs according to criteria not known to the compiler.
  1066.  The optimization options offer you a way of providing the compiler specific
  1067.  goals for optimizing your code.
  1068.  
  1069.  
  1070.  1.5.1  Choosing Speed or Size (/Ot and /Os)
  1071.  
  1072.  In addition to the default optimizations, the Microsoft C compiler also
  1073.  automatically uses the /Ot option, which optimizes for speed. The /Ot option
  1074.  enables optimizations that increase speed but may also increase size. If you
  1075.  would rather optimize for program size, use the /Os option. The /Os option
  1076.  enables optimizations that decrease program size but may also decrease
  1077.  program speed.
  1078.  
  1079.  To optimize for speed or size on a per-function basis, use the optimize
  1080.  pragma with the t option. The on setting instructs the compiler to optimize
  1081.  for speed; the off setting instructs the compiler to optimize for
  1082.  compactness of code. For example,
  1083.  
  1084.    #pragma optimize( "t", off )    /* Optimize for smallest
  1085.                                      code. */
  1086.    .
  1087.    .
  1088.    .
  1089.    #pragma optimize( "t", on )     /* Optimize for fastest
  1090.                                      code. */
  1091.  
  1092.  
  1093.  1.5.2  Generating Intrinsic Functions (/Oi)
  1094.  
  1095.  In place of some normal function calls, the C compiler can insert "intrinsic
  1096.  functions," which operate more quickly. Every time a function is called, a
  1097.  set of instructions must be executed to store parameters and to create space
  1098.  for local variables. When the function returns, more code must be executed
  1099.  to release space used by local variables and parameters and to return values
  1100.  to the calling routine. These instructions take time to execute. In the
  1101.  context of an average-sized function, the additional code is minimal, but if
  1102.  the function is only a line or two, the additional code can comprise almost
  1103.  half of the function's compiled code.
  1104.  
  1105.  One way to avoid this type of code expansion is to avoid such short
  1106.  functions, especially in often-used sections of code where speed is
  1107.  critical. But many library functions contain only a line or two of code. The
  1108.  compiler provides two forms of certain library functions. One form is a
  1109.  standard C function, which requires the overhead of a function call. The
  1110.  other form is a set of instructions that
  1111.  
  1112.  performs the same action as the function without issuing a function call.
  1113.  This second form is called an intrinsic function. Intrinsic functions are
  1114.  always faster than their function-call equivalents and can provide
  1115.  significant optimizations at the object-code level.
  1116.  
  1117.  For example, the function strcpy might be written as follows:
  1118.  
  1119.    int strcpy(char * dest, char * source)
  1120.    {
  1121.        while( *dest++ = *source++ );
  1122.    }
  1123.  
  1124.  The compiler contains an intrinsic form of strcpy. If you instruct the
  1125.  compiler to generate intrinsic functions, any call to strcpy will be
  1126.  replaced with this intrin-sic form.
  1127.  
  1128.  ────────────────────────────────────────────────────────────────────────────
  1129.  NOTE
  1130.  
  1131.  While the example above is written in C for clarity, most of the library
  1132.  functions use assembly language to take full advantage of the 80x86
  1133.  instruction set. Intrinsic functions are not simply library functions
  1134.  defined as macros.
  1135.  ────────────────────────────────────────────────────────────────────────────
  1136.  
  1137.  Compiling with the /Oi option causes the compiler to use the intrinsic forms
  1138.  of the following functions:
  1139.  
  1140.  abs             labs            memset          strcat
  1141.  _disable        lrotl           outp            strcmp
  1142.  _enable         lrotr           outpw           strcpy
  1143.  fabs            memcmp          rotl            strlen
  1144.  inp             memcpy          rotr            strset
  1145.  inpw
  1146.  
  1147.  While the following floating-point functions do not have true intrinsic
  1148.  forms, they do have versions that pass arguments directly to the
  1149.  floating-point chip instead of pushing them on the normal argument stack:
  1150.  
  1151.  acos            fmod            acosl           fmodl
  1152.  asin            log             asinl           logl
  1153.  atan            log10           atanl           log10l
  1154.  atan2           pow             atan2l          powl
  1155.  ceil            sin             ceill           sinl
  1156.  cos             sinh            cosl            sinhl
  1157.  cosh            sqrt            coshl           sqrtl
  1158.  exp             tan             expl            tanl
  1159.  floor           tanh            floorl          tanhl
  1160.  
  1161.  ────────────────────────────────────────────────────────────────────────────
  1162.  WARNING
  1163.  
  1164.  The compiler performs optimizations assuming math intrinsics have no side
  1165.  effects. This assumption is true except if you have written your own matherr
  1166.  function and that function alters global variables. If you have written a
  1167.  matherr function to handle floating-point errors, and your function has side
  1168.  effects, use the function pragma to instruct the compiler not to generate
  1169.  intrinsic code for math functions.
  1170.  ────────────────────────────────────────────────────────────────────────────
  1171.  
  1172.  
  1173.  If you want the compiler to generate intrinsic functions for only a subset
  1174.  of the functions listed above, use the intrinsic pragma rather than the /Oi
  1175.  option. The intrinsic pragma has the following format:
  1176.  
  1177.    #pragma intrinsic( function1, ... )
  1178.  
  1179.  If you want to have intrinsic functions generated for most of the functions
  1180.  above and function calls for only a few, compile with the /Oi option and
  1181.  force function use with the function pragma. The function pragma has the
  1182.  following format:
  1183.  
  1184.    #pragma function( function1, ... )
  1185.  
  1186.  The following code illustrates the use of the intrinsic pragma:
  1187.  
  1188.    #pragma intrinsic(abs)
  1189.  
  1190.    void main( void )
  1191.    {
  1192.        int i, j;
  1193.  
  1194.        i = big_routine_1();
  1195.        j = abs( i );
  1196.        big_routine_2( j );
  1197.    }
  1198.  
  1199.  Generating intrinsic functions for this program causes the call to abs to be
  1200.  replaced with assembly-language code that takes the absolute value of a
  1201.  number. The program will execute more quickly because the function-calling
  1202.  overhead is no longer required when abs is called.
  1203.  
  1204.  In the previous example, the overall speed increase is small because there
  1205.  is only a single call to abs. In the following example, where the call to
  1206.  abs is in a loop and there are many calls, you can save a significant amount
  1207.  of execution time by generating intrinsic functions.
  1208.  
  1209.    #pragma intrinsic( abs )
  1210.    void main( void )
  1211.    {
  1212.    int i, j, x;
  1213.  
  1214.    for( j = 0; j < 1000; j++ )
  1215.        {
  1216.            for( i = 0; i < 1000; i++)
  1217.            {
  1218.                x += abs( i - j );
  1219.            }
  1220.       }
  1221.       printf( "The value of x is %d\n", x );
  1222.    }
  1223.  
  1224.  The following is a list of restrictions on using the intrinsic forms of
  1225.  function calls:
  1226.  
  1227.  
  1228.    ■   Do not use the intrinsic forms of the floating-point math functions
  1229.        with the alternate math libraries (mLIBCAy.LIB).
  1230.  
  1231.    ■   Do not use the intrinsic forms of the floating-point math functions in
  1232.        OS/2 dynamic-link libraries (DLLs) because you must use the alternate
  1233.        math library with LLIBCDLL.LIB.
  1234.  
  1235.    ■   If you use the /Ox (maximum optimization) option, you are enabling the
  1236.        /Oi (generate intrinsic functions) option. Be careful that your use of
  1237.        /Ox does not conflict with the points listed previously.
  1238.  
  1239.  
  1240.  ────────────────────────────────────────────────────────────────────────────
  1241.  NOTE
  1242.  
  1243.  Intrinsic versions of _enable, _disable, inp, outp, inpw, and outpw
  1244.  do not work under OS/2. You must use the library versions. You can use the
  1245.  function pragma to force these functions to become library calls.
  1246.  ────────────────────────────────────────────────────────────────────────────
  1247.  
  1248.  
  1249.  1.5.3  Assuming No Aliasing (/Oa and /Ow)
  1250.  
  1251.  An "alias" is a name used to refer to a memory location already referred to
  1252.  by a different name. Because a memory access takes more time than it takes
  1253.  to access the CPU's registers, the compiler tries to store frequently used
  1254.  variables in registers. However, the aliasing reduces the extent to which a
  1255.  compiler can keep variables in registers.
  1256.  
  1257.  A pointer is a reference to a memory location. Because the value of a
  1258.  pointer is not determined until the program is run, the compiler has no way
  1259.  of knowing which memory location will be modified when the program executes;
  1260.  it could be a reference to a variable. Therefore, the compiler must assume
  1261.  that any time the value pointed to by any pointer changes, the value of any
  1262.  variable might also change. This limits the extent to which the compiler can
  1263.  move values from memory to registers.
  1264.  
  1265.  The /Oa option tells the compiler to ignore the possibility of multiple
  1266.  aliases for a memory location. In the list that follows, the term
  1267.  "reference" means read or write; that is, whether a variable is on the
  1268.  left-hand side of an assignment statement or the right-hand side, you are
  1269.  still referring to it. In addition, any function calls that use a variable
  1270.  as a parameter are references to that variable. When you tell the compiler
  1271.  to assume that you are not doing aliasing, it expects that the following
  1272.  rules are being followed for any variable not declared as volatile:
  1273.  
  1274.  
  1275.    ■   If a variable is used directly, no pointers are used to reference that
  1276.        variable.
  1277.  
  1278.    ■   If a pointer is used to refer to a variable, that variable is not
  1279.        referred to directly.
  1280.  
  1281.    ■   If a pointer is used to modify a memory location, no other pointers
  1282.        are used to access the same memory location.
  1283.  
  1284.  
  1285.  To clarify how these rules affect your code, consider the following example:
  1286.  
  1287.  
  1288.    char    p;
  1289.    char    *ptr_p;
  1290.  
  1291.    ptr_p = &p;    /* Take the address of p. */
  1292.  
  1293.  You can now refer either to  *ptr_p  or to  p, but not to both within the
  1294.  same function. If you must refer to the variable by both names, you are
  1295.  using aliases.
  1296.  
  1297.  Code referring to the same location with two pointers uses aliases. For
  1298.  example,
  1299.  
  1300.    char    *p_buf;
  1301.    char    *p_alias;
  1302.  
  1303.    if( (p_alias = p_buf = malloc( 5000 )) == NULL )
  1304.        return;
  1305.    else
  1306.    {
  1307.        .
  1308.        .
  1309.        .
  1310.    }
  1311.  
  1312.  The code in the example above is common. It demonstrates dynamically
  1313.  allocating a block of memory from the heap, and preserving the original
  1314.  address in  p_buf. The program then performs all pointer arithmetic on the
  1315.  alias  p_alias. When the function finishes with the block of memory,  p_buf
  1316.  is a valid argument for the free function because it still contains the
  1317.  original address.
  1318.  
  1319.   The /Oa and /Ow options tell the compiler that you have not used aliases in
  1320.  your code.
  1321.  
  1322.  The difference between the /Oa and the /Ow option is that when you use /Oa
  1323.  you specify that you will not be doing aliasing (which allows the compiler
  1324.  to perform significant optimizations that might not otherwise have been
  1325.  possible), and that function calls are safe. The /Ow option is similar to
  1326.  the /Oa option, except that after a function call, pointer variables must be
  1327.  reloaded from memory.
  1328.  
  1329.  Here is an example of a program that would be a poor candidate for the /Oa
  1330.  or /Ow optimization option:
  1331.  
  1332.    int g;
  1333.  
  1334.    void main( void )
  1335.    {
  1336.        add_em( &g );
  1337.    }
  1338.  
  1339.    int add_em( int *p )
  1340.    {
  1341.        *p = 2;          /* Assign a value to an alias for g. */
  1342.        g = 3;           /* Assign a value directly to g. */
  1343.        return( *p + g );
  1344.    }
  1345.  
  1346.  In the function  add_em, both  g  and  *p  refer to the same memory
  1347.  location. This location is first assigned  2, then  3. The value pointed to
  1348.  by  *p  (the alias for  g) is then added to  g, and the result is returned
  1349.  to the main program. If you do not use the /Oa command-line option, the
  1350.  compiler assumes that the reference to  *p  could refer to the same memory
  1351.  location as does  g  and makes no attempt to use a register to store the
  1352.  value of either. If, however, you do specify the /Oa option, the compiler
  1353.  assumes that  g  and  *p  refer to different memory locations and stores
  1354.  each in a different register. At the return statement,  g  will have a
  1355.  different value than  *p, even though both aliases should actually contain
  1356.  the same value.
  1357.  
  1358.  Note that the compiler keeps values in registers for only a limited time. If
  1359.  different aliases to a memory location occur in different functions, for
  1360.  example, they will not cause unexpected results. When in doubt, avoid
  1361.  aliasing.
  1362.  
  1363.   Bugs involving aliasing are difficult to spot.
  1364.  
  1365.  Aliasing bugs most frequently show up as corruption of data. If you find
  1366.  that global or local variables are being assigned seemingly random values,
  1367.  take the following steps to determine if you have a problem with
  1368.  optimization and aliasing:
  1369.  
  1370.  
  1371.    ■   Compile the program with /Od (disable optimizations).
  1372.  
  1373.    ■   If the program works when compiled with the /Od option, check your
  1374.        normal compile options for the /Oa option (assume no aliasing).
  1375.  
  1376.    ■   If you were using the /Oa option, fix your compile options so that /Oa
  1377.        is not specified.
  1378.  
  1379.  
  1380.  ────────────────────────────────────────────────────────────────────────────
  1381.  NOTE
  1382.  You can instruct the compiler to disable optimizations that are unsafe with
  1383.  code that does aliasing by using the optimize pragma with the a or w option.
  1384.  ────────────────────────────────────────────────────────────────────────────
  1385.  
  1386.  
  1387.  1.5.4  Performing Loop Optimizations (/Ol)
  1388.  
  1389.  The /Ol option enables a set of optimizations involving loops. Because loops
  1390.  involve sections of code that are executed repeatedly, they are targets for
  1391.  optimization. These optimizations all involve moving code or rewriting code
  1392.  so that it executes faster.
  1393.  
  1394.  Loop optimization can be turned on with the /Ol option or with the loop_opt
  1395.  pragma. The following line enables loop optimization for all subsequent
  1396.  functions:
  1397.  
  1398.    #pragma loop_opt( on )
  1399.  
  1400.  The following line turns it off:
  1401.  
  1402.    #pragma loop_opt( off )
  1403.  
  1404.   The /Ol option removes invariant code.
  1405.  
  1406.  An optimal loop contains only expressions whose values change through each
  1407.  execution of the loop. Any subexpression whose value is constant should be
  1408.  evaluated before the body of the loop is executed. Unfortunately, these
  1409.  subexpressions are not always readily apparent. The optimizer can remove
  1410.  many of these expressions from the body of a loop at compile time. This
  1411.  example illustrates invariant code in a loop:
  1412.  
  1413.    i = -100;
  1414.    while( i < 0 )
  1415.    {
  1416.        i += x + y;
  1417.    }
  1418.  
  1419.  In the preceding example, the expression  x + y  does not change in the loop
  1420.  body. Loop optimization removes this subexpression from the body of the loop
  1421.  so that it is only executed once, not every time the loop body is executed.
  1422.  The optimizer will change the code to the following fragment:
  1423.  
  1424.    i = -100;
  1425.    t = x + y;
  1426.    while( i < 0 )
  1427.    {
  1428.        i += t;
  1429.    }
  1430.  
  1431.  Loop optimization is much more effective when the compiler can assume no
  1432.  aliasing. While you can use loop optimization without the /Oa or /Ow option,
  1433.  use /Oa to ensure that the most options possible are used.
  1434.  
  1435.  Here is a code fragment that could have an aliasing problem:
  1436.  
  1437.    i = -100;
  1438.    while( i < 0 )
  1439.    {
  1440.        i += x + y;
  1441.        *p = i;
  1442.    }
  1443.  
  1444.  If you do not specify the /Oa option, the compiler must assume that either
  1445.  x  or  y  could be modified by the assignment to  *p. Therefore, the
  1446.  compiler cannot assume the subexpression  x + y  is constant for each loop
  1447.  iteration. If you specify that you are not doing any aliasing (with the /Oa
  1448.  option), the compiler assumes that modifying  *p  cannot affect either  x
  1449.  or  y, and that the subexpression is indeed constant and can be removed from
  1450.  the loop, as in the previous example.
  1451.  
  1452.  ────────────────────────────────────────────────────────────────────────────
  1453.  NOTE
  1454.  
  1455.  All loop optimizations specified by the /Ol option or the loop_opt pragma
  1456.  are safe optimizations. To enable aggressive loop optimizations, you must
  1457.  use the enable aggressive optimizations (/Oz) option. While the
  1458.  optimizations enabled by the combination of /Ol and /Oz are not safe for all
  1459.  cases, they will work properly for most programs.
  1460.  ────────────────────────────────────────────────────────────────────────────
  1461.  
  1462.  
  1463.  1.5.5  Disabling Unsafe Loop Optimizations (/On)
  1464.  
  1465.  The disable unsafe loop optimizations (/On) option is an obsolescent option
  1466.  and is only retained for compatibility with existing makefiles. Loop
  1467.  optimizations are, by default, safe optimizations. The /On option is the
  1468.  default and has the opposite effect of the /Oz (enable aggressive
  1469.  optimizations) option.
  1470.  
  1471.  
  1472.  1.5.6  Enabling Aggressive Optimizations (/Oz)
  1473.  
  1474.  The compiler can perform extremely aggressive optimizations. These
  1475.  optimizations produce high code quality both in terms of speed and size.
  1476.  Certain programs, however, cannot be optimized with the technologies enabled
  1477.  by the /Oz option. For these programs, you should not specify this option;
  1478.  you can still use all other optimization options.
  1479.  
  1480.  Because the optimization strategies enabled by the /Oz option are so
  1481.  aggressive, they are not part of the maximum optimization (/Ox) option.
  1482.  
  1483.  Examples of the effects of the /Oz option are
  1484.  
  1485.  
  1486.    ■   Loop optimization (/Ol). Loop optimization enables a technology that
  1487.        anticipates program flow and tries to remove invariant expressions
  1488.        from loops. When you specify the enable aggressive optimizations
  1489.        option (/Oz), the compiler removes invariant expressions even when it
  1490.        might cause an error. Errors with the enable aggressive optimizations
  1491.        option occur most often when an invariant expression that can cause an
  1492.        exception is protected by an if statement. The invariant expression is
  1493.        hoisted out of the loop body, causing it to be evaluated prior to the
  1494.        evaluation of the if statement that was designed to protect it. Here
  1495.        are two examples that illustrate this problem:
  1496.  
  1497.          for( i = 0; i  100; ++i )
  1498.              if( float_val != 0.0F )
  1499.              /* Protect against divide-by-zero. */
  1500.                  float_result = pi / float_val;
  1501.  
  1502.  
  1503.          while( condition )
  1504.              if( ptr_val != NULL )
  1505.                /* Protect pointer dereference. */
  1506.                  char_var = *ptr_val;
  1507.  
  1508.  
  1509.    ■   Global register allocation (/Oe). The enable aggressive optimizations
  1510.        option enables some register allocation strategies that can cause
  1511.        invalid segment selectors to be placed in registers. Although this
  1512.        problem is benign in DOS, it causes protection faults in OS/2.
  1513.  
  1514.  
  1515.  ────────────────────────────────────────────────────────────────────────────
  1516.  NOTE
  1517.  
  1518.  You can instruct the compiler to enable aggressive optimizations on a
  1519.  function-by-function basis by using the optimize pragma with the z option.
  1520.  ────────────────────────────────────────────────────────────────────────────
  1521.  
  1522.  
  1523.  1.5.7  Removing Stack Probes (/Gs)
  1524.  
  1525.  Every time a function is called, the stack provides space for all parameters
  1526.  and local variables declared in that function. A short assembly function
  1527.  that checks for a stack overflow condition is then called. Stack overflows
  1528.  are usually caused either by infinite loops or by runaway recursive
  1529.  routines. Such errors can also be caused by extremely large parameters or
  1530.  local variables.
  1531.  
  1532.  Stack probes can be important during program development. Stack-overflow
  1533.  errors alert you to problems in your code. When the program has been tested,
  1534.  however, stack checking often becomes unnecessary. The compiler allows you
  1535.  to remove stack-checking code with either the /Gs option or the check_stack
  1536.  pragma. Eliminating stack probes produces programs that are smaller and that
  1537.  run more quickly.
  1538.  
  1539.  
  1540.  1.5.8  Enabling Global Register Allocation (/Oe)
  1541.  
  1542.  The global register allocation option (/Oe) instructs the compiler to
  1543.  analyze your program and allocate CPU registers as efficiently as possible.
  1544.  Without the global register allocation option, the compiler uses the CPU's
  1545.  registers for several purposes:
  1546.  
  1547.  
  1548.    ■   Holding temporary copies of variables
  1549.  
  1550.    ■   Holding variables declared with the register keyword
  1551.  
  1552.    ■   Passing parameters to functions declared with the _fastcall keyword
  1553.        (or functions in programs compiled with the /Gr command-line option)
  1554.  
  1555.  
  1556.  When you enable global register allocation, the compiler ignores the
  1557.  register keyword and allocates register storage to variables (and possibly
  1558.  to common subexpressions). The compiler allocates register storage to
  1559.  variables or subexpressions according to frequency of use. Because of the
  1560.  limited number of physical registers, variables held in registers are
  1561.  sometimes placed back in memory to free the register for another use. Here
  1562.  is a C program example that demonstrates how the compiler might rewrite your
  1563.  code to accomplish this:
  1564.  
  1565.    /* Original program */
  1566.  
  1567.    func()
  1568.    {
  1569.        int i, j;
  1570.        char *pc;
  1571.  
  1572.        for( i = 0; i < 1000; ++i )
  1573.        {
  1574.            j = i / 3;
  1575.            *pc++ = (char)i;
  1576.        }
  1577.  
  1578.        for( j = 0, --pc; j < 1000;
  1579.            ++j, --pc )
  1580.            *pc--;
  1581.    }
  1582.  
  1583.    /* Example of how the compiler might optimize the
  1584.     * code to move i and j in and out of registers */
  1585.  
  1586.    func()
  1587.    {
  1588.        int i, j;
  1589.        char *pc;
  1590.  
  1591.        {
  1592.        register int i; /* i is in a register for this block. */
  1593.            for( i = 0; i < 1000; ++i )
  1594.            {
  1595.                j = i / 3;
  1596.                *pc++ = (char)i;
  1597.            }
  1598.        }
  1599.  
  1600.        {
  1601.        register int j; /* j is in a register for this block. */
  1602.            for( j = 0, --pc; j < 1000;
  1603.                ++j, --pc )
  1604.                *pc--;
  1605.        }
  1606.    }
  1607.  
  1608.  In the preceding example, there are blocks (enclosed in curly braces) whose
  1609.  only purpose is to delimit the span of code across which variables should
  1610.  remain in registers.
  1611.  
  1612.  ────────────────────────────────────────────────────────────────────────────
  1613.  NOTE
  1614.  
  1615.  You can enable or disable global register allocation on a
  1616.  function-by-function basis using the optimize pragma with the e option.
  1617.  ────────────────────────────────────────────────────────────────────────────
  1618.  
  1619.  
  1620.  1.5.9  Enabling Common Subexpression Optimization (/Oc and /Og)
  1621.  
  1622.  When you use option /Og (enable global common subexpression optimizations),
  1623.  the compiler searches entire functions for common subexpressions. Option /Oc
  1624.  (default common subexpression optimization) examines only short sections of
  1625.  code for common subexpressions. You can disable default common subexpression
  1626.  optimization with the /Od option. For more information about common
  1627.  subexpression optimization, see Section 1.4, "Default Optimization."
  1628.  
  1629.  ────────────────────────────────────────────────────────────────────────────
  1630.  NOTE
  1631.  
  1632.  You can enable or disable block-scope common subexpression optimization on a
  1633.  function-by-function basis using the optimize pragma with the c option. You
  1634.  can enable or disable global common subexpression optimization on a
  1635.  function-by-function basis using the optimize pragma with the g option.
  1636.  ────────────────────────────────────────────────────────────────────────────
  1637.  
  1638.  
  1639.  1.5.10  Achieving Consistent Floating-Point Results (/Op)
  1640.  
  1641.  Floating-point numbers stored in memory use either 32, 64, or 80 bits,
  1642.  depending on whether they are of type float, type double, or type long
  1643.  double. The 80x87 family of coprocessors uses 80-bit registers for all
  1644.  operations. If a value of type float or type double is kept in these
  1645.  registers through a number of operations, it will be more accurate than if
  1646.  that value is moved to and from memory between operations.
  1647.  
  1648.  Because of the difference in precision between memory and register
  1649.  representation of a floating-point number, a value stored in memory is not
  1650.  always equal to the same value in the 80x87 register.
  1651.  
  1652.  The difference in precision primarily affects strict equality or strict
  1653.  inequality tests (== and !=); however, relational tests of magnitude (>, >=,
  1654.  , and ) can behave erroneously if the coprocessor is able to maintain
  1655.  significant digits that memory variables cannot.
  1656.  
  1657.  You can avoid the difference in precision by using the /Op option. This
  1658.  option forces floating-point values to be written to memory between
  1659.  floating-point operations. While storing these values to memory reduces the
  1660.  precision of floating-point expressions, it also ensures that these
  1661.  expressions will produce consistent results regardless of the rest of the
  1662.  code.
  1663.  
  1664.  You can change the handling of floating-point results on a
  1665.  function-by-function basis using the optimize pragma with the p option.
  1666.  
  1667.  ────────────────────────────────────────────────────────────────────────────
  1668.  NOTE
  1669.  
  1670.  Using the /Op option suppresses other optimizations because the
  1671.  floating-point registers are not available for storage of intermediate
  1672.  results. Because you suppress these optimizations, code compiled with the
  1673.  /Op option executes more slowly than code compiled without this option.
  1674.  Careful coding practices, especially in tests of strict equality and
  1675.  inequality, can alleviate the need for this option.
  1676.  ────────────────────────────────────────────────────────────────────────────
  1677.  
  1678.  
  1679.  1.5.11  Using the 80186, 80188, or 80286 Processor (/G0, /G1, /G2)
  1680.  
  1681.  The compiler generates 8086 object code (/G0) unless you take special steps.
  1682.  Because the newer processors (the 80186, 80188, and 80286) are
  1683.  backwardcompatible with the 8086 instruction set, using this instruction set
  1684.  ensures compatibility with all 80x86-based computers. While you gain
  1685.  compatibility across the entire family of 80x86 processors, you lose the
  1686.  advantage of some of the more powerful instructions in the newer processors.
  1687.  
  1688.  
  1689.  If you know your program will only be running on an 80186, 80188, or 80286
  1690.  processor, you can cause the compiler to generate instructions specific to
  1691.  these processors. These instructions increase the speed of your program, but
  1692.  you lose compatibility with machines that use older processors in the 80x86
  1693.  family. Table 1.1 lists the options for processor-specific code generation:
  1694.  
  1695.  
  1696.  Table 1.1  Processor Compatibility
  1697.  
  1698. ╓┌──────────────────────┌────────────────────────────────────────────────────╖
  1699.  Command-Line Option    Compatible Processors
  1700.  ────────────────────────────────────────────────────────────────────────────
  1701.  /G0                    8088, 8086, 80188, 80186, 80286, 80388, 80486
  1702.  /G1                    80188, 80186, 80286, 80386, 80486
  1703.  Command-Line Option    Compatible Processors
  1704.  ────────────────────────────────────────────────────────────────────────────
  1705. /G1                    80188, 80186, 80286, 80386, 80486
  1706.  /G2                    80286, 80386, 80486
  1707.  ────────────────────────────────────────────────────────────────────────────
  1708.  
  1709.  
  1710.  ────────────────────────────────────────────────────────────────────────────
  1711.  NOTE
  1712.  
  1713.  When developing only for OS/2, always use the /G2 option, because OS/2 does
  1714.  not run on the 8086, 8088, 80186, or 80188. Do not use /G2 for Family
  1715.  Applications because they might be run on machines with 8088, 8086, 80188,
  1716.  or 80186 processors.
  1717.  ────────────────────────────────────────────────────────────────────────────
  1718.  
  1719.  
  1720.  1.5.12  Optimizing for Maximum Efficiency (/Ox)
  1721.  
  1722.  The /Ox option combines a number of different optimizations:
  1723.  
  1724.  
  1725.    ■   Enable global register allocation (/Oe)
  1726.  
  1727.    ■   Enable global common subexpression optimization (/Og)
  1728.  
  1729.    ■   Enable block-scoped common subexpression optimization (/Oc)
  1730.  
  1731.    ■   Generate intrinsic functions (/Oi)
  1732.  
  1733.    ■   Perform loop optimizations (/Ol)
  1734.  
  1735.    ■   Optimize for speed (/Ot)
  1736.  
  1737.    ■   Remove stack probes (/Gs)
  1738.  
  1739.  
  1740.   Use /Ozax /Gr to get the fastest program.
  1741.  
  1742.  The /Ox option does not include several optimizations that can improve code
  1743.  efficiency: /Oa (assume no aliasing), /Oz (enable aggressive optimizations),
  1744.  and /Gr (use fastcall calling convention). Before enabling these
  1745.  optimizations, you should read the sections that describe the /Oa and /Oz
  1746.  options and the fastcall calling convention to determine if they are
  1747.  appropriate for your application.
  1748.  
  1749.   Use the optimize pragma to reduce code size.
  1750.  
  1751.  If you are more concerned with executable file size than execution time, use
  1752.  the /Ox and /Gs options, then issue the optimize pragma as follows:
  1753.  
  1754.    #pragma optimize( "t", off )
  1755.  
  1756.  This set of options produces the smallest possible code, while also
  1757.  performing some speed optimizations.
  1758.  
  1759.  
  1760.  1.6  Linker (LINK) Options that Control Optimization
  1761.  
  1762.  Most code optimization is performed before the object file is produced.
  1763.  There are four optimizations that the linker can perform to speed program
  1764.  execution and reduce the disk space used by an executable file.
  1765.  
  1766.  
  1767.  1.6.1  Enabling Far Call Optimization (/FARCALLTRANSLATION)
  1768.  
  1769.  You can call a function two ways. In a far call, the function is called
  1770.  using both the segment and the offset of the function. This allows a program
  1771.  to call a routine outside a 64K segment. In a near call, both the calling
  1772.  statement and the function must be located in the same segment. Only the
  1773.  offset is used to access the function; the segment address is implicit. You
  1774.  can only use near calls to routines located in the same segment.
  1775.  
  1776.  Because of the architecture of the processor, near function calls execute
  1777.  faster than far calls. The decision to declare functions as near or far is
  1778.  often made when selecting a memory model. As it is difficult to determine
  1779.  where the linker will place a given function in memory, it is impractical
  1780.  for the programmer to choose the way a function is called.
  1781.  
  1782.   Use /FARCALLTRANSLATION with medium, large, and huge model programs.
  1783.  
  1784.  The /FARCALLTRANSLATION option enables far call optimization. When you use
  1785.  this option, any function calls within the same segment as the function
  1786.  being called are converted to near calls. This optimization has no effect if
  1787.  you have selected the tiny, small, or compact model, because all calls are
  1788.  already near calls.
  1789.  
  1790.  The abbreviation for the /FARCALLTRANSLATION option is /F.
  1791.  
  1792.  
  1793.  How /FARCALLTRANSLATION Affects Your Code
  1794.  
  1795.  The linker can perform a form of post-optimization (an optimization that
  1796.  occurs after most of the actual code generation is complete) that translates
  1797.  far calls into near calls when possible. This optimization allows a given
  1798.  function to be called with both near and far calls in the same program. To
  1799.  perform this translation, the linker takes a section of object code such as
  1800.  
  1801.  
  1802.    CALL    FAR    _func
  1803.  
  1804.  where  func  is defined in the current segment, and replaces it with the
  1805.  following code:
  1806.  
  1807.    PUSH    CS
  1808.    CALL    NEAR    _func
  1809.    NOP
  1810.  
  1811.  This substitution works because the linker has inserted  PUSH CS  to place a
  1812.  far return address on the stack.
  1813.  
  1814.   Use /FARCALLTRANSLATION with /PACKCODE.
  1815.  
  1816.  The /FARCALLTRANSLATION option is most effective when used in conjunction
  1817.  with the /PACKCODE option discussed in Section 1.6.2. Using the /PACKCODE
  1818.  option causes far calls that were intersegment to become intrasegment calls.
  1819.  The /FARCALLTRANSLATION feature can then take advantage of the new grouping
  1820.  to translate all intrasegment far calls into near calls.
  1821.  
  1822.  
  1823.  Benefits of /FARCALLTRANSLATION
  1824.  
  1825.  The /FARCALLTRANSLATION option is of significant benefit to protected-mode
  1826.  programs. Table 1.2 illustrates why.
  1827.  
  1828.  Table 1.2  Processor Clock Cycles for Calling Sequence
  1829.  
  1830. ╓┌───────────────────┌──────────────┌──────────┌──────────────┌──────────────╖
  1831.                      Cycles  (Real             Cycles
  1832.                      Mode)                     (Protected
  1833.                                                Mode)
  1834.  ────────────────────────────────────────────────────────────────────────────
  1835.  
  1836.  Instructions        286            386        286            386
  1837.  
  1838.  ────────────────────────────────────────────────────────────────────────────
  1839.  
  1840.  Far Function Call
  1841.  
  1842.  CALL FAR PTR _func  13             17         26             34
  1843.  
  1844.  Total               13             17         26             34
  1845.  
  1846.  ────────────────────────────────────────────────────────────────────────────
  1847.  
  1848.  Near Function Call
  1849.  
  1850.                      Cycles  (Real             Cycles
  1851.                      Mode)                     (Protected
  1852.                                                Mode)
  1853. 
  1854.  PUSH CS             3              2          3              2
  1855.  
  1856.  CALL NEAR PTR       7              7          7              7
  1857.  _func
  1858.  
  1859.  NOP                 3              3          3              3
  1860.  
  1861.  Total               13             12         13             12
  1862.  
  1863.  ────────────────────────────────────────────────────────────────────────────
  1864.  
  1865.  Savings             0              5          13             22
  1866.  
  1867.  ────────────────────────────────────────────────────────────────────────────
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  1.6.2  Packing Code (/PACKCODE)
  1873.  
  1874.  The /PACKCODE linker option groups neighboring code segments together. When
  1875.  used with the /F option, the /PACKCODE option greatly increases the number
  1876.  of near calls that can be made to a function. This option can be followed
  1877.  with a limit (expressed in bytes) at which to stop packing and to begin a
  1878.  new group. Here is the syntax for the /PACKCODE option: ;/PACKCODE option
  1879.  
  1880.    /PACKCODE:number
  1881.  
  1882.  where number is an optional hexadecimal, octal, or decimal number that
  1883.  specifies the limit for packing. The radix (octal, decimal, or hexadecimal)
  1884.  is specified just as you would specify it to a C program.
  1885.  
  1886.  Radix                             Rules for Specification
  1887.  ────────────────────────────────────────────────────────────────────────────
  1888.  Octal                             Specify the octal number with a leading
  1889.                                    0. You can only use the digits 0 through
  1890.                                    7 in an octal number. For example, 07777.
  1891.  
  1892.  Decimal                           Specify the decimal number without a
  1893.                                    leading 0. For example, 65530.
  1894.  
  1895.  Hexadecimal                       Specify the hexadecimal number with a
  1896.                                    leading 0x. For example, 0x3FFF.
  1897.  
  1898.  If you omit the packing limit, the linker supplies a default value of 65,
  1899.  530.
  1900.  
  1901.  The abbreviation for the /PACKCODE option is /PACKC.
  1902.  
  1903.  
  1904.  1.6.3  Packing Data (/PACKDATA)
  1905.  
  1906.  The /PACKDATA option is analogous to the /PACKCODE option, except that it
  1907.  groups together neighboring data segments instead of code segments. This
  1908.  option is most useful when you have a large-model program that exceeds the
  1909.  OS/2 limitation of 255 segments. By using /PACKDATA, you can group segments,
  1910.  thereby reducing the total number OS/2 has to manage. Here is the syntax for
  1911.  the /PACKDATA option:
  1912.  
  1913.    /PACKDATA:number
  1914.  
  1915.  where number is an optional hexadecimal, octal, or decimal number that
  1916.  specifies the limit for packing. The radix (hexadecimal, octal, or decimal)
  1917.  is specified just as you would specify it to a C program. For more
  1918.  information on specifying hexadecimal, octal, or decimal numbers, see
  1919.  Section 1.6.2 above.
  1920.  
  1921.  If the packing limit is omitted, the linker supplies a default value of
  1922.  65,535 (0xFFFF).
  1923.  
  1924.  The abbreviation for the /PACKDATA option is /PACKD.
  1925.  
  1926.  
  1927.  1.6.4  Packing the Executable File (/EXEPACK)
  1928.  
  1929.  The executable file created by the compiler often contains sequences of
  1930.  repeated bytes. You can remove these repeated sequences with the /EXEPACK
  1931.  option. This decreases the size of the resulting executable file as well as
  1932.  program load time.
  1933.  
  1934.  ────────────────────────────────────────────────────────────────────────────
  1935.  WARNING
  1936.  
  1937.  Because the /EXEPACK option removes debug information from the executable
  1938.  file, you should not use it with the /CODEVIEW option.
  1939.  ────────────────────────────────────────────────────────────────────────────
  1940.  
  1941.  
  1942.  1.7  Optimizing in Different Environments
  1943.  
  1944.  The environment in which you plan to use a program can have a bearing on the
  1945.  types of optimizations that you should use.
  1946.  
  1947.  
  1948.  1.7.1  Optimizing in DOS
  1949.  
  1950.  You need not take special precautions for programs written under DOS unless
  1951.  you are writing a terminate-and-stay-resident (TSR) program. If an
  1952.  interrupt-driven routine could modify a memory location in a program, you
  1953.  should declare that variable volatile.
  1954.  
  1955.  
  1956.  1.7.2  Optimizing in OS/2
  1957.  
  1958.  Many of the rules for interrupt routines apply to OS/2. If one thread can
  1959.  modify variables in another thread, declare these variables as volatile.
  1960.  
  1961.  
  1962.  1.7.3  Optimizing in Microsoft Windows(tm)
  1963.  
  1964.  Microsoft Windows(tm) can move segments dynamically. As a result of dynamic
  1965.  heap compaction, pointers maintained in registers can be invalidated. The
  1966.  /Ow option instructs the compiler that you will not be using aliases, but
  1967.  that Windows might cause certain optimizations to be unsafe across function
  1968.  calls.
  1969.  
  1970.  If you are not using any aliases you must still use the /Ow option with
  1971.  Windows programs. See Section 1.5.3, "Assuming No Aliasing (/Oa and /Ow),"
  1972.  for more information.
  1973.  
  1974.  
  1975.  1.8  Choosing Function-Calling Conventions
  1976.  
  1977.  In Microsoft C, version 6.0, functions can call other functions using three
  1978.  different conventions. Note that, while no calling convention has been
  1979.  defined as "standard," most C compilers use conventions similar to those
  1980.  described here. The C calling convention requires the most object code to
  1981.  set up, but it is the only calling convention that supports functions with
  1982.  variable-length argument lists. The FORTRAN/Pascal calling convention is
  1983.  more compact, but does not allow for variable-length argument lists. The
  1984.  _fastcall, or register calling convention is the fastest of the three
  1985.  calling conventions, but it does not support  variable-length argument lists
  1986.  or mixed-language program interfaces.
  1987.  
  1988.  
  1989.  1.8.1  The C Calling Convention (/Gd)
  1990.  
  1991.  Because C allows functions to have a variable number of parameters,
  1992.  parameters must be pushed onto the stack from right to left. (If parameters
  1993.  were pushed from left to right, it would be difficult for the compiler to
  1994.  determine which parameter was first.) If you do not specify command-line
  1995.  options that modify the function-calling convention, the C calling
  1996.  convention is used; otherwise, the _cdecl keyword must be used before any
  1997.  function using the C calling convention.
  1998.  
  1999.  If, for example, you use the /Gr (register calling convention) option when
  2000.  you compile, and the function  add_two  must have the C calling convention,
  2001.  declare  add_two  as follows:
  2002.  
  2003.    int _cdecl add_two( int x, int y );
  2004.  
  2005.  
  2006.  1.8.2  The FORTRAN/Pascal Calling Convention (/Gc)
  2007.  
  2008.  Use the FORTRAN/Pascal calling convention for any functions declared with
  2009.  either the _fortran or _pascal keywords. (The two keywords currently produce
  2010.  identical results.) Parameters to these functions are always pushed on the
  2011.  stack from left to right. While any function can be declared with the
  2012.  FORTRAN/ Pascal convention, it is used primarily for prototypes to Pascal or
  2013.  FORTRAN routines called from within C programs. This calling convention can
  2014.  also produce smaller, faster programs.
  2015.  
  2016.  The /Gc option (generate Pascal-style function calls) can be used to make
  2017.  all functions in a file observe the FORTRAN/Pascal calling convention.
  2018.  
  2019.  Note that C run-time library routines must still be called using C calling
  2020.  conventions. Because these routines are declared using the _cdecl keyword
  2021.  header files, you must include the appropriate header files in any program
  2022.  using run-time library routines.
  2023.  
  2024.  Functions with variable-length parameter lists (such as printf) cannot use
  2025.  the FORTRAN/Pascal calling convention.
  2026.  
  2027.  ────────────────────────────────────────────────────────────────────────────
  2028.  NOTE
  2029.  
  2030.  The /ML, /MD, and /MT options cause all floating-point functions to be
  2031.  declared as FORTRAN/Pascal. See Chapter 16, "Dynamic Linking with OS/2," for
  2032.  more information.
  2033.  ────────────────────────────────────────────────────────────────────────────
  2034.  
  2035.  
  2036.  1.8.3  The Register Calling Convention (/Gr)
  2037.  
  2038.  You can decrease execution time if parameters to functions are passed in
  2039.  registers rather than on the stack. Compiling with the /Gr command-line
  2040.  option enables the register calling convention for an entire file. The
  2041.  _fastcall keyword enables the register calling convention on a
  2042.  function-by-function basis.
  2043.  
  2044.  Because the 80x86 processor has a limited number of registers, only the
  2045.  first three parameters are allocated to registers; the rest are passed using
  2046.  the FORTRAN/Pascal calling convention. The register calling convention can
  2047.  increase the speed of a program.
  2048.  
  2049.  ────────────────────────────────────────────────────────────────────────────
  2050.  NOTE
  2051.  
  2052.  The compiler allocates different registers for variables declared as
  2053.  register and for passing arguments using the register calling convention.
  2054.  This calling convention will not conflict with any register variables that
  2055.  you may have declared.
  2056.  ────────────────────────────────────────────────────────────────────────────
  2057.  
  2058.  Exercise caution when using the register calling convention for any function
  2059.  written in in-line assembly language. Your use of registers in
  2060.  assembly-language could conflict with the compiler's use of registers for
  2061.  storing parameters.
  2062.  
  2063.  
  2064.  1.8.4  The _fastcall Calling Convention
  2065.  
  2066.  This section describes the details of the _fastcall calling convention. The
  2067.  information is for the use of assembly-language programmers who are
  2068.  interested in using either the in-line assembler or the Microsoft Macro
  2069.  Assembler (MASM) to write functions declared as _fastcall. Functions
  2070.  declared as _fastcall accept arguments in registers rather than on the
  2071.  stack; functions declared as _cdecl or _pascal accept parameters only on the
  2072.  stack.
  2073.  
  2074.  ────────────────────────────────────────────────────────────────────────────
  2075.  WARNING
  2076.  
  2077.  The register usage documented here applies only to Microsoft C, version 6.0.
  2078.  It may change in future releases of the compiler.
  2079.  ────────────────────────────────────────────────────────────────────────────
  2080.  
  2081.  
  2082.  Argument-Passing Convention
  2083.  
  2084.  The _fastcall calling convention is a "strongly typed" register calling
  2085.  convention. This typing allows the compiler to generate better code by
  2086.  passing arguments in registers that correspond to the data type you are
  2087.  passing. Because the compiler chooses registers depending on the type of the
  2088.  argument and not in a strict linear order, the calling program and called
  2089.  function must agree on the types of the arguments in order to communicate
  2090.  data correctly.
  2091.  
  2092.  For each type of argument there is a list of register candidates. The
  2093.  arguments are allocated to registers or, if no suitable register remains
  2094.  unused, are pushed onto the stack left-to-right. Each argument is put in the
  2095.  first register candidate that does not already contain an argument. Table
  2096.  1.3 shows the basic types and the register candidate list for each.
  2097.  
  2098.  Table   1.3 Register Candidates
  2099.  
  2100. ╓┌────────────────────────────────┌──────────────────────────────────────────╖
  2101.  Type                             Register Candidates
  2102.  Type                             Register Candidates
  2103.  ────────────────────────────────────────────────────────────────────────────
  2104.  character                        AL, DL, BL
  2105.  unsigned character               AL, DL, BL
  2106.  integer                          AX, DX, BX
  2107.  unsigned integer                 AX, DX, BX
  2108.  long integer                     DX:AX
  2109.  unsigned long integer            DX:AX
  2110.  near pointer                     BX, AX, DX
  2111.  far or huge pointer              passed on the stack
  2112.  ────────────────────────────────────────────────────────────────────────────
  2113.  
  2114.  
  2115.  All far and huge pointers are pushed on the stack, as are all structures,
  2116.  unions, and floating-point types.
  2117.  
  2118.  
  2119.  Return Value Convention
  2120.  
  2121.  The _fastcall return value convention is based on the size of the return
  2122.  value, except with floating-point types. All floating point types are
  2123.  returned on the top of the NDP stack. For more information about the NDP
  2124.  stack and returning floating-point values, see Chapter 4, "Controlling
  2125.  Floating-Point Math Operations." The following list shows how values 4 bytes
  2126.  or smaller, including unions and structures, are returned from a _fastcall
  2127.  function.
  2128.  
  2129.  Size                              Return Convention
  2130.  ────────────────────────────────────────────────────────────────────────────
  2131.  1 Byte                            AL Register
  2132.  
  2133.  2 Bytes                           AX Register
  2134.  
  2135.  4 Bytes                           DX, AX Registers (for pointers, the
  2136.                                    segment is returned in DX, the offset in
  2137.                                    AX; for long integers,
  2138.                                    the most-significant byte is returned in
  2139.                                    DX, leastsignificant byte in AX)
  2140.  
  2141.  Note that the protocol for returning values 4 bytes or smaller is the same
  2142.  as for functions declared as _cdecl. To return structures and unions larger
  2143.  than 4 bytes, the calling program passes a hidden parameter as the last item
  2144.  pushed. This parameter is a near pointer, implicitly SS-relative, to a
  2145.  buffer in which the value is to be returned. A far pointer to
  2146.  SS:hidden-param must be returned in DX:AX. This is the same convention for
  2147.  returning structures as _pascal.
  2148.  
  2149.  
  2150.  Stack Adjustment Convention
  2151.  
  2152.  Unlike functions declared as _cdecl, functions declared as _fastcall must
  2153.  pop the arguments off the stack. The calling program does not adjust the
  2154.  stack after function return.
  2155.  
  2156.  
  2157.  Register Preservation Requirement
  2158.  
  2159.  All functions must preserve the DS, BP, SI, and DI registers. Your _fastcall
  2160.  function can modify the values in AX, BX, CX, DX, and ES.
  2161.  
  2162.  
  2163.  Function-Naming Convention
  2164.  
  2165.  The public name put into the object file for a function declared as
  2166.  _fastcall is the name given by the user with a leading "at sign" (@). No
  2167.  case translation is performed on the function name. The function declaration
  2168.  
  2169.  
  2170.    int _fastcall FCFunc( void );
  2171.  
  2172.  causes the compiler to place the public symbol  @FCFunc  in your object file
  2173.  at every location  FCFunc  is referenced in your program.
  2174.  
  2175.  If you do not declare the function as _fastcall in your C program, the
  2176.  compiler assumes the default calling convention. The default is usually the
  2177.  C calling convention but can be changed by the /Gc (Pascal Calling
  2178.  Convention), /Gr (Register Calling Convention), or /Gd (C Calling
  2179.  Convention) options. If the linker gives you an unresolved external
  2180.  reference, you may have failed to declare an external _fastcall function
  2181.  properly. For more information about calling conventions, see Chapter 12,
  2182.  "Programming with Mixed Languages."
  2183.  
  2184.  
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  Chapter 2  Managing Memory
  2190.  ────────────────────────────────────────────────────────────────────────────
  2191.  
  2192.  When you develop advanced applications in Microsoft C, you must pay
  2193.  attention to memory management─that is, how data and code are stored and
  2194.  accessed in memory. A well-thought-out memory strategy will make your
  2195.  programs run faster and occupy less memory.
  2196.  
  2197.  You can follow one or more of these memory management strategies:
  2198.  
  2199.  
  2200.    ■   Choose a standard memory model.
  2201.  
  2202.    ■   Create a mixed-model program with the _near, _far, _huge, and _based
  2203.        keywords.
  2204.  
  2205.    ■   Create your own customized memory model.
  2206.  
  2207.    ■   Allocate memory as you need it with the malloc family of functions.
  2208.  
  2209.  
  2210.  This chapter explains pointers, memory models (including the new tiny
  2211.  model), variations such as custom memory models and mixed models, and based
  2212.  pointers.
  2213.  
  2214.  
  2215.  2.1  Pointer Sizes
  2216.  
  2217.  One of the strengths of the C language is that it allows you to use pointers
  2218.  to directly access memory locations.
  2219.  
  2220.  Every Microsoft C program has at least two parts: the code (function
  2221.  definitions) and the data (variables and constants). As a program runs, it
  2222.  refers to elements of the code or the data by their addresses. These
  2223.  addresses can be stored in pointer variables.
  2224.  
  2225.  Pointer variables can fit into 16 bits or 32 bits, depending on the distance
  2226.  of the object to which they refer.
  2227.  
  2228.  
  2229.  2.1.1  Pointers and 64K Segments
  2230.  
  2231.  IBM personal computers and compatibles use the Intel(R) 8086, 80186, 80286,
  2232.  or 80386 processors (collectively called the 80x86 family). These processors
  2233.  have a "segmented" architecture, which means they all have a mode that
  2234.  treats memory as a series of segments, each of which occupies up to 64K of
  2235.  memory. An offset from the base of the segment allows you to access
  2236.  information within a given segment. Moving to a new segment requires
  2237.  additional machine code.
  2238.  
  2239.   A 16-bit pointer can address up to 65,536 locations.
  2240.  
  2241.  The 64K limit is necessary because the 80x86 registers are 16 bits (2 bytes)
  2242.  wide. A single register can address only 65,536 (64K) unique memory
  2243.  locations.
  2244.  
  2245.  A pointer variable that fully specifies a memory address needs 16 bits for
  2246.  the segment location and another 16 bits for the offset within the segment,
  2247.  a total of 32 bits. However, if you have several variables in the same
  2248.  general area, your program can set the segment register once and treat the
  2249.  pointers as smaller 16-bit quantities.
  2250.  
  2251.  The 80x86 register CS holds the base for the code segment; the register DS
  2252.  holds the base for the data segment. Two other segment registers are
  2253.  available: the stack segment register (SS) and the extra segment register
  2254.  (ES). (The 80386 has additional segment registers: FS and GS.)
  2255.  
  2256.  
  2257.  2.1.2  Near Pointers
  2258.  
  2259.  If you don't explicitly specify a memory model, Microsoft C defaults to the
  2260.  small model, which allots up to 64K for the code and another 64K for the
  2261.  data (see Figure 2.1).
  2262.  
  2263.  (This figure may be found in the printed book.)
  2264.  
  2265.  When a small-model program runs, the CS and DS segment registers never
  2266.  change. All code pointers and all data pointers contain 16 bits because they
  2267.  remain within the 64K range.
  2268.  
  2269.  These 16-bit pointers to objects within a single 64K segment are called
  2270.  "near pointers." Accessing a near object is called "near addressing."
  2271.  
  2272.  
  2273.  2.1.3  Far Pointers
  2274.  
  2275.  If your program needs more than 64K for code or data, at least some of the
  2276.  pointers must specify the memory segment, which means these pointers occupy
  2277.  32 bits instead of 16 bits.
  2278.  
  2279.  These larger 32-bit pointers that can point anywhere in memory are called
  2280.  "far pointers." Accessing a far object is called "far addressing."
  2281.  
  2282.   Far pointers can address any location, but they are bigger and slower.
  2283.  
  2284.  Far addressing has the advantage that your program can address any available
  2285.  memory location─up to 640K in DOS or several megabytes in OS/2. The
  2286.  disadvantages of the larger far pointers is that they take up more memory
  2287.  (four bytes instead of two) and that any use of the pointers (assigning,
  2288.  modifying, or otherwise accessing values) takes more time.
  2289.  
  2290.  Allowing either code or data to expand beyond 64K makes your programs larger
  2291.  and slower.
  2292.  
  2293.  
  2294.  2.1.4  Huge Pointers
  2295.  
  2296.  A third type of pointer in Microsoft C is the "huge" pointer, which applies
  2297.  only to data pointers. Code pointers cannot be declared as huge.
  2298.  
  2299.  A huge address is similar to a far address in that both contain 32 bits,
  2300.  made up of a segment value and an offset value. They differ only in the way
  2301.  pointer arithmetic is performed.
  2302.  
  2303.  For far pointers, Microsoft C assumes that code and data objects lie
  2304.  completely within the segment in which they start, so pointer arithmetic
  2305.  operates only on the offset portion of the address. Limiting the size of any
  2306.  single item to 64K makes pointer arithmetic faster.
  2307.  
  2308.  Huge pointers overcome this size limitation; pointer arithmetic is performed
  2309.  on all 32 bits of the data item's address, thus allowing data items
  2310.  referenced by huge pointers to span more than one segment. In this code
  2311.  fragment,
  2312.  
  2313.    int _huge *hp;
  2314.    int _far *fp;
  2315.    .
  2316.    .
  2317.    .
  2318.    hp++;
  2319.    fp++;
  2320.  
  2321.  both  hp  and  fp  are incremented. The huge pointer is incremented as a
  2322.  32-bit value that represents the combined segment and offset. Only the
  2323.  offset part of the far pointer (a 16-bit value) is incremented.
  2324.  
  2325.  Extending the size of pointer arithmetic from 16 to 32 bits causes such
  2326.  arithmetic to execute more slowly. You gain the use of larger arrays by
  2327.  paying a price in execution speed.
  2328.  
  2329.  
  2330.  2.1.5  Based Addressing
  2331.  
  2332.  When you declare near, far, and huge variables, the Microsoft C compiler and
  2333.  linker automatically manage details such as allocating memory and keeping
  2334.  track of segments.
  2335.  
  2336.  A "based pointer" is a fourth kind of pointer that operates as a 16-bit
  2337.  offset from a base that you specify. In this respect, based addressing
  2338.  differs from near, far, or huge addressing; you're responsible for naming
  2339.  the base, instead of letting the compiler decide.
  2340.  
  2341.  Based pointers are new to version 6.0 of Microsoft C. They are explained in
  2342.  more detail in Section 2.5, "Using Based Variables."
  2343.  
  2344.  
  2345.  2.2  Selecting a Standard Memory Model
  2346.  
  2347.  If you want to choose one size for all pointers, there's no need to declare
  2348.  each variable as near or far. Instead, you select a standard memory model
  2349.  and your choice applies to all variables in the program.
  2350.  
  2351.  One advantage of using standard memory models is simplicity. You specify the
  2352.  way the compiler allocates storage for code and data only once.
  2353.  
  2354.   A standard memory model assumes all pointers are the same size.
  2355.  
  2356.  Another advantage is that the standard memory models do not require the use
  2357.  of Microsoft-specific keywords such as _near and _far, so they are best for
  2358.  writing code that is portable to other (non-DOS) systems.
  2359.  
  2360.  The disadvantage of standard memory models is that, because they make global
  2361.  assumptions about the environment, they do not always produce the most
  2362.  efficient code.
  2363.  
  2364.  
  2365.  2.2.1  The Six Standard Memory Models
  2366.  
  2367.  The six Microsoft C memory models are shown in Table 2.1.
  2368.  
  2369.  Table 2.1  Memory Models
  2370.  
  2371. ╓┌─────────┌─────────────────────┌──────────┌────────────────────────────────╖
  2372.            Maximum Total Memory
  2373.  Model     Code                  Data       Data Arrays
  2374.  ────────────────────────────────────────────────────────────────────────────
  2375.            Maximum Total Memory
  2376.  Model     Code                  Data       Data Arrays
  2377.  ────────────────────────────────────────────────────────────────────────────
  2378.  Tiny      <64K                  <64K       <64K
  2379.  Small     64K                   64K        64K
  2380.  Medium    No limit              64K        64K
  2381.  Compact   64K                   No limit   64K
  2382.  Large     No limit              No limit   64K
  2383.  Huge      No limit              No limit   No limit
  2384.  ────────────────────────────────────────────────────────────────────────────
  2385.  
  2386.  
  2387.  The SETUP program creates the libraries that support the six standard memory
  2388.  models.
  2389.  
  2390.  When you choose one of the standard memory models, the compiler inserts the
  2391.  name of the corresponding C run-time library in the object file so the
  2392.  linker chooses it automatically. Each memory model has its own library,
  2393.  except for the huge memory model (which uses the large-model library) and
  2394.  the tiny model (which uses the small-model library).
  2395.  
  2396.  
  2397.  2.2.2  Limitations on Code Size and Data Size
  2398.  
  2399.  When writing a program in Microsoft C, keep in mind two limitations that
  2400.  apply to all six memory models:
  2401.  
  2402.  
  2403.    ■   No single source module can generate 64K or more of code. You must
  2404.        break large programs into modules and link their individual .OBJ files
  2405.        to create the .EXE file.
  2406.  
  2407.    ■   No single data item can exceed 64K unless it appears in a huge-model
  2408.        program or it has been declared with the _huge keyword.
  2409.  
  2410.  
  2411.  
  2412.  2.2.3  The Tiny Memory Model
  2413.  
  2414.  The tiny memory model is new to Microsoft C. It resembles the small model
  2415.  with three exceptions:
  2416.  
  2417.  
  2418.    ■   The tiny model cannot exceed 64K per program (including both code and
  2419.        data). A small-model program, on the other hand, can occupy up to
  2420.        128K: 64K for code and 64K for data.
  2421.  
  2422.    ■   The tiny model produces .COM, rather than .EXE, files. To produce .COM
  2423.        files, compile with the /AT option. Then link with the / TINY option
  2424.        and link in CRTCOM.OBJ.
  2425.  
  2426.    ■   The tiny model applies to DOS only; it is not available in OS/2.
  2427.  
  2428.  
  2429.  Although the tiny model imposes the most severe limits on code and data
  2430.  size, it produces the smallest programs. The tiny memory model only offers a
  2431.  load-time speed advantage over the small model; they both produce the
  2432.  fastest programs.
  2433.  
  2434.  
  2435.  2.2.4  The Huge Memory Model
  2436.  
  2437.  The huge memory model is nearly identical to the large model. The only
  2438.  difference is that the huge model permits individual arrays to exceed 64K in
  2439.  size. For example, an int uses two bytes, so an array of 40,000 integers,
  2440.  occupying 80,000 bytes of memory, would be permitted in the huge model. All
  2441.  other models limit each array, structure, or other data object to no more
  2442.  than 64K.
  2443.  
  2444.  ────────────────────────────────────────────────────────────────────────────
  2445.  NOTE
  2446.  Automatic arrays cannot be declared huge. Only static arrays and arrays
  2447.  occupying memory allocated by the halloc function can be huge.
  2448.  ────────────────────────────────────────────────────────────────────────────
  2449.  
  2450.   The huge model lifts  the limits on arrays.
  2451.  
  2452.  Although the huge model lifts the limits on arrays, some size restrictions
  2453.  do apply. To maintain efficient addressing, no individual array element is
  2454.  allowed to cross a segment boundary. This has the following implications:
  2455.  
  2456.  
  2457.    ■   No single element of an array can be larger than 64K. An array can be
  2458.        larger than 64K, but its individual elements cannot.
  2459.  
  2460.    ■   For any array larger than 128K, all elements must have a size in bytes
  2461.        equal to a power of 2: 2 bytes, 4 bytes, 8 bytes, 16 bytes, and so on.
  2462.        If the array is 128K or smaller, its elements can be any size, up to
  2463.        and including 64K.
  2464.  
  2465.  
  2466.  Pointer arithmetic changes within the huge model, as well. In particular,
  2467.  the sizeof operator may return an incorrect value. The ANSI draft standard
  2468.  for C defines the value returned by sizeof to be of type size_t (which, in
  2469.  Microsoft C, is
  2470.  
  2471.  an unsigned int). The size in bytes of a huge array is an unsigned long
  2472.  value, however. To find the correct value, you must use a type cast:
  2473.  
  2474.    (unsigned long)sizeof(monster_array)
  2475.  
  2476.  Similarly, the C language defines the result of subtracting two pointers as
  2477.  ptrdiff_t (a signed int in Microsoft C). Subtracting two huge pointers will
  2478.  yield a long value. Microsoft C gives the correct result with the following
  2479.  type cast:
  2480.  
  2481.    (long)(ptr1_huge - ptr2_huge)
  2482.  
  2483.  When you select huge model, all extern arrays are treated as _huge.
  2484.  Operations on data declared as _huge can be less efficient than the same
  2485.  operations on data declared as _far.
  2486.  
  2487.  
  2488.  2.2.5  Null Pointers
  2489.  
  2490.  Within the medium and compact models, code pointers and data pointers differ
  2491.  in size: one is 16 bits wide and the other is 32 bits wide. When using these
  2492.  memory models, you should be careful in your use of the manifest constant
  2493.  NULL.
  2494.  
  2495.  NULL represents a null data pointer. The C include files define it as
  2496.  
  2497.    #define NULL ((void *) 0)
  2498.  
  2499.   There can be problems in models with different sizes of code and data
  2500.  pointers.
  2501.  
  2502.  In memory models where data pointers have the same size as code pointers,
  2503.  the actual size of a null pointer doesn't matter. In memory models where
  2504.  code and data pointers are different sizes, problems can occur. Consider
  2505.  this example:
  2506.  
  2507.    void main()
  2508.    {
  2509.       func1( NULL );
  2510.       func2( NULL );
  2511.    }
  2512.  
  2513.    func1( char *dp )
  2514.    {
  2515.       .
  2516.       .
  2517.       .
  2518.    }
  2519.  
  2520.    func2( char (*fp)( void ) )
  2521.    {
  2522.       .
  2523.       .
  2524.       .
  2525.    }
  2526.  
  2527.  In the absence of function prototypes for  func1  and  func2, the compiler
  2528.  always assumes that NULL refers to data and not code.
  2529.  
  2530.  The example above works correctly in tiny, small, large, and huge models
  2531.  because, in those models, a data pointer is the same size as a code pointer.
  2532.  Under medium or compact model, however,  main  passes  NULL to  func2  as a
  2533.  null data pointer rather than as a null code pointer (a pointer to a
  2534.  function), which means the pointer is the wrong size.
  2535.  
  2536.  To ensure that your code works properly in all models, declare each function
  2537.  with a prototype. For example, before  main, include these two lines:
  2538.  
  2539.    int func1( char *dp );
  2540.    int func2( char (*fp)( void ));
  2541.  
  2542.  If you add these prototypes to the example, the code works properly in all
  2543.  memory models. Prototypes force the compiler to coerce code pointers to the
  2544.  correct size. Prototypes also enable strong type-checking of parameters.
  2545.  
  2546.  
  2547.  2.2.6  Specifying a Memory Model
  2548.  
  2549.  If you do not specify a memory model, Microsoft C defaults to the small
  2550.  model, which is adequate for many small to mid-sized programs.
  2551.  
  2552.  You can select a memory model from the Programmer's WorkBench or from the
  2553.  command line.
  2554.  
  2555.  
  2556.  Selecting from within PWB
  2557.  
  2558.  If you're compiling from the Programmer's WorkBench, open the Options menu
  2559.  and choose C Global Build Options. The available memory models appear in the
  2560.  upper left corner. Choose one of the six standard models or choose
  2561.  Customized and type in the options for a customized model.
  2562.  
  2563.  
  2564.  Selecting from the Command Line
  2565.  
  2566.  You can choose a memory model by including an option on the command line.
  2567.  For example, to compile CLICK.C as a compact-model program, type this:
  2568.  
  2569.    CL /AC CLICK.C
  2570.  
  2571.  The /AC option selects the compact memory model. The six options and four
  2572.  libraries are listed below:
  2573.  
  2574.  Option                            Memory Model: Library
  2575.  ────────────────────────────────────────────────────────────────────────────
  2576.  /AT                               Tiny Model: SLIBCxx.LIB (plus CRTCOM.OBJ)
  2577.  
  2578.  /AS                               Small Model: SLIBCxx.LIB
  2579.  
  2580.  /AM                               Medium Model: MLIBCxx.LIB
  2581.  
  2582.  /AC                               Compact Model: CLIBCxx.LIB
  2583.  
  2584.  /AL                               Large Model: LLIBCxx.LIB
  2585.  
  2586.  /AH                               Huge Model: LLIBCxx.LIB
  2587.  
  2588.  
  2589.  2.3  Mixing Memory Models
  2590.  
  2591.  In standard memory models, explained above, all data pointers are the same
  2592.  size and all code pointers are the same size.
  2593.  
  2594.  A mixed memory model selectively combines different types of pointers within
  2595.  the same program. A mixed model extends the limits of a given memory model
  2596.  while retaining its benefits.
  2597.  
  2598.   A mixed memory model lets you mix near and far pointers.
  2599.  
  2600.  For example, imagine a programming situation where you add an array to a
  2601.  small-model program, pushing the data segment past the 64K limit.
  2602.  
  2603.  You could solve the problem by moving up from the small to the compact
  2604.  memory model. Doing so would bump all data pointers from two to four bytes.
  2605.  The .EXE file would grow accordingly. Execution time would slow.
  2606.  
  2607.  A second and perhaps better solution is to stay within the standard small
  2608.  memory model, which uses near pointers, but to declare the new array as far.
  2609.  You mix near pointers and far pointers, creating a mixed model.
  2610.  
  2611.  Microsoft C lets you override the standard addressing convention for a given
  2612.  memory model by specifying that certain items are _near, _far, _huge, or
  2613.  _based. These keywords are not a standard part of the C language; they are
  2614.  Microsoft extensions, meaningful only on systems that use 80x86
  2615.  microprocessors. Using these keywords may affect the portability of your
  2616.  code.
  2617.  
  2618.  ────────────────────────────────────────────────────────────────────────────
  2619.  NOTE
  2620.  
  2621.  Previous versions of the Microsoft C Compiler accepted the keywords near,
  2622.  far, and huge without an initial underscore. Since the ANSI draft standard
  2623.  for C permits compiler implementors to reserve identifiers that begin with
  2624.  underscores, an underscore was added to these keywords to mark them as
  2625.  Microsoft-specific. To maintain compatibility with existing source code, the
  2626.  compiler still recognizes the obsolescent versions of these keywords.
  2627.  ────────────────────────────────────────────────────────────────────────────
  2628.  
  2629.  You can compile a program in the small model, for example, but declare a
  2630.  certain array to be _far. At run time, the address of that array occupies
  2631.  four bytes. The program may slow slightly when accessing items in that
  2632.  particular far array, but throughout the rest of the program, all addressing
  2633.  would be near. Note that all pointers to elements of an array declared as
  2634.  _far must also be declared as _far.
  2635.  
  2636.  Table 2.2 lists the effects of these keywords on data pointers, code
  2637.  pointers, and pointer arithmetic.
  2638.  
  2639.  Table 2.2  Addressing Declared with Microsoft Keywords
  2640.  
  2641. ╓┌────────┌─────────────────────┌─────────────────────┌──────────────────────╖
  2642.  Keyword  Data                  Code                  Arithmetic
  2643.  ────────────────────────────────────────────────────────────────────────────
  2644.  _near    Data reside in        Functions reside in   16 bits
  2645.           default data          current code
  2646.           segment; 16-bit       segment; 16-bit
  2647.           addresses             addresses
  2648.  Keyword  Data                  Code                  Arithmetic
  2649.  ────────────────────────────────────────────────────────────────────────────
  2650.          addresses             addresses
  2651.  
  2652.  _far     Data can be anywhere  Functions can be      16 bits
  2653.           in memory, not        called from anywhere
  2654.           necessarily in the    in memory; 32-bit
  2655.           default data          addresses
  2656.           segment; 32-bit
  2657.           addresses
  2658.  
  2659.  _huge    Data can be anywhere  Not applicable;       32 bits
  2660.           in memory, not        code cannot be         (data only)
  2661.           necessarily in the    declared _huge
  2662.           default data segment.
  2663.           Individual data
  2664.           items (arrays) can
  2665.           exceed 64K in size;
  2666.           32-bit addresses
  2667.  
  2668.  _based   Data can be anywhere  Not applicable;       16 bits
  2669.  Keyword  Data                  Code                  Arithmetic
  2670.  ────────────────────────────────────────────────────────────────────────────
  2671. _based   Data can be anywhere  Not applicable;       16 bits
  2672.           in memory, not        code cannot be         (data only)
  2673.           necessarily in the    declared _based
  2674.           default data
  2675.           segment; 16-bit
  2676.           addresses plus a
  2677.           known base provide
  2678.           the range of 32-bit
  2679.           addresses
  2680.  
  2681.  ────────────────────────────────────────────────────────────────────────────
  2682.  
  2683.  
  2684.  
  2685.  
  2686.  2.3.1  Pointer Problems
  2687.  
  2688.  When you declare items to be _near, _far, _huge, or _based, you can link
  2689.  with a standard run-time library. Be aware, however, that in some cases, the
  2690.  modified pointers will be incompatible with standard library functions.
  2691.  Watch for these problems that affect pointers:
  2692.  
  2693.  
  2694.    ■   A library function that expects a 16-bit pointer as an argument will
  2695.        not function properly with modified variables that occupy 32 bits. In
  2696.        other words, you can cast a near pointer to a far pointer, because it
  2697.        adds the segment value and maintains the integrity of the address. If
  2698.        you cast a far pointer to near, however, the compiler generates a
  2699.        warning message because the offset may not lie within the default data
  2700.        segment, in which case the original far address is  irretrievably
  2701.        lost.
  2702.  
  2703.    ■   A library function that returns a pointer will return a pointer of the
  2704.        default size for the memory model. This is only a problem if you are
  2705.        assigning the return value to a pointer of a smaller size. For
  2706.        example, there may be difficulties if you compile with a model that
  2707.        selects far data pointers, but you have explicitly declared the
  2708.        variable to receive the return value _near.
  2709.  
  2710.        This warning does not apply to all functions. See Section B.2.8 in
  2711.        Appendix B for a list of model-independent string and memory functions
  2712.        such as _fstrcat, the far version of strcat.
  2713.  
  2714.    ■   Based pointers pose a special problem. Based pointers are passed to
  2715.        other functions as is (without normalization). Certain functions
  2716.        expect to receive based pointers, but most do not. Therefore, in most
  2717.        cases, you must either explicitly cast a based pointer to a far
  2718.        pointer or make sure that all functions that receive based pointers
  2719.        are prototyped.
  2720.  
  2721.  
  2722.  Some run-time library functions support near, far, huge, and based
  2723.  variables. For example, halloc allocates memory for a huge data array.
  2724.  
  2725.  You can always pass the value (but not the address) of a far item to a
  2726.  small-model library routine. For example,
  2727.  
  2728.    /* Compile in small model */
  2729.    #include <stdio.h>
  2730.    long _far time_val;
  2731.  
  2732.    void main()
  2733.    {
  2734.       time( &time_val );             /* Illegal far address */
  2735.       printf( "%ld\n", time_val );   /* Legal value */
  2736.    }
  2737.  
  2738.  When you use a mixed memory model, you should include function prototypes
  2739.  with argument-type lists to ensure that all pointer arguments are passed to
  2740.  functions correctly.
  2741.  
  2742.  
  2743.  2.3.2  Declaring Near, Far, Huge, and Based Variables
  2744.  
  2745.  The _near, _far, _huge, and _based keywords modify either objects or
  2746.  pointers to objects. When using them to declare variables, keep these rules
  2747.  in mind:
  2748.  
  2749.  
  2750.    ■   The keyword always modifies the object or pointer immediately to its
  2751.        right. In complex declarations, think of the _far keyword and the item
  2752.        to its right as being a single unit. For example, in the case of the
  2753.        declaration
  2754.  
  2755.        char _far * _near *p;
  2756.  
  2757.        p  is a near pointer to a far pointer to char, which resides in the
  2758.        default data segment for the memory model being used.
  2759.  
  2760.        By contrast, the declaration
  2761.  
  2762.        char _far * _near p;
  2763.  
  2764.        is a far pointer to char that will always be stored in DGROUP,
  2765.        regardless of the memory model being used.
  2766.  
  2767.    ■   If the item immediately to the right of the keyword is an identifier,
  2768.        the keyword determines whether the item will be allocated in the
  2769.        default data segment ( _near) or a separate data segment ( _far,
  2770.        _huge, or _based). For example,
  2771.  
  2772.        char _far a;
  2773.  
  2774.        allocates  a  as an item of type char with a _far address.
  2775.  
  2776.    ■   If the item immediately to the right of the keyword is a pointer, the
  2777.        keyword determines whether the pointer will hold a near address (16
  2778.        bits), a based address (16 bits), a far address (32 bits), or a huge
  2779.        address (also 32 bits). For example,
  2780.  
  2781.        char _huge *p;
  2782.  
  2783.        allocates  p  as a huge pointer (32 bits) to an item of type char. Any
  2784.        arithmetic performed on the huge pointer  p  will affect all 32 bits.
  2785.        That is, the instruction  p++  increments the pointer as a 32-bit
  2786.        entity.
  2787.  
  2788.  
  2789.  
  2790.  2.3.3  Declaring Near and Far Functions
  2791.  
  2792.  You cannot declare functions as _huge or _based. The rules for using the
  2793.  _near and _far keywords for functions are similar to those for using them
  2794.  with data:
  2795.  
  2796.  
  2797.    ■   The keyword always modifies the function or pointer immediately to its
  2798.        right.
  2799.  
  2800.    ■   If the item immediately to the right of the keyword is a function, the
  2801.        keyword determines whether the function will be allocated as near or
  2802.        far. For example,
  2803.  
  2804.        char _far fun();
  2805.  
  2806.        defines  fun  as a function with a 32-bit address that returns a char.
  2807.        The function may be located in near memory or far memory, but it is
  2808.        called with the full 32-bit address. The _far keyword applies to the
  2809.        function, not to the return type.
  2810.  
  2811.    ■   If the item immediately to the right of the keyword is a pointer to a
  2812.        function, the keyword determines whether the function will be called
  2813.        using a near (16-bit) or far (32-bit) address. For example,
  2814.  
  2815.        char (_far *pfun)( );
  2816.  
  2817.        defines  pfun  as a far pointer (32 bits) to a function returning type
  2818.        char.
  2819.  
  2820.    ■   Function declarations must match function definitions.
  2821.  
  2822.    ■   The _huge and _based keywords do not apply to functions. That is, a
  2823.        function cannot be huge (larger than 64K) or based. A function can
  2824.        return a huge data pointer to the calling function. A function can
  2825.        return a based pointer unless it is a pointer based on _self (see
  2826.        Section 2.5.2, "Declaring Based Variables").
  2827.  
  2828.  
  2829.  The example below declares  fun1  as a far function returning type char:
  2830.  
  2831.    char _far fun1(void);              /* small model */
  2832.    char _far fun(void)
  2833.    {
  2834.        .
  2835.        .
  2836.        .
  2837.    }
  2838.  
  2839.  Here, the  fun2  function is a near function that returns a far pointer to
  2840.  type char:
  2841.  
  2842.    char _far * _near fun2( );        /* large model */
  2843.    char _far * _near fun( )
  2844.    {
  2845.        .
  2846.        .
  2847.        .
  2848.    }
  2849.  
  2850.  The example below declares  pfun  as a far pointer to a function that has an
  2851.  int return type, assigns the address of printf to  pfun, and prints "Hello
  2852.  world." twice.
  2853.  
  2854.    /* Compile in medium, large, or huge model */
  2855.  
  2856.    #include <stdio.h>
  2857.    int (_far *pfun)( char *, ... );
  2858.  
  2859.    void main()
  2860.    {
  2861.         pfun = printf;
  2862.         pfun( "Hello world.\n" );
  2863.         (*pfun)( "Hello world.\n" );
  2864.    }
  2865.  
  2866.  
  2867.  2.3.4  Pointer Conversions
  2868.  
  2869.  Passing near or far pointers as arguments to functions can cause automatic
  2870.  conversions in the size of the pointer argument. Passing a pointer to an
  2871.  unprototyped function forces the pointer size to the larger of the following
  2872.  two sizes:
  2873.  
  2874.  
  2875.    ■   The default pointer size for that type, as defined by the memory model
  2876.        selected during compilation.
  2877.  
  2878.        For example, in medium-model programs, data pointer arguments are near
  2879.        by default, and code pointer arguments are far by default.
  2880.  
  2881.    ■   The size of the type of the argument.
  2882.  
  2883.  
  2884.  Note that if you supply a based pointer as an argument to a function and do
  2885.  not specifically cast it to a far pointer type, a 16-bit offset from the
  2886.  base segment is passed.
  2887.  
  2888.   Function prototypes prevent problems that may occur in mixed memory models.
  2889.  
  2890.  
  2891.  If you provide a function prototype with complete argument types, the
  2892.  compiler performs type-checking and enforces the conversion of actual
  2893.  arguments to the declared type of the corresponding formal argument.
  2894.  However, if no declaration is present or the argument-type list is empty,
  2895.  the compiler will convert nonbased pointer arguments automatically to the
  2896.  default type or the type of the argument, whichever is larger. To avoid
  2897.  mismatched arguments, always use a prototype with the argument types.
  2898.  
  2899.  For example, the following program produces unexpected results in
  2900.  compact-model, large-model, or huge-model programs.
  2901.  
  2902.    void main( )
  2903.    {
  2904.        int _near *x;
  2905.       char _far *y;
  2906.       int z = 1;
  2907.  
  2908.       test_fun( x, y, z );   /* x is coerced to far
  2909.                                     pointer in compact,
  2910.                                     large, or huge model */
  2911.    }
  2912.  
  2913.    int test_fun( int _near *ptr1, char _far *ptr2, int a)
  2914.    {
  2915.        printf("Value of a = %d\n", a);
  2916.    }
  2917.  
  2918.  If the preceding example is compiled as a tiny, small, or medium program,
  2919.  the size of  x  is 16 bits, the size of  y  is 32 bits, and the value
  2920.  printed for  a  is 1.
  2921.  
  2922.  However, if the example is compiled in compact, large, or huge model, both
  2923.  x  and  y  are automatically converted to far pointers when they are passed
  2924.  to  test_fun. Since  ptr1, the first parameter of  test_fun, is defined as a
  2925.  near pointer argument, it takes only 16 bits of the 32 bits passed to it.
  2926.  The next parameter,  ptr2, takes the remaining 16 bits passed to  ptr1, plus
  2927.  16 bits of the 32 bits passed to it. Finally, the third parameter,  a, takes
  2928.  the leftover 16 bits from  ptr2, instead of the value of  z  in the main
  2929.  function.
  2930.  
  2931.  This shifting process does not generate an error message, because both the
  2932.  function call and the function definition are legal. In this case the
  2933.  program does not work as intended, however, since the value assigned to  a
  2934.  is not the value intended.
  2935.  
  2936.  To pass  ptr1  as a near pointer, you should include a function prototype
  2937.  that specifically declares this argument for  test_fun  as a near pointer,
  2938.  as shown below:
  2939.  
  2940.    /* First, prototype test_fun so the compiler
  2941.    * knows in advance about the near pointer argument:
  2942.    */
  2943.    int test_fun (int _near*, char _far *, int);
  2944.  
  2945.    main ( )
  2946.    {
  2947.       int _near *x;
  2948.       char _far *y;
  2949.       int z = 1;
  2950.  
  2951.       test_fun ( x, y, z );    /* now, x is not coerced
  2952.                                * to a far pointer; it is
  2953.                                * passed as a near pointer,
  2954.                                * no matter which memory
  2955.                                * model is used
  2956.                                */
  2957.    }
  2958.  
  2959.    int test_fun ( int _near *ptr1, char _far *ptr2, int a)
  2960.    {
  2961.       printf ( "Value of a = %d\n", a );
  2962.    }
  2963.  
  2964.  
  2965.  2.4  Customizing Memory Models
  2966.  
  2967.  A third way to manage memory is to combine different features from standard
  2968.  memory models to create your own customized memory model. You should have a
  2969.  thorough understanding of C memory models and the architecture of 80x86
  2970.  processors before creating your own nonstandard memory models.
  2971.  
  2972.   In a customized model, you select the size of code pointers and data
  2973.  pointers.
  2974.  
  2975.  The /Astring option lets you change the attributes of the standard memory
  2976.  models to create your own memory models. The three letters in string
  2977.  correspond to the code pointer size, the data pointer size, and the stack
  2978.  and data segment setup, respectively. Because the letter allowed in each
  2979.  field is unique to that field, you can give the letters in any order after
  2980.  /A. All three letters must be present.
  2981.  
  2982.  The standard memory-model options (/AT, /AS, /AM, /AC, /AL, and /AH) can be
  2983.  specified in the /Astring form. As an example of how to construct memory
  2984.  models, the standard memory-model options are listed below with their
  2985.  /Astring equivalents:
  2986.  
  2987.  Standard                          Custom Equivalent
  2988.  ────────────────────────────────────────────────────────────────────────────
  2989.  /AT                               /Asnd
  2990.  
  2991.  /AS                               /Asnd
  2992.  
  2993.  /AM                               /Alnd
  2994.  
  2995.  /AC                               /Asfd
  2996.  
  2997.  /AL                               /Alfd
  2998.  
  2999.  /AH                               /Alhd
  3000.  
  3001.  For example, you might want to create a huge-compact model. This model would
  3002.  allow huge data items but only one code segment. The option for specifying
  3003.  this model would be /Ashd.
  3004.  
  3005.  ────────────────────────────────────────────────────────────────────────────
  3006.  NOTE
  3007.  
  3008.  Tiny model is identical to small model except that it causes the linker to
  3009.  search for CRTCOM.LIB. The executable file generated when you specify tiny
  3010.  model is a .COM file rather than a .EXE.
  3011.  ────────────────────────────────────────────────────────────────────────────
  3012.  
  3013.  
  3014.  2.4.1  Setting a Size for Code Pointers
  3015.  
  3016.  Within a custom memory model, you choose whether code pointers are short or
  3017.  long:
  3018.  
  3019.  Option                            Size
  3020.  ────────────────────────────────────────────────────────────────────────────
  3021.  /Asxx                             Short (near) code pointers
  3022.  
  3023.  /Alxx                             Long (far) code pointers
  3024.  
  3025.  The /As (short) option tells the compiler to generate near 16-bit pointers
  3026.  and addresses for all functions. This is the default for tiny-, small-, and
  3027.  compact-model programs.
  3028.  
  3029.  The /Al (long) option means that far 32-bit pointers and addresses are used
  3030.  to address all functions. Far pointers are the default for medium-, large-,
  3031.  and huge-model programs.
  3032.  
  3033.  
  3034.  2.4.2  Setting a Size for Data Pointers
  3035.  
  3036.  Data pointers can be near, far, or huge:
  3037.  
  3038.  Option                            Size
  3039.  ────────────────────────────────────────────────────────────────────────────
  3040.  /Axnx                             Near data pointers
  3041.  
  3042.  /Axfx                             Far data pointers
  3043.  
  3044.  /Axhx                             Huge data pointers
  3045.  
  3046.  The /An (near) option tells the compiler to use 16-bit pointers and
  3047.  addresses for all data. This is the default for tiny-, small-, and
  3048.  medium-model programs.
  3049.  
  3050.  The /Af (far) option specifies that all data pointers and addresses are 32
  3051.  bits. This is the default for compact- and large-model programs.
  3052.  
  3053.  The /Ah (huge) option specifies that all data pointers and addresses are far
  3054.  (32-bit) and that arrays are permitted to extend beyond a 64K segment. This
  3055.  is the default for huge-model programs.
  3056.  
  3057.  With far data pointers, no single data item can be larger than a segment
  3058.  (64K) because address arithmetic is performed only on 16 bits (the offset
  3059.  portion) of the address. When huge data pointers are used, individual data
  3060.  items can be larger than a segment (64K) because address arithmetic is
  3061.  performed on both the segment and the offset.
  3062.  
  3063.  
  3064.  2.4.3  Setting Up Segments
  3065.  
  3066.  Within a customized model, you can choose to make the stack segment (SS)
  3067.  equal the data segment (DS), in which case they overlap:
  3068.  
  3069.  Option                            Effect
  3070.  ────────────────────────────────────────────────────────────────────────────
  3071.  /Axxd                             SS == DS
  3072.  
  3073.  /A«xx»u                           SS != DS; DS reloaded on function entry
  3074.  
  3075.  /A«xx»w                           SS != DS; DS not reloaded on function
  3076.                                    entry
  3077.  
  3078.  
  3079.  Segment Setup Option /Ad
  3080.  
  3081.  The option /Ad tells the compiler that the segment addresses stored in the
  3082.  SS and DS registers are equal. The stack segment and the default data
  3083.  segment are combined into a single segment. This is the default for all
  3084.  standard-model programs. In small- and medium-model programs, the stack plus
  3085.  all data must occupy less than 64K; thus, any data item is accessed with
  3086.  only a 16-bit offset from the segment address in the SS and DS registers.
  3087.  
  3088.  In compact-, large-, and huge-model programs, initialized global and static
  3089.  data are placed in the default data segment up to a certain threshold. The
  3090.  address of this segment is stored in the DS and SS registers. All pointers
  3091.  to data, including pointers to local data (the stack), are full 32-bit
  3092.  addresses. This is important to remember when passing pointers as arguments
  3093.  in multiple-segment programs. Although you may have more than 64K of total
  3094.  data in these models, no more than 64K of data can occupy the default
  3095.  segment. The /Gt and /ND options control allocation of items in the default
  3096.  data segment if a program exceeds this limit.
  3097.  
  3098.  
  3099.  Segment Setup Option /Au
  3100.  
  3101.  The option /Au tells the compiler that the stack segment does not
  3102.  necessarily coincide with the data segment. In addition, it adds the _loadds
  3103.  attribute to all functions within a module, forcing the compiler to generate
  3104.  code to load the DS register with the correct value prior to entering the
  3105.  function body. Combine the /ND option with /Au to name data segments other
  3106.  than the default. When /Au is combined with /ND, the address in the DS
  3107.  register is saved upon entry to each function, and the new DS value for the
  3108.  module in which the function was defined is loaded into the register. The
  3109.  previous DS value is restored on exit from the function. Therefore, only one
  3110.  data segment is accessible at any given time. The /ND option lets you
  3111.  combine these segments into a single segment.
  3112.  
  3113.  If a standard memory-model option precedes it on the command line, the /Au
  3114.  option can be specified without any letters indicating data pointer or code
  3115.  pointer sizes. The program uses a standard memory model, but different
  3116.  segments are set up for the stack and data segments.
  3117.  
  3118.  The /Au option is useful for OS/2 or Microsoft Windows dynamic-link
  3119.  libraries (DLLs), since it forces DS to be loaded on entry to each function.
  3120.  It is also useful for writing extensions to the Programmer's WorkBench. This
  3121.  is a costly operation, however, so consider using the /Aw option.
  3122.  
  3123.  
  3124.  Segment Setup Option /Aw
  3125.  
  3126.  The option /Aw, like /Au, causes the compiler to assume that the stack
  3127.  segment is separate from the data segment. The compiler does not
  3128.  automatically load the DS register at each function entry point. The /Aw
  3129.  option is useful in creating applications that interface with an operating
  3130.  system or with a program running at the operating-system level. The
  3131.  operating system or the program running under the operating system actually
  3132.  receives the data intended for the application program and places that data
  3133.  in a segment; then the operating system or program must load the DS register
  3134.  with the segment address for the application program.
  3135.  
  3136.  As with the /Au option, the /Aw option can be specified without data pointer
  3137.  and code pointer letters if a standard memory-model option precedes it on
  3138.  the command line. In such a case, the program uses the specified memory
  3139.  model just as with /Au, but the DS register is not reloaded at each function
  3140.  entry point.
  3141.  
  3142.  Even though /Au and /Aw indicate that the stack may be in a separate
  3143.  segment, the stack's size is still fixed at the default size unless this is
  3144.  overridden with the /F compiler option or the /STACK linker option.
  3145.  
  3146.  The /Aw option is useful for writing OS/2 and Microsoft Windows dynamic-link
  3147.  libraries (DLLs), but care must be taken when it is used. Declare all entry
  3148.  points to the dynamic-link library as _loadds to force DS to be loaded on
  3149.  entry to the function (exactly like the /Au option). The other functions
  3150.  will then be more efficient, though, because they will not have to perform
  3151.  redundant loads of the DS register. For example,
  3152.  
  3153.    _export _loadds _far pascal LibFunc( void )
  3154.    {
  3155.        .
  3156.        .
  3157.        .
  3158.        HelperFunc(); }
  3159.  
  3160.    HelperFunc( void )
  3161.    {
  3162.        .
  3163.        .
  3164.        .
  3165.    }
  3166.  
  3167.  The library entry point,  LibFunc, is declared as _loadds to force the DS
  3168.  register to be loaded on entry. The function  HelperFunc, which is private
  3169.  to the dynamic-link library, is declared as a normal C function. Since it
  3170.  cannot be called from outside of the module,  HelperFunc  does not need to
  3171.  reload DS.
  3172.  
  3173.  If you choose one of the options that specifies that the stack segment is
  3174.  not equal to the data segment (SS != DS), you cannot pass the address of
  3175.  frame variables as arguments to functions that take near pointers. That is,
  3176.  in tiny, small, and medium models, you cannot pass the address of a local
  3177.  variable (which is allocated on the stack) as an argument, because the
  3178.  receiving function will assume the pointer is relative to the data segment.
  3179.  However, the receiving function could solve this problem by declaring the
  3180.  pointer to be the following:
  3181.  
  3182.    based(_segname("_STACK"))
  3183.  
  3184.  Another solution would be to cast the pointer to a far pointer in both
  3185.  locations as follows:
  3186.  
  3187.    /* Call func with an explicit cast to far */
  3188.    func( (char far *)frame_var );
  3189.    .
  3190.    .
  3191.    .
  3192.    void func( char far *formal_var )
  3193.  
  3194.  
  3195.  2.4.4  Library Support for Customized Memory Models
  3196.  
  3197.  Most C programs make function calls to the routines in the C run-time
  3198.  library. When you write mixed-model programs, you are responsible for
  3199.  determining which library (if any) is suitable for your program and for
  3200.  ensuring that the appropriate library is linked. Table 2.3 shows the
  3201.  libraries from which to extract the start-up routine for each customized
  3202.  memory model.
  3203.  
  3204.  Table 2.3  Start-Up Routines for Customized Memory Models
  3205.  
  3206. ╓┌────────────────────────────────────────────────┌──────────────────────────╖
  3207.  Memory-Model Option                              From Library
  3208.  ────────────────────────────────────────────────────────────────────────────
  3209.  /Asnx; /AS plus /Ax                              SLIBCf.LIB
  3210.  /Asfx; /Ashx; /AC plus /Ax                       CLIBCf.LIB
  3211.  /Alnx; /AM plus /Ax                              MLIBCf.LIB
  3212.  /Alfx; /Alhx; /AL plus /Ax; /AH plus /Ax         LLIBCf.LIB
  3213.  ────────────────────────────────────────────────────────────────────────────
  3214.  
  3215.  
  3216.  The /Ax option represents either /Au or /Aw. In the library names,  f is
  3217.  either E (emulator library), 7 (8087/80287 library), or A (alternate math
  3218.  library).
  3219.  
  3220.  
  3221.  2.4.5  Setting the Data Threshold
  3222.  
  3223.  Option                            Effect
  3224.  ────────────────────────────────────────────────────────────────────────────
  3225.  /Gt«number»                       Sets the threshold
  3226.  
  3227.  The /Gt option causes all data items whose size is greater than to number
  3228.  bytes to be allocated to a new data segment. When number is specified, it
  3229.  must follow the /Gt option immediately, with no intervening spaces. When
  3230.  number is omitted, the default threshold value is 256. When the /Gt option
  3231.  is omitted, the default threshold value is 32,767.
  3232.  
  3233.  The /Gt option applies only to compact-, large-, and huge-model programs,
  3234.  since small- and medium-model programs have only one data segment. The
  3235.  option is particularly useful with programs that have more than 64K of
  3236.  initialized static and global data in small data items, because otherwise
  3237.  you run out of memory in the default data segment and can't link the
  3238.  program. The /Gt option has no effect on uninitialized global data.
  3239.  
  3240.  
  3241.  2.4.6  Naming Modules and Segments
  3242.  
  3243.  Option                            Effect
  3244.  ────────────────────────────────────────────────────────────────────────────
  3245.  /NM modulename                    Names the module
  3246.  
  3247.  /NT textsegment                   Names the code segment
  3248.  
  3249.  /ND datasegment                   Names the data segment
  3250.  
  3251.  "Module" is another name for an object file created by the C compiler from a
  3252.  single source file. Every module has a name. The compiler uses this name in
  3253.  error messages if problems are encountered during processing. The module
  3254.  name is usually the same as the source-file name. You can change this name
  3255.  using the /NM (name module) option. The new modulename can include any
  3256.  combination of letters and digits. The space between /NM and modulename is
  3257.  optional.
  3258.  
  3259.  Every module has at least two segments: a code segment (sometimes called the
  3260.  text segment) containing the program instructions, and a data segment
  3261.  containing the program data.
  3262.  
  3263.  The compiler normally creates the code and data segment names. The default
  3264.  names depend on the memory model chosen for the program. For example, in
  3265.  small-model programs the code segment is named _TEXT and the data segment is
  3266.  named _DATA.
  3267.  
  3268.  Table 2.4 summarizes the naming conventions for code and data segments.
  3269.  
  3270.  Table 2.4  Segment-Naming Conventions
  3271.  
  3272. ╓┌─────────┌─────────────┌───────┌───────────────────────────────────────────╖
  3273.  Model     Code          Data    Module
  3274.  ────────────────────────────────────────────────────────────────────────────
  3275.  Tiny      _TEXT         _DATA   ---
  3276.  Small     _TEXT         _DATA   ---
  3277.  Medium    module_TEXT   _DATA   filename
  3278.  Model     Code          Data    Module
  3279.  ────────────────────────────────────────────────────────────────────────────
  3280. Medium    module_TEXT   _DATA   filename
  3281.  Compact   _TEXT         _DATA   filename
  3282.  Large     module_TEXT   _DATA   filename
  3283.  Huge      module_TEXT   _DATA   filename
  3284.  ────────────────────────────────────────────────────────────────────────────
  3285.  
  3286.  
  3287.  In memory models that contain multiple data segments (compact, large, and
  3288.  huge), _DATA is the name of the default data segment. Other data segments
  3289.  have unique private names. You can override the default names with the
  3290.  options /NT (name text) and /ND (name data).
  3291.  
  3292.  The /ND option is commonly used to create and compile modules that contain
  3293.  data only. Such modules can be accessed from other parts of the program by
  3294.  declaring their variables as external.
  3295.  
  3296.  If you change the name of the default data segment with /ND, your program
  3297.  must load the DS register with the segment selector of your named data
  3298.  segment before it accesses it. You must therefore compile your program
  3299.  either with the /Astringform of the memory-model option and the /Au option
  3300.  for the segment setup, or with the /A option for a s
  3301.  
  3302.  
  3303.  
  3304.  
  3305.  Chapter 3  Using the In-Line Assembler
  3306.  ────────────────────────────────────────────────────────────────────────────
  3307.  
  3308.  This chapter explains how to use the Microsoft C in-line assembler. Assembly
  3309.  language serves many purposes, such as improving program speed, reducing
  3310.  memory needs, and controlling hardware. The in-line assembler lets you embed
  3311.  assembly-language instructions directly in your C source programs without
  3312.  extra assembly and link steps. The in-line assembler is built into the
  3313.  compiler─you don't need a separate assembler such as the Microsoft Macro
  3314.  Assembler (MASM).
  3315.  
  3316.  
  3317.  3.1  Advantages of In-Line Assembly
  3318.  
  3319.  Because the in-line assembler doesn't require separate assembly and link
  3320.  steps, it is more convenient than a separate assembler. In-line assembly
  3321.  code can use any C variable or function name that is in scope, so it is easy
  3322.  to integrate it with your program's C code. And because the assembly code
  3323.  can be mixed in-line with C statements, it can do tasks that are cumbersome
  3324.  or impossible in C alone.
  3325.  
  3326.  The uses of in-line assembly include
  3327.  
  3328.  
  3329.    ■   Writing functions in assembly language
  3330.  
  3331.    ■   Spot-optimizing speed-critical sections of code
  3332.  
  3333.    ■   Calling DOS and BIOS routines with the INT instruction
  3334.  
  3335.    ■   Creating TSR (terminate-and-stay-resident) code or handler routines
  3336.        that require knowledge of processor states
  3337.  
  3338.  
  3339.  In-line assembly is a special-purpose tool. If you plan to transport an
  3340.  application, you'll probably want to place machine-specific code in a
  3341.  separate module. And because the in-line assembler doesn't support all of
  3342.  MASM's macro and data directives, you may find it more convenient to use
  3343.  MASM for such modules.
  3344.  
  3345.  
  3346.  3.2  The _asm Keyword
  3347.  
  3348.  The _asm keyword invokes the in-line assembler and can appear wherever a C
  3349.  statement is legal. It cannot appear by itself. It must be followed by an
  3350.  assembly instruction, a group of instructions enclosed in braces, or, at the
  3351.  very least, an empty pair of braces. The term "_asm block" here refers to
  3352.  any instruction or group of instructions, whether or not in braces.
  3353.  
  3354.  Below is a simple _asm block enclosed in braces. (The code prints the "beep"
  3355.  character, ASCII 7.)
  3356.  
  3357.    _asm
  3358.    {
  3359.       mov ah, 2
  3360.       mov dl, 7
  3361.       int 21h
  3362.    }
  3363.  
  3364.  Alternatively, you can put _asm in front of each assembly instruction:
  3365.  
  3366.    _asm mov ah, 2
  3367.    _asm mov dl, 7
  3368.    _asm int 21h
  3369.  
  3370.  Since the _asm keyword is a statement separator, you can also put assembly
  3371.  instructions on the same line:
  3372.  
  3373.    _asm mov ah, 2   _asm mov dl, 7   _asm int 21h
  3374.  
  3375.   Braces can prevent ambiguity and needless repetition.
  3376.  
  3377.  All three examples generate the same code, but the first style─enclosing the
  3378.  _asm block in braces─has some advantages. The braces clearly separate
  3379.  assembly code from C code and avoid needless repetition of the _asm keyword.
  3380.  Braces can also prevent ambiguities. If you want to put a C statement on the
  3381.  same line as an _asm block, you must enclose the block in braces. Without
  3382.  the braces, the compiler cannot tell where assembly code stops and C
  3383.  statements begin. Finally, since the text in braces has the same format as
  3384.  ordinary MASM text, you can easily cut and paste text from existing MASM
  3385.  source files.
  3386.  
  3387.  The braces enclosing an _asm block don't affect variable scope, as do braces
  3388.  in C. You can also nest _asm blocks, but the nesting doesn't affect variable
  3389.  scope.
  3390.  
  3391.  
  3392.  3.3  Using Assembly Language in _asm Blocks
  3393.  
  3394.  The in-line assembler has much in common with other assemblers. For example,
  3395.  it accepts any expression that is legal in MASM, and it supports all 80286
  3396.  and 80287 instructions. This section describes the use of assembly-language
  3397.  features in _asm blocks.
  3398.  
  3399.  
  3400.  Instruction Set
  3401.  
  3402.  The in-line assembler supports the full instruction set of the Intel 80286
  3403.  and 80287 processors. It does not recognize 80386- and 80387-specific
  3404.  instructions. To use 80286 or 80287 instructions, compile with the /G2
  3405.  option.
  3406.  
  3407.  
  3408.  Expressions
  3409.  
  3410.  In-line assembly code can use any MASM expression, that is, any combination
  3411.  of operands and operators that evaluates to a single value or address.
  3412.  
  3413.  
  3414.  Data Directives and Operators
  3415.  
  3416.  Although an _asm block can reference C data types and objects, it cannot
  3417.  define data objects with MASM directives or operators. Specifically, you
  3418.  cannot use the definition directives DB, DW, DD, DQ, DT, and DF, or the
  3419.  operators DUP or THIS. Nor are MASM structures and records available. The
  3420.  in-line assembler doesn't accept the directives STRUC, RECORD, WIDTH, or
  3421.  MASK.
  3422.  
  3423.  
  3424.  EVEN and ALIGN Directives
  3425.  
  3426.  While the in-line assembler doesn't support most MASM directives, it does
  3427.  support EVEN and ALIGN. These directives put NOP (no operation) instructions
  3428.  in the assembly code as needed to align labels to specific boundaries. This
  3429.  makes instruction-fetch operations more efficient for some processors (not
  3430.  including eight-bit processors such as the Intel 8088).
  3431.  
  3432.  
  3433.  Macros
  3434.  
  3435.  The in-line assembler is not a macro assembler. You cannot use MASM macro
  3436.  directives (MACRO, REPT, IRC, IRP, and ENDM) or macro operators ( <>, !, &,
  3437.  %, and .TYPE). An _asm block can use C preprocessor directives, however. See
  3438.  Section 3.4, "Using C in _asm Blocks" for more information.
  3439.  
  3440.  
  3441.  Segment References
  3442.  
  3443.  You must refer to segments by register rather than by name (the segment name
  3444.  _TEXT is invalid, for instance). Segment overrides must use the register
  3445.  explicitly, as in ES:[BX].
  3446.  
  3447.  
  3448.  Type and Variable Sizes
  3449.  
  3450.  The LENGTH, SIZE, and TYPE operators have a limited meaning in in-line
  3451.  assembly. They cannot be used at all with the DUP operator (because you
  3452.  cannot define data with MASM directives or operators). But you can use them
  3453.  to find the size of C variables or types:
  3454.  
  3455.  
  3456.    ■   The LENGTH operator can return the number of elements in an array. It
  3457.        returns the value 1 for nonarray variables.
  3458.  
  3459.    ■   The SIZE operator can return the size of a C variable. A variable's
  3460.        size is the product of its LENGTH and TYPE.
  3461.  
  3462.    ■   The TYPE operator can return the size of a C type or variable. If the
  3463.        variable is an array, TYPE returns the size of a single element of the
  3464.        array.
  3465.  
  3466.  
  3467.  For instance, if your program has an eight-element int array,
  3468.  
  3469.    int arr[8];
  3470.  
  3471.  the following C and assembly expressions yield the size of  arr  and its
  3472.  elements:
  3473.  
  3474. ╓┌───────────┌──────────────────────────┌────────────────────────────────────╖
  3475.  _asm        C                          Size
  3476.  ────────────────────────────────────────────────────────────────────────────
  3477.  LENGTH arr  sizeof(ar)/sizeof(arr[0])  8
  3478.  SIZE arr    sizeof (arr)               16
  3479.  TYPE arr    size14(arr[0])             2
  3480.  ────────────────────────────────────────────────────────────────────────────
  3481.  
  3482.  
  3483.  
  3484.  Comments
  3485.  
  3486.  Instructions in an _asm block can use assembly-language comments:
  3487.  
  3488.    _asm mov ax, offset buff ; Load address of buff
  3489.  
  3490.  Because C macros expand into a single logical line, avoid using
  3491.  assemblylanguage comments in macros (see Section 3.8, "Defining _asm Blocks
  3492.  as C Macros"). An _asm block can also contain C-style comments, as noted
  3493.  below.
  3494.  
  3495.  
  3496.  The _emit Pseudoinstruction
  3497.  
  3498.  The _emit pseudoinstruction is similar to the DB directive of MASM. It
  3499.  allows you to define a single immediate byte at the current location in the
  3500.  current text segment. However, _emit can define only one byte at a time, and
  3501.  it can only define bytes in the text segment. It uses the same syntax as the
  3502.  INT instruction.
  3503.  
  3504.  One use for _emit is to define 80386-specific instructions, which the
  3505.  in-line assembler does not support. The following fragment, for instance,
  3506.  defines the 80386 CWDE instruction:
  3507.  
  3508.    /* Assumes 16-bit mode */
  3509.    #define cwde _asm _emit 0x66 _asm _emit 0x98
  3510.     .
  3511.     .
  3512.     .
  3513.    _asm {
  3514.         cwde
  3515.         }
  3516.  
  3517.  
  3518.  Debugging and Listings
  3519.  
  3520.   In-line assembly code can be debugged with CodeView.
  3521.  
  3522.  Programs containing in-line assembly code can be debugged with the CodeView
  3523.  debugger, assuming you compile with the /Zi option.
  3524.  
  3525.  Within CodeView, you can set breakpoints on both C and assembly-language
  3526.  lines. If you enable mixed assembly and C mode, you can display both the
  3527.  source and disassembled form of the assembly code.
  3528.  
  3529.  Note that putting multiple assembly instructions or C statements on one line
  3530.  can hamper debugging with CodeView. In source mode, the CodeView debugger
  3531.  lets you set breakpoints on a single line but not on individual statements
  3532.  on the same line. The same principle applies to an _asm block defined as a C
  3533.  macro, which expands to a single logical line.
  3534.  
  3535.  If you create a mixed source and assembly listing with the /Fc compiler
  3536.  option, the listing contains both the source and assembly forms of each
  3537.  assemblylanguage line. Macros are not expanded in listings, but they are
  3538.  expanded during compilation.
  3539.  
  3540.  See Chapter 9, "Debugging C Programs with CodeView," for more information.
  3541.  
  3542.  
  3543.  3.4  Using C in _asm Blocks
  3544.  
  3545.  Because in-line assembly instructions can be mixed with C statements, they
  3546.  can refer to C variables by name and use many other elements of C. An _asm
  3547.  block can use the following C language elements:
  3548.  
  3549.  
  3550.    ■   Symbols, including labels and variable and function names
  3551.  
  3552.    ■   Constants, including symbolic constants and enum members
  3553.  
  3554.    ■   Macros and preprocessor directives
  3555.  
  3556.    ■   Comments (both  /* */  and  // )
  3557.  
  3558.    ■   Type names (wherever a MASM type would be legal)
  3559.  
  3560.    ■   typedef names, generally used with operators such as PTR and TYPE or
  3561.        to specify structure or union members
  3562.  
  3563.  
  3564.  Within an _asm block, you can specify integer constants with either C
  3565.  notation or assembler radix notation (0x100 and 100h are equivalent, for
  3566.  instance). This allows you to define (using #define) a constant in C, and
  3567.  use it in both C and assembly portions of the program. You can also specify
  3568.  constants in octal by preceding them with a 0. For example, 0777 specifies
  3569.  an octal constant.
  3570.  
  3571.  
  3572.  3.4.1  Using Operators
  3573.  
  3574.  An _asm block cannot use C-specific operators, such as the  operator.
  3575.  However, operators shared by C and MASM, such as the * operator, are
  3576.  interpreted as assembly-language operators. For instance, outside an _asm
  3577.  block, square brackets ( [] ) are interpreted as enclosing array subscripts,
  3578.  which C automatically scales to the size of an element in the array. Inside
  3579.  an _asm block, they are seen as the MASM index operator, which yields an
  3580.  unscaled byte offset from any data object or label (not just an array). The
  3581.  following code illustrates the difference:
  3582.  
  3583.    int array[10];
  3584.  
  3585.    _asm mov array[6], bx ;  Store BX at array+6 (not scaled)
  3586.  
  3587.    array[6] = 0;         /* Store 0 at array+12 (scaled) */
  3588.  
  3589.  The first reference to  array  is not scaled, but the second is. Note that
  3590.  you can use the TYPE operator to achieve scaling based on a constant. For
  3591.  instance, the following statements are equivalent:
  3592.  
  3593.    _asm mov array[6 * TYPE int], 0 ; Store 0 at array + 12
  3594.  
  3595.    array[6] = 0;                   /* Store 0 at array + 12 */
  3596.  
  3597.  
  3598.  3.4.2  Using C Symbols
  3599.  
  3600.  An _asm block can refer to any C symbol in scope where the block appears. (C
  3601.  symbols are variable names, function names, and labels─in other words, names
  3602.  that aren't symbolic constants or enum members.)
  3603.  
  3604.  A few restrictions apply to the use of C symbols:
  3605.  
  3606.  
  3607.    ■   Each assembly-language statement can contain only one C symbol.
  3608.        Multiple symbols can appear in the same assembly instruction only with
  3609.        LENGTH, TYPE, and SIZE expressions.
  3610.  
  3611.    ■   Functions referenced in an _asm block must be declared (prototyped)
  3612.        earlier in the program. Otherwise, the compiler cannot distinguish
  3613.        between function names and labels in the _asm block.
  3614.  
  3615.    ■   An _asm block cannot use any C symbols with the same spelling as MASM
  3616.        reserved words (regardless of case). MASM reserved words include
  3617.        instruction names such as PUSH and register names such as SI.
  3618.  
  3619.    ■   Structure and union tags are not recognized in _asm blocks.
  3620.  
  3621.  
  3622.  
  3623.  3.4.3  Accessing C Data
  3624.  
  3625.  A great convenience of in-line assembly is the ability to refer to C
  3626.  variables by name. An _asm block can refer to any symbols─including variable
  3627.  names─that are in scope where the block appears. For instance, if the C
  3628.  variable  var  is in scope, the instruction
  3629.  
  3630.    _asm mov ax, var
  3631.  
  3632.  stores the value of  var  in AX.
  3633.  
  3634.  If a structure or union member has a unique name, an _asm block can refer to
  3635.  it using only the member name, without specifying the C variable or typedef
  3636.  name before the period (.) operator. If the member name is not unique,
  3637.  however, you must place a variable or typedef name immediately before the
  3638.  period (.) operator. For instance, the following structure types share
  3639.  same_name  as their member name:
  3640.  
  3641.    struct first_type
  3642.    {
  3643.       char *weasel;
  3644.       int same_name;
  3645.    };
  3646.  
  3647.    struct second_type
  3648.    {
  3649.       int wonton;
  3650.       long same_name;
  3651.    };
  3652.  
  3653.  If you declare variables with the types
  3654.  
  3655.    struct first_type hal;
  3656.    struct second_type oat;
  3657.  
  3658.  all references to the member  same_name  must use the variable name, because
  3659.   same_name  is not unique. But the member  weasel  has a unique name, so you
  3660.  can refer to it using only its member name:
  3661.  
  3662.    _asm
  3663.    {
  3664.       mov bx, OFFSET hal
  3665.       mov cx, [bx]hal.same_name ; Must use 'hal'
  3666.       mov si, [bx].weasel       ; Can omit 'hal'
  3667.    }
  3668.  
  3669.  Note that omitting the variable name is merely a coding convenience. The
  3670.  same assembly instructions are generated whether or not it is present.
  3671.  
  3672.  
  3673.  3.4.4  Writing Functions
  3674.  
  3675.  If you write a function with in-line assembly code, it's a simple matter to
  3676.  pass arguments to the function and return a value from it. The following
  3677.  examples compare a function first written for a separate assembler and then
  3678.  rewritten for the in-line assembler. The function, called  power2, receives
  3679.  two parameters, multiplying the first parameter by 2 to the power of the
  3680.  second parameter. Written for a separate assembler, the function might look
  3681.  like this:
  3682.  
  3683.    ; POWER.ASM
  3684.    ; Compute the power of an integer
  3685.    ;
  3686.           PUBLIC _power2
  3687.    _TEXT SEGMENT WORD PUBLIC 'CODE'
  3688.    _power2 PROC
  3689.  
  3690.            push bp         ; Save BP
  3691.            mov bp, sp      ; Move SP into BP so we can refer
  3692.                            ;   to arguments on the stack
  3693.            mov ax, [bp+4]  ; Get first argument
  3694.            mov cx, [bp+6]  ; Get second argument
  3695.            shl ax, cl      ; AX = AX * ( 2 ^ CL )
  3696.            pop bp          ; Restore BP
  3697.            ret             ; Return with sum in AX
  3698.  
  3699.    _power2 ENDP
  3700.    _TEXT   ENDS
  3701.            END
  3702.  
  3703.   Function arguments are usually passed on the stack.
  3704.  
  3705.  Since it's written for a separate assembler, the function requires a
  3706.  separate source file and assembly and link steps. C function arguments
  3707.  usually are passed on the stack, so this version of the  power2  function
  3708.  accesses its arguments by their positions on the stack. (Note that the MODEL
  3709.  directive, available in MASM and some other assemblers, also allows you to
  3710.  access stack arguments and local stack variables by name.)
  3711.  
  3712.  The POWER2.C program below writes the  power2  function with in-line
  3713.  assembly code:
  3714.  
  3715.    /* POWER2.C */
  3716.    #include <stdio.h>
  3717.  
  3718.    int power2( int num, int power );
  3719.  
  3720.    void main( void )
  3721.    {
  3722.       printf( "3 times 2 to the power of 5 is %d\n", \
  3723.               power2( 3, 5) );
  3724.    }
  3725.  
  3726.    int power2( int num, int power )
  3727.    {
  3728.       _asm
  3729.       {
  3730.          mov ax, num    ; Get first argument
  3731.          mov cx, power  ; Get second argument
  3732.          shl ax, cl     ; AX = AX * ( 2 to the power of CL )
  3733.       }
  3734.       /* Return with result in AX */
  3735.    }
  3736.  
  3737.  The in-line version of the  power2  function refers to its arguments by name
  3738.  and appears in the same source file as the rest of the program. This version
  3739.  also requires fewer assembly instructions. Since C automatically preserves
  3740.  BP, the _asm block doesn't need to do so. It can also dispense with the RET
  3741.  instruction, since the C part of the function performs the return.
  3742.  
  3743.  Because the in-line version of  power2  doesn't execute a C return
  3744.  statement, it causes a harmless warning if you compile at warning levels 2
  3745.  or higher:
  3746.  
  3747.    warning C4035: 'power2' : no return value
  3748.  
  3749.  The function does return a value, but the compiler cannot tell that in the
  3750.  absence of a return statement. Simply ignore the warning in this context.
  3751.  
  3752.  
  3753.  3.5  Using and Preserving Registers
  3754.  
  3755.  In general, you should not assume that a register will have a given value
  3756.  when an _asm block begins. An _asm block inherits whatever register values
  3757.  happen to result from the normal flow of control.
  3758.  
  3759.  If you use the _fastcall calling convention, the compiler passes function
  3760.  arguments in registers instead of the stack. This can create problems in
  3761.  functions with _asm blocks, since a function has no way to tell which
  3762.  parameter is in which register. If the function happens to receive a
  3763.  parameter in AX and immediately stores something else in AX, the parameter
  3764.  is lost. In addition, you must preserve the CX and ES registers in any
  3765.  function declared with _fastcall.
  3766.  
  3767.   Don't use the _fastcall calling convention for functions with _asm blocks.
  3768.  
  3769.  
  3770.  To avoid such register conflicts, don't use the _fastcall convention for
  3771.  functions that contain an _asm block. If you specify the _fastcall
  3772.  convention globally with the /Gr compiler option, declare every function
  3773.  containing an _asm block with _cdecl. (The _cdecl attribute tells the
  3774.  compiler to use the normal C calling convention for that function.) If you
  3775.  are not compiling with /Gr, avoid declaring the function with the _fastcall
  3776.  attribute.
  3777.  
  3778.  As you may have noticed in the POWER2.C example in Section 3.4.4, the
  3779.  power2  function doesn't preserve the value in the AX register. When you
  3780.  write a function in assembly language, you don't need to preserve the AX,
  3781.  BX, CX, DX, ES, and flags registers. However, you should preserve any other
  3782.  registers you use (DI, SI, DS, SS, SP, and BP).
  3783.  
  3784.  ────────────────────────────────────────────────────────────────────────────
  3785.  WARNING
  3786.  
  3787.  If your in-line assembly code changes the direction flag using the STD or
  3788.  CLD instructions, you must restore the flag to its original value.
  3789.  ────────────────────────────────────────────────────────────────────────────
  3790.  
  3791.   Functions return values in the AX and DX registers.
  3792.  
  3793.  The POWER2.C example in Section 3.4.4 also shows that functions return
  3794.  values in registers. This is true whether the function is written in
  3795.  assembly language or in C.
  3796.  
  3797.  If the return value is short (a char, int, or near pointer), it is stored in
  3798.  AX. The POWER2.C example returned a value by terminating with the desired
  3799.  value in AX.
  3800.  
  3801.  If the return value is long, store the high word in DX and the low word in
  3802.  AX. To return a longer value (such as a floating-point value), store the
  3803.  value in memory and return a pointer to the value (in AX if near or in DX:AX
  3804.  if far).
  3805.  
  3806.  Assembly instructions that appear in-line with C statements are free to
  3807.  alter the AX, BX, CX, and DX registers. C doesn't expect these registers to
  3808.  be maintained between statements, so you don't need to preserve them. The
  3809.  same is true of the SI and DI registers, with some exceptions (see Section
  3810.  3.9, "Optimizing"). You should preserve the SP and BP registers unless you
  3811.  have some reason to change them─to switch stacks, for instance.
  3812.  
  3813.  
  3814.  3.6  Jumping to Labels
  3815.  
  3816.  Like an ordinary C label, a label in an _asm block has scope throughout the
  3817.  function in which it is defined (not only in the block). Both assembly
  3818.  instructions and C goto statements can jump to labels inside or outside the
  3819.  _asm block.
  3820.  
  3821.   Labels in _asm blocks have function scope and are not case sensitive.
  3822.  
  3823.  Unlike C labels, labels defined in _asm blocks are not case sensitive, even
  3824.  when used in C statements. C labels are not case sensitive in an _asm block,
  3825.  either. (Outside an _asm block, a C label is case sensitive as usual.) The
  3826.  following do-nothing code shows all the permutations:
  3827.  
  3828.    void func( void )
  3829.    {
  3830.       goto C_Dest;  /* legal */
  3831.       goto c_dest;  /* error */
  3832.  
  3833.       goto A_Dest;  /* legal */
  3834.       goto a_dest;  /* legal */
  3835.  
  3836.       _asm
  3837.       {
  3838.          jmp C_Dest ; legal
  3839.          jmp c_dest ; legal
  3840.  
  3841.          jmp A_Dest ; legal
  3842.          jmp a_dest ; legal
  3843.  
  3844.          a_dest:    ; _asm label
  3845.       }
  3846.  
  3847.       C_Dest:       /* C label */
  3848.       return;
  3849.    }
  3850.  
  3851.  Don't use C library function names as labels in _asm blocks. For instance,
  3852.  you might be tempted to use  exit  as a label,
  3853.  
  3854.    jne exit
  3855.       .
  3856.       .
  3857.       .
  3858.    exit:
  3859.       ; More _asm code follows
  3860.  
  3861.  forgetting that exit is the name of a C library function. The code doesn't
  3862.  cause a compiler error, but it might cause a jump to the exit function
  3863.  instead of the desired location.
  3864.  
  3865.  As in MASM programs, the dollar symbol ($) serves as the current location
  3866.  counter─a label for the instruction currently being assembled. In _asm
  3867.  blocks, its main use is to make long conditional jumps:
  3868.  
  3869.    jne $+5 ; next instruction is 5 bytes long
  3870.    jmp farlabel
  3871.    ; $+5
  3872.       .
  3873.       .
  3874.       .
  3875.    farlabel:
  3876.  
  3877.  
  3878.  3.7  Calling C Functions
  3879.  
  3880.  An _asm block can call C functions, including C library routines. The
  3881.  following example calls the printf library routine:
  3882.  
  3883.    #include <stdio.h>
  3884.  
  3885.    char format[] = "%s %s\n";
  3886.    char hello[] = "Hello";
  3887.    char world[] = "world";
  3888.  
  3889.    void main( void )
  3890.    {
  3891.       _asm
  3892.       {
  3893.          mov  ax, offset world
  3894.          push ax
  3895.          mov  ax, offset hello
  3896.          push ax
  3897.          mov  ax, offset format
  3898.          push ax
  3899.          call printf
  3900.       }
  3901.    }
  3902.  
  3903.  Since function arguments are passed on the stack, you simply push the needed
  3904.  arguments─string pointers, in the example above─before calling the function.
  3905.  The arguments are pushed in reverse order, so they come off the stack in the
  3906.  desired order. To emulate the C statement
  3907.  
  3908.    printf( format, hello, world );
  3909.  
  3910.  the example pushes pointers to  world,  hello, and  format, in that order,
  3911.  then calls printf.
  3912.  
  3913.  
  3914.  3.8  Defining _asm Blocks as C Macros
  3915.  
  3916.  C macros offer a convenient way to insert assembly code into C code, but
  3917.  they demand extra care because a macro expands into a single logical line.
  3918.  To create trouble-free macros, follow these rules:
  3919.  
  3920.  
  3921.    ■   Enclose the _asm block in braces.
  3922.  
  3923.    ■   Put the _asm keyword in front of each assembly instruction.
  3924.  
  3925.    ■   Use old-style C comments ( /* comment */ ) instead of assembly-style
  3926.        comments ( ; comment ) or single-line C comments ( // comment ).
  3927.  
  3928.  
  3929.  To illustrate, the following example defines a simple macro:
  3930.  
  3931.    #define BEEP _asm \
  3932.    /* Beep sound */       \
  3933.    {                       \
  3934.       _asm mov ah, 2       \
  3935.       _asm mov dl, 7       \
  3936.       _asm int 21h         \
  3937.    }
  3938.  
  3939.  At first glance, the last three _asm keywords seem superfluous. They are
  3940.  needed, however, because the macro expands into a single line:
  3941.  
  3942.    _asm /* Beep sound */ { _asm mov ah, 2  _asm mov dl, 7 _asm int 21h }
  3943.  
  3944.  The third and fourth _asm keywords are needed as statement separators. The
  3945.  only statement separators recognized in _asm blocks are the newline
  3946.  character and _asm keyword. And since a block defined as a macro is one
  3947.  logical line, you must separate each instruction with _asm.
  3948.  
  3949.  The braces are essential as well. If you omit them, the compiler can be
  3950.  confused by C statements on the same line to the right of the macro
  3951.  invocation. Without the closing brace, the compiler cannot tell where
  3952.  assembly code stops, and it sees C statements after the _asm block as
  3953.  assembly instructions.
  3954.  
  3955.   Use C comments in _asm blocks written as macros.
  3956.  
  3957.  Assembly-style comments that start with a semicolon (;) continue to the end
  3958.  of the line. This causes problems in macros because the compiler ignores
  3959.  everything after the comment, all the way to the end of the logical line.
  3960.  The same is true of single-line C comments ( // comment ). To prevent
  3961.  errors, use old-style C comments ( /* comment */ ) in _asm blocks defined as
  3962.  macros.
  3963.  
  3964.   An _asm block written as a C macro can take arguments but cannot return a
  3965.  value.
  3966.  
  3967.  An _asm block written as a C macro can take arguments. Unlike an ordinary C
  3968.  macro, however, an _asm macro cannot return a value. So you cannot use such
  3969.  macros in C expressions.
  3970.  
  3971.  Be careful not to invoke macros of this type indiscriminately. For instance,
  3972.  invoking an assembly-language macro in a function declared with the
  3973.  _fastcall con-vention may cause unexpected results. (See Section 3.5, "Using
  3974.  and Preserving Registers.")
  3975.  
  3976.   You can convert MASM macros to C macros.
  3977.  
  3978.  Note that some MASM-style macros can be written as C macros. Below is a MASM
  3979.  macro that sets the video page to the value specified in the  page
  3980.  argument:
  3981.  
  3982.    setpage   MACRO page
  3983.              mov ah, 5
  3984.              mov al, page
  3985.              int 10h
  3986.              ENDM
  3987.  
  3988.  The following code defines  setpage  as a C macro:
  3989.  
  3990.    #define setpage( page ) _asm  \
  3991.       {                                \
  3992.          _asm mov ah, 5                \
  3993.          _asm mov al, page             \
  3994.          _asm int 10h                  \
  3995.    }
  3996.  
  3997.  Both macros do the same job.
  3998.  
  3999.  
  4000.  3.9  Optimizing
  4001.  
  4002.  The presence of an _asm block in a function affects optimization in a few
  4003.  different ways. First, as you might expect, the compiler doesn't try to
  4004.  optimize the _asm block itself. What you write in assembly language is
  4005.  exactly what you get.
  4006.  
  4007.  Second, the presence of an _asm block affects register variable storage.
  4008.  Under normal circumstances (unless you suppress optimization with the /Od
  4009.  option) the compiler automatically stores variables in registers. This is
  4010.  not done, however, in any function that contains an _asm block. To get
  4011.  register variable storage in such a function, you must request it with the
  4012.  register keyword.
  4013.  
  4014.  Since the compiler stores register variables in the SI and DI registers,
  4015.  these registers represent variables in functions that request register
  4016.  storage. The first eligible variable is stored in SI and the second in DI.
  4017.  Preserve SI and DI in such functions unless you want to change the register
  4018.  variables.
  4019.  
  4020.  Keep in mind that the name of a variable declared with register translates
  4021.  directly into a register reference (assuming a register is available for
  4022.  such use). For instance, if you declare
  4023.  
  4024.    register int sample;
  4025.  
  4026.  and the variable  sample  happens to be stored in SI, then the _asm
  4027.  instruction
  4028.  
  4029.    _asm mov ax, sample
  4030.  
  4031.  is equivalent to
  4032.  
  4033.    _asm mov ax, si
  4034.  
  4035.  If you declare a variable with register and the compiler cannot store the
  4036.  variable in a register, the compiler issues a warning to that effect at
  4037.  compile time. The solution is to remove the register declaration from that
  4038.  variable.
  4039.  
  4040.  Register variables form a slight exception to the general rule that an
  4041.  assembly-language statement can contain no more than one C symbol. If one of
  4042.  the symbols is a register variable, for example,
  4043.  
  4044.    register int v1;
  4045.    int v2;
  4046.  
  4047.  then an instruction can use two C symbols, as in
  4048.  
  4049.    mov v1, v2
  4050.  
  4051.  Finally, the presence of in-line assembly code inhibits the following
  4052.  optimizations for the entire function in which the code appears:
  4053.  
  4054.  
  4055.    ■   Loop ( /Ol )
  4056.  
  4057.    ■   Global register allocation ( /Oe )
  4058.  
  4059.    ■   Global optimizations and common subexpressions ( /Og )
  4060.  
  4061.  
  4062.  These optimizations are suppressed no matter which compiler options you use.
  4063.  
  4064.  
  4065.  
  4066.  
  4067.  
  4068.  
  4069.  
  4070.  Chapter 4  Controlling Floating-Point Math Operations
  4071.  ────────────────────────────────────────────────────────────────────────────
  4072.  
  4073.  This chapter describes how to control the way your Microsoft C programs
  4074.  perform floating-point math operations. It describes the math packages that
  4075.  you can include in C libraries when you run the SETUP program, then
  4076.  discusses  the options you can specify in the Programmer's WorkBench (PWB)
  4077.  or on the CL command line to choose the appropriate library for linking and
  4078.  controlling floating-point instructions.
  4079.  
  4080.  This chapter also explains how to override floating-point options by
  4081.  changing libraries at link time, and how to control use of the Intel math
  4082.  coprocessor (80x87) using the NO87 environment variable.
  4083.  
  4084.  
  4085.  4.1  Declaring Floating-Point Types
  4086.  
  4087.  Microsoft C supports three floating-point types that conform to the
  4088.  Institute of Electrical and Electronics Engineers (IEEE) standard 754
  4089.  format:
  4090.  
  4091.  
  4092.    1.  Type float, a 32-bit floating-point quantity
  4093.  
  4094.    2.  Type double, a 64-bit floating-point quantity
  4095.  
  4096.    3.  Type long double, an 80-bit floating-point quantity
  4097.  
  4098.  
  4099.  You can declare variables as any of these types. You can also declare
  4100.  functions that return any of these types.
  4101.  
  4102.  
  4103.  4.1.1  Declaring Variables as Floating-Point Types
  4104.  
  4105.  You can declare variables as float, double, or long double, depending on the
  4106.  needs of your application. The principal differences between the three types
  4107.  are the significance they can represent, the storage they require, and their
  4108.  range. Table 4.1 shows the relationship between significance and storage
  4109.  requirements.
  4110.  
  4111.  Table 4.1  Floating-Point Types
  4112.  
  4113. ╓┌─────────────┌───────────────────┌─────────────────────────────────────────╖
  4114.  Type          Significant Digits  Number of Bytes
  4115.  ────────────────────────────────────────────────────────────────────────────
  4116.  float         6-7                 4
  4117.  double        15-16               8
  4118.  Type          Significant Digits  Number of Bytes
  4119.  ────────────────────────────────────────────────────────────────────────────
  4120. double        15-16               8
  4121.  long double   19                  10
  4122.  ────────────────────────────────────────────────────────────────────────────
  4123.  
  4124.  
  4125.  Floating-point variables are represented by a mantissa, which contains the
  4126.  value of the number, and an exponent, which contains the order of magnitude
  4127.  of the number.
  4128.  
  4129.  Table 4.2 shows the number of bits allocated to the mantissa and the
  4130.  exponent for each floating-point type. The most-significant bit of any
  4131.  float, double, or long double is always the sign bit. If it is 1, the number
  4132.  is considered negative; otherwise, it is considered a positive number.
  4133.  
  4134.  Table 4.2  Lengths of Exponents and Mantissas
  4135.  
  4136. ╓┌─────────────┌────────────────┌────────────────────────────────────────────╖
  4137.  Type          Exponent Length  Mantissa Length
  4138.  ────────────────────────────────────────────────────────────────────────────
  4139.  Type          Exponent Length  Mantissa Length
  4140.  ────────────────────────────────────────────────────────────────────────────
  4141.  float         8 bits           23 bits
  4142.  double        11 bits          52 bits
  4143.  long double   15 bits          64 bits
  4144.  ────────────────────────────────────────────────────────────────────────────
  4145.  
  4146.  
  4147.  Because exponents are stored in an unsigned form, the exponent is biased by
  4148.  half its possible value. For type float, the bias is 127; for type double,
  4149.  it is 1,023; for type long double, it is 16,383. You can compute the actual
  4150.  exponent value by subtracting the bias value from the exponent value.
  4151.  
  4152.  The mantissa is stored as a binary fraction greater than or equal to 1 and
  4153.  less than 2. For types float and double, there is an implied leading 1 in
  4154.  the mantissa in the most-significant bit position, so the mantissas are
  4155.  actually 24 and 53 bits long, respectively, even though the most-significant
  4156.  bit is never stored in memory.
  4157.  
  4158.  Instead of the storage method just described, the floating-point package can
  4159.  store binary floating-point numbers as denormalized numbers. Denormalized
  4160.  numbers are nonzero floating-point numbers with reserved exponent values in
  4161.  which the most-significant bit of the mantissa is zero. By using
  4162.  denormalized format, the range of a floating-point number can be extended at
  4163.  the cost of precision. You cannot control whether a floating-point number is
  4164.  represented in normalized or denormalized form; the floating-point package
  4165.  determines the representation. The floating-point packages never use
  4166.  denormalized form unless the exponent becomes less than the minimum that can
  4167.  be represented in a normalized form.
  4168.  
  4169.  Table 4.3 shows the minimum and maximum value you can store in variables of
  4170.  each floating-point type. The values listed in this table apply only to
  4171.  normalized floating-point numbers; denormalized floating-point numbers have
  4172.  a smaller minimum value. Note that numbers retained in 80x87 registers are
  4173.  always represented in 80-bit normal form; numbers can only be represented in
  4174.  denormal form when stored in 32- or 64-bit floating-point variables (type
  4175.  float and type long).
  4176.  
  4177.  Table 4.3  Range of Floating-Point Types
  4178.  
  4179. ╓┌─────────────┌──────────────────────────────┌──────────────────────────────╖
  4180.  Type          Minimum Value                  Maximum Value
  4181.  Type          Minimum Value                  Maximum Value
  4182.  ────────────────────────────────────────────────────────────────────────────
  4183.  float         1.175494351 E - 38             3.402823466 E + 38
  4184.  double        2.2250738585072014 E - 308     1.7976931348623158 E + 308
  4185.  long double   3.362103143112093503 E - 4932  1.189731495357231765 E + 4932
  4186.  ────────────────────────────────────────────────────────────────────────────
  4187.  
  4188.  
  4189.  If precision is less of a concern than storage, consider using type float
  4190.  for floating-point variables. Conversely, if precision is the most important
  4191.  criterion, use type long double.
  4192.  
  4193.   Microsoft C observes type-widening rules.
  4194.  
  4195.  Floating-point variables can be promoted to a type of greater significance
  4196.  (for example, from type float to type double). Promotion often occurs when
  4197.  you perform arithmetic on floating-point variables. This arithmetic is
  4198.  always done in as high a degree of precision as the variable with the
  4199.  highest degree of precision. For example, consider the following type
  4200.  declarations:
  4201.  
  4202.    float f_short;
  4203.    double f_long;
  4204.    long double f_longer;
  4205.  
  4206.    f_short = f_short * f_long;
  4207.  
  4208.  In the preceding example, the variable  f_short  is promoted to type double
  4209.  and multiplied by  f_long; then the result is rounded to type float before
  4210.  being assigned to  f_short.
  4211.  
  4212.  In the example below (which uses the declarations from the preceding
  4213.  example), the arithmetic is done in float (32-bit) precision on the
  4214.  variables; the result is then promoted to type long double.
  4215.  
  4216.    f_longer = f_short * f_short;
  4217.  
  4218.  
  4219.  4.1.2  Declaring Functions that Return Floating-Point Types
  4220.  
  4221.  You can declare functions that return the floating-point types float,
  4222.  double, and long double. Functions that return types float or double do not
  4223.  place their return values in registers; they place their return values in a
  4224.  global location called the floating-point accumulator ( fac).
  4225.  
  4226.  When declaring a function as a floating-point type in a multithreaded
  4227.  program for OS/2, you should use the _pascal keyword to specify the
  4228.  FORTRAN/Pascal calling convention. Declaring the function as _pascal causes
  4229.  the return value to be placed on the stack, rather than in the
  4230.  floating-point accumulator,  fac.
  4231.  
  4232.   You can write re-entrant functions that return floating-point types.
  4233.  
  4234.  Using the current thread's private stack to return values allows you to
  4235.  write re-entrant functions by eliminating possible contention between
  4236.  threads for the floating-point accumulator.
  4237.  
  4238.  ────────────────────────────────────────────────────────────────────────────
  4239.  NOTE
  4240.  
  4241.  Functions that return type long double always place their return values on
  4242.  the stack. You need not use the _pascal keyword with functions declared as
  4243.  long double.
  4244.  ────────────────────────────────────────────────────────────────────────────
  4245.  
  4246.  
  4247.  4.2  C Run-Time Library Support of Type long double
  4248.  
  4249.  All of the Microsoft C run-time libraries support type long double. Each of
  4250.  the normal floating-point math functions has a special version that supports
  4251.  type long double. These functions have the same name as the functions that
  4252.  support type float and type double, except that they end with l. For
  4253.  example, the function that returns the absolute value of a variable of type
  4254.  float or type double is fabs. The long double equivalent function is fabsl.
  4255.  The two exceptions to this rule are the _atold and _strtodl functions.
  4256.  
  4257.  
  4258.  4.3  Summary of Math Packages
  4259.  
  4260.  The Microsoft C compiler offers a choice of the following three math
  4261.  packages for handling floating-point operations:
  4262.  
  4263.  
  4264.    1.  Emulator (default)
  4265.  
  4266.    2.  Math coprocessor (a library that supports the Intel 80x87 family of
  4267.        math coprocessors)
  4268.  
  4269.    3.  Alternate math
  4270.  
  4271.  
  4272.  When you install Microsoft C, the SETUP program allows you to build combined
  4273.  libraries. These libraries include the floating-point math library that you
  4274.  choose. Any programs linked with that library use the math package included
  4275.  in the library; you must use the appropriate PWB or CL option to make sure
  4276.  that the library you want is used at link time.
  4277.  
  4278.  The following descriptions of these math packages are designed to help you
  4279.  choose the appropriate math option for your needs when you build a library
  4280.  using SETUP. For more information about SETUP and about building combined
  4281.  libraries, see Installing and Using the Microsoft C Professional Development
  4282.  System.
  4283.  
  4284.  Note that this chapter does not describe mode-specific libraries. For
  4285.  simplicity, the base names of libraries are noted in their default form;
  4286.  that is mLIBCf.LIB, where m is the model designator and  f is the
  4287.  floating-point math package designator. For information about mode-specific
  4288.  libraries, see Chapter 14, "Building OS/2 Applications," or Installing and
  4289.  Using the Microsoft C Professional Development System.
  4290.  
  4291.  
  4292.  4.3.1  Emulator Package
  4293.  
  4294.  Programs created using the emulator math package automatically detect and
  4295.  use an 80x87 numeric coprocessor if one is installed. If no coprocessor is
  4296.  installed, these 80x87 instructions are carried out in software. The
  4297.  emulator package is the default math package; SETUP uses it if you do not
  4298.  explicitly choose another package. Also, the emulator math option is the
  4299.  option selected by default by the compiler if no other floating-point math
  4300.  option is specified.
  4301.  
  4302.  Use the emulator math package to maximize accuracy on systems without math
  4303.  coprocessors or if your program will be run on some systems with
  4304.  coprocessors and some systems without coprocessors.
  4305.  
  4306.  The emulator package performs basic operations to the same degree of
  4307.  accuracy as a math coprocessor. However, the emulator routines used for
  4308.  transcendental math functions (such as sin, cos, tan) differ slightly from
  4309.  the corresponding functions performed on a coprocessor. This difference can
  4310.  cause a slight discrepancy (usually within two bits) between the results of
  4311.  these operations when performed with the software emulation instead of with
  4312.  a math coprocessor.
  4313.  
  4314.   When you use the emulator package, some floating-point exceptions are
  4315.  masked.
  4316.  
  4317.  When you use a math coprocessor or the emulator floating-point math package,
  4318.  interrupt-enable, precision, underflow, and denormalized-operand exceptions
  4319.  are masked by default. The remaining floating-point exceptions are unmasked.
  4320.  See the discussion of the _control87 function in on-line help for more
  4321.  information about 80x87 floating-point exceptions.
  4322.  
  4323.  
  4324.  4.3.2  Math Coprocessor Package
  4325.  
  4326.  The math coprocessor package utilizes the 80x87 math coprocessor exclusively
  4327.  for floating-point calculations. If you use the math coprocessor package,
  4328.  the machine on which your application is to run must have an 80x87
  4329.  coprocessor to perform floating-point operations. This package gives you the
  4330.  fastest, smallest programs possible for handling floating-point math.
  4331.  
  4332.  
  4333.  4.3.3  Alternate Math Package
  4334.  
  4335.  The alternate math package gives you the smallest and fastest programs
  4336.  possible without a coprocessor. However, the program results are not as
  4337.  accurate as results given by the emulator package.
  4338.  
  4339.  The alternate math package uses the same format as the IEEE standard-format
  4340.  numbers with less precision and weaker error checking. The alternate math
  4341.  package does not support infinities, NANs ("not a number"), and denormal
  4342.  numbers.
  4343.  
  4344.  You must always use the alternate math package when developing routines that
  4345.  are to be placed in an OS/2 dynamic-link library (DLL) using LLIBCDLL.LIB.
  4346.  Do not, however, use the alternate math package for building the C run-time
  4347.  DLL using CDLLOBJS.LIB; instead, use the emulator math package. For more
  4348.  information about creating dynamic-link libraries for OS/2, see Chapter 16.
  4349.  
  4350.  
  4351.  
  4352.  4.4  Selecting Floating-Point Options (/FP)
  4353.  
  4354.  You can select a floating-point library and the method of accessing
  4355.  floatingpoint routines by setting options in PWB or by specifying
  4356.  command-line options to CL. You can choose between the emulator, alternate,
  4357.  or math coprocessor library. You can also access the floating-point routines
  4358.  by issuing a function call (or calls) or by generating in-line 80x87
  4359.  instructions to execute the floating-point operation. The smallest and the
  4360.  fastest floating-point math option is the in-line math coprocessor package
  4361.  because the compiler generates true 80x87 coprocessor instructions. If,
  4362.  however, you cannot depend on the target computer having a coprocessor, you
  4363.  must use either the emulator or alternate math options.
  4364.  
  4365.  To specify floating-point options on the CL command line, you must specify
  4366.  an option from the list in Table 4.4. You specify these options to CL
  4367.  starting with the floating-point option string /FP.
  4368.  
  4369.  Based on the floating-point option and the memory-model option you choose,
  4370.  the compiler embeds a library name in the object file that it creates. This
  4371.  library is then considered the default library; that is, the linker searches
  4372.  in the standard places for a library with that name. If it finds a library
  4373.  with that name, the linker uses the library to resolve external references
  4374.  in the object file being linked. Otherwise, it displays a message indicating
  4375.  that it could not find the library.
  4376.  
  4377.  This mechanism allows the linker to automatically link object files with the
  4378.  appropriate library. However, you can link with a different library in some
  4379.  cases. See Table 4.4 and Section 4.5, "Library Considerations for
  4380.  Floating-Point Options," for more information about linking with different
  4381.  libraries.
  4382.  
  4383.  Table 4.4 summarizes the floating-point options and their effects. These
  4384.  options are described in detail in the following sections.
  4385.  
  4386.  Table 4.4  Summary of Floating-Point Options
  4387.  
  4388. ╓┌──────────────────────┌──────────────┌──────────────┌────────────────────┌─
  4389.  Option for CL for PWB  Combined Use                                       Lib
  4390.                         of Method      Effect         Coprocessor          Sel
  4391.  Option for CL for PWB  Combined Use                                       Lib
  4392.                         of Method      Effect         Coprocessor          Sel
  4393.  ─────────────────────────────────────────────────────────────────────────────
  4394.  /FPi                   In-line        Default;       Uses coprocessor if  mLI
  4395.  In-Line                               larger than    present(1)
  4396.  Emulation                             /FPi87, but
  4397.                                        can work
  4398.                                        without a
  4399.                                        coprocessor;
  4400.                                        most
  4401.                                        efficient way
  4402.                                        to get
  4403.                                        maximum
  4404.                                        precision
  4405.                                        without a
  4406.                                        coprocessor
  4407.  
  4408.  /FPi87                 In-line        Smallest and   Requires             mLI
  4409.  In-Line Math                          fastest        coprocessor
  4410.  Coprocessor                           option
  4411.                                        available
  4412.  Option for CL for PWB  Combined Use                                       Lib
  4413.                         of Method      Effect         Coprocessor          Sel
  4414.  ─────────────────────────────────────────────────────────────────────────────
  4415.                                       available
  4416.                                        with a
  4417.                                        coprocessor
  4418.  
  4419.  /FPc                   Calls          Slower than    Uses coprocessor if  mLI
  4420.  Calls to                              /FPi, but      present(1)
  4421.  Emulator                              allows use of
  4422.                                        alternate
  4423.                                        math library
  4424.                                        at link time
  4425.  
  4426.  /FPc87                 Calls          Slower than    Requires             mLI
  4427.  Calls to Math                         /FPi87, but    coprocessor unless
  4428.  Coprocessor                           allows use of  library changed at
  4429.                                        alternate      link time(5)
  4430.                                        math library
  4431.                                        at link time
  4432.  
  4433.  Option for CL for PWB  Combined Use                                       Lib
  4434.                         of Method      Effect         Coprocessor          Sel
  4435.  ─────────────────────────────────────────────────────────────────────────────
  4436. 
  4437.  /FPa                   Calls          Fastest and    Ignores              mLI
  4438.  Alternate Math                        smallest       coprocessor
  4439.                                        option
  4440.                                        available
  4441.                                        without a
  4442.                                        coprocessor,
  4443.                                        but
  4444.                                        sacrifices
  4445.                                        some accuracy
  4446.                                        for speed
  4447.  
  4448.  ─────────────────────────────────────────────────────────────────────────────
  4449.  
  4450.  
  4451.  
  4452.  (1)  Use of the coprocessor can be suppressed by setting NO87.
  4453.  (2)  Can be linked explicitly with mLIBC7.LIB at link time.
  4454.  (3)  Can be linked explicitly with mLIBCA.LIB at link time.
  4455.  (4)  Can be linked explicitly with mLIBCE.LIB at link time.
  4456.  (5)  Use of the coprocessor can be suppressed by setting NO87 if you change
  4457.  to the emulator library at link time.
  4458.  
  4459.  
  4460.  Optimizations such as constant propagation and constant subexpression
  4461.  elimination can cause some expressions to be evaluated at compile time. Such
  4462.  evaluations always use IEEE format and are unaffected by the floating-point
  4463.  option you choose. For more information about optimizing, see Chapter 1,
  4464.  "Optimizing C Programs."
  4465.  
  4466.   You can specify floatingpoint options in the Programmer's WorkBench.
  4467.  
  4468.  To specify floating-point options when using the Programmer's WorkBench, you
  4469.  must modify the C Global Build Options (available on the Options menu). In
  4470.  the C Global Build Options dialog box, select one of the following
  4471.  floating-point math options:
  4472.  
  4473.  Option                            Effect
  4474.  ────────────────────────────────────────────────────────────────────────────
  4475.  Emulation Calls                   Generates calls; makes emulator math
  4476.                                    library the
  4477.                                    default (/FPc)
  4478.  
  4479.  80x87 Calls                       Generates calls; makes math coprocessor
  4480.                                    library the default (/FPc87)
  4481.  
  4482.  Fast Alternate Math               Generates calls; makes alternate math
  4483.                                    library the
  4484.                                    default (/FPa)
  4485.  
  4486.  Inline Emulation                  Generates in-line instructions; makes
  4487.                                    emulator math library the default
  4488.                                    (/FPi); this is the default option
  4489.  
  4490.  Inline 80x87                      Generates in-line instructions; selects
  4491.  Instructions                      math coprocessor library (/FPi87)
  4492.  
  4493.  
  4494.  4.4.1  In-Line Emulator Option (/FPi)
  4495.  
  4496.  The in-line emulator option (/FPi) generates in-line instructions for an
  4497.  80x87 coprocessor and places the name of the emulator library (mLIBCE.LIB)
  4498.  in the object file. At link time, you can specify the math coprocessor
  4499.  library (mLIBC7.LIB) instead. If you do not choose a floating-point option,
  4500.  the compiler uses the in-line emulator option by default.
  4501.  
  4502.  The in-line emulator option is useful if you cannot be sure that an 80x87
  4503.  coprocessor will be available on the target computer. Programs compiled
  4504.  using the in-line emulator option work as described below:
  4505.  
  4506.  
  4507.    ■   If a coprocessor is present at run time, the program uses the
  4508.        coprocessor.
  4509.  
  4510.    ■   If no coprocessor is present, the program uses the emulator. In this
  4511.        case, the in-line emulator option offers the most efficient way to get
  4512.        maximum precision in floating-point results.
  4513.  
  4514.  
  4515.  When you use the in-line emulator option, the compiler does not generate
  4516.  in-line 80x87 instructions. For real-mode code, the compiler generates
  4517.  software interrupts to library code, which then fixes up the interrupts to
  4518.  use either the emulator or the coprocessor, depending on whether a
  4519.  coprocessor is present. For protected-mode code, the compiler generates no
  4520.  such interrupts; it generates 80x87 instructions. If the target computer
  4521.  does not have a coprocessor, an "unsupported extension" exception occurs,
  4522.  which is vectored to library code. If you want true in-line 80x87
  4523.  instructions, use the in-line math coprocessor option (/FPi87).
  4524.  
  4525.  ────────────────────────────────────────────────────────────────────────────
  4526.  NOTE
  4527.  
  4528.  In an OS/2 dynamic-link library built with LLIBCDLL.LIB, you cannot use code
  4529.  that requires the emulator library. You must use the alternate math library
  4530.  instead.
  4531.  ────────────────────────────────────────────────────────────────────────────
  4532.  
  4533.  
  4534.  4.4.2  In-Line Math Coprocessor Instructions Option (/FPi87)
  4535.  
  4536.  The in-line math coprocessor instructions option (/FPi87) instructs the
  4537.  compiler to place 80x87 coprocessor instructions in your code for many math
  4538.  operations. It also causes the name of a math coprocessor library
  4539.  (mLIBC7.LIB) to be embedded in the object file.
  4540.  
  4541.  If you use the in-line math coprocessor instructions option and link with
  4542.  the library mLIBC7.LIB, an 80x87 coprocessor must be present at run time, or
  4543.  the program fails and the following error message is displayed:
  4544.  
  4545.    run-time error R6002
  4546.    - floating point not loaded
  4547.  
  4548.  Compiling with the in-line math coprocessor instructions option results in
  4549.  the smallest, fastest programs possible for handling floating-point results.
  4550.  
  4551.  
  4552.  
  4553.  4.4.3  Calls to Emulator Option (/FPc)
  4554.  
  4555.  The calls to emulator option (/FPc) generates floating-point calls to the
  4556.  emulator library and places the names of an emulator library (mLIBCE.LIB) in
  4557.  the object file. At link time, you can specify a math coprocessor library
  4558.  (mLIBC7.LIB) or an alternate math library (mLIBCA.LIB) instead. Thus, the
  4559.  calls to emulator option gives you more flexibility in the libraries you can
  4560.  use for linking than the in-line emulator option.
  4561.  
  4562.  Using the calls to emulator option is also recommended in the following
  4563.  cases:
  4564.  
  4565.  
  4566.    ■   If you compile modules that perform floating-point operations and plan
  4567.        to include these modules in a library
  4568.  
  4569.    ■   If you compile modules that you want to link with libraries other than
  4570.        the libraries provided with Microsoft C
  4571.  
  4572.  
  4573.  You cannot link with an alternate math library if your program uses the
  4574.  intrinsic forms of floating-point library routines (that is, if you have
  4575.  compiled the program with the /Oi or /Ox option, selected the Generate
  4576.  Intrinsic Functions option from the Debug Build Options or Release Build
  4577.  Options dialog box in the Programmer's WorkBench, or specified math
  4578.  functions in an intrinsic pragma).
  4579.  
  4580.  
  4581.  4.4.4  Calls to Math Coprocessor Option (/FPc87)
  4582.  
  4583.  The calls to math coprocessor option (/FPc87) generates function calls to
  4584.  routines in the math coprocessor library (mLIBC7.LIB) that issue the
  4585.  corresponding 80x87 instructions. As with the in-line math coprocessor
  4586.  instructions option (/FPi87), at link time you can choose to link with an
  4587.  emulator library (mLIBCE.LIB). However, /FPc offers more flexibility in
  4588.  choosing libraries, since you can change your mind and link with the
  4589.  appropriate alternate math library as well (mLIBCA.LIB).
  4590.  
  4591.  The disadvantages of using the calls to math coprocessor option as opposed
  4592.  to the in-line coprocessor option are the following:
  4593.  
  4594.  
  4595.    ■   Your executable size is larger because a call requires more
  4596.        instructions than a true coprocessor instruction.
  4597.  
  4598.    ■   Your program does not execute as fast because you must issue a
  4599.        function call for each floating-point operation.
  4600.  
  4601.  
  4602.  You cannot link with an alternate math library if your program uses the
  4603.  intrinsic forms of floating-point library routines (that is, if you have
  4604.  compiled the program with the /Oi or /Ox option, selected the Generate
  4605.  Intrinsic Functions option from the Debug Build Options or Release Build
  4606.  Options dialog box in the Programmer's WorkBench, or specified math
  4607.  functions in an intrinsic pragma).
  4608.  
  4609.  You must have a math coprocessor installed to run programs compiled with the
  4610.  /FPc option and linked with a math coprocessor library. Otherwise, the
  4611.  program fails and the following error message is displayed:
  4612.  
  4613.    run-time error R6002
  4614.    - floating point not loaded
  4615.  
  4616.  ────────────────────────────────────────────────────────────────────────────
  4617.  NOTE
  4618.  
  4619.  Certain optimizations are not performed when you use the calls to math
  4620.  coprocessor option. This can reduce the efficiency of your code; also, since
  4621.  arithmetic of different precision can result, there may be slight
  4622.  differences in your results.
  4623.  ────────────────────────────────────────────────────────────────────────────
  4624.  
  4625.  
  4626.  4.4.5  Use Alternate Math Option (/FPa)
  4627.  
  4628.  The use alternate math option (/FPa) generates floating-point calls and
  4629.  selects the alternate math library for the appropriate memory model
  4630.  (mLIBCA.LIB). Calls to this library provide the fastest and smallest option
  4631.  for code intended to run on a machine without an 80x87 coprocessor. With
  4632.  this option, you can choose an emulator library (mLIBCE.LIB) or a math
  4633.  coprocessor library (mLIBC7.LIB) at link time.
  4634.  
  4635.  You cannot link with an alternate math library if your program uses the
  4636.  intrinsic forms of floating-point library routines (that is, if you have
  4637.  compiled the program with the /Oi or /Ox option, selected the Generate
  4638.  Intrinsic Functions from the Debug Build Options or Release Build Options
  4639.  dialog box in the Programmer's WorkBench, or specified math functions in an
  4640.  intrinsic pragma).
  4641.  
  4642.  
  4643.  4.5  Library Considerations for Floating-Point Options
  4644.  
  4645.  You may want to use libraries in addition to the default library for the
  4646.  floating-point option you have chosen in your compile options. For example,
  4647.  you may want to create your own libraries (or other collections of
  4648.  subprograms in object-file form), then link these libraries at a later time
  4649.  with object files that you have compiled using different options.
  4650.  
  4651.  The following sections describe these cases and ways to handle them.
  4652.  Although the discussion assumes that you are putting your object files into
  4653.  libraries, the same considerations apply if you are simply using individual
  4654.  object files.
  4655.  
  4656.  
  4657.  4.5.1  Using One Standard Library for Linking
  4658.  
  4659.  You must use only one standard C run-time library when you link. You can
  4660.  control which library is used in one of two ways:
  4661.  
  4662.  
  4663.    1.  In the Programmer's WorkBench, add the name of the C run-time library
  4664.        file you want to the program list using the Edit Program List option
  4665.        from the Make menu. You must also modify the Linker Options (from the
  4666.        Make menu) by specifying No Default Library Search.
  4667.  
  4668.    2.  From the LINK command line, give the /NODEFAULTLIBRARYSEARCH (/NOD)
  4669.        option and then specify the name of the combined library file you want
  4670.        to use in the link-libinfo field of the CL command line. This
  4671.        overrides the library names embedded in the object files.
  4672.  
  4673.  
  4674.  
  4675.  4.5.2  In-Line Instructions or Calls
  4676.  
  4677.  When deciding on a floating-point option, you should decide whether you want
  4678.  to use in-line instructions. If you do, compile with the in-line math
  4679.  coprocessor instructions (/FPi87) or in-line emulator (/FPi) option.
  4680.  Otherwise, compile for floating-point function calls using the calls to math
  4681.  coprocessor (/FPc87), calls to emulator (/FPc), or alternate math (/FPa)
  4682.  option.
  4683.  
  4684.  If you choose to use in-line instructions for your precompiled object files,
  4685.  you cannot link with an alternate math library (mLIBCA.LIB). However,
  4686.  in-line instructions achieve the best performance from your programs on
  4687.  machines that have an 80x87 coprocessor installed.
  4688.  
  4689.  If you choose to use calls, your programs are slower, but at link time you
  4690.  can switch to any standard C run-time library (that is, any library created
  4691.  by the SETUP program) that supports the memory model you have chosen.
  4692.  
  4693.  
  4694.  4.6  Compatibility between Floating-Point Options
  4695.  
  4696.  Each time you compile a source file, you can specify a floating-point
  4697.  option. When you link two or more source files to produce an executable
  4698.  program file, you must ensure that floating-point operations are handled
  4699.  consistently and that the environment is set up properly to allow the linker
  4700.  to find the required library.
  4701.  
  4702.  If you are building libraries of C routines that contain floating-point
  4703.  operations, the calls to emulator option (/FPc) provides the most
  4704.  flexibility.
  4705.  
  4706.  The examples that follow illustrate how you can link your program with a
  4707.  library other than the default. The floating-point option and the substitute
  4708.  library are compatible.
  4709.  
  4710.  The example below compiles the program  CALC.C  with the medium-model
  4711.  option (/AM). Because no floating-point option is specified, the default
  4712.  in-line emulator option (/FPi) is used. The in-line emulator option
  4713.  generates 80x87 instructions and specifies the emulator library MLIBCE.LIB
  4714.  in the object file. The /LINK field specifies the /NODEFAULTLIBRARYSEARCH
  4715.  (/NOD) option and the names of the medium-model math coprocessor library.
  4716.  Specifying the math coprocessor library forces the program to use an 80x87
  4717.  coprocessor; the program fails if a coprocessor is not present.
  4718.  
  4719.    CL /AM CALC.C /link MLIBC7 /NOD
  4720.  
  4721.  The example below compiles  CALC.C  using the small (default) memory model
  4722.  and the alternate math option (/FPa). The /LINK field specifies the /NOD
  4723.  option and the library SLIBCE.LIB. Specifying the emulator library causes
  4724.  all floating-point calls to refer to the emulator library instead of the
  4725.  alternate math library.
  4726.  
  4727.    CL /FPa CALC.C /link SLIBCE /NOD
  4728.  
  4729.  The example below compiles  CALC.C  with the calls to math coprocessor
  4730.  option (/FPc87), which places the library name SLIBC7.LIB in the object
  4731.  file. The /LINK field overrides this default-library specification by giving
  4732.  the /NOD option and the name of the small-model alternate math library
  4733.  (SLIBCA.LIB).
  4734.  
  4735.    CL /FPc87 CALC.C /link SLIBCA.LIB/NOD
  4736.  
  4737.  
  4738.  4.7  Using the NO87 Environment Variable
  4739.  
  4740.  Programs compiled using either the calls to emulator (/FPc) or the in-line
  4741.  emulator (/FPi) option automatically use an 80x87 coprocessor at run time if
  4742.  one is installed. You can override this and force the use of the software
  4743.  emulator by setting an environment variable named NO87.
  4744.  
  4745.   Use the NO87 environment variable to suppress use of the 80x87 coprocessor
  4746.  at run time.
  4747.  
  4748.  If NO87 is set to any value when the program is executed, use of the
  4749.  coprocessor is suppressed. The value of the NO87 setting is printed on the
  4750.  standard output as a message. The message is printed only if a coprocessor
  4751.  is present and suppressed; if no coprocessor is present, no message appears.
  4752.  If you don't want a message to be printed, set NO87 equal to one or more
  4753.  spaces. A blank string for NO87 causes a blank line to be printed.
  4754.  
  4755.  Note that only the presence or absence of the NO87 definition is important
  4756.  in suppressing use of the coprocessor. The actual value of the NO87 setting
  4757.  is used only for printing the message.
  4758.  
  4759.  The NO87 variable takes effect with any program linked with an emulator
  4760.  library (mLIBCE.LIB). It has no effect on programs linked with math
  4761.  coprocessor libraries (mLIBC7.LIB) or programs linked with alternate math
  4762.  libraries (mLIBCA.LIB).
  4763.  
  4764.  When a program that uses an emulator library is executed and an 80x87
  4765.  coprocessor is present, the example below causes the message  Use of
  4766.  coprocessor suppressed  to appear.
  4767.  
  4768.    SET NO87=Use of coprocessor suppressed
  4769.  
  4770.  The syntax below sets the NO87 variable to the space character. Use of the
  4771.  coprocessor is still suppressed, but no message is displayed.
  4772.  
  4773.    SET NO87=space
  4774.  
  4775.  
  4776.  4.8  Incompatibility Issues
  4777.  
  4778.  The exception handler in the libraries for 80x87 floating-point calculations
  4779.  (mLIBCE.LIB and mLIBC7.LIB) is designed to work without modification on the
  4780.  IBM PC family of computers and on closely compatible computers, including
  4781.  the WANG(R) PC, the AT&T(R) 6300, and the Olivetti(R) personal computers.
  4782.  Also, the libraries need not be modified for the Texas Instruments(R)
  4783.  Professional Computer, even though it is not compatible. Any machine that
  4784.  uses nonmaskable interrupts (NMI) for 80x87 exceptions will run with the
  4785.  unmodified libraries. If your computer is not one of these, and if you are
  4786.  not sure whether it is completely compatible, you may need to modify the
  4787.  math coprocessor libraries.
  4788.  
  4789.  All Microsoft languages that support 80x87 coprocessors intercept 80x87
  4790.  exceptions in order to produce accurate results and properly detect error
  4791.  conditions. To make the libraries work correctly on incompatible machines,
  4792.  you can modify the libraries. To make this easier, an assembly-language
  4793.  source file, EMOEM.ASM, is included on the C 6.0 distribution disk. Any
  4794.  machine that sends the 80x87 exception to an 8259 Priority Interrupt
  4795.  Controller (master or master/slave) can be supported by a simple table
  4796.  change to the EMOEM.ASM module. The source file contains further
  4797.  instructions about how to modify EMOEM.ASM, patch libraries, and executable
  4798.  files.
  4799.  
  4800.  
  4801.  
  4802.  
  4803.  
  4804.  
  4805.  PART II  Improving Programmer Productivity
  4806.  ────────────────────────────────────────────────────────────────────────────
  4807.  
  4808.   The Microsoft C Professional Development System helps you write and debug
  4809.  software rapidly.
  4810.  
  4811.  Chapter 5 describes the quick compile and incremental compile options, both
  4812.  of which can save you time when compiling programs. Chapter 5 also describes
  4813.  the incremental linker, ILINK, which can save you time when you link your
  4814.  application. Chapter 6 describes NMAKE, a powerful new program maintenance
  4815.  utility that automates your program build process. Chapter 7 describes how
  4816.  to build help files with HELPMAKE, the help-file maintenance utility. When
  4817.  you need to share documentation in a readily accessible form, you can add it
  4818.  to the Microsoft Advisor on-line help system using the information in
  4819.  Chapter 7. Chapter 8 explains how to customize the Programmer's WorkBench to
  4820.  make it a personalized development platform. Chapter 9 offers procedures
  4821.  (and some tips) for using the CodeView debugger to find errors in your
  4822.  programs.
  4823.  
  4824.  
  4825.  
  4826.  
  4827.  
  4828.  
  4829.  Chapter 5  Compiling and Linking Quickly
  4830.  ────────────────────────────────────────────────────────────────────────────
  4831.  
  4832.  The fundamental processes of compiling and linking take time to perform. The
  4833.  larger your application grows, the longer it takes to compile and link.
  4834.  
  4835.  This chapter describes how you can speed up compiling by using the quick
  4836.  compiler and incremental compile option, and how you can speed up linking by
  4837.  using ILINK, the Incremental Linker.
  4838.  
  4839.  
  4840.  5.1  Compiling Quickly
  4841.  
  4842.  This section describes two ways to speed up the compiling process: using the
  4843.  quick compiler and using the incremental compile option.
  4844.  
  4845.  
  4846.  5.1.1  Quick Compiler
  4847.  
  4848.  The Microsoft C Professional Development System includes two separate C
  4849.  compilers: the full compiler and the quick compiler. If you don't specify
  4850.  otherwise, your program is compiled by the full compiler.
  4851.  
  4852.  You access the quick compiler by specifying the /qc command-line option for
  4853.  CL or by selecting the Quick Compile option from the C Release Build or C
  4854.  Debug Build Options dialogs in the PWB Options menu.
  4855.  
  4856.  The quick compiler cannot perform as many optimizations as the full
  4857.  compiler, but it is much faster. You can use it to save time during
  4858.  development, whenever optimizations are not critical. When your application
  4859.  is finished, you can compile with the full compiler, using all the desired
  4860.  optimizations.
  4861.  
  4862.  On-line help for the /qc option describes which optimizations the quick
  4863.  compiler can perform.
  4864.  
  4865.  
  4866.  5.1.2  Incremental Compile Option
  4867.  
  4868.  You can speed up compiling even more by compiling incrementally. Incremental
  4869.  compilation means that the compiler compiles only those functions that have
  4870.  changed since you last compiled.
  4871.  
  4872.  The incremental compile option is available only with the quick compiler
  4873.  (see the previous section). You can access it from within PWB or from the
  4874.  DOS command line. Within PWB, select the Incremental Compile option in the C
  4875.  Release Build dialog box or in the C Debug Build Options dialog box. From
  4876.  the DOS command line, specify the /Gi option for CL.
  4877.  
  4878.  The incremental compile option automatically triggers another time-saving
  4879.  feature: the Incremental Linker, which is described in the next section.
  4880.  
  4881.  
  4882.  5.2  Linking Quickly with ILINK
  4883.  
  4884.   ILINK links only those modules that have changed since the last link.
  4885.  
  4886.  The Incremental Linker (ILINK) offers the same advantage in linking that the
  4887.  incremental compile option offers in compiling. Rather than link every
  4888.  module in an application, as LINK does, ILINK links only those modules that
  4889.  have changed since the last link. The more modules your application
  4890.  contains, the more time ILINK can potentially save.
  4891.  
  4892.  In a normal development scenario, you use LINK at the beginning and end of
  4893.  the process, and use ILINK in the middle. In the early stages of
  4894.  development, when your application contains only a few modules, ILINK offers
  4895.  no speed advantage over LINK. Once your application contains several
  4896.  modules, you can save time by using ILINK.
  4897.  
  4898.   You must link once with LINK to prepare for incremental linking.
  4899.  
  4900.  To prepare for incremental linking, you must run LINK using /INCREMENTAL, as
  4901.  described in Section 5.2.1. At the same time, you have the option of adding
  4902.  padding bytes to code or data segments by specifying the /PADCODE and
  4903.  /PADDATA options. Padding allows ILINK to expand a segment without relinking
  4904.  the entire module in which it is contained.
  4905.  
  4906.  Now you can link with ILINK during the rest of development. If changes in
  4907.  your code require a full link, ILINK invokes LINK automatically. When the
  4908.  application is finished, you link a last time with LINK to produce the final
  4909.  executable file.
  4910.  
  4911.  You can use ILINK with programs compiled for any memory model except tiny
  4912.  model. (Memory models are described in Chapter 2, "Managing Memory.")
  4913.  Typically, ILINK is not efficient for small- or compact-model programs
  4914.  unless they were compiled with the incremental compile option, which is
  4915.  described in Section 5.1.2.
  4916.  
  4917.  
  4918.  5.2.1  Preparing for Incremental Linking
  4919.  
  4920.  There are three LINK options that relate to the use of ILINK. One of them
  4921.  (/INCREMENTAL) is mandatory; the other two (/PADCODE and /PADDATA) are
  4922.  optional. This section explains the LINK options that prepare for ILINK. See
  4923.  on-line help for a complete list of LINK options.
  4924.  
  4925.  
  4926.  The /INCREMENTAL Option
  4927.  
  4928.  The /INCREMENTAL (/INC) option prepares an object file for incremental
  4929.  linking. You must always run LINK using this option before using ILINK. When
  4930.  you specify /INC, the linker produces two extra files: a symbol file (.SYM)
  4931.  and an ILINK support file (.ILK). The .SYM and .ILK files tell ILINK which
  4932.  parts of the executable file need to be updated.
  4933.  
  4934.  You must use /INCREMENTAL whenever you use the /PADCODE and /PADDATA
  4935.  options, which are described below.
  4936.  
  4937.  
  4938.  The /PADCODE Option
  4939.  
  4940.  The /PADCODE option causes LINK to add padding bytes at the end of a
  4941.  module's code segment. The padding bytes leave room for the code segment to
  4942.  grow in subsequent links, allowing ILINK to update only that module. You can
  4943.  use the /PADCODE option only when /INC is also specified.
  4944.  
  4945.  Code padding is usually necessary for programs using the small memory model.
  4946.  It is also recommended for compact- or mixed-model programs. You do not need
  4947.  to specify /PADCODE for other memory models (medium, large, or huge).
  4948.  
  4949.  If you don't specify /PADCODE, LINK doesn't pad the code segment at all. To
  4950.  add padding, specify the desired number of bytes. The optimum amount of
  4951.  padding depends on how much your code changes from one link to the next. If
  4952.  you expect to add only a little code, choose a relatively small amount of
  4953.  padding, say 32 to 64 bytes. If ILINK issues the message
  4954.  
  4955.    padding exceeded
  4956.  
  4957.  and performs a full link more often than desired, increase the padding by a
  4958.  small amount, say 32 bytes. In any case, remember that the total size of a
  4959.  code segment, including padding bytes, cannot exceed 64K (65,535) bytes.
  4960.  
  4961.  
  4962.  The /PADDATA Option
  4963.  
  4964.  Like /PADCODE, the /PADDATA option causes LINK to add padding bytes that
  4965.  leave room for the segment to grow in subsequent links. However, the
  4966.  /PADDATA option pads the end of the data segment rather than the code
  4967.  segment. You can use /PADDATA only when /INC is also specified.
  4968.  
  4969.  If you don't specify /PADDATA, LINK adds 16 bytes of padding by default. The
  4970.  default padding amount should suffice in many cases, since public variables
  4971.  are added less frequently than code. If you need more padding, specify the
  4972.  desired number of bytes. Remember that the total size of a data segment,
  4973.  including padding bytes, cannot exceed 64K (65,535) bytes.
  4974.  
  4975.  
  4976.  5.2.2  Incremental Violations
  4977.  
  4978.  ILINK can generate two kinds of errors: real errors and incremental
  4979.  violations. Real errors are errors such as undefined symbols that cannot be
  4980.  resolved by a full link. If ILINK detects a real error, it displays an error
  4981.  message (real errors are documented in on-line help).
  4982.  
  4983.  Incremental violations are caused by code changes you have made that go
  4984.  beyond the scope of incremental linking. When an incremental violation
  4985.  occurs, ILINK invokes LINK automatically. The following sections describe
  4986.  the incremental violations.
  4987.  
  4988.  
  4989.  Changing Libraries
  4990.  
  4991.  An incremental violation occurs when a library changes. Furthermore, if an
  4992.  altered module shares a code segment with a library, ILINK needs access to
  4993.  the library as well as to the altered module.
  4994.  
  4995.  If you add a function, procedure, or subroutine call to a library that has
  4996.  never been called before, ILINK invokes LINK automatically.
  4997.  
  4998.  
  4999.  Exceeding Code/Data Padding
  5000.  
  5001.  An incremental violation occurs if two or more modules contribute to the
  5002.  same physical segment and either module exceeds its padding. The padding
  5003.  allows the module to increase the specified number of bytes before another
  5004.  full link is required.
  5005.  
  5006.  
  5007.  Moving or Deleting Data Symbols
  5008.  
  5009.  An incremental violation occurs if a data symbol is moved or deleted. To add
  5010.  new data symbols without requiring a full link, add the new symbols at the
  5011.  end of all other data symbols in the module.
  5012.  
  5013.  
  5014.  Deleting Code Symbols
  5015.  
  5016.  You can move or add code symbols, but an incremental violation occurs if you
  5017.  delete any code symbols from a module. Code symbols can be moved within a
  5018.  module but cannot be moved between modules.
  5019.  
  5020.  
  5021.  Changing Segment Definitions
  5022.  
  5023.  An incremental violation results if you add, delete, or change the order of
  5024.  segment definitions.
  5025.  
  5026.  
  5027.  Adding CodeView(R) Debugger Information
  5028.  
  5029.  If you include CodeView debugger information for a module when you fully
  5030.  link (by compiling and linking with CodeView debugger support), ILINK
  5031.  supports CodeView debugger information for the module. ILINK maintains
  5032.  symbolic information for current symbols, and it adds information for any
  5033.  new symbols. However, if you try to add CodeView debugger information for a
  5034.  module that did not previously have CodeView debugger support, an
  5035.  incremental violation occurs. See Chapter 9, "Debugging C Programs with
  5036.  CodeView," for more information about CodeView.
  5037.  
  5038.  
  5039.  
  5040.  
  5041.  
  5042.  
  5043.  Chapter 6  Managing Development Projects with NMAKE
  5044.  ────────────────────────────────────────────────────────────────────────────
  5045.  
  5046.  The Microsoft Program-Maintenance Utility (NMAKE) is a sophisticated command
  5047.  processor that can save time and simplify project management. By determining
  5048.  which project files depend on others, NMAKE can automatically execute the
  5049.  commands needed to update your project when any project file has changed.
  5050.  
  5051.  The advantage of using NMAKE over simple batch files is that NMAKE does only
  5052.  what is needed. You don't waste time rebuilding files that are already
  5053.  up-to-date. NMAKE also has advanced features, such as macros, that help you
  5054.  manage complex projects.
  5055.  
  5056.  This chapter provides complete documentation for NMAKE. Information about
  5057.  NMAKE is also available in on-line help. If you are familiar with MAKE, the
  5058.  predecessor of NMAKE, be sure to read Section 6.9, "Differences Between
  5059.  NMAKE and MAKE." There are some important differences between the two
  5060.  utilities.
  5061.  
  5062.  
  5063.  6.1  Overview of NMAKE
  5064.  
  5065.  NMAKE works by comparing the times and dates of two sets of files, which are
  5066.  called "targets" and "dependents." A target is normally a file that you want
  5067.  to create, such as an executable file. A dependent is a file used to create
  5068.  a target, such as a C source file.
  5069.  
  5070.  When you run NMAKE, it reads a "description file" that you supply. The
  5071.  description file consists of one or more blocks. Each block typically lists
  5072.  a target, the target's dependents, and the command that builds the target.
  5073.  NMAKE compares the date and time of the target to those of its dependents.
  5074.  If any dependent has changed more recently than the target, NMAKE updates
  5075.  the target by executing the command listed in the block.
  5076.  
  5077.  NMAKE's main purpose is to help you update applications quickly and simply.
  5078.  However, it can execute any command, so it is not limited to compiling and
  5079.  linking. NMAKE can also make backups, move files, and do many other project
  5080.  management tasks.
  5081.  
  5082.  
  5083.  6.2  The NMAKE Command
  5084.  
  5085.  When you run NMAKE, you can supply the description-file name and other
  5086.  arguments using the following syntax:
  5087.  
  5088.  NMAKE «options» «macros» «targets» «descriptfile»
  5089.  
  5090.  All of the command-line fields are optional. If you don't supply any
  5091.  arguments, NMAKE looks for a default description file named MAKEFILE and
  5092.  follows various other defaults that are described in this chapter.
  5093.  
  5094.  The options field lists NMAKE options, which are described in Section 6.4,
  5095.  "Command-Line Options."
  5096.  
  5097.  The macros field lists macro definitions, which allow you to replace text in
  5098.  the description file. Macros are described in Section 6.3.3.
  5099.  
  5100.  The targets field lists targets to build. If you do not list any targets,
  5101.  NMAKE builds only the first target in the description file. (This is a
  5102.  significant departure from the behavior of MAKE, NMAKE's predecessor. See
  5103.  Section 6.9, "Differences between NMAKE and MAKE.")
  5104.  
  5105.  The descriptfile field specifies a description file. If this field is
  5106.  absent, NMAKE automatically looks for a file named MAKEFILE in the current
  5107.  directory. You can also specify the description file with the /F option (for
  5108.  information, see Section 6.4, "Command-Line Options").
  5109.  
  5110.  Below is a typical NMAKE command:
  5111.  
  5112.    NMAKE /S "program = sample" sort.exe search.exe
  5113.  
  5114.  The command supplies four arguments: an option (/S), a macro definition
  5115.  ("program = sample"), and two target specifications (sort.exe search.exe).
  5116.  
  5117.  Because the command does not specify a description file, NMAKE looks for the
  5118.  default description file, MAKEFILE. The /S option tells NMAKE to suppress
  5119.  the display of commands as they are executed. The macro definition performs
  5120.  a text substitution throughout the description file, replacing every
  5121.  instance of  program  with  sample. The target specifications tell NMAKE to
  5122.  update the targets SORT.EXE and SEARCH.EXE.
  5123.  
  5124.  
  5125.  6.3  NMAKE Description Files
  5126.  
  5127.  You must always supply NMAKE with a description file. In addition to
  5128.  description blocks, which tell NMAKE how to build your project's target
  5129.  files, the description file can contain comments, macros, inference rules,
  5130.  and directives. This section describes all the elements of description
  5131.  files.
  5132.  
  5133.  
  5134.  6.3.1  Description Blocks
  5135.  
  5136.  Description blocks form the heart of the description file. Figure 6.1
  5137.  illustrates a typical NMAKE description block, including the three parts:
  5138.  targets, dependents, and commands.
  5139.  
  5140.  (This figure may be found in the printed book.)
  5141.  
  5142.   A target is a file that you want to build.
  5143.  
  5144.  The targets part of the description block lists one or more files to build.
  5145.  The line that lists targets and dependents is called the "dependency line."
  5146.  
  5147.  
  5148.  The example in Figure 6.1 tells NMAKE to build a single target, MYAPP.EXE.
  5149.  Although single targets are common, you can also list multiple targets;
  5150.  separate each target name with a space. If the rightmost target name is one
  5151.  character long, put a space between the name and the colon.
  5152.  
  5153.  The target is normally a file, but it can also be a "pseudotarget," a name
  5154.  that allows you to build groups of files or execute a group of commands. See
  5155.  Section 6.3.6, "Pseudotargets."
  5156.  
  5157.   A dependent is a file used to build a target.
  5158.  
  5159.  The dependents part of the description block lists one or more files from
  5160.  which the target is built. It is separated from the targets part by a colon.
  5161.  The example in Figure 6.1 lists three dependents:
  5162.  
  5163.    myapp.exe : myapp.obj another.obj myapp.def
  5164.  
  5165.  The example tells NMAKE to build the target MYAPP.EXE whenever MYAPP.OBJ,
  5166.  ANOTHER.OBJ, or MYAPP.DEF has changed more recently than MYAPP.EXE.
  5167.  
  5168.  If any dependents of a target are listed as targets in other description
  5169.  blocks, then NMAKE builds those files before it builds the original target.
  5170.  Essentially NMAKE evaluates a "dependency tree" for the entire description
  5171.  file. It builds files in the order needed to update the original target,
  5172.  never building a target until all files that depend on it are up-to-date.
  5173.  
  5174.  The dependent list can also include a list of directories in which NMAKE
  5175.  should search for dependents. The directory list is enclosed in curly braces
  5176.  ( {} ) and precedes the dependent list. NMAKE searches the current directory
  5177.  first, then the directories you list:
  5178.  
  5179.    forward.exe : {\src\alpha;d:\proj}pass.obj
  5180.  
  5181.  In the line above, the target, FORWARD.EXE, has one dependent: PASS.OBJ. The
  5182.  directory list specifies two directories:
  5183.  
  5184.    {\src\alpha;d:\proj}
  5185.  
  5186.  NMAKE begins searching for PASS.OBJ in the current directory. If it is not
  5187.  found, NMAKE searches the \ SRC \ ALPHA directory, then the D:\ PROJ
  5188.  directory. If NMAKE cannot find a dependent in the current directory or a
  5189.  listed directory, it looks for an inference rule that describes how to
  5190.  create the dependent (see Section 6.3.4, "Inference Rules").
  5191.  
  5192.   The commands part of a  description block can contain  one or more
  5193.  commands.
  5194.  
  5195.  The commands part of the description block lists the command(s) NMAKE should
  5196.  use to build the target. This can be any command that you can execute from
  5197.  the command line. The example tells NMAKE to build MYAPP.EXE using the
  5198.  following LINK command:
  5199.  
  5200.      LINK myapp another.obj, /align:16, NUL, os2, myapp
  5201.  
  5202.  Notice that the line above is indented. NMAKE uses indentation to
  5203.  distinguish between the dependency line and command line. If the command
  5204.  appears on a separate line, as here, it must be indented at least one space
  5205.  or tab. The dependency line must not be indented (it cannot start with a
  5206.  space or tab).
  5207.  
  5208.  Many targets are built with a single command, but you can place more than
  5209.  one command after the dependency line. A long command can span several lines
  5210.  if each line ends with a backslash ( \ ).
  5211.  
  5212.  You can also place the command at the end of the dependency line. Separate
  5213.  the command from the rightmost dependent with a semicolon.
  5214.  
  5215.  In OS/2 description files, NMAKE imposes a slight restriction on the use of
  5216.  the CD, CHDIR, and SET commands. Do not place any of these commands on a
  5217.  command line that uses the ampersand (&) to execute multiple commands. For
  5218.  instance, the following command line is legal in an OS/2 description file,
  5219.  
  5220.    DIR & COPY sample.c backup.c
  5221.  
  5222.  but this line is not legal because it places a CD command after the
  5223.  ampersand:
  5224.  
  5225.    DIR & CD \mydir
  5226.  
  5227.  To use CD, CHDIR, or SET in a description block, place the command on a
  5228.  separate line:
  5229.  
  5230.    DIR
  5231.    CD \mydir
  5232.  
  5233.  Your OS/2 user's documentation contains more information about using the
  5234.  ampersand in command lines.
  5235.  
  5236.  
  5237.  Wild Cards
  5238.  
  5239.  You can use DOS wild-card characters (* and ?) to specify target and
  5240.  dependent file names. NMAKE expands wild cards in target names when it reads
  5241.  the description file. It expands wild cards in the dependent names when it
  5242.  builds the target. For example, the following description block compiles all
  5243.  source files with the .C extension:
  5244.  
  5245.    bondo.exe : *.c
  5246.        CL *.c
  5247.  
  5248.  
  5249.  Command Modifiers
  5250.  
  5251.  Command modifiers provide extra control over the command listed in a
  5252.  description block. They are special characters that appear in front of a
  5253.  command. You can use more than one modifier for a single command. Table 6.1
  5254.  describes the three NMAKE command modifiers.
  5255.  
  5256.  Table   6.1 Command Modifiers
  5257.  
  5258. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  5259.  Character                         Action
  5260.  ────────────────────────────────────────────────────────────────────────────
  5261.  At sign (@)                       Prevents NMAKE from displaying the
  5262.                                    command as it executes. In the example
  5263.                                    below, NMAKE does not display the ECHO
  5264.                                    command line:
  5265.  
  5266.                                    sort.exe : sort.obj
  5267.                                       @ECHO sorting
  5268.  
  5269.                                    The output of the ECHO command appears
  5270.                                    as usual.
  5271.  
  5272.  Dash (-)                          Turns off error checking for the command.
  5273.  Character                         Action
  5274.  ────────────────────────────────────────────────────────────────────────────
  5275. Dash (-)                          Turns off error checking for the command.
  5276.                                    If the dash is followed by a number,
  5277.                                    NMAKE stops only if the error level
  5278.                                    returned by the command is greater than
  5279.                                    the number. In the following example, if
  5280.                                    the program  sample  returned an error
  5281.                                    code NMAKE does not stop but continues
  5282.                                    to execute commands:
  5283.  
  5284.                                    light.lst : light.txt
  5285.                                       -sample light.txt
  5286.  
  5287.  Exclamation point (!)             Executes the command for each dependent
  5288.                                    file if the command uses the predefined
  5289.                                    macros $? or $**. The $? macro refers to
  5290.                                    all dependent files that are out-of-date
  5291.                                    with respect to the target. The $**
  5292.                                    macro refers to all dependent files in
  5293.                                    the description block (see Section 6.3.3,
  5294.  Character                         Action
  5295.  ────────────────────────────────────────────────────────────────────────────
  5296.                                   the description block (see Section 6.3.3,
  5297.                                    "Macros"). For example,
  5298.  
  5299.                                    print:hop.asm skip.bas jump.c
  5300.                                       !print $** lpt1:
  5301.  
  5302.                                    generates the following commands:
  5303.  
  5304.                                    print hop.asm lpt1:
  5305.                                    print skip.bas lpt1:
  5306.                                    print jump.c lpt1:
  5307.  
  5308.  ────────────────────────────────────────────────────────────────────────────
  5309.  
  5310.  
  5311.  
  5312.  
  5313.  Using Control Characters as Literals
  5314.  
  5315.  Occasionally, you may need to list a file name that contains a character
  5316.  that NMAKE uses as a control character. These characters are
  5317.  
  5318.  # ( ) $ ^ \ { } ! @ -
  5319.  
  5320.  To use an NMAKE control character as a literal character, place a caret (^)
  5321.  in front of it. For example, say that you define a macro that ends with a
  5322.  backslash:
  5323.  
  5324.    exepath=c:\bin\
  5325.  
  5326.  The line above is intended to define a macro named  exepath  with the value
  5327.  c:\bin\. But the second backslash causes unexpected results. Since the
  5328.  back-slash is the NMAKE line-continuation character, the line actually
  5329.  defines the macro  exepath  as  c:\bin  followed by whatever appears on the
  5330.  next line of the description file. You can solve the problem by placing a
  5331.  caret in front of the second backslash:
  5332.  
  5333.    exepath=c:\bin^\
  5334.  
  5335.  You can also use a caret to place a literal newline character in a
  5336.  description file. This feature can be useful in macro definitions:
  5337.  
  5338.    XYZ=abc^
  5339.    def
  5340.  
  5341.  NMAKE interprets the example as if you assigned the C-style string  abc\ndef
  5342.  to the  XYZ  macro. This effect differs from using the backslash ( \s ) to
  5343.  continue a line. A newline character that follows a backslash is replaced
  5344.  with a space.
  5345.  
  5346.  Carets that precede noncontrol characters are ignored. The line
  5347.  
  5348.    ign^ore : these ca^rets
  5349.  
  5350.  is interpreted as
  5351.  
  5352.    ignore : these carets
  5353.  
  5354.  A caret that appears in quotation marks is treated as a literal caret
  5355.  character.
  5356.  
  5357.  
  5358.  Listing a Target in Multiple Description Blocks
  5359.  
  5360.  You can specify more than one description block for the same target by
  5361.  placing two colons (::) after the target. This feature can be useful for
  5362.  building a complex target, such as a library, that contains components
  5363.  created with different commands. For example,
  5364.  
  5365.    target.lib :: a.asm b.asm c.asm
  5366.       CL a.asm b.asm c.asm
  5367.       LIB target -+a.obj -+b.obj -+c.obj;
  5368.    target.lib :: d.c e.c
  5369.       CL /c d.c e.c
  5370.       LIB target -+d.obj -+e.obj;
  5371.  
  5372.  Both description blocks update the library named TARGET.LIB. If any of the
  5373.  assembly-language files have changed more recently than the library, NMAKE
  5374.  executes the commands in the first block to assemble the source files and
  5375.  update
  5376.  
  5377.  the library. Similarly, if any of the C-language files have changed, NMAKE
  5378.  executes the second group of commands, which compile the C files and update
  5379.  the library.
  5380.  
  5381.  If you use a single colon in the example above, NMAKE issues an error
  5382.  message. It is legal, however, to use single colons if commands are listed
  5383.  in only one block. In this case, dependency lines are cumulative. For
  5384.  example,
  5385.  
  5386.    target: jump.bas
  5387.    target: up.c
  5388.       echo Building target...
  5389.  
  5390.  is equivalent to
  5391.  
  5392.    target: jump.bas up.c
  5393.       echo Building target...
  5394.  
  5395.  
  5396.  6.3.2  Comments
  5397.  
  5398.  You can place comments in a description file by preceding them with a number
  5399.  sign (#):
  5400.  
  5401.    # This comment appears on its own line
  5402.    huey.exe : huey.obj dewey.obj # Comment on the same line
  5403.       link huey.obj dewey.obj;
  5404.  
  5405.  A comment extends to the end of the line in which it appears. Command lines
  5406.  cannot contain comments.
  5407.  
  5408.  
  5409.  6.3.3  Macros
  5410.  
  5411.   Macros allow you to do text replacements throughout the description file.
  5412.  
  5413.  Macros offer a convenient way to replace a string in the description file
  5414.  with another string. The text is automatically replaced each time you run
  5415.  NMAKE. Macros are useful in a variety of tasks, including the following:
  5416.  
  5417.  
  5418.    ■   To create a standard description file for several projects. The macro
  5419.        represents the file names used in commands. These file names are then
  5420.        defined when you run NMAKE. When you switch to a different project,
  5421.        you can change file names throughout the description file by changing
  5422.        a single macro.
  5423.  
  5424.    ■   To control the options that NMAKE passes to the compiler or linker.
  5425.        When you specify options in a macro, you can change options throughout
  5426.        the description file in one easy step.
  5427.  
  5428.  
  5429.  You can define your own macros or use predefined macros. This section begins
  5430.  by describing user-defined macros.
  5431.  
  5432.  
  5433.  User-Defined Macros
  5434.  
  5435.  You can define a macro with
  5436.  
  5437.    macroname = string
  5438.  
  5439.  The macroname can be any combination of letters, digits, and the underscore
  5440.  ( _ ) character. Macro names are case sensitive. NMAKE interprets  MyMacro
  5441.  and  MYMACRO  as different macro names.
  5442.  
  5443.  The string can be any string, including a null string. For example,
  5444.  
  5445.    command = LINK
  5446.  
  5447.  defines a macro named  command  and assigns it the string  LINK.
  5448.  
  5449.  You can define macros in the description file or on the command line. In the
  5450.  description file, you must define each macro on a separate line; the line
  5451.  cannot start with a space or tab. The string can contain embedded spaces,
  5452.  and NMAKE ignores spaces on either side of the equal sign. You do not need
  5453.  to enclose string in quotation marks (if you do, they become part of the
  5454.  string).
  5455.  
  5456.  Slightly different rules apply when you define a macro on the command line,
  5457.  because of the way that the command line handles spaces. You must enclose
  5458.  string in quotation marks if it contains embedded spaces. No spaces can
  5459.  surround the equal sign. You can also enclose the entire macro definition,
  5460.  macroname and string, in quotation marks. For example,
  5461.  
  5462.    NMAKE "program=sample"
  5463.  
  5464.  defines the macro  program, assigning it the value  sample.
  5465.  
  5466.  Once you have defined a macro, you can "undefine" it with the !UNDEF
  5467.  directive (see Section 6.3.5, "Directives").
  5468.  
  5469.  
  5470.  Invoking Macros
  5471.  
  5472.  You invoke a macro by enclosing its name in parentheses preceded by a dollar
  5473.  sign ($). (The parentheses are optional if macroname is one character long.)
  5474.  For example, you can invoke the  command  macro defined above as
  5475.  
  5476.    $(command)
  5477.  
  5478.  When NMAKE runs, it replaces every occurrence of  $(command)  with  LINK.
  5479.  The following description file defines and uses three macros:
  5480.  
  5481.    program = sample
  5482.    c = LINK
  5483.    options =
  5484.  
  5485.    $(program).exe : $(program).obj
  5486.       $c  $(options)  $(program).obj;
  5487.  
  5488.  NMAKE interprets the description block as
  5489.  
  5490.    sample.exe : sample.obj
  5491.       LINK    sample.obj;
  5492.  
  5493.  NMAKE replaces every occurrence of  $(program)  with  sample, every instance
  5494.  of  $c  with  LINK, and every instance of  $(options) with a null string.
  5495.  Because  c  is only one character long, you do not need to enclose it in
  5496.  parentheses.
  5497.  
  5498.  If you invoke a macro that is not defined, NMAKE treats the macro as a null
  5499.  string.
  5500.  
  5501.  Occasionally, you may need to use the dollar sign ($) as a literal
  5502.  character. Use two signs ($$), or precede it with a caret (^$).
  5503.  
  5504.  
  5505.  Predefined Macros
  5506.  
  5507.  NMAKE provides several predefined macros, which represent various file names
  5508.  and commands. Predefined macros are useful in their own right, and they are
  5509.  also employed in predefined inference rules, which are described later in
  5510.  this chapter. Table 6.2 lists NMAKE predefined macros.
  5511.  
  5512.  Table   6.2  Predefined Macros
  5513.  
  5514. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  5515.  Macro                             Meaning
  5516.  ────────────────────────────────────────────────────────────────────────────
  5517.  $@                                The current target's full name.
  5518.  
  5519.  $*                                The current target's base name (full
  5520.                                    name minus the file
  5521.                                    extension).
  5522.  
  5523.  $**                               The dependents of the current target.
  5524.  
  5525.  Macro                             Meaning
  5526.  ────────────────────────────────────────────────────────────────────────────
  5527. 
  5528.  $?                                The dependents that are out-of-date with
  5529.                                    respect to the current target.
  5530.  
  5531.  $$@                               The target that NMAKE is currently
  5532.                                    evaluating. You can only use this macro
  5533.                                    to specify a dependent.
  5534.  
  5535.  $<                                The dependent file that is out-of-date
  5536.                                    with respect to the current target
  5537.                                    (evaluated only for inference rules).
  5538.  
  5539.  $(CC)                             The command to invoke the C compiler. By
  5540.                                    default, $(CC) is predefined as  CC = cl,
  5541.                                    which invokes the optimizing compiler.
  5542.  
  5543.  $(AS)                             The command that invokes the Microsoft
  5544.                                    Macro Assembler. NMAKE predefines this
  5545.                                    macro as  AS = masm.
  5546.  Macro                             Meaning
  5547.  ────────────────────────────────────────────────────────────────────────────
  5548.                                   macro as  AS = masm.
  5549.  
  5550.  
  5551.  
  5552.  Table   6.2  (continued)
  5553.  
  5554. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  5555.  Macro                             Meaning
  5556.  ────────────────────────────────────────────────────────────────────────────
  5557.  $(MAKE)                           The name with which the NMAKE utility is
  5558.                                    invoked. This macro is used to invoke
  5559.                                    NMAKE recursively. It causes the line on
  5560.                                    which it appears to be executed even if
  5561.                                    the /N option is on. You can redefine
  5562.                                    this macro if you want to execute
  5563.                                    another program.
  5564.                                    The $(MAKE) macro is useful for building
  5565.                                    different versions of a program. The
  5566.                                    following description file invokes NMAKE
  5567.  Macro                             Meaning
  5568.  ────────────────────────────────────────────────────────────────────────────
  5569.                                   following description file invokes NMAKE
  5570.                                    recursively to build targets in the
  5571.                                    VERS1 and VERS2 directories.
  5572.  
  5573.                                    all :vers1 vers2
  5574.                                    versl  :
  5575.                                       cd versl
  5576.                                       $(MAKE)
  5577.                                       cd  . .
  5578.                                    vers2 :
  5579.                                       cd vers2
  5580.                                       $(MAKE)
  5581.                                       cd . .
  5582.  
  5583.                                    The example changes to the VERS1
  5584.                                    directory, then invokes NMAKE
  5585.                                    recursively, causing NMAKE to process
  5586.                                    the file MAKEFILE in that directory.
  5587.                                    Then it changes to the VERS2 directory
  5588.  Macro                             Meaning
  5589.  ────────────────────────────────────────────────────────────────────────────
  5590.                                   Then it changes to the VERS2 directory
  5591.                                    and invokes NMAKE again, processing the
  5592.                                    file MAKEFILE in that directory.
  5593.                                    Deeply recursive build procedures can
  5594.                                    exhaust NMAKE's run-time stack, causing
  5595.                                    a run-time error. To eliminate the error,
  5596.                                    use the EXEHDR utility to increase
  5597.                                    NMAKE's run-time stack. The following
  5598.                                    command, for example, gives NMAKE.EXE a
  5599.                                    stack size of 16,384 (0x4000) bytes:
  5600.  
  5601.                                    exehdr /stack:0x4000 nmake.exe
  5602.  
  5603.  $(MAKEFLAGS)                      The NMAKE options currently in effect.
  5604.                                    If you invoke NMAKE recursively, you
  5605.                                    should use the command:  $(MAKE)
  5606.                                    $(MAKEFLAGS). You cannot redefine this
  5607.                                    macro.
  5608.  
  5609.  Macro                             Meaning
  5610.  ────────────────────────────────────────────────────────────────────────────
  5611. 
  5612.  $(MAKEDIR)                        The directory from which NMAKE is
  5613.                                    invoked.
  5614.  
  5615.  ────────────────────────────────────────────────────────────────────────────
  5616.  
  5617.  
  5618.  
  5619.  Like user-defined macro names, predefined macro names are case sensitive.
  5620.  NMAKE interprets  CC  and  cc  as different macro names.
  5621.  
  5622.   Macro modifiers allow you to specify parts of predefined macros
  5623.  representing file names.
  5624.  
  5625.  You can append characters to any of the first six macros in Table 6.2 to
  5626.  modify its meaning. Appending a D specifies the directory part of the file
  5627.  name only, an F specifies the file name, a B specifies just the base name,
  5628.  and an R specifies the complete file name without the extension. If you add
  5629.  one of these characters, you must enclose the macro name in parentheses.
  5630.  (The predefined macros $$@ and $** are the only exceptions to the rule that
  5631.  macro names more than one character long must be enclosed in parentheses.)
  5632.  
  5633.  For example, assume that $@ has the value C:\ SOURCE \ PROG \ SORT.OBJ. The
  5634.  list below shows the effect of combining the special characters with $@:
  5635.  
  5636.  Macro                             Value
  5637.  ────────────────────────────────────────────────────────────────────────────
  5638.  $(@D)                             C:\ SOURCE \ PROG
  5639.  
  5640.  $(@F)                             SORT.OBJ
  5641.  
  5642.  $(@B)                             SORT
  5643.  
  5644.  $(@R)                             C:\ SOURCE \ PROG \ SORT
  5645.  
  5646.  For example, in the code below, the macro $? represents the names of all
  5647.  dependents that are more recent than the target. The exclamation point
  5648.  causes NMAKE to execute the LIB command once for each dependent in the list.
  5649.  As a result, the LIB command is executed up to three times, each time
  5650.  replacing a module with a newer version.
  5651.  
  5652.    trig.lib : sin.obj cos.obj arctan.obj
  5653.            !LIB trig.lib -+$?;
  5654.  
  5655.  In the following example, NMAKE updates a group of include files:
  5656.  
  5657.    # Include files depend on versions in current directory
  5658.    DIR=c:\include
  5659.    $(DIR)\globals.h : globals.h
  5660.     COPY globals.h $@
  5661.    $(DIR)\types.h : types.h
  5662.     COPY types.h $@
  5663.    $(DIR)\macros.h : macros.h
  5664.     COPY macros.h $@
  5665.  
  5666.  Each of the files GLOBALS.H, TYPES.H, and MACROS.H in the directory  C:\
  5667.  INCLUDE depends on its counterpart in the current directory. If one of the
  5668.  include files is out-of-date, NMAKE replaces it with the file of the same
  5669.  name from the current directory.
  5670.  
  5671.  
  5672.  Substitution within Macros
  5673.  
  5674.  Just as macros allow you to substitute text in a description file, you can
  5675.  also substitute text within a macro itself. Use the following form:
  5676.  
  5677.    $(macroname:string1 = string2)
  5678.  
  5679.   You can replace text in a macro, as well as in the description file.
  5680.  
  5681.  Every occurrence of string1 is replaced by string2 in the macro macroname.
  5682.  Do not put any spaces or tabs between macroname and the colon. Spaces
  5683.  between the colon and string1 are made part of string1. If string2 is a null
  5684.  string, all occurrences of string1 are deleted from the macroname macro.
  5685.  
  5686.  The following description file illustrates macro substitution:
  5687.  
  5688.    SRCS = prog.c sub1.c sub2.c
  5689.    prog.exe : $(SRCS:.c=.obj)
  5690.            LINK  $**;
  5691.  
  5692.    DUP : $(SRCS)
  5693.            !COPY $** c:\backup
  5694.  
  5695.  The predefined macro $** stands for the names of all the dependent files
  5696.  (see the previous section). If you invoke the example file with a command
  5697.  line that specifies both targets, NMAKE executes the following commands:
  5698.  
  5699.    LINK prog.obj sub1.obj sub2.obj;
  5700.  
  5701.    COPY prog.c c:\backup
  5702.    COPY sub1.c c:\backup
  5703.    COPY sub2.c c:\backup
  5704.  
  5705.  The macro substitution does not alter the definition of the  SRCS  macro,
  5706.  rather, it simply replaces the listed characters. When NMAKE builds the
  5707.  target PROG.EXE, it gets the definition for the predefined macro $** (the
  5708.  dependent list) from the dependency line, which specifies the macro
  5709.  substitution in  SRCS. The same is true for the second target,  DUP. In this
  5710.  case, however, no macro substitution is requested, so  SRCS  retains its
  5711.  original value, and $** represents the names of the C source files. (In the
  5712.  example above, the target  DUP  is a pseudotarget; Section 6.3.6 describes
  5713.  pseudotargets.)
  5714.  
  5715.  You can also perform substitution in the following predefined macros: $@,
  5716.  $*, $**, $?, and $. The principle is the same as for other macros. The
  5717.  command in the following description block substitutes within a predefined
  5718.  macro:
  5719.  
  5720.    target.abc : depend.xyz
  5721.       echo $(@:targ=blank)
  5722.  
  5723.  If dependent  depend.xyz  is out-of-date relative to target  target.abc,
  5724.  then NMAKE executes the command
  5725.  
  5726.    echo blanket.abc
  5727.  
  5728.  The example uses the predefined macro $@, which equals the full name of the
  5729.  current target ( target.abc). It substitutes  blank  for  targ  in the
  5730.  target, resulting in  blanket.abc. Note that you do not put the usual dollar
  5731.  sign in front of the predefined macro. The example uses
  5732.  
  5733.    $(@:targ=blank)
  5734.  
  5735.  instead of
  5736.  
  5737.    $($@:targ=blank)
  5738.  
  5739.  to substitute within the predefined macro $@.
  5740.  
  5741.  
  5742.  Inherited Macros
  5743.  
  5744.  When NMAKE executes, it creates macros equivalent to every current
  5745.  environment variable. These are called "inherited" macros because they have
  5746.  the same names and values as the corresponding environment variables. (The
  5747.  inherited macro is all uppercase, however, even if the corresponding
  5748.  environment variable is not.)
  5749.  
  5750.  Inherited macros can be used like other macros. You can also redefine them.
  5751.  The following example redefines the inherited macro  PATH:
  5752.  
  5753.    PATH = c:\tools\bin
  5754.  
  5755.    sample.obj : sample.c
  5756.       CL /c sample.c
  5757.  
  5758.   Inherited macros take their definitions from environment variables.
  5759.  
  5760.  No matter what value PATH had in the DOS environment, it has the value
  5761.  c:\tools\bin  when NMAKE executes the CL command in this description block.
  5762.  Redefining the inherited macro does not affect the original environment
  5763.  variable; when NMAKE terminates, PATH has its original value.
  5764.  
  5765.  The /E option defeats macro inheritance. If you supply this option, NMAKE
  5766.  ignores any attempt to redefine a macro that derives from an environment
  5767.  variable.
  5768.  
  5769.  
  5770.  Precedence among Macro Definitions
  5771.  
  5772.  If you define the same macro in more than one place, NMAKE uses the macro
  5773.  with the highest precedence. The precedence from highest to lowest is as
  5774.  follows:
  5775.  
  5776.  
  5777.    1.  Macros defined on the command line
  5778.  
  5779.    2.  Macros defined in a description file or include file
  5780.  
  5781.    3.  Inherited macros
  5782.  
  5783.    4.  Macros defined in the TOOLS.INI file
  5784.  
  5785.    5.  Predefined macros such as CC and AS
  5786.  
  5787.  
  5788.  The /E option defeats any attempt to redefine inherited macros. If you run
  5789.  NMAKE with this option, macros inherited from environment variables override
  5790.  any same-named macros in the description file.
  5791.  
  5792.  
  5793.  6.3.4  Inference Rules
  5794.  
  5795.  Inference rules are templates that NMAKE uses to create files with a given
  5796.  extension. For instance, when NMAKE encounters a description block with no
  5797.  commands, it tries to apply an inference rule that tells how to create the
  5798.  target from the dependent files, given the two extensions. Similarly, if a
  5799.  dependent file does not exist, NMAKE tries to apply an inference rule that
  5800.  tells how to create the missing dependent from another file with the same
  5801.  base name.
  5802.  
  5803.   Inference rules tell NMAKE how to create files with a certain extension.
  5804.  
  5805.  Inference rules provide a convenient shorthand for common operations. For
  5806.  instance, you can use an inference rule to avoid repeating the same command
  5807.  in several description blocks.
  5808.  
  5809.  You can define your own inference rules or use predefined inference rules.
  5810.  This section begins by describing user-defined inference rules.
  5811.  
  5812.  
  5813.  User-Defined Inference Rules
  5814.  
  5815.  You can define inference rules in the description file or in the TOOLS.INI
  5816.  file. An inference-rule definition lists two file extensions and one or more
  5817.  commands. For instance, the following inference rule tells NMAKE how to
  5818.  build a .OBJ file using a .C file:
  5819.  
  5820.    .C.OBJ:
  5821.       CL /c $<;
  5822.  
  5823.  The first line lists two extensions. The second extension (.OBJ) specifies
  5824.  the type of the desired file and the first (.C) specifies the type of the
  5825.  desired file's dependent. The second line lists the command used to build
  5826.  the desired file. Here, the predefined macro $ represents the name of a
  5827.  dependent that is out-of-date relative to the target.
  5828.  
  5829.  NMAKE could apply the above inference rule to the following description
  5830.  block:
  5831.  
  5832.    sample.obj :
  5833.  
  5834.  The description block lists only a target, SAMPLE.OBJ. Both the dependent
  5835.  and the command are missing. However, given the target's base name and
  5836.  extension, plus the above inference rule, NMAKE has enough information to
  5837.  build the target. NMAKE first looks for a .C file with the same base name as
  5838.  the target. If SAMPLE.C exists, NMAKE compares its date to that of
  5839.  SAMPLE.OBJ (the comparison is triggered by the predefined macro $). If
  5840.  SAMPLE.C has changed more recently, NMAKE compiles it using the CL command
  5841.  listed in the inference rule:
  5842.  
  5843.    CL/c sample.c
  5844.  
  5845.  ────────────────────────────────────────────────────────────────────────────
  5846.  NOTE
  5847.  
  5848.  NMAKE applies an inference rule only if the base name of the file it is
  5849.  trying to create matches the base name of a file that already exists. Thus,
  5850.  inference rules are useful only when there is a one-to-one correspondence
  5851.  between the desired file and its dependent. You cannot define an inference
  5852.  rule that replaces several modules in a library, for example.
  5853.  ────────────────────────────────────────────────────────────────────────────
  5854.  
  5855.  
  5856.  Extension Search Paths
  5857.  
  5858.  If an inference rule does not specify a search path, as in the example
  5859.  above, NMAKE looks for files in the current directory. You can specify a
  5860.  single path for each of the extensions, using the following form:
  5861.  
  5862.    {frompath}. fromext{topath}. toext:
  5863.            commands
  5864.  
  5865.  NMAKE searches in the frompath directory for files with the fromext
  5866.  extension. It uses commands to create files with the toext extension in the
  5867.  topath directory.
  5868.  
  5869.  
  5870.  Predefined Inference Rules
  5871.  
  5872.  NMAKE provides predefined inference rules to perform these common
  5873.  development tasks:
  5874.  
  5875.  
  5876.    ■   Creating an .OBJ file by compiling a .C file
  5877.  
  5878.    ■   Creating an .OBJ file by assembling an .ASM file
  5879.  
  5880.    ■   Creating an .EXE file by compiling a .C file and linking the resulting
  5881.        .OBJ file
  5882.  
  5883.  
  5884.  Table 6.3 describes the predefined inference rules.
  5885.  
  5886.  Table   6.3 Predefined Inference Rules
  5887.  
  5888. ╓┌───────────────┌─────────────────────────┌─────────────────────────────────╖
  5889.  Inference Rule  Command                   Default Action
  5890.  ────────────────────────────────────────────────────────────────────────────
  5891.  .c.obj          $(CC) $(CFLAGS) /c $*.c   cl /c $*.c
  5892.  .asm.obj        $(AS) $(AFLAGS) $*;       masm $*;
  5893.  .c.exe          $(CC) $(CFLAGS) $*.c      cl $*.c
  5894.  ────────────────────────────────────────────────────────────────────────────
  5895.  
  5896.  
  5897.  For example, say that you have the following description file:
  5898.  
  5899.    sample.exe :
  5900.  
  5901.  Like the previous example, this description block lists a target without any
  5902.  dependents or commands. NMAKE looks at the target's extension (.EXE) and
  5903.  checks for an inference rule that describes how to create a .EXE file. The
  5904.  last rule in Table 6.3 provides this information:
  5905.  
  5906.    .c.exe:
  5907.       $(CC) $(CFLAGS) $*.c
  5908.  
  5909.  To apply this rule, NMAKE first looks for a file with the same base name as
  5910.  the target (SAMPLE) and the .C extension. If SAMPLE.C exists in the current
  5911.  directory, NMAKE executes the CL command given in the rule. The command
  5912.  compiles SAMPLE.C and links the resulting file SAMPLE.OBJ to create
  5913.  SAMPLE.EXE.
  5914.  
  5915.  
  5916.  Precedence among Inference Rules
  5917.  
  5918.  If the same inference rule is defined in more than one place, NMAKE uses the
  5919.  rule with the highest precedence. The precedence from highest to lowest is
  5920.  
  5921.  
  5922.    1.  Inference rules defined in the description file
  5923.  
  5924.    2.  Inference rules defined in the TOOLS.INI file
  5925.  
  5926.    3.  Predefined inference rules
  5927.  
  5928.  
  5929.  NMAKE uses a predefined inference rule only if no user-defined inference
  5930.  rule exists for the desired operation.
  5931.  
  5932.  
  5933.  6.3.5  Directives
  5934.  
  5935.  Directives allow you to write description files that are similar to batch
  5936.  files. Directives can execute commands conditionally, display error
  5937.  messages, include other files, and turn on or off certain options.
  5938.  
  5939.   NMAKE directives are similar to C preprocessor directives.
  5940.  
  5941.  A directive begins with an exclamation point (!), which must appear at the
  5942.  beginning of the line. You can place spaces between the exclamation point
  5943.  and the directive keyword. (See Table 6.4.)
  5944.  
  5945.  Table 6.4  Directives
  5946.  
  5947. ╓┌────────────────────────┌──────────────────────────────────────────────────╖
  5948.  Directive                Description
  5949.  ────────────────────────────────────────────────────────────────────────────
  5950.  !CMDSWITCHES             Turns on or off one of four NMAKE options: /D, /I,
  5951.  {+| -}opt...
  5952.                            /N, and /S. If no options are specified, the
  5953.                           options are reset to the way they were when NMAKE
  5954.                           started. Turn an option on by preceding it with a
  5955.                           plus sign (+), or turn it off by preceding it
  5956.                           with a minus sign (-). Using this keyword updates
  5957.                           the MAKEFLAGS macro.
  5958.  
  5959.  !ELSE                    Executes the statements between the !ELSE and
  5960.                           !ENDIF keywords if the statements preceding the
  5961.                           !ELSE keyword were not executed.
  5962.  
  5963.  !ENDIF                   Marks the end of the !IF, !IFDEF, or !IFNDEF
  5964.                           block of statements.
  5965.  
  5966.  Directive                Description
  5967.  ────────────────────────────────────────────────────────────────────────────
  5968. 
  5969.  !ERROR text              Causes text to be printed and then stops
  5970.                           execution.
  5971.  
  5972.  !IF constantexpression   Executes the statements between the !IF keyword
  5973.                           and the next !ELSE or !ENDIF keyword if constant
  5974.                           expression evaluates to a nonzero value.
  5975.  
  5976.  !IFDEF macroname         Executes the statements between the !IFDEF
  5977.                           keyword and the next !ELSE or !ENDIF keyword if
  5978.                           macroname is defined. NMAKE considers a macro
  5979.                           with a null value to be defined.
  5980.  
  5981.  !IFNDEF macroname        Executes the statements between the !IFNDEF
  5982.                           keyword and the next !ELSE or !ENDIF keyword if
  5983.                           macroname is not defined.
  5984.  
  5985.  !INCLUDE filename        Reads and evaluates the file filename before
  5986.                           continuing with the current description file. If
  5987.  Directive                Description
  5988.  ────────────────────────────────────────────────────────────────────────────
  5989.                          continuing with the current description file. If
  5990.                           filename is enclosed by angle brackets (< >),
  5991.                           NMAKE searches for the file in the directories
  5992.                           specified by the INCLUDE macro. Otherwise, it
  5993.                           looks only in the current directory. The
  5994.                           INCLUDE macro is initially set to the value of
  5995.                           the
  5996.                           INCLUDE environment variable.
  5997.  
  5998.  !UNDEF macroname         Marks macroname as being undefined in NMAKE's
  5999.                           symbol table.
  6000.  
  6001.  ────────────────────────────────────────────────────────────────────────────
  6002.  
  6003.  
  6004.  
  6005.  The constantexpression used with the !IF directive can consist of integer
  6006.  constants, string constants, or program invocations. Integer constants can
  6007.  use the C unary operators for numerical negation (-), one's complement (~),
  6008.  and logical negation (!). They can also use any of the C binary operators
  6009.  listed in Table 6.5.
  6010.  
  6011.  Table 6.5  Directive Operators
  6012.  
  6013. ╓┌─────────────────────┌─────────────────────────────────────────────────────╖
  6014.  Operator              Description
  6015.  ────────────────────────────────────────────────────────────────────────────
  6016.  +                     Addition
  6017.  -                     Subtraction
  6018.  *                     Multiplication
  6019.  /                     Division
  6020.  %                     Modulus
  6021.  &                     Bitwise AND
  6022.  |                     Bitwise OR
  6023.  ^^                    Bitwise XOR
  6024.  &&                    Logical AND
  6025.  ||                    Logical OR
  6026.  <<                    Left shift
  6027.  >>                    Right shift
  6028.  ==                    Equality
  6029.  Operator              Description
  6030.  ────────────────────────────────────────────────────────────────────────────
  6031. ==                    Equality
  6032.  !=                    Inequality
  6033.  <                     Less than
  6034.  >                     Greater than
  6035.  <=                    Less than or equal to
  6036.  >=                    Greater than or equal to
  6037.  ────────────────────────────────────────────────────────────────────────────
  6038.  
  6039.  
  6040.  You can group expressions using parentheses. NMAKE treats numbers as decimal
  6041.  unless they start with 0 (octal) or 0x (hexadecimal). Use the equality (==)
  6042.  operator to compare two strings for equality or the inequality (!=) operator
  6043.  to compare for inequality. Enclose strings with quotes. Program invocations
  6044.  must be in square brackets ([ ]).
  6045.  
  6046.  The following example illustrates directives:
  6047.  
  6048.    !INCLUDE <infrules.txt>
  6049.    !CMDSWITCHES +D
  6050.    winner.exe:winner.obj
  6051.    !IFDEF debug
  6052.    !  IF "$(debug)"=="y"
  6053.         LINK /CO winner.obj;
  6054.    !  ELSE
  6055.         LINK winner.obj;
  6056.    !  ENDIF
  6057.    !ELSE
  6058.    !  ERROR Macro named debug is not defined.
  6059.    !ENDIF
  6060.  
  6061.  The !INCLUDE directive causes NMAKE to insert the file INFRULES.TXT into the
  6062.  description file. The !CMDSWITCHES directive turns on the /D option, which
  6063.  displays the dates of the files as they are checked. If WINNER.EXE is
  6064.  out-of-date with respect to WINNER.OBJ, the !IFDEF directive checks to see
  6065.  if the macro  debug  is defined. If it is defined, the !IF directive checks
  6066.  to see if it is set to  y. If it is, the linker is invoked with the /CO
  6067.  option; otherwise it is invoked without. If the  debug  macro is not
  6068.  defined, the !ERROR directive prints the message and NMAKE stops.
  6069.  
  6070.  
  6071.  6.3.6  Pseudotargets
  6072.  
  6073.  Pseudotargets are useful for building a group of files or executing a group
  6074.  of commands.
  6075.  
  6076.  A "pseudotarget" is similar to a target, but it is not a file. It is a name
  6077.  that serves as a "handle" for building a group of files or executing a group
  6078.  of commands. In the following example,  UPDATE  is a pseudotarget.
  6079.  
  6080.    UPDATE: *.*
  6081.     !COPY $** a:\product
  6082.  
  6083.  When NMAKE evaluates a pseudotarget, it always considers the dependents to
  6084.  be out-of-date. In the example, NMAKE copies each of the dependent files to
  6085.  the specified drive and directory.
  6086.  
  6087.  Like macro names, pseudotarget names are case sensitive. Predefined
  6088.  pseudotarget names are all uppercase.
  6089.  
  6090.  The pseudotargets in Table 6.6 are predefined to provide special rules in a
  6091.  description file. You can use their names on the command line, in a
  6092.  description file, or in the TOOLS.INI file. You need not specify them as
  6093.  targets; NMAKE uses the rules they define no matter where they appear.
  6094.  
  6095.  Table   6.6  Pseudotargets
  6096.  
  6097. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  6098.  Pseudotarget                      Action
  6099.  ────────────────────────────────────────────────────────────────────────────
  6100.  .IGNORE:                          Ignores exit codes returned by programs
  6101.                                    called from the description file. Same
  6102.                                    effect as invoking NMAKE with the /I
  6103.                                    option.
  6104.  
  6105.  .PRECIOUS: target(s)              Tells NMAKE not to delete target(s) if
  6106.                                    the commands that build it are quit or
  6107.                                    interrupted. Using this pseudotarget
  6108.                                    overrides the NMAKE default. By default,
  6109.                                    NMAKE deletes the target if it cannot be
  6110.                                    sure the target is built successfully.
  6111.  
  6112.                                    The .PRECIOUS pseudotarget is rarely
  6113.  Pseudotarget                      Action
  6114.  ────────────────────────────────────────────────────────────────────────────
  6115.                                   The .PRECIOUS pseudotarget is rarely
  6116.                                    needed. Like most professional tools,
  6117.                                    Microsoft language tools clean up by
  6118.                                    themselves when errors occur.
  6119.  
  6120.  .SILENT:                          Does not display lines as they are
  6121.                                    executed. Same effect as invoking NMAKE
  6122.                                    with the /S option.
  6123.  
  6124.  .SUFFIXES:list                    Lists file suffixes for NMAKE to try
  6125.                                    when building a target file for which no
  6126.                                    dependents are specified. This list is
  6127.                                    used together with inference rules. See
  6128.                                    Section 6.3.4, "Inference Rules."
  6129.  
  6130.                                    When NMAKE finds a target without any
  6131.                                    dependents, it searches the current
  6132.                                    directory for a file with the same base
  6133.                                    name as the target and a suffix from the
  6134.  Pseudotarget                      Action
  6135.  ────────────────────────────────────────────────────────────────────────────
  6136.                                   name as the target and a suffix from the
  6137.                                    list. If NMAKE finds such a file, and if
  6138.                                    an inference rule applies to the file,
  6139.                                    then NMAKE treats the file as a depen-
  6140.                                    dent of the target. The order of the
  6141.                                    suffixes in the list defines the order
  6142.                                    in which NMAKE searches for the file.
  6143.                                    The list is predefined as follows:
  6144.  
  6145.                                    .SUFFIXES: .obj .exe .c .asm
  6146.  
  6147.                                    To add suffixes to the list, specify
  6148.                                    .SUFFIXES :
  6149.                                    followed by the new suffixes. To clear
  6150.                                    the list, specify  .SUFFIXES:
  6151.  
  6152.  ────────────────────────────────────────────────────────────────────────────
  6153.  
  6154.  
  6155.  
  6156.  
  6157.  6.3.7  PWB's extmake Syntax
  6158.  
  6159.  NMAKE description files can use the same syntax as the extmake switch of PWB
  6160.  (see Chapter 8, "Customizing the Microsoft Programmer's WorkBench"). This
  6161.  syntax allows you to determine the drive, path, base name, and extension of
  6162.  the first dependent, information that is not otherwise available. The file
  6163.  name, and parts of its name, are represented using the syntax
  6164.  
  6165.    %|partsF
  6166.  
  6167.  where parts is one or more of the following:
  6168.  
  6169.  Letter                            Description
  6170.  ────────────────────────────────────────────────────────────────────────────
  6171.  d                                 Drive
  6172.  
  6173.  e                                 File extension
  6174.  
  6175.  f                                 File base name
  6176.  
  6177.  p                                 Path
  6178.  
  6179.  s                                 Complete name
  6180.  
  6181.  The following example uses extmake syntax:
  6182.  
  6183.    sample.obj : sample.c
  6184.       CL /Fod:%|pfF %|dfeF
  6185.  
  6186.  In this example, the sequence  %|pfF  represents the path and base name of
  6187.  the first dependent file, while the sequence  %|dfeF  represents the drive,
  6188.  base name, and extension of the same file. The example, then, compiles the
  6189.  file and writes the output to a file on the same path but with the default
  6190.  .OBJ extension.
  6191.  
  6192.  The percent symbol (%) is a replacement character in DOS and OS/2 command
  6193.  lines in the description file. To use extmake syntax in command-line
  6194.  arguments, specify each percent symbol as a double percent symbol (%%).
  6195.  
  6196.  
  6197.  6.4  Command-Line Options
  6198.  
  6199.  NMAKE accepts a number of options, which are listed in Table 6.7. You can
  6200.  specify options in uppercase or lowercase and use either a slash or dash.
  6201.  For example, -A, /A, -a, and /a all represent the same option.
  6202.  
  6203.  Table   6.7 NMAKE Options
  6204.  
  6205. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  6206.  Option                            Action
  6207.  ────────────────────────────────────────────────────────────────────────────
  6208.  /A                                Builds all of the requested targets even
  6209.                                    if they are not out-of-date.
  6210.  
  6211.  /C                                Suppresses nonfatal error or warning
  6212.                                    messages and the NMAKE logo display.
  6213.  
  6214.  /D                                Displays the modification date of each
  6215.                                    file.
  6216.  
  6217.  /E                                Causes environment variables to override
  6218.  Option                            Action
  6219.  ────────────────────────────────────────────────────────────────────────────
  6220. /E                                Causes environment variables to override
  6221.                                    macro definitions in description files.
  6222.                                    See Section 6.3.3, "Macros."
  6223.  
  6224.  /F filename                       Specifies filename as the name of the
  6225.                                    description file. If you supply a dash
  6226.                                    (-) instead of a file name, NMAKE gets
  6227.                                    input from the standard input device
  6228.                                    instead of the description file.
  6229.  
  6230.  /HELP                             Calls the QuickHelp utility. If the
  6231.                                    QuickHelp program is not available,
  6232.                                    NMAKE displays the most commonly used
  6233.                                    NMAKE options.
  6234.  
  6235.  /I                                Ignores return codes from commands
  6236.                                    listed in the description file. NMAKE
  6237.                                    processes the whole description file
  6238.                                    even if errors occur.
  6239.  Option                            Action
  6240.  ────────────────────────────────────────────────────────────────────────────
  6241.                                   even if errors occur.
  6242.  
  6243.  /N                                Displays but does not execute the
  6244.                                    description file's commands. This option
  6245.                                    is useful for debugging description
  6246.                                    files and checking which targets are
  6247.                                    out-of-date.
  6248.  
  6249.  /NOLOGO                           Suppresses the NMAKE logo display.
  6250.  
  6251.  /P                                Displays all macro definitions and
  6252.                                    target descriptions on the standard
  6253.                                    output device.
  6254.  
  6255.  /Q                                Returns zero if the target is up-to-date
  6256.                                    and nonzero if it is not. This option is
  6257.                                    useful when running NMAKE from a batch
  6258.                                    file.
  6259.  
  6260.  Option                            Action
  6261.  ────────────────────────────────────────────────────────────────────────────
  6262. 
  6263.  /R                                Ignores inference rules and macros that
  6264.                                    are predefined or defined in the
  6265.                                    TOOLS.INI file.
  6266.  
  6267.  /S                                Suppresses the display of commands
  6268.                                    listed in the description file.
  6269.  
  6270.  /T                                Changes the modification dates for
  6271.                                    out-of-date target files to the current
  6272.                                    date.
  6273.  
  6274.  /X filename                       Sends all error output to filename,
  6275.                                    which can be a file or a device. If you
  6276.                                    supply a dash (-) instead of a file name,
  6277.                                    the error output is sent to the standard
  6278.                                    output device.
  6279.  
  6280.  /Z                                Used for internal communication between
  6281.  Option                            Action
  6282.  ────────────────────────────────────────────────────────────────────────────
  6283. /Z                                Used for internal communication between
  6284.                                    NMAKE and PWB.
  6285.  
  6286.  /?                                Displays a brief summary of NMAKE syntax
  6287.                                    and exits to the operating system.
  6288.  
  6289.  ────────────────────────────────────────────────────────────────────────────
  6290.  
  6291.  
  6292.  
  6293.  The following command specifies two NMAKE options:
  6294.  
  6295.    NMAKE /f sample.mak /c targ1 targ2
  6296.  
  6297.  The /f option tells NMAKE to read the description file SAMPLE.MAK. The /c
  6298.  option tells NMAKE not to display nonfatal error messages and warnings. The
  6299.  command lists two targets (targ1  and  targ2) to update.
  6300.  
  6301.    NMAKE  /D /N targ1 targ1.mak
  6302.  
  6303.  In the example above, NMAKE updates the target  targ1. If the current
  6304.  directory does not contain a file named MAKEFILE, NMAKE reads the file
  6305.  TARG1.MAK as the description file. The /D option displays the modification
  6306.  date of each file; the /N option displays the commands without executing
  6307.  them.
  6308.  
  6309.  
  6310.  6.5  NMAKE Command Files
  6311.  
  6312.  Occasionally, you may need to give NMAKE a long list of command-line
  6313.  arguments that exceeds the maximum length of a command line (128 characters
  6314.  in DOS, 256 in OS/2). To do this, place the command arguments in a file,
  6315.  then give the name of the file when you run NMAKE.
  6316.  
  6317.  For instance, say that you create a file named UPDATE, which consists of
  6318.  this line:
  6319.  
  6320.    /S "program = sample" sort.exe search.exe
  6321.  
  6322.  If you start NMAKE with the command
  6323.  
  6324.    NMAKE @update
  6325.  
  6326.  NMAKE reads its command-line arguments from UPDATE. The at sign (@) tells
  6327.  NMAKE to read arguments from the file. The effect is the same as if you
  6328.  typed the arguments directly on the command line:
  6329.  
  6330.    NMAKE /S "program = sample" sort.exe search.exe
  6331.  
  6332.  Within the file, line breaks between arguments are treated as spaces. Macro
  6333.  definitions that contain spaces must be enclosed in quotation marks, just as
  6334.  if you typed them on the command line. You can continue a macro definition
  6335.  across multiple lines by ending each line except the last with a backslash (
  6336.  \ ):
  6337.  
  6338.    /S "program \
  6339.    = sample" sort.exe search.exe
  6340.  
  6341.  This file is equivalent to the first example. The backslash in the example
  6342.  allows the macro definition ("program = sample" ) to span two lines.
  6343.  
  6344.  
  6345.  6.6  The TOOLS.INI File
  6346.  
  6347.  You can customize NMAKE by placing commonly used macros and inference rules
  6348.  in the TOOLS.INI initialization file. Settings for NMAKE must follow a line
  6349.  that begins with  [NMAKE]. This part of the initialization file can contain
  6350.  macro definitions, .SUFFIXES lists, and inference rules. For example,
  6351.  
  6352.    [NMAKE]
  6353.    CC=cl
  6354.    CFLAGS=-Gc -Gs -W3 -Oat
  6355.    .c.obj:
  6356.        $(CC) -c $(CFLAGS) $*.c
  6357.  
  6358.  If TOOLS.INI contains the code above, NMAKE reads and applies the lines
  6359.  following  [NMAKE]. The example defines the macros CC and CFLAGS and
  6360.  redefines the inference rule for making .OBJ files from .C sources.
  6361.  
  6362.  NMAKE looks for TOOLS.INI in the current directory. If it is not found
  6363.  there, NMAKE searches the directory specified by the INIT environment
  6364.  variable.
  6365.  
  6366.  
  6367.  6.7  In-Line Files
  6368.  
  6369.  NMAKE can write "in-line files," which can contain any text you specify. One
  6370.  use for in-line files is to write a response file for another utility such
  6371.  as LIB. (Response files are useful when you need to supply a program with a
  6372.  long list of arguments that exceeds the maximum length of the command line.)
  6373.  
  6374.  
  6375.  Use this syntax to create an in-line file:
  6376.  
  6377.    target : dependents
  6378.       command << «filename»
  6379.    inlinetext
  6380.    <<«KEEP | NOKEEP»
  6381.  
  6382.  All of the text between the two sets of double angle brackets () is placed
  6383.  in the in-line file. The filename is optional. If you don't supply filename,
  6384.  NMAKE gives the in-line file a unique name. NMAKE places the in-line file in
  6385.  the current directory or, if the TMP environment variable is defined, in the
  6386.  directory specified by TMP.
  6387.  
  6388.  The in-line file can be temporary or permanent. If you don't specify
  6389.  otherwise, or if you specify NOKEEP, it is temporary. Specify KEEP to retain
  6390.  the file.
  6391.  
  6392.  The following example creates a LIB response file named LIB.LRF:
  6393.  
  6394.    math.lib : add.obj sub.obj mul.obj div.obj
  6395.      LIB @<<lib.lrf
  6396.    math.lib
  6397.    -+add.obj-+sub.obj-+mul.obj-+div.obj
  6398.    listing
  6399.    <<KEEP
  6400.  
  6401.  The resulting response file tells LIB which library to use, the commands to
  6402.  execute, and the listing file to produce:
  6403.  
  6404.    math.lib
  6405.    -+add.obj-+sub.obj-+mul.obj-+div.obj
  6406.    listing
  6407.  
  6408.  The in-line file specification can create more than one in-line file. For
  6409.  instance,
  6410.  
  6411.    target.abc : depend.xyz
  6412.       cat <<file1 <<file2
  6413.    I am the contents of file1.
  6414.    <<KEEP
  6415.    I am the contents of file2.
  6416.    <<KEEP
  6417.  
  6418.  The example creates two in-line files named FILE1 and FILE2; then NMAKE
  6419.  executes the command:
  6420.  
  6421.    CAT file1 file2
  6422.  
  6423.  The KEEP keywords tell NMAKE not to delete FILE1 and FILE2 when done.
  6424.  
  6425.  
  6426.  6.8  NMAKE Operations Sequence
  6427.  
  6428.  If you are writing a complex description file, you may need to know the
  6429.  exact order of steps that NMAKE follows. This section describes those steps
  6430.  in order.
  6431.  
  6432.  When you run NMAKE from the command line, its first task is to find the
  6433.  description file, following these steps:
  6434.  
  6435.  
  6436.    1.  If NMAKE is invoked with the /F option, it uses the file name
  6437.        specified in the option.
  6438.  
  6439.    2.  If /F is not specified, NMAKE looks for a file named MAKEFILE in the
  6440.        current directory. If such a file exists, it is used as a description
  6441.        file.
  6442.  
  6443.    3.  If MAKEFILE is not in the current directory, NMAKE parses the command
  6444.        line for the first string that is not an option or a macro definition
  6445.        and treats this string as a file name. If the file-name extension does
  6446.        not appear in the .SUFFIXES list, NMAKE uses the file as the
  6447.        description file. If the extension appears in the .SUFFIXES list,
  6448.        NMAKE tries additional strings until it finds a suitable file. (See
  6449.        Section 6.3.6, "Pseudotargets," for a description of the .SUFFIXES
  6450.        list.)
  6451.  
  6452.    4.  If NMAKE still has not found a description file, it returns an error.
  6453.  
  6454.  
  6455.  NMAKE stops searching for a description file as soon as it finds one, even
  6456.  if other potential description files exist. If you specify /F, NMAKE uses
  6457.  the file specified by that option even if MAKEFILE exists in the current
  6458.  directory. Similarly, if NMAKE uses MAKEFILE, any description file listed in
  6459.  the command line is treated as a target.
  6460.  
  6461.   If you do not specify targets, NMAKE updates only the first target in the
  6462.  description file.
  6463.  
  6464.  Next, NMAKE updates every target listed on the command line. If none is
  6465.  listed, NMAKE updates only the first target in the description file. (This
  6466.  behavior differs from the older MAKE program's default; see Section 6.9,
  6467.  "Differences between NMAKE and MAKE.")
  6468.  
  6469.  NMAKE then applies macro definitions and inference rules in the following
  6470.  order, from highest to lowest priority:
  6471.  
  6472.  
  6473.    1.  Macros defined on the command line
  6474.  
  6475.    2.  Macros defined in a description file or include file
  6476.  
  6477.    3.  Inherited macros
  6478.  
  6479.    4.  Macros defined in the TOOLS.INI file
  6480.  
  6481.    5.  Predefined macros such as CC and AS
  6482.  
  6483.  
  6484.  Definitions in later steps take precedence over definitions in earlier
  6485.  steps. The /E option, however, causes inherited macros to override macros
  6486.  defined on the command line. The /R option causes NMAKE to ignore macros and
  6487.  inference rules that are predefined or defined in TOOLS.INI.
  6488.  
  6489.  Now NMAKE updates each target in the order in which it appears in the
  6490.  description file. It compares the date and time of each dependent with that
  6491.  of the target and performs the commands needed to update the target. If you
  6492.  specify the /A option or if the target is a pseudotarget, NMAKE updates the
  6493.  target even if its dependents are not out-of-date.
  6494.  
  6495.  If the target has no explicit dependents, NMAKE looks in the current
  6496.  directory for one or more files whose extensions are in the .SUFFIXES list.
  6497.  If it finds such files, NMAKE treats them as dependents and updates the
  6498.  target according to the commands.
  6499.  
  6500.  If no commands are given to update the target or if the dependents cannot be
  6501.  found, NMAKE applies inference rules to build the target. By default, it
  6502.  tries to build .EXE files from .OBJ files; and it tries to build .OBJ files
  6503.  from .C and .ASM sources. In practice, this means you should specify .OBJ
  6504.  files as dependents, because NMAKE compiles your source files when it can't
  6505.  find the .OBJ files.
  6506.  
  6507.  NMAKE normally quits processing the description file when a command  returns
  6508.  an error. In addition, if it cannot tell that the target was built
  6509.  successfully, NMAKE deletes the partially created target. If you use the /I
  6510.  commandline option, NMAKE ignores exit codes and attempts to continue
  6511.  processing. The .IGNORE pseudotarget has the same effect. To prevent NMAKE
  6512.  from  deleting the partially created target, specify the target name in the
  6513.  .PRECIOUS pseudotarget.
  6514.  
  6515.  Alternatively, you can use the dash (-) command modifier to ignore the error
  6516.  code for an individual command. An optional number after the dash tells
  6517.  NMAKE to continue if the command returns an error code that is less than or
  6518.  equal to the number, and to stop if the error code is greater than the
  6519.  number.
  6520.  
  6521.  You can help document errors by using the !ERROR directive to print
  6522.  descriptive text. The directive causes NMAKE to print some text, then stop,
  6523.  even if you use /I, .IGNORE, or the dash (-) modifier.
  6524.  
  6525.  
  6526.  6.9  Differences between NMAKE and MAKE
  6527.  
  6528.  As its name implies, NMAKE is a new utility that replaces the older
  6529.  Microsoft MAKE program. NMAKE differs from MAKE in the following ways:
  6530.  
  6531.  
  6532.    ■   NMAKE does not evaluate targets sequentially. Instead, NMAKE updates
  6533.        the targets you specify when you invoke it, regardless of their
  6534.        positions in the description file. If no targets are specified, NMAKE
  6535.        updates only the first target in the file.
  6536.  
  6537.    ■   NMAKE accepts command-line arguments from a file.
  6538.  
  6539.    ■   NMAKE provides more command-line options.
  6540.  
  6541.    ■   NMAKE provides more predefined macros.
  6542.  
  6543.    ■   NMAKE permits substitutions within macros.
  6544.  
  6545.    ■   NMAKE supports directives placed in the description file.
  6546.  
  6547.    ■   NMAKE allows you to specify include files in the description file.
  6548.  
  6549.  
  6550.  The first item in the list deserves special emphasis. While MAKE normally
  6551.  builds every target, working from beginning to end of the description file,
  6552.  NMAKE expects you to specify targets on the command line. If you do not,
  6553.  NMAKE builds only the first target in the description file.
  6554.  
  6555.  The difference is clear if you run NMAKE using a typical MAKE description
  6556.  file, which lists a series of subordinate targets followed by a higher-level
  6557.  target that depends on the subordinates:
  6558.  
  6559.    pmapp.obj : pmapp.c
  6560.       CL /c /G2sw /W3 pmapp.c
  6561.  
  6562.    pmapp.exe : pmapp.obj pmapp.def
  6563.       LINK pmapp, /align:16, NUL, os2, pmapp
  6564.  
  6565.  MAKE builds both targets (PMAPP.OBJ and PMAPP.EXE), but NMAKE builds only
  6566.  the first target (PMAPP.OBJ).
  6567.  
  6568.  Because of these performance differences, you may want to convert MAKE files
  6569.  to NMAKE files. MAKE description files are easy to convert. A simple method
  6570.  is to create a new description block at the beginning of the file. Give this
  6571.  block a pseudotarget named  ALL  and list the top-level target as a
  6572.  dependent of  ALL. To build  ALL, NMAKE must update every target upon which
  6573.  the target of  ALL  depends:
  6574.  
  6575.    ALL : pmapp.exe
  6576.  
  6577.    pmapp.obj : pmapp.c
  6578.       CL /c /G2sw /W3 pmapp.c
  6579.  
  6580.    pmapp.exe : pmapp.obj pmapp.def
  6581.       LINK pmapp, /align:16, NUL, os2, pmapp
  6582.  
  6583.  If the above file is named MAKEFILE, you can update the target PMAPP.EXE
  6584.  with the command
  6585.  
  6586.    NMAKE
  6587.  
  6588.  or the command
  6589.  
  6590.    NMAKE ALL
  6591.  
  6592.  Note that it is not necessary to list PMAPP.OBJ as a dependent of  ALL.
  6593.  NMAKE builds a dependency tree for the entire description file, and builds
  6594.  whatever files are needed to update PMAPP.EXE. So if PMAPP.C is out-ofdate
  6595.  with respect to PMAPP.OBJ, NMAKE compiles PMAPP.C to create PMAPP.OBJ, then
  6596.  links PMAPP.OBJ to create PMAPP.EXE.
  6597.  
  6598.  The same technique is suitable for description files with more than one
  6599.  top-level target. List all of the top-level targets as dependents of  ALL:
  6600.  
  6601.    ALL : pmapp.exe second.exe another.exe
  6602.  
  6603.  The example updates the targets PMAPP.EXE, SECOND.EXE, and ANOTHER.EXE.
  6604.  
  6605.  If the description file lists a single, top-level target, you can use an
  6606.  even simpler technique. Move the top-level block to the beginning of the
  6607.  file:
  6608.  
  6609.    pmapp.exe : pmapp.obj pmapp.def
  6610.       LINK pmapp, /align:16, NUL, os2, pmapp
  6611.  
  6612.    pmapp.obj : pmapp.c
  6613.       CL /c /G2sw /W3 pmapp.c
  6614.  
  6615.  NMAKE updates the second target (PMAPP.OBJ) whenever needed to keep the
  6616.  first target (PMAPP.EXE) current.
  6617.  
  6618.  
  6619.  
  6620.  
  6621.  
  6622.  
  6623.  Chapter 7  Creating Help Files with HELPMAKE
  6624.  ────────────────────────────────────────────────────────────────────────────
  6625.  
  6626.  If you have used PWB or other Microsoft language products such as QuickC,
  6627.  you are familiar with the many advantages of on-line help. The Microsoft
  6628.  Help-File-Creation Utility (HELPMAKE) allows you to create your own help
  6629.  files for use with Microsoft products. It also allows you to customize the
  6630.  help files supplied with Microsoft language products.
  6631.  
  6632.  HELPMAKE translates help text files into a help database accessible from
  6633.  within the following:
  6634.  
  6635.  
  6636.    ■   Microsoft C 6.0 Programmer's WorkBench (PWB)
  6637.  
  6638.    ■   QuickHelp Utility
  6639.  
  6640.    ■   Microsoft Editor 1.02
  6641.  
  6642.    ■   Microsoft QuickC 2.0
  6643.  
  6644.    ■   Microsoft QuickPascal 1.0
  6645.  
  6646.    ■   Microsoft QuickBASIC 4.5
  6647.  
  6648.  
  6649.  This chapter describes how to create and modify help files using the
  6650.  HELPMAKE utility.
  6651.  
  6652.  
  6653.  7.1  Structure and Contents of a Help Database
  6654.  
  6655.  HELPMAKE creates a help database from one or more input files that contain
  6656.  information formatted for the help system. This section defines some of the
  6657.  terms involved in formatting and outlines the formats that HELPMAKE can
  6658.  process.
  6659.  
  6660.  
  6661.  7.1.1  Contents of a Help File
  6662.  
  6663.  As you might expect, each help text file starts with a topic and some
  6664.  information about the topic, then lists another topic and some information
  6665.  about it, and so on. In HELPMAKE terminology, topics are called "contexts";
  6666.  the information is called "topic text."
  6667.  
  6668.  The .context command introduces a context. In the source file for C 6.0
  6669.  help, for example, this line introduces help for the open function:
  6670.  
  6671.    .context open
  6672.  
  6673.  The .context command and other formatting elements are described in Section
  6674.  7.5, "Help Text Conventions."
  6675.  
  6676.  Whether a context is one or several words depends on the application.
  6677.  QuickBASIC, for example, considers spaces to be delimiters, so in QuickBASIC
  6678.  help files contexts are limited to a single word. Other applications, such
  6679.  as the Microsoft Editor, can handle contexts that span several words. Either
  6680.  way, the application simply hands the context to an internal "help engine,"
  6681.  which searches the database for information.
  6682.  
  6683.  Often, especially with library routines, the same information applies to
  6684.  more than one subject. For example, the string-to-number functions strtod,
  6685.  strtol, and stroul share the same help text. The help file lists all three
  6686.  function names as contexts for one block of topic text. The converse,
  6687.  however, is not true. You cannot specify different blocks of topic text, in
  6688.  different places in the help file, to describe a single subject.
  6689.  
  6690.   Cross-references help you navigate through  a help database.
  6691.  
  6692.  Cross-references make it possible to view information about related topics,
  6693.  including header files and code examples. The help for the open function,
  6694.  for example, references the access function and the ASCII header file
  6695.  FCNTL.H. Cross-references can point to other contexts in the same help
  6696.  database, to contexts in other help databases, or to ASCII files outside the
  6697.  database.
  6698.  
  6699.  Help files can have two kinds of cross-references:
  6700.  
  6701.  
  6702.    ■   Implicit
  6703.  
  6704.    ■   Explicit, or hyperlinks
  6705.  
  6706.  
  6707.   Implicit cross-references  are coded with an ordinary .context command.
  6708.  
  6709.  The word "open" is an implicit cross-reference throughout C 6.0 help. If you
  6710.  select the word "open" anywhere in C 6.0 help, the help system displays
  6711.  information on the open function. As illustrated above, the context for open
  6712.  begins with an ordinary .context command. As a result, anywhere that you
  6713.  select "open," the help system references this context.
  6714.  
  6715.   Hyperlinks are explicit cross-references marked  by invisible text.
  6716.  
  6717.  A "hyperlink" is an explicit cross-reference tied to a word or phrase at a
  6718.  specific location in the help file. You create hyperlinks when you write the
  6719.  help text. The hyperlink consists of a word or phrase followed by invisible
  6720.  text that gives the context to which the hyperlink refers.
  6721.  
  6722.  For example, to cause an instance of the word "formatting" to display help
  6723.  on the printf function, you would create an explicit cross-reference from
  6724.  the word "formatting" to the context "printf." Elsewhere in the file,
  6725.  "formatting" has no special significance but, at that one position, it
  6726.  references the help for printf.  Section 7.5.4 describes how to create
  6727.  hyperlinks.
  6728.  
  6729.   Formatting flags let you change the appearance of text.
  6730.  
  6731.  Help text can also include formatting flags to control the appearance of the
  6732.  text on the screen. Using these flags, you can make certain words appear in
  6733.  various colors, inverse video, and so forth, depending on the application
  6734.  displaying help and the graphics capabilities of the host computer.
  6735.  
  6736.  
  6737.  7.1.2  Help File Formats
  6738.  
  6739.  You can create help files using any of three formats:
  6740.  
  6741.  
  6742.    ■   QuickHelp format
  6743.  
  6744.    ■   Rich Text Format (RTF)
  6745.  
  6746.    ■   Minimally formatted ASCII
  6747.  
  6748.  
  6749.  In addition, you can reference unformatted ASCII files, such as include
  6750.  files, from within a help database.
  6751.  
  6752.  An entire help system (such as the one supplied with Microsoft C or
  6753.  QuickBASIC) can use any combination of files formatted with different format
  6754.  types. With C, for example, the README.DOC information file is encoded as
  6755.  minimally formatted ASCII; the help files for the PWB, C language, and
  6756.  run-time library are encoded in the QuickHelp format. The database also
  6757.  cross-references the header (include) files, which are unformatted ASCII
  6758.  files stored outside the database.
  6759.  
  6760.  
  6761.  QuickHelp
  6762.  
  6763.  QuickHelp format is the default and is the format into which HELPMAKE
  6764.  decodes help databases. Use any text editor to create a QuickHelp-format
  6765.  help text file. QuickHelp format also lends itself to a relatively easy
  6766.  automated translation from other document formats.
  6767.  
  6768.  QuickHelp files can contain any kind of cross-reference or formatting
  6769.  attribute. Typically, you use QuickHelp format for any changes to a database
  6770.  supplied by Microsoft.
  6771.  
  6772.  
  6773.  RTF
  6774.  
  6775.  Rich Text Format (RTF) is a Microsoft word-processing format that many other
  6776.  word processors also support. You can create RTF help text with any word
  6777.  processor that generates RTF output. You can also use any utility program
  6778.  that takes word-processor output and produces an RTF file.
  6779.  
  6780.  Use RTF when you want to transfer help files from one application to another
  6781.  while retaining formatting information. You can format RTF files directly
  6782.  with the word-processing program; you need not edit them to insert any
  6783.  special commands or tags. Like QuickHelp files, RTF files can contain
  6784.  formatting attributes and cross-references.
  6785.  
  6786.  
  6787.  Minimally Formatted ASCII
  6788.  
  6789.  Minimally formatted ASCII files simply define contexts and their topic text.
  6790.  These files cannot contain screen-formatting commands or explicit
  6791.  crossreferences (implicit cross-references are allowed). They are often used
  6792.  to display text such as README.DOC and small help files that do not require
  6793.  compression.
  6794.  
  6795.  
  6796.  Unformatted ASCII
  6797.  
  6798.  Unformatted ASCII files are exactly what their name implies: regular ASCII
  6799.  files with no special formatting commands, context definitions, or special
  6800.  information. An unformatted ASCII file does not become part of the help
  6801.  database. Only its name is used as the object of a cross-reference. The
  6802.  standard C header (include) files are unformatted ASCII files used for
  6803.  cross-references by the help system for the C run-time library. Unformatted
  6804.  ASCII files are also useful for storing program examples.
  6805.  
  6806.  
  6807.  7.2  Invoking HELPMAKE
  6808.  
  6809.  The HELPMAKE program can encode or decode help files, allowing you to create
  6810.  new help files or modify existing ones. Encoding converts a text file to a
  6811.  compressed help database. HELPMAKE can encode text files written in
  6812.  QuickHelp, RTF, and minimally formatted ASCII format. Decoding converts a
  6813.  help database to a text file for editing. HELPMAKE always decodes a help
  6814.  database into a QuickHelp format text file.
  6815.  
  6816.  Invoke HELPMAKE with the following syntax:
  6817.  
  6818.    HELPMAKE «options» { /En | /D } { sourcefiles }
  6819.  
  6820.  The options modify the action of HELPMAKE; they are described in Section
  6821.  7.3.
  6822.  
  6823.   Use the /E option to encode with HELPMAKE and use the /D option to decode.
  6824.  
  6825.  
  6826.  You must supply either the /E (encode) or the /D (decode) option. When
  6827.  encoding (/E) to create a help database, you must use the /O option to
  6828.  specify the file name of the database.
  6829.  
  6830.  The sourcefile field is required. It specifies the input file for HELPMAKE.
  6831.  If you use the /D (decode) option, sourcefile can be one or more help
  6832.  database files (such as QC.HLP). HELPMAKE decodes the database files into a
  6833.  single text file. If you use the /E (encode) option, sourcefile can be one
  6834.  or more help text files (such as QC.SRC). Separate file names with a space.
  6835.  Standard wild-card characters can also be used.
  6836.  
  6837.  The example below invokes HELPMAKE with the /V, /E, and /O options (see
  6838.  Section 7.3.1, "Options for Encoding"). HELPMAKE reads input from the text
  6839.  file  my.txt  and writes the compressed help database in the file  my.hlp.
  6840.  The /E option causes maximum compression. Note that the DOS redirection
  6841.  symbol (>) sends a log of HELPMAKE activity to the file  my.log. You may
  6842.  find it helpful to redirect the log file because, in its more verbose modes
  6843.  (given by /V), HELPMAKE may generate a lengthy log.
  6844.  
  6845.    HELPMAKE /V /E /Omy.hlp my.txt > my.log
  6846.  
  6847.  The example below invokes HELPMAKE to decode the help database  my.hlp  into
  6848.  the text file  my.src, given with the /O option. Once again, the /V option
  6849.  results in verbose output, and the output is directed to the log file
  6850.  my.log. Section 7.3.2 describes additional options for decoding.
  6851.  
  6852.    HELPMAKE /V /D /Omy.src my.hlp > my.log
  6853.  
  6854.  
  6855.  7.3  HELPMAKE Options
  6856.  
  6857.  HELPMAKE accepts a number of command-line options, which are described
  6858.  below. You can specify options in uppercase or lowercase letters, and
  6859.  precede them with either a forward slash ( / ) or a dash (-). For example,
  6860.  -L, /L, -l, and /l all represent the same option. Most options apply only to
  6861.  encoding; others apply only to decoding; and a few apply to both.
  6862.  
  6863.  
  6864.  7.3.1  Options for Encoding
  6865.  
  6866.  When you encode a file─that is, when you build a help database─you must
  6867.  specify the /E option. In addition, you can supply various other options
  6868.  that control the way HELPMAKE works. All the options that apply when
  6869.  encoding are listed below:
  6870.  
  6871.  Option                            Action
  6872.  ────────────────────────────────────────────────────────────────────────────
  6873.  /Ac                               Specifies c as an application-specific
  6874.                                    control character for the help database
  6875.                                    file. The character marks a line that
  6876.                                    contains special information for
  6877.                                    internal use by the application. For
  6878.                                    example, QuickC uses the colon (:).
  6879.  
  6880.  /C                                Indicates that the context strings for
  6881.                                    this help file are case sensitive. At
  6882.                                    run time, all searches for help topics
  6883.                                    are case sensitive if the help database
  6884.                                    was built with the /C option in effect.
  6885.  
  6886.  /E«n»                             Creates (encodes) a help database from a
  6887.                                    specified text file. The optional n
  6888.                                    indicates the amount of compression to
  6889.                                    take place. If n is omitted, HELPMAKE
  6890.                                    compresses the file as much as possible,
  6891.                                    thereby reducing the size of the file by
  6892.                                    about 50%. The more compression
  6893.                                    requested, the longer HELPMAKE takes to
  6894.                                    create a database file. The value of n
  6895.                                    is a number in the range 0 - 15. It is
  6896.                                    the sum of successive powers of 2
  6897.                                    representing various compression
  6898.                                    techniques, as listed below:
  6899.  
  6900.                                    Value       Technique
  6901.  ────────────────────────────────────────────────────────────────────────────
  6902.                                    0           No compression
  6903.  
  6904.                                    1           Run-length compression
  6905.  
  6906.                                    2           Key word compression
  6907.  
  6908.                                    4           Extended key word
  6909.                                                compression
  6910.  
  6911.                                    8           Huffman compression
  6912.  
  6913.                                    Add values to combine compression
  6914.                                    techniques. For example, use /E3 to get
  6915.                                    run-length and key word compression.
  6916.                                    This is useful in the testing stages of
  6917.                                    creating a help database when you need
  6918.                                    to create the database quickly and are
  6919.                                    not too concerned with size.
  6920.  
  6921.  /H                                Displays a summary of HELPMAKE syntax
  6922.                                    and exits.
  6923.  
  6924.  /HELP                             Invokes QH.EXE, the QuickHelp utility,
  6925.                                    for help about HELPMAKE. If QuickHelp is
  6926.                                    not available, displays the same
  6927.                                    information as the /H option.
  6928.  
  6929.  /K filename                       Optimizes key word compression by
  6930.                                    supplying a
  6931.                                    list of characters that act as word
  6932.                                    separators. The filename is a file
  6933.                                    containing your list of separator
  6934.                                    characters.
  6935.  
  6936.                                    When you select key word compression,
  6937.                                    HELPMAKE scans the help file to identify
  6938.                                    "key words." A key word is any word that
  6939.                                    occurs often enough to justify replacing
  6940.                                    it with a shorter character sequence.
  6941.                                    HELPMAKE normally uses the following
  6942.                                    characters as word separators:
  6943.  
  6944.                                    ■ All characters from 0-32 (including
  6945.                                    the space)
  6946.  
  6947.                                    ■ !"#&'( )*+'-, /:;<=>?@[\]^_`{|}~
  6948.  
  6949.                                    ■ 127
  6950.  
  6951.                                    When performing key word compression,
  6952.                                    HELPMAKE treats as a word any series of
  6953.                                    characters not appearing in the
  6954.                                    separator list.
  6955.  
  6956.                                    Depending on the content of your help
  6957.                                    file, you may be able to improve key
  6958.                                    word compression by using the /K option
  6959.                                    to specify a different list of separator
  6960.                                    characters. For instance, the default
  6961.                                    separator list contains the number sign
  6962.                                    (#). If your help file contains #include
  6963.                                    directives, HELPMAKE normally treats
  6964.                                    #include as the word include without a
  6965.                                    number sign. To cause HELPMAKE to treat
  6966.                                    #include as a word, you could specify
  6967.                                    the following separator list:
  6968.  
  6969.  
  6970.                                    !"&'()*+'-,/:;<=>?@[\]^_`{|}~
  6971.  
  6972.                                    The list above does not include the
  6973.                                    number sign. HELPMAKE always treats
  6974.                                    characters in the range
  6975.                                    0-32 as separators, so you do not need
  6976.                                    to include them. Your list must include
  6977.                                    all the other characters you want
  6978.                                    HELPMAKE to use as separators, including
  6979.                                    the space.
  6980.  
  6981.  /L                                Locks the generated file so that it
  6982.                                    cannot be decoded by HELPMAKE at a later
  6983.                                    time.
  6984.  
  6985.  /Odestfile                        Specifies destfile as the name of the
  6986.                                    help database.
  6987.  
  6988.  /Sn                               Specifies the type of input file,
  6989.                                    according to the following n values:
  6990.  
  6991.                                    Option      File Type
  6992.  ────────────────────────────────────────────────────────────────────────────
  6993.                                    /S1         Rich Text Format (RTF)
  6994.  
  6995.                                    /S2         QuickHelp (default)
  6996.  
  6997.                                    /S3         Minimally formatted ASCII
  6998.  
  6999.  /T                                Translates dot commands into internal
  7000.                                    format. If your help file contains dot
  7001.                                    commands other than .context, you should
  7002.                                    supply this option when encoding it. Dot
  7003.                                    commands are described in Section 7.6.1,
  7004.                                    "QuickHelp Format," and in later
  7005.                                    sections.
  7006.  
  7007.  /V«n»                             Indicates the verbosity of diagnostic
  7008.                                    and informational output, depending on
  7009.                                    the value of n. Increasing the value
  7010.                                    adds more information to the output. If
  7011.                                    you omit this option or specify only /V,
  7012.                                    HELPMAKE gives you its most verbose
  7013.                                    output. The possible values of n are
  7014.                                    listed below:
  7015.  
  7016.                                    Option      Effect
  7017.  ────────────────────────────────────────────────────────────────────────────
  7018.                                    /V          Maximum diagnostic output
  7019.  
  7020.                                    /V0         No diagnostic output and no
  7021.                                                banner
  7022.  
  7023.                                    /V1         Prints only HELPMAKE banner
  7024.                                                (default)
  7025.  
  7026.                                    /V2         Prints pass names
  7027.  
  7028.                                    /V3         Prints contexts on first
  7029.                                                pass
  7030.  
  7031.                                    /V4         Prints contexts on each pass
  7032.  
  7033.                                    /V5         Prints any intermediate
  7034.                                                steps within each pass
  7035.  
  7036.                                    /V6         Prints statistics on help
  7037.                                                file and compression
  7038.  
  7039.  /Wwidth                           Indicates the fixed width of the
  7040.                                    resulting help text in number of
  7041.                                    characters. The values of width can
  7042.                                    range from 11 to 255. If the /W option
  7043.                                    is omitted, the default is 76. When
  7044.                                    encoding RTF source (/S1), HELPMAKE
  7045.                                    automatically formats the text to width.
  7046.                                    When encoding QuickHelp (/S2) or
  7047.                                    minimally formatted ASCII (/S3) files,
  7048.                                    HELPMAKE truncates lines to this width.
  7049.  
  7050.  
  7051.  7.3.2  Options for Decoding
  7052.  
  7053.  To decode a help database into QuickHelp files, you must use the /D option.
  7054.  In addition, HELPMAKE accepts other options to control the decoding process.
  7055.  The list below shows all the options that are valid when decoding:
  7056.  
  7057.  Option                            Action
  7058.  ────────────────────────────────────────────────────────────────────────────
  7059.  /D«letter»                        Decodes the input file into its original
  7060.                                    text or component parts. If a
  7061.                                    destination file is not specified with
  7062.                                    the /O option, the help file is decoded
  7063.                                    to stdout. HELPMAKE decodes the file
  7064.                                    differently depending on the letter
  7065.                                    specified:
  7066.  
  7067.                                    Letter      Effect
  7068.  ────────────────────────────────────────────────────────────────────────────
  7069.                                    /D          "Decode." Fully decodes the
  7070.                                                help database, leaving all
  7071.                                                cross-references and
  7072.                                                formatting information
  7073.                                                intact.
  7074.  
  7075.                                    /DS         "Decode split." Splits the
  7076.                                                concatenated, compressed
  7077.                                                help database into its
  7078.                                                components using their
  7079.                                                original names. If the
  7080.                                                database was created without
  7081.                                                concatenation (the default),
  7082.                                                HELPMAKE simply copies it to
  7083.                                                a file with its original
  7084.                                                name. No decompression
  7085.                                                occurs.
  7086.  
  7087.                                    /DU         "Decode unformatted."
  7088.                                                Decompresses the database
  7089.                                                and removes all screen
  7090.                                                formatting and
  7091.                                                cross-references. The output
  7092.                                                can still be used later for
  7093.                                                input and recompression, but
  7094.                                                all screen formatting and
  7095.                                                cross-references are lost.
  7096.  
  7097.  /H                                Displays a summary of HELPMAKE syntax
  7098.                                    and exits without encoding or decoding
  7099.                                    any files.
  7100.  
  7101.  /HELP                             Invokes QH.EXE, the QuickHelp utility,
  7102.                                    for information about HELPMAKE. If
  7103.                                    QuickHelp is not available, displays the
  7104.                                    same information as the /H option.
  7105.  
  7106.  /Odestfile                        Specifies destfile for the decoded
  7107.                                    output from HELPMAKE. If destfile is
  7108.                                    omitted, the help database is decoded to
  7109.                                    stdout. HELPMAKE always decodes help
  7110.                                    database files into QuickHelp format.
  7111.  
  7112.  /T                                Translates dot commands from internal
  7113.                                    format into dot-command format. You
  7114.                                    should always supply this option when
  7115.                                    decoding a help database that contains
  7116.                                    dot commands other than .context.
  7117.  
  7118.  /V«n»                             Indicates the verbosity of diagnostic
  7119.                                    and informational output depending on
  7120.                                    the value of n. The possible values are
  7121.                                    listed below. If you omit this option or
  7122.                                    specify only /V, HELPMAKE gives you its
  7123.                                    most verbose output.
  7124.  
  7125.                                    Option      Effect
  7126.  ────────────────────────────────────────────────────────────────────────────
  7127.                                    /V          Maximum diagnostic output
  7128.  
  7129.                                    /V0         No diagnostic output and no
  7130.                                                banner
  7131.  
  7132.                                    /V1         Prints only the HELPMAKE
  7133.                                                banner
  7134.  
  7135.                                    /V2         Prints pass names
  7136.  
  7137.                                    /V3         Prints contexts on first
  7138.                                                pass
  7139.  
  7140.  
  7141.  7.4  Creating a Help Database
  7142.  
  7143.  You can create a Microsoft-compatible help database by either of two
  7144.  methods.
  7145.  
  7146.  The first method is to decompress an existing help database, modify the
  7147.  resulting help text file, and recompress the help text file to form a new
  7148.  database.
  7149.  
  7150.  The second and simpler method is to append a new help database to an
  7151.  existing help database. This method involves the following steps:
  7152.  
  7153.  
  7154.    1.  Create a help text file in QuickHelp format, RTF, or minimally
  7155.        formatted ASCII.
  7156.  
  7157.    2.  Use HELPMAKE to create a help database file. The example below invokes
  7158.        HELPMAKE, using SAMPLE.TXT as the input file and producing a help
  7159.        database file named  sample.hlp:
  7160.  
  7161.        HELPMAKE /V /E /Osample.hlp sample.txt > sample.log
  7162.  
  7163.  
  7164.    3.  Make a backup copy of the existing database file (for safety's sake).
  7165.  
  7166.    4.  Append the new help database file to the existing help database. The
  7167.        example below concatenates the new database  sample.hlp  onto the end
  7168.        of the CLANG.HLP database:
  7169.  
  7170.        COPY clang.hlp /b + sample.hlp /b
  7171.  
  7172.  
  7173.    5.  Test the database. The  sample.hlp  database contains the context
  7174.        sample. If you type the word "sample" in the PWB and request help on
  7175.        it, the help window displays the text associated with the context
  7176.        sample.
  7177.  
  7178.  
  7179.  
  7180.  7.5  Help Text Conventions
  7181.  
  7182.  Microsoft help databases have a common structure and follow certain
  7183.  organizational conventions. You should follow the same conventions to create
  7184.  Microsoft-compatible help files.
  7185.  
  7186.  
  7187.  7.5.1  Structure of the Help Text File
  7188.  
  7189.  The help-retrieval capability that is built into Microsoft products is
  7190.  simply a data-retrieval tool. It imposes no restrictions on the content and
  7191.  format of the help text. The HELPMAKE utility and the display routines built
  7192.  into Microsoft language environments, however, make certain assumptions
  7193.  about the format of help text. This section provides some guidelines for
  7194.  creating help text files compatible with those assumptions.
  7195.  
  7196.  In all three help text formats, the help text source file is a sequence of
  7197.  topics, each preceded by one or more unique context definitions. The
  7198.  following list specifies the various formats and the corresponding context
  7199.  definition statements:
  7200.  
  7201.  Format                            Context Definition
  7202.  ────────────────────────────────────────────────────────────────────────────
  7203.  QuickHelp                         .context context
  7204.  
  7205.  RTF                               \ par >>context \ par
  7206.  
  7207.  Minimally formatted               >>context
  7208.  
  7209.  ASCII                             (none)
  7210.  
  7211.  In QuickHelp format, each topic begins with one or more .context statements
  7212.  that define the context strings that map to the topic text. Subsequent lines
  7213.  up to the next .context statement constitute the topic text.
  7214.  
  7215.  In RTF format, each context definition must be in a paragraph of its own
  7216.  (denoted by \ par), beginning with the help delimiter (>>). Subsequent
  7217.  paragraphs up to the next context definition constitute the topic text.
  7218.  
  7219.  In minimally formatted ASCII, each context definition must be on a separate
  7220.  line, and each must begin with the help delimiter (>>). As in RTF and
  7221.  QuickHelp files, subsequent lines up to the next context definition
  7222.  constitute the topic text.
  7223.  
  7224.  See Section 7.6, "Using Help Database Formats," for detailed information
  7225.  about these three formats.
  7226.  
  7227.  
  7228.  7.5.2  Local Contexts
  7229.  
  7230.  Context strings that begin with an "at" sign (@) are defined as "local" and
  7231.  have no implicit cross-references. They are used in cross-references instead
  7232.  of the context string that otherwise is generated.
  7233.  
  7234.  When you use a local context, HELPMAKE does not generate a global context
  7235.  string (a context string that is known throughout the help file). Instead,
  7236.  it embeds an encoded cross-reference that has meaning only within the
  7237.  current context. For example,
  7238.  
  7239.    .context normal
  7240.    This is a normal topic, accessible by the context string "normal."
  7241.    [button\v@local\v] is a cross-reference to the following topic.
  7242.  
  7243.    .context @local
  7244.  
  7245.    This topic can be reached only if the user browses
  7246.    sequentially through the file or uses the cross-reference
  7247.    in the previous topic.
  7248.  
  7249.  In the example above, the text  [button\v@local\v]  defines  local  as a
  7250.  local context. If the user selects the text  [button]  or scrolls through
  7251.  the file, the help system displays the topic text that follows the context
  7252.  definition for  local. Because  local  is defined with the "at" sign (@), it
  7253.  can be accessed only by a hyperlink within the help file or by sequentially
  7254.  browsing through the file. Making a context local saves file space and
  7255.  speeds access.
  7256.  
  7257.  
  7258.  7.5.3  Context Prefixes
  7259.  
  7260.  Microsoft help databases use several context prefixes. A "context prefix" is
  7261.  a single letter followed by a period. It appears before a context string
  7262.  that has a predefined meaning. If you decode a Microsoft help database, many
  7263.  of these contexts may appear in the resulting text file.
  7264.  
  7265.   Most context prefixes are internal.
  7266.  
  7267.  Except for the h. prefix, which is described below, context prefixes are
  7268.  internal. You do not need to add them in help files that you write.
  7269.  
  7270.  You can use the h. prefix to identify standard help-file contexts. For
  7271.  instance, h.default identifies the default help screen: the screen that
  7272.  normally appears when you select "top-level" help. Table 7.1 lists the
  7273.  standard h. contexts.
  7274.  
  7275.  Table 7.1  Standard h. Contexts
  7276.  
  7277. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  7278.  Context                           Description
  7279.  ────────────────────────────────────────────────────────────────────────────
  7280.  h.contents                        The table of contents for the help file.
  7281.                                    You should also define the string
  7282.                                    "contents" for direct reference to this
  7283.                                    context.
  7284.  
  7285.  h.default                         The default help screen, typically
  7286.                                    displayed when the user presses SHIFT+F1
  7287.                                    at the "top level" in most applications.
  7288.                                    The contents are generally devoted to
  7289.  Context                           Description
  7290.  ────────────────────────────────────────────────────────────────────────────
  7291.                                   The contents are generally devoted to
  7292.                                    information about using help.
  7293.  
  7294.  h.index                           The index for the help file.  You can
  7295.                                    also define the string "index" for
  7296.                                    direct reference to this context.
  7297.  
  7298.  h.notfound                        The help text that is displayed when the
  7299.                                    help system cannot find information
  7300.                                    about the requested context. The text
  7301.                                    could be an index of contexts, a topical
  7302.                                    list, or general information about using
  7303.                                    help.
  7304.  
  7305.  h.pg#                             A specific page within the help file.
  7306.                                    This is used in response to a "go to
  7307.                                    page #" request.
  7308.  
  7309.  h.pg$                             The help text that is logically last in
  7310.  Context                           Description
  7311.  ────────────────────────────────────────────────────────────────────────────
  7312. h.pg$                             The help text that is logically last in
  7313.                                    the file. This is used by some
  7314.                                    applications in response to a "go to the
  7315.                                    end" request made within the help window.
  7316.  
  7317.  h.pg1                             The help text that is logically first in
  7318.                                    the file. This is used by some
  7319.                                    applications in response to a "go to the
  7320.                                    beginning" request made within the help
  7321.                                    window.
  7322.  
  7323.  h.title                           The title of the help database.
  7324.  
  7325.  ────────────────────────────────────────────────────────────────────────────
  7326.  
  7327.  
  7328.  
  7329.  The context prefixes in Table 7.2 are internal to Microsoft products. They
  7330.  appear in decompressed databases, but you do not need to use them.
  7331.  
  7332.  Table 7.2  Microsoft Product Context Prefixes
  7333.  
  7334. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  7335.  Prefix                            Purpose
  7336.  ────────────────────────────────────────────────────────────────────────────
  7337.  d.                                Dialog box. Each dialog box is assigned
  7338.                                    a number. Its help context string is d.
  7339.                                    followed by the number (for example,
  7340.                                    d.12).
  7341.  
  7342.  e.                                Error number. If a product supports the
  7343.                                    error-numbering scheme used by Microsoft
  7344.                                    languages, it displays help for each
  7345.                                    error using this prefix. For example,
  7346.                                    the context  e.c1234  refers to the C
  7347.                                    compiler error message number C1234.
  7348.  
  7349.  m.                                Menu item. Contexts that relate to
  7350.                                    product menu items are defined by their
  7351.                                    accelerator keys. For example, the Exit
  7352.  Prefix                            Purpose
  7353.  ────────────────────────────────────────────────────────────────────────────
  7354.                                   accelerator keys. For example, the Exit
  7355.                                    selection on the FILE menu item is
  7356.                                    accessed by ALT+F X and is referenced in
  7357.                                    help by  m.f.x.
  7358.  
  7359.  n.                                Message number. Each message box is
  7360.                                    assigned a number. Its help context
  7361.                                    string is n. plus the number (for
  7362.                                    example,  n.5 ).
  7363.  
  7364.  ────────────────────────────────────────────────────────────────────────────
  7365.  
  7366.  
  7367.  
  7368.  
  7369.  7.5.4  Hyperlinks
  7370.  
  7371.  Explicit cross-references, or hyperlinks, in the help text file are marked
  7372.  with invisible text. A hyperlink comprises a word or phrase followed by
  7373.  invisible text that gives the context to which the hyperlink refers.
  7374.  
  7375.  The keystroke that activates the hyperlink depends on the application.
  7376.  Consult the documentation for each product to find the specific keystroke
  7377.  needed.
  7378.  
  7379.  When the user activates the hyperlink, the help system displays the topic
  7380.  named by the invisible text. The invisible cross-reference text is formatted
  7381.  as one of the following:
  7382.  
  7383.  Hyperlink Text                    Action
  7384.  ────────────────────────────────────────────────────────────────────────────
  7385.  contextstring                     Causes the help topic associated with
  7386.                                    contextstring to be displayed. For
  7387.                                    example,  exeformat  results in the
  7388.                                    display of the help topic associated
  7389.                                    with the context  exeformat.
  7390.  
  7391.  filename!                         Treats filename as a single topic to be
  7392.                                    displayed. For example,
  7393.                                    $INCLUDE:stdio.h!  searches the
  7394.                                    INCLUDE environment variable for file
  7395.                                    STDIO.H and displays it as a single help
  7396.                                    topic.
  7397.  
  7398.  filename!contextstring            Works the same way as contextstring
  7399.                                    above, except that only the help file
  7400.                                    filename is searched for the context. If
  7401.                                    the file is not already open, the help
  7402.                                    system finds it (by searching either the
  7403.                                    current path or an explicit environment
  7404.                                    variable) and opens it. For example,
  7405.                                    $BIN:readme.doc!patches  searches for
  7406.                                    readme.doc  in the BIN environment
  7407.                                    variable and displays the topic
  7408.                                    associated with  patches.
  7409.  
  7410.  In the following example, the word  Example  is a hyperlink:
  7411.  
  7412.    \bSee also:\p   \uExample\p\vopen.ex\v
  7413.  
  7414.  The hyperlink refers to  open.ex. If you select any of the letters of
  7415.  Example, the help system displays the topic whose context is  open.ex. On
  7416.  the screen, this line appears as follows:
  7417.  
  7418.    See also:   Example
  7419.  
  7420.  An application might display  See also:  and  Example  in different colors
  7421.  or character types, depending on such factors as your default color
  7422.  selection and type of monitor.
  7423.  
  7424.  When a hyperlink needs to cross-reference more than one word, you must use
  7425.  an anchor, as in the following example:
  7426.  
  7427.    \bSee also:\p   \uExample\p\vprintf.ex\v, fprintf, scanf, sprintf,
  7428.    vfprintf, vprintf, vsprintf
  7429.                \aformatting table\vprintf.table\v
  7430.  
  7431.  This part of the example is an anchored hyperlink:
  7432.  
  7433.    \aformatting table\vprintf.table\v
  7434.  
  7435.   Anchored hyperlinks must fit on a single line.
  7436.  
  7437.  The \ a flag creates an anchor for the cross-reference. In the example, the
  7438.  phrase following the \ a flag (formatting table) is the hyperlink. It refers
  7439.  to the context  printf.table. The first \v flag marks both the end of the
  7440.  hyperlink and the beginning of the invisible text. The name  printf.table
  7441.  is invisible; it does not appear on the screen when the help is displayed.
  7442.  The second \v flag ends the invisible text.
  7443.  
  7444.  
  7445.  7.6  Using Help Database Formats
  7446.  
  7447.  The text format of the database can be any of three types. The list below
  7448.  briefly describes these types. Sections 7.6.1-7.6.3 describe the formatting
  7449.  types in detail.
  7450.  
  7451.  An entire help system (such as the one supplied with the Professional
  7452.  Development System or QuickC) can use any combination of files formatted
  7453.  with different format types. With C, for example, the README.DOC information
  7454.  file is encoded as minimally formatted ASCII; and the help files for the C
  7455.  language and run-time library are encoded in the QuickHelp format. The
  7456.  database also cross-references the header (include) files, which are
  7457.  unformatted ASCII files stored outside the database.
  7458.  
  7459.  Type                              Characteristics
  7460.  ────────────────────────────────────────────────────────────────────────────
  7461.  QuickHelp                         Uses dot commands and embedded
  7462.                                    formatting characters (the default
  7463.                                    formatting type expected by HELPMAKE);
  7464.                                    supports highlighting, color, and
  7465.                                    cross-references. This format must be
  7466.                                    compressed before using.
  7467.  
  7468.  Minimally formatted ASCII         Uses a help delimiter (>>) to define
  7469.                                    help contexts; does not support
  7470.                                    highlighting, color, or crossreferences.
  7471.                                    This format can be compressed, but
  7472.                                    compression is not required.
  7473.  
  7474.  RTF                               Uses a subset of standard RTF; supports
  7475.                                    highlighting, color, and
  7476.                                    cross-references; supports dot commands.
  7477.                                    This format must be compressed before
  7478.                                    using.
  7479.  
  7480.  
  7481.  7.6.1  QuickHelp Format
  7482.  
  7483.  The QuickHelp format uses a dot command and embedded formatting flags to
  7484.  convey information to HELPMAKE.
  7485.  
  7486.  
  7487.  QuickHelp Dot Commands
  7488.  
  7489.  QuickHelp supports a number of dot commands, which identify topics and
  7490.  convey other topic-related information to the help system. If your help file
  7491.  contains dot commands other than .context, you must supply the /T option
  7492.  when encoding and decoding with HELPMAKE.
  7493.  
  7494.   You can define more than one context for a single topic.
  7495.  
  7496.  The most important dot command is the .context command. Every topic in a
  7497.  QuickHelp file begins with one or more .context commands. Each .context
  7498.  command defines a context string for the topic text. You can define more
  7499.  than one context for a single topic, as long as you do not place any topic
  7500.  text between them.
  7501.  
  7502.  Typical dot commands are shown below. The first defines a context for the
  7503.  #include C preprocessor directive. The second set illustrates multiple
  7504.  contexts for one block of topic text. In this case, the same topic text
  7505.  explains all of the string-to-number conversion routines in C.
  7506.  
  7507.    .context #include
  7508.           .
  7509.           .description of #include goes here
  7510.           .
  7511.    .context strtod
  7512.    .context strtol
  7513.    .context strtoul
  7514.           .
  7515.           . description of string-to-number functions goes here
  7516.           .
  7517.  
  7518.  The QuickHelp format supports several other dot commands. Table 7.3 lists
  7519.  all of the dot commands available in QuickHelp format.
  7520.  
  7521.  Table 7.3  QuickHelp Dot Commands
  7522.  
  7523. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  7524.  Command                           Action
  7525.  ────────────────────────────────────────────────────────────────────────────
  7526.  .category string                  Lists the category in which the current
  7527.                                    topic appears and its position in the
  7528.                                    list of topics. The category name is
  7529.                                    used by the QuickHelp Topic command,
  7530.                                    which brings up the list of topics to
  7531.                                    which the current topic belongs. Some
  7532.                                    applications, such as the PWB, use this
  7533.                                    name as a pointer to the applicable
  7534.                                    table of contents.
  7535.  
  7536.  .command                          Indicates that the topic text is not a
  7537.                                    displayable help topic. Use this command
  7538.                                    to hide hyperlink topics and other
  7539.                                    internal information. Hyperlink topics
  7540.                                    are described in Section 7.5.5,
  7541.  Command                           Action
  7542.  ────────────────────────────────────────────────────────────────────────────
  7543.                                   are described in Section 7.5.5,
  7544.                                    "Hyperlink Commands."
  7545.  
  7546.  .comment string                   The string is a comment that appears
  7547.                                    only in the help source file. Comments
  7548.                                    are especially useful for documenting
  7549.                                    the purpose of cross-references.
  7550.  
  7551.                                    Because comments are not inserted in the
  7552.                                    help database, they are not restored
  7553.                                    when you decompress a help file.
  7554.  
  7555.  .context string                   The string introduces a topic.
  7556.  
  7557.  
  7558.  
  7559.  .end                              Ends a paste section. See the .paste
  7560.                                    command below.
  7561.  
  7562.  Command                           Action
  7563.  ────────────────────────────────────────────────────────────────────────────
  7564. 
  7565.  .freeze numlines                  Indicates that the first numlines lines
  7566.                                    should be frozen as the top line of the
  7567.                                    help screen. This is normally used to
  7568.                                    freeze a row of cross-reference buttons
  7569.                                    at the top of a help topic that might be
  7570.                                    scrolled.
  7571.  
  7572.  .length topiclength               Indicates the default window size, in
  7573.                                    topiclength lines, of the topic about to
  7574.                                    be displayed. This command is always the
  7575.                                    first line in the topic if present.
  7576.  
  7577.  .list                             Indicates that the current topic
  7578.                                    contains a list of topics. QuickHelp
  7579.                                    displays a highlighted line; you can
  7580.                                    choose
  7581.                                    a topic by moving the highlighted line
  7582.                                    over the desired topic and pressing
  7583.  Command                           Action
  7584.  ────────────────────────────────────────────────────────────────────────────
  7585.                                   over the desired topic and pressing
  7586.                                    ENTER. Help searches for the first word
  7587.                                    of the line.
  7588.  
  7589.  .mark name «column»               Defines a mark immediately preceding the
  7590.                                    following line of text. This command can
  7591.                                    be used in help script commands to
  7592.                                    indicate that the display of a
  7593.                                    particular topic begins at the marked
  7594.                                    line. The name identifies the mark. The
  7595.                                    optional column value is an integer that
  7596.                                    indicates a column location within the
  7597.                                    specified line.
  7598.  
  7599.  .next context                     Tells the help system to look up the
  7600.                                    next topic using
  7601.                                    context instead of the next topic's name.
  7602.                                    You can use this command to skip large
  7603.                                    blocks of .command or .popup topics.
  7604.  Command                           Action
  7605.  ────────────────────────────────────────────────────────────────────────────
  7606.                                   blocks of .command or .popup topics.
  7607.  
  7608.  .previous context                 Tells the help system to look up the
  7609.                                    previous topic using context instead of
  7610.                                    the previous topic's name. You can use
  7611.                                    this command to skip large blocks of
  7612.                                    .command or .popup topics.
  7613.  
  7614.  .paste pastename                  Begins a paste section. The pastename
  7615.                                    appears in the QuickHelp Paste menu.
  7616.  
  7617.  .popup                            Tells the help system to display the
  7618.                                    current topic as a popup instead of a
  7619.                                    normal, scrollable topic.
  7620.  
  7621.  .ref string(s)                    Tells the help system to display the
  7622.                                    list of string topics in the Reference
  7623.                                    menu. You can list as many topics as
  7624.                                    needed; separate each additional string
  7625.  Command                           Action
  7626.  ────────────────────────────────────────────────────────────────────────────
  7627.                                   needed; separate each additional string
  7628.                                    with a comma.
  7629.  
  7630.  .topic text                       Defines text as the name or title to be
  7631.                                    displayed in place of the context string
  7632.                                    if the application help displays a title.
  7633.                                    This command is always the first line in
  7634.                                    the context unless you also use the
  7635.                                    .length command.
  7636.  
  7637.  ────────────────────────────────────────────────────────────────────────────
  7638.  
  7639.  
  7640.  
  7641.  
  7642.  QuickHelp Formatting Flags
  7643.  
  7644.  The QuickHelp format supports a number of formatting flags that are used to
  7645.  highlight parts of the help database and to mark hyperlinks in the help
  7646.  text.
  7647.  
  7648.  Each formatting flag consists of a backslash ( \ ) followed by a character.
  7649.  Table 7.4 lists the formatting flags.
  7650.  
  7651.  Table 7.4  Formatting Flags
  7652.  
  7653. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  7654.  Formatting Flag                   Action
  7655.  ────────────────────────────────────────────────────────────────────────────
  7656.  \a                                Anchors text for cross-references
  7657.  
  7658.  \b, \B                            Turns boldface on or off
  7659.  
  7660.  \i, \I                            Turns italics on or off
  7661.  
  7662.  \p, \P                            Turns off all attributes
  7663.  
  7664.  \u, \U                            Turns underlining on or off
  7665.  
  7666.  \v, \V                            Turns invisibility on or off (hides
  7667.  Formatting Flag                   Action
  7668.  ────────────────────────────────────────────────────────────────────────────
  7669. \v, \V                            Turns invisibility on or off (hides
  7670.                                    cross-references in text)
  7671.  
  7672.  \\                                Inserts a single backslash in text
  7673.  
  7674.  ────────────────────────────────────────────────────────────────────────────
  7675.  
  7676.  
  7677.  
  7678.  On monochrome monitors, text labeled with the bold, italic, and underlining
  7679.  attributes appears in various ways, depending on the application (for
  7680.  example, high intensity and reverse video are commonly displayed). On color
  7681.  monitors, these attributes are translated by the application into suitable
  7682.  colors, depending on the user's default color selections.
  7683.  
  7684.  The \b, \i, \u, and \v options are toggles, turning on and off their
  7685.  respective attributes. You can use several of these on the same text. Use
  7686.  the \p attribute to turn off all attributes. Use the \v attribute to hide
  7687.  cross-references and hyperlinks in the text.
  7688.  
  7689.  HELPMAKE truncates the lines in QuickHelp files to the width specified with
  7690.  the /W option. (See Section 7.3.1, "Options for Encoding," for more
  7691.  information.) Only visible characters count toward the character-width
  7692.  limit. Lines that begin with an application-specific control character are
  7693.  truncated to 255 characters regardless of the width specification. See
  7694.  Section 7.3.1 for more information about application-specific control
  7695.  characters.
  7696.  
  7697.  In the example below, the \b flag initiates boldface text for  Returns:, and
  7698.  the \p flag that follows the word reverts to plain text for the remainder of
  7699.  the line.
  7700.  
  7701.    \bReturns:\p    a handle if successful, or -1 if not.
  7702.                errno:  EACCES, EEXIST, EMFILE, ENOENT
  7703.  
  7704.  In the example below, \a anchors text for the hyperlink  Example . The \v
  7705.  flags define the cross-reference to be  sample_prog  and cause the text
  7706.  between the flags to be invisible. Cross-references are described in the
  7707.  following section.
  7708.  
  7709.    \aExample \vsample_prog\v
  7710.  
  7711.  
  7712.  QuickHelp Cross-References
  7713.  
  7714.  Help databases contain two types of cross-references: implicit
  7715.  cross-references and explicit cross-references. They are described in
  7716.  Section 7.1.1, "Contents of a Help File."
  7717.  
  7718.  An implicit cross-reference is any word that appears both in the topic text
  7719.  and as a context in the help file. For example, any time you request help on
  7720.  the word "close," the help window displays help on the close function. You
  7721.  don't need to code implicit cross-references in your help text files.
  7722.  
  7723.   Insert formatting flags to mark explicit cross-references.
  7724.  
  7725.  Explicit cross-references (hyperlinks) are words or phrases on the screen
  7726.  that are associated with a context. For example, the word "Example" in the
  7727.  initial help-screen area for any C function is an explicit cross-reference
  7728.  to the C program example for that function. You must insert formatting flags
  7729.  in your help text files to mark explicit cross-references.
  7730.  
  7731.  If the hyperlink consists of a single word, you can use invisible text to
  7732.  flag it in the source file. The \v formatting flag creates invisible text,
  7733.  as follows:
  7734.  
  7735.    hyperlink\vcontext\v
  7736.  
  7737.  Specify the first \v flag immediately following the word you want to use as
  7738.  the hyperlink. Following the flag, insert the context that the hyperlink
  7739.  crossreferences. The second \v flag marks the end of the context; that is,
  7740.  the end of the invisible text. HELPMAKE generates a cross-reference whose
  7741.  context is the invisible text, and whose hyperlink is the entire word.
  7742.  
  7743.  If the hyperlink consists of a phrase, rather than a single word, you must
  7744.  use anchored text to create explicit cross-references. Use the \a and \v
  7745.  flags to create anchored text as follows:
  7746.  
  7747.    \ahyperlink-words\vcontext\v
  7748.  
  7749.  The \a flag marks an anchor for the cross-reference. The text that follows
  7750.  the \a flag is the hyperlink. The hyperlink must fit entirely on one line.
  7751.  The first \v flag marks both the end of the hyperlink and the beginning of
  7752.  the invisible text that
  7753.  
  7754.  contains the cross-reference context. The second \v flag marks the end of
  7755.  the invisible text.
  7756.  
  7757.  The following example contains three implicit cross-references to the C
  7758.  routines abs, cabs, and fabs.
  7759.  
  7760.    See also: abs, cabs, fabs
  7761.  
  7762.  The following example shows the encoding for an explicit cross-reference to
  7763.  an example program and a function template from the help database for the C
  7764.  run-time library:
  7765.  
  7766.    See also: Example\vopen.ex\v, Template\vopen.tm\v, close
  7767.  
  7768.  Here, the hyperlinks are  Example  and  Template, which reference the
  7769.  contexts  open.ex  and  open.tm. The example also contains an implicit
  7770.  cross-reference to the close function.
  7771.  
  7772.  The following example shows the encoding for an explicit cross-reference to
  7773.  an entire family of functions:
  7774.  
  7775.    See also: \ais... functions\vis_functions\v, atoi
  7776.  
  7777.  The cross-reference uses anchored text to associate a phrase, rather than
  7778.  just a word, with a context. In this example, the hyperlink is the anchored
  7779.  phrase  is... functions, and it cross-references the context  is_functions.
  7780.  In addition, the example contains an implicit cross-reference to the atoi
  7781.  routine.
  7782.  
  7783.  The code below is an example in QuickHelp format that contains a single
  7784.  entry:
  7785.  
  7786.    .context open
  7787.    .length 13
  7788.    \bInclude:\p   <fcntl.h>, <io.h>, <sys\\types.h>, <sys\\stat.h>
  7789.  
  7790.    \bPrototype:\p  int open(char *path, int flag[, int mode]);
  7791.              flag:  O_APPEND O_BINARY O_CREAT O_EXCL O_RDONLY
  7792.                     O_RDWR    O_TEXT    O_TRUNC  O_WRONLY
  7793.                     (can be joined by |)
  7794.              mode:  S_IWRITE  S_IREAD   S_IREAD | S_IWRITE
  7795.  
  7796.    \bReturns:\p    a handle if successful, or -1 if not.
  7797.                errno:  EACCES, EEXIST, EMFILE, ENOENT
  7798.  
  7799.    \bSee also:\p  \uExample\p\vopen.ex\v, \uTemplate\p\vopen.tp\v,
  7800.                 access, chmod, close, creat, dup, dup2, fopen, sopen, umask
  7801.  
  7802.  The .length command near the beginning of the example specifies the size of
  7803.  the initial window for the help text. Here, the initial window displays 13
  7804.  lines.
  7805.  
  7806.  The manifest constants (such as O_WRONLY and EEXIST), the C keywords (such
  7807.  as int and char), and the other functions (such as sopen and access) are
  7808.  implicit cross-references. The words  Example  and  Template  are explicit
  7809.  cross-references to the example  open.ex  and to the open template  open.tp,
  7810.  respectively. Note the use of double backslashes in the include file names.
  7811.  
  7812.  
  7813.  
  7814.  7.6.2  Minimally Formatted ASCII Format
  7815.  
  7816.  A minimally formatted ASCII text file comprises a sequence of topics, each
  7817.  preceded by one or more unique context definitions. Each context definition
  7818.  must be on a separate line beginning with a help delimiter (>>). Subsequent
  7819.  lines up to the next context definition constitute the topic text.
  7820.  
  7821.   Minimally formatted ASCII files cannot contain highlighting.
  7822.  
  7823.  Minimally formatted ASCII files can be used in two ways. You can compress
  7824.  the file with HELPMAKE, creating a help database, or an application can
  7825.  access the uncompressed file directly. Uncompressed files are somewhat
  7826.  larger and slower to search, however. Minimally formatted ASCII files are of
  7827.  fixed width, and they cannot contain highlighting (or other nondefault
  7828.  attributes) or cross-references.
  7829.  
  7830.  The following example, coded in minimally formatted ASCII, shows the same
  7831.  text as the QuickHelp example in the previous section. The first line of the
  7832.  example defines  open  as a context string. The minimally formatted ASCII
  7833.  help file must begin with the help delimiter (>>), so that HELPMAKE or the
  7834.  application can verify that the file is indeed an ASCII help file.
  7835.  
  7836.    >>>>open
  7837.  
  7838.    Include:    <fcntl.h>, <io.h>, <sys\types.h>, <sys\stat.h>
  7839.  
  7840.    Prototype:  int open(char *path, int flag[, int mode]);
  7841.            flag:  O_APPEND  O_BINARY  O_CREAT  O_EXCL  O_RDONLY
  7842.                   O_RDWR    O_TEXT    O_TRUNC  O_WRONLY
  7843.                   (can be joined by |)
  7844.            mode:  S_IWRITE  S_IREAD   S_IREAD | S_IWRITE
  7845.  
  7846.    Returns:    a handle if successful, or -1 if not.
  7847.                errno:  EACCES, EEXIST, EMFILE, ENOENT
  7848.  
  7849.    See also:  access, chmod, close, creat, dup, dup2, fopen, sopen, umask
  7850.  
  7851.  When displayed, the help information appears exactly as it is typed into the
  7852.  file. Any formatting codes are treated as ASCII text. Note that you do not
  7853.  need to escape backslashes in minimally formatted ASCII files.
  7854.  
  7855.  If you compress minimally formatted ASCII files, they are smaller and faster
  7856.  to search.
  7857.  
  7858.  
  7859.  7.6.3  Rich Text Format (RTF)
  7860.  
  7861.  RTF is a Microsoft word-processing format supported by many other word
  7862.  processors. It allows documents to be transferred from one application to
  7863.  another without losing any formatting information. The HELPMAKE utility
  7864.  recognizes a subset of the full RTF syntax. If your file contains any RTF
  7865.  code that is not part of the subset, HELPMAKE ignores the code and strips it
  7866.  out of the file.
  7867.  
  7868.  Certain word-processing and file-conversion programs generate the RTF code
  7869.  automatically as output. You need not worry about inserting RTF codes
  7870.  yourself; you can simply format your help files directly with a
  7871.  word-processor that generates RTF, using the attributes supported by the
  7872.  subset. The only items you need to insert are the help delimiter (>>) and
  7873.  context string that start each entry.
  7874.  
  7875.  HELPMAKE recognizes the subset of RTF listed below:
  7876.  
  7877.  RTF Code                          Action
  7878.  ────────────────────────────────────────────────────────────────────────────
  7879.  \b                                Boldface. The application decides how to
  7880.                                    display this; often it is intensified
  7881.                                    text.
  7882.  
  7883.  \fi <nnn>                         Paragraph first-line indent.
  7884.  
  7885.  \i                                Italic. The application decides how to
  7886.                                    display this; often it is reverse video.
  7887.  
  7888.  \li <nnn>                         Paragraph indent from left margin.
  7889.  
  7890.  \line                             New line (not new paragraph).
  7891.  
  7892.  \par                              End of paragraph.
  7893.  
  7894.  \pard                             Default paragraph formatting.
  7895.  
  7896.  \plain                            Default attributes. On most screens this
  7897.                                    is nonblinking normal intensity.
  7898.  
  7899.  \tab                              Tab character.
  7900.  
  7901.  \ul                               Underline. The application decides how
  7902.                                    to display this; some adapters that do
  7903.                                    not support underlining display it as
  7904.                                    blue text.
  7905.  
  7906.  \v                                Hidden text. Hidden text is used for
  7907.                                    cross-reference information and for some
  7908.                                    application-specific communications; it
  7909.                                    is not displayed.
  7910.  
  7911.  Using the word-processing program, you can break the topic text into
  7912.  paragraphs. When HELPMAKE compresses the file, it formats the text to the
  7913.  width given with the / W option, ignoring the paragraph formats.
  7914.  
  7915.  As with the other text formats, each entry in the database source consists
  7916.  of one or more context strings, followed by topic text. An RTF file can
  7917.  contain QuickHelp dot commands.
  7918.  
  7919.  The help delimiter (>>) at the beginning of any paragraph denotes the
  7920.  beginning of a new help entry. The text that follows on the same line is
  7921.  defined as a context for the topic. If the next paragraph also begins with
  7922.  the help delimiter, it also defines a context string for the same topic
  7923.  text. You can define any number of contexts for a block of topic text. The
  7924.  topic text comprises all subsequent paragraphs up to the next paragraph that
  7925.  begins with the help delimiter.
  7926.  
  7927.  The code below is an example of a help database that contains a single entry
  7928.  using subset RTF text. Note that RTF uses curly braces ({}) for nesting.
  7929.  Thus, the entire file is enclosed in curly braces, as is each specially
  7930.  formatted text item.
  7931.  
  7932.    {\rtf1
  7933.    \pard >>open\par
  7934.      {\b Include:}    <fcntl.h>, <io.h>, <sys\\types.h>, <sys\\stat.h>\par
  7935.    \par
  7936.      {\b Syntax:}     int open( char * filename, int oflag[, int pmode ]
  7937.  );\par
  7938.                 oflag:  O_APPEND  O_BINARY  O_CREAT  O_EXCL  O_RDONLY\par
  7939.                         O_RDWR    O_TEXT    O_TRUNC  O_WRONLY\par
  7940.                         (may be joined by |)\par
  7941.                 pmode:  S_IWRITE  S_IREAD   S_IREAD | S_IWRITE\par
  7942.    \par
  7943.      {\b Returns:}    a handle if successful, or -1 if not.\par
  7944.                 errno:  EACCES, EEXIST, EMFILE, ENOENT\par
  7945.    \par
  7946.      {\b See also:}  Examples{\v open.ex}, access, chmod, close, creat,
  7947.  dup,\par
  7948.                 dup2, fopen, sopen, umask\par
  7949.    >>open.ex\par
  7950.    To build this help file, use the following command:\par
  7951.    \par
  7952.    HELPMAKE /S1 /E15 /OOPEN.HLP OPEN.RTF\par
  7953.    \par
  7954.  
  7955.            < Back >{\v !B}
  7956.    }
  7957.  
  7958.  Actual RTF output normally contains additional information that is not
  7959.  visible to the user; HELPMAKE ignores this extra information.
  7960.  
  7961.  
  7962.  
  7963.  
  7964.  
  7965.  
  7966.  Chapter 8  Customizing the Microsoft Programmer's WorkBench
  7967.  ────────────────────────────────────────────────────────────────────────────
  7968.  
  7969.  Designed with flexibility in mind, the Microsoft Programmer's WorkBench
  7970.  (PWB) provides a highly extensible development platform for the Microsoft C
  7971.  Professional Development System. Using PWB it is easy to change basic
  7972.  environment features such as screen colors and key assignments, and you can
  7973.  add powerful new functions of your own using macros and C-language
  7974.  extensions.
  7975.  
  7976.  This chapter explains four methods for customizing the Programmer's
  7977.  WorkBench: setting switches, assigning keystrokes, writing macros, and
  7978.  writing C extensions. While it explains customization methods, the chapter
  7979.  does not document every customizable feature of the Programmer's WorkBench.
  7980.  Use on-line help as your primary source of information about these and other
  7981.  PWB features.
  7982.  
  7983.  This chapter assumes you are familiar with basic PWB operations and
  7984.  terminology. If you are not, read "Using the Programmer's WorkBench" in
  7985.  Installing and Using the Microsoft C Professional Development System.
  7986.  
  7987.  
  7988.  8.1  Setting Switches
  7989.  
  7990.  The Programmer's WorkBench has a number of "switches," or user-configurable
  7991.  options, that control features such as screen colors. Each switch has a name
  7992.  and can be assigned a value.
  7993.  
  7994.  There are two ways to set PWB switches. The easiest way is by choosing
  7995.  Editor Settings in the Options menu. You can also edit the TOOLS.INI
  7996.  initialization file. These methods can also be used for more elaborate
  7997.  customizations, such as writing macros.
  7998.  
  7999.  
  8000.  8.1.1  Editing the <assign> Pseudofile
  8001.  
  8002.  If you choose Editor Settings in the Options menu, PWB changes to the
  8003.  <assign> pseudofile and displays it in the current window. (A pseudofile is
  8004.  constructed dynamically by PWB; it exists only in memory.) The <assign>
  8005.  file lists all the current PWB settings.
  8006.  
  8007.  To change a switch, edit the line where it appears. For instance, the
  8008.  vscroll switch controls how many lines PWB scrolls vertically; its default
  8009.  setting is 1. To change it, move to the corresponding line:
  8010.  
  8011.    vscroll:1
  8012.  
  8013.  Change the 1 to 3 and move the cursor to another line. PWB highlights the
  8014.  line to indicate the change is legal. (If you make an illegal change, PWB
  8015.  signals an error.) The change takes effect immediately: now PWB scrolls text
  8016.  three vertical lines at a time.
  8017.  
  8018.  If you don't explicitly save a change, it disappears at the end of the
  8019.  current session. You can save a change by saving <assign> as you would any
  8020.  other file (by pressing ALT+A ALT+A F2). When you exit PWB, you are asked if
  8021.  you want to save TOOLS.INI, the PWB initialization file, which records
  8022.  customizations. Answer yes (type  Y)  to save the change.
  8023.  
  8024.  You can also use this method for more elaborate customizations, such as
  8025.  writing macros (see Section 8.3, "Writing Macros"). Simply insert a few
  8026.  blank lines in <assign> and enter the new information in them. Note that PWB
  8027.  only pays attention to lines you change or add to <assign>. Deleting a line
  8028.  has no effect.
  8029.  
  8030.  
  8031.  8.1.2  Editing the TOOLS.INI Initialization File
  8032.  
  8033.  Another way to customize PWB is by editing TOOLS.INI, the initialization
  8034.  file used by PWB and other Microsoft language tools. This method is useful
  8035.  if you customize PWB extensively.
  8036.  
  8037.  While the <assign> file lists every customizable PWB item, the TOOLS.INI
  8038.  file contains lines only for items you have customized. Those items not
  8039.  mentioned in TOOLS.INI are set to a default value.
  8040.  
  8041.  
  8042.  Dividing TOOLS.INI into Sections
  8043.  
  8044.  Since several tools can use TOOLS.INI, the file may contain information that
  8045.  doesn't relate to PWB. If you customize more than one tool, TOOLS.INI is
  8046.  divided into sections, one for each tool. Each section begins with a tag
  8047.  consisting of the tool's base name enclosed in square brackets:  [PWB]  for
  8048.  PWB.EXE, [NMAKE]  for NMAKE.EXE, and so on.
  8049.  
  8050.  For example, say you set the vscroll switch to 3 and save the change, but
  8051.  you have not customized PWB in any other way. Your TOOLS.INI file will
  8052.  contain this section:
  8053.  
  8054.    [PWB]
  8055.    vscroll:3
  8056.  
  8057.  Settings following this tag are put in effect by PWB every time it starts.
  8058.  
  8059.  You can also create sections of TOOLS.INI that PWB reads only in certain
  8060.  circumstances. You can create sections for different video adapters,
  8061.  file-name extensions, and operating system versions.
  8062.  
  8063.  If you use more than one video display, TOOLS.INI can have a different
  8064.  section for each display:
  8065.  
  8066.  
  8067.    ■   [PWB-mono]
  8068.  
  8069.    ■   [PWB-cga]
  8070.  
  8071.    ■   [PWB-ega]
  8072.  
  8073.    ■   [PWB-vga]
  8074.  
  8075.  
  8076.  After each tag, you can set different screen colors, dimensions, and other
  8077.  display-specific switches.
  8078.  
  8079.  You can also create a section for files with specific extensions. For
  8080.  instance, your TOOLS.INI file could contain a section beginning with the tag
  8081.  
  8082.  
  8083.    [PWB-.C]
  8084.  
  8085.  for C source files, and
  8086.  
  8087.    [PWB-.ASM]
  8088.  
  8089.  for assembly-language (.ASM) source files. Each time you load a file with
  8090.  the designated extension, PWB reads the appropriate section of TOOLS.INI.
  8091.  For each file type, you could use a different set of macros and other
  8092.  customizations.
  8093.  
  8094.  TOOLS.INI can also contain sections specific to operating system versions.
  8095.  The following tag introduces a section specific to DOS version 3.20, for
  8096.  instance:
  8097.  
  8098.    [PWB-3.20]
  8099.  
  8100.  You can combine tags as needed. For example, the tag
  8101.  
  8102.    [PWB-3.20 PWB-10.10R]
  8103.  
  8104.  applies to DOS version 3.20 and OS/2 version 1.1 real mode.
  8105.  
  8106.  You can also create a section in TOOLS.INI containing switches for a
  8107.  userwritten extension. See Section 8.4.3, "Describing Functions and
  8108.  Switches." On-line help contains additional information about TOOLS.INI
  8109.  tags.
  8110.  
  8111.  
  8112.  8.2  Assigning Keystrokes
  8113.  
  8114.  PWB allows you to assign any editing function to almost any keystroke.
  8115.  Reassigning keystrokes doesn't change PWB graphic interface, however.
  8116.  
  8117.  Keystrokes, like switches, are listed in the <assign> pseudofile (choose Key
  8118.  Assignments in the Options menu) and can be changed there. For example, say
  8119.  you want to assign the home cursor function to the SHIFT+HOME keystroke. The
  8120.  default keystroke assignment for home is:
  8121.  
  8122.    home:ctrl+home
  8123.  
  8124.  If you change the assignment to
  8125.  
  8126.    home:shift+home
  8127.  
  8128.  SHIFT+HOME moves the cursor to the home (upper left) window position.
  8129.  
  8130.  It is legal to assign more than one keystroke to the same function. For
  8131.  example, many keystrokes invoke the select function, which selects a text
  8132.  region. Thus, the previous example adds a new keystroke (SHIFT+HOME) for the
  8133.  home function; it does not remove the previous assignment (CTRL+HOME).
  8134.  
  8135.  There are two limitations on keystroke assignments:
  8136.  
  8137.  
  8138.    ■   You can't reassign a keystroke that PWB is using for a menu. For
  8139.        instance, if ALT+F pulls down the File menu, PWB ignores any attempt
  8140.        to reassign ALT+F.
  8141.  
  8142.    ■   You can't reassign ALT plus the number keys 1 - 9 (ALT+1, ALT+2, and
  8143.        so on). These keystrokes are reserved for the file history menu items.
  8144.  
  8145.  
  8146.  Each keystroke can only invoke one function. If you mistakenly assign a
  8147.  key-stroke to more than one function, PWB uses the most recent assignment.
  8148.  For example,
  8149.  
  8150.    home:ctrl+a
  8151.    setfile:ctrl+a
  8152.  
  8153.  assigns the CTRL+A keystroke to two different functions, home and setfile.
  8154.  The second assignment overrides the first, assigning CTRL+A to setfile.
  8155.  
  8156.  Occasionally, you may want to "unassign," or disable, a keystroke. This is
  8157.  done by assigning the unassigned function to the keystroke. For example,
  8158.  
  8159.    unassigned:ctrl+a
  8160.  
  8161.  disables CTRL+A. PWB signals an error when you press any unassigned key.
  8162.  
  8163.  
  8164.  8.3  Writing Macros
  8165.  
  8166.  The fastest way to create a new editing function for PWB is to write a
  8167.  macro. The function can be as simple as inserting a long word or phrase, or
  8168.  it can perform complex tasks by invoking PWB functions and other macros.
  8169.  
  8170.  
  8171.  8.3.1  Macro Syntax
  8172.  
  8173.  A macro can contain any combination of PWB functions, literal text, and
  8174.  macro operators. You can define as many as 1,024 macros at one time.
  8175.  
  8176.   Literal text is case sensitive.
  8177.  
  8178.  Literal text is anything inside double quotes. Inside literal text, you can
  8179.  represent a double quote as  \" and a backslash as  \\. Text is case
  8180.  sensitive inside quotes and case insensitive outside them.
  8181.  
  8182.  The following macro comments out a line of C source code:
  8183.  
  8184.    comment:=begline "/* " endline " */"
  8185.    comment:alt+c
  8186.  
  8187.  The first line names the macro and tells what it does. The begline and
  8188.  endline editor functions move the cursor, while the text inside quotes is
  8189.  printed at the current cursor position. The second line assigns a keystroke
  8190.  (ALT+C) to the macro.
  8191.  
  8192.  A macro definition must fit on one logical line. If necessary, you can use
  8193.  the backslash ( \ ) to continue the definition on the next line. For
  8194.  instance, the definition
  8195.  
  8196.    comment:=begline "/* " endline " */"
  8197.  
  8198.  could be written as
  8199.  
  8200.    comment:=begline  \
  8201.    "/* " endline  \
  8202.    " */"
  8203.  
  8204.  Notice the extra space before each backslash. If you want a space between
  8205.  the end of one line and the beginning of the other, you must precede the
  8206.  backslash with two spaces.
  8207.  
  8208.  You can use the arg function to pass arguments to functions. For example,
  8209.  the following macro passes the argument  15  to the plines function (which
  8210.  scrolls text down):
  8211.  
  8212.    movedown:=arg "15" plines
  8213.  
  8214.  Because arg precedes the literal text, the text doesn't appear on the
  8215.  screen. Instead, it is passed as an argument to the next function, plines.
  8216.  The macro scrolls the current text down 15 lines.
  8217.  
  8218.  Arguments can use regular expression syntax, as well (regular expressions
  8219.  are documented in on-line help):
  8220.  
  8221.    endword:=arg arg "( !.!$!\\:!;!\\)!\\(!,)" psearch
  8222.  
  8223.  The arg arg sequence directs the psearch function to treat the text argument
  8224.  as a regular expression search pattern. This search pattern tells PWB to
  8225.  search for the next period, end of line ($), colon, semicolon, close
  8226.  parenthesis, open parenthesis, or comma.
  8227.  
  8228.  A macro can invoke other macros:
  8229.  
  8230.    lcomment:= "/* "
  8231.    rcomment:= " */"
  8232.    commentout:=begline lcomment endline rcomment
  8233.    commentout:alt+z
  8234.  
  8235.  The  commentout  macro invokes the previously defined macros  lcomment  and
  8236.  rcomment.
  8237.  
  8238.  In addition to standard PWB functions, macros can invoke user-defined
  8239.  (extension) functions. See Section 8.4, "Writing and Building C Extensions."
  8240.  
  8241.  
  8242.  
  8243.  8.3.2  Macro Responses
  8244.  
  8245.  Some PWB functions ask you for confirmation. For example, the meta exit
  8246.  (quit without saving) function normally asks if you really want to exit.
  8247.  Such questions always take the answer "yes" (y) or "no" (n).
  8248.  
  8249.  When you invoke such a function in a macro, the function assumes an answer
  8250.  of yes and does not ask for confirmation. For example, the macro definition
  8251.  
  8252.  
  8253.    quit:=meta exit
  8254.    quit:alt+x
  8255.  
  8256.  invokes meta exit when you press ALT+X. Because the meta exit function is
  8257.  invoked from a macro, PWB exits without asking for confirmation.
  8258.  
  8259.  The following operators allow you to restore normal prompting or change the
  8260.  default responses:
  8261.  
  8262.  Operator                          Description
  8263.  ────────────────────────────────────────────────────────────────────────────
  8264.  <                                 Asks for confirmation; if not followed
  8265.                                    by another < operator, prompts for all
  8266.                                    further questions
  8267.  
  8268.  <y                                Assumes a response of yes
  8269.  
  8270.  <n                                Assumes a response of no
  8271.  
  8272.  A response operator applies to the function immediately preceding it. For
  8273.  instance, you can add the  operator to the  quit  macro definition to
  8274.  restore the usual prompt:
  8275.  
  8276.    quit:=meta exit <
  8277.    quit:alt+x
  8278.  
  8279.  Now the macro prompts for a response before it exits.
  8280.  
  8281.  
  8282.  8.3.3  Macro Arguments
  8283.  
  8284.  If you enter an argument in PWB and then invoke a macro, the argument is
  8285.  passed to the first function in the macro that takes an argument:
  8286.  
  8287.    tripleit:=copy paste paste
  8288.  
  8289.  The  tripleit  macro invokes the copy and paste editing functions. If you
  8290.  highlight a text area and then invoke the macro, your highlighted argument
  8291.  is passed to the copy function, which copies the argument to the clipboard.
  8292.  The macro then invokes paste twice. The effect is to insert two copies of
  8293.  the highlighted text.
  8294.  
  8295.   You cannot pass more than one argument from PWB to a macro.
  8296.  
  8297.  You cannot pass more than one argument from PWB to a macro, even if the
  8298.  macro invokes more than one function that can accept an argument. The
  8299.  argument always goes to the first function in the macro that takes an
  8300.  argument.
  8301.  
  8302.  You can also prompt for input inside a macro and pass the input as an
  8303.  argument using the prompt function as shown below:
  8304.  
  8305.    newfile:=arg "Next file: " prompt setfile <
  8306.    newfile:alt+n
  8307.  
  8308.  The  newfile  macro prompts for a file name and then switches to the
  8309.  specified file. The sequence  arg "Next file: " passes a text argument to
  8310.  prompt, which prints the text on the dialog line and waits for input. The
  8311.  input is passed as a text argument to the setfile function, which switches
  8312.  to that file. For more information on the prompt function, see on-line help.
  8313.  
  8314.  
  8315.  
  8316.  8.3.4  Macro Conditionals
  8317.  
  8318.  Macros can take different actions depending on certain conditions. Such
  8319.  macros take advantage of the fact that PWB editing functions generally
  8320.  return values─a TRUE (nonzero) value if successful or FALSE (zero) if
  8321.  unsuccessful.
  8322.  
  8323.  Macros can use four conditional operators:
  8324.  
  8325.  Operator                          Description
  8326.  ────────────────────────────────────────────────────────────────────────────
  8327.  :>label                           Defines a label that can be targeted by
  8328.                                    other operators
  8329.  
  8330.  =>label                           Jumps to label
  8331.  
  8332.  +>label                           Jumps to label if the previous function
  8333.                                    returns TRUE
  8334.  
  8335.  ->label                           Jumps to label if the previous function
  8336.                                    returns FALSE
  8337.  
  8338.  For example, the  leftmarg  macro moves the cursor to the left margin of the
  8339.  editing window:
  8340.  
  8341.    leftmarg:=:>leftmore left +>leftmore
  8342.  
  8343.  The macro above invokes the left function repeatedly (jumping to the label
  8344.  leftmore) until it returns FALSE, indicating the cursor has reached the left
  8345.  margin.
  8346.  
  8347.  The label must appear immediately after the conditional operator, with no
  8348.  intervening spaces. A conditional operator without a label exits the macro
  8349.  immediately if the condition is true. If the condition is false, the macro
  8350.  continues execution. The following example demonstrates this:
  8351.  
  8352.    turnon:=insertmode +> insertmode
  8353.  
  8354.  This macro turns on insert mode regardless of whether insert mode is
  8355.  currently on or off. If insert mode is off, the first invocation of
  8356.  insertmode toggles the mode on and returns TRUE, causing the +> operator to
  8357.  terminate the macro. If insert mode is currently on, the first invocation of
  8358.  insertmode turns insert mode off and returns FALSE. The macro then invokes
  8359.  insertmode a second time, turning insert mode back on.
  8360.  
  8361.  
  8362.  8.3.5  Temporary Macros
  8363.  
  8364.  Occasionally, you may want to create a macro that lasts only through the
  8365.  current session. This can be done with the assign function. For example, the
  8366.  following steps create the  comment  macro described above.
  8367.  
  8368.  To create the macro:
  8369.  
  8370.  
  8371.    ■   Press ALT+A
  8372.  
  8373.    ■   Type  comment:=begline "/* " endline " */"
  8374.  
  8375.    ■   Press ALT+=
  8376.  
  8377.  
  8378.  To assign the ALT+C keystroke to the macro:
  8379.  
  8380.  
  8381.    ■   Press ALT+A
  8382.  
  8383.    ■   Type  comment:alt+c
  8384.  
  8385.    ■   Press ALT+=
  8386.  
  8387.  
  8388.  The macro is available immediately and then disappears at the end of the
  8389.  current session.
  8390.  
  8391.  
  8392.  8.3.6  Macro Recordings
  8393.  
  8394.  Another way to create a macro is by recording your own actions. The entire
  8395.  sequence of actions is saved and can be replayed later by pressing a key.
  8396.  
  8397.  You start the recording by invoking the record function. PWB names the
  8398.  resulting macro recordvalue by default, but you can use other names as well.
  8399.  To record a macro:
  8400.  
  8401.  
  8402.    ■   Choose Record On from the Edit menu to start the recording.
  8403.  
  8404.    ■   Perform the actions you want to record.
  8405.  
  8406.    ■   Choose Record On again to end the recording.
  8407.  
  8408.    ■   If recordvalue is not already assigned, assign it to a keystroke as
  8409.        described above.
  8410.  
  8411.  
  8412.  After you complete these steps, a macro named recordvalue is available
  8413.  through the keystroke you assigned in the last step above. When you press
  8414.  this key, PWB replays the actions you recorded.
  8415.  
  8416.  If you don't do anything more, the recorded macro is temporary─it disappears
  8417.  when you exit PWB. To save the macro permanently:
  8418.  
  8419.  
  8420.    ■   Open the <record> pseudofile (press ALT+A, type  <record>, press F2).
  8421.  
  8422.    ■   Copy the macro definition in <record>.
  8423.  
  8424.    ■   Paste the definition into the  [PWB]  section of your TOOLS.INI file.
  8425.  
  8426.  
  8427.  Studying recorded macros can teach you a lot about macros and editor
  8428.  functions. If you open the <record> pseudofile in a second window before you
  8429.  record, you can watch PWB write the macro definition function by function.
  8430.  
  8431.  If you save a recorded macro, you'll want to name it something other than
  8432.  recordvalue, the default name. To do this, pass the new name as an argument
  8433.  when you start the recording:
  8434.  
  8435.  
  8436.    ■   Press ALT+A ALT+A.
  8437.  
  8438.    ■   Type the new name.
  8439.  
  8440.    ■   Choose Record On from the Edit menu to start recording.
  8441.  
  8442.    ■   Complete the recording as usual.
  8443.  
  8444.  
  8445.  You can expand an existing macro using the same process. If you supply the
  8446.  name of an existing macro, PWB appends the recorded commands to the macro
  8447.  instead of replacing it.
  8448.  
  8449.   You can record a series  of actions without  executing them.
  8450.  
  8451.  You can also make a "silent" recording, which records a series of actions
  8452.  without executing them. Start the recording with a meta record command
  8453.  (press F9 SHIFT+CTRL+R). Then complete the recording process as described
  8454.  above.
  8455.  
  8456.  
  8457.  8.4  Writing and Building C Extensions
  8458.  
  8459.  An "extension" is a file containing one or more user-written functions. PWB
  8460.  loads extensions at run time. Once the extension has been loaded, its
  8461.  functions can be assigned their own keystrokes, given arguments, and invoked
  8462.  in macros, exactly like other PWB functions.
  8463.  
  8464.   User-written functions execute more quickly than macros.
  8465.  
  8466.  The ability to load and call user-written functions makes PWB highly
  8467.  extensible. Because they consist of compiled C code, your functions can
  8468.  perform more complex jobs than macros can, and they execute many times
  8469.  faster.
  8470.  
  8471.  An extension contains executable code, but it differs from a normal
  8472.  executable file in some important ways:
  8473.  
  8474.  
  8475.    ■   It does not contain the usual C start-up code.
  8476.  
  8477.    ■   It contains special data structures that describe its functions to
  8478.        PWB.
  8479.  
  8480.    ■   Its functions are declared in a form that allows PWB to call them and
  8481.        pass arguments to them.
  8482.  
  8483.    ■   Its functions can call native PWB functions, and some, but not all, C
  8484.        library functions.
  8485.  
  8486.  
  8487.  This section explains how to build, load, and invoke a PWB extension. The
  8488.  example, CENTER.C, serves as a basis for discussion throughout the rest of
  8489.  this chapter.
  8490.  
  8491.  The CENTER.C extension contains one extension function,  CenterLine, which
  8492.  centers a line or range of lines in the current file.
  8493.  
  8494.    /* CENTER.C: Sample PWB extension */
  8495.  
  8496.    #define LINE_LENGTH 80 /* Assumes 80-column screen */
  8497.  
  8498.    #include <string.h>
  8499.    /* PWB extension header file */
  8500.    #include "ext.h"
  8501.  
  8502.    PWBFUNC CenterLine( unsigned argData,
  8503.                        ARG _far *pArg,
  8504.                        flagType fMeta );
  8505.  
  8506.    /* Switch Table */
  8507.    struct swiDesc   swiTable[] =
  8508.    {
  8509.       { NULL, NULL, 0 }
  8510.    };
  8511.  
  8512.    /* Command Table */
  8513.    struct cmdDesc   cmdTable[] =
  8514.    {
  8515.       { "CenterLine", CenterLine, 0, NOARG | LINEARG },
  8516.       { NULL, NULL, 0, 0 }
  8517.    };
  8518.  
  8519.    /* Initialization Function */
  8520.    void EXTERNAL WhenLoaded( void )
  8521.    {
  8522.       DoMessage( "Loading Center extension" );
  8523.    }
  8524.  
  8525.    /* Extension (user-written) function */
  8526.    PWBFUNC CenterLine( unsigned argData,
  8527.                        ARG _far *pArg,
  8528.                        flagType fMeta )
  8529.    {
  8530.       PFILE pFile;
  8531.       LINE  yStart, yEnd;
  8532.       int   len;
  8533.       char *pBuf, buf[BUFLEN];
  8534.  
  8535.    /* Get a handle to the current file */
  8536.       pFile = FileNameToHandle( "", "" );
  8537.  
  8538.       /* Handle various argument types */
  8539.       switch( pArg->argType )
  8540.       {
  8541.          case NOARG:  /* No argument. Center current line */
  8542.             yStart = yEnd = pArg->arg.noarg.y;
  8543.             break;
  8544.  
  8545.          case LINEARG:  /*  Center range of lines */
  8546.             yStart = pArg->arg.linearg.yStart;
  8547.             yEnd = pArg->arg.linearg.yEnd;
  8548.             break;
  8549.       }
  8550.  
  8551.       /* Center current line or range of lines */
  8552.       for( ; yStart <= yEnd; yStart++ )
  8553.       {
  8554.          /* Get a line from the current file */
  8555.          len = GetLine( yStart, buf, pFile );
  8556.  
  8557.          if( len > 0 )
  8558.          {
  8559.             /* Center the text in this line */
  8560.             pBuf = buf + strspn( buf, " \t" );
  8561.             len = strlen( pBuf );
  8562.             memmove( buf+(LINE_LENGTH-len) / 2, pBuf, len+1 );
  8563.             memset( buf, ' ', (LINE_LENGTH - len) / 2 );
  8564.  
  8565.             /* Write modified line back to the current file */
  8566.             PutLine( yStart, buf, pFile );
  8567.          }
  8568.       }
  8569.       return TRUE;
  8570.    }
  8571.  
  8572.  Building and using a PWB extension involves four basic steps:
  8573.  
  8574.  
  8575.    1.  Compiling
  8576.  
  8577.    2.  Linking
  8578.  
  8579.    3.  Loading the extension into PWB
  8580.  
  8581.    4.  Assigning a keystroke to each function in the extension
  8582.  
  8583.  
  8584.  You can build extensions for both real mode (DOS) and OS/2 protected mode.
  8585.  
  8586.  
  8587.  8.4.1  Building Real-Mode Extensions
  8588.  
  8589.  This section describes how to build extensions for real mode.
  8590.  
  8591.  
  8592.  Compiling
  8593.  
  8594.  The source (.C) file for an extension must include EXT.H, the extension
  8595.  header file. Since an extension is not a stand-alone executable file, it
  8596.  doesn't have a main function; so its source file is compiled with the /c
  8597.  (compile, but don't link) option:
  8598.  
  8599.    CL /c /Gs /ACw CENTER.C
  8600.  
  8601.  The /Gs option turns off stack checking; the /ACw option selects the
  8602.  required custom memory model.
  8603.  
  8604.  PWB extension interface is designed for C programmers. However, you can
  8605.  write extensions in assembly language or other languages if you simulate the
  8606.  required C memory model (in which SS is not assumed to equal DS).
  8607.  
  8608.  
  8609.  Linking
  8610.  
  8611.  The first object file in the link command must be the stub EXTHDR.OBJ:
  8612.  
  8613.    link exthdr center, center.mxt;
  8614.  
  8615.  PWB can load a file with any name, but most programmers use the .MXT
  8616.  extension to distinguish a PWB extension from a normal .EXE file.
  8617.  
  8618.  
  8619.  Loading the Extension
  8620.  
  8621.  Once the extension is built, you can cause PWB to load it by adding a load
  8622.  command to your TOOLS.INI file:
  8623.  
  8624.    load:center
  8625.  
  8626.  You don't need to supply a file extension; PWB assumes the correct file
  8627.  extension. To specify a path, supply the path name preceded by a dollar sign
  8628.  ($):
  8629.  
  8630.    load:$INIT:center
  8631.  
  8632.  The example tells PWB to search the directories specified in the INIT
  8633.  environment variable. If listed, the environment variable must be in
  8634.  uppercase.
  8635.  
  8636.  TOOLS.INI can contain multiple load commands for different extensions.
  8637.  However, loading each extension involves a certain amount of memory
  8638.  overhead, and there is no way to unload an extension from memory. To
  8639.  conserve memory, place all frequently used functions in a single extension
  8640.  and load only that extension.
  8641.  
  8642.  
  8643.  Assigning Keystrokes to Functions
  8644.  
  8645.  After an extension has been loaded, you must provide some way to invoke its
  8646.  functions from inside PWB. A keystroke is the most common means, although
  8647.  extension functions, like native PWB functions, can be invoked in various
  8648.  ways.
  8649.  
  8650.  You can assign the ALT+C keystroke to the  CenterLine  function with:
  8651.  
  8652.    CenterLine:alt+c
  8653.  
  8654.  Once the  CenterLine  function has been assigned to this keystroke, you can
  8655.  invoke it by pressing ALT+C.
  8656.  
  8657.  
  8658.  8.4.2  Building Protected-Mode Extensions
  8659.  
  8660.  The build process for OS/2 protected mode differs only slightly from the
  8661.  real-mode build process.
  8662.  
  8663.  
  8664.  Compiling
  8665.  
  8666.  The source (.C) file for an extension must include EXT.H, the extension
  8667.  header file. Since an extension is not a stand-alone executable file, it
  8668.  doesn't have a main function; so its source file is compiled with the /c
  8669.  (compile, but don't link) option:
  8670.  
  8671.    CL /c /Gs /ACw CENTER.C
  8672.  
  8673.  The /Gs option turns off stack checking; the /ACw option selects the
  8674.  required custom memory model.
  8675.  
  8676.  PWB extension interface is designed for C programmers. However, you can
  8677.  write extensions in assembly language or other languages if you simulate the
  8678.  required C memory model (in which SS is not assumed to equal DS).
  8679.  
  8680.  
  8681.  Linking
  8682.  
  8683.  Link with EXTHRDP.OBJ instead of EXTHDR.OBJ. Specify the .PXT extension for
  8684.  the output file. List the EXT.DEF definitions file:
  8685.  
  8686.    link exthdrp center, center.pxt,, os2, ext.def
  8687.  
  8688.  
  8689.  Loading the Extension
  8690.  
  8691.  In protected mode, PWB assumes the .PXT file extension. If your extension is
  8692.  not found, PWB assumes the .DLL file extension.
  8693.  
  8694.   You cannot create a bound extension.
  8695.  
  8696.  There is no way to create a bound extension (one that runs in both real and
  8697.  protected mode). However, you can build separate versions of an extension
  8698.  and use a single TOOLS.INI load command to load the correct extension in
  8699.  each mode. PWB loads the real-mode file (.MXT) in real mode and the
  8700.  protected-mode file (.PXT or .DLL) in protected mode.
  8701.  
  8702.  
  8703.  Assigning Keystrokes to Functions
  8704.  
  8705.  After an extension has been loaded, you must provide some way to invoke its
  8706.  functions from inside PWB. A keystroke is the most common means, although
  8707.  extension functions, like native PWB functions, can be invoked in various
  8708.  ways.
  8709.  
  8710.  You can assign the ALT+C keystroke to the  CenterLine  function with:
  8711.  
  8712.    CenterLine:alt+c
  8713.  
  8714.  Once the  CenterLine  function has been assigned to this keystroke, you can
  8715.  invoke it by pressing ALT+C.
  8716.  
  8717.  
  8718.  8.4.3  Describing Functions and Switches
  8719.  
  8720.  To call functions in your extension, PWB must know certain information about
  8721.  each function, such as the name and address of the function, what types of
  8722.  arguments it accepts, and what switches (if any) it employs. You provide
  8723.  this information in a pair of arrays─cmdTable and swiTable─that must be
  8724.  present in every PWB extension.
  8725.  
  8726.  
  8727.  The cmdTable Array
  8728.  
  8729.  Every extension must contain an array of structures named cmdTable. This
  8730.  array provides the information PWB needs to call the extension's functions.
  8731.  
  8732.  
  8733.  The cmdTable array is an array of structures of type cmdDesc (which is
  8734.  declared in EXT.H). Each structure in the array describes one function in
  8735.  the extension. The array is terminated with a structure whose members are
  8736.  all null.
  8737.  
  8738.  For instance, the CENTER.C extension has one function, named  CenterLine, so
  8739.  its cmdTable array contains two structures (one for  CenterLine  and the
  8740.  other to terminate the table):
  8741.  
  8742.    struct cmdDesc cmdTable[] =
  8743.    {
  8744.       { "CenterLine", CenterLine, 0, NOARG | LINEARG },
  8745.       { NULL, NULL, 0, 0 }
  8746.    };
  8747.  
  8748.  Each cmdDesc structure in cmdTable contains these members:
  8749.  
  8750.  
  8751.    ■   The function's name
  8752.  
  8753.    ■   The function's address
  8754.  
  8755.    ■   Reserved item (must be 0)
  8756.  
  8757.    ■   The argument types the function accepts
  8758.  
  8759.  
  8760.  The last member in the list is an integer containing bitflags representing
  8761.  types of arguments that your function accepts. You can combine more than one
  8762.  bitflag using the OR ( | ) operator.
  8763.  
  8764.  For instance, the  CenterLine  function can handle an argument of the type
  8765.  LINEARG, or no arguments (NOARG). So it lists the types:
  8766.  
  8767.    NOARG | LINEARG
  8768.  
  8769.  There are many argument types in addition to these. For information about
  8770.  specific argument types, see the Extensions topic in on-line help.
  8771.  
  8772.  
  8773.  The swiTable Array
  8774.  
  8775.  Extension functions, such as native PWB functions, can respond to user-
  8776.  configurable switches. From the viewpoint of an extension function, a switch
  8777.  is usually a variable that the user can change at run time. Your function
  8778.  must be ready to respond to these changes, and PWB must have some way to
  8779.  convey them. The vehicle for this interchange is an array of structures
  8780.  named swiTable.
  8781.  
  8782.  The swiTable array is similar to the cmdTable array described above. It is
  8783.  an array of structures, terminated by a structure whose members are all
  8784.  null. Each structure in swiTable describes one switch used by a function in
  8785.  your extension.
  8786.  
  8787.  The CENTER.C extension doesn't take any switches, so its swiTable array only
  8788.  contains a terminating null structure:
  8789.  
  8790.    struct swiDesc swiTable[] =
  8791.    {
  8792.        { NULL, NULL, 0 }
  8793.    };
  8794.  
  8795.  Each structure in swiTable is of type swiDesc, whose members are
  8796.  
  8797.  
  8798.    ■   A pointer to the switch name
  8799.  
  8800.    ■   A pointer to the switch or a function
  8801.  
  8802.    ■   A flag that indicates the type of the switch
  8803.  
  8804.  
  8805.  A switch can be one of three types: SWI_BOOLEAN for TRUE/FALSE conditions,
  8806.  SWI_NUMERIC for numerics, or SWI_SPECIAL for strings.
  8807.  
  8808.  The second member of swiDesc is a pointer. It points to the switch itself if
  8809.  the switch is type SWI_BOOLEAN or SWI_NUMERIC, or to a string-handling
  8810.  function if the switch is type SWI_SPECIAL.
  8811.  
  8812.  For instance, the following code creates a numeric switch with the default
  8813.  value 27:
  8814.  
  8815.    static int n = 27;
  8816.  
  8817.    struct swiDesc swiTable[] =
  8818.    {
  8819.       { "newswitch", &n, SWI_NUMERIC | RADIX10 },
  8820.       { NULL,  NULL, 0 }
  8821.    };
  8822.  
  8823.  The first structure in the example above contains the name of the switch
  8824.  ("newswitch"), a pointer to the variable that contains the switch's value
  8825.  (&n), and the switch's type (SWI_NUMERIC).
  8826.  
  8827.  In this example, the third structure member contains another constant,
  8828.  RADIX10. If a switch is type SWI_NUMERIC, you must supply a second constant
  8829.  to tell PWB whether to interpret user-assigned values as decimal (RADIX10)
  8830.  or hexadecimal (RADIX16) numbers.
  8831.  
  8832.  If the switch is type SWI_SPECIAL, the second member of swiDesc is a pointer
  8833.  to an additional string-handling function that you write. This function must
  8834.  be of type int far _pascal. Each time the text switch changes, PWB calls
  8835.  your function, passing it the address of the updated string as a char far
  8836.  pointer. The following code stores the updated string in a buffer named
  8837.  mystring:
  8838.  
  8839.    char mystring[BUFLEN];
  8840.  
  8841.    int far _pascal setstr( char far *ptr )
  8842.    {
  8843.       strcpy( mystring, ptr );
  8844.    }
  8845.  
  8846.  If desired, you can list switches for extension functions separately from
  8847.  other switches. Whenever PWB loads an extension, it looks in TOOLS.INI for a
  8848.  section with this form:
  8849.  
  8850.    [PWB-ext]
  8851.  
  8852.  where ext is the base name of the extension. If the extension exists, PWB
  8853.  recognizes the settings immediately following the tag. For instance, if your
  8854.  extension SAMPLE.MXT uses a numeric switch named  numbills, you can set
  8855.  numbills  to the value 66 with:
  8856.  
  8857.    [PWB-SAMPLE]
  8858.    numbills:66
  8859.  
  8860.  
  8861.  8.4.4  Initializing Functions
  8862.  
  8863.  Every PWB extension must contain a function named WhenLoaded, which PWB
  8864.  calls immediately after loading the extension. The WhenLoaded function
  8865.  provides a chance to do any initialization that your functions require. (If
  8866.  your functions don't need any initialization, they can simply return.)
  8867.  
  8868.  The CENTER.C extension uses WhenLoaded to display a loading message:
  8869.  
  8870.    void EXTERNAL WhenLoaded( void )
  8871.    {
  8872.        DoMessage( "Loading Center extension" );
  8873.    }
  8874.  
  8875.  DoMessage is a PWB function that displays a message on the dialog line.
  8876.  Section 8.4.7, "Calling PWB Functions," lists PWB functions and explains how
  8877.  to call them.
  8878.  
  8879.  
  8880.  8.4.5  Prototyping Functions
  8881.  
  8882.  To be called by PWB, each extension function must be declared as type
  8883.  PWBFUNC and accept the parameters argData, pArg, and fMeta. The  CenterLine
  8884.  function in the section of CENTER.C code below follows  this model:
  8885.  
  8886.    PWBFUNC CenterLine( unsigned argData,
  8887.                        ARG _far *pArg,
  8888.                        flagType fMeta )
  8889.  
  8890.  The PWBFUNC type is actually a macro that evaluates to flagType _pascal
  8891.  _loadds _far. The flagType return type declares that the function returns
  8892.  either TRUE (nonzero) or FALSE (zero). Your function should return a value
  8893.  so that it can be used in a macro with conditionals. The modifiers _pascal,
  8894.  _loadds, and _far specify the calling conventions PWB expects editor
  8895.  functions to have.
  8896.  
  8897.  
  8898.  8.4.6  Receiving Parameters
  8899.  
  8900.  Like native PWB functions, extension functions can receive parameters from
  8901.  the user. The CENTER.C example allows you to select a range of lines to
  8902.  center, for example. The selected range is passed as a parameter to the
  8903.  CenterLine  function.
  8904.  
  8905.  Extension functions receive parameters in much the same way ordinary C
  8906.  programs receive command-line parameters. In both cases, the parameters are
  8907.  passed in a predefined data construct─argc and argv for a normal C program,
  8908.  and the following parameters for an extension function:
  8909.  
  8910.  Parameter                         Description
  8911.  ────────────────────────────────────────────────────────────────────────────
  8912.  argData                           The keystroke used to invoke your
  8913.                                    function
  8914.  
  8915.  pArg                              A pointer to a structure containing
  8916.                                    arguments passed to your function
  8917.  
  8918.  fMeta                             TRUE (nonzero) if meta precedes the
  8919.                                    argument, otherwise FALSE (zero)
  8920.  
  8921.  The first parameter is rarely used. Most extension functions receive all
  8922.  their parameter data in the second parameter, pArg. This parameter is a
  8923.  pointer to a structure of type ARG, which contains:
  8924.  
  8925.  Parameter                         Description
  8926.  ────────────────────────────────────────────────────────────────────────────
  8927.  argType                           An integer that indicates the argument
  8928.                                    type
  8929.  
  8930.  arg                               A union of structures, one structure for
  8931.                                    each
  8932.                                    argument type
  8933.  
  8934.  Typically, your function tests pArg->argType to find out what type of
  8935.  parameter PWB has passed. Once the type is known, the function responds
  8936.  accordingly. The following code from CENTER.C handles two argument types:
  8937.  
  8938.    switch( pArg->argType )
  8939.    {
  8940.       case NOARG:  /* No argument. Center current line */
  8941.          yStart = yEnd = pArg->arg.noarg.y;
  8942.          break;
  8943.  
  8944.       case LINEARG:  /*  Center range of lines */
  8945.          yStart = pArg->arg.linearg.yStart;
  8946.          yEnd = pArg->arg.linearg.yEnd;
  8947.          break;
  8948.    }
  8949.  
  8950.   PWB rejects invalid arguments.
  8951.  
  8952.  If your function takes only one argument, it doesn't need to test
  8953.  pArg->argType at all. PWB knows beforehand what argument types your function
  8954.  accepts (via cmdDesc) and rejects any invalid arguments.
  8955.  
  8956.  Once the argument type is known, your function can access the parameters
  8957.  through pArg->arg, a structure whose members differ for each argument type.
  8958.  In the NOARG (no arguments) case, it contains x and y values identifying the
  8959.  cursor position in the current file:
  8960.  
  8961.    struct noargType
  8962.    {           /* no argument    */
  8963.       LINE y;  /* cursor line    */
  8964.       COL  x;  /* cursor column  */
  8965.    };
  8966.  
  8967.  The CENTER.C example uses the y value in this structure (noarg.y, the cursor
  8968.  line) to center the current line:
  8969.  
  8970.    case NOARG:  /* No argument. Center current line */
  8971.       yStart = yEnd = pArg->arg.noarg.y;
  8972.       break;
  8973.  
  8974.  Similarly, in the LINEARG case, the pArg->arg structure contains three
  8975.  values:
  8976.  
  8977.    struct lineargType
  8978.    {                /* line argument specified */
  8979.       int  cArg;    /* count of args pressed   */
  8980.       LINE yStart;  /* starting line of range  */
  8981.       LINE yEnd;    /* ending line of range    */
  8982.    };
  8983.  
  8984.  The CENTER.C example uses the starting and ending values in this structure
  8985.  (yStart and yEnd) to center a range of selected lines:
  8986.  
  8987.    case LINEARG:  /*  Center range of lines */
  8988.       yStart = pArg->arg.linearg.yStart;
  8989.       yEnd = pArg->arg.linearg.yEnd;
  8990.       break;
  8991.  
  8992.  The method is the same for other argument types. The pArg->arg structures
  8993.  for all argument types are described in on-line help.
  8994.  
  8995.  
  8996.  8.4.7  Calling PWB Functions
  8997.  
  8998.  Many of PWB's internal functions are public. Your extension function can
  8999.  call them for the same purposes that PWB itself does. This section
  9000.  demonstrates the most commonly used PWB functions─those that manipulate the
  9001.  current file.
  9002.  
  9003.  A list of callable PWB functions appears near the end of this section. For
  9004.  complete information on specific PWB functions, consult on-line help.
  9005.  
  9006.  
  9007.  Getting a File Handle
  9008.  
  9009.  Extension functions can do many different tasks, but they typically
  9010.  manipulate a file in some way. The extension function in the CENTER.C
  9011.  example rewrites a line or lines in the current file, for example. The
  9012.  current file is the one that appears in the editing window. Since it is
  9013.  already open for editing, you can access the current file without opening
  9014.  it. Simply assign its file handle to a variable in your function.
  9015.  
  9016.  PWB file-handling functions use file handles of type PFILE. The CENTER.C
  9017.  example declares the following handle variable:
  9018.  
  9019.    PFILE pFile;
  9020.  
  9021.  The FileNameToHandle function gets a handle to a file that is already open
  9022.  for editing:
  9023.  
  9024.    pFile = FileNameToHandle( "", "" );
  9025.  
  9026.  The function takes two string arguments. If the first string is null, as
  9027.  here, the FileNameToHandle function returns a handle to the current file.
  9028.  You can use the AddFile function to get handles to other files (in which
  9029.  case you may need to use other PWB functions such as FileRead).
  9030.  
  9031.  
  9032.  Reading a Line From the File
  9033.  
  9034.  Once your function has a file handle, it can read from the file with the
  9035.  GetLine function, which reads one line at a time:
  9036.  
  9037.    len = GetLine( yStart, buf, pFile );
  9038.  
  9039.  The first argument is a line number, the second a pointer to a buffer, and
  9040.  the third a file handle. So the above call reads line number  yStart  from
  9041.  the file whose handle is  pFile  into the buffer  buf. Note that the first
  9042.  line in a file is line 0, not line 1.
  9043.  
  9044.  Once you have read a line into a local buffer, you can manipulate it as
  9045.  desired. CENTER.C uses its buffer  buf  to center the line's text.
  9046.  
  9047.  
  9048.  Writing a Line to the File
  9049.  
  9050.  After modifying a line, you can write it back to the file. The PutLine
  9051.  function writes one line at a time:
  9052.  
  9053.    PutLine( yStart, buf, pFile );
  9054.  
  9055.  PutLine takes the same arguments as GetLine─a line number, buffer pointer,
  9056.  and file handle. In CENTER.C, the above call writes the line from  buf  to
  9057.  line  yStart  in the file whose handle is  pFile.
  9058.  
  9059.  
  9060.  Summary of PWB Functions
  9061.  
  9062.  If you understand how CENTER.C works, you know the basics of using PWB
  9063.  functions in your own functions. The rest is just a matter of learning the
  9064.  details of individual functions. Table 8.1 lists the PWB functions, grouping
  9065.  them by category. For additional information on specific functions, consult
  9066.  on-line help.
  9067.  
  9068.  Table 8.1  Callable PWB Functions
  9069.  
  9070. ╓┌──────────────────┌──────────────────┌─────────────────────────────────────╖
  9071.  Category           Function           Description
  9072.  ────────────────────────────────────────────────────────────────────────────
  9073.  Block Operations   CopyBox            Insert rectangular area
  9074.  Category           Function           Description
  9075.  ────────────────────────────────────────────────────────────────────────────
  9076.  Block Operations   CopyBox            Insert rectangular area
  9077.  
  9078.                     CopyLine           Insert range of lines
  9079.  
  9080.                     CopyStream         Insert stream of text
  9081.  
  9082.                     DelBox             Delete rectangular area
  9083.  
  9084.                     DelLine            Delete range of lines
  9085.  
  9086.                     DelStream          Delete stream of text
  9087.  
  9088.  Build              fGetMake           Get extmake setting
  9089.  
  9090.                     fSetMake           Set extmake setting
  9091.  
  9092.  Color              GetColor           Get color of specified line
  9093.  
  9094.                     PutColor           Set color of specified line
  9095.  Category           Function           Description
  9096.  ────────────────────────────────────────────────────────────────────────────
  9097.                    PutColor           Set color of specified line
  9098.  
  9099.  Cursor             GetCursor          Get cursor position
  9100.  
  9101.                     MoveCur            Move cursor
  9102.  
  9103.  Dialog             DoMessageBox       Create message dialog
  9104.  
  9105.                     PopUpBox           Display text in dialog
  9106.                                        window
  9107.  
  9108.  Display            BadArg             Report that argument was invalid
  9109.  
  9110.                     Display            Update screen
  9111.  
  9112.                     DoMessage          Display message on dialog line
  9113.  
  9114.  File               AddFile            Open new file and get file handle
  9115.  
  9116.  Category           Function           Description
  9117.  ────────────────────────────────────────────────────────────────────────────
  9118. 
  9119.                     DelFile            Delete contents of file buffer
  9120.  
  9121.                     fChangeFile        Change current file to named file
  9122.  
  9123.                     FileNameToHandle   Get handle to open file
  9124.  
  9125.                     FileRead           Copy disk file to file
  9126.                                        buffer
  9127.  
  9128.                     FileWrite          Copy file buffer to disk file
  9129.  
  9130.  
  9131.  
  9132.  Table 8.1   (continued)
  9133.  
  9134. ╓┌────────────────┌─────────────────┌────────────────────────────────────────╖
  9135.  Category         Function          Description
  9136.  ────────────────────────────────────────────────────────────────────────────
  9137.  Category         Function          Description
  9138.  ────────────────────────────────────────────────────────────────────────────
  9139.                   pFileToTop        Make specified file the current file
  9140.  
  9141.                   RemoveFile        Remove file from memory
  9142.  
  9143.  Keyboard         KbHook            Restore keyboard control to PWB
  9144.  
  9145.                   KbUnHook          Remove keyboard control from PWB
  9146.  
  9147.                   ReadChar          Get information on next keystroke
  9148.  
  9149.  Format           ReadCmd           Get keystroke information in CmdDesc
  9150.  
  9151.  Line             FileLength        Get length of file
  9152.  
  9153.                   GetLine           Get line from file
  9154.  
  9155.                   PutLine           Write line to file
  9156.  
  9157.  List             GetListEntry      Get item from list
  9158.  Category         Function          Description
  9159.  ────────────────────────────────────────────────────────────────────────────
  9160. List             GetListEntry      Get item from list
  9161.  
  9162.                   ScanList          Process list
  9163.  
  9164.  Memory           Falloc            Allocate far memory
  9165.  
  9166.                   Fdalloc           Deallocate far memory
  9167.  
  9168.  Miscellaneous    fExecute          Execute macro
  9169.  
  9170.                   FindSwitch        Get information about switch
  9171.  
  9172.                   GetEditorObject   Get internal PWB data item
  9173.  
  9174.                   GetString         Get input from dialog line
  9175.  
  9176.                   mgetenv           Get environment string
  9177.  
  9178.                   NameToFunc        Get information about function or macro
  9179.  Category         Function          Description
  9180.  ────────────────────────────────────────────────────────────────────────────
  9181.                  NameToFunc        Get information about function or macro
  9182.  
  9183.                   NameToKeys        Get key(s) assigned to specified
  9184.                                     function
  9185.  
  9186.                   Replace           Replace character
  9187.  
  9188.                   SetEditorObject   Set internal PWB data item
  9189.  
  9190.                   SetKey            Assign function to
  9191.                                     keystroke
  9192.  
  9193.  Search           REsearch          Search for regular
  9194.                                     expression
  9195.  
  9196.                   search            Search for string
  9197.  
  9198.  Virtual Memory   fpbtoVM           Copy data to virtual memory
  9199.  
  9200.  Category         Function          Description
  9201.  ────────────────────────────────────────────────────────────────────────────
  9202. 
  9203.                   VMalloc           Allocate virtual memory
  9204.  
  9205.                   VMFree            Free virtual memory
  9206.  
  9207.                   VMtofpb           Copy data from virtual memory
  9208.  
  9209.  Window           CloseWnd          Close window
  9210.  
  9211.                   Resize            Resize window
  9212.  
  9213.                   SplitWnd          Split window
  9214.  
  9215.  ────────────────────────────────────────────────────────────────────────────
  9216.  
  9217.  
  9218.  
  9219.  
  9220.  8.4.8  Calling C Library Functions
  9221.  
  9222.  You can write many useful extension functions using only PWB functions
  9223.  listed in the previous section. It is also possible to call C library
  9224.  routines, with some limitations. An extension written for OS/2 protected
  9225.  mode can call any C library routine if it is linked with EXTHDRP.OBJ and the
  9226.  .DLL C run-time library. The list of usable routines is shorter for
  9227.  real-mode (DOS) extensions linked with the non-.DLL run-time library.
  9228.  
  9229.  Before you call a C library routine, ask whether the task can be done with a
  9230.  PWB function. If the answer is yes, you should always call a PWB function in
  9231.  preference to the C library routine. This practice ensures compatibility
  9232.  between your functions and PWB.
  9233.  
  9234.  The following categories of C library routines are always safe to use in
  9235.  real mode:
  9236.  
  9237.  
  9238.    ■   Buffer manipulation
  9239.  
  9240.    ■   Character classification and conversion
  9241.  
  9242.    ■   Data conversion
  9243.  
  9244.    ■   String manipulation
  9245.  
  9246.  
  9247.  This list includes the library routines you are most likely to need in an
  9248.  extension function. If your extension function calls C library functions,
  9249.  you must link with the compact-model C library.
  9250.  
  9251.  The following routines should not be used in real mode:
  9252.  
  9253.  
  9254.    ■   Routines that need C start-up support (most input/output functions)
  9255.  
  9256.    ■   Memory management routines, such as malloc, and routines that call
  9257.        them
  9258.  
  9259.    ■   Process control routines such as spawn and exec
  9260.  
  9261.  
  9262.  If you are in doubt about a particular C library routine, you can always use
  9263.  it and see what happens. If the linker displays the following message,
  9264.  
  9265.    error L2044: __acrtused : symbol multiply defined, use /NOE
  9266.  
  9267.  the routine requires C start-up support and should not be used.
  9268.  
  9269.  
  9270.  
  9271.  
  9272.  
  9273.  
  9274.  Chapter 9  Debugging C Programs with CodeView
  9275.  ────────────────────────────────────────────────────────────────────────────
  9276.  
  9277.  Even experienced programmers occasionally find bugs in their programs. This
  9278.  chapter explores techniques that will help you locate these errors quickly,
  9279.  using the Microsoft CodeView debugger.
  9280.  
  9281.  This chapter describes:
  9282.  
  9283.  
  9284.    ■   How to display and modify variables and memory
  9285.  
  9286.    ■   How to control the flow of execution while debugging
  9287.  
  9288.    ■   Advanced CodeView debugging techniques
  9289.  
  9290.    ■   How to control CodeView's behavior with command-line switches and the
  9291.        TOOLS.INI file
  9292.  
  9293.  
  9294.  CodeView supports the Microsoft mouse (or any fully compatible pointing
  9295.  device). All operations are described first using the mouse; the keyboard
  9296.  command follows.
  9297.  
  9298.  For information about debugging OS/2 programs that use threads or processes,
  9299.  see Chapter 15, "Creating OS/2 Multithread Applications."
  9300.  
  9301.  
  9302.  9.1  Understanding CodeView Windows
  9303.  
  9304.  CodeView divides the screen into logically separate sections called windows,
  9305.  so that a large amount of information can be displayed in an organized and
  9306.  easy-to-read fashion. Each window is a discrete section of the display that
  9307.  operates independently of the other windows.
  9308.  
  9309.   Each window displays a different type of data.
  9310.  
  9311.  Each CodeView window has a distinct function. The name of each window
  9312.  described below appears in the top of the window's frame:
  9313.  
  9314.  
  9315.    ■   The Source window displays the source code. You can open a second
  9316.        Source window to view an include file, another source file, or the
  9317.        same source file at a different location.
  9318.  
  9319.    ■   The Command window accepts debugging commands.
  9320.  
  9321.    ■   The Watch window displays the current values of selected variables.
  9322.  
  9323.    ■   The Local window lists the values of all variables local to the
  9324.        current function or block.
  9325.  
  9326.    ■   The Memory window shows the contents of memory. You can open a second
  9327.        Memory window to view a different section of memory.
  9328.  
  9329.    ■   The Register window displays the contents of the microprocessor's
  9330.        registers, as well as the processor flags.
  9331.  
  9332.    ■   The 8087 window displays the registers of the coprocessor or its
  9333.        software emulator.
  9334.  
  9335.  
  9336.  CodeView starts running with three windows displayed. The Local window is at
  9337.  the top, the Source window fills the middle of the screen, and the Command
  9338.  window is at the bottom.
  9339.  
  9340.  There are two ways to open windows. You can choose the desired window from
  9341.  the View menu. (Note that you can open more than one of certain windows,
  9342.  such as Source or Memory.) In addition, some operations (such as selecting a
  9343.  Watch variable) open the appropriate window automatically, if it is not
  9344.  already open.
  9345.  
  9346.   All displays are  updated automatically.
  9347.  
  9348.  CodeView continually and automatically updates the contents of all windows.
  9349.  However, if you want to interact with a particular window (for instance, to
  9350.  enter a command, set a breakpoint, or modify a variable), you must select
  9351.  that window as the focus of user interaction.
  9352.  
  9353.  The selected window is called the "current" window. The current window is
  9354.  marked in three ways:
  9355.  
  9356.  
  9357.    ■   The window's name is highlighted in white.
  9358.  
  9359.    ■   The text cursor appears in the window.
  9360.  
  9361.    ■   The vertical and horizontal scroll bars are moved into the window.
  9362.  
  9363.  
  9364.  To select a new current window, click left in the window (position the mouse
  9365.  cursor in the window and press the left mouse button) that you want to be
  9366.  current. You can also press F6 or SHIFT+F6 to move the focus from one window
  9367.  to the next.
  9368.  
  9369.  Windows often contain more information than can be displayed in the area
  9370.  allotted to the window. There are two ways to view these additional
  9371.  contents. You can drag on the window's horizontal or vertical scroll bars.
  9372.  (Position the mouse pointer on the bar and, while holding down the left
  9373.  mouse button, drag the mouse in the appropriate direction.) You can also use
  9374.  the direction keys (LEFT, RIGHT, UP, DOWN) to move the text cursor.
  9375.  
  9376.  Typing commands into the Source window causes CodeView to temporarily shift
  9377.  its focus to the Command window. Whatever you type is appended to the last
  9378.  line in the Command window. If the Command window is closed, CodeView beeps
  9379.  in response to your entry and ignores the input.
  9380.  
  9381.  
  9382.  Adjusting the Windows
  9383.  
  9384.  Although you cannot change the relative positions of the windows, you can
  9385.  change their size or remove them. The Maximize, Size, and Close commands
  9386.  from the View menu perform these functions, or you can press CTRL+F10,
  9387.  CTRL+F8, and CTRL+F4, respectively. Window manipulations are especially easy
  9388.  with a mouse:
  9389.  
  9390.  
  9391.    ■   To maximize a window (enlarge it so it fills the screen), click left
  9392.        on the up arrow at the right end of the window's top border. To
  9393.        restore the window to its previous size and position, click left on
  9394.        the double arrow at the right end of the top border.
  9395.  
  9396.    ■   To change the size of a window, position the mouse pointer anywhere
  9397.        along the white line at the top of the window. Press and hold down the
  9398.        left mouse button. When two double arrows appear on the line, you can
  9399.        drag the mouse to enlarge or reduce the window. The same action on a
  9400.        vertical border widens or narrows the window.
  9401.  
  9402.    ■   To close a window, click left on the dot at the left end of the top
  9403.        border. You can also close any window in the View menu whose name has
  9404.        a dot next to it by selecting that window from the menu or by pressing
  9405.        that window's acclerator key. The adjacent windows automatically
  9406.        expand to recover the empty space.
  9407.  
  9408.  
  9409.  CodeView stores session information in a file called CURRENT.STS, which is
  9410.  created in the directory pointed to by the INIT environment variable. The
  9411.  session information includes such items as the name of the program being
  9412.  debugged, which CodeView windows were open, and the breakpoint locations.
  9413.  This information becomes the default status the next time you run CodeView.
  9414.  
  9415.  
  9416.  
  9417.  9.2  Overview of Debugging Techniques
  9418.  
  9419.  There is no single best approach to debugging for all programs or users.
  9420.  CodeView offers a variety of debugging tools that let you pick a method
  9421.  appropriate to the program or your work habits. The following section  may
  9422.  help you decide how to approach a particular program.
  9423.  
  9424.  Broadly speaking, two things can go wrong in a program:
  9425.  
  9426.  
  9427.    ■   The program doesn't manipulate the data the way you expected it to.
  9428.  
  9429.    ■   The flow of execution is incorrect.
  9430.  
  9431.  
  9432.  These problems occasionally overlap. Incorrect execution can corrupt the
  9433.  data, and bad data can cause execution to take an unexpected turn. Because
  9434.  CodeView allows you to trace program execution and display whatever
  9435.  combination of variables you want simultaneously, you don't have to know
  9436.  ahead of time whether the problem is bad data manipulation, a bad execution
  9437.  path, or some combination of these.
  9438.  
  9439.  CodeView has features that deal specifically with the problems of bad data
  9440.  and incorrect execution:
  9441.  
  9442.  
  9443.    ■   You can view and modify any program variable, any section of memory,
  9444.        or any processor register.
  9445.  
  9446.    ■   You can monitor the path of execution and precisely control where
  9447.        execution pauses.
  9448.  
  9449.  
  9450.  The following sections explain how to view and modify data and describe how
  9451.  execution is controlled.
  9452.  
  9453.  
  9454.  9.3  Viewing and Modifying Program Data
  9455.  
  9456.  The CodeView debugger offers a variety of ways to display program variables,
  9457.  processor registers, and memory. You can also modify the values of all these
  9458.  items as the program executes. This section shows how to display and modify
  9459.  variables, registers, and memory.
  9460.  
  9461.  
  9462.  9.3.1  Displaying Variables in the Watch Window
  9463.  
  9464.  To add a variable to the Watch window, position the cursor on the name of
  9465.  the variable using either the mouse or the direction keys (LEFT, RIGHT, UP,
  9466.  DOWN). Then select the Add Watch command from the Watch menu, or press
  9467.  CTRL+W.
  9468.  
  9469.  A dialog box appears with the selected variable's name displayed in the
  9470.  Expression field. If you don't want to watch the variable shown, type in the
  9471.  name of the variable you want to watch. Pressing ENTER or clicking left on
  9472.  the OK button adds this variable to the Watch window.
  9473.  
  9474.  The Watch window appears at the top of the screen. Adding a Watch variable
  9475.  automatically opens the Watch window if the window doesn't already exist.
  9476.  
  9477.  A newly added variable may be followed by the message:
  9478.  
  9479.    <Watch Expression Not in Context>
  9480.  
  9481.  This message appears when program execution has not yet reached the block
  9482.  where the variable is defined. (A block is a section of code enclosed in
  9483.  curly braces.) Global variables (those declared outside C functions) never
  9484.  cause CodeView to display this message; they can be watched from anywhere in
  9485.  the program.
  9486.  
  9487.  To remove a variable from the Watch window, use the Delete Watch command
  9488.  from the Watch menu, and select the variable to be removed using the list in
  9489.  the dialog box. You can also position the cursor on any line in the Watch
  9490.  window and press CTRL+Y to delete the line.
  9491.  
  9492.   There is no limit to how many variables you can watch.
  9493.  
  9494.  You can place as many variables as you like in the Watch window; the
  9495.  quantity is limited only by available memory. You can scroll through the
  9496.  Watch window to position it at those variables you want to view. CodeView
  9497.  automatically updates all watched variables as the program runs, including
  9498.  those not currently visible.
  9499.  
  9500.  Loops (do, for, or while) cause problems when they don't terminate
  9501.  correctly. Displaying loop variables in the Watch window is an easy way to
  9502.  determine whether a loop variable achieves its proper value.
  9503.  
  9504.  
  9505.  9.3.2  Displaying Expressions in the Watch Window
  9506.  
  9507.  You may have noticed that the Add Watch dialog box prompts for an
  9508.  expression, not simply a variable name. As this suggests, you can enter an
  9509.  expression (that is, any valid combination of variables, constants, and
  9510.  operators) for CodeView to evaluate and display.
  9511.  
  9512.   Expressions can use the  syntax of other languages.
  9513.  
  9514.  You are not limited to evaluating C expressions. The Language command of the
  9515.  Options menu offers a choice of BASIC or FORTRAN expression evaluation, if
  9516.  one of these languages better suits your needs. The ability to select the
  9517.  language evaluator is especially useful when debugging mixed-language
  9518.  programs. Remember that C-specific features, such as type casting or pointer
  9519.  conversions, are not available in other languages.
  9520.  
  9521.   You can display more information with expressions than with individual
  9522.  variables.
  9523.  
  9524.  By reducing several variables to a single, easily read value, an expression
  9525.  can be easier to interpret than the components that make it up. Imagine a
  9526.  for loop with two variables whose ratio is supposed to remain constant. You
  9527.  suspect that one of these variables (you aren't sure which) sometimes takes
  9528.  the wrong value. With (var1 / var2)  displayed as an expression in the Watch
  9529.  window, you can easily see when this single value changes; you don't have to
  9530.  mentally divide two numbers.
  9531.  
  9532.  You can also display Boolean expressions. For example, if a variable is
  9533.  never supposed to be larger than 100 or less than 25,  (var < 25 || var >
  9534.  100)  evaluates to 1 (true) when  var  goes out-of-bounds.
  9535.  
  9536.  
  9537.  9.3.3  Displaying Arrays and Structures
  9538.  
  9539.  Most program variables are scalar quantities─a single character or a single
  9540.  integer or floating-point value. These appear in the Watch window with the
  9541.  variable name to the left, followed by an equal sign (=) and the current
  9542.  value.
  9543.  
  9544.   You can view arrays and structures in expanded form.
  9545.  
  9546.  Arrays and structures contain multiple values, arranged in one or more
  9547.  layers. They are often referred to as "aggregate" data items. CodeView lets
  9548.  you control how much of these variables is shown; that is, whether all,
  9549.  part, or none of their internal structure is displayed.
  9550.  
  9551.  An array initially appears in the Watch window in this form:
  9552.  
  9553.    +wordholder[]  = [...]
  9554.  
  9555.  The brackets indicate that this variable contains more than one element. The
  9556.  plus sign (+) indicates that the variable has not yet been expanded to
  9557.  display its components.
  9558.  
  9559.  To expand the array, double-click anywhere on the line. You can also
  9560.  position the cursor on the line and press ENTER. For example, if  wordholder
  9561.   is a six-character array containing the word "Basic," the Watch window
  9562.  display changes to the following :
  9563.  
  9564.    -wordholder[]
  9565.       [0]  =  66 'B'
  9566.       [1]  =  97 'a'
  9567.       [2]  =  115 's'
  9568.       [3]  =  105 'i'
  9569.       [4]  =  99 'c'
  9570.       [5]  =  0 ''
  9571.  
  9572.  Note that both the individual character values and their ASCII decimal
  9573.  equivalents are listed. The minus sign (-) indicates no further expansion is
  9574.  possible. To contract the array, double-click on its line (or position the
  9575.  cursor on the line and press ENTER) again.
  9576.  
  9577.  If it is inconvenient to view a character array in this form, cast the
  9578.  variable's name to a character pointer by placing  (char *)  in front of the
  9579.  name. The character array is then displayed as a string delimited by
  9580.  apostrophes.
  9581.  
  9582.  You can display arrays with more than one dimension. Imagine a 5 x 5 integer
  9583.  array named  matrix, whose diagonal elements are the numbers 1 through 5 and
  9584.  whose other elements are zero. Unexpanded, the array is displayed like this:
  9585.  
  9586.  
  9587.    +matrix[]  = [...]
  9588.  
  9589.  Double-clicking on  matrix  (or pressing ENTER) changes the display:
  9590.  
  9591.    -matrix[]
  9592.      +[0][]  =  [...]
  9593.      +[1][]  =  [...]
  9594.      +[2][]  =  [...]
  9595.      +[3][]  =  [...]
  9596.      +[4][]  =  [...]
  9597.  
  9598.  The actual values of the elements are not shown yet. You have to descend one
  9599.  more level to see them. To view the elements of the third row of the array,
  9600.  position the cursor anywhere on the fourth line and press ENTER:
  9601.  
  9602.    -matrix[]
  9603.      +[0][]  =  [...]
  9604.      +[1][]  =  [...]
  9605.      -[2][]
  9606.         [0]  = 0
  9607.         [1]  = 0
  9608.         [2]  = 3
  9609.         [3]  = 0
  9610.         [4]  = 0
  9611.      +[3][]  =  [...]
  9612.      +[4][]  =  [...]
  9613.  
  9614.  Expanding the fifth row of the array produces this display:
  9615.  
  9616.    -matrix[]
  9617.      +[0][]  =  [...]
  9618.      +[1][]  =  [...]
  9619.      -[2][]
  9620.         [0]  = 0
  9621.         [1]  = 0
  9622.         [2]  = 3
  9623.         [3]  = 0
  9624.         [4]  = 0
  9625.      +[3][]  =  [...]
  9626.      -[4][]
  9627.         [0]  = 0
  9628.         [1]  = 0
  9629.         [2]  = 0
  9630.         [3]  = 0
  9631.         [4]  = 5
  9632.  
  9633.   You can view individual elements instead of  the entire array.
  9634.  
  9635.  Any element of an array (or structure) can be independently expanded or
  9636.  contracted. If you only want to view one or two elements of a large array,
  9637.  specify the particular array or structure elements in the Expression field
  9638.  of the Add Watch dialog box; you need not display every element of the
  9639.  variable.
  9640.  
  9641.   You can dereference pointers.
  9642.  
  9643.  You can dereference a pointer in the same way as you expand an array or
  9644.  structure. The pointer address is displayed, followed by all the elements of
  9645.  the variable to which the pointer currently refers. Multiple levels of
  9646.  indirection (that is, pointers referencing other pointers) can be displayed
  9647.  simultaneously.
  9648.  
  9649.  
  9650.  9.3.4  Displaying Array Elements Dynamically
  9651.  
  9652.  You do not have to display every element of an array. If specific subscripts
  9653.  are given, the corresponding element is displayed.
  9654.  
  9655.  You can also specify a dynamic array element, which changes as some other
  9656.  variable changes. For example, suppose that the loop variable  p  is a
  9657.  subscript for the array variable  catalogprice. The Watch window expression
  9658.  catalogprice[p]  displays only the array element currently specified by p,
  9659.  not the entire array.
  9660.  
  9661.  You can mix constant and variable subscripts. For example, the expression
  9662.  bigarray[3][i]  displays only the element in the third row of the array to
  9663.  which the index variable  i  points.
  9664.  
  9665.  
  9666.  9.3.5  Using Quick Watch
  9667.  
  9668.  Selecting the Quick Watch command from the Watch menu (or pressing SHIFT+F9)
  9669.  displays the Quick Watch dialog box. If the text cursor is in the Source,
  9670.  Local, or Watch window, the variable at the current cursor position appears
  9671.  in the dialog box. If this is not the item you wish to display, type in the
  9672.  desired expression or variable, then press ENTER. The selected item is
  9673.  displayed immediately.
  9674.  
  9675.  The Quick Watch display automatically expands arrays and structures to their
  9676.  first level. For example, an array with three dimensions is expanded to the
  9677.  first dimension. You can expand or contract an element just as you would in
  9678.  the Watch window: position the cursor on the appropriate line and press
  9679.  ENTER. If the array needs more lines than the Quick Watch window can
  9680.  display, drag the mouse along the scroll bar, or press DOWN or PGDN to view
  9681.  the rest of the array.
  9682.  
  9683.   You can add Quick Watch variables to the Watch window.
  9684.  
  9685.  If you decide to add a Quick Watch item to the Watch window, select the Add
  9686.  Watch button. Arrays and structures appear in the Watch window expanded as
  9687.  they were displayed in the Quick Watch box.
  9688.  
  9689.  Quick Watch is a convenient way to take a quick look at a variable or
  9690.  expression. Since only one Quick Watch variable can be viewed at a time, you
  9691.  would not use Quick Watch for most of the variables you want to view.
  9692.  
  9693.  
  9694.  9.3.6  Displaying Memory
  9695.  
  9696.  Selecting the Memory command from the View menu opens a Memory window. Up to
  9697.  two Memory windows can be open at one time.
  9698.  
  9699.  By default, memory is displayed as hexadecimal byte values, with 16 bytes
  9700.  per line. At the end of each line is a second display of the same memory in
  9701.  ASCII form. Values that correspond to printable ASCII characters (decimal 32
  9702.  through 127) are displayed in that form. Values outside this range are shown
  9703.  as periods.
  9704.  
  9705.   You can display memory  values in any form.
  9706.  
  9707.  Byte values are not always the most convenient way to view memory. If the
  9708.  area of memory you're examining contains character strings or floating-point
  9709.  values, you might prefer to view them in a directly readable form. The
  9710.  Memory Window command of the Options menu displays a dialog box with a
  9711.  variety of display options:
  9712.  
  9713.  
  9714.    ■   ASCII characters
  9715.  
  9716.    ■   Byte, word, or double-word binary values
  9717.  
  9718.    ■   Signed or unsigned integer decimal values
  9719.  
  9720.    ■   Short (32 bit), long (64 bit), or ten-byte (80 bit) floating-point
  9721.        values
  9722.  
  9723.  
  9724.  You can also directly cycle through these display formats by pressing F3.
  9725.  
  9726.  If a section of memory cannot be displayed as a valid floating-point number,
  9727.  the number shown includes the characters  NAN  (not a number).
  9728.  
  9729.  
  9730.  Displaying Variables with a Live Expression
  9731.  
  9732.  Section 9.3.4, "Displaying Array Elements Dynamically," explains how to
  9733.  display a specific array element by adding the appropriate expression to the
  9734.  Watch window. It is also possible to watch a particular memory area that
  9735.  your program uses to store data in the Memory window. This CodeView display
  9736.  feature is called a "live expression."
  9737.  
  9738.  "Live" means that the area of memory displayed changes to reflect the value
  9739.  of a pointer or subscript. For example, if  buffer  is an array and  pbuf
  9740.  is a pointer to that array, then  *pbuf  points to the array element
  9741.  currently referenced. A live expression displays the section of memory
  9742.  beginning with this element. If your program changes the value of  pbuf,
  9743.  CodeView dynamically adjusts the Memory window display.
  9744.  
  9745.  Live expressions are displayed in a Memory window, not in the Watch window.
  9746.  To create a live expression, select the Memory Window command of the Options
  9747.  menu, then select the Live Expression check box. Enter the name of the
  9748.  element you want to view. For example, if  strgptr  is a pointer to an array
  9749.  of characters, and you want to see what it currently points at, enter
  9750.  *strgptr. Then select the OK button or press ENTER to view that memory area.
  9751.  
  9752.  
  9753.  A new Memory window opens. The first memory location in the window is the
  9754.  first memory location of the live expression. The section of memory
  9755.  displayed changes to the section the pointer currently references.
  9756.  
  9757.  You can use the Memory Window command of the Options menu to display the
  9758.  value of the live expression in a directly readable form. This is especially
  9759.  convenient when the live expression represents strings or floating-point
  9760.  values, which are difficult to interpret in hexadecimal form.
  9761.  
  9762.  It is usually more convenient to view an item in the Watch window than as a
  9763.  live expression. However, some items are more easily viewed as live
  9764.  expressions. For example, you can examine what is currently on top of the
  9765.  stack. Enter SS:SP as the live expression.
  9766.  
  9767.  
  9768.  9.3.7  Displaying the Processor Registers
  9769.  
  9770.  Selecting the Register command from the View menu (or pressing F2) opens a
  9771.  window on the right side of the screen. The current values of the
  9772.  microprocessor's registers appear in this window.
  9773.  
  9774.  At the bottom of the window is a group of mnemonics representing the
  9775.  processor flags. When you first open the Register window, all values are
  9776.  shown in normal-intensity video. Any subsequent changes are marked in
  9777.  high-intensity video. For example, suppose the overflow flag is not set when
  9778.  the Register window is first opened. The corresponding mnemonic is NV and it
  9779.  appears in light gray. If the overflow flag is subsequently set, the
  9780.  mnemonic changes to OV and appears in bright white.
  9781.  
  9782.  Selecting the 386 Instructions command from the Options menu displays the
  9783.  registers as 32-bit values, but only if your computer uses an 80386
  9784.  processor, and only when running the real-mode version of CodeView.
  9785.  Selecting this command a second time toggles back to a 16-bit display.
  9786.  
  9787.  You can also display the registers of an 8087/287/387 coprocessor in a
  9788.  separate window by selecting the 8087 command from the View menu. If your
  9789.  program uses the coprocessor emulator, the emulated registers are displayed
  9790.  instead.
  9791.  
  9792.  
  9793.  9.3.8  Modifying the Values of Variables, Registers, and Memory
  9794.  
  9795.  You can easily change the values of variables, memory locations, or
  9796.  registers displayed in the Watch, Local, Memory, Register, or 8087 windows.
  9797.  Simply position the cursor at the value you want to change and edit it to
  9798.  the appropriate value. If you change your mind, press ALT+BKSP to undo the
  9799.  last change you made.
  9800.  
  9801.  The starting address of each line of memory displayed is shown at the left
  9802.  of the Memory window, in CS:IP form. Altering the address automatically
  9803.  shifts the display to the corresponding section of memory. If that section
  9804.  is not used by your program, memory locations are displayed as double
  9805.  question marks (??).
  9806.  
  9807.   Byte display form is different from other forms.
  9808.  
  9809.  When you select Byte display from the Memory Window Options dialog box,
  9810.  CodeView presents both a hexadecimal and an ASCII representation of the data
  9811.  in memory. (Byte display is the default.) You can change data in memory
  9812.  either by entering new hex values over the hexadecimal representation of
  9813.  your data or by entering character values over the character representation.
  9814.  
  9815.  
  9816.  To toggle a processor flag, click left on its mnemonic. You can also
  9817.  position the cursor on a mnemonic, then press any key (except TAB or SPACE).
  9818.  Repeat to restore the flag to its previous setting.
  9819.  
  9820.   Be cautious when modifying memory or a register.
  9821.  
  9822.  The effect of changing a register, flag, or memory location may vary from no
  9823.  effect at all, to crashing the operating system. You should be cautious when
  9824.  altering "machine-level" values; most of the items you would want to change
  9825.  can be altered from the Watch window.
  9826.  
  9827.  One instance where direct manipulation of register values can be valuable is
  9828.  when you are debugging in-line assembly code. You can change register values
  9829.  to test assumptions before making changes in your source code and
  9830.  recompiling.
  9831.  
  9832.  
  9833.  9.4  Controlling Execution
  9834.  
  9835.  There are two forms of program execution under CodeView:
  9836.  
  9837.  
  9838.    ■   Continuous; the program executes until either a previously specified
  9839.        "breakpoint" has been reached or the program terminates normally.
  9840.  
  9841.    ■   Single-step; the program pauses after each line of code has been
  9842.        executed.
  9843.  
  9844.  
  9845.  Sections 9.4.1 and 9.4.2 explain how each form of execution works and the
  9846.  most effective way to use each.
  9847.  
  9848.  
  9849.  9.4.1  Continuous Execution
  9850.  
  9851.  Continuous execution lets you quickly execute the bug-free sections of code,
  9852.  which would otherwise take a long time to execute a single step at a time.
  9853.  
  9854.  The simplest form of continuous execution is to click right (position the
  9855.  mouse pointer and press the right mouse button) anywhere on the line of code
  9856.  you want to debug or examine in more detail. The program executes at full
  9857.  speed up to the beginning of this line, then pauses. You can do the same
  9858.  thing by positioning the text cursor on this line, then pressing F7.
  9859.  
  9860.  You can also pause execution at a specific line of code with a "breakpoint."
  9861.  There are several types of breakpoints. Breakpoints are explained in the
  9862.  following section.
  9863.  
  9864.  
  9865.  Selecting Breakpoint Lines
  9866.  
  9867.   Breakpoints can be  tied to lines of code.
  9868.  
  9869.  You can skip over the parts of the program that you don't want to examine by
  9870.  specifying one or more lines as "breakpoints." The program executes at full
  9871.  speed up to the first breakpoint, then pauses. Pressing F5 continues program
  9872.  execution up to the next breakpoint, and so on. (You can halt execution at
  9873.  any time by pressing CTRL+BREAK or ALT+SYSRQ.)
  9874.  
  9875.   There is no limit to the  number of breakpoints.
  9876.  
  9877.  You can set as many breakpoints as you like (limited only by available
  9878.  memory). There are several ways to set breakpoints:
  9879.  
  9880.  
  9881.    ■   Double-click anywhere on the desired breakpoint line. The selected
  9882.        line is highlighted to show that it is a breakpoint. To remove the
  9883.        breakpoint, double-click on the line a second time.
  9884.  
  9885.    ■   Position the cursor anywhere on the line at which you want execution
  9886.        to pause. Press F9 to select the line as a breakpoint. (CodeView
  9887.        highlights lines that have been selected as breakpoints.) Press F9 a
  9888.        second time to remove the breakpoint.
  9889.  
  9890.    ■   Display the Set Breakpoint dialog box by selecting Set Breakpoint from
  9891.        the Watch menu. Choose one of the breakpoint options that permits a
  9892.        line ("location") to be specified. The line on which the text cursor
  9893.        currently rests is the default breakpoint line in the Location field.
  9894.        If this line is not the desired breakpoint, enter the line number
  9895.        desired. (The line number must begin with a period.) Use F9 or the
  9896.        Edit Breakpoints screen of the Watch menu to remove the breakpoint.
  9897.  
  9898.  
  9899.   Not every line can  be a breakpoint.
  9900.  
  9901.  A breakpoint line must be a program line that represents executable code.
  9902.  You cannot select a blank line, a comment line, or a declaration line (such
  9903.  as a variable declaration or a preprocessor statement) as a breakpoint.
  9904.  
  9905.  A breakpoint can also be set at a function or an explicit address. To set a
  9906.  breakpoint at a function, simply enter its name in the Set Breakpoint dialog
  9907.  box. To set a breakpoint at an address, enter the address in CS:IP form.
  9908.  
  9909.  ────────────────────────────────────────────────────────────────────────────
  9910.  NOTE
  9911.  
  9912.  By default, Microsoft compilers optimize your code. In the process of
  9913.  optimization, some lines of code may be repositioned or reorganized for more
  9914.  efficient execution. These changes can prevent CodeView from recognizing the
  9915.  corresponding lines of source code as breakpoints. Therefore, it is a good
  9916.  idea to disable optimization during development (use the /Od switch). You
  9917.  can restore optimization once debugging is completed.
  9918.  ────────────────────────────────────────────────────────────────────────────
  9919.  
  9920.  Once execution has paused, you can continue execution by pressing F5 or
  9921.  clicking left on the <F5> button in the display.
  9922.  
  9923.  
  9924.  Setting Breakpoint Values
  9925.  
  9926.   Breakpoints can be  tied to variables.
  9927.  
  9928.  Breakpoints are not limited to specific lines of code. CodeView can also
  9929.  break execution when a variable reaches a particular value, or just changes
  9930.  value. You can also combine these value breakpoints with line breakpoints,
  9931.  so that execution stops at a specific line only if a variable has
  9932.  simultaneously reached a particular value, or changed value. You must use
  9933.  the check boxes in the Set Breakpoint dialog box to select these other types
  9934.  of breakpoints.
  9935.  
  9936.  To pause execution when an expression reaches a particular value, enter that
  9937.  expression in the Expression field of the Set Breakpoint dialog box. For
  9938.  example, assume you have declared a tree structure as follows:
  9939.  
  9940.    struct Tagtree
  9941.    {
  9942.        char * s;                /* Pointer to a string */
  9943.        struct TAGtree * left;   /* Pointer to left branch */
  9944.        struct TAGtree * right;  /* Pointer to right branch */
  9945.    };
  9946.  
  9947.    struct TAGtree t;
  9948.  
  9949.  You can then pause execution when your tree traversal reaches a terminal
  9950.  node by entering the expression  (t.left == NULL) || (t.right == NULL).
  9951.  
  9952.  To pause execution when a variable changes value, you need to enter only the
  9953.  name of the variable in the Expression field. For large variables (such as
  9954.  arrays or character strings), you can specify the number of bytes you want
  9955.  checked (up to 32K) in the Length field.
  9956.  
  9957.  ────────────────────────────────────────────────────────────────────────────
  9958.  NOTE
  9959.  
  9960.  When a breakpoint is tied to a variable, CodeView must check the variable's
  9961.  value after each machine instruction is executed. This slows execution
  9962.  greatly. For maximum speed when debugging, either tie conditional
  9963.  breakpoints to specific lines, or set conditional breakpoints only after you
  9964.  have reached the section of code that needs to be debugged.
  9965.  ────────────────────────────────────────────────────────────────────────────
  9966.  
  9967.  
  9968.  Using Breakpoints
  9969.  
  9970.  Here are several examples that show how breakpoints can help you find the
  9971.  cause of a problem.
  9972.  
  9973.  One of the most common bugs is a for loop that executes too many or too few
  9974.  times. If you set a breakpoint that encloses the loop statements, the
  9975.  program pauses after each iteration. With the loop variable or critical
  9976.  program variables in the Watch or Local windows, it should be easy to see
  9977.  what the loop is doing wrong.
  9978.  
  9979.   You can specify how  many times a breakpoint  line is executed.
  9980.  
  9981.  You do not have to pause at a breakpoint the first time execution reaches
  9982.  it. CodeView lets you specify the number of times you want to ignore the
  9983.  breakpoint condition before pausing. Enter the decimal number in the Pass
  9984.  Count field of the Set Breakpoint dialog box of the Watch menu.
  9985.  
  9986.  For example, suppose your program repeatedly calls a function to create a
  9987.  binary tree. You suspect that something goes wrong with the process about
  9988.  halfway through. You could mark the line that calls the function as the
  9989.  breakpoint, then specify how many times this line is to execute before
  9990.  execution pauses. Running the program creates a representative (but
  9991.  unfinished) tree structure that can be examined from the Watch window. You
  9992.  can then continue your analysis using single-stepping.
  9993.  
  9994.  Another programming error is erroneously assigning a value to a variable.
  9995.  Enter the variable in the Expression field of the Set Breakpoint dialog box.
  9996.  Execution breaks whenever this variable changes value.
  9997.  
  9998.   You can assign new  values to variables while  execution is paused.
  9999.  
  10000.  Breakpoints are a convenient way to pause the program so you can assign new
  10001.  values to variables. For example, if a limit value is set by a variable, you
  10002.  can change the value to see whether program execution is affected.
  10003.  Similarly, you can pass a variety of values to a switch statement to see if
  10004.  they are correctly processed.
  10005.  
  10006.  This ability to alter variables is an especially convenient way to test new
  10007.  functions without having to write a stand-alone test program.
  10008.  
  10009.  
  10010.  9.4.2  Single-Stepping
  10011.  
  10012.  In single-stepping, CodeView pauses after each line of code is executed. (If
  10013.  a line contains more than one executable statement, CodeView executes all
  10014.  the statements on the line before pausing.) The next line to be executed is
  10015.  highlighted in reverse video.
  10016.  
  10017.   There are two ways  to single-step.
  10018.  
  10019.  You can single-step through a program with the Step and Trace functions.
  10020.  Step (executed by pressing F10) steps over function calls. All the code in
  10021.  the function is executed but, to you, the function appears to execute as a
  10022.  single step. Trace (executed by pressing F8) traces through every step of
  10023.  all functions for which CodeView has symbolic information. Each line of the
  10024.  function is executed as a separate step. (CodeView has no symbolic
  10025.  information about run-time functions; therefore, they are executed as a
  10026.  single step.)
  10027.  
  10028.  You can alternate between Trace and Step as you like. The method you use
  10029.  depends only on whether you want to see what happens within a particular
  10030.  function.
  10031.  
  10032.  You can Trace through the program continuously (without having to press F8),
  10033.  using the Animate command of the Run menu. The speed of execution is
  10034.  controlled by the Trace Speed command from the Options menu. You can halt
  10035.  animated execution at any time by pressing any key.
  10036.  
  10037.  
  10038.  9.5  Replaying a Debug Session
  10039.  
  10040.  CodeView can automatically create a "tape" (a disk file) with all the
  10041.  debugging instructions and input data you entered when testing a program.
  10042.  The tape is then "replayed" to repeat the debugging process. This dynamic
  10043.  replay feature is unique to the CodeView debugger and is activated by
  10044.  selecting the History On command from the Run menu. Selecting History On a
  10045.  second time terminates  recording.
  10046.  
  10047.  You can use the recording as a bookmark. You can quit after a long debugging
  10048.  session, then pick up the session later in the same place.
  10049.  
  10050.   Dynamic replay makes it  easy to correct a mistake.
  10051.  
  10052.  The principal use of dynamic replay is to allow you to back up when you make
  10053.  an error or overshoot the section of code with the bug. This feature is
  10054.  important because not all bugs are located when executing the program in a
  10055.  linear fashion.
  10056.  
  10057.  For example, you may have to manually execute a function many times before
  10058.  its bug appears. If you then enter a command that alters the machine's or
  10059.  program's status and thereby lose the information you need to find the cause
  10060.  of the bug, you would have had to restart the program and manually repeat
  10061.  every debugging step to return to that point. Even worse, if you don't
  10062.  remember the exact sequence of events that exposed the bug, it could take
  10063.  hours to find your way back.
  10064.  
  10065.  Dynamic replay eliminates this problem. Selecting the Undo command from the
  10066.  Run menu automatically restarts the program and rapidly executes every debug
  10067.  command up to (but not including) the last one you entered. You can repeat
  10068.  this process as many times as you like until you return to the desired point
  10069.  in execution.
  10070.  
  10071.  To add additional steps to an existing tape, select History On, then select
  10072.  Replay. When replay has completed, perform whatever new debugging steps you
  10073.  want, then select History On a second time to terminate recording. The new
  10074.  tape contains both the original and the added commands.
  10075.  
  10076.  ────────────────────────────────────────────────────────────────────────────
  10077.  NOTE
  10078.  
  10079.  CodeView records only those mouse commands that apply to CodeView. Mouse
  10080.  commands recognized by the application being debugged are not recorded.
  10081.  ────────────────────────────────────────────────────────────────────────────
  10082.  
  10083.  
  10084.  Replay Limitations under OS/2
  10085.  
  10086.  There are some limitations to dynamic replay when debugging under OS/2:
  10087.  
  10088.  
  10089.    ■   The program must not respond to asynchronous events.
  10090.  
  10091.    ■   Breakpoints must be specified at specific source lines or for specific
  10092.        symbols (rather than by absolute addresses), or replay may fail.
  10093.  
  10094.    ■   Single-thread programs behave normally during replay. However, one of
  10095.        the threads in a multithread program may cause an asynchronous event,
  10096.        violating the first restriction. Multithread programs are, therefore,
  10097.        more likely to fail during replay.
  10098.  
  10099.    ■   Multiprocess replay will fail. Each new process invokes a new CodeView
  10100.        session. The existence of multiple sessions makes it impractical to
  10101.        record the sequence of events if you execute commands in a session
  10102.        other than the original.
  10103.  
  10104.    ■   Replay under Presentation Manager is not currently supported because
  10105.        it violates the first restriction.
  10106.  
  10107.  
  10108.  
  10109.  9.6  Advanced CodeView Techniques
  10110.  
  10111.  Once you are comfortable displaying and changing variables, stepping through
  10112.  the program, and using dynamic replay, you might want to experiment with the
  10113.  advanced techniques explained below.
  10114.  
  10115.  
  10116.  Setting Command-Line Arguments
  10117.  
  10118.  If your program retrieves command-line arguments, you can specify them with
  10119.  the Set Runtime Arguments command from the Run menu. Enter the arguments in
  10120.  the Command Line field before you begin execution. (Arguments entered after
  10121.  execution begins cause an automatic restart.)
  10122.  
  10123.  
  10124.  Multiple Source Windows
  10125.  
  10126.  You can open two Source windows at the same time. The windows can display
  10127.  two different sections of the same program, or one can show the high-level
  10128.  listing and the other the assembly-language listing. In the latter case, the
  10129.  contents of the windows track, with the next assembly-language instruction
  10130.  to be executed matching the next line of source code.
  10131.  
  10132.  You can move freely between these windows, executing a single line of source
  10133.  code or a single assembly instruction at a time. The assembly-language
  10134.  window must be opened in CS:IP mode.
  10135.  
  10136.  
  10137.  Calling Functions
  10138.  
  10139.  Any C function in your program (whether user-written or from the library)
  10140.  can be called from the Command window or the Watch window, using the
  10141.  following format:
  10142.  
  10143.    ?funcname (varlist)
  10144.  
  10145.  The function is evaluated and the returned value is displayed in the Command
  10146.  window.
  10147.  
  10148.  The function does not have to be called by your program to be available for
  10149.  evaluation. For example, all the .OBJ code specified in the linker input
  10150.  response file is linked. The functions in this code can then be evaluated
  10151.  from the Command window.
  10152.  
  10153.  This feature allows you to run functions from within CodeView that you would
  10154.  not normally include in the final version of your program. For example, you
  10155.  could include the OS/2 API functions that control semaphores, then execute
  10156.  them from the Command window to manipulate the run-time environment at any
  10157.  point in the debugging process.
  10158.  
  10159.  
  10160.  Checking for Undefined Pointers
  10161.  
  10162.  Until a pointer has been explicitly assigned a value, its value is
  10163.  undefined. That is, its value may be completely random, or it may be some
  10164.  consistent value that does not point to a useful data address (such as -1).
  10165.  
  10166.  
  10167.  Accessing data through an uninitialized pointer will cause unpredictable
  10168.  program behavior and, under OS/2, will usually result in a protection
  10169.  violation. Because many C programs use pointers heavily, tracking down
  10170.  exactly which pointer variable was left uninitialized is tedious.
  10171.  
  10172.  CodeView can help locate the problem quickly. If you use an uninitialized
  10173.  pointer (or "null pointer" under OS/2) the operating system will generate a
  10174.  protection violation. By examining the Calls menu, you can determine the
  10175.  last line of your code that was executed before the protection violation
  10176.  occurred.
  10177.  
  10178.  Under DOS, you can take advantage of the fact that global or static
  10179.  variables are initialized to 0 to track down uninitialized pointers. Set a
  10180.  conditional breakpoint that stops when location 0 changes, then start
  10181.  execution. Execution will pause when your program makes an assignment to
  10182.  that location.
  10183.  
  10184.  ────────────────────────────────────────────────────────────────────────────
  10185.  NOTE
  10186.  
  10187.  For near pointers, location 0 is DS:0000; for far pointers, location 0 is
  10188.  0000:0000.
  10189.  ────────────────────────────────────────────────────────────────────────────
  10190.  
  10191.  
  10192.  Using Breakpoints Efficiently
  10193.  
  10194.  Breakpoints slow execution when debugging. You can increase CodeView's speed
  10195.  by using the /R command-line switch if you have an 80386-based computer.
  10196.  This switch enables the 386's four debug registers, which support breakpoint
  10197.  checking in hardware rather than in software.
  10198.  
  10199.  
  10200.  Printing Selected Items
  10201.  
  10202.  You can print all or part of the contents of any window with the Print
  10203.  command from the File menu. The check box lets you print the complete
  10204.  contents of the window, only the material that is currently viewable in the
  10205.  window, or selected text from the window. Text is selected by dragging the
  10206.  mouse across it, or by holding down the SHIFT key and pressing the direction
  10207.  keys (LEFT, RIGHT, UP, DOWN).
  10208.  
  10209.  By default, print output is to the file CODEVIEW.LST in the current
  10210.  directory. You can choose whether the new material will be appended to an
  10211.  existing file or overwrite it, using the Append/Overwrite check box. If you
  10212.  would like print output to go to a different file, type its name in the To
  10213.  File Name field. If you want the output to go to a printer, enter the
  10214.  appropriate device name, such as LPT1 or COM2.
  10215.  
  10216.  
  10217.  Handling Register Variables
  10218.  
  10219.  A register variable is stored in one of the microprocessor's registers,
  10220.  rather than in RAM. This speeds access to the variable.
  10221.  
  10222.  There are two ways for a conventional variable to become a register
  10223.  variable. One way is declaring the variable as a register variable; if a
  10224.  register is free, the compiler will store the variable there. The other way
  10225.  occurs during optimization, when the compiler stores an often-used variable
  10226.  (such as a loop variable) in a register to speed up execution.
  10227.  
  10228.  Register variables can cause problems during debugging. As with local
  10229.  variables, they are only visible within the function where they are defined.
  10230.  In addition, a register variable may not always be displayed with its
  10231.  current value.
  10232.  
  10233.  In general, it is a good idea to turn off all optimization and to avoid
  10234.  declaring register variables until the program has been fully debugged. Any
  10235.  side effects produced by optimization or register variables can then be
  10236.  easily isolated.
  10237.  
  10238.  
  10239.  Redirecting CodeView Input and Output
  10240.  
  10241.  The Command window accepts DOS-like commands that redirect input and output.
  10242.  These commands can also be included on the command line that invokes
  10243.  CodeView. Whatever follows the /C option in the command line is treated as
  10244.  CodeView commands that are immediately executed at start-up.
  10245.  
  10246.    CV/c "infile; t >outfile" myprog
  10247.  
  10248.  Input is redirected to  infile, which can contain start-up commands for
  10249.  CodeView. When CodeView exhausts all commands in the input file, focus
  10250.  automatically shifts to the command window. Output is sent to  outfile  and
  10251.  echoed to the Command window. The  t  must precede the  >  command for
  10252.  output to be sent to the Command window.
  10253.  
  10254.  Redirection is a useful way to automate CodeView start-up. It also lets you
  10255.  keep a viewable record of command-line input and output, a feature not
  10256.  available with dynamic replay. (No record is kept of mouse operations.) Some
  10257.  applications (particularly interactive ones) may need modification to allow
  10258.  for redirection of input to the application itself.
  10259.  
  10260.  
  10261.  Using CodeView with Additional Memory
  10262.  
  10263.  If your computer uses expanded or extended memory, you can increase
  10264.  CodeView's functionality by selecting the /X or /E option. CodeView moves as
  10265.  much as it can of itself, the debugging table, and the program to higher
  10266.  memory (above the first megabyte).
  10267.  
  10268.  The /X option uses extended memory and gives the greatest speed increase.
  10269.  This option requires the HIMEM.SYS driver, which is included on your
  10270.  distribution disks. Add  DEVICE = HIMEM.SYS  to your CONFIG.SYS file to load
  10271.  HIMEM.SYS at boot time.
  10272.  
  10273.  The /E option uses expanded memory. The speed increase is not as great as
  10274.  that supplied by the /X option. The expanded memory manager (EMM) must be
  10275.  LIM 4.0, and no single module's debug information can exceed 48K. If the
  10276.  symbol table exceeds this limit, try reducing file-name information by not
  10277.  specifying paths at compile time and using /Zi only with those sections of
  10278.  the program that need debugging (use /Zd otherwise).
  10279.  
  10280.  If you do not specify either /X or /E (or the /D disk-overlay option),
  10281.  CodeView automatically searches for the HIMEM.SYS driver and extended memory
  10282.  so it can implement the /X option. If it fails, CodeView searches for
  10283.  expanded memory to implement the /E option. If that search fails, CodeView
  10284.  uses a default disk overlay of 64K. (See the description of the /D option
  10285.  below.)
  10286.  
  10287.  
  10288.  9.7  Controlling CodeView with Command-Line Options
  10289.  
  10290.  The following options can be added to the command line that invokes
  10291.  CodeView:
  10292.  
  10293.  Option                            Effect
  10294.  ────────────────────────────────────────────────────────────────────────────
  10295.  /2                                Two-monitor debugging. The display
  10296.                                    adapters must be configured for
  10297.                                    different addresses. One display shows
  10298.                                    the output of the application; the other
  10299.                                    shows CodeView.
  10300.  
  10301.  /25                               Display in 25-line mode.
  10302.  
  10303.  /43                               Display in 43-line mode (EGA or VGA
  10304.                                    only).
  10305.  
  10306.  /50                               Display in 50-line mode (VGA only).
  10307.  
  10308.  /B                                Display in black and white. This assures
  10309.                                    that the display is readable when a
  10310.                                    color display is not used.
  10311.  
  10312.  /Ccommands                        All items following this switch are
  10313.                                    treated as CodeView commands to be
  10314.                                    executed immediately on start-up.
  10315.                                    Commands must be separated with a
  10316.                                    semicolon (;).
  10317.  
  10318.  /D«ddd»                           Use disk overlays, where ddd is the
  10319.                                    decimal size of the overlay buffer, in
  10320.                                    kilobytes. The acceptable range is 16K
  10321.                                    to 128K. The default size is 64K. DOS
  10322.                                    only.
  10323.  
  10324.  /E                                Use expanded memory for symbolic
  10325.                                    information. DOS only.
  10326.  
  10327.  /F                                Flip screen video pages. When your
  10328.                                    application does not use graphics, eight
  10329.                                    video screen pages are available.
  10330.                                    Switching from CodeView to the output
  10331.                                    screen is accomplished more quickly than
  10332.                                    swapping (/S) by directly selecting the
  10333.                                    appropriate video page. Cannot be used
  10334.                                    with /S. DOS only.
  10335.  
  10336.  /Inumber                          Turns nonmaskable interrupts and
  10337.                                    8259-interrupt trapping on (/I1) or off
  10338.                                    (/I2).
  10339.  
  10340.  /K                                Disables installation of keyboard
  10341.                                    monitors for the program being debugged.
  10342.  
  10343.  /Ldlls                            Load DLLs specified. DLLs must be
  10344.                                    separated by a semicolon (;). OS/2 only.
  10345.  
  10346.  /M                                Disable the mouse.
  10347.  
  10348.  /Nnumber                          /N0 tells CodeView to trap; /N1 tells it
  10349.                                    not to.
  10350.  
  10351.  /O                                Debug child processes ("offspring").
  10352.                                    OS/2 only.
  10353.  
  10354.  /R                                Use 386 hardware debug registers. DOS
  10355.                                    only.
  10356.  
  10357.  /S                                Swap screen in buffers. When your
  10358.                                    program uses graphics, all eight screen
  10359.                                    buffers must be used. Switching from
  10360.                                    CodeView to the output screen is
  10361.                                    accomplished by saving the previous
  10362.                                    screen in a buffer. Cannot be used with
  10363.                                    /F. DOS only.
  10364.  
  10365.  /X                                Use extended memory for symbolic
  10366.                                    information. DOS only.
  10367.  
  10368.  
  10369.  9.8  Customizing CodeView with the TOOLS.INI FILE
  10370.  
  10371.  The TOOLS.INI file customizes the behavior and user interface of several
  10372.  Microsoft products. The TOOLS.INI file is a plain ASCII text file. You
  10373.  should place it in a directory pointed to the INIT environment variable. (If
  10374.  you do not use the INIT environment variable, CodeView looks for TOOLS.INI
  10375.  only in its source directory.)
  10376.  
  10377.  The CodeView section of TOOLS.INI is preceded by the following line:
  10378.  
  10379.    [cv]
  10380.  
  10381.  If you are running the protected-mode version of CodeView, use  [cvp]
  10382.  instead. If you run both versions, include both:  [cv cvp].
  10383.  
  10384.  Most of the TOOLS.INI customizations control screen colors, but you can also
  10385.  specify such things as start-up commands or the name of the file that
  10386.  receives CodeView output. On-line help contains full information about all
  10387.  TOOLS.INI switches for CodeView.
  10388.  
  10389.  
  10390.  
  10391.  
  10392.  
  10393.  
  10394.  PART III  Special Environments
  10395.  ────────────────────────────────────────────────────────────────────────────
  10396.  
  10397.  The Microsoft C Professional Development System provides a platform from
  10398.  which you can build graphics applications and interface with programs
  10399.  written in other languages.
  10400.  
  10401.  Chapter 10 discusses using the real-world graphics functions to set video
  10402.  modes, draw basic shapes, and use graphic fonts. Chapter 11 describes
  10403.  "presentation graphics," sophisticated charts and graphics that show data
  10404.  relationships. Chapter 12 explains how to write C programs so that they
  10405.  interface with assembly language routines or routines written in other
  10406.  languages. Chapter 13 describes portability of Microsoft C to other
  10407.  environments.
  10408.  
  10409.  
  10410.  
  10411.  
  10412.  
  10413.  
  10414.  Chapter 10  Communicating with Graphics
  10415.  ────────────────────────────────────────────────────────────────────────────
  10416.  
  10417.  A map, a chart, an illustration, a graph, or some other visual aid often can
  10418.  communicate more information more quickly and more vividly than would
  10419.  several screens of text.
  10420.  
  10421.  The extensive Microsoft C graphics library allows you to communicate your
  10422.  ideas graphically. The functions range from the simple to the complex; from
  10423.  functions that turn on a pixel to functions that draw graphs and charts
  10424.  complete with labels and legends.
  10425.  
  10426.  This chapter describes low-level graphics functions that draw basic shapes
  10427.  such as lines, circles, and rectangles. It introduces video modes, color
  10428.  palettes, coordinate systems, and synopses of the graphics and font
  10429.  functions. For complete function prototypes and example programs, use
  10430.  on-line help.
  10431.  
  10432.  ────────────────────────────────────────────────────────────────────────────
  10433.  NOTE
  10434.  
  10435.  The ANSI C standard does not define any standard graphics functions. The
  10436.  functions described in this section are unique to Microsoft C and are not
  10437.  portable to other implementations of C.
  10438.  ────────────────────────────────────────────────────────────────────────────
  10439.  
  10440.  
  10441.  10.1  Video Modes
  10442.  
  10443.  Graphics adapters are boards or cards inside the computer that are
  10444.  responsible for displaying text and graphics on the screen. Commonly used
  10445.  adapters include:
  10446.  
  10447.  
  10448.    ■   CGA (Color Graphics Adapter)
  10449.  
  10450.    ■   EGA (Enhanced Graphics Adapter)
  10451.  
  10452.    ■   HGC (Hercules Graphics Card)
  10453.  
  10454.    ■   MCGA (Multicolor Graphics Array)
  10455.  
  10456.    ■   MDPA (Monochrome Display Printer Adapter)
  10457.  
  10458.    ■   VGA (Video Graphics Array)
  10459.  
  10460.  
  10461.  In addition, there are Olivetti versions of the CGA, EGA, and VGA (called
  10462.  OCGA, OEGA, and OVGA in this chapter).
  10463.  
  10464.   The video modes available at run time depend on your graphics adapter and
  10465.  monitor.
  10466.  
  10467.  Adapters can enter one or more "video modes." The video mode controls the
  10468.  resolution and number of colors on the video display. Microsoft C supports
  10469.  17 video modes, which fall into two broad categories:
  10470.  
  10471.  
  10472.    ■   "Text modes," where characters are displayed
  10473.  
  10474.    ■   "Graphics modes," where individual pixels can be turned on and off
  10475.  
  10476.  
  10477.  The graphics adapter and the type of monitor in use determine which of the
  10478.  17 video modes are available at run time. See Section 10.1.2 for a list of
  10479.  video modes.
  10480.  
  10481.  
  10482.  10.1.1  Sample Low-Level Graphics Program
  10483.  
  10484.  The program ERESBOX.C below shows, in a few lines, the steps you follow to
  10485.  enter and exit a graphics mode. It sets the video mode _ERESCOLOR, draws a
  10486.  box, waits for a keypress, and returns to default mode, which is the video
  10487.  mode in effect when the program began running.
  10488.  
  10489.    /* ERESBOX.C -- Enters _ERESCOLOR mode and draws a box */
  10490.  
  10491.    #include <graph.h> /* graphics functions */
  10492.    #include <stdio.h> /* puts */
  10493.    #include <conio.h> /* getch */
  10494.  
  10495.    main()
  10496.    {
  10497.       if( _setvideomode( _ERESCOLOR ) ) /* EGA 640x350 mode */
  10498.       {
  10499.          _rectangle( _GBORDER, 10, 10, 110, 110 ); /* draw */
  10500.          getch(); /* wait for a keypress */
  10501.          _setvideomode( _DEFAULTMODE ); /* return to default */
  10502.       } else puts( "Can't enter _ERESCOLOR graphics mode." )
  10503.    }
  10504.  
  10505.  The program above illustrates the steps you follow to display graphics:
  10506.  
  10507.  
  10508.    ■   Include the header file GRAPH.H. It contains function prototypes,
  10509.        macros, useful structures, and symbolic constants such as _ERESCOLOR,
  10510.        _GBORDER, and _DEFAULTMODE.
  10511.  
  10512.        #include <graph.h>
  10513.  
  10514.  
  10515.    ■   Call the _setvideomode function, which sets the desired video mode.
  10516.        The function returns 0 if the hardware does not support the requested
  10517.        mode. (See Section 10.1.2, "Setting a Video Mode.")
  10518.  
  10519.        if( _setvideomode( _ERESCOLOR ) )
  10520.  
  10521.  
  10522.    ■   Draw the graphics on the screen. The example program calls the
  10523.        _rectangle function. (See Section 10.4.3, "Drawing Points, Lines, and
  10524.        Shapes.")
  10525.  
  10526.        _rectangle( _GBORDER, 10, 10, 110, 110 )
  10527.  
  10528.  
  10529.    ■   Exit the graphics mode and return to whatever video mode was in effect
  10530.        before the program began. Call _setvideomode, passing the constant
  10531.        _DEFAULTMODE. In some cases, you might want to skip this step, exiting
  10532.        the program with the graphics screen still in place.
  10533.  
  10534.        _setvideomode( _DEFAULTMODE );
  10535.  
  10536.  
  10537.  
  10538.  In addition, you must link with the GRAPHICS.LIB library, which contains the
  10539.  function code. If you use window-coordinate functions (which require
  10540.  floating-point calculations) and if you have not created a standard combined
  10541.  library containing a floating-point component, you must explicitly link with
  10542.  a floating-point math library.
  10543.  
  10544.  
  10545.  10.1.2  Setting a Video Mode
  10546.  
  10547.  The _setvideomode function turns on one of the 17 available video modes.
  10548.  Pass it a single integer that tells it which mode to display. The constants
  10549.  in Table 10.1 are defined in the GRAPH.H file. The dimensions are listed in
  10550.  pixels for video graphics mode and in columns for video text mode.
  10551.  
  10552.  Table 10.1  Constants that Represent Video Modes
  10553.  
  10554. ╓┌────────────────┌───────────────────────────────────────┌──────────────────╖
  10555.  Constant (Name)  Description                             Mode/Hardware
  10556.  ────────────────────────────────────────────────────────────────────────────
  10557.  _DEFAULTMODE     Restores the original mode              All/All
  10558.  
  10559.  _ERESCOLOR       640  x  350, 4 or 16 color              Graphics/EGA
  10560.  
  10561.  _ERESNOCOLOR     640  x  350, BW                         Graphics/EGA
  10562.  
  10563.  _HRES16COLOR     640  x  200, 16 color                   Graphics/EGA
  10564.  
  10565.  Constant (Name)  Description                             Mode/Hardware
  10566.  ────────────────────────────────────────────────────────────────────────────
  10567. 
  10568.  _HERCMONO*       720  x 348, BW                          Graphics/HGC
  10569.  
  10570.  _HRESBW          640  x  200, BW                         Graphics/CGA
  10571.  
  10572.  _MAXCOLORMODE    Graphics mode with the most colors      Graphics/All┼
  10573.  
  10574.  _MAXRESMODE      Graphics mode with the highest          Graphics/All┼
  10575.                   resolution
  10576.  
  10577.  _MRES4COLOR      320  x  200, 4 color                    Graphics/All
  10578.  
  10579.  _MRES16COLOR     320  x  200, 16 color                   Graphics/EGA
  10580.  
  10581.  _MRES256COLOR    320  x  200, 256 color                  Graphics/VGA
  10582.  
  10583.  _MRESNOCOLOR     320  x  200, 4 gray                     Graphics/CGA
  10584.  
  10585.  _ORESCOLOR       640  x  400, 1 of 16 colors             Graphics/Olivetti
  10586.  Constant (Name)  Description                             Mode/Hardware
  10587.  ────────────────────────────────────────────────────────────────────────────
  10588. _ORESCOLOR       640  x  400, 1 of 16 colors             Graphics/Olivetti
  10589.  
  10590.  _TEXTBW40        40 column text, 16 gray                 Text/CGA
  10591.  
  10592.  _TEXTBW80        80 column text, 16 gray                 Text/CGA
  10593.  
  10594.  _TEXTC40         40 column text, 16/8 color              Text/CGA
  10595.  
  10596.  _TEXTC80         80 column text, 16/8 color              Text/CGA
  10597.  
  10598.  _TEXTMONO        80 column text, BW                      Text/MDPA
  10599.  
  10600.  _VRES2COLOR      640  x  480, BW                         Graphics/VGA
  10601.  
  10602.  _VRES16COLOR     640  x  480, 16 color                   Graphics/VGA
  10603.  
  10604.  ────────────────────────────────────────────────────────────────────────────
  10605.  
  10606.  
  10607.  
  10608.  * Before attempting to enter _HERCMONO mode, you must install the
  10609.  terminate-and-stay-resident program MSHERC.COM, which comes in the Microsoft
  10610.  C package. If you have both a Hercules adapter and an additional graphics
  10611.  adapter in the same computer, use the /H option  to put the Hercules into
  10612.  HALF mode to avoid unpredictable and undesirable results.
  10613.  ┼ _MAXRESMODE and _MAXCOLORMODE support all adapters except the MDPA. See
  10614.  Section
  10615.  
  10616.  
  10617.  If the hardware does not support the selected mode, _setvideomode returns 0.
  10618.  
  10619.  
  10620.  Some graphics adapters are able to enter additional video modes:
  10621.  
  10622.  
  10623.    ■   EGA adapters can display all CGA modes.
  10624.  
  10625.    ■   HGC adapters can enter _TEXTMONO mode.
  10626.  
  10627.    ■   MCGA adapters can display all CGA modes, plus _VRES2COLOR and
  10628.        _MRES256COLOR.
  10629.  
  10630.    ■   VGA adapters can display all EGA and CGA modes.
  10631.  
  10632.  
  10633.  
  10634.  10.1.3  Reading the videoconfig Structure
  10635.  
  10636.  At any time, you can inquire about the current video configuration by
  10637.  passing the _getvideoconfig function a structure of type videoconfig. The
  10638.  structure contains 11 members, all of which are short integers. They are
  10639.  listed in Table 10.2.
  10640.  
  10641.  Table 10.2  Members of a videoconfig Structure
  10642.  
  10643. ╓┌──────────────────────┌────────────────────────────────────────────────────╖
  10644.  Member                 Description
  10645.  ────────────────────────────────────────────────────────────────────────────
  10646.  adapter*               Active display adapter
  10647.  bitsperpixel           Number of bits per pixel
  10648.  memory                 Adapter video memory in kilobytes
  10649.  Member                 Description
  10650.  ────────────────────────────────────────────────────────────────────────────
  10651. memory                 Adapter video memory in kilobytes
  10652.  mode*                  Current video mode
  10653.  monitor*               Active display monitor
  10654.  numcolors              Number of color indexes
  10655.  numtextcols            Number of text columns available
  10656.  numtextrows            Number of text rows available
  10657.  numvideopages          Number of video pages available
  10658.  numxpixels             Number of pixels on the x axis
  10659.  numypixels             Number of pixels on the y axis
  10660.  ────────────────────────────────────────────────────────────────────────────
  10661.  
  10662.  
  10663.  * Possible values for the mode, adapter, and monitor items are listed in the
  10664.  GRAPH.H file.
  10665.  
  10666.  
  10667.  The _getvideoconfig function initializes these values. Most of the values
  10668.  are self-explanatory. For example, if numxpixels holds 640, the current
  10669.  video mode contains 640 horizontal pixels, numbered 0 - 639.
  10670.  
  10671.  The READVC.C example program below illustrates how to initialize and examine
  10672.  a videoconfig structure:
  10673.  
  10674.    /* READVC.C -- Reads the videoconfig structure */
  10675.  
  10676.    #include <graph.h>
  10677.    #include <stdio.h>
  10678.  
  10679.    main()
  10680.    {
  10681.       struct videoconfig vc;
  10682.  
  10683.       _getvideoconfig( &vc );
  10684.       printf( "Text Rows = %i.\n", vc.numtextrows );
  10685.    }
  10686.  
  10687.  First, the program declares a structure  vc  of type videoconfig. Next, it
  10688.  calls _getvideoconfig to initialize the structure. Finally, it prints a
  10689.  member of the structure.
  10690.  
  10691.  
  10692.  10.1.4  Maximizing Resolution or Color
  10693.  
  10694.  Two symbolic constants are new to Microsoft C 6.0: _MAXRESMODE and
  10695.  _MAXCOLORMODE. The first selects the highest possible resolution for the
  10696.  graphics adapter and monitor currently in use. The second selects the
  10697.  graphics mode with the greatest number of colors. The constants work with
  10698.  all graphics adapters except the MDPA. (See Table 10.3.)
  10699.  
  10700.  Table 10.3  Constants for Maximum Resolution and Color
  10701.  
  10702. ╓┌────────────────┌──────────────┌───────────────────────────────────────────╖
  10703.  Adapter/Monitor  _MAXRESMODE    _MAXCOLORMODE
  10704.  ────────────────────────────────────────────────────────────────────────────
  10705.  CGA              _HRESBW        _MRES4COLOR
  10706.  EGA color        _HRES16COLOR   _HRES16COLOR
  10707.  EGA ecd 64K      _ERESCOLOR     _HRES16COLOR
  10708.  EGA ecd 256K     _ERESCOLOR     _ERESCOLOR
  10709.  EGA mono         _ERESNOCOLOR   _ERESNOCOLOR
  10710.  HGC              _HERCMONO      _HERCMONO
  10711.  MCGA             _VRES2COLOR    _MRES256COLOR
  10712.  Adapter/Monitor  _MAXRESMODE    _MAXCOLORMODE
  10713.  ────────────────────────────────────────────────────────────────────────────
  10714. MCGA             _VRES2COLOR    _MRES256COLOR
  10715.  MDPA             Fails          Fails
  10716.  OCGA             _ORESCOLOR     _MRES4COLOR
  10717.  OEGA color       _ORESCOLOR     _ERESCOLOR
  10718.  VGA/OVGA         _VRES16COLOR   _MRES256COLOR
  10719.  ────────────────────────────────────────────────────────────────────────────
  10720.  
  10721.  
  10722.  
  10723.  10.1.5  Selecting Your Own Video Modes
  10724.  
  10725.  A program that will run only on a single machine with a known graphics
  10726.  adapter can enter the appropriate video mode immediately. However, if you
  10727.  attempt to run the program on another machine with a different adapter, it
  10728.  may not run correctly, if at all.
  10729.  
  10730.  If your program might run on a variety of computers and you prefer to select
  10731.  your own video modes, initialize a videoconfig structure by calling the
  10732.  _getvideoconfig function. Then check the adapter member and use a switch
  10733.  statement to enter the selected video mode.
  10734.  
  10735.  For example, suppose you know that a program will run on monochrome systems
  10736.  equipped with either an EGA adapter or a Hercules adapter. To enter the
  10737.  appropriate mode, use code such as this:
  10738.  
  10739.    struct videoconfig vc;
  10740.  
  10741.    _getvideoconfig( &vc );
  10742.  
  10743.    switch( vc.adapter )
  10744.    {
  10745.       case _EGA:
  10746.          _setvideomode( _ERESNOCOLOR );
  10747.          break;
  10748.       case _HGC:
  10749.          _setvideomode( _HERCMONO );
  10750.          break;
  10751.    }
  10752.  
  10753.  
  10754.  10.2  Mixing Colors and Changing Palettes
  10755.  
  10756.  Depending on the graphics card installed and the video mode in effect, you
  10757.  can display 2, 4, 8, 16, or 256 colors on the screen at the same time. You
  10758.  specify a color by selecting a color index (sometimes called a "pixel value"
  10759.  or "color attribute"). The color indexes are numbered from 0 to n-1, where n
  10760.  is the number of colors in the palette.
  10761.  
  10762.  CGA adapters offer four different palettes containing predefined fixed color
  10763.  sets.
  10764.  
  10765.   All video modes that support color offer a color palette.
  10766.  
  10767.  EGA, MCGA, and VGA adapters have palettes that can be redefined to suit your
  10768.  needs. You can change the visible color associated with any color index by
  10769.  remapping to a color index a color value that describes the true color (the
  10770.  amount of red, green, and blue) you want to display.
  10771.  
  10772.  Olivetti adapters (OCGA, OEGA, and OVGA) support the standard CGA, EGA, and
  10773.  VGA modes (and palettes), plus an additional Olivetti mode described in
  10774.  Section 10.2.2, "Olivetti Palettes."
  10775.  
  10776.  ────────────────────────────────────────────────────────────────────────────
  10777.  NOTE
  10778.  
  10779.  The distinction between a color index and a color value is important. A
  10780.  color index is always a short integer. A color value is always a long
  10781.  integer. The only exception to this rule involves _setbkcolor, which uses a
  10782.  color index cast to a long integer in CGA and text modes.
  10783.  ────────────────────────────────────────────────────────────────────────────
  10784.  
  10785.  
  10786.  10.2.1  CGA Palettes
  10787.  
  10788.  The CGA (Color Graphics Adapter) supports two color video modes: _MRES4COLOR
  10789.  and _MRESNOCOLOR, which display four colors selected from one of several
  10790.  predefined palettes of colors. They display these foreground colors against
  10791.  a background color that can be any one of the 16 available colors. With the
  10792.  CGA hardware, the palette of foreground colors is predefined and cannot be
  10793.  changed. Each palette number is an integer. (See Table 10.4.)
  10794.  
  10795.  Table 10.4  CGA Palettes in _MRES4COLOR Mode
  10796.  
  10797. ╓┌───────────────┌─────────────┌───────────────┌─────────────────────────────╖
  10798.                                Color Index
  10799.  Palette         1             2               3
  10800.  Number
  10801.  ────────────────────────────────────────────────────────────────────────────
  10802.  0               Green         Red             Brown
  10803.  
  10804.  1               Cyan          Magenta         Light Gray
  10805.  
  10806.  2               Light Green   Light Red       Yellow
  10807.  
  10808.  3               Light Cyan    Light Magenta   White
  10809.  
  10810.  ────────────────────────────────────────────────────────────────────────────
  10811.  
  10812.  
  10813.  
  10814.   _MRESNOCOLOR produces palettes with shades of gray on monochrome monitors.
  10815.  
  10816.  
  10817.  The _MRESNOCOLOR video mode produces palettes containing various shades of
  10818.  gray on monochrome monitors. However, the _MRESNOCOLOR mode displays colors
  10819.  when used with a color display. Only two palettes are available in this
  10820.  mode. Table 10.5 shows the colors available in the two palettes.
  10821.  
  10822.  Table 10.5  CGA Palettes in _MRESNOCOLOR Mode
  10823.  
  10824. ╓┌───────────────┌────────────┌─────────────┌────────────────────────────────╖
  10825.                               Color Index
  10826.  Palette         1            2             3
  10827.  Number
  10828.  ────────────────────────────────────────────────────────────────────────────
  10829.  0               Blue         Red           Light Gray
  10830.  
  10831.  1               Light Blue   Light Red     White
  10832.  
  10833.  ────────────────────────────────────────────────────────────────────────────
  10834.  
  10835.  
  10836.  
  10837.  You can use the _selectpalette function only in the _MRES4COLOR,
  10838.  _MRESNOCOLOR, and _ORESCOLOR graphics modes. To change palettes in other
  10839.  video modes, use the _remappalette or _remapallpalette functions.
  10840.  
  10841.  
  10842.  10.2.2  Olivetti(R) Palettes
  10843.  
  10844.  Olivetti graphics adapters are found in most Olivetti computers (including
  10845.  the M24, M28, M240, M280, and M380) and in the AT&T 6300 series computers.
  10846.  These adapters function the same as their non-Olivetti equivalents; that is,
  10847.  the OCGA, OEGA, and OVGA adapters support CGA, EGA, and VGA modes,
  10848.  respectively. In addition, Olivetti adapters can enter the high resolution
  10849.  _ORESCOLOR mode.
  10850.  
  10851.  In _ORESCOLOR mode, you can choose one of 16 foreground colors by passing a
  10852.  value in the range 0 -15 to the _selectpalette function. The background
  10853.  color is always black.
  10854.  
  10855.  
  10856.  10.2.3  VGA Palettes
  10857.  
  10858.  Depending on the video mode currently in effect, a VGA (Video Graphics
  10859.  Array) screen has 2, 16, or 256 color indexes chosen from a pool of 262,144
  10860.  (256K) color values.
  10861.  
  10862.  To name a color value, specify a level of intensity ranging from 0 - 63 for
  10863.  each of the red, green, and blue components. The long integer that defines a
  10864.  color value contains four bytes (32 bits):
  10865.  
  10866.  (This figure may be found in the printed book.)
  10867.  
  10868.  The most-significant byte should contain zeros. The two high bits in the
  10869.  remaining three bytes should also be zero (these bits are ignored).
  10870.  
  10871.  To mix a light red (pink), turn red all the way up, and mix in some green
  10872.  and blue:
  10873.  
  10874.  (This figure may be found in the printed book.)
  10875.  
  10876.  The number 0x0020203FL represents this value in hexadecimal notation. You
  10877.  can also use the following macro:
  10878.  
  10879.    #define RGB ( r, g, b ) (0x3F3F3FL & ((long)(b) << 16 | (g) << 8 | (r)))
  10880.  
  10881.  To create pure yellow (100% red plus 100% green) and assign it to a variable
  10882.   yel, use this line:
  10883.  
  10884.    yel = RGB( 63, 63, 0 );
  10885.  
  10886.  For white, turn all the colors on:  RGB( 63, 63, 63). For black, set all
  10887.  colors to 0:  RGB( 0, 0, 0 ).
  10888.  
  10889.  Once you have the color value,
  10890.  
  10891.  
  10892.    ■   Call _remappalette, passing a color index and a color value.
  10893.  
  10894.    ■   Call _setcolor to make that color index the current color.
  10895.  
  10896.    ■   Draw something.
  10897.  
  10898.  
  10899.  The program YELLOW.C below shows how to remap a color. It draws a rectangle
  10900.  in color index 3 and then changes index 3 to the color value 0x00003F3FL
  10901.  (yellow).
  10902.  
  10903.    /* YELLOW.C -- Draws a yellow box on the screen */
  10904.    /* Requires VGA or EGA */
  10905.  
  10906.    #include <graph.h> /* graphics functions */
  10907.    #include <conio.h> /* getch */
  10908.  
  10909.    main()
  10910.    {
  10911.       short int index3 = 3;
  10912.       long int yellow = 0x00003F3FL;
  10913.       long int old3;
  10914.  
  10915.    if( _setvideomode( _HRES16COLOR ) )
  10916.       {
  10917.                /* set current color to index 3*/
  10918.          _setcolor( index3 );
  10919.                /* draw a rectangle in that color */
  10920.          _rectangle( _GBORDER, 10, 10, 110, 110 );
  10921.                /* wait for a keypress */
  10922.          getch();
  10923.                /* change index 3 to yellow */
  10924.          old3 = _remappalette( index3, yellow );
  10925.                /* wait for a keypress */
  10926.          getch();
  10927.                /* restore the old color */
  10928.          _remappalette( index3, old3 );
  10929.          getch();
  10930.                 /* back to default mode */
  10931.          _setvideomode( _DEFAULTMODE );
  10932.       } else _outtext( "This program requires EGA or VGA." );
  10933.    }
  10934.  
  10935.  
  10936.  10.2.4  MCGA Palettes
  10937.  
  10938.  In terms of color mixing, the MCGA (Multicolor Graphics Array) adapter is
  10939.  the same as the VGA. It can display any of 256K colors. It cannot enter all
  10940.  of the VGA video modes, however. It is limited to CGA modes and _VRES2COLOR
  10941.  and _MRES256COLOR.
  10942.  
  10943.  
  10944.  10.2.5  EGA Palettes
  10945.  
  10946.  Mixing colors in EGA (Enhanced Graphics Adapter) is similar to the VGA
  10947.  mixing described in Section 10.2.3, but there are fewer levels of intensity
  10948.  for the red, green, and blue (RGB) components. In the modes that offer 64
  10949.  colors, the RGB values include two bits and can range in value from 0 - 3.
  10950.  The long integer that defines a color value looks like this:
  10951.  
  10952.  (This figure may be found in the printed book.)
  10953.  
  10954.  The bits marked  0  should be zeros; the bits marked  ?  are ignored. EGA
  10955.  color values are defined this way to maintain compatibility with VGA color
  10956.  values.
  10957.  
  10958.  To form a pure red color value, use the constant 0x00000030L. For cyan (blue
  10959.  plus green), use 0x00303000L. The RGB macro defined above for VGA color
  10960.  mixing can be used as is, or you can modify it for EGA monitors:
  10961.  
  10962.    #define EGARGB( r, g, b ) (0x303030L & ((long)(b) << 20 | (g) << 12 | (r
  10963.    << 4)))
  10964.  
  10965.  In this macro, you would pass values in the range 0 -3 instead of 0 - 63.
  10966.  
  10967.  For an example program that remaps a color index to a color value, see
  10968.  YELLOW.C in Section 10.2.3, "VGA Palettes."
  10969.  
  10970.  
  10971.  10.2.6  Symbolic Constants
  10972.  
  10973.  The GRAPH.H file defines the following constants, which can be used as
  10974.  ready-made color values for EGA and VGA adapters:
  10975.  
  10976. ╓┌─────────────┌──────────────┌──────────────────────────────────────────────╖
  10977.  ────────────────────────────────────────────────────────────────────────────
  10978.  _BLACK        _GREEN         _LIGHTYELLOW
  10979.  _BLUE         _LIGHTBLUE     _MAGENTA
  10980.  _BRIGHTWHITE  _LIGHTCYAN     _RED
  10981.  _BROWN        _LIGHTGREEN    _WHITE
  10982.  _CYAN         _LIGHTMAGENTA
  10983.  _GRAY         _LIGHTRED
  10984.  
  10985.  
  10986.  For example, to change color index 1 to red, use the line
  10987.  
  10988.    _remappalette( 1, _RED );
  10989.  
  10990.  which causes any object currently drawn with color index 1 to change to red.
  10991.  The default color value associated with index 1 is blue.
  10992.  
  10993.  
  10994.  10.3  Specifying Points within Coordinate Systems
  10995.  
  10996.  A coordinate system describes points on the screen in terms of their
  10997.  horizontal (x) and vertical (y) positions. You specify a certain location by
  10998.  providing two values that map to a unique position.
  10999.  
  11000.   Graphics functions usually use viewport and window coordinates.
  11001.  
  11002.  Coordinates on the physical screen never change. Only five functions, listed
  11003.  in Section 10.3.1, use physical coordinates. All other graphics functions
  11004.  use one of these two coordinate systems:
  11005.  
  11006.  
  11007.    ■   Viewport coordinates (short integers)
  11008.  
  11009.    ■   Window coordinates (double-precision floating-point numbers)
  11010.  
  11011.  
  11012.  Viewports and windows can occupy all of the physical screen or just part of
  11013.  it. The three coordinate systems and conventions for naming points and
  11014.  regions of the screen are described below.
  11015.  
  11016.  
  11017.  10.3.1  Physical Coordinates
  11018.  
  11019.  Within the physical screen, the upper left corner is called the "origin."
  11020.  The x and y coordinates for the origin are always (0, 0). The x axis extends
  11021.  in the positive direction left to right, while the y axis extends in the
  11022.  positive direction top to bottom.
  11023.  
  11024.  For example, the video mode _VRES16COLOR has a resolution of 640 x 480,
  11025.  which means the x axis contains the values 0 - 639 (left to right), and the
  11026.  y axis contains 0 - 479 (top to bottom). (See Figure 10.1.)
  11027.  
  11028.  (This figure may be found in the printed book.)
  11029.  
  11030.  Only five functions use physical coordinates: _setcliprgn, _setvieworg,
  11031.  _setviewport, _getviewcoord, and _getphyscoord.
  11032.  
  11033.  The _setcliprgn function establishes a "clipping region." Attempts to draw
  11034.  inside the region succeed, while attempts to draw outside the region are
  11035.  clipped (ignored). When you first enter a graphics mode, the clipping region
  11036.  defaults to the entire screen.
  11037.  
  11038.  The _setvieworg function changes the current location of the origin. When a
  11039.  program first enters a graphics mode, the physical origin and the viewport
  11040.  origin are in the upper left corner. The following code moves the viewport
  11041.  origin to the physical screen location (50, 100):
  11042.  
  11043.    _setvieworg( 50, 100 );
  11044.  
  11045.  The effect on the screen is illustrated in Figure 10.2. Note that the number
  11046.  of pixels remains constant, but the range of legal x values changes from a
  11047.  range of 0 to 639 (physical screen) to -50 to 589. The legal y values change
  11048.  as well.
  11049.  
  11050.  (This figure may be found in the printed book.)
  11051.  
  11052.  All graphics functions are affected by the new origin, including _arc,
  11053.  _ellipse, _lineto, _moveto, _outgtext,  _pie, and _rectangle.
  11054.  
  11055.  The third function that uses physical coordinates is _setviewport, described
  11056.  below, which establishes the boundaries of the current viewport.
  11057.  
  11058.  
  11059.  10.3.2  Viewport Coordinates
  11060.  
  11061.  The default viewport coordinate system is identical to the physical screen
  11062.  coordinate system. The _setviewport function creates a new viewport within
  11063.  the boundaries of the physical screen. A standard viewport has two
  11064.  distinguishing features:
  11065.  
  11066.  
  11067.    ■   The origin of a viewport initially lies in the upper left corner of
  11068.        the viewport, not the upper left corner of the physical screen.
  11069.  
  11070.    ■   The clipping region matches the outer boundaries of the viewport.
  11071.  
  11072.  
  11073.   Graphics output functions require viewport or window coordinate values.
  11074.  
  11075.  In other words, the _setviewport function does the same thing as would two
  11076.  separate calls to _setvieworg and _setcliprgn. All graphics output functions
  11077.  require values that are either viewport coordinates or window coordinates.
  11078.  
  11079.  For example,
  11080.  
  11081.    _setviewport( 50, 50, 200, 100 );
  11082.  
  11083.  creates the viewport illustrated in Figure 10.3. The values passed to the
  11084.  _setviewport function are physical screen locations of opposite corners.
  11085.  After the viewport is created, the viewport origin lies in the upper left
  11086.  corner.
  11087.  
  11088.  (This figure may be found in the printed book.)
  11089.  
  11090.  
  11091.  10.3.3  Window Coordinates
  11092.  
  11093.  The _setwindow function allows you to use floating-point coordinates instead
  11094.  of integers. More importantly, it scales the screen coordinates to almost
  11095.  any size within the current viewport. Window functions take double-precision
  11096.  arguments and have names that end with the suffixes _w or _wxy. The function
  11097.  _lineto_w is the window-coordinate equivalent of the viewport function
  11098.  _lineto.
  11099.  
  11100.  To create a window for charting 12 months of average temperatures ranging
  11101.  from - 40 to 100, use this line:
  11102.  
  11103.    _setwindow( TRUE, 1.0, -40.0, 12.0, 100.0 );
  11104.  
  11105.  The first argument is the invert flag, which puts the lowest y value at the
  11106.  bottom of the screen instead of the top. The minimum and maximum coordinates
  11107.  follow. The new organization of the screen is shown in Figure 10.4.
  11108.  
  11109.  (This figure may be found in the printed book.)
  11110.  
  11111.  If you plot a point with _setpixel_w or draw a line with _lineto_w, the
  11112.  values are automatically scaled to the established window.
  11113.  
  11114.  Window-coordinate graphics provide a lot of flexibility. You can fit an axis
  11115.  into a small range (such as 151.25 to 151.45) or into a large range (-50,000
  11116.  to 80,000), depending on the type of data to be graphed. In addition, by
  11117.  changing the window coordinates and redrawing a figure, you can create the
  11118.  effects of zooming in or panning across a figure.
  11119.  
  11120.  
  11121.  10.3.4  Screen Locations
  11122.  
  11123.  A coordinate system needs two values (a horizontal and a vertical position)
  11124.  to describe the location of a point on the screen. There are times, however,
  11125.  when it is more convenient to use one variable instead of two.
  11126.  
  11127.  Some graphics functions require you to pass the location of a point on the
  11128.  screen. Others return a value that represents a location. The GRAPH.H file
  11129.  defines two structures that allow you to refer to a point with a single
  11130.  variable.
  11131.  
  11132.  
  11133.    ■   An xycoord structure contains two short integers called xcoord and
  11134.        ycoord for use in viewport graphics.
  11135.  
  11136.    ■   A _wxycoord structure contains two doubles called wx and wy for use in
  11137.        window-coordinate graphics.
  11138.  
  11139.  
  11140.  For example, you pass four doubles to the _rectangle_w function: an x and y
  11141.  position for the upper left corner of the window and an x and y position for
  11142.  the lower right corner. The _rectangle_wxy function takes two _wxycoord
  11143.  structures.
  11144.  
  11145.  
  11146.  10.3.5  Bounding Rectangles
  11147.  
  11148.  Certain figures such as arcs and ellipses are centered within a "bounding
  11149.  rectangle," specified by two points that define the opposite corners of the
  11150.  rectangle. The center of the rectangle becomes the center of the figure, and
  11151.  the rectangle's borders determine the size of the figure. Figure 10.5 shows
  11152.  start and end vectors and a bounding rectangle in which a pie shape has been
  11153.  drawn with the _pie function. The first two sets of coordinates are x1, y1,
  11154.  x2, and y2. They define the boundaries of the rectangle. The pie shape needs
  11155.  two other points, x3, y3, x4, and y4, which indicate the starting and ending
  11156.  lines.
  11157.  
  11158.  (This figure may be found in the printed book.)
  11159.  
  11160.  
  11161.  10.3.6  The Pixel Cursor
  11162.  
  11163.  A "pixel cursor" is a location on the screen. The _moveto function positions
  11164.  this cursor at a given spot. Nothing visible appears. If you call _lineto, a
  11165.  line is drawn from the current pixel cursor to another point. The _lineto
  11166.  function also changes the location of the pixel cursor. When you call
  11167.  _outgtext to display fonted text, the characters are drawn at the current
  11168.  pixel cursor location.
  11169.  
  11170.  To draw a series of connected lines, call _lineto several times.
  11171.  
  11172.  The _getcurrentposition function returns the cursor location in an xycoord
  11173.  structure.
  11174.  
  11175.  
  11176.  10.4  Graphics Functions
  11177.  
  11178.  This section lists the functions that work in one or more bit-mapped
  11179.  graphics modes. Most of these functions are present in several forms. The
  11180.  function names that end with _w use double values as arguments and the
  11181.  window coordinate system. Functions that end with _wxy use the window
  11182.  coordinate system and a _wxycoord structure to define the coordinates.
  11183.  Functions with no suffix use the viewport coordinate system.
  11184.  
  11185.  
  11186.  10.4.1  Controlling Video Modes
  11187.  
  11188.  The functions described below affect the current video mode, coordinate
  11189.  systems, clipping regions, viewports, and windows. For more information, use
  11190.  on-line help.
  11191.  
  11192.  _clearscreen - Erases the text or graphics screen and fills it with the
  11193.  current background color (note that setting the video mode automatically
  11194.  clears the screen). Pass one of the constants _GCLEARSCREEN, _GVIEWPORT, or
  11195.  _GWINDOW. No return value.
  11196.  
  11197.  _getphyscoord - Converts viewport coordinates to physical coordinates. Pass
  11198.  an x and y coordinate from the viewport. The function returns an xycoord
  11199.  structure, which includes an x and a y position from the physical screen.
  11200.  
  11201.  _getvideoconfig - Obtains the status of the current graphics environment.
  11202.  Pass it the address of a structure of type _videoconfig. See Section 10.1.3.
  11203.  "Reading the videoconfig Structure."
  11204.  
  11205.  _getviewcoord - Converts physical coordinates to viewport coordinates. Pass
  11206.  two integers: an x and y coordinate. The function returns an xycoord
  11207.  structure containing the equivalent position within the viewport.
  11208.  
  11209.  _getviewcoord_w - Converts window coordinates to viewport coordinates. Pass
  11210.  two doubles that name points within the window. Returns the equivalent
  11211.  viewport coordinates as an xycoord structure.
  11212.  
  11213.  _getviewcoord_wxy - Converts window coordinates to viewport coordinates in
  11214.  an xycoord structure. Pass a _wxycoord structure.
  11215.  
  11216.  _getwindowcoord - Converts viewport coordinates to window coordinates. Pass
  11217.  two integers representing viewport coordinates. Returns a _wxycoord
  11218.  structure.
  11219.  
  11220.  _setcliprgn - Limits graphic output to part of the screen, called the
  11221.  "clipping  region." Pass four values: the x and y coordinate of the upper
  11222.  left corner (on the physical screen) and the coordinates of the lower right
  11223.  corner. The default clipping region is the entire screen. See Section
  11224.  10.3.1, "Physical Coordinates."
  11225.  
  11226.  _setvideomode - Selects an operating mode for the display screen. Pass a
  11227.  constant, such as _HRES16COLOR. Returns 0 if the video mode selected is not
  11228.  supported by the hardware. See Section 10.1.2, "Setting a Video Mode."
  11229.  
  11230.  _setvideomoderows - Sets the video mode and the number of rows for text
  11231.  operations. Pass two values: a video mode and the desired number of text
  11232.  rows (25, 30, 43, 50, or 60). Pass the symbolic constant _MAXTEXTROWS to get
  11233.  the largest available number of rows. Returns the number of rows or 0 if
  11234.  unsuccessful.
  11235.  
  11236.  _setvieworg - Repositions the viewport origin. Pass an x and y position: the
  11237.  physical screen location that will become the new origin. Returns the
  11238.  previous origin in an xycoord structure.
  11239.  
  11240.  _setviewport - Creates a viewport, including a clipping region and a new
  11241.  origin in the upper left corner of the viewport. Subsequent calls to
  11242.  graphics routines will be limited to the viewport area. Pass four short
  11243.  integers that indicate the physical screen locations of the x and y
  11244.  coordinates in the upper left and  lower right corners of the viewport. No
  11245.  return value.
  11246.  
  11247.  _setwindow - Defines a window coordinate system. Pass five values: a short
  11248.  invert flag (TRUE or FALSE) and four doubles that represent the extreme
  11249.  values in the upper left and lower right portions of the current viewport.
  11250.  See Section 10.3.3, "Window Coordinates."
  11251.  
  11252.  
  11253.  10.4.2  Changing Colors
  11254.  
  11255.  The functions below control colors and color palettes. For an introduction
  11256.  to this topic, see Section 10.2, "Mixing Colors and Changing Palettes." For
  11257.  function prototypes and more information, consult on-line help.
  11258.  
  11259.  _getbkcolor - Reports the current background color as a long integer. In
  11260.  EGA, MCGA, and VGA video modes, this is a color value. In CGA and text
  11261.  modes, it is a color index.
  11262.  
  11263.  _getcolor - Returns the current color index.
  11264.  
  11265.  _remapallpalette - Assigns new color values to all color indexes. Pass a
  11266.  pointer to an array of color values. Returns 0 if unsuccessful.
  11267.  
  11268.  _remappalette - Assigns a color value to a specific color index. Pass a
  11269.  short color index and a long color value (which specifies the amount of red,
  11270.  green, and blue). Returns the previous color value for that index or -1 if
  11271.  unsuccessful. See Section 10.2.1, "CGA Palettes."
  11272.  
  11273.  _selectpalette - Selects a predefined palette. This function applies only to
  11274.  the CGA video modes _MRES4COLOR and _MRESNOCOLOR and the Olivetti graphics
  11275.  mode _ORESCOLOR. To change palettes in other color video modes, use
  11276.  _remappalette instead. Pass a short integer in the range 0 - 4 for CGA, or 0
  11277.  -15 for Olivetti mode. Returns the value of the previous palette.
  11278.  
  11279.  _setbkcolor - Sets the current background color. Always pass a long integer.
  11280.  In EGA, MCGA, and VGA modes, this value is a color value. In CGA and text
  11281.  modes, this is a color index cast to a long integer. Returns the old
  11282.  background color or -1 if unsuccessful.
  11283.  
  11284.  _setcolor - Sets the color index to be used for graphic output. It affects
  11285.  later calls to functions such as _arc, _ellipse, _floodfill, _lineto,
  11286.  _outgtext, _outtext, _pie, _rectangle, and _setpixel. Returns the previous
  11287.  color or -1 if unsuccessful.
  11288.  
  11289.  
  11290.  10.4.3  Drawing Points, Lines, and Shapes
  11291.  
  11292.  The functions described below draw points, lines, and shapes. For a
  11293.  definition of bounding rectangle and pixel cursor, see Sections 10.3.5 and
  11294.  10.3.6.
  11295.  
  11296.  _arc - Draws an elliptical arc. Pass eight short integers: four pairs of x
  11297.  and y coordinates. The first two pairs are the corners of the bounding
  11298.  rectangle. The third and fourth are the starting and ending points of the
  11299.  arc. Returns 0 if unsuccessful.
  11300.  
  11301.  _arc_wxy - Draws an arc within the window. Pass four wxycoord structures.
  11302.  The first two are the corners of the bounding rectangle. The third and
  11303.  fourth are the starting and ending points of the arc. Returns 0 if
  11304.  unsuccessful.
  11305.  
  11306.  _ellipse - Draws an ellipse or a circle. Pass a short fill flag ( _GBORDER
  11307.  or _GFILLINTERIOR) and four short integers representing the corners of the
  11308.  bounding rectangle. Returns 0 if unsuccessful.
  11309.  
  11310.  _ellipse_w - Draws an ellipse or a circle within a window. Pass a short fill
  11311.  flag ( _GBORDER or _GFILLINTERIOR) and four doubles representing the corners
  11312.  of the bounding rectangle. Returns 0 if unsuccessful.
  11313.  
  11314.  _ellipse_wxy - Draws an ellipse or a circle. Pass a short fill flag (
  11315.  _GBORDER or  _GFILLINTERIOR) and two _wxycoord structures representing the
  11316.  two corners of the bounding rectangle. Returns 0 if unsuccessful.
  11317.  
  11318.  _getcurrentposition - Returns the current pixel cursor position in viewport
  11319.  coordinates as an xycoord structure. The current position can be changed by
  11320.  _arc, _lineto, and _moveto. The default position is the center of the
  11321.  viewport.
  11322.  
  11323.  _getcurrentposition_w - Returns the current position of the pixel cursor as
  11324.  a _wxycoord structure containing the x and y coordinates. Pass nothing.
  11325.  
  11326.  _getpixel - Returns a pixel's color index. Pass a short x and y coordinate
  11327.  (in viewport coordinates). If the point is outside the clipping region, the
  11328.  function returns -1.
  11329.  
  11330.  _getpixel_w - Returns a pixel's color index. Pass two doubles: an x and y
  11331.  coordinate.
  11332.  
  11333.  _lineto - Draws a line from the current pixel cursor position to a specified
  11334.  point. Pass a short x and a short y position. Returns 0 if unsuccessful.
  11335.  
  11336.  _lineto_w - Draws a line from the current pixel position to a specified
  11337.  window coordinate point. Pass a double x and y position. Returns 0 if
  11338.  unsuccessful.
  11339.  
  11340.  _moveto - Moves the pixel cursor to a specified point (with no graphic
  11341.  output). Pass an x and y position. Returns the coordinates of the previous
  11342.  position in an xycoord structure.
  11343.  
  11344.  _moveto_w - Moves the pixel cursor to a specified point in a window. Pass
  11345.  two doubles: an x and a y coordinate. Returns the previous position as a
  11346.  _wxycoord structure.
  11347.  
  11348.  _ pie - Draws a figure shaped like a pie slice. Pass a short fill flag and
  11349.  eight short integers. The first four describe the bounding rectangle. The
  11350.  final four represent the starting vector and ending vector. Returns 0 if
  11351.  unsuccessful.
  11352.  
  11353.  _ pie_wxy - Draws a pie-slice figure within a window. Pass a short fill flag
  11354.  and four _wxycoord structures. The first two describe the bounding
  11355.  rectangle. The second two represent the starting vector and ending vector.
  11356.  Returns 0 if unsuccessful.
  11357.  
  11358.  _rectangle - Draws a rectangle in the current line style. Pass a short fill
  11359.  flag ( _GFILLINTERIOR or _GBORDER) and four short integers: the x and y
  11360.  coordinates of opposite corners. Returns 0 if unsuccessful.
  11361.  
  11362.  _rectangle_w - Draws a rectangle in the current line style. Pass a short
  11363.  fill flag ( _GFILLINTERIOR or _GBORDER) and four doubles: the x and y window
  11364.  coordinates of opposite corners. Returns 0 if unsuccessful.
  11365.  
  11366.  _rectangle_wxy - Draws a rectangle in the current line style. Pass a short
  11367.  fill flag ( _GFILLINTERIOR or _GBORDER) and two _wxycoord structures
  11368.  describing the x and y coordinates of opposite corners. Returns 0 if
  11369.  unsuccessful.
  11370.  
  11371.  _setpixel - Sets a pixel to the current color (which is selected by
  11372.  _setcolor). Pass it integer x and y coordinates. Returns the previous value
  11373.  of the pixel or -1 if unsuccessful.
  11374.  
  11375.  _setpixel_w - Sets a pixel to the current color (which is selected by
  11376.  _setcolor). Pass it double x and y coordinates describing a position within
  11377.  the window. Returns the previous value of the pixel or -1 if unsuccessful.
  11378.  
  11379.  
  11380.  10.4.4  Defining Patterns
  11381.  
  11382.  The following functions control the style in which straight lines are drawn
  11383.  and the fill pattern used for solid shapes. For more information, use
  11384.  on-line help.
  11385.  
  11386.  _floodfill - Fills a bounded shape with the fill pattern set by _setfillmask
  11387.  in the current color established by _setcolor. Pass an x and y coordinate
  11388.  and a boundary color (the color index that marks the edge of the shape to be
  11389.  filled). Returns 0 if unsuccessful.
  11390.  
  11391.  _floodfill_w - Fills a bounded shape with the fill pattern set by
  11392.  _setfillmask. Pass doubles that describe an x and y position within the
  11393.  window and a boundary color (the color index that marks the edge of the
  11394.  shape to be filled). Returns 0 if unsuccessful.
  11395.  
  11396.  _getfillmask - Returns the address of the current fill mask, an
  11397.  eight-character array, or 0 if the fill mask is not currently defined.
  11398.  
  11399.  _getlinestyle - Returns the line style, a short integer whose bits
  11400.  correspond to the screen pixels turned on or off within a line.
  11401.  
  11402.  _setfillmask - Sets the current fill mask used by _floodfill and functions
  11403.  that draw solid shapes (_ellipse, _pie, and _rectangle). Pass the address of
  11404.  an array of eight unsigned characters, where each bit represents a pixel.
  11405.  The pixels are drawn in the current color. No return value.
  11406.  
  11407.  _setlinestyle - Sets the current style, which is used to draw the straight
  11408.  lines within _lineto, _rectangle, and _pie. Pass an unsigned short integer
  11409.  within which the bits correspond to the pixels on screen. For example,
  11410.  0xFFFF represents a solid line, 0xAAAA is a dotted line, and 0xF0F0 is
  11411.  dashed.
  11412.  
  11413.  
  11414.  10.4.5  Manipulating Images
  11415.  
  11416.  The functions described below can be used to create animated graphics. The
  11417.  _getimage and _putimage functions act like a rubber stamp; after capturing a
  11418.  shape, you can make copies anywhere on the screen.
  11419.  
  11420.  _getimage - Stores a screen image in memory. Pass four integers (the
  11421.  coordinates of the bounding rectangle) and a pointer to a storage buffer.
  11422.  Call _imagesize to find out how much memory is required. No return value.
  11423.  
  11424.  _getimage_w - Stores a screen image in memory. Pass four doubles (the
  11425.  coordinates of the bounding rectangle) and a pointer to a storage buffer.
  11426.  Call _imagesize_w to find out how much memory is required. No return value.
  11427.  
  11428.  _getimage_wxy - Same as _getimage_w, but you pass two _wxycoord structures
  11429.  and a pointer to memory.
  11430.  
  11431.  _imagesize - Returns a long integer representing the size of an image in
  11432.  bytes. Call this function in preparation for a call to _getimage. Pass four
  11433.  integers: the x and y coordinates of opposite corners of the portion of the
  11434.  screen to be saved.
  11435.  
  11436.  _imagesize_w - Returns the size of an image in bytes in preparation for a
  11437.  call to _getimage_w and _putimage_w. Pass four doubles: the x and y window
  11438.  coordinates of opposite corners of the portion of the screen to be saved.
  11439.  
  11440.  _imagesize_wxy - Same as _imagesize_w, but you pass two _wxycoord
  11441.  structures.
  11442.  
  11443.  _putimage - Retrieves an image from memory and displays it on the active
  11444.  screen page. The image should previously have been saved to memory with
  11445.  _getimage. Pass two short integers (coordinates where the image is to be
  11446.  placed), a pointer to the image, and a short integer indicating what kind of
  11447.  action to take: _GAND, _GOR, _GPRESET, _GPSET, or _GXOR. No return value.
  11448.  
  11449.  _putimage_w - Displays an image from memory within a window. The image
  11450.  should previously have been saved to memory with _getimage_w. Pass two
  11451.  doubles (coordinates where the image is to be placed), a pointer to the
  11452.  image, and a short integer indicating what kind of action to take: _GAND,
  11453.  _GOR, _GPRESET, _GPSET, or _GXOR. No return value.
  11454.  
  11455.  
  11456.  10.5  Using Graphic Fonts
  11457.  
  11458.  A "font" is a collection of stylized text characters. Each font consists of
  11459.  a typeface with several type sizes.
  11460.  
  11461.  A "typeface" is the name of the displayed text─Courier, for example, or
  11462.  Roman. The list on the next page shows six of the typefaces available with
  11463.  the Microsoft C font library.
  11464.  
  11465.  "Type size" measures the screen area occupied by individual characters in
  11466.  units of screen pixels. For example, "Courier 12 x 9" denotes text of
  11467.  Courier typeface, with each character occupying a screen area of 12 vertical
  11468.  pixels by 9 horizontal pixels.
  11469.  
  11470.  A font's spacing can be fixed or proportional. "Fixed" means that all
  11471.  characters have the same width in pixels. "Proportional" means the width
  11472.  varies. An i, for example, is thinner than an M.
  11473.  
  11474.  The Microsoft C font functions use two methods to create fonts. The first
  11475.  technique generates Courier, Helv, and Tms Rmn fonts through a "bit-mapping"
  11476.  (or "raster-mapping") technique. Bit-mapping defines character images with
  11477.  binary data. Each bit in the map corresponds to a screen pixel. If a bit is
  11478.  1, its associated pixel is set to the current screen color.
  11479.  
  11480.  The second method creates the remaining three type styles─Modern, Script,
  11481.  and Roman─as "vector-mapped" fonts. Vector-mapping represents each character
  11482.  in terms of lines and arcs.
  11483.  
  11484.  Each method has advantages and disadvantages. Bit-mapped characters are more
  11485.  completely formed since the pixel mapping is predetermined. However, they
  11486.  cannot be scaled. Vector-mapped text can be scaled to any size, but the
  11487.  characters tend to lack the solid appearance of the bit-mapped characters.
  11488.  
  11489.  The following list shows six sample typefaces:
  11490.  
  11491.  (This figure may be found in the printed book.)
  11492.  
  11493.  Table 10.6 lists available sizes for each font. Note that the bit-mapped
  11494.  fonts come in preset sizes as measured in pixels. The vector-mapped fonts
  11495.  can be scaled to any size.
  11496.  
  11497.  Table 10.6  Typefaces and Type Sizes in the C Library
  11498.  
  11499. ╓┌─────────┌────────┌─────────────────┌──────────────────────────────────────╖
  11500.  Typeface  Mapping  Size (in pixels)  Spacing
  11501.  ────────────────────────────────────────────────────────────────────────────
  11502.  Courier   Bit      10 x 8, 12 x 9,   Fixed
  11503.                     15 x 12
  11504.  
  11505.  Helv      Bit      10 x 5, 12 x 7,   Proportional
  11506.                     15 x 8, 18 x 9,
  11507.                     22 x 12, 28 x 16
  11508.  
  11509.  Tms Rmn   Bit      10 x 5, 12 x 6,   Proportional
  11510.  Typeface  Mapping  Size (in pixels)  Spacing
  11511.  ────────────────────────────────────────────────────────────────────────────
  11512. Tms Rmn   Bit      10 x 5, 12 x 6,   Proportional
  11513.                     15 x 8, 16 x 9,
  11514.                     20 x 12, 26 x 16
  11515.  
  11516.  Modern    Vector   Scaled            Proportional
  11517.  
  11518.  Script    Vector   Scaled            Proportional
  11519.  
  11520.  Roman     Vector   Scaled            Proportional
  11521.  
  11522.  ────────────────────────────────────────────────────────────────────────────
  11523.  
  11524.  
  11525.  
  11526.  
  11527.  10.5.1  Using the C Font Library
  11528.  
  11529.  Data for both bit-mapped and vector-mapped fonts reside in .FON files. For
  11530.  example, the files MODERN.FON, ROMAN.FON, and SCRIPT.FON hold data for the
  11531.  three vector-mapped fonts.
  11532.  
  11533.   You can use Microsoft Windows .FON files.
  11534.  
  11535.  The Microsoft C .FON files are identical to the .FON files used in the
  11536.  Microsoft Windows operating environment. If you have access to Windows, you
  11537.  can use any of its .FON files with Microsoft C font functions. In addition,
  11538.  several vendors offer software that creates or modifies .FON files, allowing
  11539.  you to design your own fonts.
  11540.  
  11541.  Your programs should follow these three steps to display fonted text:
  11542.  
  11543.  
  11544.    1.  Register the fonts.
  11545.  
  11546.    2.  Set the current font from the register.
  11547.  
  11548.    3.  Display text using the current font.
  11549.  
  11550.  
  11551.  The following sections describe each of the three steps in detail. An
  11552.  example program in Section 10.5.5 demonstrates these steps.
  11553.  
  11554.  
  11555.  10.5.2  Registering the Fonts
  11556.  
  11557.  The fonts must first be organized into a list in memory, a process called
  11558.  "registering." Register fonts by calling the function _registerfonts. This
  11559.  function reads header information from specified .FON files, building a list
  11560.  of file information but not reading any mapping data from the files.
  11561.  
  11562.  The GRAPH.H file prototypes the _registerfonts function as
  11563.  
  11564.    short far _registerfonts( unsigned char far * );
  11565.  
  11566.  The argument points to a string containing a file name. The file name is the
  11567.  name of the .FON file for the desired font. The file name can include wild
  11568.  cards, allowing you to register several fonts with one call to
  11569.  _registerfonts.
  11570.  
  11571.  If it successfully reads one or more .FON files, _registerfonts returns the
  11572.  number of fonts. If the function fails, it returns a negative error code.
  11573.  
  11574.  
  11575.  10.5.3  Setting the Current Font
  11576.  
  11577.  Call the function _setfont to select a current font. This function checks to
  11578.  see if the requested font is registered, then reads the mapping data from
  11579.  the appropriate .FON file. A font must be registered and marked current
  11580.  before your program can display text in that font.
  11581.  
  11582.  The GRAPH.H file prototypes the_setfonts function as
  11583.  
  11584.    short far _setfont( unsigned char far * );
  11585.  
  11586.  The function's argument is a pointer to a character string. The string
  11587.  consists of letter codes that describe the desired font, as outlined here:
  11588.  
  11589.  Option Code                       Meaning
  11590.  ────────────────────────────────────────────────────────────────────────────
  11591.  b                                 The best fit from the registered fonts.
  11592.                                    This option instructs _setfont to accept
  11593.                                    the closest-fitting font if a font of
  11594.                                    the specified size is not registered.
  11595.  
  11596.                                    If at least one font is registered, the
  11597.                                    b option always sets a current font. If
  11598.                                    you do not specify the b option and an
  11599.                                    exact matching font is not registered,
  11600.                                    the
  11601.                                    _setfont function will fail. In this
  11602.                                    case, any existing current font remains
  11603.                                    current. Refer to on-line help for a
  11604.                                    description of error codes returned by
  11605.                                    _setfont.
  11606.  
  11607.                                    The _setfont function uses four criteria
  11608.                                    for selecting the best fit. In
  11609.                                    descending order of precedence, the four
  11610.                                    criteria are pixel height, typeface,
  11611.                                    pixel width, and spacing (fixed or
  11612.                                    proportional). If you request a
  11613.                                    vector-mapped font, _setfont sizes the
  11614.                                    font to correspond with the specified
  11615.                                    pixel height and width. If you request a
  11616.                                    raster-mapped (bit-mapped) font,
  11617.                                    _setfont chooses the closest available
  11618.                                    size. If the requested type size for a
  11619.                                    raster-mapped font fits exactly between
  11620.                                    two registered fonts, the smaller size
  11621.                                    takes precedence.
  11622.  
  11623.  f                                 Fixed-spaced font.
  11624.  
  11625.  hy                                Character height, where y is the height
  11626.                                    in pixels.
  11627.  
  11628.  nx                                Font number x, where x is less than or
  11629.                                    equal to the value returned by
  11630.                                    _registerfonts. For example, the option
  11631.                                    n3 makes the third registered font
  11632.                                    current, if three or more fonts are
  11633.                                    registered.
  11634.  
  11635.  p                                 Proportional-spaced font.
  11636.  
  11637.  r                                 Raster-mapped (bit-mapped) font.
  11638.  
  11639.  t`fontname'                       Typeface of the font in single quotes.
  11640.                                    The fontname string is one of the
  11641.                                    following:
  11642.  
  11643.    courier  modern  helv  script  tms rmn  roman
  11644.  
  11645.                                    Note the space in tms rmn. Additional
  11646.                                    font files use other names for fontname.
  11647.                                    Refer to the vendor's documentation for
  11648.                                    these names.
  11649.  
  11650.  v                                 Vector-mapped font.
  11651.  
  11652.  wx                                Character width, where x is the width in
  11653.                                    pixels.
  11654.  
  11655.  Option codes are not case sensitive and can be listed in any order. You can
  11656.  separate codes with spaces or any other character that is not a valid option
  11657.  code. The _setfont function ignores all invalid codes.
  11658.  
  11659.  The _setfont function updates a data area with parameters of the current
  11660.  font. The data area is in the form of a structure, defined in GRAPH.H as
  11661.  follows:
  11662.  
  11663.    struct _fontinfo
  11664.    {
  11665.       int     type;          /* set = vector,clear = bit map */
  11666.       int     ascent;        /* pix dist from top to base */
  11667.       int     pixwidth;      /* character width in pixels */
  11668.       int     pixheight;     /* character height in pixels */
  11669.       int     avgwidth;      /* average character width */
  11670.       char    filename[81];  /* file name including path */
  11671.       char    faceName[32];  /* font name */
  11672.    };
  11673.  
  11674.  If you want to retrieve the parameters of the current font, call the
  11675.  function _getfontinfo.
  11676.  
  11677.  
  11678.  10.5.4  Displaying Text
  11679.  
  11680.  The last step, displaying text, consists of two parts. First you must select
  11681.  a screen position for the text with the graphics function _moveto. Then
  11682.  display fonted text at that position with the function _outgtext. The
  11683.  _moveto function takes pixel coordinates as arguments. The coordinates
  11684.  locate the top left of the first character in the text string.
  11685.  
  11686.  
  11687.  10.5.5  A Sample Program
  11688.  
  11689.  The program SAMPLER.C displays sample text in all the available fonts, then
  11690.  exits when a key is pressed. Make sure the .FON files are in the current
  11691.  directory before running the program.
  11692.  
  11693.    /* SAMPLER.C: Displays sample text in various fonts. */
  11694.  
  11695.    #include <stdio.h>
  11696.    #include <conio.h>
  11697.    #include <stdlib.h>
  11698.    #include <graph.h>
  11699.    #include <string.h>
  11700.    #define NFONTS 6
  11701.  
  11702.    main()
  11703.  
  11704.    {
  11705.       static unsigned char *text[2*NFONTS] =
  11706.       {
  11707.           "COURIER",        "courier",
  11708.           "HELV",           "helv",
  11709.           "TMS RMN",        "tms rmn",
  11710.           "MODERN",         "modern",
  11711.           "SCRIPT",         "script",
  11712.           "ROMAN",          "roman"
  11713.       };
  11714.       static unsigned char *face[NFONTS] =
  11715.       {
  11716.           "t'courier'",
  11717.           "t'helv'",
  11718.           "t'tms rmn'",
  11719.           "t'modern'",
  11720.           "t'script'",
  11721.           "t'roman'"
  11722.       };
  11723.  
  11724.    static unsigned char list[20];
  11725.       struct videoconfig vc;
  11726.       int mode = _VRES16COLOR;
  11727.       register i;
  11728.  
  11729.    /*   Read header info from all .FON files in
  11730.        *   current directory
  11731.        */
  11732.  
  11733.       if( _registerfonts( "*.FON" ) < 0 )
  11734.       {
  11735.          _outtext( "Error: can't register fonts" );
  11736.          exit( 0 );
  11737.       }
  11738.  
  11739.       /*   Set highest available video mode */
  11740.  
  11741.       if( _setvideomode( _MAXRESMODE ) == 0 )
  11742.          exit ( 0 );
  11743.  
  11744.       /*   Copy video configuration into structure vc */
  11745.  
  11746.       _getvideoconfig( &vc );
  11747.  
  11748.       /*   Display six lines of sample text */
  11749.  
  11750.       for( i = 0; i < NFONTS; i++ )
  11751.       {
  11752.          strcpy( list, face[i] );
  11753.          strcat( list, "h30w24b" );
  11754.  
  11755.          if( _setfont( list ) >= 0 )
  11756.          {
  11757.              _setcolor( i + 1 );
  11758.              _moveto( 0, (i * vc.numypixels) / NFONTS );
  11759.              _outgtext( text[i * 2] );
  11760.              _moveto( vc.numxpixels / 2,
  11761.                          (i * vc.numypixels) / NFONTS );
  11762.              _outgtext( text[(i * 2) + 1] );
  11763.          }
  11764.          else
  11765.          {
  11766.              _setvideomode( _DEFAULTMODE );
  11767.              _outtext( "Error: can't set font" );
  11768.              exit( 0 );
  11769.          }
  11770.       }
  11771.  
  11772.    getch();
  11773.       _setvideomode( _DEFAULTMODE );
  11774.  
  11775.       /* Return memory when finished with fonts */
  11776.  
  11777.       _unregisterfonts();
  11778.       exit( 0 );
  11779.    }
  11780.  
  11781.  
  11782.  10.5.6  Using Fonts Effectively
  11783.  
  11784.  Displaying fonts is simply another form of graphics; using fonts effectively
  11785.  requires little programming effort. Still, there are a few things to watch:
  11786.  
  11787.  
  11788.  
  11789.    ■   Remember that the video mode should be set only once. If you generate
  11790.        an image with presentation graphics and want to add text to it, do not
  11791.        reset the video mode prior to calling the font routines. Doing so will
  11792.        blank the screen, destroying the original image.
  11793.  
  11794.    ■   The _setfont function reads specified .FON files to obtain mapping
  11795.        data for the current font. Each call to _setfont causes a disk access
  11796.        and overwrites the old font data in memory. If you want to show text
  11797.        of different styles on the same screen, display all text of one font
  11798.        before moving on to the others. Minimizing the number of calls to
  11799.        _setfont saves time spent in disk I/O and memory reloads.
  11800.  
  11801.    ■   When your program finishes using the fonts library, you may want to
  11802.        free the memory occupied by the register list by calling
  11803.        _unregisterfonts. This function frees the memory allocated by
  11804.        _registerfonts. The register information for each type size of each
  11805.        font takes up approximately 140 bytes of memory.
  11806.  
  11807.    ■   Aesthetic suggestions for the printed page also apply to screen text.
  11808.        Typefaces are more effective when they do not compete with each other
  11809.        for attention. Restricting the number of styles per screen to one or
  11810.        two generally results in a more pleasing, less cluttered image.
  11811.  
  11812.  
  11813.  
  11814.  
  11815.  
  11816.  
  11817.  
  11818.  
  11819.  
  11820.  Chapter 11  Creating Charts and Graphs
  11821.  ────────────────────────────────────────────────────────────────────────────
  11822.  
  11823.  The low-level graphics functions described in Chapter 10, "Communicating
  11824.  with Graphics," draw points, lines, and shapes. Although it is possible to
  11825.  use them to generate charts and graphs, an additional set of high-level
  11826.  graphics functions is better suited to this task.
  11827.  
  11828.  "Presentation graphics" is a set of high-level functions that displays
  11829.  presentation-quality graphics. These functions transform numeric data into
  11830.  pie charts, bar and column charts, line graphs, and scatter diagrams.
  11831.  
  11832.  This chapter describes how to use presentation graphics.
  11833.  
  11834.  
  11835.  11.1  Overview of Presentation Graphics
  11836.  
  11837.  The presentation graphics library PGCHART.LIB contains 22 functions. They
  11838.  are listed in Table 11.1 for convenient reference.
  11839.  
  11840.  Table 11.1  Presentation Graphics Function
  11841.  
  11842. ╓┌───────────────────┌─────────────────────┌─────────────────────────────────╖
  11843.  Primary Functions   Secondary Functions
  11844.  ────────────────────────────────────────────────────────────────────────────
  11845.  _pg_chart           _pg_analyzechart      _pg_hlabelchart
  11846.  Primary Functions   Secondary Functions
  11847.  ────────────────────────────────────────────────────────────────────────────
  11848.  _pg_chart           _pg_analyzechart      _pg_hlabelchart
  11849.  _pg_chartms         _pg_analyzechartms    _pg_resetpalette
  11850.  _pg_chartpie        _pg_analyzepie        _pg_resetstyleset
  11851.  _pg_chartscatter    _pg_analyzescatter    _pg_setchardef
  11852.  _pg_chartscatterms  _pg_analyzescatterms  _pg_setpalette
  11853.  _pg_defaultchart    _pg_getchardef        _pg_setstyleset
  11854.  _pg_initchart       _pg_getpalette        _pg_vlabelchart
  11855.                      _pg_getstyleset
  11856.  ────────────────────────────────────────────────────────────────────────────
  11857.  
  11858.  
  11859.   The seven primary functions initialize variables and display selected chart
  11860.  types.
  11861.  
  11862.  In most cases, you will be using only seven "primary functions." These
  11863.  functions initialize variables and display selected chart types. The 15
  11864.  "secondary functions" of presentation graphics do not directly display
  11865.  charts. Most of them retrieve or set data in the presentation graphics chart
  11866.  environment.
  11867.  
  11868.  Among the secondary functions are the "analysis functions," identified by
  11869.  the prefix _pg_analyze. These five functions calculate default values that
  11870.  pertain to a given chart type and data set. Calling an analysis function has
  11871.  the same effect as calling a corresponding primary function, except that the
  11872.  chart is not displayed. This allows you to pass on to the library the burden
  11873.  of calculating values. You can then make modifications to the resulting
  11874.  values and call a primary routine to display the chart.
  11875.  
  11876.  Use the _pg_hlabelchart and _pg_vlabelchart functions to display text that
  11877.  is not part of a title or axis label on your chart. These functions enable
  11878.  you to attach notes or other messages to your chart.
  11879.  
  11880.  
  11881.  11.2  Parts of a Graph
  11882.  
  11883.  This section describes the terms used to refer to the different kinds of
  11884.  information that can be plotted. The various types of charts and graphs are
  11885.  also defined.
  11886.  
  11887.  
  11888.  Data Series
  11889.  
  11890.  Data that are related by a common idea or purpose constitute a "series." For
  11891.  example, the prices of a futures commodity over the course of a year form a
  11892.  single series of data. The volume forms a second data series.
  11893.  
  11894.  When you include several series in one chart, characteristics such as color
  11895.  and pattern can help distinguish one from another. You can more readily
  11896.  differentiate series on a color monitor than you can on a monochrome
  11897.  monitor. The number of series that can appear on the same chart depends on
  11898.  the chart type and the number of available colors.
  11899.  
  11900.  
  11901.  Categories
  11902.  
  11903.  "Categories" are nonnumeric data. A set of categories forms a frame of
  11904.  reference for the comparison of numeric data. For example, the months of the
  11905.  year are categories against which numeric data such as inches of rainfall
  11906.  can be plotted.
  11907.  
  11908.  Regional sales provide another example. A chart can compare a company's
  11909.  sales in different parts of the country. Each region forms a category.
  11910.  
  11911.  
  11912.  Values
  11913.  
  11914.  "Values" are numeric data. Sales, stock prices, air temperatures, and
  11915.  populations are all series of values that can be plotted against categories
  11916.  or against other values.
  11917.  
  11918.  Presentation graphics allows you to overlay different series of value data
  11919.  on a single graph. For example, average monthly temperatures or monthly
  11920.  sales of heating oil during different years─or a combination of temperatures
  11921.  and sales─can be plotted together on the same graph.
  11922.  
  11923.  
  11924.  Pie Charts
  11925.  
  11926.  "Pie charts" are used to represent data by showing the relationship of each
  11927.  part to the whole. A good example is a company's annual budget. A pie chart
  11928.  allows you to view each area of revenue or spending by its relative size
  11929.  within the context of the entire company budget.
  11930.  
  11931.  Presentation graphics can display either a standard or an "exploded" pie
  11932.  chart. The exploded view shows the pie with one or more pieces separated for
  11933.  emphasis. You can label each slice of a pie chart with a percentage figure
  11934.  if you wish.
  11935.  
  11936.  
  11937.  Bar and Column Charts
  11938.  
  11939.  As the name implies, a "bar chart" shows data as horizontal bars. Bar charts
  11940.  show comparisons among items rather than absolute value.
  11941.  
  11942.  "Column charts" are vertical bar charts. Column charts are frequently used
  11943.  to show variations over a period of time, since they suggest time flow
  11944.  better than a bar chart.
  11945.  
  11946.  
  11947.  Line Graphs
  11948.  
  11949.  "Line graphs" illustrate trends or changes in data. They show how a series
  11950.  of values varies against a particular category─for example, average
  11951.  temperatures throughout one year.
  11952.  
  11953.  Traditionally, line graphs show a collection of data points connected by
  11954.  lines. Presentation graphics can also plot points that are not connected by
  11955.  lines.
  11956.  
  11957.  
  11958.  Scatter Diagrams
  11959.  
  11960.  A "scatter diagram" is the only type of graph available in presentation
  11961.  graphics that directly compares values with values. A scatter diagram simply
  11962.  plots points.
  11963.  
  11964.  Scatter diagrams illustrate the relationship between numeric values in
  11965.  different groups of data. They graphically show trends and correlations not
  11966.  easily detected from rows and columns of raw numbers.
  11967.  
  11968.  Scatter diagrams are most useful with large amounts of data. Consider, for
  11969.  example, the relationship between personal income and family size. If you
  11970.  poll one thousand wage earners for their income and family size, you have a
  11971.  scatter diagram with one thousand points. If you combine your results so
  11972.  that you are left with one average income for each family size, you have a
  11973.  line graph.
  11974.  
  11975.  
  11976.  Axes
  11977.  
  11978.  All presentation graphics charts except pie charts are displayed with two
  11979.  perpendicular reference axes. The vertical, or y, axis runs from top to
  11980.  bottom of the chart and is placed against the left side of the screen. The
  11981.  horizontal, or x, axis runs from left to right across the bottom of the
  11982.  screen.
  11983.  
  11984.   The chart type determines the axis used for category data and the axis for
  11985.  value data.
  11986.  
  11987.  The x axis is the category axis for column and line charts and the value
  11988.  axis for bar charts. The y axis is the value axis for column and line charts
  11989.  and the category axis for bar charts.
  11990.  
  11991.  
  11992.  Chart Windows
  11993.  
  11994.  The "chart window" defines that part of the screen on which the chart is
  11995.  drawn. By default, the window fills the entire screen, but presentation
  11996.  graphics allows you to resize the window for smaller graphs. By redefining
  11997.  the chart window to different screen locations, you can view separate graphs
  11998.  together on the same screen.
  11999.  
  12000.  
  12001.  Data Windows
  12002.  
  12003.  While the chart window defines the entire graph including axes and labels,
  12004.  the "data window" defines only the actual plotting area. This is the portion
  12005.  of the graph to the right of the y axis and above the x axis. You cannot
  12006.  specify or adjust the size of the data window. Presentation graphics
  12007.  automatically determines its size based on the dimensions of the chart
  12008.  window.
  12009.  
  12010.  
  12011.  Chart Styles
  12012.  
  12013.  Each of the five types of presentation graphics charts can appear in two
  12014.  different "chart styles," as described in Table 11.2.
  12015.  
  12016.  Table 11.2  Presentation Graphics Chart Styles
  12017.  
  12018. ╓┌───────────┌───────────────────┌───────────────────────────────────────────╖
  12019.  Chart Type  Chart Style #1      Chart Style #2
  12020.  ────────────────────────────────────────────────────────────────────────────
  12021.  Pie         With percentages    Without percentages
  12022.  Bar         Side-by-side        Stacked
  12023.  Column      Side-by-side        Stacked
  12024.  Line        Points with lines   Points only
  12025.  Scatter     Points with lines   Points only
  12026.  ────────────────────────────────────────────────────────────────────────────
  12027.  
  12028.  
  12029.  Bar and column charts have only one style when displaying a single series of
  12030.  data. The styles "side-by-side" and "stacked" are applicable when more than
  12031.  one series appears on the same chart. The first style arranges the bars or
  12032.  columns for the different series side by side, showing relative heights or
  12033.  lengths. The stacked style, illustrated for a column chart in Figure 11.3,
  12034.  emphasizes relative sizes between bars or columns.
  12035.  
  12036.  
  12037.  Legends
  12038.  
  12039.   Legends help identify  individual data series.
  12040.  
  12041.  When displaying more than one data series on a chart, presentation graphics
  12042.  uses different colors, line styles, or patterns to differentiate them.
  12043.  Presentation graphics also can display a "legend" that labels the different
  12044.  series of a chart. For a pie chart, the legend labels individual slices of
  12045.  the pie.
  12046.  
  12047.  A sample of the color and pattern used to graph the series appears next to
  12048.  the series label. This identifies the set of data to which the labels
  12049.  belong.
  12050.  
  12051.  You may change the font displayed by calling the _registerfonts and _setfont
  12052.  functions (see Section 10.5 for more information about using fonts). If you
  12053.  don't select a font, presentation graphics defaults to an internal font.
  12054.  
  12055.  
  12056.  11.3  Writing a Presentation Graphics Program
  12057.  
  12058.  To write a C program that uses presentation graphics, follow these steps:
  12059.  
  12060.  
  12061.    1.  Include the required header files, GRAPH.H and PGCHART.H, as well as
  12062.        any other header files your program may need.
  12063.  
  12064.    2.  Set the video mode to a graphics mode. See Chapter 10, "Communicating
  12065.        with Graphics," for a description of video modes.
  12066.  
  12067.    3.  Initialize the presentation graphics chart environment. Presentation
  12068.        graphics places charting parameters in data structures. The amount of
  12069.        initialization that must be done by your program depends on how
  12070.        extensively it relies on the defaults.
  12071.  
  12072.    4.  Assemble the plot data. Data can be collected in a variety of ways: by
  12073.        calculating it elsewhere in the program, reading it from files, or
  12074.        entering it from the keyboard. All plot data must be assembled in
  12075.        arrays because the presentation graphics functions locate them through
  12076.        pointers.
  12077.  
  12078.    5.  Call presentation graphics functions to display the chart. Pause while
  12079.        the chart is on the screen.
  12080.  
  12081.    6.  Reset the video mode. When your program detects the signal to
  12082.        continue, it should reset the video to its original (default) mode.
  12083.  
  12084.  
  12085.  After compiling the program, link it to the library modules PGCHART.LIB and
  12086.  GRAPHICS.LIB.
  12087.  
  12088.  The sample programs in Sections 11.3.1-11.3.3 use 5 of the 22 presentation
  12089.  graphics functions: _pg_initchart, _pg_defaultchart, _pg_chartpie,
  12090.  _pg_chart, and _pg_chartscatter. Each program is commented so that you can
  12091.  recognize the steps given in this section.
  12092.  
  12093.  
  12094.  11.3.1  Pie Chart
  12095.  
  12096.  The following program uses presentation graphics to display a pie chart for
  12097.  monthly sales of orange juice over a year. The chart, which is shown in
  12098.  Figure 11.1, remains on the screen until a key is pressed.
  12099.  
  12100.    /* PIE.C:  Create sample pie chart.  */
  12101.  
  12102.    #include <conio.h>
  12103.    #include <string.h>
  12104.    #include <graph.h>
  12105.    #include <pgchart.h>
  12106.  
  12107.    #define MONTHS 12
  12108.  
  12109.    typedef enum {FALSE, TRUE} boolean;
  12110.  
  12111.    float far value[MONTHS] =
  12112.    {
  12113.        33.0, 27.0, 42.0, 64.0,106.0,157.0,
  12114.       182.0,217.0,128.0, 62.0, 43.0, 36.0
  12115.    };
  12116.    char far *category[MONTHS] =
  12117.    {
  12118.       "Jan", "Feb", "Mar", "Apr",
  12119.       "May", "Jun", "Jly", "Aug",
  12120.       "Sep", "Oct", "Nov", "Dec"
  12121.    };
  12122.    short far explode[MONTHS] = {0};
  12123.  
  12124.    main()
  12125.    {
  12126.       chartenv env;
  12127.       int mode = _VRES16COLOR;
  12128.  
  12129.    /* Set highest video mode available */
  12130.  
  12131.       if( _setvideomode( _MAXRESMODE ) == 0 )
  12132.          exit( 0 );
  12133.  
  12134.       /* Initialize chart library and a default pie chart */
  12135.  
  12136.       _pg_initchart();
  12137.       _pg_defaultchart( &env, _PG_PIECHART, _PG_PERCENT );
  12138.  
  12139.       /* Add titles and some chart options */
  12140.  
  12141.       strcpy( env.maintitle.title, "Good Neighbor Grocery" );
  12142.       env.maintitle.titlecolor = 6;
  12143.       env.maintitle.justify = _PG_RIGHT;
  12144.       strcpy( env.subtitle.title, "Orange Juice Sales" );
  12145.       env.subtitle.titlecolor = 6;
  12146.       env.subtitle.justify = _PG_RIGHT;
  12147.       env.chartwindow.border = FALSE;
  12148.  
  12149.       /* Parameters for call to _pg_chartpie are:
  12150.        *
  12151.        *    env        - Environment variable
  12152.        *    category   - Category labels
  12153.        *    value      - Data to chart
  12154.        *    explode    - Separated pieces
  12155.        *    MONTHS     - Number of data values
  12156.        */
  12157.       if( _pg_chartpie( &env, category, value,
  12158.                         explode, MONTHS ) )
  12159.       {
  12160.          _setvideomode( _DEFAULTMODE );
  12161.          _outtext( "Error:  can't draw chart" );
  12162.       }
  12163.       else
  12164.       {
  12165.          getch();
  12166.          _setvideomode( _DEFAULTMODE );
  12167.       }
  12168.       return( 0 );
  12169.    }
  12170.  
  12171.  (This figure may be found in the printed book.)
  12172.  
  12173.  
  12174.  11.3.2  Bar, Column, and Line Charts
  12175.  
  12176.  The code for the PIE.C program needs only minor alterations to produce bar,
  12177.  column, and line charts for the same data:
  12178.  
  12179.  
  12180.    ■   Replace the call to _pg_chartpie with _pg_chart. This function
  12181.        produces bar, column, and line charts depending on the value of the
  12182.        second argument for _pg_defaultchart.
  12183.  
  12184.    ■   Give new arguments to _pg_defaultchart that specify chart type and
  12185.        style.
  12186.  
  12187.    ■   Assign titles for the x axis and y axis in the structure  env.
  12188.  
  12189.    ■   Remove references to array  explode, which is applicable only to pie
  12190.        charts.
  12191.  
  12192.  
  12193.  The following example produces a bar chart for the store owner's data. The
  12194.  result is shown in Figure 11.2.
  12195.  
  12196.    /* BAR.C:  Create sample bar chart. */
  12197.  
  12198.    #include <conio.h>
  12199.    #include <string.h>
  12200.    #include <graph.h>
  12201.    #include <pgchart.h>
  12202.  
  12203.    #define MONTHS 12
  12204.  
  12205.    typedef enum {FALSE, TRUE} boolean;
  12206.  
  12207.    float far value[MONTHS] =
  12208.    {
  12209.        33.0, 27.0, 42.0, 64.0,106.0,157.0,
  12210.       182.0,217.0,128.0, 62.0, 43.0, 36.0
  12211.    };
  12212.    char far *category[MONTHS] =
  12213.    {
  12214.       "Jan", "Feb", "Mar", "Apr",
  12215.       "May", "Jun", "Jly", "Aug",
  12216.       "Sep", "Oct", "Nov", "Dec"
  12217.    };
  12218.  
  12219.    main()
  12220.    {
  12221.       chartenv env;
  12222.       int mode = _VRES16COLOR;
  12223.  
  12224.       /* Set highest video mode available */
  12225.  
  12226.       if( _setvideomode( _MAXRESMODE ) == 0 )
  12227.          exit( 0 );
  12228.  
  12229.       /* Initialize chart library and a default bar chart */
  12230.       _pg_initchart();
  12231.       _pg_defaultchart( &env, _PG_BARCHART, _PG_PLAINBARS );
  12232.  
  12233.       /* Add titles and some chart options */
  12234.  
  12235.       strcpy( env.maintitle.title, "Good Neighbor Grocery" );
  12236.       env.maintitle.titlecolor = 6;
  12237.       env.maintitle.justify = _PG_RIGHT;
  12238.       strcpy( env.subtitle.title, "Orange Juice Sales" );
  12239.       env.subtitle.titlecolor = 6;
  12240.       env.subtitle.justify = _PG_RIGHT;
  12241.       strcpy( env.yaxis.axistitle.title, "Months" );
  12242.       strcpy( env.xaxis.axistitle.title, "Quantity (cases)" );
  12243.       env.chartwindow.border = FALSE;
  12244.  
  12245.    /* Parameters for call to _pg_chart are:
  12246.        *    env        - Environment variable
  12247.        *    category   - Category labels
  12248.        *    value      - Data to chart
  12249.        *    MONTHS     - Number of data values
  12250.        */
  12251.       if( _pg_chart( &env, category, value, MONTHS ) )
  12252.       {
  12253.          _setvideomode( _DEFAULTMODE );
  12254.          _outtext( "Error:  can't draw chart" );
  12255.       }
  12256.       else
  12257.       {
  12258.          getch();
  12259.          _setvideomode( _DEFAULTMODE );
  12260.       }
  12261.       return( 0 );
  12262.    }
  12263.  
  12264.  (This figure may be found in the printed book.)
  12265.  
  12266.  The grocer's bar chart becomes a column chart in two easy steps. Simply
  12267.  specify the new chart type when calling _pg_defaultchart and change the axis
  12268.  titles. To produce a column chart for the grocer's data, replace the call to
  12269.  _pg_defaultchart with
  12270.  
  12271.    _pg_defaultchart( &env, _PG_COLUMNCHART, _PG_PLAINBARS );
  12272.  
  12273.  Replace the last two calls to strcpy with
  12274.  
  12275.    strcpy( env.xaxis.axistitle.title, "Months" );
  12276.    strcpy( env.yaxis.axistitle.title, "Quantity (cases)" );
  12277.  
  12278.  Note that now the x axis is labeled "Months" and the y axis is labeled
  12279.  "Quantity (cases)." Figure 11.3 shows the resulting column chart.
  12280.  
  12281.  (This figure may be found in the printed book.)
  12282.  
  12283.  Creating an equivalent line chart requires only one change. Use the same
  12284.  code as for the column chart and replace the call to _pg_defaultchart with
  12285.  
  12286.    _pg_defaultchart( &env, _PG_LINECHART, _PG_POINTANDLINE );
  12287.  
  12288.  Figure 11.4 shows the line chart for the grocer's data.
  12289.  
  12290.  (Please refer to the printed book.)
  12291.  
  12292.  (This figure may be found in the printed book.)
  12293.  
  12294.  
  12295.  11.3.3  Scatter Diagram
  12296.  
  12297.  The program SCATTER.C displays a scatter diagram that illustrates the
  12298.  relationship between the sales of orange juice and hot chocolate throughout
  12299.  a 12-month period. Figure 11.5 shows the results of SCATTER.C. Notice that
  12300.  the scatter points form a slightly curved line, indicating that a
  12301.  correlation exists between the sales of the two products. The demand for
  12302.  orange juice is roughly inverse to the demand for hot chocolate.
  12303.  
  12304.    /* SCATTER.C:  Create sample scatter diagram. */
  12305.  
  12306.    #include <conio.h>
  12307.    #include <string.h>
  12308.    #include <graph.h>
  12309.    #include <pgchart.h>
  12310.  
  12311.    #define MONTHS 12
  12312.  
  12313.    typedef enum {FALSE, TRUE} boolean;
  12314.  
  12315.    /* Orange juice sales */
  12316.  
  12317.    float far xvalue[MONTHS] =
  12318.    {
  12319.        33.0, 27.0, 42.0, 64.0,106.0,157.0,
  12320.       182.0,217.0,128.0, 62.0, 43.0, 36.0
  12321.    };
  12322.  
  12323.    /* Hot chocolate sales */
  12324.  
  12325.    float far yvalue[MONTHS] =
  12326.    {
  12327.       37.0, 37.0, 30.0, 19.0, 10.0,  5.0,
  12328.        2.0,  1.0,  7.0, 15.0, 28.0, 39.0
  12329.    };
  12330.  
  12331.    main()
  12332.    {
  12333.       chartenv env;
  12334.       int mode = _VRES16COLOR;
  12335.  
  12336.       /* Set highest video mode available */
  12337.  
  12338.       if( _setvideomode( _MAXRESMODE ) == 0 )
  12339.          exit( 0 );
  12340.       /* Initialize chart library and default
  12341.        * scatter diagram
  12342.        */
  12343.       _pg_initchart();
  12344.       _pg_defaultchart( &env, _PG_SCATTERCHART,
  12345.                         _PG_POINTONLY );
  12346.  
  12347.       /* Add titles and some chart options */
  12348.  
  12349.       strcpy( env.maintitle.title, "Good Neighbor Grocery" );
  12350.       env.maintitle.titlecolor = 6;
  12351.       env.maintitle.justify = _PG_RIGHT;
  12352.       strcpy( env.subtitle.title,
  12353.               "Orange Juice vs Hot Chocolate" );
  12354.       env.subtitle.titlecolor = 6;
  12355.       env.subtitle.justify = _PG_RIGHT;
  12356.       env.yaxis.grid = TRUE;
  12357.       strcpy( env.xaxis.axistitle.title,
  12358.               "Orange Juice Sales" );
  12359.       strcpy( env.yaxis.axistitle.title,
  12360.               "Hot Chocolate Sales" );
  12361.       env.chartwindow.border = FALSE;
  12362.  
  12363.    /* Parameters for call to _pg_chartscatter are:
  12364.        *    env        - Environment variable
  12365.        *    xvalue     - X-axis data
  12366.        *    yvalue     - Y-axis data
  12367.        *    MONTHS     - Number of data values
  12368.        */
  12369.       if( _pg_chartscatter( &env, xvalue,
  12370.                             yvalue, MONTHS ) )
  12371.       {
  12372.          _setvideomode( _DEFAULTMODE );
  12373.          _outtext( "Error:  can't draw chart" );
  12374.       }
  12375.       else
  12376.       {
  12377.          getch();
  12378.          _setvideomode( _DEFAULTMODE );
  12379.       }
  12380.       return( 0 );
  12381.    }
  12382.  
  12383.  (This figure may be found in the printed book.)
  12384.  
  12385.  
  12386.  11.4  Manipulating Colors and Patterns
  12387.  
  12388.  Presentation graphics displays each data series in a way that makes it
  12389.  discernible from other series. It does this by defining a separate "palette"
  12390.  for every data series in a chart. Palettes consist of entries that determine
  12391.  color, line style, fill pattern, and point character used to graph the
  12392.  series.
  12393.  
  12394.  Presentation graphics maintains its palettes as an array of structures. The
  12395.  header file PGCHART.H defines the palette structures as shown below:
  12396.  
  12397.    /* Typedef for pattern bitmap */
  12398.    typedef unsigned char fillmap[8];
  12399.  
  12400.    /* Typedef for palette entry definition */
  12401.    typedef struct
  12402.    {
  12403.       unsigned short color;
  12404.       unsigned short style;
  12405.       fillmap        fill;
  12406.       char           plotchar;
  12407.    } paletteentry;
  12408.  
  12409.    /* Typedef for palette definition */
  12410.    typedef paletteentry palettetype[_PG_PALETTELEN];
  12411.  
  12412.  Do not confuse the presentation graphics palettes with the adapter display
  12413.  palettes, which are register values kept by the video controller. The
  12414.  function _selectpalette described in Chapter 10, "Communicating with
  12415.  Graphics," sets the display palette. It does not define the data series
  12416.  palettes used by presentation graphics.
  12417.  
  12418.  
  12419.  11.4.1  Color Pool
  12420.  
  12421.   The color pool determines the  colors of graphic elements (axes, labels,
  12422.  legends, titles).
  12423.  
  12424.  Presentation graphics organizes all chart colors into a "color pool." The
  12425.  color pool holds the color index values valid for the current graphics mode.
  12426.  (Refer to Chapter 10, "Communicating with Graphics," for more information
  12427.  about the color index.) Palette structures contain color codes that refer to
  12428.  the color pool. A palette's color index determines the colors used to graph
  12429.  the data series associated with the palette. The colors of labels, titles,
  12430.  legends, and axes are determined by the contents of the color pool.
  12431.  
  12432.  The first element of the color pool is always 0, which is the color index
  12433.  for the screen background color. The second element is always the highest
  12434.  color index available for the graphics mode. The remaining elements repeat
  12435.  the sequences of available pixel values, beginning with 1.
  12436.  
  12437.  As shown in the example in Section 11.4, the first member of a palette data
  12438.  structure is
  12439.  
  12440.    unsigned short color;
  12441.  
  12442.  This member defines the color index for the data series associated with the
  12443.  palette.
  12444.  
  12445.  An example should make this clearer. A graphics mode of _MRES4COLOR (320 by
  12446.  200 pixels) provides four colors for display. Color index values from 0 to 3
  12447.  determine the possible colors─say, black, green, red, and brown,
  12448.  respectively. The first eight elements of this color pool are shown below.
  12449.  
  12450. ╓┌─────────────────┌────────────┌────────────────────────────────────────────╖
  12451.  Color Pool Index  Color Index  Color
  12452.  ────────────────────────────────────────────────────────────────────────────
  12453.  0                 0            Black
  12454.  1                 3            Brown
  12455.  Color Pool Index  Color Index  Color
  12456.  ────────────────────────────────────────────────────────────────────────────
  12457. 1                 3            Brown
  12458.  2                 1            Green
  12459.  3                 2            Red
  12460.  4                 3            Brown
  12461.  5                 1            Green
  12462.  6                 2            Red
  12463.  7                 3            Brown
  12464.  ────────────────────────────────────────────────────────────────────────────
  12465.  
  12466.  
  12467.  Notice that the sequence of available foreground colors repeats from the
  12468.  third element. The first data series in this case would be plotted in brown,
  12469.  the second series in green, the third series in red, the fourth series again
  12470.  in brown, and so forth.
  12471.  
  12472.  Video adapters such as the EGA or the Hercules(R) InColor(tm) Card allow 16
  12473.  on-screen colors. This allows presentation graphics to graph more series
  12474.  without duplicating colors.
  12475.  
  12476.  
  12477.  11.4.2  Style Pool
  12478.  
  12479.  Presentation graphics matches the color pool with a collection of different
  12480.  line styles called the "style pool." Entries in the style pool define the
  12481.  appearance of lines such as axes and grids. Lines can be solid, dotted,
  12482.  dashed, or some combination of styles.
  12483.  
  12484.  The second member of a palette structure defines a style code as
  12485.  
  12486.    unsigned short style;
  12487.  
  12488.  Each palette contains a style code that refers to an entry in the style pool
  12489.  in the same way that it contains a color code that refers to an entry in the
  12490.  color pool. The style code value in a palette is applicable only to line
  12491.  graphs and lined scatter diagrams. The style code determines the appearance
  12492.  of the lines drawn between points.
  12493.  
  12494.   Use the different line styles in the style pool to differentiate series.
  12495.  
  12496.  The palette's style code adds further variety to the lines of a multiseries
  12497.  graph. It is most useful when the number of lines in a chart exceeds the
  12498.  number of available colors. For example, a graph of nine different data
  12499.  series must repeat colors if only three foreground colors are available for
  12500.  the display. However, the style code for each color repetition will be
  12501.  different, ensuring that none of the lines looks the same.
  12502.  
  12503.  
  12504.  11.4.3  Pattern Pool
  12505.  
  12506.  Presentation graphics also maintains a pool of "fill patterns" that
  12507.  determine the fill design for column, bar, and pie charts. The third member
  12508.  of the palette structure holds the fill pattern. The pattern member is an
  12509.  array:
  12510.  
  12511.    fillmap fill;
  12512.  
  12513.  where  fillmap  is type-defined as
  12514.  
  12515.    typedef unsigned char fillmap[8];
  12516.  
  12517.  Each fill pattern array holds an 8-by-8 bit map that defines the fill
  12518.  pattern for the data series associated with the palette. Table 11.3 shows
  12519.  how a fill pattern of diagonal stripes is created with the  fill  pattern
  12520.  array.
  12521.  
  12522.  The bit map in Table 11.3 corresponds to screen pixels. Each of the eight
  12523.  layers of the map is a binary number, where a solid circle signifies 1 and
  12524.  an open circle signifies 0. Thus the first layer of the map─that is, the
  12525.  first byte─represents the binary number 10011001, which is the decimal
  12526.  number 153.
  12527.  
  12528.  Table   11.3 Fill Patterns
  12529.  
  12530. ╓┌───────────────────────────────────┌───────────────────────────────────────╖
  12531.  Bit Map                             Value in Fill
  12532.  ────────────────────────────────────────────────────────────────────────────
  12533.    ∙  ∙      ∙  ∙                fill[0] = 153
  12534.      ∙  ∙      ∙  ∙              fill[1] = 204
  12535.  ∙      ∙  ∙      ∙              fill[2] = 102
  12536.  ∙  ∙      ∙  ∙                  fill[3] =   51
  12537.    ∙  ∙      ∙  ∙                fill[4] = 153
  12538.      ∙  ∙      ∙  ∙              fill[5] = 204
  12539.  Bit Map                             Value in Fill
  12540.  ────────────────────────────────────────────────────────────────────────────
  12541.     ∙  ∙      ∙  ∙              fill[5] = 204
  12542.  ∙      ∙  ∙      ∙              fill[6] = 102
  12543.  ∙  ∙      ∙  ∙                  fill[7] =   51
  12544.  ────────────────────────────────────────────────────────────────────────────
  12545.  
  12546.  
  12547.  For example, if you want to create the pattern in Table 11.3 for your
  12548.  chart's first data series, you must reset the  fill array for the first
  12549.  palette structure. You can do this in five steps:
  12550.  
  12551.  
  12552.    1.  Declare a structure of type palettetype to hold the palette
  12553.        parameters.
  12554.  
  12555.    2.  Call _pg_initchart to initialize the palettes with default values.
  12556.  
  12557.    3.  Call the presentation graphics function _pg_getpalette to retrieve a
  12558.        copy of the current palette data.
  12559.  
  12560.    4.  Assign the values given in Table 11.3 to the array  fill  for the
  12561.        first palette.
  12562.  
  12563.    5.  Call the presentation graphics function _pg_setpalette to load the
  12564.        modified palette values.
  12565.  
  12566.  
  12567.  The following lines of code demonstrate these five steps:
  12568.  
  12569.    /* Declare a structure array for palette data. */
  12570.  
  12571.    palettetype palette_struct;
  12572.    .
  12573.    .
  12574.    .
  12575.    /* Initialize chart library */
  12576.  
  12577.    _pg_initchart();
  12578.    .
  12579.    .
  12580.    .
  12581.    /* Copy current palette data into palette_struct */
  12582.  
  12583.    _pg_getpalette( palette_struct );
  12584.  
  12585.    /* Reinitialize fill pattern for first palette using
  12586.       values in Table .3 */
  12587.  
  12588.    palette_struct[1].fill[0] = 153;
  12589.    palette_struct[1].fill[1] = 204;
  12590.    palette_struct[1].fill[2] = 102;
  12591.    palette_struct[1].fill[3] =  51;
  12592.    palette_struct[1].fill[4] = 153;
  12593.    palette_struct[1].fill[5] = 204;
  12594.    palette_struct[1].fill[6] = 102;
  12595.    palette_struct[1].fill[7] =  51;
  12596.  
  12597.    /* Load new palette data */
  12598.  
  12599.    _pg_setpalette( palette_struct );
  12600.  
  12601.  Now when you display your bar or column chart, the first series appears
  12602.  filled with the striped pattern shown in Table 11.3.
  12603.  
  12604.  Palette structures are used differently with pie charts. Instead of
  12605.  clarifying multiple series, fill patterns, line styles, and colors, palette
  12606.  structures are used to distinguish individual slices in a pie chart.
  12607.  Palettes are recycled if the number of slices exceeds _PG_PALETTELEN. Thus,
  12608.  the first palette dictates not only the appearance of the first slice, but
  12609.  of slice number _PG_PALETTELEN as well. The second palette determines the
  12610.  appearance of both the second slice and of slice number _PG_PALETTELEN + 1,
  12611.  and so forth.
  12612.  
  12613.  
  12614.  11.4.4  Character Pool
  12615.  
  12616.  The last member of a palette structure is an index number in a pool of ASCII
  12617.  characters:
  12618.  
  12619.    char plotchar;
  12620.  
  12621.  The member plotchar represents plot points on line graphs and scatter
  12622.  diagrams. Each palette uses a different character to distinguish plot points
  12623.  between data series.
  12624.  
  12625.  
  12626.  11.5  Customizing the Chart Environment
  12627.  
  12628.  The presentation graphics functions are designed to be flexible. You can use
  12629.  the system of default values to produce professional-looking charts with a
  12630.  minimum of programming effort. Or you can fine-tune the appearance of your
  12631.  charts by overriding default values and initializing variables explicitly in
  12632.  your program.
  12633.  
  12634.  The header file PGCHART.H defines a structure type chartenv, which organizes
  12635.  the chart environment variables. The chart environment describes everything
  12636.  about a chart except the plots themselves. It is the blank page, in other
  12637.  words, ready for plotting data. The environment determines the appearance of
  12638.  text, axes, grid lines, and legends.
  12639.  
  12640.  Colors and line styles in the chart environment are taken from palettes. In
  12641.  this way, the appearance of titles and axis lines matches the colors and
  12642.  line styles of plotted data series.
  12643.  
  12644.   You can reset any variable in the environment.
  12645.  
  12646.  Calling the _pg_defaultchart function fills the chart environment with
  12647.  default values. Presentation graphics allows you to reset any variable in
  12648.  the environment before displaying a chart. Except for adjusting the palette
  12649.  values, all initialization of data is done through a chartenv type
  12650.  structure.
  12651.  
  12652.  The sample chart programs provided in Section 11.3, "Writing a Presentation
  12653.  Graphics Program," illustrate how to adjust variables in the chart
  12654.  environment. These programs create a structure  env  of type chartenv. The
  12655.  structure  env  contains the chart environment variables, initialized by the
  12656.  call to the _pg_defaultchart function. Environment variables such as the
  12657.  chart title are then given specific values, as in
  12658.  
  12659.    strcpy( env.maintitle.title, "Good Neighbor Grocery" );
  12660.  
  12661.  Environment variables that determine colors and line styles deserve special
  12662.  mention. The chart environment holds several such variables, which can be
  12663.  recognized by their names. For example, the variable titlecolor specifies
  12664.  the color of title text. Similarly, the variable gridstyle specifies the
  12665.  line style used to draw the chart grid.
  12666.  
  12667.  These variables are index numbers, but do not refer directly to the color
  12668.  pool or line pool. They correspond instead to palette numbers. If you set
  12669.  titlecolor to 2, presentation graphics uses the color code in the second
  12670.  palette to determine the title's color. Thus, the title in this case would
  12671.  be the same color as the chart's second data series. If you change the color
  12672.  code in the palette, you'll also change the title's color.
  12673.  
  12674.  A structure of type chartenv consists of four types of secondary structures.
  12675.  The file PGCHART.H type-defines these secondary structures: titletype,
  12676.  axistype, windowtype, and legendtype.
  12677.  
  12678.  The remainder of this section describes the chart environment of
  12679.  presentation graphics. It first examines structures of the four secondary
  12680.  structures that make up the chart environment structure. The section
  12681.  concludes with a description of the chartenv structure type. Each section
  12682.  begins with a brief explanation of the structure's purpose, followed by a
  12683.  listing of the structure type definition as it appears in the PGCHART.H
  12684.  file. All symbolic constants are defined in the file PGCHART.H.
  12685.  
  12686.  
  12687.  11.5.1  titletype Structures
  12688.  
  12689.  Structures of type titletype determine text, color, and placement of titles
  12690.  appearing in the graph. The PGCHART.H file defines the structure type as
  12691.  
  12692.    typedef struct
  12693.    {
  12694.       char     title[_PG_TITLELEN];  /* Title text */
  12695.       short    titlecolor;           /* Palette color
  12696.                                         for title text */
  12697.       short    justify;              /* _PG_LEFT, _PG_CENTER,
  12698.                                         _PG_RIGHT */
  12699.    } titletype;
  12700.  
  12701.  The following list describes titletype members:
  12702.  
  12703.  Member Variable                   Description
  12704.  ────────────────────────────────────────────────────────────────────────────
  12705.  justify                           An integer specifying how the title is
  12706.                                    justified within the chart window. The
  12707.                                    symbolic constants defined
  12708.                                    in PGCHART.H for this variable are
  12709.                                    _PG_LEFT,
  12710.                                    _PG_CENTER, and _PG_RIGHT.
  12711.  
  12712.  titlecolor                        An integer between 1 and _PG_PALETTELEN
  12713.                                    that specifies a title's color. The
  12714.                                    default value for
  12715.                                    titlecolor is 1.
  12716.  
  12717.  title[_PG_TITLELEN]               A character array containing title text.
  12718.                                    For example, if  env  is a structure of
  12719.                                    type chartenv, then  env.maintitle.title
  12720.                                     holds the character string used for the
  12721.                                    main title of the chart. Similarly,
  12722.                                    env.xaxis.axistitle.title  contains the
  12723.                                    x axis title. The number of characters
  12724.                                    in a title must be one less than
  12725.                                    _PG_TITLELEN to allow room for a null
  12726.                                    terminator.
  12727.  
  12728.  
  12729.  11.5.2  axistype Structures
  12730.  
  12731.  Structures of type axistype contain variables for the axes such as color,
  12732.  scale, grid style, and tick marks. The PGCHART.H file defines the structure
  12733.  type as the following:
  12734.  
  12735.    typedef struct
  12736.    {
  12737.       short       grid;          /* TRUE=grid lines drawn;
  12738.                                     FALSE=no lines */
  12739.       short       gridstyle;     /* Style bytes for grid */
  12740.       titletype   axistitle;     /* Title definition
  12741.                                     for axis */
  12742.       short       axiscolor;     /* Color for axis */
  12743.       short       labeled;       /* TRUE=ticks marks and titles
  12744.                                     drawn */
  12745.       short       rangetype;     /* _PG_LINEARAXIS,
  12746.                                     _PG_LOGAXIS */
  12747.       float       logbase;       /* Base used if log axis */
  12748.       short       autoscale;     /* TRUE=next 7 values
  12749.                                     calculated by system */
  12750.       float       scalemin;      /* Minimum value of scale */
  12751.       float       scalemax;      /* Maximum value of scale */
  12752.       float       scalefactor;   /* Scale factor for data on
  12753.                                     this axis */
  12754.       titletype   scaletitle;    /* Title definition for
  12755.                                     scaling factor */
  12756.       float       ticinterval;   /* Distance between tick marks
  12757.                                     (world coord.) */
  12758.       short       ticformat;     /* _PG_EXPFORMAT or
  12759.                                     _PG_DECFORMAT */
  12760.       short       ticdecimals;   /* Number of decimals for tick
  12761.                                     labels (max=9) */
  12762.    } axistype;
  12763.  
  12764.  The following list describes axistype member variables:
  12765.  
  12766.  Member Variable                   Description
  12767.  ────────────────────────────────────────────────────────────────────────────
  12768.  autoscale                         A Boolean variable. If autoscale is set
  12769.                                    to TRUE,
  12770.                                    presentation graphics automatically
  12771.                                    determines
  12772.                                    values for scalefactor, scalemax,
  12773.                                    scalemin,
  12774.                                    scaletitle, ticdecimals, ticformat, and
  12775.                                    ticinterval
  12776.                                    (see below). If autoscale equals FALSE,
  12777.                                    these seven variables must be specified
  12778.                                    in your program.
  12779.  
  12780.  axiscolor                         An integer between 1 and _PG_PALETTELEN
  12781.                                    that specifies the color used for the
  12782.                                    axis and parallel grid lines. (See
  12783.                                    description for gridstyle below.) Note
  12784.                                    that this member does not determine the
  12785.                                    color of
  12786.                                    the axis title. That selection is made
  12787.                                    through the
  12788.                                    axistitle structure.
  12789.  
  12790.  axistitle                         A titletype structure that defines the
  12791.                                    title of the associated axis. The title
  12792.                                    of the y axis displays vertically to the
  12793.                                    left of the y axis, and the title of the
  12794.                                    x axis displays horizontally below the x
  12795.                                    axis.
  12796.  
  12797.  grid                              A Boolean true/false value that
  12798.                                    determines whether grid lines are drawn
  12799.                                    for the associated axis. Grid lines span
  12800.                                    the data window perpendicular to the
  12801.                                    axis.
  12802.  
  12803.  gridstyle                         An integer between 1 and _PG_PALETTELEN
  12804.                                    that specifies the grid's line style.
  12805.                                    Lines can be solid, dashed, dotted, or
  12806.                                    some combination. The default value for
  12807.                                    gridstyle is 1.
  12808.  
  12809.                                    Note that the color of the parallel axis
  12810.                                    determines the color of the grid lines.
  12811.                                    Thus, the x axis grid is the same color
  12812.                                    as the y axis, and the y axis grid is
  12813.                                    the same color as the x axis.
  12814.  
  12815.  labeled                           A Boolean value that determines whether
  12816.                                    tick marks and labels are drawn on the
  12817.                                    axis. Axis labels should not be confused
  12818.                                    with axis titles. Axis labels are
  12819.                                    numbers or descriptions such as "23.2"
  12820.                                    or "January" attached to each tick mark.
  12821.  
  12822.  logbase                           If rangetype is logarithmic, the logbase
  12823.                                    variable determines the log base used to
  12824.                                    scale the axis. The default value is 10.
  12825.  
  12826.  rangetype                         An integer that determines whether the
  12827.                                    scale of the axis is linear or
  12828.                                    logarithmic. The variable rangetype
  12829.                                    applies only to value data.
  12830.  
  12831.                                    Specify a linear scale with
  12832.                                    _PG_LINEARAXIS. A linear scale is best
  12833.                                    when the difference between axis minimum
  12834.                                    and maximum is relatively small. For
  12835.                                    example, a linear axis range 0 - 10
  12836.                                    results in 10 tick marks evenly spaced
  12837.                                    along the axis.
  12838.  
  12839.                                    Use _PG_LOGAXIS to specify a logarithmic
  12840.                                    rangetype. Logarithmic scales are useful
  12841.                                    when
  12842.                                    the range is very large or when the data
  12843.                                    varies exponentially. Line graphs of
  12844.                                    exponentially varying data can be made
  12845.                                    straight with a logarithmic
  12846.                                    rangetype.
  12847.  
  12848.  scalefactor                       All numeric data are scaled by dividing
  12849.                                    each
  12850.                                    value by scalefactor. For relatively
  12851.                                    small values,
  12852.                                    scalefactor should be 1, which is the
  12853.                                    default. But data with large values
  12854.                                    should be scaled by an appropriate
  12855.                                    factor. For example, data in the range
  12856.                                    2 million - 20 million should be plotted
  12857.                                    with
  12858.                                    scalemin set to 2, scalemax set to 20,
  12859.                                    and
  12860.                                    scalefactor set to 1 million.
  12861.  
  12862.                                    If autoscale is set to TRUE,
  12863.                                    presentation graphics automatically
  12864.                                    determines a suitable value for
  12865.                                    scalefactor based on the range of data
  12866.                                    to be plotted. Presentation graphics
  12867.                                    selects only values that are a factor of
  12868.                                    1 thousand─that is, values such as 1
  12869.                                    thousand, 1 million, or 1 billion. It
  12870.                                    then labels the
  12871.                                    scaletitle appropriately (see below). If
  12872.                                    you desire some other value for scaling,
  12873.                                    you must set autoscale to FALSE and set
  12874.                                    scalefactor to the desired scaling value.
  12875.  
  12876.  scalemax                          Highest value represented by the axis.
  12877.  
  12878.  scalemin                          Lowest value represented by the axis.
  12879.  
  12880.  scaletitle                        A titletype structure defining a string
  12881.                                    of text that
  12882.                                    describes the value of scalefactor. If
  12883.                                    autoscale is TRUE, presentation graphics
  12884.                                    automatically writes a scale description
  12885.                                    to scaletitle. If autoscale equals FALSE
  12886.                                    and scalefactor is 1, scaletitle.title
  12887.                                    should be blank. Otherwise your program
  12888.                                    should copy an appropriate scale
  12889.                                    description to scaletitle.title, such as
  12890.                                    "( x 1000)," "(in millions of units),"
  12891.                                    or "times 10 thousand dollars."
  12892.  
  12893.                                    For the y axis, the scaletitle text
  12894.                                    displays vertically between the axis
  12895.                                    title and the y axis. For the x axis,
  12896.                                    the scale title appears below the x axis
  12897.                                    title.
  12898.  
  12899.  ticdecimals                       Number of digits to display after the
  12900.                                    decimal point in tick labels. Maximum
  12901.                                    value is 9. (This variable applies only
  12902.                                    to axes with value data and is ignored
  12903.                                    for the category axis.)
  12904.  
  12905.  ticformat                         An integer that determines format of the
  12906.                                    labels
  12907.                                    assigned to each tick mark. Set
  12908.                                    ticformat  to
  12909.                                    _PG_EXPFORMAT for exponential format or
  12910.                                    to _PG_DECFORMAT for decimal. The
  12911.                                    default is _PG_DECFORMAT. (This variable
  12912.                                    applies only to axes with value data and
  12913.                                    is ignored for the category axis.)
  12914.  
  12915.  ticinterval                       Sets interval between tick marks on the
  12916.                                    axis. The tick interval is measured in
  12917.                                    the same units as the numeric data
  12918.                                    associated with the axis. For example,
  12919.                                    if 2 sequential tick marks correspond to
  12920.                                    the values 20 and 25, the tick interval
  12921.                                    between them is 5. (This variable
  12922.                                    applies only to axes with value data and
  12923.                                    is ignored for the category axis.)
  12924.  
  12925.  
  12926.  11.5.3  windowtype Structures
  12927.  
  12928.  Structures of type windowtype contain sizes, locations, and color codes for
  12929.  the three windows produced by presentation graphics: the chart window, the
  12930.  data window, and the legend. Windows are located on the screen relative to
  12931.  the screen's logical origin. By changing the logical origin, you can display
  12932.  charts that are partly or completely off the screen.
  12933.  
  12934.  The PGCHART.H file defines windowtype as the following:
  12935.  
  12936.    typedef struct
  12937.    {
  12938.       short  x1;            /* Left edge of window in
  12939.                                pixels */
  12940.       short  y1;            /* Top edge of window in
  12941.                                pixels */
  12942.       short  x2;            /* Right edge of window in
  12943.                                pixels */
  12944.       short  y2;            /* Bottom edge of window in
  12945.                                pixels */
  12946.       short  border;        /* TRUE for border, FALSE
  12947.                                otherwise */
  12948.       short  background;    /* Internal palette color for
  12949.                                window background */
  12950.       short  borderstyle;   /* Style bytes for window
  12951.                                border */
  12952.       short  bordercolor;   /* Internal palette color for
  12953.                                window border */
  12954.    } windowtype;
  12955.  
  12956.  The following list describes windowtype member variables:
  12957.  
  12958.  Member Variable                   Description
  12959.  ────────────────────────────────────────────────────────────────────────────
  12960.  background                        An integer between 1 and _PG_PALETTELEN
  12961.                                    that specifies the window's background
  12962.                                    color. The default value for background
  12963.                                    is 1.
  12964.  
  12965.  border                            A Boolean variable that determines
  12966.                                    whether a border frame is drawn around a
  12967.                                    window.
  12968.  
  12969.  bordercolor                       An integer between 1 and _PG_PALETTELEN
  12970.                                    that specifies the color of the window's
  12971.                                    border frame. The default value is 1.
  12972.  
  12973.  borderstyle                       An integer between 1 and _PG_PALETTELEN
  12974.                                    that specifies the line style of the
  12975.                                    window's border frame. The default value
  12976.                                    is 1.
  12977.  
  12978.  x1, y1, x2, y2                    Window coordinates in pixels. The
  12979.                                    ordered pair
  12980.                                    (x1, y1) specifies the coordinate of the
  12981.                                    upper left corner of the window. The
  12982.                                    ordered pair ( x2, y2 ) specifies the
  12983.                                    coordinate of the lower right corner.
  12984.  
  12985.                                    The reference point for the coordinates
  12986.                                    depends on the type of window. The chart
  12987.                                    window is located relative to the
  12988.                                    logical origin, usually the upper left
  12989.                                    corner of the screen. The data and
  12990.                                    legend windows are located relative to
  12991.                                    the upper left corner of the chart
  12992.                                    window. This allows you to change the
  12993.                                    position of the chart window without
  12994.                                    having to redefine coordinates for the
  12995.                                    other two windows.
  12996.  
  12997.  
  12998.  11.5.4  legendtype Structures
  12999.  
  13000.  Structures of type legendtype contain size, location, and colors of the
  13001.  chart legend. The PGCHART.H file defines the structure type as the
  13002.  following:
  13003.  
  13004.    typedef struct
  13005.    {
  13006.       short      legend;        /* TRUE=draw legend;
  13007.                                    FALSE=no legend */
  13008.       short      place;         /* _PG_RIGHT, _PG_BOTTOM,
  13009.                                    _PG_OVERLAY */
  13010.       short      textcolor;     /* Palette color for text*/
  13011.       short      autosize;      /* TRUE=system calculates
  13012.                                    legend size */
  13013.       windowtype legendwindow;  /* Window definition for
  13014.                                    legend */
  13015.    } legendtype;
  13016.  
  13017.  The following list describes legendtype member variables:
  13018.  
  13019.  Member Variable                   Description
  13020.  ────────────────────────────────────────────────────────────────────────────
  13021.  autosize                          A Boolean true/false variable that
  13022.                                    determines whether presentation graphics
  13023.                                    is to automatically
  13024.                                    calculate the size of the legend. If
  13025.                                    autosize equals FALSE, the legend window
  13026.                                    must be specified in the legendwindow
  13027.                                    structure (see below).
  13028.  
  13029.  legend                            A Boolean true/false variable that
  13030.                                    determines whether a legend is to appear
  13031.                                    on the chart. The legend variable is
  13032.                                    ignored by functions that graph
  13033.                                    single-series charts.
  13034.  
  13035.  legendwindow                      A windowtype structure that defines
  13036.                                    coordinates, background color, and
  13037.                                    border frame for the legend. Coordinates
  13038.                                    given in legendwindow are ignored if
  13039.                                    autosize is set to TRUE.
  13040.  
  13041.  place                             An integer that specifies the location
  13042.                                    of the legend relative to the data
  13043.                                    window. Setting place equal
  13044.                                    to the constant _PG_RIGHT positions the
  13045.                                    legend
  13046.                                    to the right of the data window. Setting
  13047.                                    place to
  13048.                                    _PG_BOTTOM positions the legend below
  13049.                                    the data window. Setting place to
  13050.                                    _PG_OVERLAY positions the legend within
  13051.                                    the data window.
  13052.  
  13053.                                    These settings influence the size of the
  13054.                                    data window. If place equals _PG_RIGHT
  13055.                                    or _PG_BOTTOM, presentation graphics
  13056.                                    automatically sizes the data window to
  13057.                                    accommodate the legend. If place equals
  13058.                                    _PG_OVERLAY, the data window is sized
  13059.                                    without regard to the legend.
  13060.  
  13061.  textcolor                         An integer between 1 and _PG_PALETTELEN
  13062.                                    that specifies the color of text within
  13063.                                    the legend window.
  13064.  
  13065.  
  13066.  11.5.5  chartenv Structures
  13067.  
  13068.  A structure of type chartenv defines the chart environment. The following
  13069.  listing shows that a chartenv type structure consists almost entirely of
  13070.  structures of the four types described above.
  13071.  
  13072.  The PGCHART.H file defines the chartenv structure type as the following:
  13073.  
  13074.    typedef struct
  13075.    {
  13076.       short       charttype;     /* Chart type */
  13077.       short       chartstyle;    /* Chart style */
  13078.       windowtype  chartwindow;   /* Window definition for
  13079.                                     overall chart */
  13080.       windowtype  datawindow;    /* Window definition for data
  13081.                                     part of chart */
  13082.       titletype   maintitle;     /* Main chart title */
  13083.       titletype   subtitle;      /* Chart subtitle */
  13084.       axistype    xaxis;         /* Definition for x axis */
  13085.       axistype    yaxis;         /* Definition for y axis */
  13086.       legendtype  legend;        /* Definition for legend */
  13087.    } chartenv;
  13088.  
  13089.   Initialize the chart environment with the _pg_defaultchart function.
  13090.  
  13091.  The data in a chartenv type structure is initialized by calling the function
  13092.  _pg_defaultchart. If your program does not call _pg_defaultchart, it must
  13093.  explicitly define every variable in the chart environment─a tedious
  13094.  procedure. The recommended method for adjusting the appearance of your chart
  13095.  is to initialize variables for the proper chart type by calling the
  13096.  _pg_defaultchart function, and then to reassign selected environment
  13097.  variables such as titles.
  13098.  
  13099.  The following list describes chartenv member variables:
  13100.  
  13101.  Member Variable                   Description
  13102.  ────────────────────────────────────────────────────────────────────────────
  13103.  chartstyle                        An integer that determines the style of
  13104.                                    the chart
  13105.                                    (see Table 11.2). Legal values for
  13106.                                    chartstyle are _PG_PERCENT and
  13107.                                    _PG_NOPERCENT for pie charts;
  13108.                                    _PG_PLAINBARS and _PG_STACKEDBARS for
  13109.                                    bar and column charts; and _PG_POINTONLY
  13110.                                    and  _PG_POINTANDLINE for line graphs
  13111.                                    and scatter diagrams. This variable
  13112.                                    corresponds to the third argument for
  13113.                                    the _pg_defaultchart function.
  13114.  
  13115.  charttype                         An integer that determines the type of
  13116.                                    chart displayed. The value of charttype
  13117.                                    is _PG_BARCHART, _PG_COLUMNCHART,
  13118.                                    _PG_LINECHART, _PG_SCATTERCHART, or
  13119.                                    _PG_PIECHART. This variable corresponds
  13120.                                    to the second argument for the
  13121.                                    _pg_defaultchart function.
  13122.  
  13123.  chartwindow                       A windowtype structure that defines the
  13124.                                    appearance of the chart window.
  13125.  
  13126.  datawindow                        A windowtype structure that defines the
  13127.                                    appearance of the data window.
  13128.  
  13129.  legend                            A legendtype structure that defines the
  13130.                                    appearance of the legend window.
  13131.  
  13132.  maintitle                         A titletype structure that defines the
  13133.                                    appearance of the main title of the
  13134.                                    chart.
  13135.  
  13136.  subtitle                          A titletype structure that defines the
  13137.                                    appearance of the chart's subtitle.
  13138.  
  13139.  xaxis                             An axistype structure that defines the
  13140.                                    appearance of the x axis. (This variable
  13141.                                    is not applicable for pie charts.)
  13142.  
  13143.  yaxis                             An axistype structure that defines the
  13144.                                    appearance of the y axis. (This variable
  13145.                                    is not applicable for pie charts.)
  13146.  
  13147.  
  13148.  
  13149.  
  13150.  
  13151.  
  13152.  
  13153.  
  13154.  
  13155.  
  13156.  Chapter 12  Programming with Mixed Languages
  13157.  ────────────────────────────────────────────────────────────────────────────
  13158.  
  13159.  There are times when your Microsoft C programs need to call programs written
  13160.  in other languages or when programs written in other languages need to call
  13161.  your C functions. This is called mixed-language programming. For example,
  13162.  when a particular subprogram is available commercially in a language other
  13163.  than C or when algorithms are described more naturally in a different
  13164.  language, you need to use more than one language.
  13165.  
  13166.  This chapter describes the elements of mixed-language programming─how to
  13167.  make calls from programs written in one language to routines written in
  13168.  another.
  13169.  
  13170.  
  13171.  12.1  Making Mixed-Language Calls
  13172.  
  13173.  Mixed-language programming always involves a call to a function, procedure,
  13174.  or subroutine. For example, a BASIC main module may need to execute a
  13175.  specific task that you would like to program separately. Instead of calling
  13176.  a BASIC subprogram, however, you decide to call a C function.
  13177.  
  13178.  Mixed-language calls involve calling functions in separate modules. Instead
  13179.  of compiling all of your source modules with the same compiler, you use
  13180.  different compilers. In the instance mentioned above, you would compile the
  13181.  mainmodule source file with the BASIC compiler, another source file (written
  13182.  in C) with the C compiler, and then link the two object files.
  13183.  
  13184.  Figure 12.1 illustrates how the syntax of a mixed-language call works, using
  13185.  the instance mentioned above.
  13186.  
  13187.  (This figure may be found in the printed book.)
  13188.  
  13189.  In Figure 12.1, the BASIC call to C is  CALL Prn, similar to a call to a
  13190.  BASIC subprogram. There are two differences between this mixed-language call
  13191.  and a call between two BASIC modules:
  13192.  
  13193.  
  13194.    1.  The subprogram  Prn  is implemented in C, using standard C syntax.
  13195.  
  13196.    2.  The implementation of the call in BASIC is affected by the DECLARE
  13197.        statement, which uses the CDECL keyword to create compatibility with
  13198.        C. The DECLARE statement (which is described in detail in the
  13199.        Microsoft BASIC Language Reference and the Microsoft BASIC
  13200.        Programmer's Guide) is an example of a mixed-language "interface"
  13201.        statement. These interface statements override default naming and
  13202.        calling conventions. Each language provides its own form of interface.
  13203.  
  13204.  
  13205.  You can make mixed-language calls to routines regardless of whether they
  13206.  have return values. (In this chapter, "routine" refers to any function,
  13207.  procedure, or subroutine that can be called from another module.)
  13208.  
  13209.  Table 12.1 shows the correspondence between calls to routines in different
  13210.  languages.
  13211.  
  13212.  Table 12.1  Language Equivalents for Routine Calls
  13213.  
  13214. ╓┌───────────────────┌────────────────────┌──────────────────────────────────╖
  13215.  Language            Return Value         No Return Value
  13216.  ────────────────────────────────────────────────────────────────────────────
  13217.  Assembly Language   Procedure            Procedure
  13218.  BASIC               FUNCTION procedure   Subprogram
  13219.  C                   function             (void) function
  13220.  FORTRAN             FUNCTION             SUBROUTINE
  13221.  Pascal              Function             Procedure
  13222.  ────────────────────────────────────────────────────────────────────────────
  13223.  
  13224.  
  13225.  For example, a C module can make a subprogram call to a FORTRAN subroutine.
  13226.  You can prototype a FORTRAN subroutine as a function with a  void type.
  13227.  
  13228.  ────────────────────────────────────────────────────────────────────────────
  13229.  NOTE
  13230.  
  13231.  BASIC DEF FN functions and GOSUB subroutines cannot be called from another
  13232.  language.
  13233.  ────────────────────────────────────────────────────────────────────────────
  13234.  
  13235.  
  13236.  12.2  Language Convention Requirements
  13237.  
  13238.  To mix languages, the calling program must observe the same conventions as
  13239.  the called program. The conventions described in this section govern the
  13240.  following:
  13241.  
  13242.  
  13243.    ■   How compilers treat identifiers, including function and variable names
  13244.        (naming convention)
  13245.  
  13246.    ■   How the subprogram call is implemented (calling convention)
  13247.  
  13248.    ■   How parameters are passed (parameter-passing convention)
  13249.  
  13250.  
  13251.  
  13252.  12.2.1  Naming Convention Requirement
  13253.  
  13254.  Both the calling program and the called subprogram must agree on the names
  13255.  of identifiers. Identifiers can refer to subprograms (functions, procedures,
  13256.  and subroutines) or to variables that have a public or global scope. Each
  13257.  language alters the names of identifiers.
  13258.  
  13259.  The term "naming convention" refers to the way a compiler alters the name of
  13260.  the routine before placing it in an object file. Languages may alter the
  13261.  identifier names differently. You can choose between several naming
  13262.  conventions to ensure that the names in the calling program agree with those
  13263.  in the called program. If the names of called routines are stored
  13264.  differently in each object file, the linker will not be able to find a
  13265.  match. It will instead report unresolved external references.
  13266.  
  13267.  Microsoft compilers place machine code into object files; they also place
  13268.  the names of all publicly accessed routines and variables in object files.
  13269.  The linker can then compare the name of a routine called in one module with
  13270.  the name of a routine defined in another module, and recognize a match.
  13271.  Names are stored in the ASCII (American Standard Code for Information
  13272.  Interchange) character set.
  13273.  
  13274.   Some languages translate names to uppercase.
  13275.  
  13276.  BASIC, FORTRAN, and Pascal use similar naming conventions. They translate
  13277.  each letter to uppercase. BASIC type declaration characters (%, &, !, #, $)
  13278.  are dropped.
  13279.  
  13280.  Each language recognizes a different number of characters. FORTRAN
  13281.  recognizes the first 31 characters of any name (unless identifier names are
  13282.  truncated), Pascal the first 8, and BASIC the first 40. If a name is longer
  13283.  than the language will recognize, additional characters are simply not
  13284.  placed in the object file.
  13285.  
  13286.  ────────────────────────────────────────────────────────────────────────────
  13287.  NOTE
  13288.  
  13289.  Versions of Microsoft FORTRAN previous to version 5.0 truncated identifiers
  13290.  to six characters. As of version 5.0, FORTRAN retains up to 31 characters of
  13291.  significance unless you use the /4Yt option.
  13292.  ────────────────────────────────────────────────────────────────────────────
  13293.  
  13294.   C is a case-sensitive language.
  13295.  
  13296.  The C compiler does not translate any letters to uppercase. It inserts a
  13297.  leading underscore ( _ ) in front of the name of each routine. C recognizes
  13298.  the first 31 characters of a name.
  13299.  
  13300.  Differences in naming conventions are dealt with automatically by
  13301.  mixedlanguage keywords, as long as you follow two rules:
  13302.  
  13303.  
  13304.    1.  If you use any FORTRAN routines that were compiled with the /4Yt
  13305.        command-line option or with the $TRUNCATE metacommand enabled, make
  13306.        all names 6 characters or less. Make all names 6 characters or less
  13307.        when using FORTRAN routines compiled with versions of the FORTRAN
  13308.        compiler prior to 5.0.
  13309.  
  13310.    2.  Do not use the /NOIGNORECASE linker option (which causes the linker to
  13311.        treat identifiers in a case-sensitive manner). With C modules, this
  13312.        means that you must be careful not to rely upon differences between
  13313.        uppercase and lowercase letters when programming.
  13314.  
  13315.        CL automatically uses the /NOIGNORECASE option when linking. To solve
  13316.        the problems created by this behavior, either link separately with the
  13317.        LINK utility, or use all lowercase letters in your C function names
  13318.        and public variables (global variables that are not declared as
  13319.        static).
  13320.  
  13321.  
  13322.  ────────────────────────────────────────────────────────────────────────────
  13323.  NOTE
  13324.  
  13325.  If you use the command-line option /Gc (generate Pascal-style function
  13326.  calls) when you compile, or if you declare a function or variable with the
  13327.  _pascal keyword, the compiler will translate your identifiers to uppercase.
  13328.  ────────────────────────────────────────────────────────────────────────────
  13329.  
  13330.  Figure 12.2 illustrates a complete mixed-language development example,
  13331.  showing how naming conventions enter into the process.
  13332.  
  13333.  (This figure may be found in the printed book.)
  13334.  
  13335.  In Figure 12.2, note that the BASIC compiler inserts a leading underscore in
  13336.  front of  Prn  as it places the name into the object file, because the CDECL
  13337.  keyword directs the BASIC compiler to use the C naming convention. BASIC
  13338.  will also convert all letters to lowercase when this keyword is used.
  13339.  (Converting letters to lowercase is not part of the C naming convention;
  13340.  however, it is consistent with the programming style of many C programs.)
  13341.  
  13342.  
  13343.  12.2.2  Calling Convention Requirement
  13344.  
  13345.  The term "calling convention" refers to the way a language implements a
  13346.  call. The choice of calling convention affects the machine instructions that
  13347.  a compiler generates to execute (and return from) a function, procedure, or
  13348.  subroutine call.
  13349.  
  13350.  It is crucial that the two routines concerned (the routine issuing a call
  13351.  and the routine being called) use the same protocol. Otherwise, the
  13352.  processor may receive inconsistent instructions, causing the program to
  13353.  behave incorrectly.
  13354.  
  13355.  The use of a calling convention affects programming in three ways:
  13356.  
  13357.  
  13358.    1.  The calling routine uses a calling convention to determine the order
  13359.        in which to pass arguments (parameters) to another routine. This
  13360.        convention can be specified in a mixed-language interface statement or
  13361.        declaration.
  13362.  
  13363.    2.  The called routine uses a calling convention to determine the order in
  13364.        which to receive the parameters passed to it. In most languages, this
  13365.        convention can be specified in the routine's heading. BASIC, however,
  13366.        always uses its own convention to receive parameters.
  13367.  
  13368.    3.  Both the calling routine and the called routine must agree on which of
  13369.        them is responsible for adjusting the stack after all parameters are
  13370.        removed.
  13371.  
  13372.  
  13373.  In other words, each call to a routine uses a certain calling convention;
  13374.  each routine heading specifies or assumes some calling convention. The two
  13375.  conventions must be compatible. With all languages except BASIC, it is
  13376.  possible to change the calling convention at the point of the call or at the
  13377.  declaration of the called routine. Usually, however, it is easier to adopt
  13378.  the convention of the called routine. For example, a C function would use
  13379.  its own convention to call another C function, and would use the Pascal
  13380.  convention to call Pascal.
  13381.  
  13382.  BASIC, FORTRAN, and Pascal use the same standard calling convention. C uses
  13383.  a different convention.
  13384.  
  13385.  
  13386.  Effects of Calling Conventions
  13387.  
  13388.  Calling conventions dictate three things:
  13389.  
  13390.  
  13391.    1.  The way parameters are communicated from one routine to another (in
  13392.        Microsoft mixed-language programming, parameters or pointers to the
  13393.        parameters are passed on the stack)
  13394.  
  13395.    2.  The order in which parameters are passed from one routine to another
  13396.  
  13397.    3.  The part of the program responsible for adjusting the stack
  13398.  
  13399.  
  13400.   Some languages pass parameters in a different order than C.
  13401.  
  13402.  The BASIC, FORTRAN and Pascal calling conventions push parameters onto the
  13403.  stack in the order in which they appear in the source code. For example, the
  13404.  BASIC statement
  13405.  
  13406.    CALL Calc( A, B )
  13407.  
  13408.  pushes argument  A  onto the stack before it pushes  B. These conventions
  13409.  also specify that the stack is adjusted by the called routine just before
  13410.  returning control to the caller.
  13411.  
  13412.  The C calling convention pushes parameters onto the stack in the reverse
  13413.  order from their appearance in the source code. For example, the C function
  13414.  call
  13415.  
  13416.    calc( a, b );
  13417.  
  13418.  pushes  b  onto the stack before it pushes  a. In contrast with the other
  13419.  high-level languages, the C calling convention specifies that a calling
  13420.  routine always adjusts the stack immediately after the called routine
  13421.  returns control.
  13422.  
  13423.  The BASIC, FORTRAN, and Pascal conventions produce slightly less object
  13424.  code. However, the C convention makes calling with a variable number of
  13425.  parameters possible. (Because the first parameter is always the last one
  13426.  pushed, it is always on the top of the stack; therefore it has the same
  13427.  address relative to the frame pointer, regardless of how many parameters
  13428.  were actually passed.)
  13429.  
  13430.  ────────────────────────────────────────────────────────────────────────────
  13431.  NOTE
  13432.  
  13433.  The _fastcall keyword, which specifies that parameters are to be passed in
  13434.  registers, is incompatible with programs written in other languages. Avoid
  13435.  using _fastcall or the /Gr command-line option for C functions that you
  13436.  intend to make public to BASIC, FORTRAN, or Pascal programs.
  13437.  ────────────────────────────────────────────────────────────────────────────
  13438.  
  13439.  
  13440.  12.2.3  Parameter-Passing Requirement
  13441.  
  13442.  Your programs must agree on the calling convention and the naming
  13443.  convention; they must also agree on the order in which they pass parameters.
  13444.  It is important that your routines send parameters in the same way to ensure
  13445.  proper data transmission and correct program results.
  13446.  
  13447.  Microsoft compilers support three methods for passing a parameter:
  13448.  
  13449.  Method                            Description
  13450.  ────────────────────────────────────────────────────────────────────────────
  13451.  Near reference                    Passes a variable's near (offset)
  13452.                                    address. This address is expressed as an
  13453.                                    offset from the default data segment.
  13454.  
  13455.                                    This method gives the called routine
  13456.                                    direct access to the variable itself.
  13457.                                    Any change the routine makes to the
  13458.                                    parameter changes the variable in the
  13459.                                    calling routine.
  13460.  
  13461.  Far reference                     Passes a variable's far (segmented)
  13462.                                    address.
  13463.  
  13464.                                    This method is similar to passing by
  13465.                                    near reference, except that a longer
  13466.                                    address is passed. This method is slower
  13467.                                    than passing by near reference, but is
  13468.                                    necessary when you pass data that is
  13469.                                    outside the default data segment. (This
  13470.                                    is an issue in BASIC or Pascal only if
  13471.                                    you have specifically requested far
  13472.                                    memory.)
  13473.  
  13474.  Value                             Passes only the variable's value, not
  13475.                                    its address.
  13476.  
  13477.                                    With this method, the called routine
  13478.                                    knows the value of the parameter but has
  13479.                                    no access to the original variable.
  13480.                                    Changes to a value passed by a parameter
  13481.                                    have no affect on the value of the
  13482.                                    parameter in the calling routine.
  13483.  
  13484.  These different parameter-passing methods mean that you must consider the
  13485.  following when programming with mixed languages:
  13486.  
  13487.  
  13488.    ■   You need to make sure that the called routine and the calling routine
  13489.        use the same method for passing each parameter (argument). In most
  13490.        cases, you will need to check the parameter-passing defaults used by
  13491.        each language and possibly make adjustments. Each language has
  13492.        keywords or language features that allow you to change
  13493.        parameter-passing methods.
  13494.  
  13495.    ■   You may want to choose a specific parameter-passing method rather than
  13496.        using the defaults of any language.
  13497.  
  13498.  
  13499.  Table 12.2 summarizes the parameter-passing defaults for each language.
  13500.  
  13501.  Table 12.2  Parameter-Passing Defaults
  13502.  
  13503. ╓┌─────────┌─────────────────────┌─────────────────────┌─────────────────────╖
  13504.  Language  Near Reference        Far Reference         By Value
  13505.  Language  Near Reference        Far Reference         By Value
  13506.  ────────────────────────────────────────────────────────────────────────────
  13507.  BASIC     All                   ---                   ---
  13508.  
  13509.  C         Near arrays           Far arrays            All data except
  13510.                                                        arrays
  13511.  
  13512.  FORTRAN   All (medium model)    All (large model)     With attributes(1)
  13513.  
  13514.  Pascal    VAR, CONST            VARS, CONSTS          Other parameters
  13515.  
  13516.  ────────────────────────────────────────────────────────────────────────────
  13517.  
  13518.  
  13519.  
  13520.  (1)  When a PASCAL or C attribute is applied to a FORTRAN routine, passing
  13521.  by value becomes the  default.
  13522.  
  13523.  
  13524.  
  13525.  12.3  Compiling and Linking
  13526.  
  13527.  After you have written your source files and decided on a naming convention,
  13528.  a calling convention, and a parameter-passing convention, you are ready to
  13529.  compile and link individual modules.
  13530.  
  13531.  
  13532.  12.3.1  Compiling with Correct Memory Models
  13533.  
  13534.  With BASIC, FORTRAN, and Pascal, no special options are required to compile
  13535.  source files that are part of a mixed-language program.
  13536.  
  13537.   With C, not all memory models are compatible with other languages.
  13538.  
  13539.  BASIC, FORTRAN, and Pascal use only far (segmented) code addresses.
  13540.  Therefore, you must use one of two techniques with C programs that call one
  13541.  of these languages: compile C modules in medium, large, or huge model (using
  13542.  the /AX command-line options), because these models also use far code
  13543.  addresses; or apply the _far keyword to the definitions of C functions you
  13544.  make public. If you use the /AX command-line option to specify medium,
  13545.  large, or huge model, all your function calls become far by default. This
  13546.  means you don't have to declare your functions explicitly with the _far
  13547.  keyword.
  13548.  
  13549.  Choice of memory model affects the default data pointer size in C and
  13550.  FORTRAN, although this default can be overridden with the _near and _far
  13551.  keywords. With C and FORTRAN, choice of memory model also affects whether
  13552.  data objects are located in the default data segment; if a data object is
  13553.  not located in the default data segment, it cannot be passed by near
  13554.  reference.
  13555.  
  13556.  For more information about code and data address sizes in C, refer to
  13557.  Chapter 2, "Managing Memory."
  13558.  
  13559.  
  13560.  12.3.2  Linking with Language Libraries
  13561.  
  13562.  In most cases, you can easily link modules compiled with different
  13563.  languages. Do any of the following to ensure that all required libraries
  13564.  link in the correct order:
  13565.  
  13566.  
  13567.    ■   Put all language libraries in the same directory as the source files.
  13568.  
  13569.    ■   List directories containing all needed libraries in the LIB
  13570.        environment variable.
  13571.  
  13572.    ■   Let the linker prompt you for libraries.
  13573.  
  13574.  
  13575.  In each of the cases above, the linker finds libraries in the order that it
  13576.  requires them. If you enter the library names on the command line, make sure
  13577.  you enter them in an order that allows the linker to resolve your program's
  13578.  external references. Here are some points to observe when specifying
  13579.  libraries on the command line:
  13580.  
  13581.  
  13582.    ■   If you are using FORTRAN to write one of your modules, you need to
  13583.        link with the /NOD (no default libraries) option and explicitly
  13584.        specify all the libraries you need on the link command line. You can
  13585.        also specify these libraries with an automatic-response file (or batch
  13586.        file), but you cannot use a default-library search.
  13587.  
  13588.    ■   If your program uses both FORTRAN and C, specify the library for the
  13589.        most recent of the two language products first. In addition, make sure
  13590.        that you choose a C-compatible library when you install FORTRAN.
  13591.  
  13592.    ■   If you are listing BASIC libraries on the LINK command line, specify
  13593.        those libraries first.
  13594.  
  13595.  
  13596.  The following example shows how to link two modules,  mod1  and  mod2, with
  13597.  a user library, GRAFX, the C run-time library, LLIBCE, and the FORTRAN
  13598.  run-time library, LLIBFORE:
  13599.  
  13600.    LINK /NOD mod1 mod2,,,GRAFX+LLIBCE+LLIBFORE
  13601.  
  13602.  
  13603.  12.4  C Calls to High-Level Languages
  13604.  
  13605.  Just as you can call Microsoft C routines from other Microsoft languages,
  13606.  you can call routines written in Microsoft FORTRAN and Pascal from C. With
  13607.  FORTRAN, Pascal, and C, freestanding routines can be written with no
  13608.  restriction. When calling BASIC routines, however, you must write the main
  13609.  program in BASIC; any subprograms are free to call one another, whether they
  13610.  are written in C or BASIC.
  13611.  
  13612.  For information about how to pass particular kinds of data, see Section
  13613.  12.9, "Handling Data in Mixed-Language Programming."
  13614.  
  13615.  
  13616.  Executing a Mixed-Language Call
  13617.  
  13618.  The C interface to other languages uses standard C prototypes, with the
  13619.  _fortran or _pascal keyword. Using either of these keywords causes the
  13620.  routine to be called with the FORTRAN/Pascal naming and calling convention.
  13621.  (The FORTRAN/Pascal convention also works for BASIC.) Here are the
  13622.  recommended steps for executing a mixed-language call from C:
  13623.  
  13624.  
  13625.    1.  Write a prototype for each mixed-language routine called. The
  13626.        prototype should declare the routine extern for the purpose of program
  13627.        documentation.
  13628.  
  13629.        Instead of using the _fortran or _pascal keyword, you can simply
  13630.        compile with the Pascal calling convention option (/Gc). The /Gc
  13631.        option causes all functions in the module to use the FORTRAN/Pascal
  13632.        naming and calling conventions, except where you apply the _cdecl
  13633.        keyword.
  13634.  
  13635.    2.  Pass the values of variables or pointers to variables. You can obtain
  13636.        a pointer to a variable with the address-of (&) operator.
  13637.  
  13638.        In C, array names are always passed as pointers to the first element
  13639.        of the array; they are always passed by reference.
  13640.  
  13641.        The prototype you declare for your function ensures that you are
  13642.        passing the correct length address (that is, near or far).
  13643.  
  13644.    3.  Issue a function call in your program as though you were calling a C
  13645.        function.
  13646.  
  13647.    4.  Always compile the C module in either medium, large, or huge model, or
  13648.        use the _far keyword in your function prototype. This ensures that a
  13649.        far (intersegment) call is made to the routine.
  13650.  
  13651.  
  13652.  
  13653.  Using the _fortran or _pascal Keyword
  13654.  
  13655.  There are two rules of syntax that apply when you use the _fortran or
  13656.  _pascal keyword:
  13657.  
  13658.  
  13659.    1.  The _fortran and _pascal keywords modify only the item immediately to
  13660.        their right.
  13661.  
  13662.    2.  The _near and _far keywords can be used with the _fortran and _pascal
  13663.        keywords in prototypes. The sequences _fortran _far and _far _fortran
  13664.        are equivalent.
  13665.  
  13666.  
  13667.  The keywords _pascal and _fortran have the same effect on the program; using
  13668.  one or the other makes no difference except for internal program
  13669.  documentation. Use _fortran to declare a FORTRAN routine, _pascal to declare
  13670.  a Pascal rou-tine, and either keyword to declare a BASIC routine.
  13671.  
  13672.  The following examples demonstrate the syntax rules presented above.
  13673.  
  13674.  The example below declares func to be a BASIC, Pascal, or FORTRAN function
  13675.  taking two short parameters and returning a short value.
  13676.  
  13677.    short _pascal func( short sarg1, short sarg2 );
  13678.  
  13679.  The example below declares func to be pointer to a BASIC, Pascal, or FORTRAN
  13680.  routine that takes a long parameter and returns no value. The keyword void
  13681.  is appropriate when the called routine is a BASIC subprogram, Pascal
  13682.  procedure, or FORTRAN subroutine, since it indicates that the function
  13683.  returns no value.
  13684.  
  13685.    void ( _fortran * func )( long larg );
  13686.  
  13687.  The example below declares func to be a _near BASIC, Pascal, or FORTRAN
  13688.  routine. The routine receives a double parameter by reference (because it
  13689.  expects a pointer to a double) and returns a short value.
  13690.  
  13691.    short _near _pascal func( _near double * darg );
  13692.  
  13693.  The example below is equivalent to the preceding example ( _pascal _near is
  13694.  equivalent to _near _pascal).
  13695.  
  13696.    short _pascal _near func( _near double * darg );
  13697.  
  13698.   You can make C adopt the conventions of other languages.
  13699.  
  13700.  When you call a BASIC subprogram, you must use the FORTRAN/Pascal
  13701.  conventions to make the call. When you call FORTRAN or Pascal, however, you
  13702.  have a choice. You can make C adopt the conventions described in the
  13703.  previous section, or you can make the FORTRAN or Pascal routine adopt the C
  13704.  conventions.
  13705.  
  13706.  To make a FORTRAN or Pascal routine adopt the C conventions, put the C
  13707.  attribute in the heading of the routine's definition. The following example
  13708.  shows the syntax for the C attribute in a FORTRAN subroutine-definition
  13709.  heading:
  13710.  
  13711.    SUBROUTINE FFROMC [C] (N)
  13712.    INTEGER*2 N
  13713.  
  13714.  The following example shows the syntax for the C attribute in a Pascal
  13715.  procedure-definition heading:
  13716.  
  13717.    PROCEDURE Pfromc( n : INTEGER ) [C];
  13718.  
  13719.  To make a C function adopt the FORTRAN/Pascal conventions, declare the
  13720.  function as _fortran or _pascal. For example,
  13721.  
  13722.    void _pascal CfromP( int n );
  13723.  
  13724.  
  13725.  12.5  C Calls to BASIC
  13726.  
  13727.  No BASIC routine can be executed unless the main program is in BASIC,
  13728.  because a BASIC routine requires the environment to be initialized in a way
  13729.  that is unique to BASIC. No other language will perform this special
  13730.  initialization.
  13731.  
  13732.  However, your program can start up in BASIC, call a C function that does
  13733.  most of the work of the program, and then call BASIC subprograms and
  13734.  function procedures as needed. Figure 12.3 illustrates how to do this.
  13735.  
  13736.  (This figure may be found in the printed book.)
  13737.  
  13738.  Follow these rules when you call BASIC from C:
  13739.  
  13740.  
  13741.    1.  Start up in a BASIC main module. You will need to use the DECLARE
  13742.        statement to provide an interface to the C module.
  13743.  
  13744.    2.  In the C module, write a prototype for the BASIC routine and include
  13745.        type information for parameters. Use either the _fortran or _pascal
  13746.        keyword to modify the routine itself.
  13747.  
  13748.    3.  Make sure that all data are passed as near pointers. BASIC can pass
  13749.        data in a variety of ways but is unable to receive data in any form
  13750.        other than near reference. With near pointers, the program assumes
  13751.        that the data are in the default data segment. If you want to pass
  13752.        data that are not in the default data segment, copy the data to a
  13753.        variable in the default data segment.
  13754.  
  13755.    4.  Compile the C module in medium or large model to ensure far
  13756.        (intersegment) calls.
  13757.  
  13758.  
  13759.  The example below demonstrates a BASIC program that calls a C function. The
  13760.  C function then calls a BASIC function that returns twice the number passed
  13761.  to it and a BASIC subprogram that prints two numbers.
  13762.  
  13763.    ' BASIC source
  13764.    '
  13765.    ' The main program is in BASIC because of BASIC's start-up
  13766.    ' requirements.  The BASIC main program calls the C function
  13767.    ' Cprog.
  13768.    '
  13769.    ' Cprog calls the BASIC subroutine Dbl.
  13770.    '
  13771.    DEFINT A-Z
  13772.    DECLARE SUB Cprog CDECL()
  13773.    CALL Cprog
  13774.    END
  13775.    '
  13776.    FUNCTION Dbl(N) STATIC
  13777.        Dbl = N*2
  13778.    END FUNCTION
  13779.    '
  13780.    SUB Printnum(A,B) STATIC
  13781.        PRINT "The first number is ";A
  13782.        PRINT "The second number is ";B
  13783.    END SUB
  13784.  
  13785.  
  13786.    /* C source; compile in medium or large model */
  13787.  
  13788.    int _fortran dbl( int _near * N );
  13789.    void _fortran printnum( int _near * A, int _near * B );
  13790.  
  13791.    void cprog()
  13792.    {
  13793.    int a = 5;
  13794.    int b = 6;
  13795.  
  13796.        printf( "%d times 2 is %d\n", a, dbl( &a ) );
  13797.        printnum( &a, &b );
  13798.    }
  13799.  
  13800.  In the previous example, note that the addresses of  a  and  b  are passed,
  13801.  since BASIC expects to receive addresses for parameters. This is important
  13802.  because C passes parameters by value unless you use the address-of (&)
  13803.  operator to obtain the address, or are passing an array. Also note that the
  13804.  function prototype for  printnum  declares the parameters as near pointers.
  13805.  The prototype causes the
  13806.  
  13807.  variables to be passed by near reference. If  a  or  b  is declared as _far,
  13808.  the C compiler issues a warning that you are converting a far pointer to a
  13809.  near pointer and that a segment was lost in the conversion.
  13810.  
  13811.  Calling and naming conventions are resolved by the CDECL keyword in the
  13812.  BASIC declaration of Cprog, and by the _fortran keyword in the C declaration
  13813.  of  dbl  and  printnum.
  13814.  
  13815.   BASIC can invoke one of your functions as part of the termination
  13816.  procedure.
  13817.  
  13818.  Versions of QuickBASIC later than 4.0 provide a "user entry point,"
  13819.  B_OnExit, which can be called directly from C. The B_OnExit function enables
  13820.  you to make sure you have performed an orderly termination. The following
  13821.  code shows how to use B_OnExit.
  13822.  
  13823.    #include <malloc.h>    /* For declaration of _fmalloc */
  13824.    #include <stdlib.h>    /* For declaration of onexit_t */
  13825.  
  13826.    /* The prototype for B_OnExit declares it as a function
  13827.     * returning type onexit_t that takes one parameter. The
  13828.     * parameter is a far pointer to a function that returns
  13829.     * no value.
  13830.     */
  13831.    extern onexit_t _pascal _far B_OnExit( onexit_t );
  13832.    void TermProc( void );
  13833.  
  13834.    int * p_IntArray;
  13835.  
  13836.    void InitProc( void )
  13837.    {
  13838.        /* Allocate far space for 20-integer array */
  13839.  
  13840.        p_IntArray = (int *)_fmalloc( 20 * sizeof( int ) );
  13841.  
  13842.        /* Log termination routine (TermProc) with BASIC. */
  13843.  
  13844.        B_OnExit( TermProc );
  13845.    }
  13846.  
  13847.    void TermProc( void )
  13848.    {
  13849.        free( p_IntArray );    /* Release far space allocated */
  13850.    }                          /* previously by InitProc.     */
  13851.  
  13852.  
  13853.  12.6  C Calls to FORTRAN
  13854.  
  13855.  This section shows two examples of C-FORTRAN programs. There are two types
  13856.  of subprogram calls to FORTRAN routines: calls to subroutines and calls to
  13857.  functions. Functions return a value, while subroutines do not. The examples
  13858.  in the next sections illustrate how to handle the difference between
  13859.  function and subroutine calls.
  13860.  
  13861.  
  13862.  12.6.1  Calling a FORTRAN Subroutine from C
  13863.  
  13864.  The example below demonstrates a C main module calling a FORTRAN subroutine,
  13865.  MAXPARAM. This subroutine adjusts the lower of two arguments to be equal to
  13866.  the higher argument.
  13867.  
  13868.    /* C source file - calls FORTRAN subroutine
  13869.     * Compile in medium or large model
  13870.     */
  13871.  
  13872.    extern void _fortran maxparam( int _near * I, int _near * J );
  13873.  
  13874.    /* Declare as void, because there is no return value.
  13875.     * FORTRAN keyword causes C to use FORTRAN/Pascal
  13876.     * calling and naming conventions.
  13877.     * Two integer parameters, passed by near reference.
  13878.     */
  13879.  
  13880.    main()
  13881.    {
  13882.        int a = 5;
  13883.        int b = 7;
  13884.  
  13885.        printf( "a = %d, b = %d", a, b );
  13886.        maxparam( &a, &b );
  13887.        printf( "a = %d, b = %d", a, b );
  13888.    }
  13889.  
  13890.    C   FORTRAN source file, subroutine MAXPARAM
  13891.    C
  13892.    $NOTRUNCATE
  13893.  
  13894.        SUBROUTINE MAXPARAM (I, J)
  13895.        INTEGER*2 I [NEAR]
  13896.        INTEGER*2 J [NEAR]
  13897.    C
  13898.    C   I and J received by near reference,
  13899.    C   because of NEAR attribute
  13900.    C
  13901.        IF (I .GT. J) THEN
  13902.            J = I
  13903.        ELSE
  13904.            I = J
  13905.        ENDIF
  13906.        END
  13907.  
  13908.  In the previous example, the C program adopts the naming convention and
  13909.  call-ing convention of the FORTRAN subroutine. The two programs must agree
  13910.  on whether parameters are to be passed by reference or by value. The
  13911.  following keywords affect how the two programs interface:
  13912.  
  13913.  
  13914.    ■   The _fortran keyword directs C to call  maxparam  with the FORTRAN/
  13915.        Pascal naming convention (as  MAXPARAM); _fortran also directs C to
  13916.        call  maxparam  with the FORTRAN/Pascal calling convention.
  13917.  
  13918.    ■   Since the FORTRAN subroutine  MAXPARAM  may alter the value of either
  13919.        parameter, both parameters must be passed by reference. In this case,
  13920.        near reference was chosen; this method is specified in C by the use of
  13921.        near pointers, and in FORTRAN by applying the NEAR keyword to the
  13922.        parameter declarations.
  13923.  
  13924.        Far reference could have been specified by using far pointers in C. In
  13925.        that case, you would not declare the FORTRAN subroutine  MAXPARAM
  13926.        with the NEAR keyword. If you compile the FORTRAN program in medium
  13927.        model, declare  MAXPARAM  using the FAR keyword.
  13928.  
  13929.  
  13930.  
  13931.  12.6.2  Calling a FORTRAN Function from C
  13932.  
  13933.  The example below demonstrates a C main module calling the FORTRAN  function
  13934.   fact. This function returns the factorial of an integer value.
  13935.  
  13936.    /* C source file - calls FORTRAN function.
  13937.     * Compile in medium or large model.
  13938.     */
  13939.  
  13940.    int _fortran fact( int N );
  13941.  
  13942.    /* FORTRAN keyword causes C to use FORTRAN/Pascal
  13943.     * calling and naming conventions.
  13944.     * Integer parameter passed by value.
  13945.     */
  13946.  
  13947.    main()
  13948.    {
  13949.     int x = 3;
  13950.     int y = 4;
  13951.  
  13952.        printf( "The factorial of x   is %4d", fact( x ) );
  13953.        printf( "The factorial of y   is %4d", fact( y ) );
  13954.        printf( "The factorial of x+y is %4d", fact( x + y ) );
  13955.    }
  13956.  
  13957.    C   FORTRAN source file - factorial function
  13958.    C
  13959.    $NOTRUNCATE
  13960.        INTEGER*2 FUNCTION FACT (N)
  13961.        INTEGER*2 N [VALUE]
  13962.    C
  13963.    C   N is received by value, because of VALUE attribute
  13964.    C
  13965.           INTEGER*2 I
  13966.           FACT = 1
  13967.           DO 100 I = 1, N
  13968.               FACT = FACT * I
  13969.    100    CONTINUE
  13970.           RETURN
  13971.           END
  13972.  
  13973.  In the example above, the C program adopts the naming convention and calling
  13974.  convention of the FORTRAN subroutine. Both programs must agree on whether
  13975.  parameters are passed by reference or by value. Note that the C program
  13976.  passes the parameters by value rather than by reference. Passing parameters
  13977.  by value is the default for C. To accept parameters passed by value, the
  13978.  keyword VALUE is used in the declaration of  N  in the FORTRAN function. The
  13979.  _fortran keyword directs C to call  fact  with the FORTRAN/Pascal naming
  13980.  convention (as  FACT); _fortran also directs C to call  fact  with the
  13981.  FORTRAN/Pascal calling convention.
  13982.  
  13983.  When passing a parameter that should not be changed, pass the parameter by
  13984.  value. Passing by value is the default method in C and is specified in
  13985.  FORTRAN by applying the VALUE attribute to the parameter declaration.
  13986.  
  13987.  
  13988.  12.7  C Calls to Pascal
  13989.  
  13990.  This section shows two examples of C-Pascal programs. There are two types of
  13991.  subprogram calls to Pascal routines: calls to procedures and calls to
  13992.  functions. Functions return a value, while procedures do not. The examples
  13993.  in the next sections illustrate how to handle the difference between
  13994.  function and procedure calls.
  13995.  
  13996.  
  13997.  12.7.1  Calling a Pascal Procedure from C
  13998.  
  13999.  The following example demonstrates a C main module calling a Pascal
  14000.  procedure,  maxparam. This procedure adjusts the lower of two arguments to
  14001.  be equal to the higher argument.
  14002.  
  14003.    /* C source file - calls Pascal procedure.
  14004.     * Compile in medium or large model.
  14005.     */
  14006.  
  14007.    void _pascal maxparam( int _near * a, int _near * b );
  14008.  
  14009.    /* Declare as void, because there is no return value.
  14010.     * The _pascal keyword causes C to use FORTRAN/Pascal
  14011.     * calling and naming conventions.
  14012.     * Two integer params, passed by near reference.
  14013.     */
  14014.  
  14015.    main()
  14016.    {
  14017.        int a = 5;
  14018.        int b = 7;
  14019.  
  14020.        printf( "a = %d, b = %d", a, b );
  14021.        maxparam( &a, &b );
  14022.        printf( "a = %d, b = %d", a, b );
  14023.    }
  14024.  
  14025.    { Pascal source code - Maxparam procedure. }
  14026.  
  14027.    MODULE Psub;
  14028.    PROCEDURE Maxparam( VAR a:INTEGER; VAR b:INTEGER );
  14029.  
  14030.    { Two integer parameters are received by near reference. }
  14031.    { Near reference is specified with the VAR keyword. }
  14032.  
  14033.        BEGIN
  14034.            if a > b THEN
  14035.                b := a
  14036.            ELSE
  14037.                a := b
  14038.        END;
  14039.    END.
  14040.  
  14041.  In the example above, the C program adopts the Pascal naming convention and
  14042.  calling convention. Both programs must agree on whether parameters are
  14043.  passed by reference or by value; the following keywords affect the
  14044.  conventions:
  14045.  
  14046.  
  14047.    ■   The _pascal keyword directs C to call  Maxparam  with the FORTRAN/
  14048.        Pascal naming convention (as  MAXPARAM); _pascal also directs C to
  14049.        call Maxparam  with the FORTRAN/Pascal calling convention.
  14050.  
  14051.    ■   Since the procedure  Maxparam  can alter the value of either
  14052.        parameter, both parameters must be passed by reference. In this case,
  14053.        near reference is used; this method is specified in C by the use of
  14054.        near pointers, and in Pascal with the VAR keyword.
  14055.  
  14056.        Far reference could have been specified by using far pointers in C. To
  14057.        specify far reference in Pascal, use the VARS keyword instead of VAR.
  14058.  
  14059.  
  14060.  
  14061.  12.7.2  Calling a Pascal Function from C
  14062.  
  14063.  The example below demonstrates a C main module calling Pascal function
  14064.  fact. This function returns the factorial of an integer value.
  14065.  
  14066.    /* C source file - calls Pascal function.
  14067.     * Compile in medium or large model.
  14068.     */
  14069.  
  14070.    int _pascal fact(int n);
  14071.  
  14072.    /* PASCAL keyword causes C to use FORTRAN/Pascal
  14073.     * calling and naming conventions.
  14074.     * Integer parameter passed by value.
  14075.     */
  14076.  
  14077.    main()
  14078.    {
  14079.        int x = 3;
  14080.        int y = 4;
  14081.  
  14082.        printf( "The factorial of x   is %4d", fact( x ) );
  14083.        printf( "The factorial of y   is %4d", fact( y ) );
  14084.        printf( "The factorial of x+y is %4d", fact( x + y ) );
  14085.    }
  14086.  
  14087.    { Pascal source code - factorial function. }
  14088.  
  14089.    MODULE Pfun;
  14090.    FUNCTION Fact (n : INTEGER) : INTEGER;
  14091.  
  14092.    {Integer parameters received by value, the Pascal default. }
  14093.  
  14094.        BEGIN
  14095.            Fact := 1;
  14096.            WHILE n > 0 DO
  14097.                BEGIN
  14098.                    Fact := Fact * n;
  14099.                    n := n - 1;          {Parameter n modified.}
  14100.                END;
  14101.        END;
  14102.    END.
  14103.  
  14104.  In the example above, the C program adopts the Pascal naming convention and
  14105.  calling convention. Both programs must agree on whether parameters are
  14106.  passed by reference or by value. The _pascal keyword directs C to call  fact
  14107.   with the FORTRAN/Pascal naming convention (as  FACT);  _pascal also directs
  14108.  C to call fact  with the FORTRAN/Pascal calling convention.
  14109.  
  14110.  The Pascal function  fact  should receive a parameter by value. Otherwise,
  14111.  the Pascal function will corrupt the parameter's value in the calling
  14112.  module. Passing by value is the default method for both C and Pascal.
  14113.  
  14114.  
  14115.  12.8  C Calls to Assembly Language
  14116.  
  14117.  In Microsoft C, Version 6.0, you can write assembly-language programs either
  14118.  by using the in-line assembler or by creating a stand-alone module using the
  14119.  Microsoft Macro Assembler (MASM). If you use the in-line assembler, you do
  14120.  not need to take any special precautions other than those outlined in
  14121.  Chapter 3, "Using the In-Line Assembler." This section explains the
  14122.  techniques for interfacing your assembly-language routines with your C
  14123.  program.
  14124.  
  14125.  When deciding whether to use the in-line assembler or MASM, there are
  14126.  several considerations. Here is a list of advantages MASM provides over the
  14127.  in-line assembler:
  14128.  
  14129.  
  14130.    ■   MASM supports declaration of data in MASM format; in-line assembly
  14131.        does not.
  14132.  
  14133.    ■   MASM has a more powerful macro capability than in-line assembly.
  14134.  
  14135.    ■   Modules written for MASM can be interfaced more easily with modules
  14136.        written in more than one Microsoft high-level language.
  14137.  
  14138.    ■   MASM assembles large assembly-language programs more quickly than the
  14139.        in-line assembler.
  14140.  
  14141.    ■   MASM supports assembly-language code written prior to the existence of
  14142.        the in-line assembler.
  14143.  
  14144.    ■   MASM error messages and warnings are more complete than those of the
  14145.        in-line assembler.
  14146.  
  14147.  
  14148.  The in-line assembler is far more efficient for some assembly-language
  14149.  programming tasks. Here are some of the benefits of the in-line assembler:
  14150.  
  14151.  
  14152.    ■   You can do spot optimizations by including short sections of
  14153.        assemblylanguage code in your C programs with the in-line assembler.
  14154.  
  14155.    ■   Code written in in-line assembler does not necessarily incur the
  14156.        overhead of a function call; code assembled using MASM always does.
  14157.  
  14158.    ■   You can include in-line assembly code in your C source files; code
  14159.        written for MASM must be in a separate file.
  14160.  
  14161.  
  14162.  
  14163.  12.8.1  Writing the Assembly-Language Procedure
  14164.  
  14165.  You must write your assembly-language procedure so that it uses the same
  14166.  call-ing conventions and naming conventions as your C program. If you follow
  14167.  these conventions, you will be able to write recursive procedures
  14168.  (procedures that call themselves), and you will be able to use the CodeView
  14169.  debugger to locate errors in the code.
  14170.  
  14171.  ────────────────────────────────────────────────────────────────────────────
  14172.  NOTE
  14173.  
  14174.  This section discusses only the simplified segment directives provided with
  14175.  the Microsoft Macro Assembler, version 5.0. If you are using a version prior
  14176.  to 5.0, you have to specify complete SEGMENT directives.
  14177.  ────────────────────────────────────────────────────────────────────────────
  14178.  
  14179.  The standard assembly-language interface method consists of these steps:
  14180.  
  14181.  
  14182.    1.  Setting up the procedure
  14183.  
  14184.    2.  Entering the procedure
  14185.  
  14186.    3.  Allocating local data (optional)
  14187.  
  14188.    4.  Preserving register values
  14189.  
  14190.    5.  Accessing parameters
  14191.  
  14192.    6.  Returning a value (optional)
  14193.  
  14194.    7.  Exiting the procedure
  14195.  
  14196.  
  14197.  The next sections describe each of these steps in detail.
  14198.  
  14199.  
  14200.  12.8.2  Setting Up the Procedure
  14201.  
  14202.  The linker cannot combine the assembly-language procedure with the C program
  14203.  unless you define compatible segments and declare the procedure properly.
  14204.  Perform the following steps to set up the procedure:
  14205.  
  14206.  
  14207.    1.  Use the .MODEL directive at the beginning of the source file; this
  14208.        directive automatically causes the appropriate kind of returns to be
  14209.        generated (NEAR for tiny, small or compact models, FAR for medium,
  14210.        large, or huge models).
  14211.  
  14212.        If you are using a version of MASM prior to 5.0, declare the procedure
  14213.        NEAR for small or compact model, FAR for medium, large, or huge
  14214.        models.
  14215.  
  14216.    2.  Use the simplified segment directives .CODE and .DATA to declare the
  14217.        code and data segments.
  14218.  
  14219.        If you are using a version of MASM prior to 5.0, declare the segments
  14220.        using the SEGMENT, GROUP, and ASSUME directives. These directives are
  14221.        described in the Microsoft Macro Assembler Reference .
  14222.  
  14223.    3.  Use the PUBLIC directive to declare the procedure label public. This
  14224.        declaration makes the procedure visible to other modules. Also declare
  14225.        any data you want to make public as PUBLIC.
  14226.  
  14227.    4.  Use the EXTRN directive to declare any global data or procedures
  14228.        accessed by the routine as external. The safest way to use EXTRN is to
  14229.        place the directive outside any segment definition; however, place
  14230.        near data inside the data segment.
  14231.  
  14232.    5.  Observe the C naming convention; precede all procedure names and
  14233.        global data names with an underscore.
  14234.  
  14235.  
  14236.  
  14237.  12.8.3  Entering the Procedure
  14238.  
  14239.  When you enter the procedure, in most cases you will want to set up a "stack
  14240.  frame." This allows you to access parameters passed on the stack and to
  14241.  allocate local data on the stack. You do not need to set up the stack frame
  14242.  if your procedure accepts no arguments and does not use the stack.
  14243.  
  14244.  To set up the stack frame, issue the instructions:
  14245.  
  14246.    push    bp
  14247.    mov     bp,sp
  14248.  
  14249.  This sequence establishes BP as the frame pointer. You cannot use SP for
  14250.  this purpose because it is not an index or base register. Also, the value of
  14251.  SP may change as more data are pushed onto the stack. However, the value of
  14252.  the base register BP remains constant for the life of the procedure unless
  14253.  your program changes it, so each parameter can be addressed as an offset
  14254.  from BP.
  14255.  
  14256.  The instruction sequence above preserves the value of BP, since it will be
  14257.  needed in the calling procedure as soon as your assembly-language procedure
  14258.  returns. It then transfers the value in SP to BP to establish a stack frame
  14259.  on entry to the procedure.
  14260.  
  14261.  
  14262.  12.8.4  Allocating Local Data
  14263.  
  14264.  Your assembly-language procedure can use the same technique for allocating
  14265.  temporary storage for local data that is used by high-level languages. To
  14266.  set up local data space, decrease the contents of SP just after setting up
  14267.  the stack frame. (To ensure correct execution, always increase or decrease
  14268.  SP by an even number.) Decreasing SP reserves space on the stack for local
  14269.  data. You must restore the space at the end of the procedure as follows:
  14270.  
  14271.    push    bp
  14272.    mov     bp,sp
  14273.    sub     sp,space
  14274.  
  14275.  In the example above,  space  is the total size in bytes of the local data
  14276.  you want to allocate. Local variables are then accessed as fixed negative
  14277.  displacements from BP.
  14278.  
  14279.  In the following example, the entry sequence establishes a stack frame and
  14280.  allocates temporary local storage for two words (4 bytes) of data. Later in
  14281.  the example, the program accesses the local storage, initializing both to 0.
  14282.  
  14283.  
  14284.    push    bp          ; Save old stack frame.
  14285.    mov     bp,sp       ; Set up new stack frame.
  14286.    sub     sp,4        ; Allocate 4 bytes of local storage.
  14287.    .
  14288.    .
  14289.    .
  14290.    mov     WORD PTR [bp-2],0
  14291.    mov     WORD PTR [bp-4],0
  14292.  
  14293.  Note that local variables are also called dynamic, stack, or automatic
  14294.  variables.
  14295.  
  14296.  
  14297.  12.8.5  Preserving Register Values
  14298.  
  14299.  A procedure called from C should preserve the values of SI, DI, SS, and DS
  14300.  (in addition to BP, which is already saved). You should push any register
  14301.  value that your procedure modifies onto the stack after setting up the stack
  14302.  frame and allocating local storage, but prior to entering the main body of
  14303.  the procedure. Registers that your procedure does not alter need not be
  14304.  preserved.
  14305.  
  14306.  ────────────────────────────────────────────────────────────────────────────
  14307.  WARNING
  14308.  
  14309.  Routines that your assembly-language procedure calls must not alter the SI,
  14310.  DI, SS, DS, or BP registers. If they do, and you have not preserved the
  14311.  registers, they can corrupt the calling program's register variables,
  14312.  segment registers, and stack frame, causing program failure. If your
  14313.  procedure modifies the direction flag using the STD or CLD instructions, you
  14314.  must preserve the flags register.
  14315.  ────────────────────────────────────────────────────────────────────────────
  14316.  
  14317.  The example below shows an entry sequence that sets up a stack frame,
  14318.  allocates 4 bytes of local data space on the stack, then preserves the SI,
  14319.  DI, and flags registers.
  14320.  
  14321.    push    bp        ; Save caller's stack frame.
  14322.    mov     bp,sp     ; Establish new stack frame.
  14323.    sub     sp,4      ; Allocate local data space.
  14324.    push    si        ; Save SI and DI registers.
  14325.    push    di
  14326.    pushf             ; Save the flags register.
  14327.    .
  14328.    .
  14329.    .
  14330.  
  14331.  In the example above, you must exit the procedure with the following code:
  14332.  
  14333.    popf              ; Restore the flags register.
  14334.    pop    di         ; Restore the old value in the DI
  14335.                        register.
  14336.    pop    si         ; Restore the old value in the SI
  14337.                        register.
  14338.    mov    sp,bp      ; Restore the stack pointer.
  14339.    pop    bp         ; Restore the frame pointer.
  14340.    ret               ; Return to the calling routine.
  14341.  
  14342.  If you do not issue the instructions above in the order shown, you will
  14343.  place incorrect data in registers. Follow the rules below when restoring the
  14344.  calling program's registers, stack pointer, and frame pointer:
  14345.  
  14346.  
  14347.    ■   Pop all registers that you preserve in the reverse order from which
  14348.        they were pushed onto the stack. So, in the example above, SI and DI
  14349.        are pushed, and DI and SI are popped.
  14350.  
  14351.    ■   Restore the stack pointer by transferring the value of BP into SP
  14352.        before restoring the value of the frame pointer.
  14353.  
  14354.    ■   Always restore the frame pointer last.
  14355.  
  14356.  
  14357.  
  14358.  12.8.6  Accessing Parameters
  14359.  
  14360.  Once you have established the frame pointer, allocated local storage (if
  14361.  required), and pushed any registers that need to be preserved, you can write
  14362.  the main body of the procedure. Figure 12.4 shows how functions that observe
  14363.  the C calling convention use the stack frame.
  14364.  
  14365.  (This figure may be found in the printed book.)
  14366.  
  14367.  The stack frame for the assembly-language procedure shown in Figure 12.4 is
  14368.  established by the following:
  14369.  
  14370.  
  14371.    1.  The calling program pushes each of the parameters onto the stack,
  14372.        after which SP points to the last parameter pushed.
  14373.  
  14374.    2.  The calling program issues a CALL instruction, which causes the return
  14375.        address (the place in the calling program to which control will
  14376.        ultimately return) to be placed on the stack. This address can be
  14377.        either two bytes long (for near calls) or four bytes long (for far
  14378.        calls). SP now points to this address.
  14379.  
  14380.    3.  The first instruction of the called procedure saves the old value of
  14381.        BP, with the instruction  push bp. SP now points to the saved copy of
  14382.        BP.
  14383.  
  14384.    4.  BP is used to hold the current value of SP, with the instruction  mov
  14385.        bp,sp. BP therefore now points to the old value of BP (saved on the
  14386.        stack).
  14387.  
  14388.    5.  While BP remains constant throughout the procedure, SP is often
  14389.        decreased to provide room on the stack for local data or saved
  14390.        registers.
  14391.  
  14392.  
  14393.  In general, the displacement (from BP) for a parameter x is equal to the
  14394.  size of return address plus 2 plus the total size of parameters between x
  14395.  and BP.
  14396.  
  14397.  To calculate the size of parameters between x and BP, you must start with
  14398.  the rightmost parameter because C pushes parameters from right to left. For
  14399.  example, consider a FAR procedure that has one argument of type int (two
  14400.  bytes). The displacement of the parameter is
  14401.  
  14402.    Argument's displacement = size of far return address + 2
  14403.                            = 4 + 2
  14404.                            = 6
  14405.  
  14406.  The argument can thus be loaded into BP with the following instruction:
  14407.  
  14408.    mov    bx,[bp+6]
  14409.  
  14410.  Once you determine the displacement of each parameter, you can use EQU
  14411.  directives or structures to refer to the parameter with a single identifier
  14412.  name in your assembly source code. For example, you can use a more readable
  14413.  name to reference the parameter at  BP+6  if you put the following statement
  14414.  at the beginning of the assembly source file:
  14415.  
  14416.    Arg1    EQU    [bp+6]
  14417.  
  14418.  You can then refer to the first parameter in your source as  Arg1  in any
  14419.  instruction. Use of this feature is optional.
  14420.  
  14421.  For far (segmented) addresses, Microsoft C pushes the segment address before
  14422.  pushing the offset address. When pushing arguments larger than two bytes,
  14423.  high-order words are always pushed before low-order words, and parameters
  14424.  larger than two bytes are stored on the stack in most-significant,
  14425.  least-significant order.
  14426.  
  14427.  This standard for pushing segment addresses before pushing offset addresses
  14428.  facilitates the use of the assembly-language instructions LDS (load data
  14429.  segment) and LES (load extra segment).
  14430.  
  14431.  
  14432.  12.8.7  Returning a Value
  14433.  
  14434.  Your assembly-language procedure can return a value to a C calling program.
  14435.  All return values of four bytes or less are passed in registers. Far
  14436.  pointers to return values larger than four bytes are returned in the DX and
  14437.  AX registers. The DX register contains the segment address; the AX register
  14438.  contains the offset relative to the segment contained in DX.
  14439.  
  14440.  Table 12.3 shows the register conventions for returning simple data types to
  14441.  a C program.
  14442.  
  14443.  Table   12.3 Register Conventions for Simple Return Values
  14444.  
  14445. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  14446.  Data Type                         Registers
  14447.  ────────────────────────────────────────────────────────────────────────────
  14448.  char                              AL
  14449.  
  14450.  Data Type                         Registers
  14451.  ────────────────────────────────────────────────────────────────────────────
  14452. 
  14453.  int, short, _near *               AX
  14454.  
  14455.  long, _far *                      High-order portion (or segment address)
  14456.                                    in DX;
  14457.                                    low-order portion (or offset address) in
  14458.                                    AX
  14459.  
  14460.  ────────────────────────────────────────────────────────────────────────────
  14461.  
  14462.  
  14463.  
  14464.   Your procedures can return structures.
  14465.  
  14466.  To return a structure from a procedure that uses the C calling convention,
  14467.  you must copy the structure to a global variable, then return a pointer to
  14468.  that variable in the AX register (DX:AX, if you compiled in compact, large,
  14469.  or huge model).
  14470.  
  14471.  Procedures that use the FORTRAN/Pascal calling convention return structures
  14472.  similarly, with the following exceptions:
  14473.  
  14474.  
  14475.    ■   The calling program allocates space for the return value on the stack.
  14476.  
  14477.    ■   The calling program passes a pointer to the location where the return
  14478.        value is to be placed in a hidden parameter.
  14479.  
  14480.    ■   Instead of copying your structure into a global data item, you copy it
  14481.        into the location pointed to by the hidden parameter.
  14482.  
  14483.    ■   You must still return the pointer to that location in the AX register
  14484.        (or DX:AX for far data models).
  14485.  
  14486.  
  14487.   You can return floating-point values from your procedures.
  14488.  
  14489.  Procedures that use the C calling convention and return type float or type
  14490.  double must always copy their return values into the global variable fac. To
  14491.  return floating-point values from procedures declared with the
  14492.  FORTRAN/Pascal calling convention, you must return the result on the stack,
  14493.  just as you would a structure.
  14494.  
  14495.  To return a value of type long double, you must place the value on the
  14496.  NDP(80x87) stack using the FLD instruction. The C run-time math routines
  14497.  guarantee that the only value on the NDP stack is a return value; your
  14498.  routines must observe the same rule.
  14499.  
  14500.  
  14501.  12.8.8  Exiting the Procedure
  14502.  
  14503.  Before you exit your assembly-language procedure, you must perform several
  14504.  steps to restore the calling program's environment. Some of these steps are
  14505.  dependent on actions you took in allocating space for local variables and
  14506.  preserving registers.
  14507.  
  14508.  You must follow these steps (if appropriate to your procedure) in the order
  14509.  shown:
  14510.  
  14511.  
  14512.    1.  If you saved any of the registers SS, DS, SI, or DI, they must be
  14513.        popped off the stack in the reverse order from which they were saved.
  14514.        If you pop these registers in any other order, your program will
  14515.        behave incorrectly.
  14516.  
  14517.    2.  If you allocated local data space at the beginning of the procedure,
  14518.        you must restore SP with the instruction  mov s  p  ,bp.
  14519.  
  14520.    3.  Restore BP with the instruction  pop bp. This step is always
  14521.        necessary.
  14522.  
  14523.    4.  Return to the calling program by issuing the ret instruction.
  14524.  
  14525.  
  14526.  The following example shows the simplest possible entry and exit sequence.
  14527.  In the entry sequence, no registers are saved and no local data space is
  14528.  allocated.
  14529.  
  14530.    push   bp
  14531.    mov    bp,sp   ; Set up the new stack frame.
  14532.    .
  14533.    .
  14534.    .
  14535.    pop    bp      ; Restore the caller's stack frame.
  14536.    ret
  14537.  
  14538.  The following example shows an entry and exit sequence for a procedure that
  14539.  saves SI and DI and allocates local data space on the stack.
  14540.  
  14541.    push   bp
  14542.    mov    bp,sp   ; Establish local stack frame.
  14543.    sub    sp,4    ; Allocate space for local data.
  14544.    push   si      ; Preserve the SI and DI registers.
  14545.    push   di
  14546.    .
  14547.    .
  14548.    .
  14549.    pop    di      ; Pop saved registers.
  14550.    pop    si
  14551.    mov    sp,bp   ; Free local data space.
  14552.    pop    bp      ; Restore old stack frame.
  14553.    ret
  14554.  
  14555.  
  14556.  12.9  Handling Data in Mixed-Language Programming
  14557.  
  14558.  This section contains detailed information about naming and calling
  14559.  conventions in a mixed-language program. It also describes how various
  14560.  languages represent strings, numerical data, arrays, and logical data.
  14561.  
  14562.  
  14563.  12.9.1  Default Naming and Calling Conventions
  14564.  
  14565.  Each language has its own default naming and calling conventions (Table
  14566.  12.4).
  14567.  
  14568.  Table 12.4  Default Naming and Calling Conventions
  14569.  
  14570. ╓┌─────────┌──────────────────┌──────────────────┌───────────────────────────╖
  14571.            Calling            Naming             Parameter
  14572.  Language  Convention         Convention         Passing
  14573.  ────────────────────────────────────────────────────────────────────────────
  14574.  BASIC     FORTRAN/Pascal     Case insensitive   Near reference
  14575.  
  14576.            Calling            Naming             Parameter
  14577.  Language  Convention         Convention         Passing
  14578.  ────────────────────────────────────────────────────────────────────────────
  14579. 
  14580.  C         C                  Case sensitive     Value (scalar variables),
  14581.                                                  reference (arrays and
  14582.                                                  pointers)
  14583.  
  14584.  FORTRAN   FORTRAN/Pascal     Case insensitive   Reference
  14585.  
  14586.  Pascal    FORTRAN/Pascal     Case insensitive   Value
  14587.  
  14588.  ────────────────────────────────────────────────────────────────────────────
  14589.  
  14590.  
  14591.  
  14592.  
  14593.  BASIC Conventions
  14594.  
  14595.  When you call BASIC routines from C, you must pass all arguments by near
  14596.  reference (near pointer). You can modify the conventions observed by BASIC
  14597.  routines that interface with C functions by using the DECLARE, BYVAL, SEG,
  14598.  and CALLS keywords. For more information on these keywords, see the
  14599.  Microsoft BASIC Language Reference or the Microsoft BASIC Programmer's
  14600.  Guide.
  14601.  
  14602.  
  14603.  FORTRAN Conventions
  14604.  
  14605.  You can modify the conventions observed by FORTRAN routines that call C
  14606.  functions by using the INTERFACE, VALUE, PASCAL, and C keywords. For more
  14607.  information about the use of these keywords, see the Microsoft FORTRAN
  14608.  Reference.
  14609.  
  14610.  
  14611.  Pascal Conventions
  14612.  
  14613.  You can modify the conventions observed by Pascal routines that interface
  14614.  with C functions by using the VAR, CONST, ADR, VARS, CONSTS, ADRS, and C
  14615.  keywords. For more information about the use of these keywords, see the
  14616.  Microsoft Pascal Compiler User's Guide.
  14617.  
  14618.  
  14619.  12.9.2  Numeric Data Representation
  14620.  
  14621.  Table 12.5 shows how to declare numeric variables of similar type in
  14622.  different languages.
  14623.  
  14624.  Table 12.5  Equivalent Numeric Data Types
  14625.  
  14626. ╓┌─────────────┌───────────────────┌──────────────────────┌──────────────────╖
  14627.  BASIC         C                   FORTRAN                Pascal
  14628.  ────────────────────────────────────────────────────────────────────────────
  14629.  x%            short               INTEGER*2              INTEGER2
  14630.  
  14631.  INTEGER       int                 ---                    INTEGER
  14632.                                                           (default)
  14633.  
  14634.  ---           unsigned short(1)   ---                    WORD
  14635.  
  14636.  ---           unsigned            ---                    ---
  14637.  
  14638.  x&            long                INTEGER*4              INTEGER4
  14639.  BASIC         C                   FORTRAN                Pascal
  14640.  ────────────────────────────────────────────────────────────────────────────
  14641. x&            long                INTEGER*4              INTEGER4
  14642.  
  14643.  LONG          ---                 INTEGER (default)      ---
  14644.  
  14645.  ---           unsigned long(1)    ---                    ---
  14646.  
  14647.  x!            float               REAL*4                 REAL4
  14648.  
  14649.  x (default)   ---                 REAL                   REAL (default)
  14650.  
  14651.  SINGLE        ---                 ---                    ---
  14652.  
  14653.  x#            double              REAL*8                 REAL8
  14654.  
  14655.  DOUBLE        ---                 DOUBLE                 ---
  14656.                                    PRECISION
  14657.  
  14658.  ---           long double         REAL*16                REAL16
  14659.  
  14660.  BASIC         C                   FORTRAN                Pascal
  14661.  ────────────────────────────────────────────────────────────────────────────
  14662. 
  14663.  ---           unsigned char       CHARACTER*1(2)         CHAR
  14664.  
  14665.  ────────────────────────────────────────────────────────────────────────────
  14666.  
  14667.  
  14668.  
  14669.  (1)  Types unsigned short and unsigned long are not supported by BASIC or
  14670.  FORTRAN. Type  unsigned long is not supported by Pascal. A signed integral
  14671.  type can be substituted, but the maximum range will be less.
  14672.  (2)  The FORTRAN type CHARACTER*1 is not the same as LOGICAL.
  14673.  
  14674.  
  14675.  The FORTRAN types COMPLEX*8 and COMPLEX*16 are not implemented in C but can
  14676.  be represented with structures.
  14677.  
  14678.  The FORTRAN types LOGICAL*2 and LOGICAL*4 are not implemented in C.
  14679.  LOGICAL*2 is stored as a one-byte Boolean indicator followed by an unused
  14680.  byte; LOGICAL*4 is stored as a one-byte Boolean indicator followed by three
  14681.  unused bytes.
  14682.  
  14683.  
  14684.  12.9.3  Strings
  14685.  
  14686.  Each language implements strings differently. This section describes the
  14687.  ways that strings are implemented in Microsoft languages.
  14688.  
  14689.  
  14690.  C String Format
  14691.  
  14692.  C stores strings as arrays of bytes and uses a null character (  '\0'  ) as
  14693.  an end-of-string delimiter. For example, consider the following string:
  14694.  
  14695.    char c_string[] = "C text string";
  14696.  
  14697.  This string is represented in memory as follows:
  14698.  
  14699.  (This figure may be found in the printed book.)
  14700.  
  14701.  Because  c_string  is an array like any other, C passes it by reference in
  14702.  function calls.
  14703.  
  14704.  
  14705.  BASIC String Format
  14706.  
  14707.  BASIC stores strings as four-byte descriptors pointing to the actual string
  14708.  data. The format of the descriptor is as follows:
  14709.  
  14710.  (This figure may be found in the printed book.)
  14711.  
  14712.  The first field of the string descriptor contains an integer indicating the
  14713.  length (in bytes) of the string. The second field contains the address of
  14714.  the string in the default data segment.
  14715.  
  14716.  Do not attempt to alter the length of BASIC strings, because they are
  14717.  managed by BASIC string-space management routines. You cannot count on a
  14718.  particular string remaining at a given offset during the execution of a
  14719.  BASIC program because the BASIC string-space management routines allocate
  14720.  strings to different areas of memory depending on program requirements.
  14721.  
  14722.  The format of the string at DS:Address is a simple array of characters. The
  14723.  string is exactly the length indicated in the descriptor.
  14724.  
  14725.   To pass a BASIC string to C, append a null character.
  14726.  
  14727.  Because C needs the null character to delimit the end of the string, you
  14728.  should append  chr$( 0 )  to your BASIC string before passing it to your C
  14729.  function. For example,
  14730.  
  14731.    A$ = "I am a BASIC string"
  14732.    A$ = A$ + chr$( 0 )
  14733.  
  14734.    CALL CFunc( SADD(A$) )
  14735.  
  14736.  Note that the BASIC call is made by near reference using the SADD keyword.
  14737.  
  14738.   Use a string descriptor to pass a C string to BASIC.
  14739.  
  14740.  To pass a C string to BASIC, create a structure for the string descriptor.
  14741.  For example,
  14742.  
  14743.    char c_string[] = "C String Data";
  14744.  
  14745.    struct tagBASICStringDes
  14746.    {
  14747.        char *   sd_addr;
  14748.        int      sd_len;
  14749.    } str_des;
  14750.  
  14751.    str_des.sd_addr = c_string;
  14752.    str_des.sd_len = strlen( c_string );
  14753.  
  14754.    BASICFunction( &str_des );
  14755.  
  14756.  
  14757.  FORTRAN String Format
  14758.  
  14759.  FORTRAN stores strings as a series of bytes at a fixed location in memory.
  14760.  There is no delimiter at the end of the string. Consider the string declared
  14761.  as follows:
  14762.  
  14763.    STR = 'FORTRAN STRING'
  14764.  
  14765.  The string is stored in memory as follows:
  14766.  
  14767.  (This figure may be found in the printed book.)
  14768.  
  14769.  FORTRAN passes strings by reference, as it does all other data.
  14770.  
  14771.  ────────────────────────────────────────────────────────────────────────────
  14772.  NOTE
  14773.  
  14774.  FORTRAN's variable length strings cannot be used in mixed-language
  14775.  programming because the temporary variable used to communicate string length
  14776.  is not accessible to other languages.
  14777.  ────────────────────────────────────────────────────────────────────────────
  14778.  
  14779.  To pass a C string to FORTRAN (or Pascal), pass the variable by reference as
  14780.  you normally would. In your FORTRAN or Pascal routine, you must specify the
  14781.  length of the string; strings that are passed as arguments from one language
  14782.  to another must be of fixed length.
  14783.  
  14784.  
  14785.  Pascal String Format
  14786.  
  14787.  Pascal represents strings as fixed-length arrays of CHAR or as strings with
  14788.  a length byte followed by the string data.
  14789.  
  14790.   To pass a fixed-length string to C, append a null character.
  14791.  
  14792.  To pass a fixed-length string to a C function, use the concatenation
  14793.  operator (*) to append a null character. Then pass the string to the C
  14794.  function by reference (by declaring the string as CONST, CONSTS, VAR, or
  14795.  VARS). For example,
  14796.  
  14797.    PROGRAM PasStr( input, output );
  14798.    type
  14799.        stype15 = string(15);  { fixed-length }
  14800.    var
  14801.        str : stype15;
  14802.  
  14803.    PROCEDURE PasStrToC( VAR s1 : stype15 ) [C]; EXTERN;
  14804.  
  14805.    BEGIN
  14806.        str := 'Pass this to C' * chr( 0 );
  14807.        PasStrToC( str );
  14808.    END.
  14809.  
  14810.  A more flexible way to pass Pascal strings to C functions is to declare them
  14811.  as type ADRMEM or ADSMEM, then pass the address of the string. For example,
  14812.  
  14813.  
  14814.    PROCEDURE PasStrToC( s1adr : ADRMEM ) [C]; EXTERN;
  14815.  
  14816.  Then you can call the C function with this code:
  14817.  
  14818.    PasStrToC( ADR str );
  14819.  
  14820.  Using this method, you can pass strings of different lengths to C functions.
  14821.  
  14822.  
  14823.  ────────────────────────────────────────────────────────────────────────────
  14824.  NOTE
  14825.  
  14826.  The Pascal type LSTRING is not compatible with C; you can pass a string
  14827.  declared as LSTRING by first assigning it to another variable of type
  14828.  STRING, then passing that variable.
  14829.  ────────────────────────────────────────────────────────────────────────────
  14830.  
  14831.  Whenever you pass a variable of type STRING or type LSTRING by value, Pascal
  14832.  pushes the whole string onto the stack and passes the length of the string
  14833.  as another parameter. C cannot access strings passed in this manner.
  14834.  
  14835.   Before passing a string from C to Pascal, make sure enough space is
  14836.  allocated.
  14837.  
  14838.  Passing a string from a C function to a Pascal function or procedure is
  14839.  identical to passing a string from a C function to a FORTRAN routine. The
  14840.  only provision you must make is to specify the length of the string to your
  14841.  Pascal function.
  14842.  
  14843.  
  14844.  12.9.4  Arrays
  14845.  
  14846.  When you use an array in a program written in a single language, the method
  14847.  for array handling is consistent. When you mix languages, you need to be
  14848.  aware of the differences between array-handling techniques in various
  14849.  languages.
  14850.  
  14851.  Unlike most Microsoft languages, BASIC keeps an array descriptor, which is
  14852.  similar to the BASIC string descriptor discussed in Section 12.9.3,
  14853.  "Strings." This array descriptor is necessary because BASIC handles memory
  14854.  allocation for arrays dynamically (at run time). Dynamic allocation requires
  14855.  BASIC to shift arrays in memory.
  14856.  
  14857.   To pass a BASIC array to a  C function, use the VARPTR and VARSEG keywords.
  14858.  
  14859.  
  14860.  The VARPTR and VARSEG keywords obtain the address of the first element of
  14861.  the array and its segment, respectively. The example below shows how to call
  14862.  a C function with a near reference and a far reference to an array:
  14863.  
  14864.    DIM ARRAY%( 20 )
  14865.    DECLARE CNearArray CDECL( BYVAL Addr AS INTEGER )
  14866.    DECLARE CFarArray CDECL( BYVAL Addr AS INTEGER, BYVAL Seg AS INTEGER )
  14867.    .
  14868.    .
  14869.    .
  14870.    CALL CNearArray( VARPTR( ARRAY%(0) ) )
  14871.    CALL CFarArray( VARPTR( ARRAY%(0) ), VARSEG( ARRAY%(0) ) )
  14872.  
  14873.  The C functions receiving  ARRAY  can be declared as follows:
  14874.  
  14875.    _cdecl CNearArray( int * array );
  14876.    _cdecl CFarArray( int far * array );
  14877.  
  14878.  The routine that receives the array must not make a call back to BASIC. If
  14879.  it does, the location of the array data could change, and the address that
  14880.  was passed to the routine would become meaningless.
  14881.  
  14882.  If you only need to pass one member of the array from BASIC to your C
  14883.  function, you can pass it by value as follows:
  14884.  
  14885.    CALL CFunc( ARRAY%(8) )
  14886.  
  14887.  
  14888.  12.9.5  Array Declaration and Indexing
  14889.  
  14890.  Each language varies in the way that arrays are declared and indexed. Array
  14891.  indexing is a source-level consideration and involves no transformation of
  14892.  data. There are two differences in the way elements are indexed by each
  14893.  language:
  14894.  
  14895.  
  14896.    1.  The value of the lower array bound is different among Microsoft
  14897.        languages.
  14898.  
  14899.        By default, FORTRAN indexes the first element of an array as 1. BASIC
  14900.        and C index it as 0. Pascal lets you begin indexing at any integer
  14901.        value. Recent versions of BASIC and FORTRAN also give you the option
  14902.        of specifying lower bounds at any integer value.
  14903.  
  14904.    2.  Some languages vary subscripts in row-major order; others vary
  14905.        subscripts in column-major order.
  14906.  
  14907.        This issue only affects arrays with more than one dimension. With
  14908.        row-major order (used by C and Pascal), the rightmost dimension
  14909.        changes first. With column-major order (used by FORTRAN, and BASIC by
  14910.        default), the leftmost dimension changes first. Thus, in C, the first
  14911.        four elements of an array declared as  X[3][3]  are
  14912.  
  14913.        X[0][0]    X[0][1]    X[0][2]    X[1][0]
  14914.  
  14915.        In FORTRAN, the four elements are
  14916.  
  14917.        X(1,1)     X(2,1)     X(3,1)     X(1,2)
  14918.  
  14919.        The C and FORTRAN arrays shown above illustrate the difference between
  14920.        row-major and column-major order as well as the difference in the
  14921.        assumed lower bound between C and FORTRAN. Table 12.6 shows
  14922.        equivalences for array declarations in each language. In this table, r
  14923.        is the number of elements of the row dimension (which changes most
  14924.        slowly), and c is the number of elements of the column dimension
  14925.        (which changes most quickly).
  14926.  Table 12.6  Equivalent Array Declarations
  14927.  
  14928. ╓┌─────────┌────────────────────────────────┌────────────────────────────────╖
  14929.  Language  Array Declaration                Notes
  14930.  ────────────────────────────────────────────────────────────────────────────
  14931.  BASIC     DIM x(r-1, c-1)                  With default lower bounds of 0
  14932.  
  14933.  Language  Array Declaration                Notes
  14934.  ────────────────────────────────────────────────────────────────────────────
  14935. 
  14936.  C         type x[r][c]                     When passed by reference
  14937.            struct { type x[r][c]; } x       When passed by value
  14938.  
  14939.  FORTRAN   type x(c, r)                     With default lower bounds of 1
  14940.  
  14941.  Pascal    x : ARRAY [a..a+r-1, b..b+c-1]
  14942.            OF type
  14943.  
  14944.  ────────────────────────────────────────────────────────────────────────────
  14945.  
  14946.  
  14947.  
  14948.  
  14949.  
  14950.  The order of indexing extends to any number of dimensions you declare. For
  14951.  example, the C declaration
  14952.  
  14953.    int arr1[2][10][15][20];
  14954.  
  14955.  is equivalent to the FORTRAN declaration
  14956.  
  14957.    INTEGER*2 ARR1( 20, 15, 10, 2 )
  14958.  
  14959.  The constants used in a C array declaration represent dimensions, not upper
  14960.  bounds as they do in other languages. Therefore, the last element in the C
  14961.  array declared as  int arr[5][5]  is  arr[4][4], not  arr[5][5].
  14962.  
  14963.  
  14964.  12.9.6  Structures, Records, and User-Defined Types
  14965.  
  14966.  The C struct type, the BASIC user-defined type, the FORTRAN record (defined
  14967.  with the STRUCTURE keyword), and the Pascal record type are equivalent.
  14968.  Therefore, these data types can be passed between C, FORTRAN, Pascal, and
  14969.  BASIC.
  14970.  
  14971.  These types can be affected by the storage method. By default, C, FORTRAN,
  14972.  and Pascal use word alignment for types shorter than one word (type char and
  14973.  unsigned char). This storage method specifies that occasional bytes can be
  14974.  inserted as padding so that word and double-word objects start on an even
  14975.  boundary. (In addition, all nested structures and records start on a word
  14976.  boundary.)
  14977.  
  14978.  If you are passing a structure or record across a mixed-language interface,
  14979.  your calling routine and called routine must agree on the storage method and
  14980.  parameter-passing convention. Otherwise, data will not be interpreted
  14981.  correctly.
  14982.  
  14983.  Because Pascal, FORTRAN, and C use the same storage method for structures
  14984.  and records, you can interchange data between routines without taking any
  14985.  special precautions unless you modify the storage method. Make sure the
  14986.  storage methods agree before interchanging data between C, FORTRAN, and
  14987.  Pascal.
  14988.  
  14989.  BASIC packs user-defined types, so your C function must also pack structures
  14990.  (using the /Zp command-line option or the pack pragma) to agree.
  14991.  
  14992.  You can pass structures as parameters by value or by reference. Both the
  14993.  calling program and the called program must agree on the parameter-passing
  14994.  convention. See Section 12.2.3, "Parameter-Passing Requirement," for more
  14995.  information about the language you are using.
  14996.  
  14997.  
  14998.  12.9.7  External Data
  14999.  
  15000.  External data refers to data that is both static and public; that is, the
  15001.  data is stored in a set place in memory as opposed to being allocated on the
  15002.  stack, and the data is visible to other modules.
  15003.  
  15004.  External data can be defined in C, Pascal, and assembly language. Note that
  15005.  a data definition is distinct from an external declaration. A data
  15006.  definition causes a compiler to create a data object; an external
  15007.  declaration informs a compiler that the object is to be found in another
  15008.  module. FORTRAN can only define external data in COMMON blocks. (See Section
  15009.  12.9.9, "Common Blocks," for more information about sharing external data
  15010.  with FORTRAN programs.)
  15011.  
  15012.  There are three requirements for programs that share external data between
  15013.  languages:
  15014.  
  15015.  
  15016.    1.  One of the modules must define the data.
  15017.  
  15018.        You can define a static data object in a C module by defining a data
  15019.        object outside all functions. (If you use the static keyword in C,
  15020.        however, the data object will not be made public.)
  15021.  
  15022.    2.  The other modules that will access the data must declare the data as
  15023.        external.
  15024.  
  15025.        In C, you can declare data as external by using an extern declaration,
  15026.        similar to the extern declaration for functions. In FORTRAN and
  15027.        Pascal, you can declare data as external by adding the EXTERN
  15028.        attribute to the data declaration.
  15029.  
  15030.    3.  Resolve naming-convention differences.
  15031.  
  15032.        In C, you can adopt the FORTRAN/Pascal naming convention by applying
  15033.        _fortran or _pascal to the data declaration. In FORTRAN and Pascal,
  15034.        you can adopt the C naming convention by applying the C attribute to
  15035.        the data declaration.
  15036.  
  15037.  
  15038.  
  15039.  12.9.8  Pointers and Address Variables
  15040.  
  15041.  Rather than passing data directly, you may want to pass the address of a
  15042.  piece of data. Passing the address amounts to passing the data by reference.
  15043.  In some cases, such as in BASIC arrays, there is no other way to pass a data
  15044.  item as a parameter.
  15045.  
  15046.  C programs always pass array variables by address. All other types are
  15047.  passed by value unless you use the address-of (&) operator to obtain the
  15048.  address.
  15049.  
  15050.  The Pascal ADR and ADS types are equivalent to near and far pointers,
  15051.  respectively, in C. You can pass ADR and ADS variables as ADRMEM or ADSMEM.
  15052.  BASIC and FORTRAN do not have formal address types. However, they do provide
  15053.  ways for storing and passing addresses.
  15054.  
  15055.  BASIC programs can access a variable's segment address with the VARSEG
  15056.  function and its offset address with the VARPTR function. The values
  15057.  returned by these intrinsic functions should then be passed or stored as
  15058.  ordinary integer variables. If you pass them to another language, pass by
  15059.  value. Otherwise you will be attempting to pass the address of the address,
  15060.  rather than the address itself.
  15061.  
  15062.  To pass a near address, pass only the offset; if you need to pass a far
  15063.  address, you may have to pass the segment and the offset separately. Pass
  15064.  the segment address first, unless CDECL is in effect.
  15065.  
  15066.  FORTRAN programs can determine near and far addresses with the LOC and
  15067.  LOCFAR functions. Store the result of the LOC function as INTEGER*2 and the
  15068.  result of the LOCFAR function as INTEGER*4.
  15069.  
  15070.  As with BASIC, if you pass the result of LOC or LOCFAR to another language,
  15071.  be sure to pass by value.
  15072.  
  15073.  
  15074.  12.9.9  Common Blocks
  15075.  
  15076.  You can pass individual members of a FORTRAN or BASIC common block in an
  15077.  argument list, just as you can any data item. However, you can also give a
  15078.  different language module access to the entire common block at once.
  15079.  
  15080.  C modules can reference the items of a common block by first declaring a
  15081.  structure with fields that correspond to the common-block variables. Having
  15082.  defined a structure with the appropriate fields, the C module must then
  15083.  connect with the common block itself. The next two sections present methods
  15084.  for gaining access to common blocks.
  15085.  
  15086.  
  15087.  Passing the Address of a Common Block
  15088.  
  15089.  To pass the address of a common block, simply pass the address of the first
  15090.  variable in the block. (In other words, pass the first variable by
  15091.  reference.) The receiving C module should expect to receive a structure by
  15092.  reference.
  15093.  
  15094.  In the example below, the C function  initcb  receives the address of the
  15095.  variable  N, which it considers to be a pointer to a structure with three
  15096.  fields:
  15097.  
  15098.    C      FORTRAN SOURCE CODE
  15099.    C
  15100.           COMMON /CBLOCK/N, X, Y
  15101.           INTEGER*2 N
  15102.           REAL*8    X, Y
  15103.    .
  15104.    .
  15105.    .
  15106.           CALL INITCB( N )
  15107.  
  15108.  
  15109.    /* C source code */
  15110.  
  15111.  
  15112.    /* Explicitly set structure packing to word-alignment */
  15113.    #pragma pack( 2 );
  15114.  
  15115.    struct block_type
  15116.    {
  15117.        int     n;
  15118.        double  x;
  15119.        double  y;
  15120.    };
  15121.  
  15122.    initcb( struct block_type * block_hed )
  15123.    {
  15124.        block_hed-n = 1;
  15125.        block_hed-x = 10.0;
  15126.        block_hed-y = 20.0;
  15127.    }
  15128.  
  15129.  
  15130.  Accessing Common Blocks Directly
  15131.  
  15132.  You can access FORTRAN common blocks directly by defining a structure with
  15133.  the appropriate fields and then using the methods described in Section
  15134.  12.9.7, "External Data." Here is an example of accessing common blocks
  15135.  directly:
  15136.  
  15137.    struct block_type
  15138.    {
  15139.        int    n;
  15140.        double x;
  15141.        double y;
  15142.    };
  15143.  
  15144.    extern struct block_type fortran cblock;
  15145.  
  15146.   You cannot access common blocks directly using BASIC common blocks.
  15147.  
  15148.  Note that the technique of accessing common blocks directly works with
  15149.  FORTRAN common blocks, but not with BASIC common blocks. If your C module
  15150.  must work with both FORTRAN and BASIC common blocks, pass the address of the
  15151.  common block as a parameter to the function.
  15152.  
  15153.  
  15154.  12.9.10  Using a Varying Number of Parameters
  15155.  
  15156.  Some C functions (for example printf) accept a variable number of
  15157.  parameters. To call such a function from another language, you need to
  15158.  suppress the type-checking that normally forces a call to be made with a
  15159.  fixed number of parameters. In BASIC, you can remove this type-checking by
  15160.  omitting a parameter list from the DECLARE statement. In FORTRAN or Pascal,
  15161.  you can call routines with a variable number of parameters by including the
  15162.  VARYING attribute in your interface to the routine, along with the C
  15163.  attribute. You must use the C attribute because a variable number of
  15164.  parameters is feasible only with the C calling convention.
  15165.  
  15166.  
  15167.  
  15168.  
  15169.  
  15170.  
  15171.  Chapter 13  Writing Portable Programs
  15172.  ────────────────────────────────────────────────────────────────────────────
  15173.  
  15174.  Because C compilers exist on a variety of computers, some C applications
  15175.  developed for one computer system can be ported to other systems. However,
  15176.  some aspects of language behavior depend on how a particular C compiler is
  15177.  implemented and how a specific computer operates. Therefore, when designing
  15178.  a program to be ported to another system, it is important that you examine
  15179.  programming assumptions.
  15180.  
  15181.  This chapter describes programming assumptions that can affect writing
  15182.  portable programs.
  15183.  
  15184.  The American National Standards Institute Standard for the C Language (the
  15185.  ANSI Standard) details every instance where language behavior is defined by
  15186.  the implementation. Appendix C summarizes implementation-defined behavior
  15187.  for Microsoft C.
  15188.  
  15189.  
  15190.  13.1  Assumptions about Hardware
  15191.  
  15192.  To make C programs portable, you must examine two aspects of your code:
  15193.  hardware assumptions and compiler dependency. This section deals with
  15194.  hardware assumptions. Section 13.2, "Assumptions about the Compiler," deals
  15195.  with compiler dependency.
  15196.  
  15197.  
  15198.  13.1.1  Size of Basic Types
  15199.  
  15200.  In C, the size of basic types (char, signed int, unsigned int, float,
  15201.  double, and long double) is implementation-defined, so relying on a
  15202.  particular data type to be a given size reduces the portability of a
  15203.  program.
  15204.  
  15205.   Don't make assumptions about the size of data types.
  15206.  
  15207.  Because the size of basic types is left to the implementation, do not make
  15208.  assumptions about the size or alignment of data types within aggregate
  15209.  types. Use only the sizeof operator to determine the size or amount of
  15210.  storage required for a variable or a type.
  15211.  
  15212.  Following are some rules governing the size of data types.
  15213.  
  15214.  
  15215.  Type char
  15216.  
  15217.  Type char is the smallest of the basic types, but it must be large enough to
  15218.  hold any of the characters in the implementation's basic character set.
  15219.  Normally, variables of type char are one byte.
  15220.  
  15221.  
  15222.  Type int and Type short int
  15223.  
  15224.  Type int and type short int often correspond to the register size of the
  15225.  target machine. Both int and short are greater than or equal to the size of
  15226.  type char but less than or equal to the size of type long.
  15227.  
  15228.  If you assume that type int is a certain size, your code may not be portable
  15229.  because
  15230.  
  15231.  
  15232.    ■   An int can be defined as a 16-bit (two-byte) or a 32-bit quantity.
  15233.  
  15234.    ■   An int is not always large enough to hold array indexes. For large
  15235.        arrays, you must use unsigned int; for extremely large arrays, use
  15236.        long. To be certain your code is portable, define your array indexes
  15237.        as type size_t. You may not know, before porting your code, the
  15238.        maximum value to expect an array index of type int to hold. The file
  15239.        LIMITS.H contains manifest constants, listed below, for the maximum
  15240.        and minimum values of each basic integral type.
  15241.  
  15242.  
  15243.  Constant                          Value
  15244.  ────────────────────────────────────────────────────────────────────────────
  15245.  CHAR_BIT                          Number of bits in a variable of type
  15246.                                    char
  15247.  
  15248.  CHAR_MIN                          Minimum value a variable of type char
  15249.                                    can hold
  15250.  
  15251.  CHAR_MAX                          Maximum value a variable of type char
  15252.                                    can hold
  15253.  
  15254.  SCHAR_MIN                         Minimum value a variable of type signed
  15255.                                    char
  15256.                                    can hold
  15257.  
  15258.  SCHAR_MAX                         Maximum value a variable of type signed
  15259.                                    char
  15260.                                    can hold
  15261.  
  15262.  UCHAR_MAX                         Maximum value a variable of type
  15263.                                    unsigned char can hold
  15264.  
  15265.  SHRT_MIN                          Minimum value a variable of type short
  15266.                                    can hold
  15267.  
  15268.  SHRT_MAX                          Maximum value a variable of type short
  15269.                                    can hold
  15270.  
  15271.  USHRT_MAX                         Maximum value a variable of type
  15272.                                    unsigned short can hold
  15273.  
  15274.  INT_MIN                           Minimum value a variable of type int can
  15275.                                    hold
  15276.  
  15277.  INT_MAX                           Maximum value a variable of type int can
  15278.                                    hold
  15279.  
  15280.  UINT_MAX                          Maximum value a variable of type
  15281.                                    unsigned int
  15282.                                    can hold
  15283.  
  15284.  LONG_MIN                          Minimum value a variable of type long
  15285.                                    can hold
  15286.  
  15287.  LONG_MAX                          Maximum value a variable of type long
  15288.                                    can hold
  15289.  
  15290.  ULONG_MAX                         Maximum value a variable of type
  15291.                                    unsigned long can hold
  15292.  
  15293.  
  15294.  Type float, Type double, and Type long double
  15295.  
  15296.  Type float is the smallest of the basic floating-point types. Type double is
  15297.  usually larger than type float, and type long double is usually the largest
  15298.  of the floating-point types. You can make only these portability assumptions
  15299.  about floating-point types:
  15300.  
  15301.  
  15302.    ■   Any value that can be represented as type float can be represented as
  15303.        type double (type float is a subset of type double).
  15304.  
  15305.    ■   Any value that can be represented as type double can be represented as
  15306.        type long double (type double is a subset of type long double).
  15307.  
  15308.  
  15309.  The file FLOAT.H contains manifest constants, listed below, for the maximum
  15310.  and minimum values of each basic floating-point type.
  15311.  
  15312.  Constant                          Value
  15313.  ────────────────────────────────────────────────────────────────────────────
  15314.  DBL_DIG                           Number of decimal digits of precision a
  15315.                                    variable of type double can hold
  15316.  
  15317.  DBL_MAX                           Maximum value a variable of type double
  15318.                                    can hold
  15319.  
  15320.  DBL_MAX_10_EXP                    Maximum value (base 10) the exponent of
  15321.                                    a variable of type double can hold
  15322.  
  15323.  DBL_MAX_EXP                       Maximum value (base 2) the exponent of a
  15324.                                    variable of type double can hold
  15325.  
  15326.  DBL_MIN                           Minimum positive value a variable of
  15327.                                    type double can hold
  15328.  
  15329.  DBL_MIN_10_EXP                    Minimum value (base 10) the exponent of
  15330.                                    a variable of type double can hold
  15331.  
  15332.  DBL_MIN_EXP                       Minimum value (base 2) the exponent of a
  15333.                                    variable of type double can hold
  15334.  
  15335.  FLT_DIG                           Number of decimal digits of precision a
  15336.                                    variable of type float can hold
  15337.  
  15338.  FLT_MAX                           Maximum value a variable of type float
  15339.                                    can hold
  15340.  
  15341.  FLT_MAX_10_EXP                    Maximum value (base 10) the exponent of
  15342.                                    a variable of type float can hold
  15343.  
  15344.  FLT_MAX_EXP                       Maximum value (base 2) the exponent of a
  15345.                                    variable of type float can hold
  15346.  
  15347.  FLT_MIN                           Minimum positive value a variable of
  15348.                                    type float can hold
  15349.  
  15350.  FLT_MIN_10_EXP                    Minimum value (base 10) the exponent of
  15351.                                    a variable of type float can hold
  15352.  
  15353.  FLT_MIN_EXP                       Minimum value (base 2) the exponent of a
  15354.                                    variable of type float can hold
  15355.  
  15356.  LDBL_DIG                          Number of decimal digits of precision a
  15357.                                    variable of type long double can hold
  15358.  
  15359.  LDBL_MAX                          Maximum value a variable of type long
  15360.                                    double can hold
  15361.  
  15362.  LDBL_MAX_10_EXP                   Maximum value (base 10) the exponent of
  15363.                                    a variable of type long double can hold
  15364.  
  15365.  LDBL_MAX_EXP                      Maximum value (base 2) the exponent of a
  15366.                                    variable of type long double can hold
  15367.  
  15368.  LDBL_MIN                          Minimum positive value a variable of
  15369.                                    type long double can hold
  15370.  
  15371.  LDBL_MIN_10_EXP                   Minimum value (base 10) the exponent of
  15372.                                    a variable of type long double can hold
  15373.  
  15374.  LDBL_MIN_EXP                      Minimum value (base 2) the exponent of a
  15375.                                    variable of type long double can hold
  15376.  
  15377.  
  15378.  Microsoft C Type Sizes
  15379.  
  15380.  Table 13.1 summarizes the size of the basic types in Microsoft C.
  15381.  
  15382.  Table 13.1  Size of Basic Types in Microsoft C
  15383.  
  15384. ╓┌─────────────────────────────────────┌─────────────────────────────────────╖
  15385.                                        Number
  15386.  Type                                  of Bytes
  15387.  ────────────────────────────────────────────────────────────────────────────
  15388.  char, unsigned char                   1
  15389.  
  15390.  int, short, unsigned int,             2
  15391.  unsigned short
  15392.  
  15393.  near pointer                          2
  15394.  
  15395.                                        Number
  15396.  Type                                  of Bytes
  15397.  ────────────────────────────────────────────────────────────────────────────
  15398. 
  15399.  long, unsigned long                   4
  15400.  
  15401.  far pointer                           4
  15402.  
  15403.  float                                 4
  15404.  
  15405.  double                                8
  15406.  
  15407.  long double                           10
  15408.  
  15409.  ────────────────────────────────────────────────────────────────────────────
  15410.  
  15411.  
  15412.  
  15413.  
  15414.  13.1.2  Storage Order and Alignment
  15415.  
  15416.  The C language does not define any specific layout for the storage of data
  15417.  items relative to one another. The layout for storage of structure elements,
  15418.  or unions within a structure or union, is defined by the implementation.
  15419.  
  15420.  Some processors require that data longer than one byte be word-aligned
  15421.  (aligned to an even-byte address). Other processors, such as the 80x86
  15422.  family, do not have such a restriction.
  15423.  
  15424.  
  15425.  Structure Order and Alignment
  15426.  
  15427.  The example below illustrates how alignment can affect your program. In the
  15428.  example, a structure is cast to type long because the programmer knew the
  15429.  order in which a particular implementation stored data.
  15430.  
  15431.    /* Nonportable code */
  15432.    struct time
  15433.    {
  15434.        char hour;     /* 0 < hour < 24   -- fits in a char */
  15435.        char minute;   /* 0 < minute < 60 -- fits in a char */
  15436.        char second;   /* 0 < second < 60 -- fits in a char */
  15437.    };
  15438.  
  15439.        .
  15440.        .
  15441.        .
  15442.        struct time now, alarm_time;
  15443.        .
  15444.        .
  15445.        .
  15446.        if ( (long)now >= (long)alarm_time )
  15447.        {
  15448.            /* sound an alarm */
  15449.        }
  15450.  
  15451.  The preceding code makes these nonportable assumptions:
  15452.  
  15453.  
  15454.    ■   The data for  hour  will be stored in a higher order position than
  15455.        minute  or  second. Because C does not guarantee storage order or
  15456.        alignment of structures or unions, the code may not be portable to
  15457.        other machines.
  15458.  
  15459.    ■   Three variables of type char will be shorter than or the same length
  15460.        as a variable of type long. Thus, the code is not portable according
  15461.        to the rules governing the size of basic types, as described in
  15462.        Section 13.1.1.
  15463.  
  15464.  
  15465.  If either of these assumptions proves false, the comparison (if statement)
  15466.  is invalid.
  15467.  
  15468.   You can write code that makes no assumptions about storage order.
  15469.  
  15470.  To make the program in the preceding example portable, you can break the
  15471.  comparison between the two long integers into a component-by-component
  15472.  comparison. This technique is illustrated in the following example:
  15473.  
  15474.    /* Portable code */
  15475.    struct time
  15476.    {
  15477.        char hour;     /* 0 < hour < 24   -- fits in a char */
  15478.        char minute;   /* 0 < minute < 60 -- fits in a char */
  15479.        char second;   /* 0 < second < 60 -- fits in a char */
  15480.    };
  15481.  
  15482.        .
  15483.        .
  15484.        .
  15485.        struct time now, alarm_time;
  15486.        .
  15487.        .
  15488.        .
  15489.        if ( time_cmp( now, alarm_time ) >= 0 )
  15490.        {
  15491.            /* sound an alarm */
  15492.        }
  15493.        .
  15494.        .
  15495.        .
  15496.  
  15497.    int time_cmp( struct time t1, struct time t2 )
  15498.    {
  15499.        if( t1.hour != t2.hour )
  15500.            return( t2.hour - t1.hour );
  15501.        if( t1.minute != t2.minute )
  15502.            return( t2.minute - t1.minute );
  15503.        return( t2.second - t1.second );
  15504.    }
  15505.  
  15506.  
  15507.  Union Order and Alignment
  15508.  
  15509.  Programmers use unions most often for two purposes: to store data whose
  15510.  exact type is not known until run time or to access the same data in
  15511.  different ways.
  15512.  
  15513.  Unions falling into the second category are usually not portable. For
  15514.  example, the union below is not portable:
  15515.  
  15516.    union tag_u
  15517.    {
  15518.     char bytes_in_long[4];
  15519.     long a_long;
  15520.    };
  15521.  
  15522.  The intent of the union above is to access the individual bytes of a
  15523.  variable of type long. However, the union may not work as intended when
  15524.  ported to other computers because
  15525.  
  15526.  
  15527.    ■   It relies on a constant size for type long.
  15528.  
  15529.    ■   It may assume byte ordering within a variable of type long. (Byte
  15530.        ordering is described in detail in Section 13.1.3, "Byte Order in a
  15531.        Word.")
  15532.  
  15533.  
  15534.  The first problem can be addressed by coding the union as follows:
  15535.  
  15536.    union tag_u
  15537.    {
  15538.     char bytes_in_long[sizeof( long ) / sizeof( char )];
  15539.     long a_long;
  15540.    };
  15541.  
  15542.  Note the use of the sizeof operator to determine the size of a data type.
  15543.  
  15544.  
  15545.  13.1.3  Byte Order in a Word
  15546.  
  15547.  The order of bytes within a word (int or short) or a double word (long) can
  15548.  vary among machines. Code that assumes an internal order is not portable, as
  15549.  shown by this example:
  15550.  
  15551.    /*
  15552.     * Nonportable structure to access an
  15553.     * int in bytes.
  15554.     */
  15555.    struct tag_int_bytes
  15556.    {
  15557.        char lobyte;
  15558.        char hibyte;
  15559.    };
  15560.  
  15561.  A more portable way to access the individual bytes in a word is to define
  15562.  two macros that rely on the constant CHAR_BIT, defined in LIMITS.H:
  15563.  
  15564.    #define LOBYTE(a) (char)((a) & 0xff)
  15565.    #define HIBYTE(a) (char)((unsigned)(a) >> CHAR_BIT)
  15566.  
  15567.  The LOBYTE macro is still not completely portable. It assumes that a char is
  15568.  eight bits long, and it uses the constant  0xff  to mask the high-order
  15569.  eight bits. Because portable programs cannot rely on a given number of bits
  15570.  in a byte, consider the revision below:
  15571.  
  15572.    #define LOBYTE(a) (char)((a) & ((unsigned)~0>>CHAR_BIT))
  15573.    #define HIBYTE(a) (char)((unsigned)(a) >> CHAR_BIT)
  15574.  
  15575.  The new LOBYTE macro performs a bitwise complement on 0; that is, all zero
  15576.  bits are turned into ones. It then takes that unsigned quantity and shifts
  15577.  it right far enough to create a mask of the correct length for the
  15578.  implementation.
  15579.  
  15580.  The following code assumes that the order of bytes in a word will be
  15581.  leastsignificant first:
  15582.  
  15583.    int c;
  15584.        .
  15585.        .
  15586.        .
  15587.        fread( &c, sizeof( char ), 1, fp );
  15588.  
  15589.  The code attempts to read one byte as an int, without converting it from a
  15590.  char. However, the code will fail in any implementation where the low-order
  15591.  byte is not the first byte of an int. The following solution is more
  15592.  portable. In the example below, the data is read into an intermediate
  15593.  variable of type char before being assigned to the integer variable.
  15594.  
  15595.    int c;
  15596.        char ch;
  15597.        .
  15598.        .
  15599.        .
  15600.        fread( &ch, sizeof( char ), 1, fp );
  15601.        c = ch;
  15602.  
  15603.  The example below shows how to use the C run-time function fgetc to return
  15604.  the value. The fgetc function returns type char, but the value is promoted
  15605.  to type int when it is assigned to a variable of type int.
  15606.  
  15607.    int c;
  15608.        .
  15609.        .
  15610.        .
  15611.        c = fgetc( fp );
  15612.  
  15613.  
  15614.  Microsoft C Specific
  15615.  
  15616.  Microsoft C normally aligns data types longer than one byte to an even-byte
  15617.  address for improved performance. See the /Zp compiler option and the pack
  15618.  pragma in the Microsoft C Reference and in on-line help for information
  15619.  about controlling structure packing in Microsoft C.
  15620.  
  15621.  
  15622.  13.1.4  Reading and Writing Structures
  15623.  
  15624.  Many C programs read data from disk into structures and write data to disk
  15625.  from structures. The functions that perform disk I/O in C require you to
  15626.  specify the number of bytes to be transferred. You should always use the
  15627.  sizeof operator to obtain the size of the data to be read or written because
  15628.  differing data type sizes or alignment schemes may alter the size of a given
  15629.  structure. For example,
  15630.  
  15631.    fread( &my_struct, sizeof(my_struct), 1, fp );
  15632.  
  15633.  
  15634.  Microsoft C Specific
  15635.  
  15636.  When performing disk input and output in Microsoft C, structures may be
  15637.  different sizes depending on the structure-packing option you have selected
  15638.  (see the /Zp compiler option and the pack pragma in the Microsoft C
  15639.  Reference).
  15640.  
  15641.  
  15642.  13.1.5  Bit Fields in Structures
  15643.  
  15644.  The Microsoft C compiler implements bit fields. However, many C compilers
  15645.  do not.
  15646.  
  15647.  Bit fields allow you to access the individual bits within a data item. While
  15648.  the practice of accessing the bits in a data item is inherently nonportable,
  15649.  you can
  15650.  
  15651.  improve your chances of porting a program that uses bit fields if you make
  15652.  no assumptions about order of assignment, or size and alignment of bit
  15653.  fields.
  15654.  
  15655.  
  15656.  Order of Assignment
  15657.  
  15658.  The order of assignment of bit fields in memory is left to the
  15659.  implementation, so you cannot rely on a particular entry in a bit field
  15660.  structure to be in a higher order position than another. (This problem is
  15661.  similar to the portability constraint imposed by alignment of basic data
  15662.  types in structures. The C language does not define any specific layout for
  15663.  the storage of data items relative to one another.) See Section 13.1.2,
  15664.  "Storage Order and Alignment" for more information.
  15665.  
  15666.  
  15667.  Size and Alignment of Bit Fields
  15668.  
  15669.  The Microsoft C compiler supports bit fields up to the size of the type
  15670.  long. Each individual member of the bit field structure can be up to the
  15671.  size of the declared type. Some compilers do not support bit field-structure
  15672.  elements that are longer than type int.
  15673.  
  15674.  The example below defines a bit field,  short_bitfield, that is shorter than
  15675.  type int:
  15676.  
  15677.    struct short_bitfield
  15678.    {
  15679.        unsigned usr_bkup : 1; /* 0  <= usr_bkup <  1 */
  15680.        unsigned usr_sec  : 4; /* 9 <= usr_sec < 16 */
  15681.    };
  15682.  
  15683.  The example below defines a bit field,  long_bitfield, that has elements
  15684.  longer than type int:
  15685.  
  15686.    struct long_bitfield
  15687.    {
  15688.        unsigned long disk_pos : 22;  /* 0 <= disk_pos   < 4,194,304 */
  15689.        unsigned long rec_no   : 10;  /* 0 <= rec_no < 1,024 */
  15690.    };
  15691.  
  15692.  The bit field  short_bitfield  is likely to be supported by more
  15693.  implementations than  long_bitfield.
  15694.  
  15695.  
  15696.  Microsoft C Specific
  15697.  
  15698.  The example below introduces another portability issue: alignment of data
  15699.  defined in bit fields. The Microsoft C compiler does not allow an element in
  15700.  a structure to extend across two words. The first two elements,  day  and
  15701.  month, take up nine bits. The third,  year, would extend across a word
  15702.  boundary, so it must begin on the next word boundary.
  15703.  
  15704.    struct long_bitfield
  15705.    {
  15706.        unsigned int day    : 5;  /* 0 <= day   < 32 */
  15707.        unsigned int month  : 4;  /* 0 <= month < 16 */
  15708.        unsigned int year   : 11; /* 0 <= year  < 2048 */
  15709.    };
  15710.  
  15711.  Figure 13.1 illustrates the example above.
  15712.  
  15713.  (This figure may be found in the printed book.)
  15714.  
  15715.  Other compilers may not use the same storage techniques.
  15716.  
  15717.  
  15718.  13.1.6  Processor Arithmetic Mode
  15719.  
  15720.  Two types of arithmetic are common on digital computers: one's-complement
  15721.  arithmetic and two's-complement arithmetic. Some programs assume that all
  15722.  target computers perform two's-complement arithmetic. If you take advantage
  15723.  of the fact that a given operation causes a particular bit pattern to be set
  15724.  on either a one's-complement or two's-complement computer, your program will
  15725.  not be portable. For example, two's-complement machines represent the
  15726.  eight-bit integer value -1 as a binary 11111111. A one's-complement machine
  15727.  represents the same decimal value (-1) as 11111110. Some programmers assume
  15728.  that -1 will fill a byte or a word with ones, and use it to construct a mask
  15729.  template that they later shift. This will not work correctly on
  15730.  one's-complement machines, but the error will not surface until the
  15731.  least-significant bit is used.
  15732.  
  15733.  In two's-complement arithmetic, there is only one value that represents
  15734.  zero. In one's-complement arithmetic, there is a value for zero and a value
  15735.  for negative zero. Use the C relational operators to handle this anomaly
  15736.  correctly; if you write code that deliberately circumvents the C relational
  15737.  operators, tests for zero or NULL may not operate correctly.
  15738.  
  15739.  
  15740.  Microsoft C Specific
  15741.  
  15742.  Microsoft C produces code only for the Intel 80x86 processors, which all
  15743.  perform two's-complement arithmetic.
  15744.  
  15745.  
  15746.  13.1.7  Pointers
  15747.  
  15748.  One of the most powerful but potentially dangerous features of the C
  15749.  language is its use of indirect addressing through pointers. Bugs introduced
  15750.  by misusing pointers can be difficult to detect and isolate because the
  15751.  error often corrupts memory unpredictably.
  15752.  
  15753.  
  15754.  Casting Pointers
  15755.  
  15756.  Be sure you do not make nonportable assumptions when casting pointers to
  15757.  different types.
  15758.  
  15759.    /* Nonportable coercion */
  15760.    char c[4];
  15761.    long *lp;
  15762.  
  15763.    lp = (long *)c;
  15764.    *lp = 0x12345678L;
  15765.  
  15766.  This code is nonportable because using a cast to change an array of char to
  15767.  a pointer of type long assumes a particular byte-ordering scheme. This is
  15768.  discussed in greater detail in Section 13.1.3, "Byte Order in a Word."
  15769.  
  15770.  
  15771.  Pointer Size
  15772.  
  15773.  A pointer can be assigned (or cast) to any integer type large enough to hold
  15774.  it, but the size of the integer type depends on the machine and the
  15775.  implementation. (In fact, it can even depend on the memory model.)
  15776.  Therefore, you cannot assume:
  15777.  
  15778.    sizeof( char * ) == sizeof( int )
  15779.  
  15780.  To determine the size of any unmodified data pointer, use
  15781.  
  15782.    sizeof( void * )
  15783.  
  15784.  the size of a generic data pointer.
  15785.  
  15786.  
  15787.  Pointer Subtraction
  15788.  
  15789.  Code that assumes that pointer subtraction yields an int value is
  15790.  nonportable. Pointer subtraction yields a result of type ptrdiff_t (defined
  15791.  in STDDEF.H). Portable code must always use variables of type ptrdiff_t for
  15792.  storing the result of pointer subtraction.
  15793.  
  15794.  
  15795.  The Null Pointer
  15796.  
  15797.  In most implementations, NULL is defined as 0. In Microsoft C, it is defined
  15798.  as  ((void *)0). Because code pointers and data pointers are often different
  15799.  sizes, using 0 for the null pointer for both can lead to nonportability. The
  15800.  difference in size between code pointers and data pointers will cause
  15801.  problems for functions that expect pointer arguments longer than an int. To
  15802.  avoid these problems, use the null pointer, as defined in the include file
  15803.  STDDEF.H; use prototypes; or explicitly cast NULL to the correct data type.
  15804.  Here is a portable way to use the null pointer:
  15805.  
  15806.    /* Portable use of the NULL pointer */
  15807.    main()
  15808.    {
  15809.        func1( (char *)NULL );
  15810.        func2( (void *(*)())NULL );
  15811.    }
  15812.  
  15813.    void func1( char * c )
  15814.    {
  15815.    }
  15816.  
  15817.    void func2( void *(* func)() )
  15818.    {
  15819.    }
  15820.  
  15821.  The invocations of  func1  and  func2  explicitly cast NULL to the correct
  15822.  size. In the case of  func1, NULL is cast to type char *; in the case of
  15823.  func2, it is cast to a pointer to a function that returns type void.
  15824.  
  15825.  
  15826.  Microsoft C Specific
  15827.  
  15828.  Subtraction of pointers to huge arrays that have more than 32,767 elements
  15829.  may yield a long result. The _huge keyword is implementation-defined by
  15830.  Microsoft C and is not portable. Here is how to subtract pointers to huge
  15831.  arrays:
  15832.  
  15833.    char _huge *a;
  15834.    char _huge *b;
  15835.    long        d;
  15836.    .
  15837.    .
  15838.    .
  15839.    d = (long)( a - b );
  15840.  
  15841.  In Microsoft C, the memory model selected and the special keywords _near,
  15842.  _far, and _huge can change the size of a pointer. The Microsoft memory
  15843.  models and extended keywords are nonportable, but you should be aware of
  15844.  their effects.
  15845.  
  15846.  Sizes of generic pointers and default pointer sizes are shown in Tables 13.2
  15847.  and 13.3, respectively.
  15848.  
  15849.  Table 13.2  Size of Generic Pointers
  15850.  
  15851. ╓┌──────────────┌──────────────────────┌─────────────────────────────────────╖
  15852.  Declaration    Name                   Size
  15853.  ────────────────────────────────────────────────────────────────────────────
  15854.  void _near *   Generic near pointer   16 bits
  15855.  void _far *    Generic far pointer    32 bits
  15856.  void _huge *   Generic huge pointer   32 bits
  15857.  Declaration    Name                   Size
  15858.  ────────────────────────────────────────────────────────────────────────────
  15859. void _huge *   Generic huge pointer   32 bits
  15860.  ────────────────────────────────────────────────────────────────────────────
  15861.  
  15862.  
  15863.  Table 13.3  Default Pointer Sizes
  15864.  
  15865. ╓┌─────────────┌──────────────────┌──────────────────────────────────────────╖
  15866.  Memory Model  Code Pointer Size  Data Pointer Size
  15867.  ────────────────────────────────────────────────────────────────────────────
  15868.  Tiny          16 bits            16 bits
  15869.  Small         16 bits            16 bits
  15870.  Medium        32 bits            16 bits
  15871.  Compact       16 bits            32 bits
  15872.  Large         32 bits            32 bits
  15873.  Huge          32 bits            32 bits
  15874.  ────────────────────────────────────────────────────────────────────────────
  15875.  
  15876.  
  15877.  
  15878.  13.1.8  Address Space
  15879.  
  15880.  The amount of available memory and the address space on systems varies,
  15881.  depending on many factors outside your control. A program designed with
  15882.  portability in mind should handle insufficient-memory situations. To ensure
  15883.  that your program handles these situations, you should always check the
  15884.  error return from any of the dynamic memory allocation routines, such as
  15885.  malloc, calloc, strdup, and realloc.
  15886.  
  15887.  These situations occur not only because of a lack of installed memory but
  15888.  also because too many other applications are using memory. For example,
  15889.  
  15890.  
  15891.    ■   Installed resident software can cause your program to fail. In DOS,
  15892.        these programs are usually device drivers or
  15893.        terminate-and-stay-resident (TSR) utilities.
  15894.  
  15895.    ■   An event or combination of events in a multitasking operating system
  15896.        such as OS/2 or XENIX can cause your program to fail. These failures
  15897.        are complex and difficult to predict. Here is an example: the user has
  15898.        installed a daemon to "pop up" every so often and check the system
  15899.        status. The user is running your application along with enough other
  15900.        large applications to cause a critical shortage of memory. When the
  15901.        daemon pops up, your program may fail on a memory allocation request.
  15902.  
  15903.    ■   An application running under Windows can use an extraordinary amount
  15904.        of the global heap and not return it to the free pool. This type of
  15905.        behavior will cause Windows to deny a GlobalAlloc request.
  15906.  
  15907.  
  15908.  
  15909.  13.1.9  Character Set
  15910.  
  15911.  The C language does not define the character set used in an implementation.
  15912.  This means that any programs that assume the character set to be ASCII are
  15913.  nonportable.
  15914.  
  15915.  The only restrictions on the character set are these:
  15916.  
  15917.  
  15918.    ■   No character in the implementation's character set may be larger than
  15919.        the size of type char.
  15920.  
  15921.    ■   Each character in the set must be represented as a positive value by
  15922.        type char, whether it is treated as signed or unsigned. So, in the
  15923.        case of the ASCII character set and an eight-bit char, the maximum
  15924.        value is 127 (128 is a negative number when stored in a char
  15925.        variable).
  15926.  
  15927.  
  15928.  
  15929.  Character Classification
  15930.  
  15931.  The standard C run-time support contains a complete set of
  15932.  characterclassification macros and functions. These functions are defined in
  15933.  the CTYPE.H file and are guaranteed to be portable:
  15934.  
  15935.  isalnum         isdigit         isprint         isupper
  15936.  isalpha         isgraph         ispunct         isxdigit
  15937.  iscntrl         islower         isspace
  15938.  
  15939.  The following code fragment is not portable to implementations that do not
  15940.  use the ASCII character set:
  15941.  
  15942.    /* Nonportable */
  15943.    if( c >= 'A' && c <= 'Z' )
  15944.        /* uppercase alphabetic */
  15945.  
  15946.  Instead, consider using this:
  15947.  
  15948.    /* Portable */
  15949.    if( isalpha(c) && isupper(c) )
  15950.        /* uppercase alphabetic */
  15951.  
  15952.  The first example above is nonportable, because it assumes that uppercase  A
  15953.   is represented by a smaller value than uppercase  Z, and that no lowercase
  15954.  characters fall between the values of  A  and  Z. The second example is
  15955.  portable, because it uses the character classification functions to perform
  15956.  the tests.
  15957.  
  15958.  In a portable program, you should not perform any comparison on variables of
  15959.  type char except strict equality (==). You cannot assume the character set
  15960.  follows an increasing sequence─that may not be true on a different machine.
  15961.  
  15962.  
  15963.  
  15964.  Case Translation
  15965.  
  15966.  Translation of characters from upper- to lowercase or from lowerto uppercase
  15967.  is called "case translation." The following example shows a coding technique
  15968.  for case translation not portable to implementations using a non-ASCII
  15969.  character set.
  15970.  
  15971.    #define make_upper(c) ((c)&0xcf)
  15972.    #define make_lower(c) ((c)|0x20)
  15973.  
  15974.  This code takes advantage of the fact that you can map uppercase to
  15975.  lowercase simply by changing the state of bit 6. It is extremely efficient
  15976.  but nonportable. To write portable code, use the case-translation macros
  15977.  toupper and tolower (defined in CTYPE.H).
  15978.  
  15979.  
  15980.  13.2  Assumptions about the Compiler
  15981.  
  15982.  Different compilers translate C source code into object code in different
  15983.  ways. The ANSI draft standard for the C programming language defines how
  15984.  many of these translations must be done; others are implementation-defined.
  15985.  
  15986.  
  15987.  This section describes assumptions about how the compiler translates your C
  15988.  code, which can make your programs nonportable. For a complete description
  15989.  of how Microsoft C handles implementation-defined operations, see Appendix
  15990.  C, "Implementation-Defined Behavior."
  15991.  
  15992.  
  15993.  13.2.1  Sign Extension
  15994.  
  15995.  "Sign extension" is the propagation of the sign bit to fill unoccupied space
  15996.  when promoting to a more-significant type or when performing bitwise
  15997.  right-shift operations.
  15998.  
  15999.  
  16000.  Promotion from Shorter Types
  16001.  
  16002.  Integral promotions from shorter types occur when you make an assignment,
  16003.  perform arithmetic, perform a comparison, or perform an explicit cast.
  16004.  
  16005.  The behavior of integral promotion is well defined, except for type char.
  16006.  The implementation defines whether type char is treated as signed or
  16007.  unsigned. The code fragment below is an example of promotion as a result of
  16008.  assignment:
  16009.  
  16010.    char c1 = -3;
  16011.    int  i1;
  16012.  
  16013.    i1 = c1;
  16014.  
  16015.  In this example, the expected result of the assignment statement is that  i1
  16016.   will be set to -3. If the implementation defines type char as unsigned,
  16017.  however, sign extension will not occur, and  i1  will be 253 (on a
  16018.  two's-complement machine).
  16019.  
  16020.  Promotion can also occur as a result of a comparison of different types:
  16021.  
  16022.    char c;
  16023.  
  16024.    if( c == 0x80 )
  16025.        .
  16026.        .
  16027.        .
  16028.  
  16029.  This comparison will never evaluate as true on an implementation that
  16030.  signextends char types but treats hexadecimal constants as unsigned. Use a
  16031.  character constant of the form '\x80', or explicitly cast the constant to
  16032.  type char to perform the comparison correctly.
  16033.  
  16034.  The following comparison, which is an example of promotion as a result of a
  16035.  cast, is also nonportable:
  16036.  
  16037.    char c;
  16038.    unsigned int u;
  16039.  
  16040.    if( u == (unsigned)c )
  16041.  
  16042.  There are two problems with this code:
  16043.  
  16044.  
  16045.    ■   The char type may be treated as signed or unsigned, depending on the
  16046.        implementation.
  16047.  
  16048.    ■   If the char type is treated as signed, it can be converted to unsigned
  16049.        in two different ways: the char value may first be sign-extended to
  16050.        int, then converted to unsigned; or the char may be converted to
  16051.        unsigned char, then sign-extended to int length.
  16052.  
  16053.  
  16054.  It is always safe to compare a signed int with a char constant because C
  16055.  requires all character constants to be positive.
  16056.  
  16057.  Variables of type char are promoted to type int when passed as arguments to
  16058.  a function. This will cause sign extension on some machines. Consider the
  16059.  following code:
  16060.  
  16061.    char c = 128;
  16062.  
  16063.    printf( "%d\n", c );
  16064.  
  16065.  
  16066.  Microsoft C Specific
  16067.  
  16068.  Microsoft C allows you to treat type char as signed or unsigned. By default,
  16069.  a char is considered signed, but if you change the default char type using
  16070.  the /J compiler option, you can treat it as unsigned.
  16071.  
  16072.  
  16073.  Bitwise Right-Shift Operations
  16074.  
  16075.  Positive or unsigned integral types (char, short, int, and long) yield
  16076.  positive or zero values after a right bitwise shift (>>) operation. For
  16077.  example,
  16078.  
  16079.    (char)120 >> 4
  16080.  
  16081.  yields 7,
  16082.  
  16083.    (unsigned char)240 >> 8
  16084.  
  16085.  yields 0,
  16086.  
  16087.    (int)500 >> 8
  16088.  
  16089.  yields 1, and
  16090.  
  16091.    (unsigned int)65535 >> 4
  16092.  
  16093.  yields 4,095.
  16094.  
  16095.  Negative-signed integral types yield implementation-defined values after a
  16096.  bitwise right-shift operation. This means that you must know whether you
  16097.  want to do a signed or unsigned shift, then code accordingly.
  16098.  
  16099.  If you don't know how the implementation performs, you may get unexpected
  16100.  results. For example,  (signed char)0x80 >> 3  yields 0xf0 if the
  16101.  imple-mentation performs sign extension on right bitwise shifts. If the
  16102.  implementation does not perform the sign extension, the result is 0x10.
  16103.  
  16104.  You can use right shifts to speed up division when the divisor can be
  16105.  represented by powers of 2 and the dividend is positive. To maintain
  16106.  portability, you should use the division operator.
  16107.  
  16108.  To perform an unsigned shift, explicitly cast the data to an unsigned type.
  16109.  To perform a shift that extends the sign bit, use the division operator as
  16110.  follows: divide by 2n, where n is the number of bits you want to shift.
  16111.  
  16112.  
  16113.  13.2.2  Length and Case of Identifiers
  16114.  
  16115.  Some implementations do not support long identifiers. Some allow only 6
  16116.  characters, while others allow as many as 32. They may report each
  16117.  identifier that exceeds the maximum length or truncate identifiers to a
  16118.  given length. Truncation causes serious problems, especially if you have a
  16119.  number of similarly named variables within the scope of a block of code,
  16120.  such as the following:
  16121.  
  16122.    double acct_receivable_30_days;
  16123.    double acct_receivable_60_days;
  16124.    double acct_receivable_90_days;
  16125.    double current_interest_rate;
  16126.  
  16127.    acct_receivable_30_days *= current_interest_rate;
  16128.  
  16129.  If your target system retains only six significant characters, you will have
  16130.  to rename all your  acct_receivable  variables.
  16131.  
  16132.  Case sensitivity also affects portability. C is usually a case-sensitive
  16133.  language. That is,  CalculateInterest  is not considered the same identifier
  16134.  as calculateinterest. Some systems are not case sensitive, however, so to
  16135.  write portable code, differentiate your identifiers by something other than
  16136.  case.
  16137.  
  16138.  These problems with identifiers can occur in two locations: the compiler and
  16139.  the linker or loader. Even if the compiler can handle long and
  16140.  case-differentiated identifiers, if the linker or loader cannot, you can get
  16141.  duplicate definitions or other unexpected errors.
  16142.  
  16143.  
  16144.  Microsoft C Specific
  16145.  
  16146.  The Microsoft C compiler issues the /NOIGNORECASE command to the Microsoft
  16147.  Segmented-Executable Linker (LINK), specifically instructing it to consider
  16148.  the case of identifiers.
  16149.  
  16150.  
  16151.  13.2.3  Register Variables
  16152.  
  16153.  The number and type of register variables in a function depend on the
  16154.  implementation. You can declare more variables as register than the number
  16155.  of physical registers the implementation uses. In such a case, the compiler
  16156.  treats the excess register variables as automatic.
  16157.  
  16158.  Since the types that qualify for register class differ among
  16159.  implementations, invalid register declarations are treated as automatic.
  16160.  
  16161.  If you declare variables as register to optimize performance, declare them
  16162.  in decreasing order of importance to ensure that the compiler allocates a
  16163.  register to the most important variables.
  16164.  
  16165.  
  16166.  Microsoft C Specific
  16167.  
  16168.  The compiler ignores register declarations if you select the global register
  16169.  allocation optimization. You can select global register allocation as
  16170.  follows:
  16171.  
  16172.  Environment                       Selection
  16173.  ────────────────────────────────────────────────────────────────────────────
  16174.  CL command line                   Specify either the /Oe or /Ox option.
  16175.  
  16176.  PWB                               Select the Global Register Allocation
  16177.                                    option in the Debug Build Options or
  16178.                                    Release Build Options dialog boxes.
  16179.  
  16180.  pragma                            Use the optimize pragma with the e
  16181.                                    parameter.
  16182.  
  16183.  
  16184.  13.2.4  Functions with a Variable Number of Arguments
  16185.  
  16186.  Functions that accept a variable number of arguments are not portable.
  16187.  Although both the ANSI Standard and The C Programming Language specify how
  16188.  to write these functions and how they behave, differences still exist among
  16189.  compiler implementors about how to use variable argument lists.
  16190.  
  16191.  Many UNIX(R) systems support a standard that differs from the ANSI Standard
  16192.  for variable arguments. Although this may change, it currently presents a
  16193.  portability concern.
  16194.  
  16195.  Microsoft C run-time libraries and macros allow you to use whichever version
  16196.  of variable argument support you expect to be most portable for your
  16197.  application.
  16198.  
  16199.  
  16200.  13.2.5  Evaluation Order
  16201.  
  16202.  The C language does not guarantee the evaluation order of most expressions.
  16203.  Avoid writing constructs that depend on evaluation within an expression to
  16204.  proceed in a particular manner. For example,
  16205.  
  16206.    i = 0;
  16207.    func( i++, i++ );
  16208.    .
  16209.    .
  16210.    .
  16211.    func( int a, int b )
  16212.    {
  16213.  
  16214.  A compiler could evaluate this code fragment and pass 0 as  a  and 1 as  b.
  16215.  It could also pass 1 as  a  and 0 as  b  and conform equally with the
  16216.  standards.
  16217.  
  16218.  The C language does guarantee that an expression will be completely
  16219.  evaluated at any given "sequence point." A sequence point is a point in the
  16220.  syntax of the language at which all side effects of an expression or series
  16221.  of expressions have been completed.
  16222.  
  16223.  These are the sequence points in the C language:
  16224.  
  16225.  
  16226.    1.  The semicolon (;) statement separator
  16227.  
  16228.    2.  The call to a function after the arguments have been evaluated
  16229.  
  16230.    3.  The end of the first operand of one of the following:
  16231.  
  16232.        ■   Logical AND (&&)
  16233.  
  16234.        ■   Logical OR (||)
  16235.  
  16236.        ■   Conditional (?)
  16237.  
  16238.        ■   Comma separator (,) when used to separate statements or in
  16239.            expressions; the comma separator is not a sequence point when it
  16240.            is used between variables in declaration statements or between
  16241.            parameters in a function  invocation
  16242.  
  16243.  
  16244.    4.  The end of a full expression, such as
  16245.  
  16246.        ■   An initializer
  16247.  
  16248.        ■   The expression in an expression statement (for example, any
  16249.            expression inside parentheses)
  16250.  
  16251.        ■   The controlling expression of a while or do statement
  16252.  
  16253.        ■   Any of the three expressions of a for statement
  16254.  
  16255.        ■   The expression in a return statement
  16256.  
  16257.  
  16258.  
  16259.  
  16260.  13.2.6  Function and Macro Arguments with Side Effects
  16261.  
  16262.  Run-time support functions can be implemented either as functions or as
  16263.  macros. Avoid including expressions with side effects inside function
  16264.  invocations unless you are sure the function will not be implemented as a
  16265.  macro. Here is an illustration of how an argument with side effects can
  16266.  cause problems:
  16267.  
  16268.    #define limit_number(a) ((a>1000)?1000:(a))
  16269.  
  16270.    a = limit_number( a++ );
  16271.  
  16272.  If  a ≤ 1000, it is incremented once. If  a > 1000, it is incremented twice,
  16273.  which is probably not the intended behavior.
  16274.  
  16275.  A macro can be used safely with an argument that has side effects if it
  16276.  evaluates its parameter only once. You can determine whether a macro is safe
  16277.  only by inspecting the code.
  16278.  
  16279.  A common example of a run-time support function that is often implemented as
  16280.  a macro is toupper. You will find your program's behavior confusing if you
  16281.  use the following code:
  16282.  
  16283.    char c;
  16284.  
  16285.    c = toupper( getc() );
  16286.  
  16287.  If  toupper  is implemented as a function,  getc  will be called only once,
  16288.  and its return value will be translated to uppercase. However, if  toupper
  16289.  is implemented as a macro,  getc  will be called once or twice, depending on
  16290.  whether  c is upper- or lowercase. Consider the following macro example:
  16291.  
  16292.    #define toupper(c) ( (islower(c)) ? _toupper(c) : (c) )
  16293.  
  16294.  If you include the toupper macro in your code, the preprocessor expands it
  16295.  as follows:
  16296.  
  16297.    /* What you wrote */
  16298.    c = toupper( getc() );
  16299.  
  16300.    /* Macro expansion */
  16301.    ch = (islower( (getc()) ) ? _toupper( getc() ) : (getc()) );
  16302.  
  16303.  The expansion of the macro shows that the argument to  toupper  will always
  16304.  be called twice: once to determine if the character is lowercase and the
  16305.  next time to perform case translation (if necessary). In the example, this
  16306.  double evaluation calls the getc function twice. Because  getc  is a
  16307.  function whose side effect is to read a character from the standard input
  16308.  device, the example requests two characters from standard input.
  16309.  
  16310.  
  16311.  13.2.7  Environment Differences
  16312.  
  16313.  Many programs perform some file I/O. When writing these programs for
  16314.  portability, consider the following:
  16315.  
  16316.  
  16317.    ■   Do not hard-code file or path names. Use constants you define either
  16318.        in a header file or at the beginning of the program.
  16319.  
  16320.    ■   Do not assume the use of any particular file system. For example, the
  16321.        UNIX-model, hierarchical file system is prevalent on small computers.
  16322.        On larger systems, the file system often follows a different model.
  16323.  
  16324.    ■   Do not assume a particular display size (number of rows and columns).
  16325.  
  16326.    ■   Do not assume that display attributes exist. Some environments do not
  16327.        support such attributes as color, underlined text, blinking text,
  16328.        highlighted text, inverse text, protected text, or dim text.
  16329.  
  16330.  
  16331.  
  16332.  13.3  Portability of Data Files
  16333.  
  16334.  Data files are rarely portable across different CPUs. Structures, unions,
  16335.  and arrays have varying internal layout and alignment requirements on
  16336.  different machines. In addition, byte ordering within words and actual word
  16337.  length may vary.
  16338.  
  16339.  The best way to achieve data-file portability is to write and read data
  16340.  files as one-dimensional character arrays. This procedure prevents alignment
  16341.  and padding problems if the data are written and read as characters. The
  16342.  only portability problem you are likely to encounter if you follow this
  16343.  course is a conflict in character sets; many computers have character-set
  16344.  conversion utilities.
  16345.  
  16346.  
  16347.  13.4  Portability Concerns Specific to Microsoft C
  16348.  
  16349.  Microsoft C offers extensions that let you take advantage of the full
  16350.  capabilities of the computer. These extensions are not portable to other
  16351.  compilers or environments. The following list shows keywords specific to
  16352.  Microsoft C:
  16353.  
  16354.  _asm               _far            _huge           pascal
  16355.  _based          _fastcall       _interrupt      _pascal
  16356.  cdecl           fortran         near            _saveregs
  16357.  _cdecl          _fortran        _near           _segment
  16358.  _export         huge            _loadds         _segname
  16359.  far
  16360.  
  16361.  The Microsoft C Reference contains compatibility information for every
  16362.  function in the run-time library. Any function or macro that does not have
  16363.  the ANSI box marked may not be portable to other compilers or computer
  16364.  systems.
  16365.  
  16366.  
  16367.  13.5  Microsoft C Byte Ordering
  16368.  
  16369.  Tables 13.4 and 13.5 summarize Microsoft C byte ordering for short and long
  16370.  types, respectively. In these tables, the least-significant byte of the data
  16371.  item is b0; the next byte is denoted by b1, and so on.
  16372.  
  16373.  Since byte ordering is machine specific, any program that uses this byte
  16374.  ordering will not be portable.
  16375.  
  16376.  Table 13.4  Byte Ordering for Short Types
  16377.  
  16378. ╓┌───────────────────────────┌───────────────────────────────────────────────╖
  16379.  CPU                         Byte Order
  16380.  ────────────────────────────────────────────────────────────────────────────
  16381.  8086                        b0   b1
  16382.  CPU                         Byte Order
  16383.  ────────────────────────────────────────────────────────────────────────────
  16384.  8086                        b0   b1
  16385.  80286                       b0   b1
  16386.  PDP-11(R)                   b0   b1
  16387.  VAX-11(R)                   b0   b1
  16388.  M68000                      b1   b0
  16389.  Z8000(R)                    b1   b0
  16390.  ────────────────────────────────────────────────────────────────────────────
  16391.  
  16392.  
  16393.  Table 13.5  Byte Ordering for Long Types
  16394.  
  16395. ╓┌────────────────────────┌──────────────────────────────────────────────────╖
  16396.  CPU                      Byte Order
  16397.  ────────────────────────────────────────────────────────────────────────────
  16398.  8086                     b0  b1  b2  b3
  16399.  80286                    b0  b1  b2  b3
  16400.  PDP-11                   b2  b3  b0  b1
  16401.  VAX-11                   b0  b1  b2  b3
  16402.  M68000                   b3  b2  b1  b0
  16403.  CPU                      Byte Order
  16404.  ────────────────────────────────────────────────────────────────────────────
  16405. M68000                   b3  b2  b1  b0
  16406.  Z8000                    b3  b2  b1  b0
  16407.  ────────────────────────────────────────────────────────────────────────────
  16408.  
  16409.  
  16410.  
  16411.  
  16412.  
  16413.  
  16414.  
  16415.  PART IV  OS/2 Support
  16416.  ────────────────────────────────────────────────────────────────────────────
  16417.  
  16418.  The Microsoft C Professional Development System provides support for OS/2
  16419.  development.
  16420.  
  16421.  Chapter 14 explains many of the general issues of OS/2 development,
  16422.  including accessing the OS/2 system functions, creating module-definition
  16423.  files, and using the OS/2-specific features of utilities such as the linker
  16424.  and BIND. Chapter 15 focuses on how to create a multithread application,
  16425.  including information about C run-time library support, potential problem
  16426.  areas, and how to use CodeView to debug multithread applications. Chapter 16
  16427.  concentrates on the creation of dynamic-link libraries, including C run-time
  16428.  library support, application program interface with DLLs, and debugging DLLs
  16429.  with CodeView.
  16430.  
  16431.  
  16432.  
  16433.  
  16434.  
  16435.  
  16436.  Chapter 14  Building OS/2 Applications
  16437.  ────────────────────────────────────────────────────────────────────────────
  16438.  
  16439.  Using Microsoft C 6.0, you can create applications for OS/2. This chapter
  16440.  explains features in the compiler and the utilities that
  16441.  
  16442.  
  16443.    ■   Call the OS/2 operating system directly from C functions
  16444.  
  16445.    ■   Perform multitasking within your program by starting multiple
  16446.        execution paths known as "threads"
  16447.  
  16448.    ■   Create dynamic-link libraries that can be used by multiple
  16449.        applications
  16450.  
  16451.    ■   Work in either OS/2 or DOS to create programs for both environments
  16452.  
  16453.    ■   Develop "dual-mode" applications that will run under both OS/2 and DOS
  16454.        from a single executable program file
  16455.  
  16456.  
  16457.  This chapter contains information about accessing the OS/2 Applications
  16458.  Program Interface (API) from your C programs. It also discusses compile
  16459.  options that affect applications you develop for OS/2, module-definition
  16460.  files and import libraries, linker options specific to developing OS/2
  16461.  applications, and using the BIND utility to create dual-mode applications.
  16462.  
  16463.  Chapters 15 and 16, "Creating Multithread OS/2 Applications" and
  16464.  "Dynamic-Linking with OS/2," contain detailed information about how
  16465.  Microsoft C supports these advanced OS/2 features.
  16466.  
  16467.  
  16468.  14.1  The OS/2 Applications Program Interface
  16469.  
  16470.  The entire set of OS/2 system calls is known as the OS/2 API. You need to
  16471.  access the OS/2 API for the low-level functions provided by the operating
  16472.  system, such as
  16473.  
  16474.  
  16475.    ■   Requests for information about the display
  16476.  
  16477.    ■   Requests to display information
  16478.  
  16479.    ■   Requests for information from the pointing device (mouse)
  16480.  
  16481.    ■   Requests for information from the keyboard
  16482.  
  16483.    ■   Requests for blocks of memory
  16484.  
  16485.    ■   Requests for disk actions, including reading and writing
  16486.  
  16487.  
  16488.  You can call all of the OS/2 system services directly from programs written
  16489.  in C. Under DOS, the API operates at a lower level, requiring programs to
  16490.  set up hardware registers and generate a software interrupt to access the
  16491.  system services. Under OS/2, programs use function calls to access the
  16492.  operating system services.
  16493.  
  16494.  Sections 14.1.1-14.1.3 describe the calling conventions and precautions you
  16495.  must observe when accessing OS/2 API functions.
  16496.  
  16497.  
  16498.  14.1.1  Calling the OS/2 API
  16499.  
  16500.  Your program must declare calls to the OS/2 API with both the _far and
  16501.  _pascal keywords. Adding the _pascal keyword to the function declaration
  16502.  ensures that the FORTRAN/Pascal calling convention is used. The _far keyword
  16503.  directs the compiler to generate an intersegment call instruction. A sample
  16504.  declaration for the OS/2 API function DosExit follows:
  16505.  
  16506.    void _far _pascal DosExit( unsigned int, unsigned int );
  16507.  
  16508.  You must be sure that all pointers passed to OS/2 API functions are far
  16509.  pointers, even if you are writing a program using the small or medium memory
  16510.  models. This process can be simplified if you include the OS2.H header file.
  16511.  
  16512.  
  16513.   OS/2 API function calls are far and must use the FORTRAN/ Pascal calling
  16514.  convention.
  16515.  
  16516.  OS/2 API functions use the FORTRAN/Pascal language calling convention. They
  16517.  expect arguments to be pushed onto the stack in left-to-right order, with
  16518.  the last argument in the list pushed onto the stack last. OS/2 API functions
  16519.  remove their arguments from the stack before returning to the caller.
  16520.  Standard C functions push their arguments from right to left, with the first
  16521.  argument being the last one pushed.
  16522.  
  16523.  All OS/2 API functions return 0 if the operation is successful. They return
  16524.  an error code if the operation fails.
  16525.  
  16526.  
  16527.  14.1.2  Including the OS/2 Header Files
  16528.  
  16529.  You do not have to construct your own API declarations if you use the OS2.H
  16530.  header file. It is the first file of a set of header files that supply
  16531.  function prototypes for every OS/2 API call and definitions of special OS/2
  16532.  structures, data types, and constants.
  16533.  
  16534.  The API function prototypes define all functions as far procedures with the
  16535.  FORTRAN/Pascal calling convention. They also take care of casting all near
  16536.  pointers to far pointers and other similar type coercions.
  16537.  
  16538.   Define a constant before including OS2.H.
  16539.  
  16540.  When you include OS2.H, the most commonly used data types and macros are
  16541.  automatically defined. To minimize compile time for the C preprocessor,
  16542.  other definitions are grouped by function. They are included only if your
  16543.  source file defines the appropriate constant before including OS2.H. The
  16544.  following list shows how these manifest constants affect functions from the
  16545.  OS/2 API:
  16546.  
  16547.  Constant                          Effect
  16548.  ────────────────────────────────────────────────────────────────────────────
  16549.  INCL_BASE                         All error constants, kernel, keyboard,
  16550.                                    video, and mouse definitions (same as
  16551.                                    INCL_DOS + INCL_SUB + INCL_DOSERRORS)
  16552.  
  16553.  INCL_DOS                          All kernel system definitions
  16554.  
  16555.  INCL_DOSERRORS                    All error constants
  16556.  
  16557.  INCL_KBD                          All keyboard definitions
  16558.  
  16559.  INCL_MOU                          All mouse definitions
  16560.  
  16561.  INCL_SUB                          All keyboard, video, and mouse
  16562.                                    definitions (same as INCL_KBD + INCL_VIO
  16563.                                    + INCL_MOU)
  16564.  
  16565.  INCL_VIO                          All video-display definitions
  16566.  
  16567.  INCL_WIN                          Basic set of Presentation Manager
  16568.                                    definitions
  16569.  
  16570.  The header files have additional constants that let you include smaller
  16571.  subsets or functions not defined in the standard sets.
  16572.  
  16573.   The statement #define INCL_DOS affects the functions defined.
  16574.  
  16575.  The program in the example below calls the OS/2 kernel to request a
  16576.  nonshareable, nondiscardable memory segment for an 8K buffer. The INCL_DOS
  16577.  constant in the #define statement instructs the C preprocessor to include
  16578.  all of the kernel function definitions. The function prototype for
  16579.  DosAllocSeg declares the first and third arguments as USHORT (unsigned short
  16580.  integers). The second argument is a far pointer to the OS/2 data type SEL,
  16581.  which is used for segment selectors.
  16582.  
  16583.    #define  INCL_DOS
  16584.    #include <os2.h>
  16585.  
  16586.    VOID GetMemorySegment()
  16587.    {
  16588.         SEL    selector;
  16589.  
  16590.         if ( DosAllocSeg( 8192, &selector, 0 ) )
  16591.             puts( "Allocation failed\n" );
  16592.         else
  16593.             puts( "Successful allocation\n" );
  16594.    }
  16595.  
  16596.  The function call in the example works correctly even in a small or medium
  16597.  memory model program where the selector variable is a near data type. All
  16598.  three arguments are coerced by the function prototype to the proper types,
  16599.  regardless of the memory model used.
  16600.  
  16601.  
  16602.  14.1.3  Creating Dual-Mode Programs as Family Applications
  16603.  
  16604.  The OS/2 API has a subset of system functions that have direct DOS
  16605.  equivalents. This subset is known as the "Family Applications Program
  16606.  Interface" (Family API). Programs that use only the Family API can be run
  16607.  under DOS and the OS/2 compatibility box, as well as under OS/2.
  16608.  
  16609.   You can build a single executable file for use under both OS/2 and DOS.
  16610.  
  16611.  By creating a Family API application, you can distribute the same executable
  16612.  file to both OS/2 and DOS users. The Microsoft C compiler, linker, and
  16613.  object module librarian are examples of family applications. The benefit of
  16614.  having a single executable file is offset by a few disadvantages:
  16615.  
  16616.  
  16617.    ■   The executable file is larger, because it includes a special loader
  16618.        and OS/2 API-simulator routines for running in DOS mode.
  16619.  
  16620.    ■   In real mode, the application loads more slowly than a program created
  16621.        specifically for either OS/2 or DOS. There is no performance penalty
  16622.        in loading or running in OS/2 protected mode.
  16623.  
  16624.    ■   When running in real mode, the program cannot use advanced OS/2
  16625.        features such as multiple threads or system calls that are not part of
  16626.        the Family API. If you take special precautions (described in Section
  16627.        14.5, "The BIND Utility"), the program can take advantage of these
  16628.        features when running in OS/2 protected mode.
  16629.  
  16630.  
  16631.  Follow the same steps to build both family and protected-mode applications
  16632.  but add an extra step at the end to create the Family API program. This step
  16633.  links functions from the dynamic-link libraries directly into a stand-alone
  16634.  executable file that can run in both real and protected mode.
  16635.  
  16636.  
  16637.  Restrictions on Family Applications
  16638.  
  16639.  Programs that use the Family API are subject to certain restrictions:
  16640.  
  16641.  
  16642.    ■   They cannot overcommit memory; they must fit into the DOS 640K
  16643.        environment.
  16644.  
  16645.    ■   They cannot use advanced OS/2 features, such as threads and
  16646.        semaphores, that do not have DOS counterparts.
  16647.  
  16648.    ■   They must restrict their use of some calls to the defined common
  16649.        subset. For example, some of the file-mode options for the DosOpen
  16650.        function are not available in real mode.
  16651.  
  16652.  
  16653.  
  16654.  Family API Functions
  16655.  
  16656.  The system calls that make up Family API are listed below. The calls marked
  16657.  with an asterisk (*) have different options or behavior, depending on
  16658.  whether they are running in real mode or protected mode. The Microsoft OS/2
  16659.  Programmer's Reference explains the functions and the differences between
  16660.  their real- and protected-mode implementations.
  16661.  
  16662.  DosAllocHuge*        DosHoldSignal*       DosSubSet
  16663.  DosAllocSeg*         DosInsMessage*       DosWrite
  16664.  DosBeep              DosMkDir             KbdCharIn*
  16665.  DosBufReset          DosMove              KbdFlushBuffer*
  16666.  DosCaseMap*          DosNewSize           KbdGetStatus*
  16667.  DosChdir             DosOpen*             KbdPeek*
  16668.  DosChgFilePtr        DosPutMessage*       KbdSetStatus*
  16669.  DosCLIAccess         DosQCurDir           KbdStringIn*
  16670.  DosClose             DosQCurDisk          VioGetBuf
  16671.  DosCreateCSAlias*    DosQFHandState       VioGetConfig
  16672.  DosDelete            DosQFileInfo         VioGetCurPos
  16673.  DosDevConfig         DosQFileMode         VioGetCurType
  16674.  DosDevIOCtl*         DosQFSInfo           VioGetMode
  16675.  DosDupHandle         DosQHandType         VioGetPhysBuf
  16676.  DosErrClass          DosQVerify           VioReadCellStr
  16677.  DosError*            DosRead*             VioReadCharStr
  16678.  DosExecPgm*          DosReallocHuge*      VioScrLock*
  16679.  DosExit*             DosReallocSeg*       VioScrollDn
  16680.  DosFileLocks         DosRmDir             VioScrollLf
  16681.  DosFindClose         DosSelectDisk        VioScrollRt
  16682.  DosFindFirst         DosSetCp             VioScrollUp
  16683.  DosFindNext*         DosSetDateTime       VioScrUnLock
  16684.  DosFreeSeg*          DosSetFHandState*    VioSetCurPos
  16685.  DosGetCollate*       DosSetFileInfo       VioSetCurType
  16686.  DosGetCp             DosSetFileMode       VioSetMode
  16687.  DosGetCtryInfo*      DosSetFSInfo         VioShowBuf
  16688.  DosGetDateTime       DosSetSigHandler*    VioWrtCellStr
  16689.  DosGetDBSCEv*        DosSetVec*           VioWrtCharStr
  16690.  DosGetEnv            DosSetVerify         VioWrtCharStrAtt
  16691.  DosGetHugeShift      DosSizeSeg           VioWrtNAttr
  16692.  DosGetMachineMode    DosSleep             VioWrtNCell
  16693.  DosGetMessage*       DosSubAlloc          VioWrtNChar
  16694.  DosGetVersion        DosSubFree           VioWrtTTy
  16695.  
  16696.  14.2  Compile Options for the CL Command
  16697.  
  16698.  This section describes the compile options you must specify in the
  16699.  Programmer's WorkBench or on the CL command line to designate a program's
  16700.  target environment (OS/2, DOS, or both). It also introduces options you
  16701.  should use with certain types of OS/2 applications, such as multithread
  16702.  programs, dynamic-link libraries, and programs calling C function
  16703.  dynamic-link libraries. For an in-depth discussion of topics that affect
  16704.  multithread processes and dynamic-link libraries, see Chapter 15, "Creating
  16705.  Multithread OS/2 Applications," and Chapter 16, "Dynamic-Linking with OS/2."
  16706.  
  16707.  
  16708.  
  16709.  14.2.1  The Link Mode Options (/Lp, /Lr, and /Lc)
  16710.  
  16711.  The /Lx options (/Lp, /Lr, and /Lc) provide the flexibility of programming
  16712.  for both OS/2 and DOS in either environment. Regardless of the host
  16713.  operating system, you can build applications for either target operating
  16714.  system. You do not have to switch to the target system to build the program.
  16715.  
  16716.  
  16717.  The /Lp option produces an OS/2 protected-mode program; the /Lr option
  16718.  creates a DOS real-mode program. /Lc is a synonym for /Lr.
  16719.  
  16720.  To use these options, the mode-specific combined libraries must be
  16721.  installed. Unless you choose a default operating environment, each
  16722.  mode-specific library has the letter P or R at the end of its base name. For
  16723.  example, the protected-mode small memory model library with the emulator
  16724.  floating-point option is named SLIBCEP.LIB. The corresponding real-mode
  16725.  library is named SLIBCER.LIB. The default name, however, is SLIBCE.LIB.
  16726.  
  16727.  Installing and Using the Microsoft C Professional Development System
  16728.  describes how to create mode-specific libraries with the SETUP program. It
  16729.  also explains how to establish a default target environment by renaming
  16730.  libraries. A default environment is useful if you work mainly in one mode
  16731.  (OS/2 or DOS) but sometimes write programs for the other mode. When you set
  16732.  up OS/2 as the default mode, SLIBCEP.LIB, for example, becomes SLIBCE.LIB.
  16733.  
  16734.   Don't use /Lx options unless you have mode-specific libraries.
  16735.  
  16736.  When you use the /Lx options, you instruct the compiler to override the
  16737.  default library name in the object module's library search record and to
  16738.  substitute the mode-specific combined library name. The compiler also
  16739.  generates a link response file with the /NODEFAULTLIBRARYSEARCH (/NOD)
  16740.  linker option to override the default library. See Section 14.4, "Link
  16741.  Command-Line Options," for more information about the /NOD option.
  16742.  
  16743.  Do not use the /Lp option to specify protected mode when OS/2 is the default
  16744.  environment. If you do this, the compiler uses the name of the mode-specific
  16745.  library (e.g., SLIBCEP.LIB). Because SETUP renamed the library to SLIBCE.LIB
  16746.  to create a default environment, the library search fails. This caution also
  16747.  applies to specifying /Lr when you have installed DOS as the default
  16748.  environment.
  16749.  
  16750.  If you invoke the linker in a separate step from the compilation, you must
  16751.  specify the /NOD link option.
  16752.  
  16753.  ────────────────────────────────────────────────────────────────────────────
  16754.  NOTE
  16755.  
  16756.  There is a special library, LLIBCMT, for building multithread OS/2
  16757.  applications. Another special library, LLIBCDLL, supports multithread
  16758.  dynamic-link libraries. If you use LLIBCMT or LLIBCDLL, you must use one of
  16759.  the library selection options described in Section 14.2.3 instead of / Lp.
  16760.  ────────────────────────────────────────────────────────────────────────────
  16761.  
  16762.  
  16763.  14.2.2  Creating Bound Programs Option (/Fb)
  16764.  
  16765.  The /Fb option allows you to compile, link, and bind an application in one
  16766.  step. Binding an executable file creates a Family API program that can run
  16767.  under both OS/2 and DOS.
  16768.  
  16769.  When you use /Fb, the compiler invokes the BIND utility program immediately
  16770.  after the link step. You can also execute BIND directly (as described in
  16771.  Section 14.5, "The BIND Utility"). You must have the API.LIB and OS2.LIB
  16772.  files in the path specified by the LIB environment variable or in your
  16773.  current working directory.
  16774.  
  16775.  The syntax for the /Fb option is
  16776.  
  16777.    /Fb«bound-exe»
  16778.  
  16779.   You can specify a separate name for a bound-executable file.
  16780.  
  16781.  The optional bound-exe parameter specifies the name of the bound program. It
  16782.  must directly follow the /Fb option, without intervening spaces. The
  16783.  bound-exe name can be a file specification, a drive name, or a directory
  16784.  specification. If you specify a file name without an extension, the compiler
  16785.  appends the .EXE extension to the name. If you give a directory
  16786.  specification for bound-exe, the name must end with a backslash ( \ ) so the
  16787.  compiler can distinguish it from an ordinary file name. If you do not supply
  16788.  a name, BIND uses the name of the unbound program and overwrites it.
  16789.  
  16790.  When creating both bound and protected-mode versions with different names,
  16791.  consider this example:
  16792.  
  16793.    CL /Lp /Fbsampleb sample.c
  16794.  
  16795.  The protected-mode executable file that this command creates is called
  16796.  SAMPLE.EXE; the bound-executable file is called SAMPLEB.EXE.
  16797.  
  16798.   You may need to run BIND as a separate step instead of using the /Fb
  16799.  option.
  16800.  
  16801.  The /Fb option works only if you are doing a single-step compile and link.
  16802.  If the CL command line includes the /c (compile without link) option, the
  16803.  compiler ignores the /Fb option. If you use /c, you must run the BIND
  16804.  utility as a separate step of the program build.
  16805.  
  16806.  If your program includes calls to API functions that are not in the FAPI
  16807.  subset, you must use the /n option of the BIND utility, described in Section
  16808.  14.5, to build the dual-mode executable file. If you need to use the /n BIND
  16809.  option, you cannot compile with /Fb. You must compile without linking by
  16810.  using the /c option at the compile stage; then link the program and run the
  16811.  BIND utility with the /n option.
  16812.  
  16813.  
  16814.  14.2.3  Library Selection Options (/MT, /ML, /MD, /Zl)
  16815.  
  16816.  Special libraries are provided for building OS/2 multithread applications
  16817.  and dynamic-link libraries. You must not use these libraries with any other
  16818.  C run-time library.
  16819.  
  16820.   Special libraries must be the only C run-time libraries linked with your
  16821.  program.
  16822.  
  16823.  If you use one of these special libraries, apply one of the library
  16824.  selection options (/ML, /MD, or /MT) to tell the compiler to replace the
  16825.  default library name in the object file with the name of the special
  16826.  library. This ensures that the linker does not bring in code from the
  16827.  default libraries. If you do not specify one of the options when compiling,
  16828.  you must link with the /NOD option to prevent search of a default library,
  16829.  such as SLIBCE.LIB.
  16830.  
  16831.  If you fail to include any of these options, the linker searches the default
  16832.  library and may select the wrong version of a library function. It might,
  16833.  for example, select the single thread version of the printf function for a
  16834.  multithread program that has more than one thread calling printf.
  16835.  
  16836.  Because the /Lp option (see Section 14.2.1, "The Link Mode Options")
  16837.  instructs the compiler to specify the default protected-mode libraries
  16838.  rather than the special multithread or DLL-specific libraries, do not use it
  16839.  with /Zl or /Mx.
  16840.  
  16841.  
  16842.  Multithread Library Option (/MT)
  16843.  
  16844.  When you specify the /MT option, the compiler embeds the LLIBCMT.LIB
  16845.  library name in the object file. Chapter 15, "Creating Multithread OS/2
  16846.  Applications," explains how to build multithread applications using
  16847.  LLIBCMT.LIB. The /MT option also has the effect of combining these
  16848.  command-line options:
  16849.  
  16850.    /ALw /FPi /G2 /D MT
  16851.  
  16852.  
  16853.  C Run-Time Library for Building DLLs (/ML)
  16854.  
  16855.  Use the /ML option to specify that you are building a dynamic-link library
  16856.  that calls functions in LLIBCDLL.LIB, the C run-time library for
  16857.  dynamic-link  libraries. The library name is embedded in the object file.
  16858.  The /ML option also has the effect of combining these command-line options:
  16859.  
  16860.  
  16861.    /ALw /FPa /G2 /D MT
  16862.  
  16863.  
  16864.  C Run-Time Library for DLLs (/MD)
  16865.  
  16866.  Use the /MD option to create a dynamic-link library of C run-time routines.
  16867.  With this option, the object file does not have any library search records.
  16868.  The /MD option has the effect of combining these command-line options:
  16869.  
  16870.    /ALw /FPi /G2 /DDLL /D MT
  16871.  
  16872.  Chapter 16, "Dynamic Linking with OS/2," describes the process of building
  16873.  and using dynamic-link libraries with LLIBCDLL.LIB.
  16874.  
  16875.  
  16876.  Suppress Default Library Option (/Zl)
  16877.  
  16878.  Use the /Zl option when you want to suppress selection of a default library.
  16879.  It tells the compiler not to place the default library name in the object
  16880.  file.
  16881.  
  16882.   You can specify libraries and additional LINK options on the CL command
  16883.  line.
  16884.  
  16885.  You can specify link options or the names of libraries on the CL command
  16886.  line with the /LINK option. You can also give the library name, with its
  16887.  .LIB extension, before the /LINK option. Each command below selects the
  16888.  multithread C run-time library:
  16889.  
  16890.    CL /Zl myprog.c llibcmt.lib
  16891.  
  16892.    CL /Zl myprog.c /link llibcmt
  16893.  
  16894.  If you compile with the /c (compile without link) option, your link command
  16895.  must include the library name:
  16896.  
  16897.    LINK myprog, myprog.exe, myprog.map, llibcmt.lib, myprog.def
  16898.  
  16899.  
  16900.  14.2.4  Memory-Model Options (/Ax)
  16901.  
  16902.  You must select the memory model appropriate to your application. For
  16903.  protected-mode applications, the large model provides the most convenient
  16904.  interface with the special libraries. It provides the additional benefit of
  16905.  placing code and data into multiple segments, allowing OS/2 to swap parts of
  16906.  the program to disk efficiently.
  16907.  
  16908.   Use the large memory model with LLIBCMT (/AL and /MT).
  16909.  
  16910.  The multithread run-time C library, LLIBCMT.LIB, is a large-model library.
  16911.  All library function calls must be far calls. In addition, all pointers
  16912.  passed to functions in the library must be far pointers. If you do not
  16913.  compile with the /AL option, you use must use the keyword _far when
  16914.  declaring pointers. Variables can be declared either near or far as long as
  16915.  they are either passed by value or cast to a far address.
  16916.  
  16917.  If you want to call fopen for example, you must use code such as the
  16918.  following:
  16919.  
  16920.    FILE _far * fp;
  16921.    fp = fopen( ... );
  16922.  
  16923.  ────────────────────────────────────────────────────────────────────────────
  16924.  NOTE
  16925.  
  16926.  If you are using the compact, large, or huge memory model, data pointers are
  16927.  far by default, so you do not need to explicitly specify _far.
  16928.  ────────────────────────────────────────────────────────────────────────────
  16929.  
  16930.   Because each thread has its own stack, you have to compile in an SS != DS
  16931.  model.
  16932.  
  16933.  Multithread applications require that each thread have its own stack. As a
  16934.  result, you cannot safely assume that the stack segment is in the default
  16935.  data group (DGROUP). That means that the stack segment can be different from
  16936.  the data segment (SS != DS).
  16937.  
  16938.  To specify that you have selected an SS != DS model, you must use the /Au or
  16939.  /Aw option. The /MT option is a shorthand way of specifying this combination
  16940.  of options to the compiler:
  16941.  
  16942.    /ALw/FPi/G2/DMT
  16943.  
  16944.  The /MT option also causes the compiler to place a library search record for
  16945.  LLIBCMT in the object file.
  16946.  
  16947.  
  16948.  14.3  Module-Definition Files and Import Libraries
  16949.  
  16950.  A module-definition file tells the linker about the characteristics of an
  16951.  application or dynamic-link library. It describes names, segments, memory
  16952.  requirements, and import and export definitions. Export definitions make
  16953.  functions in the OS/2 dynamic-link libraries (DLLs) available to other
  16954.  programs. Each export definition specifies a function name. A program using
  16955.  these functions must have import definitions in order to find each
  16956.  dynamic-link function. Each import definition specifies a function name and
  16957.  the name of the dynamic-link library where the function resides.
  16958.  
  16959.  The IMPLIB utility generates a library of import definitions that can be
  16960.  examined during the link. For imported functions, the import library can be
  16961.  used in place of a module-definition file.
  16962.  
  16963.  Module-definition files are optional for most OS/2 programs. Two types of
  16964.  programs must use them:
  16965.  
  16966.  
  16967.    ■   Dynamic-link libraries
  16968.  
  16969.    ■   Programs with I/O privileges
  16970.  
  16971.  
  16972.  Each module-definition file contains one or more module statements defining
  16973.  attributes of the executable program. The statements and their associated
  16974.  attributes are listed below:
  16975.  
  16976.  Statement                         Attribute
  16977.  ────────────────────────────────────────────────────────────────────────────
  16978.  CODE                              Gives default attributes for code
  16979.                                    segments
  16980.  
  16981.  DATA                              Gives default attributes for data
  16982.                                    segments
  16983.  
  16984.  DESCRIPTION                       Describes the module in one line
  16985.  
  16986.  EXETYPE                           Identifies the operating system
  16987.  
  16988.  EXPORTS                           Defines exported functions
  16989.  
  16990.  HEAPSIZE                          Specifies local heap size, in bytes
  16991.  
  16992.  IMPORTS                           Defines imported functions
  16993.  
  16994.  LIBRARY                           Names a dynamic-link library
  16995.  
  16996.  NAME                              Names an application
  16997.  
  16998.  OLD                               Preserves import information from a
  16999.                                    previous version of the library
  17000.  
  17001.  PROTMODE                          Specifies that the module runs only in
  17002.                                    OS/2 protected mode
  17003.  
  17004.  REALMODE                          Relaxes some restrictions that the
  17005.                                    linker imposes for protected-mode
  17006.                                    programs
  17007.  
  17008.  SEGMENTS                          Gives attributes for specific segments
  17009.  
  17010.  STACKSIZE                         Specifies local stack size, in bytes
  17011.  
  17012.  STUB                              Adds a DOS 3.x executable file to the
  17013.                                    beginning of the module, usually to
  17014.                                    terminate the program when run in real
  17015.                                    mode
  17016.  
  17017.  In addition to the keywords listed above, each statement includes one or
  17018.  more fields to complete the attribute description. All keywords must be
  17019.  entered in uppercase. You can include comments in the module-definition file
  17020.  by beginning the line with a semicolon (;). For a complete list of the
  17021.  keywords and their meaning, see on-line help for information about
  17022.  module-definition files.
  17023.  
  17024.  
  17025.  14.3.1  Adding a Module-Definition File to the LINK Command
  17026.  
  17027.  The module-definition file name is the last field of the link command:
  17028.  
  17029.    LINK objects «,«exe» » «, «map» » «, «lib» » «, «def» » «;»
  17030.  
  17031.  This example uses the default libraries:
  17032.  
  17033.    LINK sample, sample.exe, sample.map,,sample.def
  17034.  
  17035.  When you use a module-definition file, you must use the /c option on the CL
  17036.  command line and link in a separate step. If you are linking without a
  17037.  module-definition file, you can use a semicolon after your last entry to
  17038.  suppress LINK's prompt for the module-definition file name and other missing
  17039.  parameters.
  17040.  
  17041.  The segmented-executable linker is the only LINK program that recognizes
  17042.  module-definition files. Since it is backwards compatible, it should be the
  17043.  only linker in your path. The QuickC linker does not process these files.
  17044.  
  17045.  The following sections illustrate ways to use module-definition files.
  17046.  On-line help describes all of the commands and options available.
  17047.  
  17048.  
  17049.  14.3.2  Creating Dynamic-Link Libraries (DLLs)
  17050.  
  17051.  You can build your own dynamic-link libraries. A simple module-definition
  17052.  file for such a library with one public function is shown below:
  17053.  
  17054.    LIBRARY Mylib INITINSTANCE
  17055.  
  17056.    DATA MULTIPLE
  17057.  
  17058.    EXPORTS
  17059.     MyProc
  17060.  
  17061.  You can use the same module-definition file you used to create the
  17062.  dynamic-link library as input to the IMPLIB utility. IMPLIB generates a
  17063.  library file with a .LIB extension for use by applications calling your
  17064.  dynamic-link routines. Section 14.3.5 describes the IMPLIB program. Chapter
  17065.  16, "Dynamic Linking with OS/2," explains how to build a dynamic-link
  17066.  library.
  17067.  
  17068.  The LIBRARY statement tells the linker that this is a dynamic-link library
  17069.  rather than an application. (Applications use the NAME statement instead of
  17070.  the LIBRARY statement.)
  17071.  
  17072.  The EXPORTS statement gives the name of the public function.
  17073.  
  17074.   You can designate exported functions in a C source file.
  17075.  
  17076.  The C language keyword _export is an alternative to the EXPORTS statement.
  17077.  When _export appears in a function declaration or definition, the compiler
  17078.  puts the function and its parameter size in the object module's export
  17079.  record. Functions with the _export keyword that are not listed in the
  17080.  module-definition file cannot have input/output privileges or alias names.
  17081.  
  17082.   Using generic library names is dangerous.
  17083.  
  17084.  Since OS/2 systems have many dynamic-link libraries installed, try to pick a
  17085.  name that uniquely identifies your library. If you choose a generic name,
  17086.  such as CRT.DLL or WINDOWS.DLL, you run the risk of having your library
  17087.  overwritten by someone else's dynamic-link library with the same name.
  17088.  
  17089.  
  17090.  14.3.3  Creating Programs with I/O Privileges
  17091.  
  17092.  OS/2 programs that must access hardware directly can designate a code
  17093.  segment with input/output privileges. This segment can then perform a
  17094.  limited set of I/O instructions but cannot make any calls to dynamic-link
  17095.  libraries.
  17096.  
  17097.  You cannot use the C run-time library functions inp and outp for input and
  17098.  output. Their use is limited to real-mode programs. You can, however, use
  17099.  in-line assembler code in your C source program to access a port.
  17100.  
  17101.  The sample module-definition file below shows two segments for a program:
  17102.  
  17103.    NAME         IOPROG
  17104.  
  17105.    EXETYPE         OS/2
  17106.  
  17107.    SEGMENTS
  17108.         _IOSEG      IOPL
  17109.         _TEXT       NOIOPL
  17110.  
  17111.    EXPORTS
  17112.         CharIn      4
  17113.         CharOut     4
  17114.  
  17115.  The first code segment contains the I/O portion of the program and has the
  17116.  IOPL keyword. The second segment is designated NOIOPL (the default).
  17117.  
  17118.   The EXPORT statement for IOPL functions must include parameter size.
  17119.  
  17120.  The EXPORTS section names two functions in the IOPL segment that can be
  17121.  called by procedures outside the segment. It also specifies the size of the
  17122.  function's parameters. Procedures with I/O privileges must specify the
  17123.  number of words needed for their parameters.
  17124.  
  17125.  ────────────────────────────────────────────────────────────────────────────
  17126.  NOTE
  17127.  
  17128.  Unless the user has specified IOPL=YES in the CONFIG.SYS file, the program
  17129.  will not load.
  17130.  ────────────────────────────────────────────────────────────────────────────
  17131.  
  17132.  
  17133.  14.3.4  Creating Presentation Manager Applications
  17134.  
  17135.  The Presentation Manager calls window and dialog procedures inside a
  17136.  Presentation Manager application. The sample module-definition file below
  17137.  exports these procedures and gives the linker additional instructions for
  17138.  building the program. Module-definition files are optional for Presentation
  17139.  Manager applications. They can be used to control the way different segments
  17140.  of the program are loaded.
  17141.  
  17142.    NAME         PMSAMPLE    WINDOWAPI
  17143.  
  17144.    EXETYPE          OS/2
  17145.    STACKSIZE        4096
  17146.  
  17147.    SEGMENTS
  17148.         _INIT       PRELOAD
  17149.         _HELP       LOADONCALL
  17150.         _TEXT       LOADONCALL
  17151.  
  17152.  In the preceding example, the NAME statement identifies the program as an
  17153.  application named PMSAMPLE. The WINDOWAPI keyword tells the linker to mark
  17154.  the executable file as a Presentation Manager application. Only programs
  17155.  marked as windows applications or windows-compatible applications can share
  17156.  the Presentation Manager screen group.
  17157.  
  17158.  The EXETYPE statement tells the linker to build a program that runs only in
  17159.  protected mode and to produce the optimal executable file for OS/2.
  17160.  
  17161.  The STACKSIZE statement allocates 4096 bytes of local stack space. This is
  17162.  the minimum stack size recommended for Presentation Manager programs.
  17163.  
  17164.   You can reduce run-time memory requirements.
  17165.  
  17166.  The SEGMENTS statement controls the way code and data segments are handled.
  17167.  By default, segments are not brought into physical memory until needed. The
  17168.  PRELOAD keyword in the example tells the system loader to load the  _INIT
  17169.  segment when the program starts. The  _TEXT  and  _HELP  segments are loaded
  17170.  on demand. You can use the compiler's /NT option to generate your own
  17171.  segment names, such as  _INIT  and  _HELP. Separate segments are useful for
  17172.  code that is executed infrequently, such as a help subsystem. This reduces
  17173.  the amount of run-time memory required for your application, since each
  17174.  segment will be loaded when and if there is a request for it.
  17175.  
  17176.  
  17177.  14.3.5  Creating Import Libraries with the IMPLIB Utility
  17178.  
  17179.  Applications that call dynamic-link library functions must use import
  17180.  definitions that specify the location of each dynamic-link function. The
  17181.  definitions consist of a function name and the name of the dynamic-link
  17182.  library file where it resides.
  17183.  
  17184.  Although the application can use a module-definition file to create the
  17185.  import definitions, it is easier to use import libraries built by the IMPLIB
  17186.  utility.
  17187.  
  17188.  IMPLIB creates an import library in the form of a file with a .LIB
  17189.  extension, which is read by the linker. At link time, the .LIB file is
  17190.  specified in the LINK command line, along with other libraries.
  17191.  
  17192.  IMPLIB accepts two types of sources:
  17193.  
  17194.  
  17195.    ■   The module-definition file used to create the dynamic-link library
  17196.  
  17197.    ■   The dynamic-link library itself
  17198.  
  17199.  
  17200.  The IMPLIB command has the syntax:
  17201.  
  17202.    IMPLIB «/c»libfile deffile  «deffile ...»
  17203.  
  17204.  or
  17205.  
  17206.    IMPLIB  «/c»libfile dynlib «dynlib ...»
  17207.  
  17208.  The /c option directs IMPLIB to be case sensitive. By default, it is case
  17209.  insensitive.
  17210.  
  17211.  The libfile field names the new import library file. The deffile or dynlib
  17212.  fields name the input files, which are dynamic-link library or
  17213.  module-definition files.
  17214.  
  17215.  The following example creates the import library file named MYLIB.LIB from
  17216.  the MYLIB.DLL dynamic-link library:
  17217.  
  17218.    IMPLIB mylib.lib mylib.dll
  17219.  
  17220.  For more information about import libraries and IMPLIB, consult on-line
  17221.  help.
  17222.  
  17223.  
  17224.  14.4  Link Command-Line Options
  17225.  
  17226.  This section describes command-line options that control various aspects of
  17227.  the linker and the circumstances in which you will need to use them.
  17228.  
  17229.  
  17230.  /NODEFAULTLIBRARYSEARCH (/NOD)
  17231.  
  17232.   If you did not compile with /MT, /MD, or /ML, suppress default library
  17233.  searching.
  17234.  
  17235.  The /NODEFAULTLIBRARYSEARCH option prevents the linker from searching any
  17236.  library specified in an object file. When you specify this option, you
  17237.  should also specify the name of the library to be linked. The minimum
  17238.  abbreviation for this option is /NOD.
  17239.  
  17240.  If you are using the multithread library, LLIBCMT, or the dynamic-link
  17241.  library, LLIBCDLL, you should use this option. Use it with dynamic-link
  17242.  libraries built with LLIBCDLL. This is mandatory if you did not compile with
  17243.  the /Zl, /MT, or /ML options.
  17244.  
  17245.  You can select a specific library by appending the library name to the /NOD
  17246.  option, as in
  17247.  
  17248.    /NOD:LLIBCMT.LIB
  17249.  
  17250.  
  17251.  /NOEXTENDEDDICTSEARCH (/NOE)
  17252.  
  17253.  The /NOEXTENDEDDICTSEARCH option prevents the linker from searching the
  17254.  extended dictionary, which is an internal list of symbol locations
  17255.  maintained by the linker. You need to use this option if a library symbol
  17256.  (such as _setargv, _binmode, or _varstck) is redefined and you receive error
  17257.  L2044 from the linker. The minimum abbreviation for this option is /NOE.
  17258.  
  17259.  
  17260.  /NOIGNORECASE (/NOI)
  17261.  
  17262.  The /NOIGNORECASE option preserves case sensitivity. By default, LINK maps
  17263.  all names to uppercase characters. Because many C function names are a mix
  17264.  of upper- and lowercase letters, it is important to use this option. The
  17265.  compile option /Zc causes any name declared with the _pascal keyword to be
  17266.  treated without regard to case at the source level. The minimum abbreviation
  17267.  is /NOI.
  17268.  
  17269.  
  17270.  /PMTYPE
  17271.  
  17272.  The /PMTYPE option is an alternative to specifying Presentation Manager
  17273.  compatibility with the NAME statement of a module-definition file. Use the
  17274.  following syntax:
  17275.  
  17276.    /PMTYPE:type
  17277.  
  17278.  Type must be one of the following:
  17279.  
  17280.  Type                              Effect
  17281.  ────────────────────────────────────────────────────────────────────────────
  17282.  PM                                The application is an OS/2 Presentation
  17283.                                    Manager application using the
  17284.                                    Presentation Manager API and running in
  17285.                                    the Presentation Manager screen group.
  17286.                                    This type corresponds to specifying
  17287.                                    WINDOWAPI in the NAME statement of a
  17288.                                    module-definition file.
  17289.  
  17290.  VIO                               The application is compatible with the
  17291.                                    OS/2 Presentation Manager and can run in
  17292.                                    a window or in a
  17293.                                    separate screen group. This type
  17294.                                    corresponds to specifying WINDOWCOMPAT
  17295.                                    in the NAME statement of a
  17296.                                    module-definition file.
  17297.  
  17298.  NOVIO                             The application is not compatible with
  17299.                                    the OS/2
  17300.                                    Presentation Manager. It must run in a
  17301.                                    separate
  17302.                                    screen group. This type corresponds to
  17303.                                    specifying
  17304.                                    NOTWINDOWCOMPAT in the NAME statement of
  17305.                                    a module-definition file.
  17306.  
  17307.  
  17308.  14.5  The BIND Utility
  17309.  
  17310.  The BIND utility converts a protected-mode program into a program that runs
  17311.  in both OS/2 and DOS environments. It replaces Family API calls to
  17312.  dynamic-link library functions with DOS emulator routines from the API.LIB
  17313.  library. (See Section 14.1.3, "Creating Dual-Mode Programs as Family
  17314.  Applications," for a list of Family API calls.) BIND produces a stand-alone
  17315.  program file that can run under
  17316.  
  17317.  
  17318.    ■   OS/2 protected mode
  17319.  
  17320.    ■   OS/2 real mode
  17321.  
  17322.    ■   DOS 2.x and DOS 3.x
  17323.  
  17324.  
  17325.  BIND is an alternative to the C compiler's /Fb option described in Section
  17326.  14.2.2, "Creating Bound Programs Option." You must use BIND instead of the
  17327.  /Fb option when you compile with the /c (compile without link) option or
  17328.  when your program includes functions that operate only in protected mode.
  17329.  
  17330.   You can include functions in a bound application that are not members of
  17331.  the Family API.
  17332.  
  17333.  To include functions available only in protected mode, you must run the BIND
  17334.  utility with the /n option. Your run-time code must call the Family API
  17335.  function DosGetMachineMode to determine whether it is running in real or
  17336.  protected mode. When your program executes in real mode, it will be aborted
  17337.  if it tries to call a function available only in protected mode.
  17338.  
  17339.  You might choose to design your application so it executes different
  17340.  sections of code, depending on the machine mode. For example, the
  17341.  application may need to keep track of the passage of elapsed time or to
  17342.  detect time-outs. In real mode, you might use polling or timing loops or
  17343.  perhaps intercept the timer interrupts. In protected mode, you should use
  17344.  the OS/2 semaphore and timer services, such as DosSetSem and DosTimerAsync,
  17345.  instead.
  17346.  
  17347.  Invoke BIND with the following syntax:
  17348.  
  17349.    BIND infile «implibs» «linklibs» «/o outfile» «/n @file» «/n names»
  17350.    «/m mapfile»
  17351.  
  17352.  The /n option provides a way to include protected-mode functions. It has two
  17353.  formats:
  17354.  
  17355.  
  17356.    ■   A list of one or more names, separated by spaces.
  17357.  
  17358.    ■   The name of a file, preceded by the at (@) sign. The file should
  17359.        consist of a list of functions, one name per line.
  17360.  
  17361.  
  17362.  The /o option specifies a name for the bound-executable file. If it is not
  17363.  present, the name of the input file is used.
  17364.  
  17365.  The /m option causes a link map to be generated for the real-mode version of
  17366.  the executable file.
  17367.  
  17368.  To bind a program named TIMER that uses DosTimerAsync to manage time-outs
  17369.  when running in protected mode, invoke BIND as follows:
  17370.  
  17371.    BIND TIMER /n DosTimerAsync
  17372.  
  17373.  For more information about BIND and other command-line options, consult
  17374.  on-line help.
  17375.  
  17376.  
  17377.  
  17378.  
  17379.  
  17380.  
  17381.  Chapter 15  Creating Multithread OS/2 Applications
  17382.  ────────────────────────────────────────────────────────────────────────────
  17383.  
  17384.  Microsoft C, version 6.0, provides support for creating multithread
  17385.  applications under OS/2. You should consider using more than one thread if
  17386.  your application needs to manage multiple activities, such as simultaneous
  17387.  keyboard and mouse input. One thread can process keyboard input while a
  17388.  second thread filters mouse activities. A third thread could update the
  17389.  display screen based on data from the mouse and keyboard threads. At the
  17390.  same time, other threads can access disk files or get data from a
  17391.  communications port.
  17392.  
  17393.  This chapter explains the features in C 6.0 that support the creation of
  17394.  multithread programs. It also describes some important ways in which
  17395.  programming for OS/2 is different than programming for DOS.
  17396.  
  17397.  
  17398.  15.1  Multithread Programs
  17399.  
  17400.  OS/2 performs the scheduling and allocation of real hardware resources to
  17401.  multiple programs, or "processes." It does not actually schedule the
  17402.  processes themselves; it schedules threads belonging to the processes.
  17403.  
  17404.  A thread is basically a path of execution through a program. It is also the
  17405.  smallest unit of execution that OS/2 schedules. A thread consists of a
  17406.  stack, the state of the CPU registers, and an entry in the execution list of
  17407.  the system scheduler. Each thread shares all of the process's resources.
  17408.  
  17409.  A process consists of one or more threads and the code, data, and other
  17410.  resources of a program in memory. Typical program resources are open files,
  17411.  semaphores, and dynamically allocated memory. A program executes when the
  17412.  system scheduler gives one of its threads execution control. The scheduler
  17413.  determines which threads should run and when they should run. Threads of
  17414.  lower priority may have to wait while higher priority threads complete their
  17415.  tasks.
  17416.  
  17417.   Threads operate  independently and are  unaware of other threads.
  17418.  
  17419.  All threads in a process operate independently of one another. Unless you
  17420.  take special steps to make them visible to each other, each thread executes
  17421.  while completely unaware of the existence of other threads in a process.
  17422.  Threads sharing common resources, however, must coordinate their work by
  17423.  using flags, semaphores or some other method of interprocess communication.
  17424.  See Section 15.3, "Writing a Multithread Program," for more information
  17425.  about synchronizing threads.
  17426.  
  17427.  
  17428.  15.1.1  Library Support
  17429.  
  17430.   All shared functions  in a multithread  program must be re-entrant.
  17431.  
  17432.  If one thread is suspended by the OS/2 scheduler while executing the printf
  17433.  function, one of the program's other threads might start executing. If the
  17434.  second thread also calls printf, data might be corrupted. To avoid this,
  17435.  access to static data used by the function must be restricted to one thread
  17436.  at a time. This process of restricting access to certain data is called
  17437.  serialization.
  17438.  
  17439.  You do not need to serialize access to stack-based (automatic) variables
  17440.  because each thread has a different stack. Therefore, a function that uses
  17441.  only automatic (stack) variables is re-entrant. The standard C run-time
  17442.  libraries, such as SLIBCE, have a limited number of re-entrant functions. A
  17443.  multithread program needing to use C run-time library functions that are
  17444.  normally not re-entrant should be built with the multithread library
  17445.  LLIBCMT.LIB.
  17446.  
  17447.  
  17448.  The Multithread C Library LLIBCMT.LIB
  17449.  
  17450.  The support library LLIBCMT.LIB is a re-entrant large-model library for
  17451.  cre-ating multithread programs.
  17452.  
  17453.   A multithread program  linked with LLIBCMT.LIB can  use any memory model.
  17454.  
  17455.  All calls to library functions must use the large-model calling interface
  17456.  (far code pointers, far calls, and far data pointers). When your application
  17457.  calls functions in this library,
  17458.  
  17459.  
  17460.    ■   All library calls must be far calls.
  17461.  
  17462.    ■   All library calls must use the C calling convention; programs compiled
  17463.        using the /Gr (fastcall calling convention) or /Gc (Pascal calling
  17464.        convention) options must use the standard include files for the
  17465.        run-time library functions they call.
  17466.  
  17467.    ■   All data and code pointers must be far pointers.
  17468.  
  17469.    ■   Variables passed to library functions must either be passed by value
  17470.        or cast to a far address.
  17471.  
  17472.    ■   Your main function must be declared far if you are compiling with the
  17473.        small or compact memory models.
  17474.  
  17475.  
  17476.  You do not need to explicitly declare far pointers if you are using the
  17477.  compact, large, or huge memory models, since these models use far pointers
  17478.  as default. For the large and huge memory models, the function calls are
  17479.  also far by default.
  17480.  
  17481.  A small-model program calling a library function such as isupper, for
  17482.  example, must use declarations like the following:
  17483.  
  17484.    int _far _cdecl isupper( int _c );
  17485.  
  17486.   Programs built with LLIBCMT.LIB are entirely self-contained.
  17487.  
  17488.  Programs built with LLIBCMT.LIB do not share C run-time library code or data
  17489.  with any dynamic-link libraries they call. Chapter 16 explains how to build
  17490.  DLLs and how to share code and data between processes.
  17491.  
  17492.  
  17493.  Alternatives to LLIBCMT.LIB
  17494.  
  17495.  If you choose to build a multithread program without using LLIBCMT.LIB, you
  17496.  must do the following:
  17497.  
  17498.  
  17499.    ■   Use the standard C libraries and limit library calls to the set of
  17500.        re-entrant functions.
  17501.  
  17502.    ■   Use the OS/2 API thread management functions, such as DosCreateThread.
  17503.  
  17504.    ■   Provide your own synchronization for functions that are not re-entrant
  17505.        by using OS/2 services such as semaphores and the DosEnterCritSec and
  17506.        DosExitCritSec functions.
  17507.  
  17508.  
  17509.  The C run-time library functions listed below are re-entrant and can be used
  17510.  in multithread programs linked with the standard libraries.
  17511.  
  17512.  abs
  17513.  atoi
  17514.  atol
  17515.  bsearch
  17516.  chdir
  17517.  getpid
  17518.  halloc
  17519.  hfree
  17520.  itoa
  17521.  labs
  17522.  lfind
  17523.  lsearch
  17524.  memccpy
  17525.  memchr
  17526.  memcmp
  17527.  memcpy
  17528.  memicmp
  17529.  memmove
  17530.  memset
  17531.  mkdir
  17532.  movedata
  17533.  putch
  17534.  rmdir
  17535.  segread
  17536.  strcat
  17537.  strchr
  17538.  strcmp
  17539.  strcmpi
  17540.  strcpy
  17541.  stricmp
  17542.  strlen
  17543.  strlwr
  17544.  strncat
  17545.  strncmp
  17546.  strncpy
  17547.  strnicmp
  17548.  strnset
  17549.  strrchr
  17550.  strrev
  17551.  strset
  17552.  strstr
  17553.  strupr
  17554.  swab
  17555.  tolower
  17556.  toupper
  17557.  ──────────────────────────────────────────
  17558.  
  17559.  WARNING
  17560.  
  17561.  The multithread library LLIBCMT.LIB includes the _beginthread and _endthread
  17562.  functions. The _beginthread function performs initialization without which
  17563.  many C run-time functions will fail. You must use _beginthread instead of
  17564.  DosCreateThread in C programs built with LLIBCMT.LIB if you intend to call C
  17565.  run-time functions.
  17566.  ────────────────────────────────────────────────────────────────────────────
  17567.  
  17568.  
  17569.  The Multithread Library Compile Option (/MT)
  17570.  
  17571.  The /MT option for the CL command is the best way to build a multithread
  17572.  program with LLIBCMT.LIB. The /MT option embeds the LLIBCMT library name in
  17573.  the object file. Using the /MT option automatically specifies the /ALw /FPi
  17574.  /G2 /D MT options. The following list describes what these options do.
  17575.  
  17576.  Switch                            Effect
  17577.  ────────────────────────────────────────────────────────────────────────────
  17578.  /ALw                              Use the large memory model with separate
  17579.                                    stack segment; do not reload the DS
  17580.                                    register as part of the entry sequence
  17581.                                    for every function
  17582.  
  17583.  /FPi                              Generate in-line floating-point
  17584.                                    instructions and select the emulator
  17585.                                    math package
  17586.  
  17587.  /G2                               Use the 80286 processor instruction set
  17588.  
  17589.  /D MT                             Use the multithread version of the
  17590.                                    include files
  17591.  
  17592.  These options can be combined with other options to specify different memory
  17593.  models and different relationships between the data segment and the stack.
  17594.  You can override the /G2 and /FPi options by specifying a different option
  17595.  later on the command line. The following example shows how to override the
  17596.  floating-point package option:
  17597.  
  17598.    CL /MT /FPa /Lp PROG.C
  17599.  
  17600.  ────────────────────────────────────────────────────────────────────────────
  17601.  NOTE
  17602.  
  17603.  You cannot replace the /MT option with /ALw /FPi /G2. You must use /MT to
  17604.  generate multithread programs.
  17605.  ────────────────────────────────────────────────────────────────────────────
  17606.  
  17607.  
  17608.  15.1.2  Include Files
  17609.  
  17610.  The Microsoft C 6.0 include files contain conditional sections for
  17611.  multithread applications using LLIBCMT.LIB. To compile your application with
  17612.  the appropriate definitions, you can
  17613.  
  17614.  
  17615.    ■   Compile with the /MT option described in Section 15.1.1, "Library
  17616.        Support."
  17617.  
  17618.    ■   Define the symbolic constant MT in your source file or on the command
  17619.        line with the /D option.
  17620.  
  17621.  
  17622.   Always use the  standard include files.
  17623.  
  17624.  Standard include files declare C run-time library functions as they are
  17625.  implemented in the libraries. If you used the Maximum Optimization (/Ox) or
  17626.  Register Calling Convention (/Gr) option, the compiler assumes that all
  17627.  functions should be called using the register calling convention. The
  17628.  run-time library functions were compiled using either the C or the
  17629.  FORTRAN/Pascal calling convention, and the declarations in the standard
  17630.  include files tell the compiler to generate correct external references to
  17631.  these functions.
  17632.  
  17633.  See Section 15.4, "Compiling and Linking," for examples of how to use the MT
  17634.  constant.
  17635.  
  17636.  
  17637.  15.1.3  C Run-Time Library Functions for Thread Control
  17638.  
  17639.  All OS/2 programs have at least one thread. Any thread can create additional
  17640.  threads. A thread can complete its work very quickly and then terminate, or
  17641.  it can stay active for the life of the program.
  17642.  
  17643.  The LLIBCMT and LLIBCDLL C run-time libraries provide two functions for
  17644.  thread creation and termination: the _beginthread and _endthread functions.
  17645.  They also declare the global variable _threadid, which contains the address
  17646.  of an application's current thread identifier.
  17647.  
  17648.  The _beginthread function creates a new thread and returns a thread
  17649.  identifier if the operation is successful. The thread will terminate
  17650.  automatically if it completes execution, or it can terminate itself with a
  17651.  call to _endthread.
  17652.  
  17653.  The global variable _threadid holds the address of the identifier of the
  17654.  current thread. It is defined in the STDDEF.H file as shown below:
  17655.  
  17656.    /* define pointer to thread id value */
  17657.    extern int far * _threadid;
  17658.  
  17659.  ────────────────────────────────────────────────────────────────────────────
  17660.  WARNING
  17661.  
  17662.  If you are going to call C run-time routines from a program built with
  17663.  LLIBCMT.LIB, you must start your threads with the _beginthread function. Do
  17664.  not use the OS/2 functions DosExit  and DosCreateThread. Using
  17665.  DosSuspendThread can lead to a deadlock condition when more than one thread
  17666.  is blocked waiting for the suspended thread to complete its access to a C
  17667.  run-time data structure.
  17668.  ────────────────────────────────────────────────────────────────────────────
  17669.  
  17670.  The _beginthread and _endthread functions are described in detail below.
  17671.  Section 15.2 illustrates their use in a sample multithread program.
  17672.  
  17673.  
  17674.  The _beginthread Function
  17675.  
  17676.   All threads in a process  can execute concurrently.
  17677.  
  17678.  The _beginthread function creates a new thread. A thread shares the code and
  17679.  data segments of a process with other threads in the process but has its own
  17680.  unique register values, stack space, and current instruction address. The
  17681.  system gives CPU time to each thread, so that all threads in a process can
  17682.  execute concurrently. You can find a complete description of _beginthread
  17683.  and its arguments in on-line help.
  17684.  
  17685.  The _beginthread function is similar to the DosCreateThread function in the
  17686.  OS/2 API with these differences:
  17687.  
  17688.  
  17689.    ■   The _beginthread function lets you pass arguments to the thread.
  17690.  
  17691.    ■   The stack address points to the bottom of the stack. It is the address
  17692.        of the start of an array or of the start of a block of dynamically
  17693.        allocated memory. When you use the DosCreateThread call, the stack
  17694.        address points to the top of the stack.
  17695.  
  17696.    ■   If you specify NULL for the stack address, _beginthread manages
  17697.        allocation and deallocation of the thread stack for you. This option
  17698.        is advantageous because it is difficult for your program to determine
  17699.        when a thread has terminated, so you cannot know when to deallocate
  17700.        the thread stack. However, _beginthread maintains enough information
  17701.        to know when a thread has terminated and deallocates the thread's
  17702.        stack the next time its thread ID is used.
  17703.  
  17704.  
  17705.  The _beginthread function returns the thread ID number of the new thread if
  17706.  successful or -1 if there was an error. Errors include specifying an
  17707.  odd-address stack or an odd- or zero-length stack (which is different than
  17708.  passing NULL for the stack address) or trying to create too many threads.
  17709.  The multithread library, LLIBCMT.LIB, supports the maximum number of threads
  17710.  allowed by OS/2.
  17711.  
  17712.  
  17713.  The _endthread Function
  17714.  
  17715.  The _endthread function terminates a thread created by _beginthread. Threads
  17716.  terminate automatically when they complete. The _endthread function is
  17717.  useful for conditional termination from within a thread. A thread dedicated
  17718.  to communications processing, for example, can quit if it is unable to get
  17719.  control of the communications port. You can find a complete description of
  17720.  _endthread in on-line help.
  17721.  
  17722.  
  17723.  15.2  Sample Multithread C Program
  17724.  
  17725.  BOUNCE.C is a sample multithread program that creates a new thread each time
  17726.  the letter `a' or `A' is entered at the keyboard. Each thread bounces a
  17727.  "happy face" of a different color around the screen. Up to 32 threads can be
  17728.  created. The program's normal termination occurs when `q' or `Q' is entered.
  17729.  It will also terminate if it receives the CTRL+C or CTRL+BREAK signals. See
  17730.  Section 15.4, "Compiling and Linking," for details on compiling and linking
  17731.  BOUNCE.C.
  17732.  
  17733.    /*  Bounce - Creates a new thread each time the letter 'a'is typed.
  17734.     *  Each thread bounces a happy face of a different color around the
  17735.  screen.
  17736.     *  All threads are terminated when the letter 'q' is entered or when
  17737.     *  the CTRL+C/CTRL+BREAK signals are received.
  17738.     *
  17739.     *  This program requires the multithread library. For example, compile
  17740.     *  with the following command line:
  17741.     *      CL /MT BOUNCE.C
  17742.     */
  17743.  
  17744.    #define INCL_NOCOMMON                     /* Use only what we need */
  17745.    #define INCL_NOPM                         /* Don't need PM */
  17746.    #define INCL_DOSPROCESS                   /* DosBeep and DosSleep */
  17747.    #define INCL_DOSSEMAPHORES                /* OS/2 semaphore functions */
  17748.    #define INCL_DOSSIGNALS                   /* OS/2 signal functions */
  17749.    #define INCL_VIO
  17750.    #define INCL_KBD
  17751.    #include <os2.h>
  17752.    #include <stdlib.h>
  17753.    #include <string.h>
  17754.    #include <stdio.h>
  17755.    #include <process.h>
  17756.  
  17757.    #define STACK_SIZE   4096
  17758.    #define MAX_THREADS  32
  17759.  
  17760.    void main( void );                          /* Thread 1: main */
  17761.    void KbdThread( void );                     /* Thread 2: keyboard input */
  17762.    void BounceProc( char * MyID );             /* Threads 3 to n: display */
  17763.    void VioClrScr( void );                     /* Screen clear */
  17764.    void ShutDown( void );                      /* Program shutdown */
  17765.    void VioWrtCStr( char *pchString,           /* Write string to display */
  17766.                     unsigned usRow, unsigned usColumn );
  17767.    void pascal far SigHandler( unsigned SigArg,/* Signal handler */
  17768.                                unsigned SigNum );
  17769.                                                /* Screen clear macro */
  17770.    #define VioClrScr() VioScrollDn( 0, 0, 50, 80, 50, BlankCell, 0 )
  17771.  
  17772.    struct tagCoords                            /* Display coordinates */
  17773.    {
  17774.        int xLoc;
  17775.        int yLoc;
  17776.        int xInc;
  17777.        int yInc;
  17778.    };
  17779.  
  17780.    unsigned long  RunFlag = 0;                 /* "Keep Running" semaphore */
  17781.    unsigned long  ScreenLock = 0;              /* Screen update semaphore  */
  17782.  
  17783.    char BlankCell[2] = { 0x20, 0x07 };
  17784.    VIOMODEINFO vmi = { sizeof( VIOMODEINFO ) };/* Mode information */
  17785.  
  17786.    PFNSIGHANDLER PrevHandler;                  /* for SetSigHandler call */
  17787.    unsigned int  PrevAction;                   /* for SetSigHandler call */
  17788.  
  17789.    void main()                                 /* Thread One */
  17790.    {
  17791.        /* Get display screen's text row and column sizes & clear the
  17792.  screen.*/
  17793.        VioGetMode( &vmi, 0 );
  17794.        VioClrScr();
  17795.        VioWrtCStr( "Threads running: 00.  Press 'a' to start another thread",
  17796.                    vmi.row - 1, 0 );
  17797.  
  17798.        /* Set the "we are running" semaphore. */
  17799.        DosSemSet( &RunFlag );
  17800.  
  17801.        /* Start keyboard thread. Let _beginthread allocate memory
  17802.         *  for the thread's stack.
  17803.         */
  17804.        _beginthread( KbdThread, NULL, STACK_SIZE, NULL );
  17805.  
  17806.        /* Install signal handler for CTRL+BREAK & CRTL+C. */
  17807.        DosSetSigHandler( (PFNSIGHANDLER)SigHandler, &PrevHandler,
  17808.  &PrevAction,
  17809.                           SIGA_ACCEPT, SIG_CTRLC );
  17810.  
  17811.        /* Wait for "running" semaphore to clear (from signal or 'q' key). */
  17812.        DosSemWait( &RunFlag, SEM_INDEFINITE_WAIT );
  17813.  
  17814.        _endthread();             /* Kill all threads */
  17815.    }
  17816.  
  17817.    void pascal far SigHandler( unsigned int SigArg, unsigned int SigNum )
  17818.    {
  17819.        static char BreakMsg[] = "Signal Termination";
  17820.  
  17821.        ShutDown();
  17822.        VioWrtCStr( BreakMsg, vmi.row - 1, 0 );
  17823.        /* Restore original signal handler for CTRL+BREAK & CRTL+C. */
  17824.        DosSetSigHandler( (PFNSIGHANDLER)PrevHandler, &PrevHandler,
  17825.  &PrevAction,
  17826.                           PrevAction, SIG_CTRLC );
  17827.    }
  17828.    void ShutDown( void )                       /* Clean up display when done
  17829.  */
  17830.    {
  17831.        /* Lock out screen updates from BounceProc & clear "running" semaphore
  17832.  */
  17833.        DosSemWait( &ScreenLock, SEM_INDEFINITE_WAIT );
  17834.        DosSemSet( &ScreenLock );
  17835.        VioClrScr();
  17836.        DosSemClear( &RunFlag );
  17837.    }
  17838.  
  17839.    void KbdThread( void )                          /* Thread Two: keyboard */
  17840.    {
  17841.        KBDKEYINFO  KeyInfo;                        /* for KbdCharIn call */
  17842.        char        ThreadNr = 0;
  17843.        char        NThreadMsg[4];
  17844.  
  17845.        do
  17846.        {
  17847.            /* Block this thread by waiting for keyboard input. */
  17848.  
  17849.            KbdCharIn( &KeyInfo, IO_WAIT, 0 );
  17850.            if( tolower( KeyInfo.chChar ) == 'a' && ThreadNr < MAX_THREADS)
  17851.            {
  17852.                ThreadNr++;
  17853.                _beginthread( BounceProc, NULL, STACK_SIZE, &ThreadNr );
  17854.                VioWrtCharStr( NThreadMsg, sprintf( NThreadMsg, "%02d",
  17855.  ThreadNr ),
  17856.                               vmi.row - 1, 17, 0 );
  17857.            }
  17858.        } while( tolower( KeyInfo.chChar ) != 'q' );
  17859.  
  17860.        ShutDown();
  17861.    }
  17862.  
  17863.    /* getrandom returns a random number between min and max, which must be in
  17864.     * integer range.
  17865.     */
  17866.    #define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) +
  17867.  (min))
  17868.  
  17869.    void BounceProc( char * MyID )                  /* Threads Three to n */
  17870.    {
  17871.        int       xOld, yOld;
  17872.        char      MyCell[2];
  17873.        char      CurrentCell[2];
  17874.        int       CellLen = 2;
  17875.        struct tagCoords Coords;
  17876.  
  17877.     /* Generate update increments and initial display coordinates. */
  17878.        srand( (unsigned) *MyID * 3 );
  17879.        Coords.xLoc = getrandom( 0, vmi.col - 1 );
  17880.        Coords.yLoc = getrandom( 0, vmi.row - 1 );
  17881.        Coords.xInc = getrandom( -3, 3 );
  17882.        Coords.yInc = getrandom( -3, 3 );
  17883.  
  17884.        /* Set up "happy face" & generate color attribute from thread
  17885.  number.*/
  17886.        if( *MyID > 16)
  17887.            MyCell[0] = 0x01;          /* outline face */
  17888.        else
  17889.            MyCell[0] = 0x02;          /* solid face */
  17890.        MyCell[1] =  *MyID & 0x0F;     /* force black background */
  17891.  
  17892.        for( ;; )
  17893.        {
  17894.            /* Wait for display to be available, then lock it. */
  17895.            DosSemWait( &ScreenLock, SEM_INDEFINITE_WAIT );
  17896.            DosSemSet( &ScreenLock );
  17897.  
  17898.            /* If we still occupy the old screen position, blank it out. */
  17899.            VioReadCellStr( CurrentCell, &CellLen, yOld, xOld, 0 );
  17900.            if ( CurrentCell[0] == MyCell[0] && CurrentCell[1] == MyCell[1] )
  17901.                VioWrtCellStr( BlankCell, CellLen, yOld, xOld, 0 );
  17902.  
  17903.            /* Draw new face, then clear screen lock */
  17904.            VioWrtCellStr( MyCell, CellLen, Coords.yLoc, Coords.xLoc, 0 );
  17905.            DosSemClear( &ScreenLock );
  17906.  
  17907.            /* Increment the coordinates for next placement of the block. */
  17908.            xOld = Coords.xLoc;
  17909.            yOld = Coords.yLoc;
  17910.            Coords.xLoc += Coords.xInc;
  17911.            Coords.yLoc += Coords.yInc;
  17912.  
  17913.  
  17914.  
  17915.           /* If we are about to go off the screen, reverse direction */
  17916.            if( Coords.xLoc < 0 || Coords.xLoc >= vmi.col )
  17917.            {
  17918.                Coords.xInc = -Coords.xInc;
  17919.                DosBeep( 400, 50 );
  17920.            }
  17921.            if( Coords.yLoc < 0 || Coords.yLoc >= vmi.row )
  17922.            {
  17923.                Coords.yInc = -Coords.yInc;
  17924.                DosBeep( 600, 50 );
  17925.            }
  17926.  
  17927.            /* Sleep to slow down screen update rate */
  17928.            DosSleep( 75L );
  17929.        }
  17930.    }
  17931.  
  17932.    void VioWrtCStr( char *pchString, unsigned usRow, unsigned usColumn )
  17933.    {
  17934.        VioWrtCharStr( pchString, strlen( pchString ), usRow, usColumn, 0 );
  17935.    }
  17936.  
  17937.  
  17938.  15.3  Writing a Multithread Program
  17939.  
  17940.  When you write a program with multiple threads, you must coordinate their
  17941.  behavior and use of the program's resources. You must also make sure that
  17942.  each thread receives its own stack.
  17943.  
  17944.  
  17945.  Sharing Common Resources
  17946.  
  17947.  Each thread has its own stack and its own copy of the CPU registers. Other
  17948.  resources, such as files, static data, and heap memory, are shared by all
  17949.  threads in the process. Threads using these common resources must coordinate
  17950.  their work. OS/2 provides semaphores and the DosEnterCritSec and
  17951.  DosExitCritSec system services for synchronizing resources.
  17952.  
  17953.   Your program must provide  for resource conflicts.
  17954.  
  17955.  When multiple threads are accessing static data, your program must provide
  17956.  for possible resource conflicts. Consider a program where one thread updates
  17957.  a static data structure containing x,y coordinates for items to be displayed
  17958.  by another thread. If the update thread alters the x coordinate and is
  17959.  preempted before it can change the y coordinate, the display thread may be
  17960.  scheduled before the y coordinate is updated. The item would be displayed at
  17961.  the wrong location. You can avoid this type of problem by using semaphores
  17962.  to control access to the structure.
  17963.  
  17964.  Using semaphores is a way of communicating among threads or processes that
  17965.  are executing asynchronously of one another. This communication is usually
  17966.  used to coordinate the activities of multiple threads or processes,
  17967.  typically by controlling access to a shared resource by "locking" and
  17968.  "unlocking" the resource. To solve the x,y coordinate update problem
  17969.  described above, the update thread would set a semaphore indicating that the
  17970.  data structure is in use before performing the update. It would then clear
  17971.  the semaphore when both coordinates had been processed. The display thread
  17972.  must wait for the semaphore to be clear before updating the display. This
  17973.  process of waiting for a semaphore is often called "blocking" on a semaphore
  17974.  because the process is blocked and cannot continue until the semaphore
  17975.  clears.
  17976.  
  17977.   RAM semaphores are faster  than system semaphores.
  17978.  
  17979.  OS/2 supports two types of semaphores: system and RAM semaphores. You must
  17980.  use a system semaphore if more than one process needs to access the
  17981.  semaphore. You can use the much faster RAM semaphores if their use is
  17982.  confined to the threads within a process.
  17983.  
  17984.  The BOUNCE.C program in Section 15.2 uses a RAM semaphore named  ScreenLock
  17985.  to coordinate screen updates. Each time one of the display threads is ready
  17986.  to write to the screen, it calls DosSemWait with a pointer to  ScreenLock
  17987.  and constant SEM_INDEFINITE_WAIT to indicate that the DosSemWait call should
  17988.  block on the semaphore and not time out. If the ScreenLock semaphore is
  17989.  clear, the wait function returns immediately. Otherwise, the thread blocks
  17990.  until the semaphore clears. When the thread receives control again, it calls
  17991.  DosSemSet to set the ScreenLock semaphore so other threads cannot interfere
  17992.  with the display. When the thread completes the display update, it releases
  17993.  the semaphore by calling DosSemClear.
  17994.  
  17995.  The  ShutDown  routine in BOUNCE.C is called from both the keyboard thread
  17996.  and the signal handler. The routine uses the  ScreenLock  semaphore to make
  17997.  sure other threads do not write to the screen after the screen has been
  17998.  cleared.
  17999.  
  18000.  Screen displays and static data are only two of the resources requiring
  18001.  careful management. For example, your program may have multiple threads
  18002.  accessing the same file. Since another thread may have moved the file
  18003.  pointer, each thread must reset the file pointer before reading or writing.
  18004.  In addition, each thread must make sure that it is not preempted between the
  18005.  time it positions the pointer and the time it accesses the file. These
  18006.  threads should use a semaphore to coordinate access to the file by
  18007.  bracketing each file access with DosSemRequest and DosSemClear calls. The
  18008.  following code fragment illustrates this technique:
  18009.  
  18010.    HSEM    hsemIOSem;
  18011.  
  18012.    DosSemRequest( hsemIOSem, SEM_INDEFINITE_WAIT );
  18013.    fseek( fp, desired_position, 0L );
  18014.    fwrite( data, sizeof( data ), 1, fp );
  18015.    DosSemClear( hsemIOSem );
  18016.  
  18017.  
  18018.  Thread Stacks
  18019.  
  18020.   Stack checking is performed for each thread.
  18021.  
  18022.  All of an application's default stack space is allocated to the first thread
  18023.  of execution, which is known as thread 1. As a result, you must allocate
  18024.  memory to provide a separate stack for each additional thread your program
  18025.  needs. You must do this before creating the thread. Stack checking, if
  18026.  enabled, is performed for each thread. The keyboard thread in BOUNCE.C calls
  18027.  the malloc function each time the user wants to start a new display thread.
  18028.  If the allocation is successful, the _beginthread function is called. The
  18029.  first argument in the _beginthread call is a pointer to the BounceProc
  18030.  function, which will execute the threads. The last argument is an ID number
  18031.  that is passed to BounceProc. BounceProc uses the ID number to seed the
  18032.  random number generator and to select the thread's color attribute and
  18033.  display character.
  18034.  
  18035.  Threads that make calls to the C run-time library or to the OS/2 API must
  18036.  allow sufficient stack space for the library and API functions they call.
  18037.  The C printf function requires more than 500 bytes of stack space, and you
  18038.  should have 2K of stack space available when calling OS/2 API routines. To
  18039.  be safe, allocate at least 4K for each thread's stack.
  18040.  
  18041.   Use as little static  data as possible.
  18042.  
  18043.  Since each thread has its own stack, you can avoid potential collisions over
  18044.  data items by using as little static data as possible. Design your program
  18045.  to use automatic stack variables for all data that can be private to a
  18046.  thread. The only global variables in the BOUNCE.C program are either RAM
  18047.  semaphores or variables that never change once they are initialized.
  18048.  
  18049.  
  18050.  Signal Handling
  18051.  
  18052.  Signals are events that interrupt the normal flow of your program's
  18053.  execution. They are similar to hardware interrupts, but they come from the
  18054.  operating system or other programs and occur asynchronously. If you do not
  18055.  provide your own routines, OS/2 will take the default action for each
  18056.  signal, such as cancelling your program when the user enters CTRL+BREAK. You
  18057.  can install your own signal handler with the OS/2 API function
  18058.  DosSetSigHandler.
  18059.  
  18060.  ────────────────────────────────────────────────────────────────────────────
  18061.  WARNING
  18062.  
  18063.  The C run-time function signal is not supported in the multithread library
  18064.  LLIBCMT.LIB.
  18065.  ────────────────────────────────────────────────────────────────────────────
  18066.  
  18067.  When a signal occurs, OS/2 always suspends thread 1 and gives control to the
  18068.  signal handler, if installed. As a result, thread 1 must not be executing C
  18069.  run-time library code when the signal handler gets control or a potential
  18070.  deadlock condition can occur. In addition, the signal handler must not call
  18071.  C run-time library functions. Consider the following sequence of events:
  18072.  
  18073.  
  18074.    1.  Thread 2 is executing printf when the user interrupts it by pressing
  18075.        CTRL+C. The program has designated a CTRL+C signal handler, so OS/2
  18076.        immediately transfers control to the signal handler in thread 1.
  18077.  
  18078.    2.  The signal handler in thread 1 tries to execute the statement:
  18079.  
  18080.        printf( "^C: Do you want to quit?" );
  18081.  
  18082.  
  18083.    3.  The printf call in thread 2 has already locked output to the console,
  18084.        so thread 1's printf must wait for release of that lock.
  18085.  
  18086.    4.  The thread 2 printf function never regains control because the signal
  18087.        handler must complete before other processing can continue. As a
  18088.        result, it is never able to release the lock on console output.
  18089.  
  18090.  
  18091.  If a situation like this happens, the program will wait indefinitely for
  18092.  resolution of the two mutually exclusive conditions.
  18093.  
  18094.  A multithread C program can process signals if it adheres to the following
  18095.  restrictions:
  18096.  
  18097.  
  18098.    ■   Thread 1 must be dedicated to signal handling and must not call the C
  18099.        run-time library once it identifies the signal handler to OS/2 using
  18100.        the API function DosSetSigHandler. When the signal handler gets
  18101.        control, it should set a semaphore or flag so other threads in the
  18102.        program can determine that the signal has occurred and is being
  18103.        processed.
  18104.  
  18105.    ■   The other threads in the process must check the status of semaphores
  18106.        set by thread 1 and respond accordingly.
  18107.  
  18108.  
  18109.  The BOUNCE.C sample program waits until thread 2, the keyboard handler,
  18110.  starts before installing the signal handler. It then dedicates thread 1 to
  18111.  signal handling by having the thread wait for a semaphore. Thread 1 blocks
  18112.  until either the keyboard thread or the signal handler clears the semaphore.
  18113.  It then calls _endthread to terminate the process, including all the other
  18114.  threads.
  18115.  
  18116.  
  18117.  15.4  Compiling and Linking
  18118.  
  18119.  The steps for compiling and linking the multithread program BOUNCE.C are
  18120.  given below:
  18121.  
  18122.  
  18123.    1.  Ensure that the files LLIBCMT.LIB and OS2.LIB are in the directory
  18124.        specified in your LIB environment variable.
  18125.  
  18126.        The file LLIBCMT.LIB takes the place of the regular C run-time library
  18127.        files. The file OS2.LIB provides support for OS/2 system calls made in
  18128.        the program, such as KbdCharIn.
  18129.  
  18130.    2.  Compile and link the program with the CL command-line option /MT.
  18131.  
  18132.        The /Lp option instructs the compiler to create a protected-mode
  18133.        application. The /MT option implies the large memory model with a
  18134.        separate stack segment (/ALw). The multithread library functions have
  18135.        their own data segment but use the caller's stack. This option also
  18136.        sets the library search record to LLIBCMT.LIB and sets the MT symbolic
  18137.        constant for the multithread versions of the include files. The /link
  18138.        GRTEXTP option instructs the linker to search GRTEXTP.LIB, the
  18139.        character-graphics library for protected mode.
  18140.  
  18141.        To compile and link in a single step, use this CL command line:
  18142.  
  18143.        CL /Lp /MT BOUNCE.C /link grtextp
  18144.  
  18145.        For separate compile and link steps, you invoke the compiler and the
  18146.        linker with this code:
  18147.  
  18148.        CL /c /Lp /MT BOUNCE.C
  18149.        LINK BOUNCE;
  18150.  
  18151.    3.  If you choose not to use the /MT option, you must take these steps:
  18152.  
  18153.  
  18154.  
  18155.    ■   Ensure that the special multithread include file support is enabled.
  18156.  
  18157.        ■   Use the /Aw option. This is required because the functions in
  18158.            LLIBCMT.LIB have their own data segment but use the caller's
  18159.            stack. The /Aw option specifies a segment setup of SS not equal to
  18160.            DS with DS not reloaded on function entry.
  18161.  
  18162.        ■   Make sure that only far pointers are passed to library functions.
  18163.  
  18164.        ■   Make sure that all variables are either passed by value or cast to
  18165.            a far address (the large memory model).
  18166.  
  18167.        ■   Specify the multithread library and suppress default library
  18168.            selection.
  18169.  
  18170.        The multithread include files are used when you define the symbolic
  18171.        constant MT. You can do this with the CL command line option /D MT or
  18172.        within the C source file before any include statements, as shown
  18173.        below:
  18174.  
  18175.        #define MT
  18176.        #include <stdlib.h>
  18177.  
  18178.        To compile and link in a single step with the default libraries
  18179.        suppressed, this is the complete CL command line:
  18180.  
  18181.        CL /Lp /ALw /Zl /D MT BOUNCE.C /link LLIBCMT+OS2
  18182.  
  18183.        To perform a two-step compile and link with the default libraries
  18184.        suppressed in the link step, use these commands:
  18185.  
  18186.        CL /c /Lp /ALw /D MT BOUNCE.C
  18187.        LINK /NOD BOUNCE,,,LLIBCMT+OS2;
  18188.  
  18189.        1.  Run the program under OS/2.
  18190.  
  18191.  
  18192.  
  18193.  
  18194.  15.5  Avoiding Problem Areas
  18195.  
  18196.  There are several problems you can encounter in creating, linking, or
  18197.  executing a multithread C program. Some of the more common ones are
  18198.  described here.
  18199.  
  18200.  Problem                           Probable Cause
  18201.  ────────────────────────────────────────────────────────────────────────────
  18202.  LINK searches for mLIBC f.LIB.    If you omit the /NOD option from the
  18203.                                    LINK command, LINK searches for the
  18204.                                    default library. The
  18205.                                    default library should not be used with
  18206.                                    multithread programs. The /NOD option
  18207.                                    tells the computer not to search the
  18208.                                    default libraries. This problem can also
  18209.                                    be avoided by compiling with the /Zl
  18210.                                    option, which suppresses default library
  18211.                                    search records in the object files.
  18212.  
  18213.  You get error SYS1943. A program  Many OS/2 programming errors cause
  18214.  caused a protection               protection violations. A common cause of
  18215.  violation.                        protection violations is the indirect
  18216.                                    assignment of data to null pointers.
  18217.                                    This results in your program trying to
  18218.                                    access memory that does not "belong" to
  18219.                                    it, so a protection violation is issued.
  18220.                                    Protection violations also occur if your
  18221.                                    program gets a memory buffer from the
  18222.                                    operating system and then tries to read
  18223.                                    or write past the end of the
  18224.  
  18225.                                    buffer. Another cause of this error is
  18226.                                    failing to specify the condition "SS is
  18227.                                    not equal to DS" in the CL command
  18228.                                    invocation. Specify the correct
  18229.                                    conditions with the /ALw memory model
  18230.                                    option.
  18231.  
  18232.                                    An easy way to detect the cause of a
  18233.                                    protection violation is to compile your
  18234.                                    program with CodeView information, then
  18235.                                    run it in CodeView. When the protection
  18236.                                    fault occurs, OS/2 will transfer control
  18237.                                    to CodeView, and the cursor will be
  18238.                                    positioned on the line that caused the
  18239.                                    problem. See Chapter 9, "Debugging C
  18240.                                    Programs with CodeView," for more
  18241.                                    information about the CodeView debugger.
  18242.  
  18243.  Your program generates numerous   If you attempt to compile and link a
  18244.  compile and link errors.          multithread
  18245.                                    program without defining the symbolic
  18246.                                    constant MT, many of the definitions
  18247.                                    required for the multithread library
  18248.                                    will be missing. Define MT on the CL
  18249.                                    command line with /MT or /D MT, or use
  18250.                                    #define MT in your program.
  18251.  
  18252.  You can eliminate many potential problems by setting the compiler's warning
  18253.  level to one of its highest values and heeding the warning messages. By
  18254.  using the /W3 or /W4 warning level options, you can detect unintentional
  18255.  data conversions, missing function prototypes, and use of non-ANSI features.
  18256.  
  18257.  
  18258.  
  18259.  15.6  Using the Protected-Mode CodeView Debugger
  18260.  
  18261.  The protected-mode version of CodeView (CVP) has special commands for
  18262.  debugging multiple processes and threads. It adds Thread and Process items
  18263.  to the standard Run Menu. Your CONFIG.SYS file must specify IOPL=YES for
  18264.  protected-mode CodeView to run.
  18265.  
  18266.  To enable multiple process debugging, invoke CodeView with the /O
  18267.  (offspring) option. Selecting the Process item from the Run Menu brings up a
  18268.  list box of child processes associated with the parent process. You choose
  18269.  the process to be debugged by selecting it with the list box. The Process
  18270.  item will be grey (unselectable) if you did not specify the /O option. The
  18271.  /O option applies only to debugging multiple processes. You do not need to
  18272.  use it to debug multiple threads.
  18273.  
  18274.  Selecting the Thread item from the Run Menu produces a list box showing the
  18275.  status of each thread associated with the current process. You can use the
  18276.  list box to designate a different current thread or to change a thread's
  18277.  status. There are equivalent keyboard commands for each option.
  18278.  
  18279.  
  18280.  15.6.1  Compiling with the /Zi Option
  18281.  
  18282.  The compiler option /Zi causes the compiler to include symbolic information
  18283.  and line numbers in the object file for debugging with CodeView. If you run
  18284.  LINK in a separate step, you must invoke it with the /CODEVIEW option, which
  18285.  can be abbreviated as /CO. To compile and link the sample program BOUNCE.C
  18286.  in a single step, enter this code:
  18287.  
  18288.    CL /MT /Zi BOUNCE.C
  18289.  
  18290.  The following commands are for a two-step compile and link:
  18291.  
  18292.    CL /c /MT /Zi BOUNCE.C
  18293.  
  18294.    link /CO BOUNCE;
  18295.  
  18296.  
  18297.  15.6.2  Prompt for Thread Number
  18298.  
  18299.  When you debug a protected-mode program with CodeView, the command prompt is
  18300.  preceded by a three-digit number indicating the current thread. Thread 1 is
  18301.  always the current thread when you start a program. The prompt appears as
  18302.  
  18303.    001>
  18304.  
  18305.  
  18306.  15.6.3  Thread Commands
  18307.  
  18308.  Protected-mode CodeView (CVP) has special commands to control the execution
  18309.  of threads. The CodeView Thread commands are accessed using the Thread
  18310.  command from the Run menu. Dialog commands for thread control start with the
  18311.  tilde character (~). Thread commands specify which thread(s) the command
  18312.  applies to, followed by the command. The syntax of the dialog version of the
  18313.  Thread command is
  18314.  
  18315.  ~«specifier«command»»
  18316.  
  18317.  Entering the tilde character by itself displays the status of all threads.
  18318.  Enter the tilde and a specifier to see the status of particular threads.
  18319.  Legal values for the specifier field are listed below:
  18320.  
  18321.  Specifier                         Function
  18322.  ────────────────────────────────────────────────────────────────────────────
  18323.  (blank)                           Displays the status of all threads
  18324.  
  18325.  #                                 Specifies the last thread that executed
  18326.  
  18327.  .                                 Specifies the current thread
  18328.  
  18329.  *                                 Specifies all threads
  18330.  
  18331.  n                                 Specifies the number of an existing
  18332.                                    thread
  18333.  
  18334.  The optional command field controls the way specified threads are executed.
  18335.  If it is omitted, status is displayed, but thread activity is not affected.
  18336.  Thread commands are summarized below, followed by examples. For more
  18337.  information about command execution and about how other threads in the
  18338.  process may be affected, consult on-line help.
  18339.  
  18340.  Command                           Function
  18341.  ────────────────────────────────────────────────────────────────────────────
  18342.  (blank)                           Display status
  18343.  
  18344.  BP                                Set a breakpoint (used with the normal
  18345.                                    Breakpoint Set command syntax)
  18346.  
  18347.  E                                 Execute in slow motion
  18348.  
  18349.  F                                 Freeze the thread(s)
  18350.  
  18351.  G                                 Pass control to a thread
  18352.  
  18353.  P                                 Execute a program step
  18354.  
  18355.  S                                 Select specified thread as the current
  18356.                                    thread
  18357.  
  18358.  T                                 Trace a thread
  18359.  
  18360.  U                                 Unfreeze thread(s)
  18361.  
  18362.  
  18363.  Controlling a Thread Being Debugged
  18364.  
  18365.  If your program has multiple threads using the same functions, you may want
  18366.  to monitor the behavior of one particular thread. The standard Breakpoint
  18367.  Set command will affect every thread. The thread Breakpoint Set command lets
  18368.  you limit the breakpoint to one or more threads. The sample program BOUNCE.C
  18369.  has multiple threads executing the function BounceProc. This function erases
  18370.  the symbol at the thread's current screen position, writes it to a new
  18371.  location, computes the display coordinates to be used the next time the
  18372.  thread receives control, and then sleeps to slow down the rate at which the
  18373.  display is updated.
  18374.  
  18375.  Since thread-specific breakpoints can only be set for threads that are
  18376.  already running, you can set a breakpoint that will be executed after the
  18377.  target thread starts. In BOUNCE.C, the source line in thread 2 that tests
  18378.  each character received from the keyboard is a good location for such a
  18379.  breakpoint (line 113). Since thread 2 is not active when the program begins,
  18380.  you must first set a breakpoint in thread 1 after it has started thread 2
  18381.  (line 73). The first breakpoint can be set by conventional methods or by
  18382.  using the thread breakpoint command:
  18383.  
  18384.    001>~1BP .73
  18385.  
  18386.  Once you have reached the first breakpoint, you can set the keyboard test
  18387.  breakpoint for thread 2:
  18388.  
  18389.    001>~2BP .113
  18390.  
  18391.  The BOUNCE.C program starts a new thread each time the letter `a' is typed.
  18392.  (`A' is also accepted.) Once you have started the desired number of threads,
  18393.  you can trigger the thread 2 breakpoint without starting a new thread by
  18394.  pressing another key, such as the space bar. When you reach the breakpoint
  18395.  in thread 2, you can set breakpoints for the other threads. To set a
  18396.  breakpoint in thread 3's BounceProc function immediately after it has
  18397.  updated the screen (source line 168), enter this code:
  18398.  
  18399.    001>~3BP .168
  18400.  
  18401.  When this breakpoint is reached, the CodeView prompt will reflect the
  18402.  current thread number:
  18403.  
  18404.    003>
  18405.  
  18406.  You can then set other breakpoints for the thread, execute it in slow motion
  18407.  without any other threads running in the background, or enter other CodeView
  18408.  commands, such as Breakpoint Clear.
  18409.  
  18410.  
  18411.  Freezing and Unfreezing Threads
  18412.  
  18413.   Frozen threads do not execute.
  18414.  
  18415.  It can be useful to freeze one or more threads so they don't interfere with
  18416.  execution of a thread you are debugging. In the BOUNCE.C program, for
  18417.  example, you can monitor the path of a single bouncing ball by freezing all
  18418.  but one of the bounce threads. Frozen threads will not be scheduled for
  18419.  execution.
  18420.  
  18421.  If you have a large number of threads running, you can freeze all of them in
  18422.  a single command and then unfreeze the threads you want to monitor. Unfrozen
  18423.  threads continue to operate normally and will execute any breakpoints they
  18424.  encounter. The following example freezes all threads, enables threads 1 and
  18425.  4, and then checks the status of all threads:
  18426.  
  18427.    001>~*F
  18428.    001>~1U
  18429.    001>~4U
  18430.    001>~
  18431.  
  18432.  If thread 1 is waiting for a semaphore when the status command is invoked,
  18433.  the report shows the following:
  18434.  
  18435.    001 Blocked
  18436.    002 Frozen
  18437.    003 Frozen
  18438.    004 Runnable
  18439.  
  18440.  
  18441.  Switching to a Particular Thread
  18442.  
  18443.  The S (select) and E (execute) variations of the Thread command can be used
  18444.  to switch the current thread. However, when another thread causes the
  18445.  program to stop by hitting a breakpoint, the debugger will select the thread
  18446.  that encountered the breakpoint as the current thread.
  18447.  
  18448.  If you include ~.S in the breakpoint command, CodeView stops the thread that
  18449.  encounters the breakpoint, then immediately switches back to the current
  18450.  thread. The following example selects thread 4, sets a breakpoint at line
  18451.  168 in thread 3, and switches to thread 4 when the breakpoint is hit:
  18452.  
  18453.    001>~4S
  18454.    001>~3BP .168 "~.S"
  18455.    001>G
  18456.  
  18457.  
  18458.  15.6.4  Screen Groups Used by CodeView
  18459.  
  18460.  Only one CodeView session at a time is supported in protected mode. You
  18461.  cannot run multiple copies in concurrent screen groups.
  18462.  
  18463.  The View Output Screen command ( \ ) works differently in protected mode and
  18464.  in real mode. In protected mode, your application's output will be displayed
  18465.  for three seconds. The display will then revert to the CodeView display. To
  18466.  view the output window for a longer period, specify a different delay
  18467.  interval, measured in seconds, as follows:
  18468.  
  18469.    \10
  18470.  
  18471.  
  18472.  
  18473.  
  18474.  
  18475.  
  18476.  
  18477.  
  18478.  Chapter 16  Dynamic Linking with OS/2
  18479.  ────────────────────────────────────────────────────────────────────────────
  18480.  
  18481.  An OS/2 dynamic-link library (DLL) is an executable file containing
  18482.  functions that are available to other programs. In a statically linked
  18483.  program, you link the program with all its component functions when you
  18484.  build the executable file. In a dynamically linked program, the
  18485.  program-build step does not link all of the code. Instead, OS/2 links calls
  18486.  to functions in dynamic-link libraries at program load time or while the
  18487.  program is running. The DLL code and data become part of the address space
  18488.  of each program, even when the DLL is being accessed by several application
  18489.  programs.
  18490.  
  18491.  This chapter describes how to build your own dynamic-link libraries and how
  18492.  to build programs that use them.
  18493.  
  18494.  
  18495.  16.1  Overview of Dynamic Linking
  18496.  
  18497.  Dynamic linking is the process of resolving external calls when a program
  18498.  runs, instead of at link time. It offers several benefits:
  18499.  
  18500.  
  18501.    ■   Multiple programs can use the same dynamic-link library
  18502.        simultaneously. Since only one copy of the DLL is in memory, there are
  18503.        fewer demands for physical memory and swap space.
  18504.  
  18505.    ■   Updates to dynamic-link libraries do not affect the programs that use
  18506.        them, since the only connection between DLLs and application programs
  18507.        is the function-calling sequence.
  18508.  
  18509.    ■   Application programs require less disk space and memory, since their
  18510.        executable program files contain the names of DLL functions but not
  18511.        the code for the functions.
  18512.  
  18513.    ■   Dynamic-link libraries can call other dynamic-link libraries.
  18514.  
  18515.    ■   DLLs can extend the OS/2 operating system to provide new or improved
  18516.        system services. This is possible because most of OS/2 consists of a
  18517.        set of dynamic-link libraries.
  18518.  
  18519.  
  18520.  
  18521.  16.1.1  Load-Time and Run-Time Linking
  18522.  
  18523.  Dynamic linking can take place both at program load time and while the
  18524.  program is running. A program can call functions in more than one DLL and
  18525.  combine both load-time and run-time linking.
  18526.  
  18527.   For load-time dynamic linking, build a program that calls DLL functions by
  18528.  name.
  18529.  
  18530.  The linker creates special records containing the name of each DLL
  18531.  subroutine and the name of its DLL file. It does not put any DLL code into
  18532.  the program's executable file. At load time, OS/2 dynamically links the
  18533.  program and its DLLs. It brings the program and the DLLs into memory and
  18534.  updates the program's DLL calls with the address of each DLL routine. If a
  18535.  DLL is already in memory, it is not reloaded.
  18536.  
  18537.  With run-time dynamic linking, the program creates the DLL file name and
  18538.  subroutine names during execution. The program then passes these names to
  18539.  OS/2 so the operating system can load the dynamic-link library.
  18540.  
  18541.  An example of a run-time dynamic link is an extension to the Programmer's
  18542.  WorkBench (PWB). PWB has no information about which extensions it needs
  18543.  until it reads the initialization file, TOOLS.INI. PWB then sends requests
  18544.  to OS/2 to demand-load the DLLs that it needs.
  18545.  
  18546.  
  18547.  16.1.2  Application Programs and DLLs
  18548.  
  18549.  With static linking, all library code is bound into the executable program
  18550.  when you link the program. If the library changes, all programs using the
  18551.  library must be relinked. With the exception of some Microsoft Windows
  18552.  programs, all DOS programs use static linking.
  18553.  
  18554.   Updates to parts of a program are easier to deliver using DLLs.
  18555.  
  18556.  You can create loosely coupled applications and DLLs and modify the DLLs
  18557.  without relinking the program. For example, if your product has an
  18558.  underlying database access mechanism, you can package the database access
  18559.  routines into a DLL. You can then ship improvements or changes to the
  18560.  database code in a new dynamic-link library. The executable files for the
  18561.  program do not have to be relinked or redistributed.
  18562.  
  18563.  The programs calling a DLL are known as the DLL's "clients."
  18564.  
  18565.  
  18566.  16.1.3  DLLs and Microsoft C Run-Time Libraries
  18567.  
  18568.  You can construct three types of dynamic-link libraries with the Microsoft C
  18569.  Professional Development System. All of them can be multithreaded; they can
  18570.  support more than one client at a time. There are three types:
  18571.  
  18572.  
  18573.    ■   A stand-alone dynamic-link library that includes both your routines
  18574.        and code for the Microsoft C run-time library functions used by your
  18575.        DLL. This type of DLL is self-contained and completely independent of
  18576.        the programs that call it.
  18577.  
  18578.    ■   A dynamic-link library that does not use any functions from the
  18579.        Microsoft C run-time library. This type of DLL is also self-contained.
  18580.  
  18581.    ■   A private dynamic-link library that consists only of selected
  18582.        functions from the Microsoft C run-time library. This DLL is usually
  18583.        specific to one program or a closely tied group of programs.
  18584.        Application programs and dynamic-link libraries using this DLL do not
  18585.        contain any code for the C run-time library functions.
  18586.  
  18587.  
  18588.  The following sections provide more information about the differences
  18589.  between the various types of DLLs.
  18590.  
  18591.  
  18592.  Stand-Alone Dynamic-Link Libraries
  18593.  
  18594.   Stand-alone DLLs include C run-time functions.
  18595.  
  18596.  If you want to call C run-time library functions in your DLL, you can
  18597.  include the functions you need. These run-time functions are statically
  18598.  linked in the DLL and the DLL does not rely on the client or any other DLL
  18599.  for run-time support.
  18600.  
  18601.  Figure 16.1 illustrates the relationships between this type of DLL, an
  18602.  application program, and C run-time library functions. Both the application
  18603.  program and the dynamic-link library have their own copies of functions from
  18604.  the C run-time library. This ensures that
  18605.  
  18606.  
  18607.    ■   The DLL always has access to the C run-time library routines it needs.
  18608.  
  18609.    ■   The DLL is not dependent on the calling application for any support
  18610.        code.
  18611.  
  18612.    ■   The programs using the DLL do not depend on the DLL for C run-time
  18613.        library functions.
  18614.  
  18615.  
  18616.  Section 16.3.1, "DLLs with Static C Run-Time Library Functions," describes
  18617.  the steps involved in creating this type of dynamic-link library using the
  18618.  special library LLIBCDLL.LIB.
  18619.  
  18620.  (This figure may be found in the printed book.)
  18621.  
  18622.  
  18623.  DLLs without C Run-Time Library Functions
  18624.  
  18625.  You can write a dynamic-link library in C without calling any functions from
  18626.  the C run-time library. Section 16.3.2, "DLLs without C Run-Time Library
  18627.  Functions," shows how to set up this type of DLL. These DLLs contain only
  18628.  your code and require no run-time library support; they make no calls to
  18629.  run-time library functions.
  18630.  
  18631.  
  18632.  Private C Run-Time DLLs
  18633.  
  18634.   You can create a custom C run-time DLL.
  18635.  
  18636.  A C run-time DLL can be shared by multiple programs and their DLLs. You
  18637.  generate the C run-time DLL in two steps. The first builds a
  18638.  module-definition file with a list of the C run-time library functions
  18639.  needed by your application and its DLLs; the second step links the
  18640.  module-definition file with the special library CDLLOBJS.LIB to create a C
  18641.  run-time DLL.
  18642.  
  18643.  The executable files for programs and DLLs linked with a customized C
  18644.  run-time DLL do not contain any code for the C run-time library functions.
  18645.  Figure 16.2 shows the relationships of the components.
  18646.  
  18647.   A private C run-time DLL must be closely tied to its programs and
  18648.  associated DLLs.
  18649.  
  18650.  Processes and DLLs that share a private run-time DLL share environment
  18651.  strings and global C run-time data (for example, file pointers for buffered
  18652.  I/O and memory allocated with the malloc function). Therefore, the program
  18653.  and the DLLs must cooperate on the use of this data.
  18654.  
  18655.  (This figure may be found in the printed book.)
  18656.  
  18657.  A closely tied structure is suitable for a complex application consisting of
  18658.  a set of application programs that act as front-end processors to several
  18659.  DLLs. A word processor, for example, might support one user interface for
  18660.  beginners, another for intermediate users, and a third for expert users. The
  18661.  different user interfaces could be implemented in three separate executable
  18662.  program files. All three programs would share the DLLs that do most of the
  18663.  real work.
  18664.  
  18665.  Section 16.3.3, "Programs and DLLs with a C Run-Time DLL," describes the
  18666.  procedures for building a C run-time library DLL and its associated programs
  18667.  and dynamic-link libraries.
  18668.  
  18669.  
  18670.  16.2  Designing and Writing DLLs
  18671.  
  18672.  Before you write a DLL, you must determine some of the DLL's requirements.
  18673.  You need to know
  18674.  
  18675.  
  18676.    ■   Floating-point math requirements
  18677.  
  18678.    ■   Special initialization requirements such as allocation of buffers or
  18679.        registration of special termination routines
  18680.  
  18681.    ■   Termination requirements such as clearing semaphores or releasing
  18682.        allocated memory
  18683.  
  18684.    ■   Re-entrancy requirements; if the DLL is to be called by more than one
  18685.        process, it must be re-entrant
  18686.  
  18687.  
  18688.  This section explains how to design a DLL to take these requirements into
  18689.  account.
  18690.  
  18691.  
  18692.  16.2.1  Floating-Point Math Requirements
  18693.  
  18694.  Stand-alone DLLs built with the LLIBCDLL library are independent of the
  18695.  programs calling them. They are "black boxes" that must operate without
  18696.  knowing anything about their client programs and without interfering with
  18697.  their clients.
  18698.  
  18699.  One area of potential conflict for stand-alone DLLs is control of the 80x87
  18700.  math coprocessor. For a DLL to use the 80x87 coprocessor or the emulator
  18701.  floating-point library, the DLL and all of its client programs must agree on
  18702.  which process is going to handle floating-point exceptions and on which
  18703.  process is going to handle emulation if the machine does not have a
  18704.  coprocessor.
  18705.  
  18706.  Floating-point emulation is not possible with a genuinely independent DLL. A
  18707.  stand-alone DLL must use the alternate math library, which ignores the math
  18708.  coprocessor chip. The alternate math library provides the fastest processing
  18709.  available without a coprocessor, but results are not as accurate as those
  18710.  produced by the emulator floating-point library. Because the constraint
  18711.  applies only to the DLL and not to applications, clients of a stand-alone
  18712.  DLL can use any floating-point model. Since the DLL uses the alternate math
  18713.  library, it does not conflict with clients over control of the math
  18714.  coprocessor.
  18715.  
  18716.  In contrast, DLLs and programs using a private C run-time DLL are tightly
  18717.  coupled. This means that the floating-point math option is known when the
  18718.  program is built. Because these programs and DLLs all use the same C runtime
  18719.  functions (unlike the stand-alone DLL and its clients, which may incorporate
  18720.  different C run-time libraries), no contention can arise over control of the
  18721.  math coprocessor. The same floating-point math library is used for the
  18722.  entire application.
  18723.  
  18724.   The only way to use a math coprocessor within a DLL is with a private C
  18725.  run-time DLL.
  18726.  
  18727.  A private C run-time DLL uses the CDLLOBJS library and the emulator
  18728.  floating-point package. The emulator uses the 80x87 math coprocessor if one
  18729.  is installed; otherwise, it emulates the coprocessor. Floating-point
  18730.  emulation produces the most accurate results. There is no conflict over use
  18731.  of the coprocessor, since the C run-time DLL performs all floating-point
  18732.  math. The programs and DLLs calling the C run-time DLL do not have any C
  18733.  run-time library code of their own.
  18734.  
  18735.  
  18736.  16.2.2  Initialization and Termination Requirements
  18737.  
  18738.  When you design a DLL, you must decide if it has special initialization or
  18739.  termination requirements. If the DLL needs to initialize variables or
  18740.  allocate memory buffers when it starts, it needs custom start-up procedures.
  18741.  If the DLL acquires system resources for a client program, the resources
  18742.  must be released when the program completes its processing.
  18743.  
  18744.  
  18745.  Initialization
  18746.  
  18747.  All DLLs built with the Microsoft C run-time libraries must use per-process
  18748.  initialization to set up the C run-time data. Per-process initialization
  18749.  (also known as instance initialization) means that OS/2 calls the DLL's
  18750.  initialization code each time it loads a program linked with the DLL. For
  18751.  most DLLs, the default initialization routine is sufficient, and you do not
  18752.  need to take any other measures.
  18753.  
  18754.   If your DLL has special requirements, you must provide additional start-up
  18755.  processing.
  18756.  
  18757.  The C run-time library initialization function is called each time a new
  18758.  client is attached to the DLL. To override the default initialization, you
  18759.  must link your DLL with one of the following object modules, which are
  18760.  provided with the Microsoft C Professional Development System:
  18761.  
  18762.  File Name                         Description
  18763.  ────────────────────────────────────────────────────────────────────────────
  18764.  DLLINIT.OBJ                       Initialization module for DLLs built
  18765.                                    with LLIBCDLL.LIB and using C run-time
  18766.                                    library code
  18767.  
  18768.  CRTDLL_I.OBJ                      Initialization module for DLLs using a C
  18769.                                    run-time DLL built with CDLLOBJS.LIB
  18770.                                    (replaces CRTDLL.OBJ)
  18771.  
  18772.  In addition, you must declare an entry point for your own DLL initialization
  18773.  function. Your function, or the application program calling your DLL, must
  18774.  initialize the C run-time data by calling the library function C_INIT before
  18775.  any other C run-time library functions are called.
  18776.  
  18777.  The prototype for C_INIT is
  18778.  
  18779.    void _far _pascal C_INIT( void );
  18780.  
  18781.   Designate your initialization function as the DLL's starting point.
  18782.  
  18783.  To have your custom function recognized as the DLL's default initialization
  18784.  routine, it must be the starting point for the DLL. This requires an
  18785.  assembly language file with an END statement naming your function. The
  18786.  sample file, SETENTRY.ASM, in the following example shows the minimum
  18787.  assembler code required for specifying a C language function named
  18788.  SampleInit as the DLL's entry point.
  18789.  
  18790.    ; SETENTRY.ASM
  18791.    extrn _SampleInit:FAR         ;name of C start-up routine
  18792.    end   _SampleInit
  18793.  
  18794.  The following example, SAMPLE.C, shows a simple custom initialization
  18795.  routine that maintains a count of how many clients it is currently serving.
  18796.  Since this example overrides the default dynamic-link library
  18797.  initialization, it must return a nonzero status code to OS/2 to show a
  18798.  successful start-up. If a DLL initialization function returns a status of 0,
  18799.  OS/2 will not load the program using the DLL.
  18800.  
  18801.    /*                    SAMPLE.C                    */
  18802.    void _far _pascal C_INIT( void );
  18803.    int  UserCount = 0;
  18804.  
  18805.    int _export _loadds SampleInit()
  18806.    {
  18807.        UserCount++;        /* increment number of users  */
  18808.        C_INIT();           /* initialize C run-time data */
  18809.        return( 1 );        /* indicate successful start  */
  18810.    }
  18811.  
  18812.    /* code for other DLL functions belongs here */
  18813.  
  18814.  All DLLs must be linked with a module-definition file that contains a
  18815.  LIBRARY statement, such as the following:
  18816.  
  18817.    LIBRARY SAMPLE INITINSTANCE
  18818.  
  18819.  The following commands will create object files from the sample files and
  18820.  link them with DLLINIT.OBJ to make a stand-alone dynamic-link library  named
  18821.  SAMPLE.DLL. The /ML compile option, explained in Section 16.2.6, "Compile
  18822.  Options for Dynamic-Link Libraries," sets the library search record to
  18823.  LLIBCDLL.LIB.
  18824.  
  18825.    MASM /Mx SETENTRY;
  18826.    CL /c /Gs /ML SAMPLE.C
  18827.    LINK /NOE DLLINIT+SETENTRY+SAMPLE,SAMPLE.DLL,,,SAMPLE.DEF;
  18828.  
  18829.  ────────────────────────────────────────────────────────────────────────────
  18830.  WARNING
  18831.  
  18832.  For DLLs linked with Microsoft C run-time libraries, the LIBRARY statement
  18833.  in the DLL's module-definition file must specify INITINSTANCE in the
  18834.  initialization field. If you omit this, the initialization routine is called
  18835.  only when the DLL is loaded into memory for the first client program, and
  18836.  the DLL will not function properly if it is called by additional programs.
  18837.  ────────────────────────────────────────────────────────────────────────────
  18838.  
  18839.  
  18840.  Termination
  18841.  
  18842.   You may have to clean up before terminating.
  18843.  
  18844.  You may need to know when an application using your DLL is finished. If your
  18845.  DLL has created buffers, semaphores, or other resources for a particular
  18846.  application, they must be released when the application terminates.
  18847.  
  18848.  You can have an initialization routine in your DLL that calls the OS/2 API
  18849.  function DosExitList to register one or more exit subroutines for your DLL.
  18850.  OS/2 will call the exit routines when the client program finishes. The exit
  18851.  functions should free any resources your DLL acquired for the client
  18852.  program.
  18853.  
  18854.   DLLs built with LLIBCDLL.LIB have a default termination routine.
  18855.  
  18856.  The start-up routine for dynamic-link libraries built with the LLIBCDLL
  18857.  library calls DosExitList with a pointer to a default termination function.
  18858.  To replace the default processing with your own function, link the module
  18859.  DLLTERM.OBJ into the DLL. This suppresses the call to DosExitList. During
  18860.  initialization, your DLL must register its own routine by calling
  18861.  DosExitList unless you are sure the termination routine will be called
  18862.  explicitly. The termination processing must include a call to the library
  18863.  function C_TERM.
  18864.  
  18865.  The prototype for C_TERM is
  18866.  
  18867.    void _far _pascal C_TERM( void );
  18868.  
  18869.  There is no equivalent to DLLTERM.OBJ and C_TERM for DLLs using a private C
  18870.  run-time DLL built with the CDLLOBJS library. If special cleanup processing
  18871.  is required, these DLLs must provide their own termination function. The
  18872.  function is registered during initialization by calling either the C
  18873.  run-time library function atexit or the OS/2 API function DosExitList.
  18874.  
  18875.   Any DLL that calls DosExitList should also have a termination function.
  18876.  
  18877.  DLLs that set exit lists must provide termination functions that can be
  18878.  called by clients when they no longer need the DLL. If a program attaches
  18879.  itself to the DLL at run-time (using DosLoadModule), it cannot disconnect
  18880.  from the DLL as long as the exit list points to a function in the
  18881.  dynamic-link library. The DLL's termination function can perform any
  18882.  necessary cleanup and call DosExitList to remove itself from the exit list.
  18883.  
  18884.  
  18885.  ────────────────────────────────────────────────────────────────────────────
  18886.  NOTE
  18887.  There is no special termination procedure for DLLs build with CDLLOBJS.LIB
  18888.  because the C run-time termination code is called by the exit or _exit
  18889.  functions. If the process is terminated by a critical error or DosExit, C
  18890.  run-time termination does not occur.
  18891.  ────────────────────────────────────────────────────────────────────────────
  18892.  
  18893.  
  18894.  16.2.3  Making the DLL Re-Entrant
  18895.  
  18896.  Re-entrant code is code that can be shared by multiple programs in a
  18897.  multitasking environment. DLLs that may be used by more than one program
  18898.  must be re-entrant. To do this, they must isolate each client program's data
  18899.  and resources. File handles belonging to one client, for example, must not
  18900.  be used for other clients. Re-entrancy also means that the DLL cannot allow
  18901.  itself to be switched to a different thread while it is performing certain
  18902.  operations.
  18903.  
  18904.  
  18905.  Global Versus Instance Data
  18906.  
  18907.   A dynamic-link library can have separate data segments for each program
  18908.  that calls it.
  18909.  
  18910.  Separate data segments are known as "instance" data. With instance data
  18911.  segments, the DLL does not have to keep track of which resources belong to
  18912.  each client. OS/2 assigns a different data segment to each process calling
  18913.  the DLL, even though the selectors are the same.
  18914.  
  18915.  A dynamic-link library can also have a global data segment used for internal
  18916.  purposes or to support all of the programs using its services.
  18917.  
  18918.  A DLL providing time and date conversions might, for example, keep the
  18919.  current date in a global storage area. The same DLL might provide functions
  18920.  to compute elapsed time, such as the number of minutes between two clock
  18921.  readings. If static variables are used by the elapsed time functions, they
  18922.  should be in instance data segments, since the OS/2 scheduler might preempt
  18923.  the function and schedule another thread that calls the same function with
  18924.  different arguments before it has completed the first caller's task.
  18925.  
  18926.  Data sharing is controlled by DATA and SEGMENTS statements in a dynamic-link
  18927.  library's module-definition file. By default, a DLL's automatic data segment
  18928.  (the local stack and heap) is shared by all processes calling the DLL. You
  18929.  can specify a unique automatic data segment for each client process by
  18930.  specifying DATA MULTIPLE.
  18931.  
  18932.  ────────────────────────────────────────────────────────────────────────────
  18933.  WARNING
  18934.  
  18935.  DLLs built with the LLIBCDLL or CDLLOBJS C run-time libraries must use DATA
  18936.  MULTIPLE in the module-definition file.
  18937.  ────────────────────────────────────────────────────────────────────────────
  18938.  
  18939.   You can use SEGMENTS to specify attributes on a segment-by-segment basis.
  18940.  
  18941.  Using the SEGMENTS statement allows you to have both global and per-process
  18942.  (instance) data in the same DLL. The C run-time data segment must be
  18943.  per-process. The following is an example of a C program fragment and
  18944.  moduledefinition file that implement both instance and global data:
  18945.  
  18946.    /* Define static data in the shared segment SHR_SEG */
  18947.    int _based(_segname("SHR_SEG")) intvar;
  18948.    char _based(_segname("SHR_SEG")) charvar;
  18949.  
  18950.  In the module-definition file, define all data segments as nonshareable,
  18951.  then override that default for  SHR_SEG  as follows:
  18952.  
  18953.    DATA MULTIPLE NONSHARED
  18954.    SEGMENTS
  18955.        SHR_SEG     CLASS   'FAR_DATA'  SHARED
  18956.  
  18957.  Global data segments are created when OS/2 brings the dynamic-link library
  18958.  into memory for its first client process. All of the processes calling the
  18959.  DLL share the same global variables.
  18960.  
  18961.  
  18962.  Serializing Nonatomic References
  18963.  
  18964.  An atomic operation is an operation that can be completed in one machine
  18965.  language instruction. When writing a re-entrant procedure (in a multithread
  18966.  program or in a DLL), you must ensure that changes to static or global data
  18967.  are not preempted by the OS/2 scheduler before the update is complete. To
  18968.  prevent this, you must explicitly serialize nonatomic references to static
  18969.  or global data. The following code example is safe from preemption, because
  18970.  incrementing an integer requires only one machine instruction:
  18971.  
  18972.    int int_var;
  18973.    _export _loadds void _far _pascal dynlink_proc( void )
  18974.    {
  18975.       int_var++;
  18976.    }
  18977.  
  18978.  The following variation on the same function is not safe because
  18979.  incrementing  a long variable is not atomic; it requires two machine
  18980.  instructions. Between incrementing the least-significant word and the
  18981.  most-significant word, another thread could gain control of the processor.
  18982.  If that thread executes code in your DLL that uses  long_var, that data
  18983.  would be in an indeterminate state.
  18984.  
  18985.    long long_var;
  18986.    _export _loadds void _far _pascal dynlink_proc( void )
  18987.    {
  18988.       long_var++;
  18989.    }
  18990.  
  18991.  
  18992.  Critical Code Sections
  18993.  
  18994.  A critical code section is a section of code that manipulates a resource
  18995.  (such as the long variable in the previous example) while blocking all other
  18996.  threads. When your program enters a critical section, it cannot be preempted
  18997.  until it performs a DosExitCritSec or until a signal is received. You don't
  18998.  usually just alter the value of a variable; you alter it and then use it
  18999.  later. In this case, you must isolate the smallest group of operations that
  19000.  must occur without interruption. You define these sections with the
  19001.  DosEnterCritSec and DosExitCritSec OS/2 API functions, as in the following
  19002.  example:
  19003.  
  19004.    _export _loadds void _far _pascal dynlink_proc( void )
  19005.    {   static int_var;
  19006.  
  19007.       DosEnterCritSec();
  19008.       int_var += 7;
  19009.       SetLeftCorner( int_var, int_var );
  19010.       DosExitCritSec();
  19011.  
  19012.    /* Code that does not reference int_var */
  19013.    }
  19014.  
  19015.   Keep your critical sections as short as possible.
  19016.  
  19017.  While in a critical section, all other threads in the process are blocked
  19018.  from execution. Writing extremely long critical sections can make your
  19019.  program inefficient and can degrade system performance.
  19020.  
  19021.  Although other threads are blocked from execution by DosEnterCritSec and
  19022.  DosExitCritSec, these functions do not block signal handling.
  19023.  
  19024.  Note that static variables in DLLs are protected from interference from
  19025.  other processes if they are in an instance data segment designated as
  19026.  MULTIPLE in the DATA statement of the DLL's module-definition file. Memory
  19027.  is "owned" by a process and, unless specifically allocated as shareable,
  19028.  cannot be altered by any other process.
  19029.  
  19030.  
  19031.  16.2.4  Signal Handling
  19032.  
  19033.  The C library function signal is not supported for multithread programs or
  19034.  for DLLs. If you need to process signals, use the OS/2 API signal functions,
  19035.  such as DosSetSigHandler.
  19036.  
  19037.  See Chapter 15, "Creating Multithread OS/2 Applications," for more
  19038.  information about signal handling in OS/2 programs.
  19039.  
  19040.  
  19041.  16.2.5  Using Microsoft C Keywords
  19042.  
  19043.  The _export and _loadds keywords simplify writing DLLs. They are used to
  19044.  define or declare functions or pointers to functions. In the DLL, an
  19045.  exported function with a single argument might be defined as
  19046.  
  19047.    int _export _loadds sample( int )
  19048.  
  19049.  
  19050.  The _export Keyword
  19051.  
  19052.   All DLL functions that will be called from outside the library must be
  19053.  exported.
  19054.  
  19055.  The _export keyword gives a function the export attribute. Stack checking
  19056.  must be disabled for exported entry points. You can use the /Gs compile
  19057.  option or the check_stack pragma to accomplish this.
  19058.  
  19059.  Using the _export keyword is an alternative to declaring the name of the
  19060.  function in the EXPORTS section of a module-definition file. It assigns
  19061.  certain default attributes: no I/O privilege, shared data, load on demand,
  19062.  and no alias name. If the defaults are not acceptable, you must specify the
  19063.  proper attributes in the module-definition file.
  19064.  
  19065.  Not all functions in a DLL are for external use. A DLL can have any number
  19066.  of utility subroutines supporting the work of the exported functions.
  19067.  Functions that are private to the DLL should not have the _export keyword.
  19068.  
  19069.  
  19070.  The _loadds Keyword
  19071.  
  19072.  At entry to a DLL, the DS (data segment) register points to the calling
  19073.  program's data segment. To access the DLL's data, the DS register has to be
  19074.  loaded with the DLL's segment selector. The _loadds keyword causes the
  19075.  compiler to add prolog and epilog code to the function. The prolog code
  19076.  initializes the DS register to point to the function's data group. The
  19077.  epilog code restores the caller's DS register when the function terminates.
  19078.  
  19079.  
  19080.  Since loading the DS register is a high overhead operation, you should limit
  19081.  the use of _loadds to the exported functions in your DLL.
  19082.  
  19083.  ────────────────────────────────────────────────────────────────────────────
  19084.  WARNING
  19085.  
  19086.  Do not use the _loadds keyword in a function definition if the function uses
  19087.  only stack variables. If you specify _loadds in a DLL that does not have any
  19088.  static data, the linker will issue a segment fix-up error.
  19089.  ────────────────────────────────────────────────────────────────────────────
  19090.  
  19091.  
  19092.  16.2.6  Compile Options for Dynamic-Link Libraries
  19093.  
  19094.  Dynamic-link libraries must be compiled with specific options that control
  19095.  linking, memory models, and library selection.
  19096.  
  19097.  
  19098.  Compile without Linking (/c)
  19099.  
  19100.  You must use the /c option to build your DLL in separate compile and link
  19101.  steps. This is necessary because the DLL must be linked with a
  19102.  module-definition file specifying that the output file is a dynamic-link
  19103.  library. (The compiler does not pass module-definition file names to the
  19104.  linker.) The /c option is automatically specified in the makefile generated
  19105.  by the Programmer's WorkBench.
  19106.  
  19107.  
  19108.  Large Memory Model with Separate Stack (/ALw)
  19109.  
  19110.  The /ALw option instructs the compiler to use the large memory model with a
  19111.  separate stack segment. Because all DLLs use the caller's stack, you must
  19112.  use /Aw or /Au. The /Aw option sets up separate stack and data segments but
  19113.  does not cause the DS register to be reloaded at the entry to each function.
  19114.  This allows you to call private functions (functions that you do not export)
  19115.  without incurring the overhead of loading the DS register. Functions that
  19116.  you do export must also be declared using the _loadds keyword, described
  19117.  above, which sets up the proper DS register handling. If you use the /Au
  19118.  option, the DS register will be reloaded on entry to every function, which
  19119.  can cause the function calls in your DLLs to execute more slowly.
  19120.  
  19121.  All DLL functions are reached using far calls. Pointers passed to and from
  19122.  the DLL must be far pointers.
  19123.  
  19124.  
  19125.  Remove Stack Probes (/Gs)
  19126.  
  19127.  Since the DLL uses the caller's stack, you should usually use the /Gs option
  19128.  to disable stack checking within the DLL.
  19129.  
  19130.  
  19131.  Specify 80286 Code (/G2)
  19132.  
  19133.  Use the /G2 option to designate code generation for the 80286 processor
  19134.  instruction set, since OS/2 runs only on 80286 and higher model processors.
  19135.  
  19136.  
  19137.  
  19138.  Link C Run-Time into Stand-Alone DLL (/ML)
  19139.  
  19140.  Use the /ML option to build a stand-alone dynamic-link library that includes
  19141.  static code for C run-time library functions. This option has the same
  19142.  effect as using the /ALw, /FPa, /G2, and /D MT options. It changes the
  19143.  library search record to LLIBCDLL.LIB. See Section 16.3.1, "DLLs with Static
  19144.  C Run-Time Library Functions" for more information about these options.
  19145.  
  19146.  
  19147.  Link Executable or DLL with C Run-Time DLL (/MD)
  19148.  
  19149.  Use the /MD option to build an executable file or a dynamic-link library
  19150.  that calls a C run-time DLL. This option has the same effect as using the
  19151.  /ALw, /FPi, /G2, /D DLL, and /D MT options. It inhibits library search
  19152.  records. See Section 16.3.3, "Programs and DLLs with a C Run-Time DLL," for
  19153.  more information about these options.
  19154.  
  19155.  
  19156.  Suppress Default Library Selection (/Zl)
  19157.  
  19158.  If you do not compile with the /MD or /ML options described above, compile
  19159.  with the /Zl option or use the /NOD option when you link in order to inhibit
  19160.  searches for default libraries.
  19161.  
  19162.  
  19163.  16.3  Building DLLs with Microsoft C
  19164.  
  19165.   Building a DLL for OS/2 is like building an executable program file.
  19166.  
  19167.  To build a DLL, compile and link the dynamic-link library like any other
  19168.  executable file, but add a module-definition file. This module-definition
  19169.  file tells the linker that the output is a dynamic-link library.
  19170.  
  19171.  When you build applications that use a dynamic-link library, you must tell
  19172.  the linker where to find the library's dynamically linked functions. You use
  19173.  import libraries and module-definition files for this purpose.
  19174.  
  19175.  
  19176.  16.3.1  DLLs with Static C Run-Time Library Functions
  19177.  
  19178.  The LLIBCDLL library is used to create stand-alone DLLs. The library
  19179.  functions are re-entrant and can be called by multiple threads within a
  19180.  program as well as by multiple programs. The code for the stand-alone DLL's
  19181.  C run-time library functions is contained within the DLL. Programs that call
  19182.  stand-alone DLLs have their own run-time library code.
  19183.  
  19184.  
  19185.  Building the DLL
  19186.  
  19187.  The files required to build a stand-alone DLL with the LLIBCDLL library are
  19188.  listed below:
  19189.  
  19190.  File Name                         Description
  19191.  ────────────────────────────────────────────────────────────────────────────
  19192.  OS2.LIB                           OS/2 kernel import library
  19193.  
  19194.  LLIBCDLL.LIB                      Large-model multithread C run-time
  19195.                                    library for DLLs
  19196.  
  19197.  DLLINIT.OBJ                       Optional initialization module for DLLs
  19198.                                    requiring custom initialization
  19199.  
  19200.  DLLTERM.OBJ                       Optional termination module for DLLs
  19201.                                    requiring custom exit processing
  19202.  
  19203.  userdll.C                         Source code for the DLL you create
  19204.  
  19205.  userdll.DEF                       Module-definition file for the DLL you
  19206.                                    create
  19207.  
  19208.  The module JUSTIFY.C, below, is an example of source code for a simple
  19209.  dynamic-link library. The RightJustify routine calls the strlen function
  19210.  from the C run-time library and right-justifies a caller's buffer. The
  19211.  function definition includes the _export keyword. The _loadds keyword is
  19212.  omitted, since this function does not need any static data. If it did, you
  19213.  would need to specify _loadds.
  19214.  
  19215.  For simplicity, JUSTIFY.C below shows a DLL with a single function. In
  19216.  actual practice, you would usually package a group of similar utilities into
  19217.  one DLL.
  19218.  
  19219.    /* JUSTIFY.C -- Sample Dynamic-Link Library */
  19220.  
  19221.    #include <string.h>
  19222.  
  19223.    /* Right justifies the string in TargetBuff to TargetSize
  19224.     * and inserts necessary number of FillChars on the left.
  19225.     */
  19226.  
  19227.    #pragma stack_check(off)
  19228.  
  19229.    int _export RightJustify( char *TargetBuff, int TargetSize,
  19230.                              char FillChar)
  19231.    {
  19232.       char *s, *d;
  19233.        s = TargetBuff + strlen( TargetBuff );
  19234.       d = TargetBuff + TargetSize;
  19235.       while ( s = TargetBuff )
  19236.           *d-- = *s--;
  19237.       while ( d = TargetBuff )
  19238.           *d-- = FillChar;
  19239.  
  19240.       return( 0 );
  19241.    }
  19242.  
  19243.  The steps for creating a stand-alone dynamic-link library with JUSTIFY.C are
  19244.  given below. The DLL in the example is named JUSTLIB1.DLL.
  19245.  
  19246.  
  19247.    ■   Compile with the /ML Option.
  19248.  
  19249.        Compile the source file without linking. Dynamic-link libraries linked
  19250.        with LLIBCDLL must be compiled with specific options.
  19251.  
  19252.        Use the /ML option to set the library search record to LLIBCDLL.LIB
  19253.        and to indicate that C run-time code is to be included in the DLL.
  19254.        When you use /ML, the following options take effect:
  19255.  
  19256.  Option                            Effect
  19257.  ────────────────────────────────────────────────────────────────────────────
  19258.  
  19259.  /ALw                              Use large memory model with separate
  19260.                                    stack
  19261.                                    segment
  19262.  
  19263.  /G2                               Use 80286 processor instruction set
  19264.  
  19265.  /D MT                             Use the multithread version of the
  19266.                                    include files
  19267.  
  19268.  /FPa                              Generate floating-point calls and select
  19269.                                    the alternate math library
  19270.  
  19271.  The /G2 and the /ALw options can be overridden.
  19272.  
  19273.        You should also use the /Gs option to suppress stack checking and the
  19274.        /c option to compile without linking. The complete command to compile
  19275.        the sample file JUSTIFY.C is
  19276.  
  19277.        CL /ML /Gs /c JUSTIFY.C
  19278.  
  19279.    ■   Create a module-definition file.
  19280.  
  19281.        Create a module-definition file, JUSTLIB1.DEF, which includes the
  19282.        following lines:
  19283.  
  19284.        LIBRARY JUSTLIB1 INITINSTANCE
  19285.        DATA MULTIPLE
  19286.  
  19287.        The LIBRARY statement identifies the executable file, JUSTLIB1.DLL, as
  19288.        a dynamic-link library. DLLs linked with the LLIBCDLL library must
  19289.        specify INITINSTANCE in the initialization field. You could add an
  19290.        EXPORTS statement for the RightJustify function in JUSTIFY.C, but it
  19291.        is optional since the _export keyword was used in the source code.
  19292.  
  19293.        See Chapter 14, "Building OS/2 Applications," for more information
  19294.        about module-definition files.
  19295.  
  19296.    ■   Link with LLIBCDLL.LIB.
  19297.  
  19298.        Ensure that the file LLIBCDLL.LIB, which takes the place of the
  19299.        regular C run-time library, is available.
  19300.  
  19301.        Create JUSTLIB1.DLL with a command such as
  19302.  
  19303.        LINK justify,justlib1.dll,,,justlib1.def/NOI
  19304.  ────────────────────────────────────────────────────────────────────────────
  19305.  WARNING
  19306.  
  19307.  When you link with LLIBCDLL, you cannot have any other C run-time libraries
  19308.  in the link.
  19309.  ────────────────────────────────────────────────────────────────────────────
  19310.  
  19311.  
  19312.    ■   Create an import library.
  19313.  
  19314.        Applications that call DLLs use import libraries to identify DLL
  19315.        functions to the linker. The following example uses JUSTLIB1.DLL and
  19316.        the IMPLIB utility to create an import library named JUSTLIB1.LIB.
  19317.  
  19318.        IMPLIB justlib1.lib justlib1.dll
  19319.  
  19320.        For more information about import libraries, see Chapter 14, "Building
  19321.        OS/2 Applications."
  19322.  
  19323.  
  19324.  
  19325.  Building Programs that Call the DLL
  19326.  
  19327.  To link a dynamic-link library with an application, you must have one of the
  19328.  following:
  19329.  
  19330.  
  19331.    ■   A module-definition file with an IMPORTS statement for each DLL
  19332.        function called by your program
  19333.  
  19334.    ■   An import library created from the DLL itself or from a
  19335.        module-definition file
  19336.  
  19337.  
  19338.  All calls to DLLs must be far calls; all pointers passed must be far data
  19339.  pointers. If you do not compile with the large memory model option (/AL),
  19340.  you must cast the DLL function calls and pointers yourself.
  19341.  
  19342.  The sample file below, TESTJUST.C, is compiled and linked into a small-model
  19343.  program named SAMPLE1.EXE. TESTJUST.C includes a function prototype that
  19344.  declares RightJustify as a far function expecting a far pointer as its first
  19345.  argument. Because of the prototype, the compiler will generate a far call to
  19346.  RightJustify and coerce the pointer argument to the proper value.
  19347.  
  19348.    /* TESTJUST.C. Call sample DLL library */
  19349.  
  19350.    #include <stdio.h>
  19351.    #include <string.h>
  19352.  
  19353.    /* DLL function prototype */
  19354.  
  19355.    int _far RightJustify( char _far *, int, char );
  19356.  
  19357.    void main( void )
  19358.    {
  19359.        char buff[12];
  19360.  
  19361.        strcpy( buff, "ABCD" );
  19362.  
  19363.        /* Right justify to 8 characters and zero fill. */
  19364.        RightJustify( buff, 8, '0' );
  19365.        printf( "Result: %s\n", buff );
  19366.    }
  19367.  
  19368.  You need several files to link an application with a stand-alone DLL:
  19369.  
  19370.  File Name                         Description
  19371.  ────────────────────────────────────────────────────────────────────────────
  19372.  userdll.LIB                       Import library file for the DLL
  19373.  
  19374.  userapp.DEF                       Optional module-definition file for your
  19375.                                    application that contains an IMPORTS
  19376.                                    statement for each DLL function called
  19377.                                    (required if not using an import
  19378.                                    library)
  19379.  
  19380.  OS2.LIB                           Optional import library file for the
  19381.                                    OS/2 kernel (required if your
  19382.                                    application calls the kernel directly or
  19383.                                    via a C run-time library function)
  19384.  
  19385.  userapp.OBJ                       Object module(s) for your application
  19386.  
  19387.  mLIBC f P.LIB                     Regular C run-time library for protected
  19388.                                    mode, where m indicates memory model (S,
  19389.                                    C, M, L) and
  19390.                                    f indicates math package (A, E, 7)
  19391.  
  19392.  The following command lines illustrate how TESTJUST.C can be compiled  and
  19393.  linked with the standard libraries, plus the sample dynamic-link library,
  19394.  JUSTLIB1.DLL. The example uses the small memory model library and the
  19395.  JUSTLIB1.LIB import library created from JUSTLIB1.DLL to create SAMPLE1.EXE.
  19396.  
  19397.  
  19398.    CL /AS /G2 /c TESTJUST.C
  19399.    LINK TESTJUST,SAMPLE1.EXE,,JUSTLIB1;
  19400.  
  19401.  Make sure that the JUSTLIB1.DLL file is in a directory on your LIBPATH
  19402.  before executing SAMPLE1.EXE.
  19403.  
  19404.  
  19405.  16.3.2  DLLs without C Run-Time Library Functions
  19406.  
  19407.  Building a DLL that does not call any of the C run-time library functions is
  19408.  similar to creating a stand-alone DLL.
  19409.  
  19410.  To use the JUSTIFY.C sample program shown in Section 16.3.1, "DLLs with
  19411.  Static C Run-Time Library Functions," without calling C run-time functions,
  19412.  one change must be made. You must remove the call to the C run-time library
  19413.  function strlen. The strlen function was used in the sample program to
  19414.  calculate a pointer to the end of the caller's buffer. Remove the following
  19415.  line in the program JUSTIFY.C:
  19416.  
  19417.    s = TargetBuff + strlen( TargetBuff );
  19418.  
  19419.  Replace the line above with the following code fragment, which does the same
  19420.  thing without calling strlen:
  19421.  
  19422.    s = TargetBuff;
  19423.    while ( *s )
  19424.       s++;
  19425.  
  19426.  After making this change, you can use the following commands to create a DLL
  19427.  named JUSTLIB2.DLL and its import library:
  19428.  
  19429.    CL /c /ALw /G2s /Zl JUSTIFY.C
  19430.    LINK JUSTIFY,JUSTLIB2.DLL,,,JUSTLIB2.DEF/NOI
  19431.    IMPLIB JUSTLIB2.LIB JUSTLIB2.DLL
  19432.  
  19433.  Note that object modules compiled with releases of Microsoft C prior to
  19434.  Version 6.0 refer to the C run-time library variable _acrtused. C 6.0
  19435.  defines this variable if the main function is present. This causes the
  19436.  linker to automatically add the C run-time start-up module to the DLL. To
  19437.  suppress the start-up module, your source file must include a line defining
  19438.  _acrtused as follows:
  19439.  
  19440.    int _acrtused = 0;
  19441.  
  19442.  This is required only if you do not use a C run-time library and if the link
  19443.  includes object modules built with earlier versions of the compiler.
  19444.  
  19445.  
  19446.  16.3.3  Programs and DLLs with a C Run-Time DLL
  19447.  
  19448.  The CDLLOBJS.LIB and CDLLOBJS.DEF files are the foundation for building a
  19449.  DLL that consists only of C run-time library functions. The application
  19450.  programs and optional dynamic-link libraries linked with this DLL do not
  19451.  contain any C run-time library code.
  19452.  
  19453.  You create an application to use the C run-time DLL in either two or three
  19454.  phases, depending on whether or not the application has additional DLLs:
  19455.  
  19456.  
  19457.    ■   Build a C run-time DLL.
  19458.  
  19459.    ■   Build any optional DLLs that use the C run-time DLL.
  19460.  
  19461.    ■   Compile and link the application.
  19462.  
  19463.  
  19464.  The examples in this section use the JUSTIFY.C and TESTJUST.C source files
  19465.  shown in Section 16.3.1, "DLLs with Static C Run-Time Library Functions."
  19466.  
  19467.  
  19468.  Building a C Run-Time DLL
  19469.  
  19470.  The C run-time DLL is derived from the CDLLOBJS.LIB and CDLLOBJS.DEF files
  19471.  provided with the Microsoft C Professional Development System. The
  19472.  CDLLOBJS.DEF file includes export definitions for all of the C run-time
  19473.  library functions.
  19474.  
  19475.  The steps for creating a C run-time DLL are given below. The C run-time DLL
  19476.  in the example is named CEXAMPLE.DLL.
  19477.  
  19478.  
  19479.    1.  Create a module-definition file.
  19480.  
  19481.        You can use CDLLOBJS.DEF as the basis for your own module-definition
  19482.        file by copying and editing it. This allows you to create a customized
  19483.        DLL that contains only the functions your application requires. If you
  19484.        use the CDLLOBJS.DEF file without modification, every program that
  19485.        links to your C run-time DLL will get the entire C run-time library.
  19486.  
  19487.        The following examples create the sample file CEXAMPLE.DEF to define
  19488.        the custom dynamic link library CEXAMPLE.DLL. The CEXAMPLE.DEF file,
  19489.        shown below, exports the three C run-time library functions called
  19490.        from JUSTIFY.C and TESTJUST.C. It also exports functions required by
  19491.        the C run-time library start-up modules.
  19492.  
  19493.        LIBRARY CEXAMPLE INITINSTANCE
  19494.        DESCRIPTION 'Sample Dynamic-link C Run-Time Library'
  19495.        DATA MULTIPLE
  19496.        PROTMODE
  19497.        EXPORTS
  19498.               _printf
  19499.               _strlen
  19500.               _strcpy
  19501.               __CRT_INIT
  19502.               __aFchkstk
  19503.               _exit
  19504.  
  19505.    2.  Create the C run-time DLL.
  19506.  
  19507.        The files for creating a C run-time DLL are listed below:
  19508.  
  19509.  File Name                         Description
  19510.  ────────────────────────────────────────────────────────────────────────────
  19511.  
  19512.  OS2.LIB                           Import library for the OS/2 kernel
  19513.  
  19514.  CDLLOBJS.LIB                      Dynamic link C run-time library
  19515.  
  19516.  CRTLIB.OBJ                        Start-up code for C run-time DLL
  19517.  
  19518.  yourclib.DEF                      Module-definition file specifying C
  19519.                                    run-time library functions for the DLL
  19520.  
  19521.  The command to create the sample CEXAMPLE.DLL file is
  19522.  
  19523.  
  19524.        LINK /NOD /NOE /NOI crtlib.obj,cexample.dll,,cdllobjs+os2,cexample.def
  19525.  
  19526.    3.  Create an import library.
  19527.  
  19528.        You need to create a library file of import definitions that can be
  19529.        used by programs that will be linked with your custom DLL. This is a
  19530.        two-step process. The first phase uses the module-definition file and
  19531.        the IMPLIB utility to create an interim version of the library, as in
  19532.        this example:
  19533.  
  19534.        IMPLIB cexample.lib cexample.def
  19535.  
  19536.        Note that the IMPLIB utility accepts either a module-definition file
  19537.        or a DLL as input.
  19538.  
  19539.        The second step uses the LIB utility to append the file CDLLSUPP.LIB
  19540.        to the import library. You must append CDLLSUPP.LIB because it
  19541.        contains some routines that cannot be dynamically linked. The LIB
  19542.        utility requires the full path name for CDLLSUPP.LIB. If it is in a
  19543.        directory named C:\ LIB, the command to complete the library build for
  19544.        CEXAMPLE.LIB is
  19545.  
  19546.        LIB CEXAMPLE.LIB+C:\LIB\CDLLSUPP.LIB;
  19547.  
  19548.        When you have finished building the custom DLL, be sure to copy it to
  19549.        a directory specified in the LIBPATH statement of the CONFIG.SYS file.
  19550.  
  19551.  
  19552.  
  19553.  Building an Application-Specific DLL
  19554.  
  19555.  You must compile a DLL that calls a C run-time DLL with specific options
  19556.  and link it with the C run-time DLL's import library. The steps for building
  19557.  an application-specific DLL named JUSTLIB3.DLL are given below.
  19558.  
  19559.  
  19560.    1.  Compile with the /MD option.
  19561.  
  19562.        The easiest way to be sure you choose the proper options is to use the
  19563.        /MD switch, which indicates that the DLL will be used with a C
  19564.        run-time DLL. When you use /MD, library search records are suppressed
  19565.        and the following options are in effect:
  19566.  
  19567.  Option                            Effect
  19568.  ────────────────────────────────────────────────────────────────────────────
  19569.  
  19570.  /ALw                              Use large memory model with separate
  19571.                                    stack
  19572.                                    segment
  19573.  
  19574.  /G2                               Use 80286 processor instruction set
  19575.  
  19576.  /D MT                             Use the multithread version of the
  19577.                                    include files
  19578.  
  19579.  /D DLL                            Use a C run-time dynamic-link library
  19580.  
  19581.  /FPi                              Generate in-line floating-point
  19582.                                    instructions and select the emulator
  19583.                                    math package
  19584.  
  19585.  The /G2 and /ALw options can be overridden. The FPi option can be replaced
  19586.  with /FPi87 or /FPc, but not with /FPa. See Chapter 4, "Controlling
  19587.  Floating-Point Math Operations," for more information about compatible
  19588.  floating-point options.
  19589.  
  19590.        You should also use the /c option to compile without linking. The
  19591.        command line to compile the sample file JUSTIFY.C is
  19592.  
  19593.        CL /MD /c JUSTIFY.C
  19594.  
  19595.    2.  Create a module-definition file.
  19596.  
  19597.        Create a module-definition file named JUSTLIB3.DEF that includes the
  19598.        following line:
  19599.  
  19600.        LIBRARY JUSTLIB3 INITINSTANCE
  19601.  
  19602.    3.  Link the DLL with the C run-time and OS/2 import libraries.
  19603.  
  19604.        To create a DLL that will call a C run-time DLL, the following files
  19605.        must be linked together:
  19606.  
  19607.  File Name                         Description
  19608.  ────────────────────────────────────────────────────────────────────────────
  19609.  
  19610.  OS2.LIB                           Import library for the OS/2 kernel
  19611.  
  19612.  yourclib.LIB                      Import library for your C run-time DLL
  19613.  
  19614.  CRTDLL.OBJ                        Start-up code for DLLs using a C
  19615.                                    run-time DLL
  19616.  
  19617.  CRTDLL_I.OBJ                      Optional initialization module for DLLs
  19618.                                    requiring custom initialization
  19619.                                    (replaces CRTDLL.OBJ)
  19620.  
  19621.  yourdll.OBJ                       Object file for your DLL
  19622.  
  19623.  yourdll.DEF                       Module-definition file for your DLL
  19624.  
  19625.  The command for linking these files to create JUSTLIB3.DLL is
  19626.  
  19627.  
  19628.        LINK justify+crtdll,justlib3.dll,,cexample+os2,justlib3.def
  19629.  
  19630.    4.  Create an import library.
  19631.  
  19632.        Use JUSTLIB3.DLL and the IMPLIB utility to create an import library
  19633.        file, JUSTLIB3.LIB, for use by applications calling JUSTLIB3.DLL:
  19634.  
  19635.        IMPLIB JUSTLIB3.LIB JUSTLIB3.DLL
  19636.  
  19637.        Remember to copy JUSTLIB3.DLL to a directory named in the LIBPATH
  19638.        statement in the CONFIG.SYS file.
  19639.  
  19640.  
  19641.  
  19642.  Using C Run-Time and Application-Specific DLLs
  19643.  
  19644.  Application programs using a C run-time DLL, such as the sample program
  19645.  CEXAMPLE.DLL (described earilier in this section), must define the symbolic
  19646.  constants MT and DLL. These constants cause the compiler to use the
  19647.  multithread and DLL sections of the include files. You can define the
  19648.  constants in your source code or with the compiler's /D command-line option.
  19649.  Since the C run-time DLL uses the large memory model, your program must
  19650.  either use the same model or declare all C run-time functions and pointers
  19651.  passed to them as _far. If you use the standard include files for the C
  19652.  run-time functions in your program, all these declarations are made for you.
  19653.  
  19654.  
  19655.  The following files are required to link an application that calls a C
  19656.  runtime DLL:
  19657.  
  19658.  File Name                         Description
  19659.  ────────────────────────────────────────────────────────────────────────────
  19660.  OS2.LIB                           Import library for the OS/2 kernel
  19661.  
  19662.  yourclib.LIB                      Import library for your C run-time DLL
  19663.  
  19664.  yourdll.LIB                       Import library for each optional
  19665.                                    application DLL
  19666.  
  19667.  CRTEXE.OBJ                        Start-up code for executable files
  19668.                                    calling a C run-time DLL
  19669.  
  19670.  yourapp.OBJ                       Object file(s) for your application
  19671.  
  19672.  yourapp.DEF                       Optional module-definition file for your
  19673.                                    application
  19674.  
  19675.  The following commands compile and link the TESTJUST.C file from Section
  19676.  16.3.1 for use with the dynamic-link libraries CEXAMPLE.DLL and
  19677.  JUSTLIB3.DLL. The link command uses the /NOD option to suppress selection of
  19678.  the standard large-model library. The result is a program named SAMPLE2.EXE.
  19679.  
  19680.  
  19681.    CL /AL /D MT /D DLL /G2 /c TESTJUST.C
  19682.    LINK /NOD TESTJUST+CRTEXE,SAMPLE2.EXE,,CEXAMPLE+OS2+JUSTLIB3;
  19683.  
  19684.  
  19685.  16.3.4  Using CodeView to Debug Dynamic-Link Libraries
  19686.  
  19687.  The protected-mode version of CodeView (CVP) supports debugging of
  19688.  dynamic-link libraries. The /L option lets you name one or more DLLs to be
  19689.  debugged with your application.
  19690.  
  19691.  To enable full symbolic debugging, use the CodeView options /Zi when
  19692.  compiling and /CO when linking. Do this for both the DLL to be debugged and
  19693.  for the program that calls the DLL.
  19694.  
  19695.  The syntax for the /L CodeView option is
  19696.  
  19697.    /L file
  19698.  
  19699.  At least one space must separate /L from the file name(s). You can enter
  19700.  multiple DLL names. To debug the JUSTLIB3.DLL dynamic-link library and the
  19701.  SAMPLE2.EXE program discussed in the previous section, use this command
  19702.  line:
  19703.  
  19704.    CVP /L JUSTLIB3.DLL SAMPLE2.EXE
  19705.  
  19706.   Use the CodeView Trace command (F8) to enter and view DLL code.
  19707.  
  19708.  A simple way to use CodeView is to place a breakpoint at the instruction
  19709.  that calls the DLL function you want to debug. When you reach the
  19710.  breakpoint, press F8 to execute the current source line. CodeView will then
  19711.  display the DLL function's source code, allowing you to set additional
  19712.  breakpoints and enter other CodeView commands.
  19713.  
  19714.  
  19715.  
  19716.  
  19717.  
  19718.  
  19719.  Appendix A  Using Exit Codes
  19720.  ────────────────────────────────────────────────────────────────────────────
  19721.  
  19722.  When C programs terminate, they return values to the process that started
  19723.  them. These values are called "exit codes." The process that starts a C
  19724.  program can be either an operating system, such as DOS or OS/2, or another
  19725.  program. The process that starts the C program is referred to as the "parent
  19726.  process"; the program started is referred to as the "child process." The
  19727.  parent process can interpret return values as an error code sent to the
  19728.  operating system or use those return values as a form of interprocess
  19729.  communication (communication between two separate processes).
  19730.  
  19731.  
  19732.  A.1  The exit Function
  19733.  
  19734.  The exit function terminates execution of your C program and returns an exit
  19735.  code (an integer value) to the parent process. The parent process can be the
  19736.  operating system or another program, depending on how the child process was
  19737.  executed. Note that a C program always returns an integer, regardless of how
  19738.  you declare the main function.
  19739.  
  19740.  Most programs use exit codes to communicate errors to the parent process;
  19741.  these are called "error codes." By convention, programs return zero if they
  19742.  complete normally and a nonzero value if they are exiting because of an
  19743.  error. This error code (the nonzero value) can then be used by the operating
  19744.  system to control the execution of other programs (for example, from inside
  19745.  a batch file).
  19746.  
  19747.  The Microsoft C compiler is a good example of a program that returns an exit
  19748.  code. It returns 0 if no errors occur in your compile and a positive value
  19749.  if an error occurs during compilation.
  19750.  
  19751.  The following program attempts to open a file for reading. If the file
  19752.  cannot be opened, exit returns 1 to the calling program. Therefore, 1 and 0
  19753.  are both exit codes.
  19754.  
  19755.    #include <stdio.h>
  19756.  
  19757.    int main(void)
  19758.    {
  19759.       FILE * fp;
  19760.  
  19761.       if( !(fp = fopen( filename, "rb" )) )
  19762.       {
  19763.           printf("Error %d: Could not open file\n", errno);
  19764.           exit(1);
  19765.       }
  19766.  
  19767.       do_file_access(fp);
  19768.    }
  19769.  
  19770.  In the preceding example, the exit code is unpredictable because the exit
  19771.  function is not used. The value actually returned to the parent process (or
  19772.  to the operating system shell) is whatever happens to be in the AX register
  19773.  when the program terminates─in this case, whatever  do_file_access
  19774.  returned.
  19775.  
  19776.  
  19777.  A.2  Testing Exit Codes from Command and Batch Files
  19778.  
  19779.  Using the IF ERRORLEVEL command, you can test to see if a program has
  19780.  executed successfully by checking its exit code. The IF ERRORLEVEL command
  19781.  is an OS/2 command file or DOS batch file command that tests the exit code
  19782.  of the most recently executed program.
  19783.  
  19784.  IF ERRORLEVEL can help you organize program execution. For example, you can
  19785.  define program execution to be dependent on the successful exit code testing
  19786.  of earlier programs by IF ERRORLEVEL. You can also use the value of the exit
  19787.  code to branch to different commands in a batch or command file.
  19788.  
  19789.  When placed in a batch or command file, the following commands will execute
  19790.  REPORTS.EXE only if FILEMNG.EXE does not return an error:
  19791.  
  19792.    echo Running file manager....
  19793.    FILEMNG.EXE
  19794.    IF NOT ERRORLEVEL 1 REPORTS.EXE
  19795.  
  19796.  Despite the name  ERRORLEVEL, the exit code does not always denote an error.
  19797.  You can define error codes to communicate any information useful to you.
  19798.  
  19799.  Refer to the Microsoft Operating System/2 User's Guide or the Microsoft
  19800.  MS-DOS User's Guide and User's Reference for more information about the IF
  19801.  ERRORLEVEL command.
  19802.  
  19803.  
  19804.  A.3  Accessing Exit Codes from Other Programs
  19805.  
  19806.  When you use any of the spawn family of functions to run a program as the
  19807.  child of another program, the return value of spawn is the exit code of the
  19808.  function. The following code performs the same function as the batch file in
  19809.  Section A.2:
  19810.  
  19811.    void main( void )
  19812.    {
  19813.       if( !spawnl( P_WAIT, "filemng.exe", "filemng.exe",
  19814.                     NULL ) )
  19815.           spawnl( P_WAIT, "reports.exe", "reports.exe",
  19816.                    NULL );
  19817.    }
  19818.  
  19819.  The program  reports.exe  is executed only if the program  filemng.exe
  19820.  terminates with an exit code of 0.
  19821.  
  19822.  The following code uses the exit code as part of a simple menu system:
  19823.  
  19824.    void main(void)
  19825.    {
  19826.       int option;
  19827.       int menu_num = 0;  /* Initialize for first execution */
  19828.  
  19829.       while( (option = spawnl( P_WAIT, "menu.exe",
  19830.                            "menu.exe", menu_num, NULL )) )
  19831.        {
  19832.            switch( option )
  19833.            {
  19834.                case 1 :
  19835.                    menu_num = spawnl( P_WAIT, "program1.exe",
  19836.                                  "program1.exe", NULL );
  19837.                    break;
  19838.                case 2 :
  19839.                    menu_num = spawnl( P_WAIT, "program2.exe",
  19840.                                   "program2.exe", NULL );
  19841.                    break;
  19842.                case 3 :
  19843.                    menu_num = spawnl( P_WAIT, "program3.exe",
  19844.                                   "program3.exe", NULL );
  19845.                   break;
  19846.               default:       /* Guard against a bad option */
  19847.                   break;
  19848.           }
  19849.        }
  19850.    }
  19851.  
  19852.  The preceding example demonstrates how you could have a program,  menu.exe,
  19853.  that solicits input from a menu of choices. This input is interpreted and
  19854.  passed back to the main program in the form of an exit code. (The spawnl
  19855.  function returns the value of the child process's exit code.) This exit code
  19856.  value is stored in  option, which is used as a selector variable in a switch
  19857.  statement.
  19858.  
  19859.  Based on the value returned from  menu.exe, the main program executes
  19860.  program1.exe,  program2.exe, or  program3.exe. Finally,  menu_num, the exit
  19861.  code of the program selected, is used as a parameter to the next execution
  19862.  of  menu.exe.
  19863.  
  19864.  
  19865.  
  19866.  
  19867.  
  19868.  
  19869.  Appendix B  Differences between C Versions 5.1 and 6.0
  19870.  ────────────────────────────────────────────────────────────────────────────
  19871.  
  19872.  This appendix describes the differences between versions 5.1 and 6.0 of
  19873.  Microsoft C, including additions, deletions, and changes. Some of the
  19874.  changes are required by the American National Standards Institute (ANSI)
  19875.  draft standard for the C programming language. Other changes improve or
  19876.  augment the existing capabilities of the compiler.
  19877.  
  19878.  Many of the changes will have no effect on code that was written and
  19879.  compiled with previous versions of Microsoft C. In some cases, however, you
  19880.  may have to modify or correct existing code before compiling with version
  19881.  6.0.
  19882.  
  19883.  
  19884.  B.1  Modifications for ANSI Compatibility
  19885.  
  19886.  A number of changes have been made to the compiler to support the ANSI draft
  19887.  standard. These include new features (Section B.1.1) and changes (Sections
  19888.  B.1.2 - B.1.8).
  19889.  
  19890.  
  19891.  B.1.1  ANSI-Mandated New Features
  19892.  
  19893.  The following ANSI-mandated features are new to version 6.0:
  19894.  
  19895.  
  19896.    ■   The semantics for volatile have been implemented.
  19897.  
  19898.    ■   Both long and unsigned long values are allowed in switch expressions
  19899.        and case constants.
  19900.  
  19901.    ■   The compiler supports unsigned long decimal constants. It is now
  19902.        possible to initialize unsigned long variables with values larger than
  19903.        MAX_LONG using decimal (rather than hexadecimal or octal) constants.
  19904.  
  19905.    ■   Bit fields are permitted in unions.
  19906.  
  19907.    ■   The address-of operator (&) works correctly on arrays and functions.
  19908.  
  19909.    ■   Storage classes or types (or both) are now required on variable
  19910.        declarations. The compiler previously assumed that untyped variables
  19911.        (such as  a;) were integers. This declaration now generates a warning.
  19912.  
  19913.    ■   The LOCALE.H header file is new to version 6.0. It declares functions
  19914.        and structures for describing conventions that vary from one country
  19915.        to the next, such as the currency symbol and the way calendar dates
  19916.        are printed.
  19917.  
  19918.  
  19919.  
  19920.  B.1.2  Integer Promotion Rules
  19921.  
  19922.  The ANSI draft standard requires a change in the evaluation of some
  19923.  expressions that mix signed and unsigned integers. Earlier versions of the
  19924.  compiler attempted to preserve an expression's unsigned nature as much as
  19925.  possible. Version 6.0 attempts to preserve the expression's value.
  19926.  
  19927.  In version 5.1, an unsigned char promotes to an unsigned int; an unsigned
  19928.  int promotes to an unsigned long.
  19929.  
  19930.  In version 6.0, an unsigned char promotes to a signed int; an unsigned int
  19931.  promotes to a signed long.
  19932.  
  19933.  For example,
  19934.  
  19935.    main()
  19936.    {
  19937.      long int li = -256L;
  19938.      test( li );
  19939.    }
  19940.  
  19941.    test( long li)
  19942.    {
  19943.      if( li < 0xffff )
  19944.         puts( "C 6.0 does a signed compare" );
  19945.       else puts( "C 5.1 does an unsigned compare" );
  19946.    }
  19947.  
  19948.  
  19949.  B.1.3  Defining NULL as a Pointer
  19950.  
  19951.  The constant NULL is now defined as  ((void *)0). Previous versions of
  19952.  Microsoft C defined NULL as 0x0000 in small and medium models and
  19953.  0x00000000L in compact and large models.
  19954.  
  19955.  
  19956.  B.1.4  Shift Operators
  19957.  
  19958.  Shift operators now give a result that is of the same type as the left side.
  19959.  For example,
  19960.  
  19961.    short si;
  19962.    long li;
  19963.    si = 0x0001;
  19964.    li = si << 16L;
  19965.  
  19966.  The compiler previously yielded a result that was the size of the largest of
  19967.  the two values. In the example above, the short value would be automatically
  19968.  cast to a long because 16L is long. The value assigned to  li  would be
  19969.  0x00010000L in Microsoft C 5.1.
  19970.  
  19971.  To adhere to the ANSI draft standard, Microsoft C 6.0 maintains the size of
  19972.  the left operand. The variable  si  has 16 bits. Shifting left 16 times
  19973.  produces a value of 0, which is then assigned to  li.
  19974.  
  19975.  
  19976.  B.1.5  Pointers to Typedefs
  19977.  
  19978.  The rules for handling pointers to typedefs have changed subtly. For
  19979.  example, C 5.1 interprets
  19980.  
  19981.    typedef int far f_int;
  19982.    f_int *fp_i;
  19983.  
  19984.  as being equivalent to
  19985.  
  19986.    int *far fp_i;
  19987.  
  19988.  which means  fp_i  is a distant pointer to an integer. The address of  fp_i
  19989.  contains 32 bits. The size of the integer's address is indeterminate.
  19990.  
  19991.  C 6.0 interprets it as
  19992.  
  19993.    int far *fp_i;
  19994.  
  19995.  This means  fp_i  is a far pointer to an integer. The address of the integer
  19996.  contains 32 bits. The size of the address of  fp_i  is indeterminate.
  19997.  
  19998.  This affects typedefs containing _near, _far, _based, and other modifiers.
  19999.  Although these are Microsoft-specific keywords, their new behavior is
  20000.  consistent with what the ANSI draft standard requires for the const and
  20001.  volatile keywords.
  20002.  
  20003.  
  20004.  B.1.6  Identifying Nonstandard Keywords
  20005.  
  20006.  The following modifiers are specific to Microsoft C; they are not described
  20007.  in the ANSI draft standard. To identify these implementation-defined
  20008.  keywords as non-ANSI, an initial underscore has been added.
  20009.  
  20010.  C 5.1 Keyword                     C 6.0 Keyword
  20011.  ────────────────────────────────────────────────────────────────────────────
  20012.  far                               _far
  20013.  
  20014.  huge                              _huge
  20015.  
  20016.  near                              _near
  20017.  
  20018.  cdecl                             _cdecl
  20019.  
  20020.  fortran                           _fortran
  20021.  
  20022.  interrupt                         _interrupt
  20023.  
  20024.  pascal                            _pascal
  20025.  
  20026.  The compiler still accepts the obsolescent versions of these keywords,
  20027.  unless the /Za option is used.
  20028.  
  20029.  
  20030.  B.1.7  Trigraphs
  20031.  
  20032.  To maintain compatibility with and portability to other systems, Microsoft C
  20033.  6.0 supports the following trigraphs:
  20034.  
  20035.  Trigraph                          Character
  20036.  ────────────────────────────────────────────────────────────────────────────
  20037.  ??=                               #
  20038.  
  20039.  ??(                               [
  20040.  
  20041.  ??/                               \
  20042.  
  20043.  ??)                               ]
  20044.  
  20045.  ??'                               ^
  20046.  
  20047.  ??<                               {
  20048.  
  20049.  ??!                               |
  20050.  
  20051.  ??>                               }
  20052.  
  20053.  ??-                               ~
  20054.  
  20055.  
  20056.  B.1.8  ANSI Nonconformance
  20057.  
  20058.  This section lists the areas where Microsoft C 6.0 does not conform to the
  20059.  ANSI draft standard.
  20060.  
  20061.  
  20062.    ■   Microsoft C does not support multibyte characters, wide-character and
  20063.        string constants, and the related library functions and types.
  20064.  
  20065.    ■   Microsoft C contains some name-space violations in the language
  20066.        (extended keywords, such as near and far) and in the library (non-ANSI
  20067.        macros and types in header files and extended library function names,
  20068.        such as read and write).
  20069.  
  20070.  
  20071.  
  20072.  B.2  New Keywords and Functions
  20073.  
  20074.  This section describes keywords and functions that did not exist in previous
  20075.  versions of Microsoft C. Details about how to use these features can be
  20076.  found elsewhere in the documentation.
  20077.  
  20078.  
  20079.  B.2.1  In-Line Assembler
  20080.  
  20081.  The new _asm keyword allows you to mix assembly instructions with C source
  20082.  code. This feature includes the _emit function, which lets you enter
  20083.  arbitrary values into the code stream.
  20084.  
  20085.  See Chapter 3, "Using the In-Line Assembler."
  20086.  
  20087.  
  20088.  B.2.2  Based Pointers and Objects
  20089.  
  20090.  A based pointer is a special, compact form of pointer. It is always
  20091.  represented as a short offset. The address represented by such a pointer is
  20092.  calculated by adding the based pointer to its base. The base must be
  20093.  supplied each time the pointer is dereferenced, either explicitly using a
  20094.  special operator or implicitly by associating the base value with the
  20095.  pointer when it is declared. The base can be a far pointer, a near pointer,
  20096.  or a new type that represents a segment.
  20097.  
  20098.  Based pointers and objects are declared using the new keyword, _based.
  20099.  
  20100.  
  20101.  Segment Types
  20102.  
  20103.  The new type specifier, _segment, specifies a segment.
  20104.  
  20105.  Any pointer or address can be cast to _segment. If the operand is a near
  20106.  pointer, the result is the current value of the data segment register (DS).
  20107.  If the operand is a far pointer, the result is the segment part of the far
  20108.  pointer.
  20109.  
  20110.  
  20111.  Segment Names
  20112.  
  20113.  Segment names are declared using the built-in function _segname. The
  20114.  compiler recognizes four predefined segment names: _CODE, _CONST, _DATA, and
  20115.  _STACK.
  20116.  
  20117.  Each segment name represents a constant of type _segment.
  20118.  
  20119.  
  20120.  Base Operator
  20121.  
  20122.  The base operator (:>) associates a base expression (usually a segment) with
  20123.  a based pointer, to form a far pointer value. For example,
  20124.  
  20125.    0x0F01:>0x0015
  20126.  
  20127.  combines the segment 0x0F01 with the offset 0x0015 to form the effective
  20128.  address 0x0F025. The base operator's precedence falls between ( ) and [ ].
  20129.  
  20130.  
  20131.  Casting Based Pointers
  20132.  
  20133.  A based pointer can be cast to a pointer, a long integer, a short integer,
  20134.  or another based pointer. When a based pointer is converted to a far
  20135.  pointer, a long integer, a near pointer, or another based pointer having a
  20136.  different base expression, it is first normalized to a far pointer
  20137.  (including adding the offset in the base, if present, to the based pointer);
  20138.  then any additional conversions are applied.
  20139.  
  20140.  
  20141.  Operations on Based Pointers
  20142.  
  20143.  Based pointers, for the purpose of arithmetic and dereferencing, are treated
  20144.  as semantically equivalent to far pointers. When a based pointer mixes with
  20145.  another integral type (int, long, near pointer, far pointer, or based
  20146.  pointer), implicit casting is done. In some cases, the compiler can optimize
  20147.  these references and treat the pointer as an offset.
  20148.  
  20149.  The value of 0 is treated specially, as it is for near and far pointers. No
  20150.  conversions are applied to the constant 0 because it is assumed to be a null
  20151.  pointer.
  20152.  
  20153.  See Chapter 2, "Managing Memory."
  20154.  
  20155.  
  20156.  B.2.3  Based Heap Allocation Support
  20157.  
  20158.  The functions listed below provide support for allocating, expanding, and
  20159.  freeing memory for based heaps, which dynamically allocate memory for based
  20160.  items. The functions are prototyped in the MALLOC.H include file.
  20161.  
  20162. ╓┌──────────┌───────────┌────────────────────────────────────────────────────╖
  20163.  ────────────────────────────────────────────────────────────────────────────
  20164.  _bcalloc   _bheapchk   _bmalloc
  20165.  _bexpand   _bheapmin   _bmsize
  20166.  _bfree     _bheapseg   _brealloc
  20167.  _bfreeseg  _bheapset
  20168.  _bheapadd  _bheapwalk
  20169.  
  20170.  
  20171.  See Chapter 2, "Managing Memory."
  20172.  
  20173.  
  20174.  B.2.4  Releasing Unused Heap Memory
  20175.  
  20176.  The following routines release unused heap memory by shortening data
  20177.  segments. MALLOC.H contains the function prototypes.
  20178.  
  20179.  ────────────────────────────────────────────────────────────────────────────
  20180.  _fheapmin
  20181.  _heapmin
  20182.  _nheapmin
  20183.  
  20184.  
  20185.  B.2.5  Making Static Data Available to the Heap
  20186.  
  20187.  The _heapadd function is new. It allows the user to make unused static data
  20188.  available to the heap.
  20189.  
  20190.  
  20191.  B.2.6  Long Doubles
  20192.  
  20193.  Microsoft C version 5.1 treated double and long double as syntactically
  20194.  different types that were semantically equal. Both types were stored in
  20195.  memory as 64-bit quantities. For purposes of type-checking, long double and
  20196.  double have always been different types.
  20197.  
  20198.  Because the 80x87 family of math coprocessors supports an 80-bit
  20199.  floating-point type, Microsoft C version 6.0 stores long double variables in
  20200.  the 80x87 10-byte (80-bit) form.
  20201.  
  20202.  Certain functions have been modified to handle the long double type. The
  20203.  printf and scanf family of functions supports long double values with the
  20204.  trailing l. The library contains new versions of the transcendental
  20205.  functions as well as intrinsic forms that accept long double arguments.
  20206.  
  20207.  
  20208.  B.2.7  Long Double Functions
  20209.  
  20210.  All the functions below are defined in the standard include file MATH.H.
  20211.  They return long double values and results and error codes analogous to the
  20212.  double versions.
  20213.  
  20214. ╓┌───────┌───────┌───────────────────────────────────────────────────────────╖
  20215.  ────────────────────────────────────────────────────────────────────────────
  20216.  acosl   expl    _matherrl
  20217.  asinl   fabsl   modfl
  20218.  atanl   floorl  powl
  20219.  atan2l  fmodl   sinl
  20220.  _atold  frexpl  sinhl
  20221.  cabsl   hypotl  sqrtl
  20222.  ceill   ldexpl  tanl
  20223.  cosl    logl    tanhl
  20224.  coshl   log10l
  20225.  ────────────────────────────────────────────────────────────────────────────
  20226. coshl   log10l
  20227.  
  20228.  
  20229.  
  20230.  B.2.8  Model-Independent String and Memory Functions
  20231.  
  20232.  The following functions make it easier to write mixed-model programs by
  20233.  providing model-independent (large model) forms for most of the standard
  20234.  string and memory functions. These functions can be called from any point in
  20235.  any program, no matter which memory model has been selected. These functions
  20236.  take only far pointers as arguments. Thus, any data item, near or far, in
  20237.  any combination, can be handled.
  20238.  
  20239.  The names of these functions are the same as the model-dependent forms,
  20240.  except they include an _f prefix. For example, _fstrlen is the
  20241.  model-independent version of the strlen function.
  20242.  
  20243.  The functions listed below are defined in the standard include file
  20244.  STRING.H.
  20245.  
  20246.  
  20247.  Memory Functions
  20248.  
  20249. ╓┌────────────────────────────┌──────────────────────────────────────────────╖
  20250.  ────────────────────────────────────────────────────────────────────────────
  20251.  _fmemccpy                    _fmemcpy
  20252.  _fmemchr                     _fmemmove
  20253.  _fmemcmp                     _fmemset
  20254.  _fmemicmp
  20255.  
  20256.  
  20257.  
  20258.  String Functions
  20259.  
  20260. ╓┌──────────┌───────────┌────────────────────────────────────────────────────╖
  20261.  ────────────────────────────────────────────────────────────────────────────
  20262.  _fstrcat   _fstrlwr    _fstrrchr
  20263.  _fstrchr   _fstrncat   _fstrrev
  20264.  _fstrcmp   _fstrncmp   _fstrset
  20265.  _fstricmp  _fstrnicmp  _fstrspn
  20266.  _fstrcpy   _fstrncpy   _fstrstr
  20267.  ────────────────────────────────────────────────────────────────────────────
  20268. _fstrcpy   _fstrncpy   _fstrstr
  20269.  _fstrcspn  _fstrnset   _fstrtok
  20270.  _fstrlen   _fstrpbrk   _fstrupr
  20271.  
  20272.  
  20273.  
  20274.  String Duplication Functions
  20275.  
  20276.  ────────────────────────────────────────────────────────────────────────────
  20277.  _fstrdup
  20278.  _nstrdup
  20279.  
  20280.  
  20281.  B.2.9  Mixed-Model Memory Allocation Support
  20282.  
  20283.  The following functions are based on realloc, calloc, and expand, but they
  20284.  affect only near memory or far memory. MALLOC.H contains the function
  20285.  prototypes.
  20286.  
  20287. ╓┌───────────────────────────┌───────────────────────────────────────────────╖
  20288.  ────────────────────────────────────────────────────────────────────────────
  20289.  _fcalloc                    _ncalloc
  20290.  _fexpand                    _nexpand
  20291.  _frealloc                   _nrealloc
  20292.  
  20293.  
  20294.  
  20295.  B.2.10  The _fastcall Attribute (/Gr Option)
  20296.  
  20297.  Individual function prototypes can be declared with the new attribute
  20298.  _fastcall.
  20299.  
  20300.  The /Gr option enables the fastcall function-calling convention for all
  20301.  functions that are not explicitly prototyped with the _cdecl, _pascal, or
  20302.  _fortran attributes. Using /Gr on the command line causes each function in
  20303.  the module to compile as _fastcall unless the function is declared with a
  20304.  conflicting attribute, or the name of the function is main.
  20305.  
  20306.  When you use the /Gr option, all functions are assumed to use the _fastcall
  20307.  convention. As a result, to use any run-time library functions, you must
  20308.  either include the standard include files or explicitly prototype the
  20309.  function you want to call.
  20310.  
  20311.  A fastcall function receives up to three 16-bit arguments, passed in
  20312.  registers rather than on the stack. Arguments are passed in the AX, BX, and
  20313.  DX registers. This may change in future versions of the compiler.
  20314.  
  20315.  The argument types and their potential register assignments are
  20316.  
  20317.  Argument                          Registers
  20318.  ────────────────────────────────────────────────────────────────────────────
  20319.  character (3)                     AL, DL, BL
  20320.  
  20321.  short integer (3)                 AX, DX, BX
  20322.  
  20323.  near pointer (3)                  BX, AX, DX
  20324.  
  20325.  long integer (1)                  DX:AX
  20326.  
  20327.  far pointer (1)                   ES:BX
  20328.  
  20329.  If the registers for a particular class have already been used, or if an
  20330.  argument is not one of the five types listed above, it is pushed on the
  20331.  stack as usual. An argument list of types long, float, short would pass the
  20332.  long in DX:AX, push the float, and pass the short in BX.
  20333.  
  20334.  The treatment of character arguments depends further on prototypes. If there
  20335.  is no prototype, the argument is promoted to short and the rules for short
  20336.  integers apply. Only if the argument is prototyped as a char do the
  20337.  character rules apply.
  20338.  
  20339.  The _fastcall convention is not compatible with any of the following
  20340.  attributes: _interrupt, _saveregs, _export, _cdecl, _fortran, or _pascal.
  20341.  
  20342.  See Chapter 1, "Optimizing C Programs."
  20343.  
  20344.  
  20345.  B.2.11  Drive and Directory Functions
  20346.  
  20347.  Several new functions make it easier to get and set the current drive and
  20348.  the current directory. The prototypes for the following routines are in
  20349.  DIRECT.H:
  20350.  
  20351.  ────────────────────────────────────────────────────────────────────────────
  20352.  _chdrive
  20353.  _fullpath
  20354.  _getdrive
  20355.  _getdcwd
  20356.  
  20357.  
  20358.  B.2.12  Text Output Functions for OS/2
  20359.  
  20360.  Several text-mode screen functions have been added to Microsoft C 6.0 for
  20361.  OS/2. With the exception of the new _scrolltextwindow function, they are
  20362.  identical to what is defined in real mode, except for any references to
  20363.  behavior in graphics modes. The following routines are located in
  20364.  GRTEXT.LIB, and the prototypes are in GRAPH.H:
  20365.  
  20366. ╓┌─────────────────┌─────────────────┌───────────────────────────────────────╖
  20367.  ────────────────────────────────────────────────────────────────────────────
  20368.  _clearscreen      _getvideoconfig   _settextrows
  20369.  _displaycursor    _outtext          _settextwindow
  20370.  _getbkcolor       _setbkcolor       _setvideomode
  20371.  _gettextcolor     _settextcolor     _setvideomoderows
  20372.  ────────────────────────────────────────────────────────────────────────────
  20373. _gettextcolor     _settextcolor     _setvideomoderows
  20374.  _gettextcursor    _settextcursor    _scrolltextwindow
  20375.  _gettextposition  _settextposition  _wrapon
  20376.  
  20377.  
  20378.  See Part 4 of this manual, "OS/2 Support."
  20379.  
  20380.  
  20381.  B.3  New Features
  20382.  
  20383.  The features described in Sections B.3.1-B.3.10 are new to version 6.0.
  20384.  
  20385.  
  20386.  B.3.1  Strings and Macros
  20387.  
  20388.  The compiler now allows longer string literals (up to 4K) and longer macro
  20389.  expansions (up to 6K).
  20390.  
  20391.  
  20392.  B.3.2  CL Options
  20393.  
  20394.  The following options are new to Microsoft C 6.0:
  20395.  
  20396.  Option                            Action
  20397.  ────────────────────────────────────────────────────────────────────────────
  20398.  /AT                               Compiles in tiny model (.COM files).
  20399.  
  20400.  /Fr« filename»                    Outputs source browser information file.
  20401.  
  20402.  /FR« filename»                    Outputs extended source browser
  20403.                                    information file.
  20404.  
  20405.  /Gd                               Forces _cdecl calling conventions.
  20406.  
  20407.  /Gr                               Enables register (_fastcall)
  20408.                                    function-calling
  20409.                                    conventions.
  20410.  
  20411.  /MAmasmoption                     Supports invocation of the assembler
  20412.                                    using the CL driver. All MASM-supported
  20413.                                    options are accepted. In addition, the
  20414.                                    compiler recognizes file names with .ASM
  20415.                                    suffixes and passes them directly to
  20416.                                    MASM.
  20417.  
  20418.  /MD-                              Uses C run-time as DLL option. Defaults
  20419.                                    to
  20420.                                    /ALw /FPi /G2 /DDLL /DMT and inhibits
  20421.                                    library search records.
  20422.  
  20423.  /ML                               Links C run-time as part of a
  20424.                                    dynamic-link library (DLL). Defaults to
  20425.                                    /ALw /FPa /G2 /DMT and changes library
  20426.                                    search record to LLIBCDLL.LIB.
  20427.  
  20428.  /MT                               Enables multithread option. Defaults to
  20429.                                    /ALw /FPi /G2 /DMT and changes library
  20430.                                    search record to LLIBCMT.LIB.
  20431.  
  20432.  /Oe                               Enables global register allocation.
  20433.  
  20434.  /Og                               Enables global optimizations and global
  20435.                                    common subexpressions (CSEs).
  20436.  
  20437.  /Ox                               Is now equivalent to /Ocegilt /Gs. Note
  20438.                                    that this implies that maximum
  20439.                                    optimization includes the _fastcall
  20440.                                    function-calling convention.
  20441.  
  20442.  /Oz                               Enables aggressive optimizations.
  20443.  
  20444.  /Ta name                          Specifies that name is to be treated as
  20445.                                    an assembler input file.
  20446.  
  20447.  /W4                               Turns on extra warning level which
  20448.                                    supports more detailed (LINT-like)
  20449.                                    warnings and recognition of ANSI
  20450.                                    violations.
  20451.  
  20452.  /WX                               Causes warnings to be treated as errors.
  20453.                                    If a warning occurs, the .OBJ file is
  20454.                                    not created.
  20455.  
  20456.  
  20457.  B.3.3  Tiny Memory Model (.COM Files)
  20458.  
  20459.  Microsoft C 6.0 now supports the tiny memory model, which produces .COM
  20460.  rather than .EXE files (for DOS only).
  20461.  
  20462.  The /AT option selects the tiny model. This forces the linker to use options
  20463.  /NOE and /TINY. Within the linker, /TINY turns on /FARCALLTRANSLATION to
  20464.  help eliminate far segment relocations. If you link your own .OBJ files,
  20465.  link with CRTCOM.OBJ.
  20466.  
  20467.  
  20468.  B.3.4  The Optimize Pragma
  20469.  
  20470.  The optimize pragma turns optimizing options on or off:
  20471.  
  20472.    #pragma optimize("<optimization switch list>",{off|on})
  20473.  
  20474.  where <optimization switch list> can be an empty list or one or more of the
  20475.  following: a, c, e, g, l, w, n, p, t, and z. For example,
  20476.  
  20477.    #pragma optimize("lp",on) /* equivalent to /Olp */
  20478.    #pragma optimize("",off)  /* turns off all optimization */
  20479.    #pragma optimize("",on)  /* restores default settings */
  20480.  
  20481.  See Chapter 1, "Optimizing C Programs."
  20482.  
  20483.  
  20484.  B.3.5  Nameless Structures and Unions
  20485.  
  20486.  Both struct and union declarations can now be specified without a declarator
  20487.  when they are members of another structure or union.
  20488.  
  20489.  A nameless union would look like this:
  20490.  
  20491.    struct str
  20492.    {
  20493.       int a,b;
  20494.       union           /* unnamed union */
  20495.       {
  20496.          char c[4];
  20497.          long l;
  20498.          float f;
  20499.       };
  20500.       char c_array[10];
  20501.    } my_str;
  20502.    .
  20503.    .
  20504.    .
  20505.    my_str.l == 0L;
  20506.  
  20507.  A nameless structure would look like this:
  20508.  
  20509.    struct s1
  20510.    {
  20511.       int a,b,c;
  20512.    };
  20513.  
  20514.    struct s2
  20515.    {
  20516.       float y;
  20517.       struct s1;
  20518.       char str[10];
  20519.    } *p_s2;
  20520.    .
  20521.    .
  20522.    .
  20523.  
  20524.    p_s2->b = 100;
  20525.  
  20526.  
  20527.  B.3.6  Unsized Arrays as the Last Member of a Structure
  20528.  
  20529.  The compiler now allows an unsized or zero-sized array as the last member of
  20530.  a structure. The declaration of such a structure would look like this:
  20531.  
  20532.    struct var_length
  20533.    {
  20534.       <set of declarations>;
  20535.       <type> array[];
  20536.    };
  20537.  
  20538.  Unsized arrays can appear only as the last member of a structure. Structures
  20539.  containing unsized array declarations can be nested within other structures
  20540.  as long as no further members are declared in any enclosing structures.
  20541.  Arrays of such structures are not allowed.
  20542.  
  20543.  The sizeof operator, when applied to a variable of this type or to the type
  20544.  itself, assumes 0 for the size of the array.
  20545.  
  20546.  
  20547.  B.3.7  Improved Warnings
  20548.  
  20549.  A new warning level four (CL option /W4) has been added for the following
  20550.  warnings:
  20551.  
  20552.  
  20553.    ■   Detection of unused global variables
  20554.  
  20555.    ■   Expressions without side effects
  20556.  
  20557.    ■   Nonportable (non-ANSI) constructs
  20558.  
  20559.    ■   Local variable referenced before being initialized
  20560.  
  20561.    ■   Undefined or implementation-defined constructs
  20562.  
  20563.  
  20564.  
  20565.  B.3.8  Macros
  20566.  
  20567.  The number of macros definable with /D options has increased from 20 to 30.
  20568.  
  20569.  
  20570.  
  20571.  B.3.9  Improved Multithread Support in OS/2
  20572.  
  20573.  The number of OS/2 threads supported at run time has increased from 32 to
  20574.  the operating system limit. Three new options aid development of multithread
  20575.  applications and dynamic-link libraries:
  20576.  
  20577.  
  20578.    1.  /MT for building multithread programs. It implies /ALw /FPi /G2 /D MT,
  20579.        and changes the library search record emitted in the object file to
  20580.        reference LLIBCMT.
  20581.  
  20582.    2.  /ML for building a DLL that uses the C run-time library. It implies
  20583.        /ALw /FPa /G2 /D MT, and changes the library search record emitted  in
  20584.        the object file to reference LLIBCDLL.
  20585.  
  20586.    3.  /MD for building .EXE files and DLLs that share a C run-time DLL. It
  20587.        implies /ALw /FPi /G2 /DDLL /D MT, and no library search records are
  20588.        emitted in the object file.
  20589.  
  20590.  
  20591.  
  20592.  B.3.10  Pipe Support in OS/2
  20593.  
  20594.  Microsoft C 6.0 supports pipes as part of the file I/O system. The functions
  20595.  listed below are defined in the standard include file IO.H:
  20596.  
  20597.  ────────────────────────────────────────────────────────────────────────────
  20598.  _pipe
  20599.  _popen
  20600.  _pclose
  20601.  
  20602.  
  20603.  B.4  Differences in Code Generation
  20604.  
  20605.  This section lists ways in which the executable files produced by Microsoft
  20606.  C 6.0 may differ from the files produced by previous versions of the
  20607.  compiler.
  20608.  
  20609.  
  20610.  B.4.1  Speed and Space Improvements
  20611.  
  20612.  Executable files are smaller and faster.
  20613.  
  20614.  
  20615.  B.4.2  Code Quality
  20616.  
  20617.  Microsoft C 6.0 generates improved local code in default optimization cases
  20618.  and, under full optimization, supports global (function level) register
  20619.  allocation and common subexpressions (CSEs), loop optimizations, parameter
  20620.  passing through registers, and generation of in-line code for certain
  20621.  intrinsic functions.
  20622.  
  20623.  
  20624.  B.4.3  Floating-Point Code Generation
  20625.  
  20626.  In Microsoft C 6.0, the /FPi87 option suppresses the fixups previously used
  20627.  for emulation. Pure coprocessor instructions are now emitted. This makes
  20628.  object files smaller and speeds up linking, in addition to making in-line
  20629.  assembly easier to use.
  20630.  
  20631.  In version 5.1, /FPi and /FPi87 generated the same code; the only difference
  20632.  was the library. In C 6.0, the two options generate different code. It is no
  20633.  longer possible to force /FPi87 to act like /FPi. If you use /FPi87, the
  20634.  math coprocessor must be in the computer on which the program is running.
  20635.  
  20636.  Note that if you use /FPi87 you must link with mLIB7, not mLIBCE.
  20637.  
  20638.  
  20639.  B.4.4  Intrinsic Functions
  20640.  
  20641.  The intrinsic function optimization option (/Oi) causes the compiler to
  20642.  generate in-line code for the following functions:
  20643.  
  20644. ╓┌─────────┌───────┌─────────────────────────────────────────────────────────╖
  20645.  ────────────────────────────────────────────────────────────────────────────
  20646.  abs       _lrotl  _rotl
  20647.  _disable  _lrotr  _rotr
  20648.  _enable   memcmp  strcat
  20649.  ffabs     memcpy  strcmp
  20650.  inp       memset  strcpy
  20651.  inpw      outp    strlen
  20652.  labs      outpw   strset
  20653.  
  20654.  
  20655.  The compiler does not generate in-line code for the following functions,
  20656.  although it will modify the calling convention to pass the arguments on the
  20657.  floating-point chip:
  20658.  
  20659. ╓┌──────┌───────┌────────────────────────────────────────────────────────────╖
  20660.  ────────────────────────────────────────────────────────────────────────────
  20661.  acos   pow     coshl
  20662.  asin   sin     expl
  20663.  atan   sinh    floorl
  20664.  atan2  sqrt    fmodl
  20665.  ceil   tan     logl
  20666.  ────────────────────────────────────────────────────────────────────────────
  20667. ceil   tan     logl
  20668.  cos    tanh    log10l
  20669.  cosh   acosl   powl
  20670.  exp    asinl   sinl
  20671.  floor  atanl   sinhl
  20672.  fmod   atan2l  sqrtl
  20673.  log    ceill   tanl
  20674.  log10  cosl    tanhl
  20675.  
  20676.  
  20677.  
  20678.  B.5  Changes and Deletions
  20679.  
  20680.  The changes and deletions listed in this section have a high probability of
  20681.  affecting existing programs.
  20682.  
  20683.  
  20684.  B.5.1  Deleted Features
  20685.  
  20686.  The data_seg pragma has been deleted.
  20687.  
  20688.  The memory management routine sbrk has been deleted.
  20689.  
  20690.  The compiler and tools do not run under DOS 2.1. The run-time files produced
  20691.  by the compiler and linker will continue to run under DOS 2.1.
  20692.  
  20693.  
  20694.  B.5.2  Evaluation of Real Expressions
  20695.  
  20696.  Real expressions inside parentheses are now evaluated according to the
  20697.  semantics of the parentheses. For example, in the expression
  20698.  
  20699.    ((r1 / r2) * r3)
  20700.  
  20701.  the division is performed before the multiplication. Previous versions of
  20702.  the compiler might have reordered the operations.
  20703.  
  20704.  
  20705.  B.5.3  Default Optimizations
  20706.  
  20707.  Version 6.0 performs more extensive optimizations than version 5.1. This
  20708.  implies that code that had aliasing but worked with the /Oa option in 5.1
  20709.  might not work with version 6.0 and /Oa. Also, because of the improved
  20710.  optimizations, the /Od option should be used to turn off all optimizing
  20711.  before you begin debugging with CodeView.
  20712.  
  20713.  
  20714.  B.5.4  Sign Extension of char Arguments
  20715.  
  20716.  Previous versions of Microsoft C would sign-extend char arguments to int
  20717.  size before passing them to a second function. Version 6.0 does not extend
  20718.  the sign if the function is prototyped and the prototype includes a char
  20719.  argument. The most-significant byte is considered undefined.
  20720.  
  20721.  
  20722.  B.5.5  Conditional Compilation and Signed Values
  20723.  
  20724.  Version 5.1 of Microsoft C treated conditional compilation expressions as
  20725.  signed long values. Version 6.0 evaluates these expressions using the same
  20726.  rules as expressions in C. For example,
  20727.  
  20728.    #if 0xFFFFFFFFL > 1UL
  20729.    .
  20730.    .
  20731.    .
  20732.    #endif
  20733.  
  20734.  The expression evaluates to be true. It was evaluated as false in version
  20735.  5.1.
  20736.  
  20737.  
  20738.  B.5.6  The const and volatile Qualifiers
  20739.  
  20740.  The const  and volatile qualifiers must be placed after the type they
  20741.  qualify. The declaration
  20742.  
  20743.    int (const *p);
  20744.  
  20745.  is now treated as a syntax error. Previous versions of the compiler would
  20746.  accept such a construction.
  20747.  
  20748.  The following declarations are legal:
  20749.  
  20750.    int const *p_ci;  /* pointer to constant int */
  20751.    int const (*p_ci); /* pointer to constant int */
  20752.    int *const cp_i;  /* constant pointer to int */
  20753.    int (*const cp_i); /* constant pointer to int */
  20754.  
  20755.  
  20756.  B.5.7  Memory Allocation
  20757.  
  20758.  The _fmalloc function attempts to allocate far memory. It previously called
  20759.  _nmalloc if far memory was not available. Now it returns a null pointer if
  20760.  far memory isn't available, even if near memory is available.
  20761.  
  20762.  
  20763.  B.5.8  Memory Used by Command-Line Arguments
  20764.  
  20765.  Previous versions of the compiler placed the command-line argument strings
  20766.  and environment strings in the near heap. Now they are allocated though
  20767.  malloc, which means that they will be in far memory in compact and large
  20768.  models.
  20769.  
  20770.  
  20771.  B.5.9  Format Specifiers in printf
  20772.  
  20773.  The printf format specifier modifiers N, F, h, and l have changed.
  20774.  
  20775.  The specifier %Np is a synonym for %hp, but the latter is preferred.
  20776.  Likewise, %Fp is a synonym for %lp.
  20777.  
  20778.  For scanf, N and F refer to the distance to the object being read in; that
  20779.  is, whether the pointer itself is allocated near or far. The modifiers h and
  20780.  l refer to the size of the object (16-bit near pointer or 32-bit far
  20781.  pointer). In these examples,
  20782.  
  20783.    scanf("%Nlp", n_fp);
  20784.    scanf("%Fhp", f_np);
  20785.  
  20786.  the first line reads in an address that resides in near memory (N) but holds
  20787.  a 32-bit far pointer variable (lp). The second line reads in a near pointer
  20788.  value (hp) into a pointer variable that resides in far memory (F).
  20789.  
  20790.  
  20791.  B.5.10  Functions that Return Float Values
  20792.  
  20793.  In Microsoft C 5.1, a prototype or definition such as
  20794.  
  20795.    float funcname();
  20796.  
  20797.  was interpreted as
  20798.  
  20799.    double funcname()
  20800.  
  20801.  Version 6.0 interprets it as
  20802.  
  20803.    float
  20804.  
  20805.  
  20806.  
  20807.  
  20808.  
  20809.  
  20810.  Appendix C  Implementation-Defined Behavior
  20811.  ────────────────────────────────────────────────────────────────────────────
  20812.  
  20813.  The American National Standards Institute (ANSI) Standard for the C
  20814.  programming language contains an appendix called "Portability Issues." The
  20815.  ANSI appendix lists areas of the C language that ANSI leaves open to each
  20816.  particular implementation. This appendix describes how Microsoft C handles
  20817.  these implementation-defined areas of the C language.
  20818.  
  20819.  This appendix follows the same order as the ANSI Standard appendix. Each
  20820.  item covered includes references to the ANSI chapter and section that
  20821.  explains the implementation-defined behavior.
  20822.  
  20823.  ────────────────────────────────────────────────────────────────────────────
  20824.  NOTE
  20825.  
  20826.  This appendix describes the U.S. English-language version of the C compiler
  20827.  only. Foreign-language implementations of Microsoft C may differ slightly.
  20828.  ────────────────────────────────────────────────────────────────────────────
  20829.  
  20830.  
  20831.  C.1  Translation
  20832.  
  20833.  
  20834.  C.1.1  Diagnostics
  20835.  
  20836.  How a diagnostic is identified (2.1.1.3)
  20837.  
  20838.  Microsoft C produces error messages in the form:
  20839.  
  20840.    filename(line-number) : diagnostic Cnumber message
  20841.  
  20842.  where filename is the name of the source file in which the error was
  20843.  encountered; line-number is the line number at which the compiler detected
  20844.  the error; diagnostic is either "error" or "warning"; number is a unique
  20845.  four-digit number (preceded by a C) that identifies the error or warning;
  20846.  message is an explanatory message.
  20847.  
  20848.  
  20849.  C.2  Environment
  20850.  
  20851.  
  20852.  C.2.1  Arguments to main
  20853.  
  20854.  The semantics of the arguments to main (2.1.2.2)
  20855.  
  20856.  In Microsoft C, the function called at program start-up is called main.
  20857.  There is no prototype declared for main, and it can be defined with zero,
  20858.  two, or three parameters:
  20859.  
  20860.    int main( void )
  20861.    int main( int argc, char *argv[] )
  20862.    int main( int argc, char *argv[], char *envp[] )
  20863.  
  20864.  The third line above, where main accepts three parameters, is a Microsoft
  20865.  extension to the ANSI Standard. The third parameter, envp, is an array of
  20866.  pointers to environment variables. The envp array is terminated by a null
  20867.  pointer. See on-line help for more information about main and envp.
  20868.  
  20869.  The variable argc never holds a negative value.
  20870.  
  20871.  The array of strings ends with argv[argc], which contains a null pointer.
  20872.  
  20873.  All elements of the argv array are pointers to strings.
  20874.  
  20875.  A program invoked with no command-line arguments will receive a value of one
  20876.  for argc, as the name of the executable file is placed in argv[0]. (In DOS
  20877.  versions prior to 3.0, the executable file name is not available. The letter
  20878.  "C" is placed in argv[0].) Strings pointed to by argv[1] through argv[argc -
  20879.  1] represent program parameters.
  20880.  
  20881.  The parameters argc and argv are modifiable and retain their last-stored
  20882.  values between program start-up and program termination.
  20883.  
  20884.  
  20885.  C.2.2  Interactive Devices
  20886.  
  20887.  What constitutes an interactive device (2.1.2.3)
  20888.  
  20889.  Microsoft C defines the keyboard and the display as interactive devices.
  20890.  
  20891.  
  20892.  C.3  Identifiers
  20893.  
  20894.  
  20895.  C.3.1  Significant Characters without External Linkage
  20896.  
  20897.  The number of significant characters without external linkage (3.1.2)
  20898.  
  20899.  Identifiers are significant to 31 characters. The compiler does not restrict
  20900.  the number of characters you can use in an identifier; it simply ignores any
  20901.  characters beyond the limit.
  20902.  
  20903.  
  20904.  C.3.2  Significant Characters with External Linkage
  20905.  
  20906.  The number of significant characters with external linkage (3.1.2)
  20907.  
  20908.  Identifiers declared extern in programs compiled with Microsoft C are
  20909.  significant to 31 characters. You can modify this default to a smaller
  20910.  number using the /H (restrict length of external names) option. See on-line
  20911.  help for more information on the syntax of the /H option.
  20912.  
  20913.  
  20914.  C.3.3  Upper- and Lowercase
  20915.  
  20916.  Whether case distinctions are significant (3.1.2)
  20917.  
  20918.  Microsoft C treats identifiers within a compilation unit as case sensitive.
  20919.  Externally linked identifiers may or may not be case sensitive, depending on
  20920.  whether you use /NOIGNORECASE option when you invoke the linker. The default
  20921.  for the linker is to ignore case, making externally linked identifiers case
  20922.  insensitive.
  20923.  
  20924.  Thus, symbols in source files are sensitive to case. By default, symbols in
  20925.  object files are not.
  20926.  
  20927.  Two CL command-line options affect case sensitivity:
  20928.  
  20929.  
  20930.    1.  The /Gc (generate Pascal-style function calls) command-line option
  20931.        converts all external identifiers (including function names) to
  20932.        uppercase.
  20933.  
  20934.        The _pascal declarator performs the same operation on a
  20935.        function-byfunction basis.
  20936.  
  20937.    2.  The /Zc (compile case insensitive) converts all identifiers (excluding
  20938.        function names) to uppercase.
  20939.  
  20940.  
  20941.  
  20942.  C.4  Characters
  20943.  
  20944.  
  20945.  C.4.1  The ASCII Character Set
  20946.  
  20947.  Members of source and execution character sets (2.2.1)
  20948.  
  20949.  The source character set is the set of legal characters that can appear in
  20950.  source files. For Microsoft C, the source character set is the standard
  20951.  ASCII character set. Figure C.1 contains an ASCII table.
  20952.  
  20953.  ────────────────────────────────────────────────────────────────────────────
  20954.  WARNING
  20955.  
  20956.  Because keyboard and console drivers can remap the character set, programs
  20957.  intended for international distribution should check the country code.
  20958.  ────────────────────────────────────────────────────────────────────────────
  20959.  
  20960.  
  20961.  C.4.2  Multibyte Characters
  20962.  
  20963.  Shift states for multibyte characters (2.2.1)
  20964.  
  20965.  Multibyte characters are used by some implementations to represent
  20966.  foreignlanguage characters not represented in the base character set.
  20967.  Microsoft C 6.0 does not support multibyte characters.
  20968.  
  20969.  
  20970.  C.4.3  Bits per Character
  20971.  
  20972.  Number of bits in a character (2.2.4.2)
  20973.  
  20974.  The number of bits in a character is represented by the manifest constant
  20975.  CHAR_BIT. The LIMITS.H file defines CHAR_BIT as 8.
  20976.  
  20977.  
  20978.  C.4.4  Character Sets
  20979.  
  20980.  Mapping members of the source character set (3.1.3.4)
  20981.  
  20982.  The source character set and execution character set include the ANSI ASCII
  20983.  characters listed in Table C.1. Escape sequences are also shown in Table
  20984.  C.1.
  20985.  
  20986.  Table   C.1
  20987.  
  20988. ╓┌────────────────┌─────────────────┌────────────────────────────────────────╖
  20989.  Escape Sequence  Character         ASCII Value
  20990.  ────────────────────────────────────────────────────────────────────────────
  20991.  \a               Alert/bell        7
  20992.  \b               Backspace         8
  20993.  \f               Form feed         12
  20994.  \n               Newline           10
  20995.  \r               Carriage return   13
  20996.  \t               Horizontal tab    9
  20997.  \v               Vertical tab      11
  20998.  \"               Double quotation  34
  20999.  \'               Single quotation  39
  21000.  \\               Backslash         92
  21001.  ────────────────────────────────────────────────────────────────────────────
  21002.  Escape Sequence  Character         ASCII Value
  21003.  ────────────────────────────────────────────────────────────────────────────
  21004. ────────────────────────────────────────────────────────────────────────────
  21005.  
  21006.  
  21007.  
  21008.  C.4.5  Unrepresented Character Constants
  21009.  
  21010.  The value of an integer character constant that contains a character or
  21011.  escape sequence not represented in the basic execution character set or the
  21012.  extended character set for a wide character constant (3.1.3.4)
  21013.  
  21014.  Microsoft C does not support wide characters.
  21015.  
  21016.  
  21017.  C.4.6  Wide Characters
  21018.  
  21019.  The value of an integer character constant that contains more than one
  21020.  character or a wide character constant that contains more than one multibyte
  21021.  character (3.1.3.4)
  21022.  
  21023.  Microsoft C does not support wide characters or multibyte characters.
  21024.  
  21025.  
  21026.  C.4.7  Converting Multibyte Characters
  21027.  
  21028.  The current locale used to convert multibyte characters into corresponding
  21029.  wide characters (codes) for a wide character constant (3.1.3.4)
  21030.  
  21031.  Microsoft C does not support multibyte characters.
  21032.  
  21033.  
  21034.  C.4.8  Range of char Values
  21035.  
  21036.  Whether a "plain" char has the same range of values as a signed char or an
  21037.  unsigned char (3.2.1.1)
  21038.  
  21039.  All character values range from 0x00 to 0xFF, signed or unsigned. If a char
  21040.  is not explicitly marked as signed or unsigned, it defaults to the signed
  21041.  type.
  21042.  
  21043.  The CL option /J changes the default from signed to unsigned.
  21044.  
  21045.  
  21046.  C.5  Integers
  21047.  
  21048.  
  21049.  C.5.1  Range of Integer Values
  21050.  
  21051.  The representations and sets of values of the various types of integers
  21052.  (3.1.2.5)
  21053.  
  21054.  Short integers contain 16 bits (two bytes). Long integers contain 32 bits
  21055.  (four bytes). Signed integers are represented in two's-complement form. The
  21056.  mostsignificant bit holds the sign: 1 for negative, 0 for positive and zero.
  21057.  The values are listed below:
  21058.  
  21059.  Type                              Minimum and Maximum
  21060.  ────────────────────────────────────────────────────────────────────────────
  21061.  unsigned short                    0 to 65535
  21062.  
  21063.  signed short                      -32768 to 32767
  21064.  
  21065.  unsigned long                     0 to 4294967295
  21066.  
  21067.  signed long                       -2147483648 to 2147483647
  21068.  
  21069.  
  21070.  C.5.2  Demotion of Integers
  21071.  
  21072.  The result of converting an integer to a shorter signed integer, or the
  21073.  result of converting an unsigned integer to a signed integer of equal
  21074.  length, if the value cannot be represented (3.2.1.2)
  21075.  
  21076.  When a long integer is cast to a short, or a short is cast to a char, the
  21077.  least significant bytes are retained.
  21078.  
  21079.  For example, this line
  21080.  
  21081.    short x = (short)0x12345678L;
  21082.  
  21083.  assigns the value 0x5678 to  x, and this line
  21084.  
  21085.    char y = (char)0x1234;
  21086.  
  21087.  assigns the value 0x34 to  y.
  21088.  
  21089.  When signed variables are converted to unsigned and vice versa, the bit
  21090.  patterns remain the same. For example, casting -2 (0xFE) to an unsigned
  21091.  value yields 254 (also 0xFE).
  21092.  
  21093.  
  21094.  C.5.3  Signed Bitwise Operations
  21095.  
  21096.  The results of bitwise operations on signed integers (3.3)
  21097.  
  21098.  Bitwise operations on signed integers work the same as bitwise operations on
  21099.  unsigned integers. For example,  -16 & 99  can be expressed in binary as
  21100.  
  21101.    11111111 11110000
  21102.    & 00000000 01100011
  21103.      -----------------
  21104.      00000000 01100000
  21105.  
  21106.  The result of the bitwise AND is 96.
  21107.  
  21108.  
  21109.  C.5.4  Remainders
  21110.  
  21111.  The sign of the remainder on integer division (3.3.5)
  21112.  
  21113.  The sign of the remainder is the same as the sign of the dividend. For
  21114.  example,
  21115.  
  21116.    50 / -6 == -8
  21117.     50 % -6 ==  2
  21118.    -50 /  6 == -8
  21119.    -50 %  6 == -2
  21120.  
  21121.  
  21122.  C.5.5  Right Shifts
  21123.  
  21124.  The result of a right shift of a negative-value signed integral type
  21125.  (3.3.7)
  21126.  
  21127.  Shifting a negative value to the right yields half the absolute value,
  21128.  rounded down. For example, -253 (binary 11111111 00000011) shifted right one
  21129.  bit produces -127 (binary 11111111 10000001). A positive 253 shifts right to
  21130.  produce +126.
  21131.  
  21132.  Right shifts preserve the sign bit. When a signed integer shifts right, the
  21133.  mostsignificant bit remains set. When an unsigned integer shifts right, the
  21134.  mostsignificant bit is cleared. Thus, if 0xF000 is signed, a right shift
  21135.  produces 0xF800. If 0xF000 is unsigned, the result is 0x7800.
  21136.  
  21137.  Shifting a positive number right sixteen times produces 0x0000. Shifting a
  21138.  negative number right sixteen times produces 0xFFFF.
  21139.  
  21140.  
  21141.  C.6  Floating-Point Math
  21142.  
  21143.  
  21144.  C.6.1  Values
  21145.  
  21146.  The representations and sets of values of the various types of
  21147.  floating-point numbers (3.1.2.5)
  21148.  
  21149.  The float type contains 32 bits: 1 for the sign, 8 for the exponent, and 23
  21150.  for the mantissa. Its range is +/- 3.4E38 with at least 7 digits of
  21151.  precision.
  21152.  
  21153.  The double type contains 64 bits: 1 for the sign, 11 for the exponent, and
  21154.  52 for the mantissa. Its range is +/- 1.7E308 with at least 15 digits of
  21155.  precision.
  21156.  
  21157.  The long double type is new to Version 6.0 of Microsoft C. It contains 80
  21158.  bits: 1 for the sign, 15 for the exponent, and 64 for the mantissa. Its
  21159.  range is +/- 1.2E4932 with at least 17 digits of precision.
  21160.  
  21161.  
  21162.  C.6.2  Casting Integers to Floating-Point Values
  21163.  
  21164.  The direction of truncation when an integral number is converted to a
  21165.  floating-point number that cannot exactly represent the original value
  21166.  (3.2.1.3)
  21167.  
  21168.  When an integral number is cast to a floating-point value that cannot
  21169.  exactly represent the value, the value is rounded (up or down) to the
  21170.  nearest suitable value.
  21171.  
  21172.  For example, casting an unsigned long (with 32 bits of precision) to a float
  21173.  (whose mantissa has 23 bits of precision) rounds the number to the nearest
  21174.  multiple of 256. The long values 4294966913 - 4294967167 are all rounded to
  21175.  the float value 4294967040.
  21176.  
  21177.  
  21178.  C.6.3  Truncation of Floating-Point Values
  21179.  
  21180.  The direction of truncation or rounding when a floating-point number is
  21181.  converted to a narrower floating-point number (3.2.1.4)
  21182.  
  21183.  When an underflow occurs, the value of a floating-point variable is rounded
  21184.  down to zero. An overflow causes a run-time math error.
  21185.  
  21186.  
  21187.  C.7  Arrays and Pointers
  21188.  
  21189.  
  21190.  C.7.1  Largest Array Size
  21191.  
  21192.  The type of integer required to hold the maximum size of an array─that is,
  21193.  the size of size_t (3.3.3.4, 4.1.1)
  21194.  
  21195.  The size_t typedef is an unsigned short, with the range 0x0000 to 0xFFFF.
  21196.  Huge arrays can exceed this limit if they contain more than 65,535 elements.
  21197.  Arithmetic operations on huge arrays should therefore cast size_t and the
  21198.  results of an arithmetic operations on pointers to unsigned long.
  21199.  
  21200.  
  21201.  C.7.2  Casting Pointers
  21202.  
  21203.  The result of casting a pointer to an integer or vice versa (3.3.4)
  21204.  
  21205.  Near pointers are the same size as short integers; casting near to short (or
  21206.  short to near) has no immediate effect on the value.
  21207.  
  21208.  Far pointers and huge pointers are the same size as long integers. Casting
  21209.  far/huge to long (or long to far/huge) has no immediate effect on the value.
  21210.  
  21211.  
  21212.  When a near pointer is cast to a long, the 16-bit value is "normalized,"
  21213.  which means the segment (usually DS) and offset are combined to produce a
  21214.  32-bit memory location.
  21215.  
  21216.  When a far or huge pointer is cast to a short, the long value is truncated
  21217.  to a short.
  21218.  
  21219.  The compiler normalizes based pointers when necessary, unless the based
  21220.  pointer is a constant zero, in which case it is assumed to be a null
  21221.  pointer. See Chapter 13, "Writing Portable Programs," for more information
  21222.  about based pointers.
  21223.  
  21224.  
  21225.  C.7.3  Pointer Subtraction
  21226.  
  21227.  The type of integer required to hold the difference between two pointers to
  21228.  elements of the same array, ptrdiff_t (3.3.6, 4.1.1)
  21229.  
  21230.  A ptrdiff_t is a signed integer in the range -32768 to 32767, with one
  21231.  exception. Because huge pointers can address more than 64K of memory,
  21232.  subtracting one huge pointer from another can yield a result that is a long
  21233.  integer. The result of subtracting two huge pointers should be cast to a
  21234.  long.
  21235.  
  21236.  The compiler normalizes based pointers when necessary. In most cases, based
  21237.  pointers are treated as far pointers.
  21238.  
  21239.  
  21240.  C.8  Registers
  21241.  
  21242.  
  21243.  C.8.1  Availability of Registers
  21244.  
  21245.  The extent to which objects can actually be placed in registers by use of
  21246.  the register storage-class specifier (3.5.1)
  21247.  
  21248.  Two registers, SI and DI, are available in Microsoft C. Register variables
  21249.  with a type that has 16 bits may be allocated in these registers.
  21250.  
  21251.  
  21252.  C.9  Structures, Unions, Enumerations, and Bit Fields
  21253.  
  21254.  
  21255.  C.9.1  Improper Access to a Union
  21256.  
  21257.  A member of a union object is accessed using a member of a different type
  21258.  (3.3.2.3)
  21259.  
  21260.  If a union of two types is declared and one value is stored, but the union
  21261.  is accessed with the other type, the results are unreliable.
  21262.  
  21263.  For example, a union of float and int is declared. A float value is stored,
  21264.  but the program later accesses the value as an int. In such a situation, the
  21265.  value would depend on the internal storage of float values. The integer
  21266.  value would not be reliable.
  21267.  
  21268.  
  21269.  C.9.2  Sign of Bit Fields
  21270.  
  21271.  Whether a "plain" int field is treated as a signed int bit field or as an
  21272.  unsigned int bit field (3.5.2.1)
  21273.  
  21274.  Bit fields can be signed or unsigned. Plain bit fields are treated as
  21275.  signed.
  21276.  
  21277.  
  21278.  C.9.3  Storage of Bit Fields
  21279.  
  21280.  The order of allocation of bit fields within an int (3.5.2.1)
  21281.  
  21282.  Bit fields are allocated within a 16-bit integer from least-significant to
  21283.  mostsignificant bit. In the following code,
  21284.  
  21285.    struct mybitfields
  21286.    {
  21287.       unsigned a : 4;
  21288.       unsigned b : 5;
  21289.       unsigned c : 7;
  21290.    } test;
  21291.  
  21292.    void main( void )
  21293.    {
  21294.       test.a = 2;
  21295.       test.b = 31;
  21296.       test.c = 0;
  21297.    }
  21298.  
  21299.  the bits would be arranged as follows:
  21300.  
  21301.    00000001 11110010
  21302.    cccccccb bbbbaaaa
  21303.  
  21304.  Since the 80x86 processors store the low byte of integer values before the
  21305.  high byte, the integer 0x01F2 above would be stored in physical memory as
  21306.  0xF2 followed by 0x01.
  21307.  
  21308.  
  21309.  C.9.4  Alignment of Bit Fields
  21310.  
  21311.  Whether a bit field can straddle a storage-unit boundary (3.5.2.1)
  21312.  
  21313.  Bit fields default to size short, which can cross a byte boundary (see
  21314.  Section C.9.3 above) but not a 16-bit boundary. If the size and location of
  21315.  a bit field would cause it to overflow the current integer, the field is
  21316.  moved to the beginning of the next available integer.
  21317.  
  21318.  If a bit field is declared as a long, it can hold up to 32 bits.
  21319.  
  21320.  In either case, an individual field cannot cross a 16- or 32-bit boundary.
  21321.  
  21322.  
  21323.  C.9.5  The enum Type
  21324.  
  21325.  The integer type chosen to represent the values of an enumeration type
  21326.  (3.5.2.2)
  21327.  
  21328.  A variable declared as enum is a signed short integer.
  21329.  
  21330.  
  21331.  C.10  Qualifiers
  21332.  
  21333.  
  21334.  C.10.1  Access to Volatile Objects
  21335.  
  21336.  What constitutes an access to an object that has volatile-qualified type
  21337.  (3.5.3)
  21338.  
  21339.  Any reference to a volatile-qualified type is an access.
  21340.  
  21341.  
  21342.  C.11  Declarators
  21343.  
  21344.  
  21345.  C.11.1  Maximum Number
  21346.  
  21347.  The maximum number of declarators that can modify an arithmetic, structure,
  21348.  or union type (3.5.4)
  21349.  
  21350.  Microsoft C does not limit the number of declarators. The number is limited
  21351.  only by available memory.
  21352.  
  21353.  
  21354.  C.12  Statements
  21355.  
  21356.  
  21357.  C.12.1  Limits on Switch Statements
  21358.  
  21359.  The maximum number of case values in a switch statement (3.6.4.2)
  21360.  
  21361.  Microsoft C does not limit the number of case values in a switch statement.
  21362.  The number is limited only by available memory.
  21363.  
  21364.  
  21365.  C.13  Preprocessing Directives
  21366.  
  21367.  
  21368.  C.13.1  Character Constants and Conditional Inclusion
  21369.  
  21370.  Whether the value of a single-character character constant in a constant
  21371.  expression that controls conditional inclusion matches the value of the same
  21372.  character constant in the execution character set. Whether such a character
  21373.  constant can have a negative value (3.8.1)
  21374.  
  21375.  The character set used in preprocessor statements is the same as the
  21376.  execution character set. The preprocessor recognizes negative character
  21377.  values.
  21378.  
  21379.  
  21380.  C.13.2  Including Bracketed File Names
  21381.  
  21382.  The method for locating includable source files (3.8.2)
  21383.  
  21384.  The preprocessor first searches the directories specified by the CL option
  21385.  /I. If the /I option is not present or if it fails, the preprocessor uses
  21386.  the INCLUDE environment variable to find any include files within angle
  21387.  brackets. If more than one directory appears as part of the /I option or
  21388.  within the INCLUDE variable, the preprocessor searches them in the order
  21389.  they appear.
  21390.  
  21391.  For example, the command
  21392.  
  21393.    CL /ID:\MSC\INCLUDE MYPROG.C
  21394.  
  21395.  causes the preprocessor to search the directory D:\MSC\INCLUDE for include
  21396.  files such as STDIO.H.
  21397.  
  21398.  The commands
  21399.  
  21400.    SET INCLUDE = D:\MSC\INCLUDE
  21401.    CL MYPROG.C
  21402.  
  21403.  have a similar effect.
  21404.  
  21405.  If both sets of searches fail, a fatal error is generated.
  21406.  
  21407.  
  21408.  C.13.3  Including Quoted File Names
  21409.  
  21410.  The support for quoted names for includable source files (3.8.2)
  21411.  
  21412.  If the file name is fully specified, with a path that includes a colon (for
  21413.  example, F:\C6\SPECIAL\INCL\ORANGE.H), the preprocessor follows the path.
  21414.  
  21415.  If the file name is not fully specified, the preprocessor searches the
  21416.  directory of the file that included it. If the file is not found there, the
  21417.  preprocessor searches the parent directory, the parent's parent, and so on,
  21418.  terminating with the root directory.
  21419.  
  21420.  If the include file is not found in any of those directories, the rules for
  21421.  bracketed file names apply.
  21422.  
  21423.  
  21424.  C.13.4  Character Sequences
  21425.  
  21426.  The mapping of source file character sequences (3.8.2)
  21427.  
  21428.  Preprocessor statements use the same character set as source file statements
  21429.  with the exception that escape sequences are not supported.
  21430.  
  21431.  Thus, to specify a path for an include file, use only one backslash:
  21432.  
  21433.    #include "path1\path2\myfile"
  21434.  
  21435.  Within source code, two backslashes are necessary:
  21436.  
  21437.    fil = fopen( "path1\\path2\\myfile", "rt" );
  21438.  
  21439.  
  21440.  C.13.5  Pragmas
  21441.  
  21442.  The behavior on each recognized #pragma directive (3.8.6)
  21443.  
  21444.  The following pragmas are defined in the Microsoft C Reference:
  21445.  
  21446.  #pragma alloc_text                #pragma optimize
  21447.  #pragma check_pointer             #pragma pack
  21448.  #pragma check_stack               #pragma page
  21449.  #pragma comment                   #pragma pagesize
  21450.  #pragma function                  #pragma same_seg
  21451.  #pragma intrinsic                 #pragma skip
  21452.  #pragma linesize                  #pragma subtitle
  21453.  #pragma loop_opt                  #pragma title
  21454.  #pragma message
  21455.  
  21456.  
  21457.  C.13.6  Default Date and Time
  21458.  
  21459.  The definitions for _DATE_ and _TIME_ when, respectively, the date and time
  21460.  of translation are not available (3.8.8)
  21461.  
  21462.  When a hardware clock is not accessible, the default values for _DATE_ and
  21463.  _TIME_ are Friday, May 3, 1957 and 5:00 PM.
  21464.  
  21465.  
  21466.  C.14  Library Functions
  21467.  
  21468.  
  21469.  C.14.1  NULL Macro
  21470.  
  21471.  The null pointer constant to which the macro NULL expands (4.1.5)
  21472.  
  21473.  Several include files define the NULL macro as  ((void *)0).
  21474.  
  21475.  
  21476.  C.14.2  Diagnostic Printed by the assert Function
  21477.  
  21478.  The diagnostic printed by and the termination behavior of the assert
  21479.  function (4.2)
  21480.  
  21481.  The assert function prints a diagnostic message and calls the abort routine
  21482.  if the expression is false (0). The diagnostic message has the form
  21483.  
  21484.  Assertion failed: [expression], file [filename], line [linenumber]
  21485.  
  21486.  where filename is the name of the source file and linenumber is the line
  21487.  number of the assertion that failed in the source file. No action is taken
  21488.  if expression is true (nonzero).
  21489.  
  21490.  
  21491.  C.14.3  Character Testing
  21492.  
  21493.  The sets of characters tested for by the isalnum, isalpha, iscntrl, islower,
  21494.  isprint, and isupper functions (4.3.1)
  21495.  
  21496.  Function                          Tests For
  21497.  ────────────────────────────────────────────────────────────────────────────
  21498.  isalnum                           Characters 0 - 9, A-Z, a-z
  21499.                                    ASCII 48-57, 65-90, 97-122
  21500.  
  21501.  isalpha                           Characters A-Z, a-z
  21502.                                    ASCII 65-90, 97-122
  21503.  
  21504.  iscntrl                           ASCII 0 -31, 127
  21505.  
  21506.  islower                           Characters a-z
  21507.                                    ASCII 97-122
  21508.  
  21509.  isprint                           Characters A-Z, a-z, 0 - 9, punctuation,
  21510.                                    space
  21511.                                    ASCII 32-126
  21512.  
  21513.  isupper                           Characters A-Z
  21514.                                    ASCII 65-90
  21515.  
  21516.  
  21517.  C.14.4  Domain Errors
  21518.  
  21519.  The values returned by the mathematics functions on domain errors (4.5.1)
  21520.  
  21521.  The ERRNO.H file defines the domain error constant EDOM as 33.
  21522.  
  21523.  
  21524.  C.14.5  Underflow of Floating-Point Values
  21525.  
  21526.  Whether the mathematics functions set the integer expression errno to the
  21527.  value of the macro ERANGE on underflow range errors (4.5.1)
  21528.  
  21529.  A floating-point underflow does not set the expression errno to ERANGE. When
  21530.  a value approaches zero and eventually underflows, the value is set to zero.
  21531.  
  21532.  
  21533.  
  21534.  C.14.6  The fmod Function
  21535.  
  21536.  Whether a domain error occurs or zero is returned when the fmod function has
  21537.  a second argument of zero (4.5.6.4)
  21538.  
  21539.  When the fmod function has a second argument of zero, the function returns
  21540.  zero.
  21541.  
  21542.  
  21543.  C.14.7  The signal Function
  21544.  
  21545.  The set of signals for the signal function (4.7.1.1)
  21546.  
  21547.  The first argument passed to signal must be one of the symbolic constants
  21548.  listed below. The constants are defined in SIGNAL.H. Also listed is the
  21549.  operating mode support for each signal.
  21550.  
  21551.  Signal Argument                   Description
  21552.  ────────────────────────────────────────────────────────────────────────────
  21553.  SIGABRT                           Abnormal termination (real and protected
  21554.                                    mode).
  21555.  
  21556.  SIGBREAK                          CTRL+BREAK signal. Terminates the
  21557.                                    calling program (protected mode only).
  21558.  
  21559.  SIGFPE                            Floating-point error, such as overflow,
  21560.                                    division by zero, or invalid operation.
  21561.                                    Terminates the calling program (real and
  21562.                                    protected mode).
  21563.  
  21564.  SIGILL                            Illegal instruction. Terminates the
  21565.                                    calling program (protected mode only).
  21566.  
  21567.  SIGINT                            CTRL+C interrupt. Issues INT 23H (real
  21568.                                    and
  21569.                                    protected mode).
  21570.  
  21571.  SIGSEGV                           Illegal storage access. Not generated by
  21572.                                    DOS or OS/2, but supported for ANSI
  21573.                                    compatibility. Terminates the calling
  21574.                                    program (real and protected mode).
  21575.  
  21576.  SIGTERM                           Termination request sent to the program.
  21577.                                    Not generated by DOS or OS/2, but
  21578.                                    supported for ANSI compatibility.
  21579.                                    Terminates the calling program (real and
  21580.                                    protected mode).
  21581.  
  21582.  SIGUSR1                           OS/2 process flag A (protected mode
  21583.                                    only).
  21584.  
  21585.  SIGUSR2                           OS/2 process flag B (protected mode
  21586.                                    only).
  21587.  
  21588.  SIGUSR3                           OS/2 process flag C (protected mode
  21589.                                    only).
  21590.  
  21591.  
  21592.  C.14.8  Default Signals
  21593.  
  21594.  If the equivalent of signal (sig, SIG_DFL) is not executed prior to the call
  21595.  of a signal handler, the blocking of the signal that is performed (4.7.1.1)
  21596.  
  21597.  
  21598.  Signals are set to their default status when a program begins running.
  21599.  
  21600.  
  21601.  C.14.9  The SIGILL Signal
  21602.  
  21603.  Whether the default handling is reset if the SIGILL signal is received by a
  21604.  handler specified to the signal function (4.7.1.1)
  21605.  
  21606.  The SIGILL signal applies to OS/2 applications only. When SIGILL is
  21607.  received, the signal handling is not reset to the default SIG_DFL.
  21608.  
  21609.  
  21610.  C.14.10  Terminating Newline Characters
  21611.  
  21612.  Whether the last line of a text stream requires a terminating newline
  21613.  character (4.9.2)
  21614.  
  21615.  Stream functions recognize either newline or end-of-file as the terminating
  21616.  character for a line.
  21617.  
  21618.  
  21619.  C.14.11  Blank Lines
  21620.  
  21621.  Whether space characters that are written out to a text stream immediately
  21622.  before a newline character appear when read in (4.9.2)
  21623.  
  21624.  Space characters are preserved.
  21625.  
  21626.  
  21627.  C.14.12  Null Characters
  21628.  
  21629.  The number of null characters that can be appended to data written to a
  21630.  binary stream (4.9.2)
  21631.  
  21632.  Any number of null characters can be appended to a binary stream.
  21633.  
  21634.  
  21635.  C.14.13  File Position in Append Mode
  21636.  
  21637.  Whether the file position indicator of an append mode stream is initially
  21638.  positioned at the beginning or end of the file (4.9.3)
  21639.  
  21640.  When a file is opened in append mode, the file position indicator initially
  21641.  points to the end of the file.
  21642.  
  21643.  
  21644.  C.14.14  Truncation of Text Files
  21645.  
  21646.  Whether a write on a text stream causes the associated file to be truncated
  21647.  beyond that point (4.9.3)
  21648.  
  21649.  Writing to a text stream does not truncate the file beyond that point.
  21650.  
  21651.  
  21652.  C.14.15  File Buffering
  21653.  
  21654.  The characteristics of file buffering (4.9.3)
  21655.  
  21656.  Disk files accessed through standard I/O functions are fully buffered. By
  21657.  default, the buffer holds 512 bytes. Some of the low-level DOS and BIOS
  21658.  functions (all of which are non-ANSI) are unbuffered.
  21659.  
  21660.  
  21661.  C.14.16  Zero-Length Files
  21662.  
  21663.  Whether a zero-length file actually exists (4.9.3)
  21664.  
  21665.  Files with a length of zero are permitted.
  21666.  
  21667.  
  21668.  C.14.17  File Names
  21669.  
  21670.  The rules for composing valid file names (4.9.3)
  21671.  
  21672.  A file specification can include an optional drive letter (always followed
  21673.  by a colon), a series of optional directory names (separated by
  21674.  backslashes), and a file name.
  21675.  
  21676.  File names and directory names can contain up to eight characters followed
  21677.  by a period and a three-character extension. Case is ignored. The wild-card
  21678.  characters * and ? are not permitted within the name or extension.
  21679.  
  21680.  
  21681.  C.14.18  File Access Limits
  21682.  
  21683.  Whether the same file can be open multiple times (4.9.3)
  21684.  
  21685.  Opening a file that is already open is not permitted.
  21686.  
  21687.  
  21688.  C.14.19  Deleting Open Files
  21689.  
  21690.  The effect of the remove function on an open file (4.9.4.1)
  21691.  
  21692.  The remove function deletes a file, even if the file is open.
  21693.  
  21694.  
  21695.  C.14.20  Renaming with a Name that Exists
  21696.  
  21697.  The effect if a file with the new name exists prior to a call to the rename
  21698.  function (4.9.4.2)
  21699.  
  21700.  If you attempt to rename a file using a name that exists, the rename
  21701.  function fails and returns an error code.
  21702.  
  21703.  
  21704.  C.14.21  Printing Pointer Values
  21705.  
  21706.  The output for %p conversion in the fprintf function (4.9.6.1)
  21707.  
  21708.  Microsoft C supports three types of pointer conversions: %p (a pointer), %lp
  21709.  (a 32-bit far pointer), and %hp (a 16-bit near pointer).
  21710.  
  21711.  The fprintf function produces hexadecimal values of the form XXXX (an
  21712.  offset) for near pointers or XXXX:XXXX (a segment plus an offset, separated
  21713.  by a colon) for far pointers. The output for %p depends on the memory model
  21714.  in use.
  21715.  
  21716.  
  21717.  C.14.22  Reading Pointer Values
  21718.  
  21719.  The input for %p conversion in the fscanf function (4.9.6.2)
  21720.  
  21721.  When the %p format character is specified, the fscanf function converts
  21722.  pointers from hexadecimal ASCII values into the correct address.
  21723.  
  21724.  
  21725.  C.14.23  Reading Ranges
  21726.  
  21727.  The interpretation of a dash (-) character that is neither the first nor the
  21728.  last character in the scanlist for % [ conversion in the fscanf function
  21729.  (4.9.6.2)
  21730.  
  21731.  The following line
  21732.  
  21733.    fscanf( fileptr, "%[A-Z]", strptr);
  21734.  
  21735.  reads any number of characters in the range A-Z into the string to which
  21736.  strptr  points.
  21737.  
  21738.  
  21739.  C.14.24  File Position Errors
  21740.  
  21741.  The value to which the macro errno is set by the fgetpos or ftell function
  21742.  on failure (4.9.9.1, 4.9.9.4)
  21743.  
  21744.  When fgetpos or ftell fails, errno is set to the manifest constant EINVAL if
  21745.  the position is invalid or EBADF if the file number is bad. The constants
  21746.  are defined in ERRNO.H.
  21747.  
  21748.  
  21749.  C.14.25  Messages Generated by the perror Function
  21750.  
  21751.  The messages generated by the perror function (4.9.10.4)
  21752.  
  21753.  The perror function generates these messages:
  21754.  
  21755.    0  Error 0
  21756.    1
  21757.    2  No such file or directory
  21758.    3
  21759.    4
  21760.    5
  21761.    6
  21762.    7  Arg list too long
  21763.    8  Exec format error
  21764.    9  Bad file number
  21765.    10
  21766.    11
  21767.    12 Not enough core
  21768.    13 Permission denied
  21769.    14
  21770.    15
  21771.    16
  21772.    17 File exists
  21773.    18 Cross-device link
  21774.    19
  21775.    20
  21776.    21
  21777.    22 Invalid argument
  21778.    23
  21779.    24 Too many open files
  21780.    25
  21781.    26
  21782.    27
  21783.    28 No space left on device
  21784.    29
  21785.    30
  21786.    31
  21787.    32
  21788.    33 Math argument
  21789.    34 Result too large
  21790.    35
  21791.    36 Resource deadlock would occur
  21792.  
  21793.  
  21794.  C.14.26  Allocating Zero Memory
  21795.  
  21796.  The behavior of the calloc, malloc, or realloc function if the size
  21797.  requested is zero (4.10.3)
  21798.  
  21799.  The calloc, malloc, and realloc functions accept zero as an argument. No
  21800.  actual memory is allocated, but the memory size can be modified later by
  21801.  realloc.
  21802.  
  21803.  
  21804.  C.14.27  The abort Function
  21805.  
  21806.  The behavior of the abort function with regard to open and temporary  files
  21807.  (4.10.4.1)
  21808.  
  21809.  The abort function does not close files that are open or temporary. It does
  21810.  not flush stream buffers.
  21811.  
  21812.  
  21813.  C.14.28  The atexit Function
  21814.  
  21815.  The status returned by the atexit function if the value of the argument is
  21816.  other than zero, EXIT_SUCCESS, or EXIT_FAILURE (4.10.4.3r)
  21817.  
  21818.  The atexit function returns zero if successful, or a nonzero value if
  21819.  unsuccessful.
  21820.  
  21821.  
  21822.  C.14.29  Environment Names
  21823.  
  21824.  The set of environment names and the method for altering the environment
  21825.  list used by the getenv function (4.10.4.4)
  21826.  
  21827.  The set of environment names is unlimited.
  21828.  
  21829.  To change environment variables from within a C program, call the putenv
  21830.  function. To change environment variables from the DOS command line, use the
  21831.  SET command (for example, SET LIB = D:\ LIBS).
  21832.  
  21833.  Environment variables exist only as long as their host copy of DOS is
  21834.  running. For example, the line
  21835.  
  21836.    system( "SET LIB = D:\LIBS" );
  21837.  
  21838.  would run a copy of DOS, set the environment variable LIB, and return to the
  21839.  C program, exiting the secondary copy of DOS. Exiting that copy of DOS
  21840.  removes the temporary environment variable LIB.
  21841.  
  21842.  Likewise, changes made by the putenv function last only until the program
  21843.  ends.
  21844.  
  21845.  
  21846.  C.14.30  The system Function
  21847.  
  21848.  The contents and mode of execution of the string by the system function
  21849.  (4.10.4.5)
  21850.  
  21851.  The system function executes an internal DOS or OS/2 command, or an EXE,
  21852.  COM, or BAT file from within a C program rather than from the command line.
  21853.  
  21854.  
  21855.  It examines the COMSPEC environment variable to find the command
  21856.  interpreter, which is typically COMMAND.COM in DOS or CMD.EXE in OS/2. The
  21857.  system function then passes the argument string to the command interpreter.
  21858.  
  21859.  
  21860.  
  21861.  C.14.31  The strerror Function
  21862.  
  21863.  The contents of the error message strings returned by the strerror function
  21864.  (4.11.6.2)
  21865.  
  21866.  The strerror function generates these messages:
  21867.  
  21868.    0   Error 0
  21869.    1
  21870.    2   No such file or directory
  21871.    3
  21872.    4
  21873.    5
  21874.    6
  21875.    7   Arg list too long
  21876.    8   Exec format error
  21877.    9   Bad file number
  21878.    10
  21879.    11
  21880.    12  Not enough core
  21881.    13  Permission denied
  21882.    14
  21883.    15
  21884.    16
  21885.    17  File exists
  21886.    18  Cross-device link
  21887.    19
  21888.    20
  21889.    21
  21890.    22  Invalid argument
  21891.    23
  21892.    24  Too many open files
  21893.    25
  21894.    26
  21895.    27
  21896.    28  No space left on device
  21897.    29
  21898.    30
  21899.    31
  21900.    32
  21901.    33  Math argument
  21902.    34  Result too large
  21903.    35
  21904.    36  Resource deadlock would occur
  21905.  
  21906.  
  21907.  C.14.32  The Time Zone
  21908.  
  21909.  The local time zone and Daylight Saving Time (4.12.1)
  21910.  
  21911.  The local time zone is Pacific Standard Time. Microsoft C supports Daylight
  21912.  Saving Time.
  21913.  
  21914.  
  21915.  C.14.33  The clock Function
  21916.  
  21917.  The era for the clock function (4.12.2.1)
  21918.  
  21919.  The clock function's era begins (with a value of 0) when the C program
  21920.  starts to execute. It returns times measured in 1/1000th seconds.
  21921.  
  21922.  
  21923.  
  21924.  
  21925.  INDEX
  21926.  ──────────────────────────────────────────────────────────────────────────
  21927.  
  21928.  
  21929.  
  21930.  80x87 coprocessor
  21931.  80x87
  21932.    Detection of
  21933.  
  21934.      A
  21935.  Alternate math package
  21936.  
  21937.      B
  21938.  _based keyword
  21939.  
  21940.      C
  21941.  C extensions, PWB
  21942.    building protected-mode
  21943.    building real-mode
  21944.    calling C library functions
  21945.    calling C library routines
  21946.    calling PWB functions
  21947.    describing functions and switches
  21948.    initializing functions
  21949.    prototyping functions
  21950.    receiving parameters
  21951.    sample
  21952.    versus executable files
  21953.  C extentions, PWB
  21954.    building protected-mode
  21955.  Calls
  21956.    Emulator math package
  21957.    Emulator package
  21958.    Floating-point
  21959.    Math coprocessor package
  21960.  CODEVIEW.LST file
  21961.  CodeView
  21962.    debugging
  21963.      DLLs with
  21964.  CONFIG.SYS file
  21965.  CONFIG.SYS files
  21966.  _control87
  21967.  Coordinates
  21968.    overview
  21969.    physical
  21970.    screen location
  21971.    viewport
  21972.    window
  21973.  Coprocessor
  21974.  CURRENT.STS
  21975.  
  21976.      D
  21977.  Default math package
  21978.  Denormalized numbers
  21979.  Dot commands
  21980.  double
  21981.  
  21982.      E
  21983.  80x87 coprocessor
  21984.  80x87
  21985.    Detection of
  21986.  EMOEM.ASM
  21987.  Emulator math package
  21988.  Emulator package
  21989.    In-line
  21990.    With dynamic-link libraries
  21991.  Environment
  21992.    NO87 variable
  21993.  
  21994.      F
  21995.  _far keyword
  21996.  _fastcall keyword
  21997.  Files
  21998.    .FON
  21999.    CODEVIEW.LST
  22000.    CONFIG.SYS
  22001.    CURRENT.STS
  22002.    TOOLS.INI See TOOLS.INI
  22003.  Fill patterns
  22004.  float
  22005.  Floating-point accumulator
  22006.  Floating-point math
  22007.    requirements, DLLs
  22008.  Floating-point
  22009.    Alternate math package
  22010.    Biased exponent
  22011.    Compatibility between options
  22012.  Floating-Point
  22013.    Controlling
  22014.  Floating-point
  22015.    Default math package
  22016.    Default package
  22017.    Denormalized numbers
  22018.    Effect of calls on code size
  22019.    Effect of calls on speed
  22020.    Effect on optimization
  22021.    Emulator package
  22022.    Exception masking
  22023.    Exceptions
  22024.    exponent
  22025.    Fastest programs
  22026.    In libraries
  22027.    Infinities
  22028.    Interrupt-enable
  22029.    Library considerations for
  22030.    Mantissa
  22031.    Math coprocessor package
  22032.    Maximizing accuracy
  22033.    NaNs
  22034.    On non-IBM compatible computers
  22035.    Packages
  22036.    Precision
  22037.    Program size
  22038.    Program speed
  22039.    Sign bit
  22040.    Smallest programs
  22041.    Transcendental function results
  22042.    Underflow
  22043.    Using in dynamic-link libraries
  22044.    With libraries not provided by Microsoft
  22045.  .FON files
  22046.  /FPa
  22047.  /FPc
  22048.  /FPc87
  22049.  /FPi
  22050.  /FPi87
  22051.  Function calls
  22052.     near call
  22053.    C calling convention
  22054.    FORTRAN/Pascal calling convention
  22055.    register calling convention
  22056.    _fastcall calling convention
  22057.  Functions
  22058.    drive and directory (list)
  22059.    graphics (list)
  22060.    initializing
  22061.    prototyping
  22062.    Returning floating-point types
  22063.    WhenLoaded
  22064.  
  22065.      G
  22066.  /Gd option
  22067.  Gd option
  22068.  Graphics
  22069.    video modes
  22070.       default mode
  22071.       graphics mode, defined
  22072.       text mode, defined
  22073.  
  22074.      H
  22075.  Help files
  22076.    local help context
  22077.  HIMEM.SYS driver
  22078.  _huge keyword
  22079.  _huge Keyword
  22080.  _huge keyword
  22081.  
  22082.      I
  22083.  IEEE
  22084.  In-line assembly
  22085.    advantages
  22086.  In-line
  22087.    Floating-point emulator package
  22088.    Floating-point emulator
  22089.    Floating-point instructions
  22090.    Floating-point math coprocessor package
  22091.    Floating-point
  22092.  Institute of Electrical and Electronics Engineers
  22093.     see IEEE; see
  22094.  
  22095.      L
  22096.  Language conventions
  22097.    calling conventions
  22098.    naming conventions
  22099.    parameter-passing conventions
  22100.  Libraries
  22101.    dynamic-link See DLLs
  22102.    import
  22103.    special
  22104.  LINK
  22105.    /EXEPACK option
  22106.    /FARCALLTRANSLATION option
  22107.    /NODEFAULTLIBRARYSEARCH (/NOD) option
  22108.    /NOEXTENDEDDICTSEARCH (/NOE) option
  22109.    /NOIGNORECASE (/NOI) option
  22110.    /PACKCODE option
  22111.    /PACKDATA option
  22112.    /PADCODE option
  22113.    /PADDATA option
  22114.    /TINY option
  22115.    compatibility (/Lc)
  22116.    PACKCODE option
  22117.    protected-mode (/Lp)
  22118.    real-mode (/Lr)
  22119.  LLIBCDLL.LIB
  22120.  long double
  22121.  
  22122.      M
  22123.  Macros
  22124.    inherited
  22125.  Math coprocessor package
  22126.    In-line
  22127.  
  22128.      N
  22129.  _near keyword
  22130.  NO87
  22131.  
  22132.      O
  22133.  optimise pragma
  22134.  Optimization
  22135.    Effect of floating-point math on
  22136.  optimize pragma
  22137.  OS/2
  22138.    calling
  22139.  
  22140.      P
  22141.  Process
  22142.    child
  22143.    debugging multiple processes
  22144.  Programmer's WorkBench
  22145.     see PWB; see
  22146.  Pseudotargets
  22147.  PWB
  22148.    80x87 option
  22149.    Debug Build Options
  22150.    Emulation calls option
  22151.    extensions.See C extensions, PWB
  22152.    Fast alternate math option
  22153.    Global Compile Options
  22154.    Inline 80x87 Instructions option
  22155.    Inline Emulation option
  22156.    Release Build Options
  22157.    Selecting floating-point options from
  22158.  
  22159.      R
  22160.  Run-time
  22161.    support of type long double
  22162.  
  22163.      S
  22164.  SETUP
  22165.  SLLs
  22166.    data segments
  22167.  
  22168.      T
  22169.  Threads
  22170.    _beginthread function
  22171.    _endthread function
  22172.  /TINY option
  22173.  Type
  22174.    double
  22175.    float
  22176.    long double
  22177.    Promotion of floating point
  22178.    Range of floating-point
  22179.    Significance of floating-point
  22180.    Storage requirements for floating-point
  22181.    Widening for floating-point types
  22182.  Types
  22183.    double
  22184.    float
  22185.    long double
  22186.  
  22187.      V
  22188.  Variables
  22189.    Declaring as floating-point types
  22190.    Precision with floating-point
  22191.    Promotion of floating-point
  22192.    Range of floating-point
  22193.    Significance of
  22194.    Storage requirements for
  22195.  
  22196.