CGI code.

The uses of these CGI programs can be found using the Qchat Web Site Map. The links that the CGI programs make in the site are shown on this map.

To view the code in its original form (not interpreted by the web browser) view the source of this document.

Shared Functions.

The following functions are common to all of the CGI programs.


/******************************************************************************/

  /*                                                                            */

  /* Function       : ReadArguments                                             */

  /*                                                                            */

  /* Description    : Read the arguments from stdin that are supplied           */

  /*                  to a CGI program when the method is POST.                 */

  /*                  Breaks up the input into                                  */

  /*                  (Variable, Value) pairs.                                  */

  /*                  Handles translating of all the special characters         */

  /*                  that HTTP puts into the strings.                          */

  /*                                                                            */

  /******************************************************************************/



  PPARMLIST ReadArguments(int InputLength)

    {

    PPARMLIST pCur= NULL;

    PPARMLIST pHead= NULL;

    PPARMLIST pPrev= NULL;

    char *Input;

    char *pToken;



    if (InputLength < 1)

      {

      return(NULL);

      }

    /* Allocate a buffer for the input */

    Input = malloc(InputLength + 1);



    if (Input == NULL)

      {

      return(NULL);

      }



    /* Read the input */

    gets(Input);



    /* Variables are separated by the "&" character */

    pToken = strtok(Input, "&");



    while (pToken)

      {

      /* Create and fill in linked list of variable information */

      pCur = malloc(sizeof(PARMLIST));

      pCur->VariableName = pToken;

      pToken = strchr(pToken, '=');

      if (pToken)

        {

        *pToken = '\0';

        pCur->Value = ++pToken;

        PlusesToSpaces( pToken );

        TranslateEscapes( pToken );

        }

      else

        {

        pCur->Value = NULL;

        }

      if (pPrev)

        {

        pPrev->pNext = pCur;

        }



      if (!pHead)

        {

        pHead = pCur;

        }

      pPrev = pCur;

      pToken = strtok(NULL, "&");

      }



    if (pHead)

      {

      pPrev->pNext = NULL;

      }

    return(pHead);

    }



  /******************************************************************************/

  /*                                                                            */

  /* Function       : PlusesToSpaces (STATIC)                                   */

  /*                                                                            */

  /* Description    : This one's easy. It just translates any '+'               */

  /*                  characters found into ' ' characters.                     */

  /*                                                                            */

  /******************************************************************************/

  static void PlusesToSpaces(char *Str)

    {

    if (Str != NULL)

      {

      while (*Str != '\0')

        {

        if (*Str == '+')

          {

          *Str = ' ';

          }



        ++Str;

        }

      }

    }



  /******************************************************************************/

  /*                                                                            */

  /* Function       : HexVal (STATIC)                                           */

  /*                                                                            */

  /* Description    : This function returns a number that corresponds           */

  /*                  to the value of a character treated as                    */

  /*                  a hex digit. Case insensitive. Characters outside         */

  /*                  0-9,a-f,A-F have a value of 0.                            */

  /*                                                                            */

  /******************************************************************************/

  static int HexVal(char c)

    {

    int rc;



    switch (c)

      {

      case '1':

        rc = 1;

        break;

      case '2':

        rc = 2;

        break;



      case '3':

        rc = 3;

        break;



      case '4':

        rc = 4;

        break;

      case '5':

        rc = 5;

        break;



      case '6':

        rc = 6;

        break;

      case '7':

        rc = 7;

        break;

      case '8':

        rc = 8;

        break;



      case '9':

        rc = 9;

        break;



      case 'A':

      case 'a':

        rc = 10;

        break;

      case 'B':

      case 'b':

        rc = 11;

        break;



      case 'C':

      case 'c':

        rc = 12;

        break;

      case 'D':

      case 'd':

        rc = 13;

        break;



      case 'E':

      case 'e':

        rc = 14;

        break;

      case 'F':

      case 'f':

        rc = 15;

        break;

      default:

        rc = 0;

        break;

      }



    return(rc);

    }



  /******************************************************************************/

  /*                                                                            */

  /* Function       : TranslateEscapes (STATIC)                                 */

  /*                                                                            */

  /* Description    : Translate the escape sequences induced by HTTP. The       */

  /*                  sequences consist of %xx, where xx is a hex number.       */

  /*                  We replace the % character with the actual character      */

  /*                  (i.e., the one whose ASCII value is xx), and then         */

  /*                  shift over the rest of the string to remove the xx.       */

  /*                  This is done in-place.                                    */

  /*                                                                            */

  /******************************************************************************/

  static void TranslateEscapes(char *Str)

    {

    char *NextEscape;

    char RealValue;

    int AsciiValue;



    NextEscape = strchr(Str, '%');

    while (NextEscape != NULL)

      {

      AsciiValue = (16 * HexVal(NextEscape[1])) + HexVal(NextEscape[2]);

      *NextEscape = (char) AsciiValue;

      memmove(&NextEscape[1], &NextEscape[3], strlen(&NextEscape[3]) + 1);

      NextEscape = strchr(&NextEscape[1], '%');

      }

    }

  /******************************************************************************/

  /* This function will output a message if an error occurs when attempting     */

  /* to display a HTML page.                                                    */

  /******************************************************************************/

  int ErrMsg (char *msg)

    {

    printf("Content-type: text/html\n\n");

    printf("\n");

    printf("\n");

    printf("Error\n");

    printf("\n");

    printf("\n");

    printf("

Error

\n"); printf("
\n"); printf("

\n"); printf("An error occurred in the guestentry program.\n"); printf("The specific error message is shown below:\n"); printf("

\n"); printf("

%s
\n

", msg); printf("

\n"); printf("


\n"); printf("\n"); printf("\n"); return(TRUE); }