GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
db/dbmi_driver/driver.c
Go to the documentation of this file.
1 /*!
2  * \file db/dbmi_driver/driver.c
3  *
4  * \brief DBMI Library (driver) - drivers
5  *
6  * (C) 1999-2008 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author Joel Jones (CERL/UIUC), Radim Blazek
13  * \author Modified by Glynn Clements <glynn gclements.plus.com>,
14  * Markus Neteler <neteler itc.it>,
15  * Huidae Cho <grass4u gmail.com>
16  */
17 
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 
31 /*!
32  \brief Get driver (?)
33 
34  \param argc, argv arguments
35 
36  \return 0 on success
37  \return 1 on failure
38  */
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) {
56  G_setenv_nogisrc("DEBUG", getenv("DEBUG"));
57  G_setenv_nogisrc("GISDBASE", getenv("GISDBASE"));
58  G_setenv_nogisrc("LOCATION_NAME", getenv("LOCATION_NAME"));
59  G_setenv_nogisrc("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  if (procnum == DB_PROC_SHUTDOWN_DRIVER) {
143  db__send_procedure_ok(procnum);
144  break;
145  }
146  db_clear_error();
147 
148  /* find this procedure */
149  for (i = 0; procedure[i].routine; i++)
150  if (procedure[i].procnum == procnum)
151  break;
152 
153  /* if found, call it */
154  if (procedure[i].routine) {
155  if ((stat = db__send_procedure_ok(procnum)) != DB_OK)
156  break; /* while loop */
157  if ((stat = (*procedure[i].routine) ()) != DB_OK)
158  break;
159  }
160  else if ((stat =
162  break;
163  }
164 
166 
167  exit(stat == DB_OK ? 0 : 1);
168 }
void db_auto_print_protocol_errors(int)
Set auto print protocol error.
void G_setenv_nogisrc(const char *, const char *)
Set environment name to value (doesn&#39;t update .gisrc)
Definition: env.c:450
int db__send_procedure_not_implemented(int)
?
Definition: xdrprocedure.c:81
int db__send_failure(void)
Send failure code.
Definition: ret_codes.c:37
int db__send_success(void)
Send success code.
Definition: ret_codes.c:26
void db_syserror(const char *)
Report system error.
#define NULL
Definition: ccmath.h:32
int db__send_procedure_ok(int)
?
Definition: xdrprocedure.c:69
void G_set_gisrc_mode(int)
Set where to find/store variables.
Definition: env.c:63
void db_auto_print_errors(int)
Toggles printing of DBMI error messages.
int db_driver(int argc, char *argv[])
Get driver (?)
void db_clear_error(void)
Clear error status.
int(* db_driver_init)(int, char **)
int(* routine)()
Definition: procs.h:35
#define DB_PROC_SHUTDOWN_DRIVER
Definition: dbmi.h:35
int db__recv_procnum(int *)
? (driver only)
Definition: xdrprocedure.c:52
#define G_GISRC_MODE_MEMORY
Definition: gis.h:157
void db__init_driver_state(void)
Initialize driver state.
Definition: driver_state.c:25
void db__set_protocol_fds(FILE *, FILE *)
?
char * getenv()
int procnum
Definition: procs.h:34
int G_debug(int, const char *,...) __attribute__((format(printf
#define DB_OK
Definition: dbmi.h:71
const char * G_getenv(const char *)
Get environment variable.
Definition: env.c:338
int(* db_driver_finish)(void)