% % $Id$ % This file is part of the FPC documentation. % Copyright (C) 1997, by Michael Van Canneyt % % The FPC documentation is free text; you can redistribute it and/or % modify it under the terms of the GNU Library General Public License as % published by the Free Software Foundation; either version 2 of the % License, or (at your option) any later version. % % The FPC Documentation is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU % Library General Public License for more details. % % You should have received a copy of the GNU Library General Public % License along with the FPC documentation; see the file COPYING.LIB. If not, % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, % Boston, MA 02111-1307, USA. % \documentclass{report} \usepackage{a4} \usepackage{html} \makeindex \latex{\usepackage{multicol}} \latex{\usepackage{fpkman}} \html{\input{fpk-html.tex}} % define the version number here, and not in the fpk.sty !!! \newcommand{\fpkversion}{0.9.5} \newcommand{\remark}[1]{\par$\rightarrow$\textbf{#1}\par} % define many-used references. %\newcommand{\progref}{\htmladdnormallink{Programmer's guide}{../prog/prog.html}\ } %\newcommand{\refref}{\htmladdnormallink{Reference guide}{../ref/ref.html}\ } %\newcommand{\seecrt}{\htmladdnormallink{CRT}{../crt/crt.html}} %\newcommand{\seelinux}{\htmladdnormallink{Linux}{../linux/linux.html}} %\newcommand{\seestrings}{\htmladdnormallink{strings}{../strings/strings.html}} %\newcommand{\seedos}{\htmladdnormallink{DOS}{../dos/dos.html}} %\newcommand{\seegetopts}{\htmladdnormallink{getopts}{../getopts/getopts.html}} %\newcommand{\seeobjects}{\htmladdnormallink{objects}{../objects/objects.html}} %\newcommand{\seegraph}{\htmladdnormallink{graph}{../graph/graph.html}} %\newcommand{\seeprinter}{\htmladdnormallink{printer}{../printer/printer.html}} %\newcommand{\seego}{\htmladdnormallink{GO32}{../go32/go32.html}} % \newcommand{\olabel}[1]{\label{option:#1}} % We should change this to something better. See \seef etc. \newcommand{\seeo}[1]{See \ref{option:#1}} \begin{document} \title{Free Pascal :\\ User's manual} \docdescription{User's manual for \fpk, version \fpkversion} \docversion{1.0} \date{July 1997} \author{Micha\"el Van Canneyt\\Florian Kl\"ampfl} \maketitle \tableofcontents %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Introduction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Introduction} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % About this document \section{About this document} This is the user's manual for \fpk . It describes the installation and use of the \fpk compiler on the different supported platforms. It does not attempt to give an exhaustive list of all supported commands, nor a definition of the Pascal language. Look at the \refref for these things. For a description of the possibilities and the inner workings of the compiler, see the \progref. In the appendices of this document you will find lists of reserved words and compiler error messages (with descriptions). This document describes the compiler as it is/functions at the time of writing. Since the compiler is under continuous development, some of the things described here may be outdated. In case of doubt, consult the \file{README} files, distributed with the compiler. The \file{README} files are, in case of conflict with this manual, authoritative. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % About the compiler \section{About the compiler} \fpk is a 32-bit compiler for the i386 processor\footnote{Work is being done on a port to ALPHA Architecture}. Currently, it supports 2 operating systems: \begin{itemize} \item \dos \item \linux \end{itemize} and work is in progress to port it to other platforms (notably, \ostwo and \windowsnt). \fpk is designed to be, as much as possible, source compatible with Turbo Pascal 7.0 and Delphi II (although this goal is not yet attained), but it also enhances these languages with elements like function overloading. And, unlike these ancestors, it supports multiple platforms. It also differs from them in the sense that you cannot use compiled units from one system for the other. Also, at the time of writing, there is no Integrated Development Environment (IDE) available for \fpk. This gap will, hopefully, be filled in the future. \fpk consists of three parts : \begin{enumerate} \item The compiler program itself. \item The Run-Time Library (RTL). \item Utility programs and units. \end{enumerate} Of these you only need the first two, in order to be able to use the compiler. In this document, we describe the use of the compiler. The RTL is described in the \refref. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Getting more information. \section{Getting more information.} If the documentation doesn't give an answer to your questions, you can obtain more information on the Internet, on the following addresses: \begin{itemize} \item \htmladdnormallink{http://tfdec1.fys.kuleuven.ac.be/\~ michael/fpk.html} {http://tfdec1.fys.kuleuven.ac.be/~michael/fpk.html} contains information on the \linux port of the compiler. It contains also useful mail addresses and links to other places. \item \htmladdnormallink{http://www.brain.uni-freiburg.de/\~klaus/fpk-pas} {http://www.brain.uni-freiburg.de/~klaus/fpk-pas} is the main \fpk information site. It also contains the instructions for inscribing to the \textit{mailing-list}, another useful source of information. \end{itemize} Both places can be used to download the \fpk distribution, although you can probably find them on other places also. Finally, if you think something should be added to this manual (entirely possible), please do not hesitate and contact me at \htmladdnormallink{michael@tfdec1.fys.kuleuven.ac.be}{mailto:michael@tfdec1.fys.kuleuven.ac.be} . Let's get on with something useful. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Installation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Installing the compiler} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Before Installation : Requirements \section{Before Installation : Requirements} % % % System requirements \subsection{System requirements} The compiler needs at least the following hardware: \begin{enumerate} \item An I386 or higher processor. A coprocessor is not required, although it will slow down your program's performance if you do floating point calculations. \item 4 Mb of free memory. Under \dos, if you use DPMI memory management, such as under Windows, you will need at least 8 Mb. \item At least 500 Kb. free disk space. \end{enumerate} % % % Software requirements \subsection{Software requirements} \subsubsection{Under DOS} The \dos distribution contains all the files you need to run the compiler and compile pascal programs. \subsubsection{Under Linux} Under \linux you need to have the following programs installed : \begin{enumerate} \item \gnu \file{as}, the \gnu assembler. \item \gnu \file{ld}, the \gnu linker. \item Optionally (but highly recommended) : \gnu \file{make}. For easy recompiling of the compiler and Run-Time Library, this is needed. \end{enumerate} Other than that, \fpk should run on almost any \linux system. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Installing the compiler. \section{Installing the compiler.} The installation of \fpk is easy, but is platform-dependent. We discuss the process for each platform separately. % % % Installing under DOS \subsection{Installing under DOS} \subsubsection{Mandatory installation steps.} First, you must get the latest distribution files of \fpk. They come as zip files, which you must unzip first. The distribution zip file contains an installation program \file{INSTALL.EXE}. You must run this program to install the compiler. It allows you to select: \begin{itemize} \item What components you wish to install. (e.g do you want the sources or not, do you want Free Vision etc.) \item Where you want to install (the default location is \verb|C:\PP|). \end{itemize} The installation program generates a batch file which sets some environment variables : \verb|SET_PP.BAT|. This file is located in the directory where you installed \fpk. The installation program doesn't modify the \file{AUTOEXEC.BAT}, since many people (including the authors of \fpk) don't like this. You can choose to insert a call to this batch file in your \file{AUTOEXEC.BAT} file, like this : \begin{verbatim} CALL C:\PP\SET_PP.BAT \end{verbatim} (This is assuming that you installed \fpk in the default location.) In order to run \fpk from any directory on your system, you must extend your path variable to contain the \verb|C:\PP\BIN| directory. You can choose to do this in your \file{AUTOEXEC.BAT} file, but you can also insert a statement in the \verb|SET_PP.BAT| file. Whatever the location you choose, It should look something like this : \begin{verbatim} SET PATH=%PATH%;C:\PP\BIN \end{verbatim} (Again, assuming that you installed in the default location). If you want to use the graphic drivers you must modify the environment variable \var{GO32}. Instructions for doing this can be found in the documentation of the Graph unit, at the InitGraph procedure. \subsubsection{Optional Installation: The coprocessor emulation} For people who have an older CPU type, without math coprocessor (i387) it is necessary to install a coprocessor emulation, since \fpk uses the coprocessor to do all floating point operations. The installation of the coprocessor emulation is handled by the installation program (\file{INSTALL.EXE}). However, the installation program has currently a bug: If you select the coprocessor emulation the program ignores this and you must do this by hand. You should change the \var{GO32} environment variable in the \verb|SET_PP.BAT| file, as follows: \begin{verbatim} SET GO32=emu C:\PP\DRIVERS\EMU387 \end{verbatim} % % % Installing under Linux \subsection{Installing under Linux} \subsubsection{Mandatory installation steps.} The \linux distribution of \fpk comes in two flavors: \begin{itemize} \item an \file{aout} version, and \item an \file{ELF} version. \end{itemize} If you don't know which of these flavors you must use, contact you system administrator, and he will tell you. When that doesn't get you further, try the \file{ELF} distribution. \file{aout} systems are outdated, and may not be supported any more in the future. Both flavors are shipped in \file{tar} archive files. This means that you should untar them, in some directory where you have write permission, using the following command: \begin{verbatim} tar -xvf fpk.tar \end{verbatim} We supposed here that you downloaded the file \file{fpk.tar} somewhere from the Internet. When the files are untarred, you will be left with more archive files, and two install programs: an installation shell script, and a X-windows installation program. Both have the same functionality. To install \fpk, all that you need to do now is give the following command: \begin{verbatim} ./install.sh \end{verbatim} Or, if you have the XForms libraries, you can start the X-Windows based program. \begin{verbatim} ./fpkinstall \end{verbatim} And then you must answer some questions. They're very simple, they're concerned with 2 things : \begin{enumerate} \item Places where you can install different things. \item Deciding if you want to install certain components (such as sources and demo programs). \end{enumerate} If you run the installation program/script as \var{root}, you can just accept all installation defaults. If you don't run as \var{root}, you must take care to supply the installation program with directory names where you have write permission, as it will attempt to create the directories you specify. In principle, you can install it wherever you want, though. Whatever the installation program you used, at the end of installation, the installation program will generate a configuration file for the \fpk compiler which reflects the settings that you chose. It will install this file in the \file{/etc} directory, (if you are not installing as \var{root}, this will fail, and in the directory where you installed the libraries. If you want the \fpk compiler to use this configuration file, it must be present in \file{/etc}, or you can set the environment variable \var{PPC\_CONFIG\_PATH}. Under \file{csh}, you can do this by adding a \begin{verbatim} setenv PPC_CONFIG_PATH /usr/lib/ppc/0.9.1 \end{verbatim} line to your \file{.login} file in your home directory. (see also the next section) \subsubsection{Optional configuration steps} You may wish to set some environment variables. The \linux version of \fpk recognizes the following variables : \begin{itemize} \item \verb|PPC_EXEC_PATH| contains the directory where '\file{as}' and '\file{ld}' are. (default \file{/usr/bin}) \item \verb|PPC_GCCLIB_PATH| contains the directory where \file{libgcc.a} is (no default) \item \verb|PPC_CONFIG_PATH| specifies an alternate path to find \file{ppc386.cfg} (default \file{/etc}) \item \verb|PPC_ERROR_FILE| specifies the path and name of the error-definition file. (default \file{/usr/lib/ppc/errorE.msg}) \end{itemize} These locations are, however, set in the sample configuration file which is built at the end of the installation process, except for the \verb|PPC_CONFIG_PATH| variable, which you must set if you didn't install things in the default places. \subsubsection{finally} Also distributed in \fpk is a README file. It contains the latest instructions for installing \fpk, and should always be read first. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Testing the compiler \section{Testing the compiler} After the installation is completed and the environment variables are set as described above, your first program can be compiled. Included in the \fpk distribution are some demonstration programs, showing what the compiler can do. You can test if the compiler functions correctly by trying to compile these programs. The compiler is called \begin{itemize} \item \file{PPC386.EXE} under \dos, and \item \file{ppc386} under \linux \end{itemize} To compile a program (e.g \verb|demo\hello.pp|) simply type : \begin{verbatim} ppc386 -a hello \end{verbatim} at the command prompt. The option -a is needed currently to call the external assembler. This needed option will disappear when the the internal assembler works stable. but at the moment it is a mandatory option. If you got no error messages, the compiler has generated an executable called \file{hello} (no extension) under \linux, and a file \file{hello.exe} under \dos. To execute the program, simply type : \begin{verbatim} hello \end{verbatim} If all went well, you should see the following friendly greeting: \begin{verbatim} Hello world \end{verbatim} In the \dos case, this friendly greeting may be preceded by some ugly message from the \file{GO32} extender program. This unfriendly behavior can be switched off by setting the \file{GO32} environment variable. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Usage %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Compiler usage} Here we describe the essentials to compile a program and a unit. We also describe how to make a stand-alone executable of the compiled program under \dos. For more advanced uses of the compiler, see the section on configuring the compiler, and the \progref. The examples in this section suppose that you have a \file{ppc386.cfg} which is set up correctly, and which contains at least the path setting for the RTL units. In principle this file is generated by the installation program. You may have to check that it is in the correct place (see section \ref{se:config_file} for more information on this). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Compiling a program \section{Compiling a program} Compiling a program is very simple. Assuming that you have a program source in the file \file{prog.pp}, you can compile this with the following command: \begin{verbatim} ppc386 [options] prog.pp \end{verbatim} The square brackets [] indicate that what is between them is optional. If your program file has the \file{.pp} or \file{.pas} extension, you can omit this on the command line, e.g. in the previous example you could have typed: \begin{verbatim} ppc386 [options] prog \end{verbatim} If all went well, the compiler will produce an executable, or, for version 1 of the \dos extender, a file which can be converted to an executable. Under \linux and version 2 of the \dos extender, the file you obtained is the executable. You can execute it straight away, you don't need to do anything else. Under \dos, additional processing is required. See the section on creating an executable. You will notice that there are also other files in your directory, with extensions \file{.o} and \file{.s}. These contain, respectively, the assembler sources and the object files for your program. You can safely delete the assembler file, you don't need it any more\footnote{One day this will be done automatically.}. If you compiled a program, you can delete the object file (\file{.o}), but not if you compiled a unit. Then the object file contains the code of the unit, and will be linked in any program that uses the unit you compiled, so you shpuldn't remove it. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Compiling a unit \section{Compiling a unit} Compiling a unit is not essentially different from compiling a program. The difference is mainly that the linker isn't called in this case. To compile a unit in the file \file{foo.pp}, just type : \begin{verbatim} ppc386 foo \end{verbatim} Recall the remark about file extensions in the previous section. When all went well, you will be left with 2 (two) unit files: \begin{enumerate} \item \file{foo.ppu} This is the file describing the unit you just compiled. \item \file{foo.o} This file contains the actual code of the unit. This file will eventually end up in the executables. \end{enumerate} Both files are needed if you plan to use the unit for some programs. So don't delete them. If you want to distribute the unit, you must provide both the \file{.ppu} and \file{.o} file. One is useless without the other. The file containing the assembler (extension \file{.s}) can safely be deleted. You don't need it anymore. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Creating an executable for GO32V1, PMODE/DJ targets \section{Creating an executable for GO32V1, PMODE/DJ targets} This section applies only to \dos users. \linux users can skip this section (unless they're cross-compiling) % % % GO32V1 \subsection{GO32V1} When compiling under \dos, GO32V2 is the default target. However, if you use go32V1 (using the \var{-TDOS} switch), the compilation process leaves you with a file which you cannot execute right away. There are 2 things you can do when compiling has finished. The first thing is to use the \dos extender from D.J. Delorie to execute your program : \begin{verbatim} go32 prog \end{verbatim} This is fine for testing, but if you want to use a program regularly, it would be easier if you could just type the program name, i.e. \begin{verbatim} prog \end{verbatim} This can be accomplished by making a \dos executable of your compiled program. There two ways to create a \dos executable (under \dos only): \begin{enumerate} \item if the \file{GO32.EXE} is already installed on the computers where the program should run, you must only copy a program called \file{STUB.EXE} at the begin of the AOUT file. This is accomplished with the \file{AOUT2EXE.EXE} program. which comes with the compiler: \begin{verbatim} AOUT2EXE PROG \end{verbatim} and you get a \dos executable which loads the \file{GO32.EXE} automatically. the \file{GO32.EXE} executable must be in current directory or be in a directory in the \var{PATH} variable. \item The second way to create a \dos executable is to put \file{GO32.EXE} at the beginning of the \file{AOUT} file. To do this, at the command prompt, type : \begin{verbatim} COPY /B GO32.EXE+PROG PROG.EXE \end{verbatim} (assuming \fpk created a file called \file{PROG}, of course.) This becomes then a stand-alone executable for \dos, which doesn't need the \file{GO32.EXE} on the machine where it should run. \end{enumerate} % % % PMODE/DJ \subsection{PMODE/DJ} You can also use the PMODE/DJ extender to run your \fpk applications. To make an executable which works with the PMODE extender, you can simply create an GO32V2 executable (the default), and then convert it to a PMODE executable with the following two extra commands: \begin{enumerate} \item First, strip the GO32V2 header of the executable: \begin{verbatim} EXE2COFF PROG.EXE \end{verbatim} (we suppose that \file{PROG.EXE} is the program generated by the compilation process. \item Secondly, add the PMODE stub: \begin{verbatim} COPY /B PMODSTUB.EXE+PROG PROG.EXE \end{verbatim} If the \file{PMODSTUB.EXE} file isn't in your local directory, you need to supply the whole path to it. \end{enumerate} That's it. No additional steps are needed to create a PMODE extender executable. Be aware, though, that the PMODE extender doesn't support virtual memory, so if you're short on memory, you may run unto trouble. Also, officially there is not support for the PMODE/DJ extender. It just happens that the compiler and some of the programs it generates, run under this extender too. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Reducing the size of your program \section{Reducing the size of your program} When you created your program, it is possible to reduce its size. This is possible, because the compiler leaves a lot of information in the program which, strictly speaking, isn't required for the execution of it. The surplus of information can be removed with a small program called \file{strip}. It comes with the \var{GO32} development environment under \dos, and is standard on \linux machines where you can do development. The usage is simple. Just type \begin{verbatim} strip prog \end{verbatim} On the command line, and the \file{strip} program will remove all unnecessary information from your program. This can lead to size reductions of up to 30 \%. You can use the \var{-Xs} switch to let the compiler do this stripping automatically. Under \linux, you can set the \var{-k-s} option, which does the same. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Problems %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Compiling problems} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % General problems \section{General problems} \begin{itemize} \item \textbf{IO-error -2 at ...} : Under \linux you can get this message at compiler startup. It means typically that the compiler doesn't find the error definitions file. You can correct this mistake with the \var{-Fr} option under \linux. (\seeo{Fr}) \item \textbf {Error : File not found : xxx} This typically happens when your unit path isn't set correctly. Remember that the compiler looks for units only in the current directory, and in the directory where the compiler itself is. If you want it to look somewhere else too, you must explicitly tell it to do so using the \var{-Up} option (\seeo{Up}). \end{itemize} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Problems you may encounter under DOS \section{Problems you may encounter under DOS} \begin{itemize} \item \textbf{No space in environment}.\\ An error message like this can occur, if you call \verb|SET_PP.BAT| in the \file{AUTOEXEC.BAT}.\\ To solve this problem, you must extend your environment memory. To do this, search a line in the \file{CONFIG.SYS} like \begin{verbatim} SHELL=C:\DOS\COMMAND.COM \end{verbatim} and change it to the following: \begin{verbatim} SHELL=C:\DOS\COMMAND.COM /E:1024 \end{verbatim} You may just need to specify a higher value, if this parameter is already set. \item \textbf{ Coprocessor missing}\\ If the compiler writes a message that there is no coprocessor, install the coprocessor emulation. \item \textbf{Not enough DPMI memory}\\ If you want to use the compiler with \var{DPMI} you must have at least 7-8 MB free \var{DPMI} memory. \end{itemize} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Configuration. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Compiler configuration} The output of the compiler can be controlled in many ways. This can be done essentially in two distinct ways: \begin{itemize} \item Using command-line options. \item Using the configuration file: \file{ppc386.cfg}. \end{itemize} The compiler first reads the configuration file. Only then the command line options are checked. This creates the possibility to set some basic options in the configuration file, and at the same time you can still set some specific options when compiling some unit or program. First we list the command line options, and then we explain how to specify the command line options in the configuration file. When reading this, keep in mind that the options are case sensitive. While this is customary for \linux, it isn't under \dos. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Using the command-line options \section{Using the command-line options} The available options are listed by category: % % % General options \subsection{General options} \begin{description} \item[-h] if you specify this option, the compiler outputs a list of all options, and exits after that. \olabel{h} \item[-i] This option tells the compiler to print the copyright information. \olabel{i} \item[-l] This option tells the compiler to print the \fpk logo on standard output. It also gives you the \fpk version number. \olabel{l} \item[-Lx] Set the language the compiler uses for its messages. \olabel{L} \var{x} can be one of the following: \begin{itemize} \item \textbf{D} : Use German. \item \textbf{E} : Use English. \end{itemize} \end{description} % % % Options for getting feedback \subsection{Options for getting feedback} \begin{description} \item[-qxx] This option tells the compiler to print on stdout some information on what it is doing. xx can be one of the following: \olabel{q} \begin{itemize} \item \textbf{x is empty} : Be quiet. Don't output anything \item \textbf{-} : Give some information. \item \textbf{--} : Give a lot of information. \end{itemize} This is an obsolete option, and is kept only for backwards compatibility. It may disappear in the future.\\ \item[-vxxx] Be verbose. \var{xxx} is a combination of the following : \olabel{v} \begin{itemize} \item \var{e} : Tells the compiler to show only errors. This option is on by default. \item \var{i} : Tells the compiler to show some general information. \item \var{w} : Tells the compiler to issue warnings. \item \var{n} : Tells the compiler to issue notes. \item \var{l} : Tells the compiler to show the line numbers as it processes a file. Numbers are shown per 100. \item \var{u} : Tells the compiler to print the names of the files it opens. \item \var{t} : Tells the compiler to print the names of the files it tries to open. \item \var{p} : Tells the compiler to print the names of procedures and functions as it is processing them. \item \var{c} : Tells the compiler to warn you when it processes a conditional. \item \var{m} : Tells the compiler to write which macros are defined. \item \var{d} : Tells the compiler to write other debugging info. \end{itemize} \item[-w] When this option is specified, the compiler issues warning. To turn off warnings, specify \var{-w-}\\ This option is obsolete. and is kept only for backwards compatibility. It may disappear in the future. \olabel{w} \end{description} % % % Options concerning files and directories \subsection{Options concerning files and directories} \begin{description} \item [-exxx] (\linux only) \file{xxx} specifies the directory where the compiler can find the executables \file{as} (the assembler) and \file{ld} (the compiler). \olabel{e} \item [-Fexxx] This option tells the compiler to write errors, etc. to the file in \file{xxx}. \olabel{Fe} \item [-Fgxxx] (\linux only) \file{xxx} specifies the path where the compiler can find the \gnu C library. \olabel{Fg} \item [-Frxxx] (\linux only) \file{xxx} specifies the path where the compiler can find the error-definitions file. \olabel{Fr} \item [-Upxxx] \olabel{Up} Tells the compiler to add \file{xxx} to the path where to find units. \\ By default, the compiler only searches for units in the current directory and the directory where the compiler itself resides. This option tells the compiler also to look in the directory \file{xxx}. \end{description} % Options controlling the kind of output. \subsection{Options controlling the kind of output.} for more information on these options, see also \progref \begin{description} \item [-a] \olabel{a} Tells the compiler to generate an assembler source file, and to call an external assembler (\file{as}) to assemble this file. The file will not be deleted. \item [-Axxx] \olabel{A}specifies what kind of assembler should be generated . Here \var{xxx} is one of the following : \begin{itemize} \item \textbf{att} : AT\&T assembler. \item \textbf{o} : A unix .o (object) file. \item \textbf{obj} : A OMF file for using the NASM assembler. \item \textbf{nasm} : a coff file using the NASM assembler. \item \textbf{wasm} : An assembler file for the Microsoft/Borland/Watcom assembler. \end{itemize} \item [-Chxxx] \olabel {Ch} Reserves \var{xxx} bytes heap. \item [-Ci] \olabel{Ci} Generate Input/output checking code. \item [-Co] \olabel{Co} Generate Integer overflow checking code. \item [-Cr] \olabel{Cr} Generate Range checking code. \item [-Csxxx] \olabel{Cs} Set stack size to \var{xxx}. (\ostwo only). \item [-dxxx] \olabel{d} Define the symbol name \var{xxx}. This can be used to conditionally compile parts of your code. \item [-g] \olabel{g} Generate debugging information for debugging with \file{gdb}. \item[-On] \olabel{O} optimize the compiler's output; \var{n} can have one of the following values : \begin{description} \item[a] simple optimizations \item[g] optimize for size \item[G] optimize for time \item[x] optimize maximum \item[2] optimize for Pentium II (tm) \item[3] optimize for i386 \item[4] optimize for i486 \item[5] optimize for Pentium (tm) \item[6] optimizations for PentiumPro (tm) \end{description} The exact effect of these effects can be found in the appendices of the \progref. \item [-s] \olabel{s} Tells the compiler not to call the assembler and linker. You must specify also \var{-a} if you specify this. \item[-Txxx] \olabel{T}Specifies the target operating system. \var{xxx} can be one of the following: \begin{itemize} \item \textbf{DOS} : \dos and the DJ DELORIE extender. \item \textbf{OS2} : OS/2 (2.x) (this is still under development). \item \textbf{LINUX} : \linux. \item \textbf{WIN32} : Windows 32 bit (this is still under development). \item \textbf{GO32V2} : \dos and version 2 of the DJ DELORIE extender. \end{itemize} \item [-Xx] \olabel{X} executable options. This tells the compiler what kind of \linux executable should be generated. the parameter \var{x} can be one of the following: \begin{itemize} \item \textbf{e} : (\linux only) Create an \file{ELF} executable (default). \item \textbf{c} : (\linux only) Link with the C library. You should only use this when you start to port \fpk to another operating system. \item \textbf{s} : (\dos only) Strip the symbols from the executable. \end{itemize} \end{description} % % % Options concerning the sources (language options) \subsection{Options concerning the sources (language options)} for more information on these options, see also \progref \begin{description} \item [-Rxxx] \olabel{R} Specifies what assembler you use in your \var{asm} assembler code blocks. Here \var{xxx} is one of the following: \begin{description} \item [att\ ] \var{asm} blocks contain AT\&T assembler. \item [intel] \var{asm} blocks contain Intel assembler. \item [direct] \var{asm} blocks should be copied as-is in the assembler file. \end{description} \item [-S2] \olabel{Stwo} Switch on Delphi 2 extensions. \item [-Sann] \olabel{Sa} How severe should the compiler check your code ? \var{nn} can be one of the following: \begin{itemize} \item \var{0} : Only ANSI Pascal expressions allowed. \item \var{1} : Do not necessarily assign function results to variables. \item \var{2} : Address operator \var{@} returns a typed pointer. \item \var{4} : Assignment results are typed. (This allows constructs like \var{a:=b:=0}. See also ... \item \var{9} : Allows expressions with no side effect. \remark{Florian ???} \end{itemize} \item [-Sc] \olabel{Sc} Support C-style operators, i.e. \var{*=, +=, /= and -=}. \item [-Sg] \olabel{Sg} Support the \var{label} and \var{goto} commands. \item [-Si] \olabel{Si} Support \var{C++} style INLINE. \item [-Sm] \olabel{Sm} Support C-style macros. \item [-So] \olabel{So} Try to be Borland TP compatible (no function overloading etc.). \item [-Ss] \olabel{Ss} The name of constructors must be \var{init}, and the name of destructors should be \var{done}. \item [-St] \olabel{St} Allow the \var{static} keyword in objects. \item [-Un] \olabel{Un} Do not check the unit name. (Normally, the unit name is the same as the filename. This option allows both to be different.) \item [-Us] \olabel{Us} Compile a system unit. This option causes the compiler to define only some very basic types. \end{description} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Using the configuration file \section{Using the configuration file} \label{se:config_file} Using the configuration file \file{ppc386.cfg} is an alternative to command line options. When a configuration file is found, it is read, and the lines in it are treated like you typed them on the command line. They are treated before the options that you type on the command line. The compiler looks for the \file{ppc386.cfg} file in the following places : \begin{enumerate} \item The current directory. \item Under \dos, the directory where the compiler is. Under \linux, the compiler looks in the \file{/etc} directory, or, if specified, the directory in the \var{PPC\_CONFIG\_PATH} environment variable. \end{enumerate} When the compiler has finished reading the configuration file, it continues to treat the command line options. One of the command-line options allows you to specify a second configuration file: Specifying \file{@foo} on the command line will open file \file{foo}, and read further options from there. When the compiler has finished reading this file, it continues to process the command line. An important feature in the configuration file is that you can specify sections. They behave much like conditional defines. Suppose the following configuration file (named \file{myconf}) \begin{verbatim} -a #section first -Up/some_path #section second -Up/other_path. \end{verbatim} When you invoke the compiler as follows: \begin{verbatim} ppc386 -dfirst @myconf foo.pp \end{verbatim} then the compiler will read the part of the configuration file coming before the line containing \var{\#section second}. As a result the unit search path will be set to \file{/some\_path}. If, on the other hand, you invoke the compiler as \begin{verbatim} ppc386 -dsecond @myconf foo.pp \end{verbatim} Then the configuration file will be read as if the part between \var{\#section first} and \var{\#section second} didn't exist, resulting in a unit search path of \file{/other\_path}. If you put a \var{\#section common} on a line, everything that follows this keyword will be read, whatever the defined constants. In short, the \var{\#define} keywords act as conditionals. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Porting. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Porting Turbo Pascal Code} \fpk was designed to resemble Turbo Pascal as closely as possible. There are, of course, restrictions. Some of these are due to the fact that \fpk is a 32-bit compiler. Other restrictions result from the fact that \fpk works on more than one operating system. In general we can say that if you keep your program code close to ANSI Pascal, you will have no problems porting from Turbo Pascal, or even Delphi, to \fpk. To a large extent, the constructs defined by Turbo Pascal are supported. In the following sections we will list the Turbo Pascal constructs which are not supported in \fpk, and we will list in what ways \fpk extends the Turbo Pascal language. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Things that will not work \section{Things that will not work} Here we give a list of things which are defined/allowed in Turbo Pascal, but which are not supported by \fpk. Where possible, we indicate the reason. \begin{enumerate} \item Parameter lists of previously defined functions and procedures must match exactly. The reason for this is the function overloading mechanism of \fpk. (however, \seeo{So}) \item \var {(* ... *)} as comment delimiters are not allowed in versions older than 0.9.1. This can easily be remedied with a grown-up editor. \item The \var{MEM, MEMW, MEML} and \var{PORT} variables for memory and port access are not available. This is due to the operating system. Under \dos, the extender unit (\file {GO32.PPU} provides functions to remedy this. \item \var{PROTECTED, PUBLIC, TRY, THROW, EXCEPTION} are reserved words. This means you cannot create procedures or variables with the same name. While they are not reserved words in Turbo Pascal, they are in Delphi. \item The reserved words \var{FAR, NEAR} are ignored. This is because \fpk is a 32 bit compiler, so they're obsolete. \item \var{INTERRUPT} only will work on a DOS machine. \item Boolean expressions are only evaluated until their result is completely determined. The rest of the expression will be ignored. \item At the moment of writing, the assembler syntax used in \fpk is \var{AT\&T} assembler syntax. This is mainly because \fpk uses \gnu \var{as}. \item Turbo Vision is not available. \item The 'overlay' unit is not available. It also isn't necessary, since \fpk is a 32 bit compiler, so program size shouldn't be a point. \item There are more reserved words. (see appendix \ref{ch:reserved} for a list of all reserved words.) \item The command-line parameters of the compiler are different. \item The compiler switches behave different. \item Units are not binary compatible. \end{enumerate} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Things which are extra \section{Things which are extra} Here we give a list of things which are possible in \fpk, but which didn't exist in Turbo Pascal or Delphi. \begin{enumerate} \item There are more reserved words. (see appendix \ref{ch:reserved} for a list of all reserved words.) \item Functions can also return complex types, such as records and arrays. \item You can handle function results in the function itself, as a variable. Example \begin{verbatim} function a : longint; begin a:=12; while a>4 do begin {...} end; end; \end{verbatim} The example above would work with TP, but the compiler would assume that the \var{a>4} is a recursive call. To do a recursive call in this you must append \var{()} behind the function name: \begin{verbatim} function a : longint; begin a:=12; { this is the recursive call } if a()>4 then begin {...} end; end; \end{verbatim} \item There is partial support of Delphi constructs. (see the \progref for more information on this). \item The \var{exit} call accepts a return value for functions. \begin{verbatim} function a : longint; begin a:=12; if a>4 then begin exit(a*67); {function result upon exit is a*67 } end; end; \end{verbatim} \item \fpk supports function overloading. That is, you can define many functions with the same name, but with different arguments. For example: \begin{verbatim} procedure DoSomething (a : longint); begin {...} end; procedure DoSomething (a : real); begin {...} end; \end{verbatim} You can then call procedure \var{DoSomething} with an argument of type \var{Longint} or \var{Real}.\\ This feature has the consequence that a previously declared function must always be defined with the header completely the same: \begin{verbatim} procedure x (v : longint); forward; {...} procedure x;{ This will overload the previously declared x} begin {...} end; \end{verbatim} This construction will generate a compiler error, because the compiler didn't find a definition of \var{procedure x (v : longint);}. Instead you should define your procedure x as: \begin{verbatim} procedure x (v : longint); { This correctly defines the previously declared x} begin {...} end; \end{verbatim} \end{enumerate} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Turbo Pascal compatibility mode \section{Turbo Pascal compatibility mode} When you compile a program with the \var{-So} switch, the compiler will attempt to mimic the Turbo Pascal compiler in the following ways: \begin{itemize} \item Assigning a procedural variable doesn't require a @ operator. One of the differences between Turbo Pascal and \fpk is that the latter requires you to specify an address operator when assigning a value to a procedural variable. In Turbo Pascal compatibility mode, this is not required. \item Procedure overloading is disabled. \item Forward defined procedures don't need the full parameter list when they are defined. Due to the procedure overloading feature of \fpk, you must always specify the parameter list of a function when you define it, even when it was declared earlier with \var{Forward}. In Turbo Pascal compatibility mode, there is no function overloading, hence you can omit the parameter list: \begin{verbatim} Procedure a (L : Longint); Forward; ... Procedure a ; { No need to repeat the (L : Longint) } begin ... end; \end{verbatim} \item recursive function calls are handled dfferently. Consider the following example : \begin{verbatim} Function expr : Longint; begin ... Expr:=L: Writeln (Expr); ... end; \end{verbatim} In Turbo Pascal compatibility mode, the function will be called recursively when the \var{writeln} statement is processed. In \fpk, the function result will be printed. In order to call the function recusively under \fpk, you need to implement it as follows : \begin{verbatim} Function expr : Longint; begin ... Expr:=L: Writeln (Expr()); ... end; \end{verbatim} \item Any text after the final \var{End.} statement is ignored. Normally, this text is processed too. \item You cannot assign procedural variables to void pointers. \item The @ operator is typed when applied on procedures. \item You cannot nest comments. \end{itemize} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Utilities. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Utilities and units that come with Free Pascal} Besides the compiler and the Run-Time Library, \fpk comes with some utility programs and units. Here we list these programs and units. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Supplied programs \section{Supplied programs} \begin{itemize} \item \file{dumppu} is a program which shows the contents of a \fpk unit. It comes in source form, and must be compiled before you can use it. Once compiled, you can just issue the following command \begin{verbatim} dumppu foo.ppu \end{verbatim} to display the contents of the \file{foo.ppu} unit. \item Also distributed with Free Pascal comes a series of demonstration programs. These programs have no other purpose than demonstrating the capabilities of \fpk. They are located in the \file{demo} directory of the sources. \item All example programs of the documentation are available. Check out the directories that end on \file{ex} in the documentation sources. There you wll find all example sources. \end{itemize} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Supplied units \section{Supplied units} Here we list the units that come with the \fpk distribution. Since there is a difference in the supplied units per operating system, we list them separately per system. % % % Under DOS \subsection{Under DOS} \begin{itemize} \item \seestrings\ This unit provides basic string handling routines for the \var{pchar} type, comparable to similar routines in standard \var{C} libraries. \item \seeobjects\ This unit provides basic routines for handling objects. \item \seedos\ This unit provides basic routines for accessing the operating system \dos. It provides almost the same functionality as the Turbo Pascal unit. \item \seeprinter\ This unit provides all you need for rudimentary access to the printer. \item \seegetopts\ This unit gives you the \gnu \var{getopts} command-line arguments handling mechanism. It also supports long options. \item \seecrt\ This unit provides basic screen handling routines. It provides the same functionality as the Turbo Pascal \var{CRT} unit. \item \seegraph\ This unit provides basic graphics handling, with routines to draw lines on the screen, display texts etc. It provides the same functions as the Turbo Pascal unit. \item \seego\ This unit provides access to possibilities of the \var{GO32} \dos extender. \end{itemize} \remark{Florian, I don't know the full list - let me know what is available} % % % Under Linux \subsection{Under Linux} \begin{itemize} \item \seestrings\ This unit provides basic string handling routines for the \var{PChar} type, comparable to similar routines in standard \var{C} libraries. \item \seeobjects\ This unit provides basic routines for handling objects. \item \seecrt\ This unit provides basic screen handling routines. It provides the same functionality Turbo Pascal \var{CRT} unit. It works on any terminal which supports the \var{vt100} escape sequences. \item \seedos\ This unit provides an emulation of the same unit under \dos. It is intended primarily for easy porting of Pascal programs from \dos to \linux. For good performance, however, it is recommended to use the \var{linux} unit. \item \seelinux This unit provides access to the \linux operating system. It provides most file and I/O handling routines that you may need. It implements most of the standard \var{C} library constructs that you will find on a Unix system. If you do a lot of disk/file operations, the use of this unit is recommended over the one you use under Dos. \item \seeprinter\ This unit provides an interface to the standard Unix printing mechanism. \item \seegetopts This unit gives you the \gnu \var{getopts} command-line arguments handling mechanism. It also supports long options. \end{itemize} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Debugging %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Debugging your Programs} \fpk supports debug information for the \gnu debugger \var{gdb}. This chapter describes shortly how to use this feature. It doesn't attempt to describe completely the \gnu debugger, however. For more information on the workings of the \gnu debugger, see the \var{gdb} users' guide. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Compiling your program with debugger support \section{Compiling your program with debugger support} First of all, you must be sure that the compiler is compiled with debugging support. Unfortunately, there is no way to check this at run time, except by trying to compile a program with debugging support. To compile a program with debugging support, just specify the \var{-g} option on the command-line, as follows: \begin{verbatim} ppc386 -g hello.pp \end{verbatim} This will generate debugging information in the executable from your program. You will notice that the size of the executable increases substantially because of this\footnote{A good reason not to include debug information in an executable you plan to distribute.}. Note that the above will only generate debug information {\var for the code that has been generated} when compiling \file{hello.pp}. This means that if you used some units (the system unit, for instance) which were not compiled with debugging support, no debugging support will be available for the code in these units. There are 2 solutions for this problem. \begin{enumerate} \item Recompile all units manually with the \var{-g} option. \item Specify the 'build' option (\var{-B}) when compiling with debugging support. This will recompile all units, and insert debugging information in each of the units. \end{enumerate} The second option may have undesirable side effects. It may be that some units aren't found, or compile incorrectly due to missing conditionals, etc.. If all went well, the executable now contains the necessary information with which you can debug it using \gnu \var{gdb}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Using \var{gdb \section{Using \var{gdb} to debug your program} To use gdb to debug your program, you can start the debugger, and give it as an option the name of your program: \begin{verbatim} gdb hello \end{verbatim} This starts the debugger, and the debugger immediately loads your program into memory, but it does not run the program yet. Instead, you are presented with the following (more or less) message, followed by the \var{gdb} prompt \var{'(gdb)'}: \begin{verbatim} GDB is free software and you are welcome to distribute copies of it under certain conditions; type "show copying" to see the conditions. There is absolutely no warranty for GDB; type "show warranty" for details. GDB 4.15.1 (i486-slackware-linux), Copyright 1995 Free Software Foundation, Inc... (gdb) \end{verbatim} To start the program you can use the \var{run} command. You can optionally specify command-line parameters, which will then be fed to your program, for example: \begin{verbatim} (gdb) run -option -anotheroption needed_argument \end{verbatim} If your program runs without problems, \var{gdb} will inform you of this, and return the exit code of your program. If the exit code was zero, then the message \var{'Program exited normally'}. If something went wrong (a segmentation fault or so), \var{gdb} will stop the execution of your program, and inform you of this with an appropriate message. You can then use the other \var{gdb} commands to see what happened. Alternatively, you can instruct \var{gdb} to stop at a certain point in your program, with the \var{break} command. Here is a short list of \var{gdb} commands, which you are likely to need when debugging your program: \begin{description} \item [quit\ ] Exits the debugger. \item [kill\ ] Stops a running program. \item [help\ ] Gives help on all \var{gdb} commands. \item [file\ ] Loads a new program into the debugger. \item [directory\ ] Add a new directory to the search path for source files.\\ {\em Remark:} My copy of gdb needs '.' to be added explicitly to the search path, otherwise it doesn't find the sources. \item [list\ ] Lists the program sources per 10 lines. As an option you can specify a line number or function name. \item [break\ ] Sets a breakpoint at a specified line or function \item [awatch\ ] Sets a watch-point for an expression. A watch-point stops execution of your program whenever the value of an expression is either read or written. \end{description} for more information, see the \var{gdb} users' guide, or use the \var{'help'} function in \var{gdb}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % CGI. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{CGI programming in Free Pascal} In these days of heavy WWW traffic on the Internet, CGI scripts have become an important topic in computer programming. While CGI programming can be done with almost any tool you wish, most languages aren't designed for it. Perl may be a notable exception, but perl is an interpreted language, the executable is quite big, and hence puts a big load on the server machine. Because of its simple, almost intuitive, string handling and its easy syntax, Pascal is very well suited for CGI programming. Pascal allows you to quickly produce some results, while giving you all the tools you need for more complex programming. The basic RTL routines in principle are enough to get the job done, but you can create, with relatively little effort, some units which can be used as a base for more complex CGI programming. That's why, in this chapter, we will discuss the basics of CGI in \fpk. In the subsequent, we will assume that the server for which the programs are created, are based upon the NCSA \var{httpd} WWW server, as the examples will be based upon the NCSA method of CGI programming\footnote{... and it's the only WWW-server I have to my disposition at the moment.}. The two example programs in this chapter have been tested on the command line and worked, under the condition that no spaces were present in the name and value pairs provided to them. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Getting your data \section{Getting your data} Your CGI program must react on data the user has filled in on the form which your web-server gave him. The Web server takes the response on the form, and feeds it to the CGI script. There are essentially two ways of feeding the data to the CGI script. We will discuss both. % % % Data coming through standard input. \subsection{Data coming through standard input.} The first method of getting your data is through standard input. This method is invoked when the form uses a form submission method of \var{POST}. The web browser sets three environment variables \var{REQUEST\_METHOD}, \var{CONTENT\_TYPE} and \var{CONTENT\_LENGTH}. It feeds then the results of the different fields through standard input to the CGI script. All the Pascal program has to do is : \begin{itemize} \item Check the value of the \var{REQUEST\_METHOD} environment variable. The \var{getenv} function will retrieve this value this for you. \item Check the value of the \var{CONTENT\_TYPE} environment variable. \item Read \var{CONTENT\_LENGTH} characters from standard input. \var{read (c)} with \var{c} of type \var{char} will take care of that. \end{itemize} if you know that the request method will always be \var{POST}, and the \var{CONTENT\_TYPE} will be correct, then you can skip the first two steps. The third step can be done easier: read characters until you reach the end-of-file marker of standard input. The following example shows how this can be achieved: \begin{verbatim} program cgi_post; uses dos; const max_data = 1000; type datarec = record name,value : string; end; var data : array[1..max_data] of datarec; i,nrdata : longint; c : char; literal,aname : boolean; begin writeln ('Content-type: text/html'); writeln; if getenv('REQUEST_METHOD')<>'POST' then begin writeln ('This script should be referenced with a METHOD of POST'); write ('If you don''t understand this, see this '); write ('< A HREF="http://www.ncsa.uiuc.edu/SDG/Softare/Mosaic'); writeln ('/Docs/fill-out-forms/overview.html">forms overview.'); halt(1); end; if getenv('CONTENT_TYPE')<>'application/x-www-form-urlencoded' then begin writeln ('This script can only be used to decode form results'); halt(1) end; nrdata:=1; aname:=true; while not eof(input) do begin literal:=false; read(c); if c='\' then begin literal:=true; read(c); end; if literal or ((c<>'=') and (c<>'&')) then with data[nrdata] do if aname then name:=name+c else value:=value+c else begin if c='&' then begin inc (nrdata); aname:=true; end else aname:=false; end end; writeln ('