GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
driver.c
Go to the documentation of this file.
1 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <grass/gis.h>
24 #include <grass/dbmi.h>
25 #include "procs.h"
26 #define DB_DRIVER_C
27 #include "dbstubs.h"
28 
29 extern char *getenv();
30 
39 int db_driver(int argc, char *argv[])
40 {
41  int stat;
42  int procnum;
43  int i;
44  int rfd, wfd;
45  FILE *send, *recv;
46  char *modestr;
47 
48  /* Read and set environment variables, see dbmi_client/start.c */
49  if ((modestr = getenv("GRASS_DB_DRIVER_GISRC_MODE"))) {
50  int mode;
51 
52  mode = atoi(modestr);
53 
54  if (mode == G_GISRC_MODE_MEMORY) {
55  G_set_gisrc_mode(G_GISRC_MODE_MEMORY);
56  G__setenv("DEBUG", getenv("DEBUG"));
57  G__setenv("GISDBASE", getenv("GISDBASE"));
58  G__setenv("LOCATION_NAME", getenv("LOCATION_NAME"));
59  G__setenv("MAPSET", getenv("MAPSET"));
60  G_debug(3, "Driver GISDBASE set to '%s'", G_getenv("GISDBASE"));
61  }
62  }
63 
64 #ifdef __MINGW32__
65  /* TODO: */
66  /* We should close everything except stdin, stdout but _fcloseall()
67  * closes open streams not file descriptors. _getmaxstdio too big number.
68  *
69  * Because the pipes were created just before this driver was started
70  * the file descriptors should not be above a closed descriptor
71  * until it was run from a multithread application and some descriptors
72  * were closed in the mean time.
73  * Also Windows documentation does not say that new file descriptor is
74  * the lowest available.
75  */
76 
77  {
78  int err_count = 0;
79  int cfd = 3;
80 
81  while (1) {
82  if (close(cfd) == -1)
83  err_count++;
84 
85  /* no good reason for 10 */
86  if (err_count > 10)
87  break;
88 
89  cfd++;
90  }
91  }
92 
93  _setmode(_fileno(stdin), _O_BINARY);
94  _setmode(_fileno(stdout), _O_BINARY);
95 #endif
96 
97  send = stdout;
98  recv = stdin;
99 
100  /* THIS CODE IS FOR DEBUGGING WITH CODECENTER */
101 
102 /**********************************************/
103  if (argc == 3) {
104  rfd = wfd = -1;
105  sscanf(argv[1], "%d", &rfd);
106  sscanf(argv[2], "%d", &wfd);
107  send = fdopen(wfd, "w");
108  if (send == NULL) {
109  db_syserror(argv[1]);
110  exit(1);
111  }
112  recv = fdopen(rfd, "r");
113  if (recv == NULL) {
114  db_syserror(argv[2]);
115  exit(1);
116  }
117  }
118 
119 /**********************************************/
120 
121  db_clear_error();
125 
126 #ifndef USE_BUFFERED_IO
127  setbuf(recv, NULL);
128  setbuf(send, NULL);
129 #endif
130  db__set_protocol_fds(send, recv);
131 
132  if (db_driver_init(argc, argv) == DB_OK)
134  else {
136  exit(1);
137  }
138 
139  stat = DB_OK;
140  /* get the procedure number */
141  while (db__recv_procnum(&procnum) == DB_OK) {
142 #ifdef __MINGW32__
143  if (procnum == DB_PROC_SHUTDOWN_DRIVER) {
144  db__send_procedure_ok(procnum);
145  break;
146  }
147 #endif
148  db_clear_error();
149 
150  /* find this procedure */
151  for (i = 0; procedure[i].routine; i++)
152  if (procedure[i].procnum == procnum)
153  break;
154 
155  /* if found, call it */
156  if (procedure[i].routine) {
157  if ((stat = db__send_procedure_ok(procnum)) != DB_OK)
158  break; /* while loop */
159  if ((stat = (*procedure[i].routine) ()) != DB_OK)
160  break;
161  }
162  else if ((stat =
163  db__send_procedure_not_implemented(procnum)) != DB_OK)
164  break;
165  }
166 
168 
169  exit(stat == DB_OK ? 0 : 1);
170 }
void db__set_protocol_fds(FILE *send, FILE *recv)
Definition: xdr.c:71
int db__send_success()
Definition: ret_codes.c:5
void db_auto_print_protocol_errors(int flag)
int db__send_failure()
Definition: ret_codes.c:11
void db_auto_print_errors(int flag)
char * G_getenv(const char *name)
Get environment variable.
Definition: env.c:267
char * getenv()
int(* routine)()
Definition: procs.h:35
int stat
Definition: g3dcolor.c:369
void db_syserror(const char *s)
void G_set_gisrc_mode(int mode)
Set where to find/store variables.
Definition: env.c:59
int db__recv_procnum(int *n)
Definition: xdrprocedure.c:31
int db__send_procedure_not_implemented(int n)
Definition: xdrprocedure.c:46
void db_clear_error(void)
int db_driver(int argc, char *argv[])
Get driver (?)
Definition: driver.c:39
int(* db_driver_finish)()
return NULL
Definition: dbfopen.c:1394
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int G__setenv(const char *name, const char *value)
Set environment name to value.
Definition: env.c:388
int db__send_procedure_ok(int n)
Definition: xdrprocedure.c:41
void db__init_driver_state(void)
Initialize driver state.
Definition: driver_state.c:25
int procnum
Definition: procs.h:34
int(* db_driver_init)()
tuple mode
Definition: tools.py:1481