driver.c

Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <sys/stat.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include <grass/gis.h>
00006 #include <grass/dbmi.h>
00007 #include "procs.h"
00008 #define DB_DRIVER_C
00009 #include "dbstubs.h"
00010 
00011 extern char *getenv();
00012 
00020 int
00021 db_driver (int argc, 
00022     char *argv[])
00023 {
00024     int stat;
00025     int procnum;
00026     int i;
00027     int rfd, wfd;
00028     FILE *send, *recv;
00029     char *modestr;
00030 
00031     /* Read and set enviroment variables, see dbmi_client/start.c */
00032     if ( (modestr = getenv ( "GRASS_DB_DRIVER_GISRC_MODE" )) ) {
00033         int mode;
00034         mode = atoi ( modestr );
00035                 
00036         if ( mode == G_GISRC_MODE_MEMORY ) {
00037             G_set_gisrc_mode ( G_GISRC_MODE_MEMORY );
00038             G__setenv( "DEBUG", getenv ( "DEBUG" ) );
00039             G__setenv( "GISDBASE", getenv ( "GISDBASE" ) );
00040             G__setenv( "LOCATION_NAME", getenv ( "LOCATION_NAME" ) );
00041             G__setenv( "MAPSET", getenv ( "MAPSET" ) );
00042             G_debug (3, "Driver GISDBASE set to '%s'", G_getenv ( "GISDBASE" ) );
00043         }
00044     }
00045 
00046 #ifdef __MINGW32__
00047     /* TODO: */
00048     /* We should close everything except stdin, stdout but _fcloseall()
00049      * closes open streams not file descriptors. _getmaxstdio too big number.
00050      * 
00051      * Because the pipes were created just before this driver was started 
00052      * the file descriptors should not be above a closed descriptor
00053      * until it was run from a multithread application and some descriptors 
00054      * were closed in the mean time. 
00055      * Also Windows documentation does not say that new file descriptor is 
00056      * the lowest available.
00057      */
00058  
00059      { 
00060          int err_count = 0;
00061          int cfd = 3;
00062         
00063          while ( 1 ) 
00064          {
00065              if ( close(cfd) == -1 ) err_count++;
00066 
00067              /* no good reason for 10 */  
00068              if ( err_count > 10 ) break;
00069          
00070              cfd++;
00071          }
00072      }
00073 #endif
00074 
00075     send = stdout;
00076     recv = stdin;
00077 
00078 /* THIS CODE IS FOR DEBUGGING WITH CODECENTER */
00079 /**********************************************/
00080     if (argc == 3)
00081     {
00082         rfd = wfd = -1;
00083         sscanf (argv[1], "%d", &rfd);
00084         sscanf (argv[2], "%d", &wfd);
00085         send = fdopen (wfd, "w");
00086         if (send == NULL)
00087         {
00088             db_syserror(argv[1]);
00089             exit(1);
00090         }
00091         recv = fdopen (rfd, "r");
00092         if (recv == NULL)
00093         {
00094             db_syserror(argv[2]);
00095             exit(1);
00096         }
00097     }
00098 /**********************************************/
00099 
00100     db_clear_error();
00101     db_auto_print_errors(0);
00102     db_auto_print_protocol_errors(1);
00103     db__init_driver_state();
00104 
00105 #ifndef USE_BUFFERED_IO
00106     setbuf (recv, NULL);
00107     setbuf (send, NULL);
00108 #endif
00109     db__set_protocol_fds (send, recv);
00110 
00111     if(db_driver_init (argc, argv) == DB_OK)
00112         db__send_success();
00113     else
00114     {
00115         db__send_failure();
00116         exit(1);
00117     }
00118 
00119     stat = DB_OK;
00120     /* get the procedure number */
00121     while (db__recv_procnum (&procnum) == DB_OK)
00122     {
00123 #ifdef __MINGW32__
00124         if ( procnum == DB_PROC_SHUTDOWN_DRIVER ) {
00125             db__send_procedure_ok(procnum);
00126             break;
00127         }
00128 #endif
00129         db_clear_error();
00130 
00131     /* find this procedure */
00132         for (i = 0; procedure[i].routine; i++)
00133             if (procedure[i].procnum == procnum)
00134                 break;
00135 
00136      /* if found, call it */
00137         if (procedure[i].routine)
00138         {
00139             if((stat = db__send_procedure_ok(procnum)) != DB_OK)
00140                 break; /* while loop */
00141             if((stat = (*procedure[i].routine)()) != DB_OK)
00142                 break;
00143         }
00144         else if ((stat = db__send_procedure_not_implemented(procnum)) != DB_OK)
00145             break;
00146     }
00147 
00148     db_driver_finish();
00149 
00150     exit (stat == DB_OK ? 0 : 1);
00151 }
00152 

Generated on Sun Apr 6 17:31:38 2008 for GRASS by  doxygen 1.5.5