www.beck-ipc.com

CGI Examples Available - SC12 @CHIP-RTOS V1.10


    IPC@CHIP Documentation Index          CGI News

CGI Examples

Available CGI API examples in C:
  1. minicgi.c - Builds an HTML page
  2. countcgi.c - Builds a dynamic HTML page
  3. dk40cgi.c   - Reads DK40 I/O pins
  4. dk40_set.c - Reads and writes DK40 I/O pins
  5. secure.c    - Example of password protected page
  6. submit.c    - Building of formular
  7. submit2.c   - Formulars with multiple items
Also there are source files (cgiapi.c, cgiapi.h) containing wrapper functions which provide a C-Library interface to the assembly language/software interrupt based CGI API.

Available Turbo Pascal examples:
  1. cgimini.pas - Builds an HTML page
  2. countcgi.pas - Builds a dynamic HTML page
Examples in C

For understanding CGI in the IPC@CHIP we provide some example programs written in C.   The examples are compiled with various versions of Borland and Microsoft compilers.   Which compiler version was used for a particular example is stated within the example's source files.   The include file cgi.h contains all the required type definitions and constants.  
  1. minicgi.exe
    The CGI function installed by this program produces a HTML page which contains some of the browser's request parameters.   This program was tested and compiled with Borland C 3.0 and Microsoft Visual C 1.52   The compiler differences are described in the source files.

    Examples browser inputs:
      http://192.168.205.4/minicgi
      http://192.168.205.4/minicgi?Argument

  2. countcgi.exe
    We build a dynamic HTML page which contains the current value of a counter incremented in the main loop of the program.   This program is tested and compiled with Borland C 3.0 and Microsoft Visual C 1.52.   The compilers differences are described in the source files.

    Example browser input:
      http://192.168.205.4/countcgi

  3. dk40cgi.exe
    The CGI function of this program produces a HTML page which contains the current values of the DK40 I/O pins.

    Example browser input:
      http://192.168.205.4/dk40


  4. dk40_set.exe
    Demonstrates set and reset control over the DK40 output pins via browser.

    Example browser input:
      http://192.168.205.4/dk40

  5. secure.exe
    This is an example of a protected page.

    The first browser input ...
      http://192.168.205.4/dk40_secure
    requires the input of a valid user name and password, e.g.:
      User name: user
      Password: password

  6. submit.exe
    The building of formulars is demonstrated here.
  7. submit2.exe
    The building of formulars with more than one item is demonstrated here.
Important:
  1. The recommended memory model for DOS programs is "Large".
  2. CGI functions should be programmed as short as possible, without long or endless waits.  
  3. CGI functions compiled with Borland C must be declared as "huge".
  4. CGI functions compiled with Microsoft C must be declared as " far _saveregs _loadds".  
  5. Users of Microsoft Visual C must set the compiler option "struct member byte alignment" to "1 byte" or must use "#pragma pack(1)" in their source.
  6. The command  cgistat  at the IPC@CHIP command prompt lists all CGI functions installed.
  7. URL names for CGI functions are case-sensitive.
  8. If you use Microsoft C-Compilers then increase the Web server's WEBSERVERSTACK stack size value in the chip.ini file.   The default stack size of the Web server task is 2048 Bytes.   Programmers of CGI functions who are using Microsoft C-Compilers with C-Library functions, e.g. sprintf, which requires a lot of stack space should increase this allocation to 6144 (6 Kbytes).   More stack space for the Web server task is also required if your CGI function uses a large amount of stack for automatic data (local variables) declared inside the CGI function call.  

Building Turbo Pascal CGI procedures

Since @CHIP-RTOS version 0.65, it is possible to write CGI procedures with Turbo Pascal
This is a little bit different from writing a CGI function with the C compilers.
A Turbo Pascal program which contain a CGI procedure uses the same data structures (records)
as a C language CGI function.

Declaration of Turbo Pascal CGI procedures

A CGI procedure written with Turbo Pascal must be declared without any parameters and with the interrupt declaration, e.g.:

    procedure CgiMini_Proc;interrupt;
The interrupt declaration will motivate the compiler to emit code that sets the CPU's DS data segment register on entry to the procedure, thus allowing access to the Pascal program's data.

The IPC@CHIP Web server handles the call to a Pascal CGI function differently than it does the call to a C language CGI function.   One input parameter is needed by all CGI functions, C or Pascal.   This one parameter is a far pointer to a rpCgi type structure (or in Pascal, a record).   When calling a C language CGI function this pointer is pushed onto the stack using a normal parameter passing mechanism.   For Pascal CGI functions this pointer is instead passed in the CPU's ES:DI registers.   Consequently this pointer must be recovered by the Pascal program.   This can be done as follows:

procedure CgiMini_Proc;interrupt;
    var
    ESreg    :    Integer;
    DIreg    :    Integer;
    CGIRequest    :    rpCGIptr;

    begin
        asm
        mov ax,es
        mov Esreg,ax
        mov ax,di
        mov DIreg,ax
    end;
    CGIRequest    :=    ptr(ESreg,DIreg);
    ............
end;

Installing a Pascal CGI procedure:

Pascal CGI procedure must be installed at the start of the DOS program with the CGI_INSTALL_PAS API call.   (CGI functions written in C must still be installed with the standard CGI_INSTALL API.)

For better understanding of programming CGI with Turbo Pascal, we provide some example programs compiled with Borland Pascal 7.0.

  1. cgiMini.exe
    The CGI function installed by this program produces a HTML page which contains some of the request parameters.

    Examples for browser inputs:
      http://192.168.205.4/cgimini
      http://192.168.205.4/cgimini?Argument

  2. countcgi.exe
    We build a dynamic HTML page which contains the current value of a counter incremented in the main loop of the program.

    Browser input:     http://192.168.205.4/countcgi




End of document