GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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 /*!
30  \brief Get driver (?)
31 
32  \param argc, argv arguments
33 
34  \return 0 on success
35  \return 1 on failure
36  */
37 int db_driver(int argc, char *argv[])
38 {
39  int stat;
40  int procnum;
41  int i;
42  int rfd, wfd;
43  FILE *send, *recv;
44  char *modestr;
45 
46  /* Read and set environment variables, see dbmi_client/start.c */
47  if ((modestr = getenv("GRASS_DB_DRIVER_GISRC_MODE"))) {
48  int mode;
49 
50  mode = atoi(modestr);
51 
52  if (mode == G_GISRC_MODE_MEMORY) {
54  G_setenv_nogisrc("DEBUG", getenv("DEBUG"));
55  G_setenv_nogisrc("GISDBASE", getenv("GISDBASE"));
56  G_setenv_nogisrc("LOCATION_NAME", getenv("LOCATION_NAME"));
57  G_setenv_nogisrc("MAPSET", getenv("MAPSET"));
58  G_debug(3, "Driver GISDBASE set to '%s'", G_getenv("GISDBASE"));
59  }
60  }
61 
62 #ifdef __MINGW32__
63  /* TODO: */
64  /* We should close everything except stdin, stdout but _fcloseall()
65  * closes open streams not file descriptors. _getmaxstdio too big number.
66  *
67  * Because the pipes were created just before this driver was started
68  * the file descriptors should not be above a closed descriptor
69  * until it was run from a multithread application and some descriptors
70  * were closed in the mean time.
71  * Also Windows documentation does not say that new file descriptor is
72  * the lowest available.
73  */
74 
75  {
76  int err_count = 0;
77  int cfd = 3;
78 
79  while (1) {
80  if (close(cfd) == -1)
81  err_count++;
82 
83  /* no good reason for 10 */
84  if (err_count > 10)
85  break;
86 
87  cfd++;
88  }
89  }
90 
91  _setmode(_fileno(stdin), _O_BINARY);
92  _setmode(_fileno(stdout), _O_BINARY);
93 #endif
94 
95  send = stdout;
96  recv = stdin;
97 
98  /* THIS CODE IS FOR DEBUGGING WITH CODECENTER */
99 
100  /**********************************************/
101  if (argc == 3) {
102  rfd = wfd = -1;
103  sscanf(argv[1], "%d", &rfd);
104  sscanf(argv[2], "%d", &wfd);
105  send = fdopen(wfd, "w");
106  if (send == NULL) {
107  db_syserror(argv[1]);
108  exit(1);
109  }
110  recv = fdopen(rfd, "r");
111  if (recv == NULL) {
112  db_syserror(argv[2]);
113  exit(1);
114  }
115  }
116 
117  /**********************************************/
118 
119  db_clear_error();
123 
124 #ifndef USE_BUFFERED_IO
125  setbuf(recv, NULL);
126  setbuf(send, NULL);
127 #endif
128  db__set_protocol_fds(send, recv);
129 
130  if (db_driver_init(argc, argv) == DB_OK)
132  else {
134  exit(1);
135  }
136 
137  stat = DB_OK;
138  /* get the procedure number */
139  while (db__recv_procnum(&procnum) == DB_OK) {
142  break;
143  }
144  db_clear_error();
145 
146  /* find this procedure */
147  for (i = 0; procedure[i].routine; i++)
148  if (procedure[i].procnum == procnum)
149  break;
150 
151  /* if found, call it */
152  if (procedure[i].routine) {
153  if ((stat = db__send_procedure_ok(procnum)) != DB_OK)
154  break; /* while loop */
155  if ((stat = (*procedure[i].routine)()) != DB_OK)
156  break;
157  }
158  else if ((stat = db__send_procedure_not_implemented(procnum)) != DB_OK)
159  break;
160  }
161 
163 
164  exit(stat == DB_OK ? 0 : 1);
165 }
#define NULL
Definition: ccmath.h:32
int db_driver(int argc, char *argv[])
Get driver (?)
#define DB_PROC_SHUTDOWN_DRIVER
Definition: dbmi.h:35
#define DB_OK
Definition: dbmi.h:71
int(* db_driver_finish)(void)
int(* db_driver_init)(int, char **)
int db__send_procedure_ok(int)
?
Definition: xdrprocedure.c:69
int db__recv_procnum(int *)
? (driver only)
Definition: xdrprocedure.c:52
void db_clear_error(void)
Clear error status.
int db__send_success(void)
Send success code.
Definition: ret_codes.c:26
int db__send_failure(void)
Send failure code.
Definition: ret_codes.c:37
int db__send_procedure_not_implemented(int)
?
Definition: xdrprocedure.c:81
void db_auto_print_protocol_errors(int)
Set auto print protocol error.
void db__init_driver_state(void)
Initialize driver state.
Definition: driver_state.c:24
void db__set_protocol_fds(FILE *, FILE *)
?
void db_syserror(const char *)
Report system error.
void db_auto_print_errors(int)
Toggles printing of DBMI error messages.
void G_set_gisrc_mode(int)
Set where to find/store variables.
Definition: env.c:63
const char * G_getenv(const char *)
Get environment variable.
Definition: env.c:360
int G_debug(int, const char *,...) __attribute__((format(printf
void G_setenv_nogisrc(const char *, const char *)
Set environment name to value (doesn't update .gisrc)
Definition: env.c:472
#define G_GISRC_MODE_MEMORY
Definition: gis.h:181
int procnum
Definition: procs.h:33
int(* routine)(void)
Definition: procs.h:34