00001 #include <sys/types.h>
00002 #include <sys/stat.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 #include "gis.h"
00006 #include "dbmi.h"
00007 #include "procs.h"
00008 #define DB_DRIVER_C
00009 #include "dbstubs.h"
00010
00011 extern char *getenv();
00012
00019 int
00020 db_driver(int argc,
00021 char *argv[])
00022 {
00023 int stat;
00024 int procnum;
00025 int i;
00026 int rfd, wfd;
00027 FILE *send, *recv;
00028 char *modestr;
00029
00030
00031 if ( (modestr = getenv ( "GRASS_DB_DRIVER_GISRC_MODE" )) ) {
00032 int mode;
00033 mode = atoi ( modestr );
00034
00035 if ( mode == G_GISRC_MODE_MEMORY ) {
00036 G_set_gisrc_mode ( G_GISRC_MODE_MEMORY );
00037 G__setenv( "DEBUG", getenv ( "DEBUG" ) );
00038 G__setenv( "GISDBASE", getenv ( "GISDBASE" ) );
00039 G__setenv( "LOCATION_NAME", getenv ( "LOCATION_NAME" ) );
00040 G__setenv( "MAPSET", getenv ( "MAPSET" ) );
00041 G_debug (3, "Driver GISDBASE set to '%s'", G_getenv ( "GISDBASE" ) );
00042 }
00043 }
00044
00045 send = stdout;
00046 recv = stdin;
00047
00048
00049
00050 if (argc == 3)
00051 {
00052 rfd = wfd = -1;
00053 sscanf (argv[1], "%d", &rfd);
00054 sscanf (argv[2], "%d", &wfd);
00055 send = fdopen (wfd, "w");
00056 if (send == NULL)
00057 {
00058 db_syserror(argv[1]);
00059 exit(1);
00060 }
00061 recv = fdopen (rfd, "r");
00062 if (recv == NULL)
00063 {
00064 db_syserror(argv[2]);
00065 exit(1);
00066 }
00067 }
00068
00069
00070 db_clear_error();
00071 db_auto_print_errors(0);
00072 db_auto_print_protocol_errors(1);
00073 db__init_driver_state();
00074
00075 #ifndef USE_BUFFERED_IO
00076 setbuf (recv, NULL);
00077 setbuf (send, NULL);
00078 #endif
00079 db__set_protocol_fds (send, recv);
00080
00081 if(db_driver_init (argc, argv) == DB_OK)
00082 db__send_success();
00083 else
00084 {
00085 db__send_failure();
00086 exit(1);
00087 }
00088
00089
00090 stat = DB_OK;
00091
00092 while (db__recv_procnum (&procnum) == DB_OK)
00093 {
00094 db_clear_error();
00095
00096
00097 for (i = 0; procedure[i].routine; i++)
00098 if (procedure[i].procnum == procnum)
00099 break;
00100
00101
00102 if (procedure[i].routine)
00103 {
00104 if((stat = db__send_procedure_ok(procnum)) != DB_OK)
00105 break;
00106 if((stat = (*procedure[i].routine)()) != DB_OK)
00107 break;
00108 }
00109 else if ((stat = db__send_procedure_not_implemented(procnum)) != DB_OK)
00110 break;
00111 }
00112
00113 db_driver_finish();
00114
00115 exit (stat == DB_OK ? 0 : 1);
00116 }
00117