home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 10 / amigaformatcd10.iso / -in_the_mag- / html_tutorial / mas_cgi.cpp < prev    next >
C/C++ Source or Header  |  1996-09-03  |  2KB  |  104 lines

  1. // (C) M.A.Smith University of Brighton
  2. //
  3. // Permission is granted to use this code
  4. //   provided this declaration and copyright notice remains intact.
  5. //
  6. // 26 August 1995
  7.  
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <iomanip.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <string.h>
  14.  
  15.  
  16. #include "parse.h"
  17. #include "parse.cpp"
  18.  
  19. void process_script( char [] );
  20. void process( char [] );
  21. char* getenv_n( char [] );
  22.  
  23.  
  24. // Main program
  25. //
  26.  
  27. int main()
  28. {
  29.   char *query_str = getenv("QUERY_STRING");
  30.   Parse list( query_str == 0 ? "file=mas2" : query_str );
  31.  
  32.   process_script( list.get_item( "file" ) );
  33.   return 0;
  34. }
  35.  
  36.  
  37. void process_script( char file[] )
  38. {
  39.   enum State { NORMAL, IN_ESC };
  40.   cout << "Content-type: text/html" << "\n"
  41.        << "\n" << "\n";
  42.   ifstream script( file );
  43.   if ( script.fail() ) return;
  44.   script >> resetiosflags( ios::skipws );
  45.   const int MAX_MES = 200;
  46.   char  c;
  47.   char  mes[ MAX_MES ];
  48.   char  start[] = "[=";  const int START_LEN = strlen( start );
  49.   char  end[]   = "=]";  const int END_LEN = strlen( start );
  50.   int   i, p=0, mes_len = 0;
  51.   State state_is = NORMAL;
  52.  
  53.   while ( script >> c, !script.eof() )
  54.   {
  55.     switch ( state_is )
  56.     {
  57.       case NORMAL :
  58.     if ( c == start[p] ) {         // Escape Sequence
  59.       p++;
  60.       if ( p >= START_LEN ) {      // Recognized Escape Seq.
  61.         state_is = IN_ESC;
  62.         mes_len = 0; p = 0;
  63.       }
  64.     } else {                       // Not Escape Seq.
  65.       for ( i=0; i<p; i++ )        // Write any gather chars
  66.         cout << start[i];
  67.       cout << c;                   // Current char
  68.       p = 0;
  69.     }
  70.     break;
  71.       case IN_ESC :                    // In Escape Sequence
  72.     if ( c == end[p] ) {           // End of Escape Seq
  73.       p++;
  74.       if ( p >= END_LEN ) {        // End of Escape Seq
  75.         mes[ mes_len ] = '\0';     // Message to process
  76.         process( mes );
  77.         p = 0; state_is = NORMAL;
  78.       }
  79.     } else {                       // Process goatherd chrs
  80.       if ( mes_len < MAX_MES - 10 ) {
  81.         for ( i = 0; i< p; i++ ) {
  82.           mes[mes_len++] = end[i];
  83.         }
  84.         mes[mes_len++] = c; p = 0; // Add to goatherd chrs
  85.       }
  86.     }
  87.     break;
  88.     }
  89.   }
  90. }
  91.     
  92. void process( char mes[] )
  93. {
  94.   Parse list( mes );
  95.   if ( list.get_item( "put_env" ) != 0 )
  96.   {
  97.     cout << getenv_n( list.get_item("put_env") );
  98.   }
  99.   if ( list.get_item( "exec" ) != 0 )
  100.   {
  101.     cout << list.get_item("exec");
  102.   }
  103. }
  104.