GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 1999-2008 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Joel Jones (CERL/UIUC), Radim Blazek
10 * \author Modified by Glynn Clements <glynn gclements.plus.com>,
11 * Markus Neteler <neteler itc.it>,
12 * Huidae Cho <grass4u gmail.com>
13 */
14
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <stdlib.h>
18#include <stdio.h>
19#include <fcntl.h>
20#include <grass/gis.h>
21#include <grass/dbmi.h>
22#include "procs.h"
23#define DB_DRIVER_C
24#include "dbstubs.h"
25
26/*!
27 \brief Get driver (?)
28
29 \param argc, argv arguments
30
31 \return 0 on success
32 \return 1 on failure
33 */
34int db_driver(int argc, char *argv[])
35{
36 int stat;
37 int procnum;
38 int i;
39 int rfd, wfd;
40 FILE *send, *recv;
41 char *modestr;
42
43 /* Read and set environment variables, see dbmi_client/start.c */
44 if ((modestr = getenv("GRASS_DB_DRIVER_GISRC_MODE"))) {
45 int mode;
46
47 mode = atoi(modestr);
48
49 if (mode == G_GISRC_MODE_MEMORY) {
51 G_setenv_nogisrc("DEBUG", getenv("DEBUG"));
52 G_setenv_nogisrc("GISDBASE", getenv("GISDBASE"));
53 G_setenv_nogisrc("LOCATION_NAME", getenv("LOCATION_NAME"));
54 G_setenv_nogisrc("MAPSET", getenv("MAPSET"));
55 G_debug(3, "Driver GISDBASE set to '%s'", G_getenv("GISDBASE"));
56 }
57 }
58
59#ifdef __MINGW32__
60 /* TODO: */
61 /* We should close everything except stdin, stdout but _fcloseall()
62 * closes open streams not file descriptors. _getmaxstdio too big number.
63 *
64 * Because the pipes were created just before this driver was started
65 * the file descriptors should not be above a closed descriptor
66 * until it was run from a multithread application and some descriptors
67 * were closed in the mean time.
68 * Also Windows documentation does not say that new file descriptor is
69 * the lowest available.
70 */
71
72 {
73 int err_count = 0;
74 int cfd = 3;
75
76 while (1) {
77 if (close(cfd) == -1)
78 err_count++;
79
80 /* no good reason for 10 */
81 if (err_count > 10)
82 break;
83
84 cfd++;
85 }
86 }
87
90#endif
91
92 send = stdout;
93 recv = stdin;
94
95 /* THIS CODE IS FOR DEBUGGING WITH CODECENTER */
96
97 /**********************************************/
98 if (argc == 3) {
99 rfd = wfd = -1;
100 sscanf(argv[1], "%d", &rfd);
101 sscanf(argv[2], "%d", &wfd);
102 send = fdopen(wfd, "w");
103 if (send == NULL) {
104 db_syserror(argv[1]);
105 exit(1);
106 }
107 recv = fdopen(rfd, "r");
108 if (recv == NULL) {
109 db_syserror(argv[2]);
110 exit(1);
111 }
112 }
113
114 /**********************************************/
115
120
121#ifndef USE_BUFFERED_IO
122 setbuf(recv, NULL);
123 setbuf(send, NULL);
124#endif
125 db__set_protocol_fds(send, recv);
126
127 if (db_driver_init(argc, argv) == DB_OK)
129 else {
131 exit(1);
132 }
133
134 stat = DB_OK;
135 /* get the procedure number */
136 while (db__recv_procnum(&procnum) == DB_OK) {
139 break;
140 }
142
143 /* find this procedure */
144 for (i = 0; procedure[i].routine; i++)
145 if (procedure[i].procnum == procnum)
146 break;
147
148 /* if found, call it */
149 if (procedure[i].routine) {
151 break; /* while loop */
152 if ((stat = (*procedure[i].routine)()) != DB_OK)
153 break;
154 }
156 break;
157 }
158
160
161 exit(stat == DB_OK ? 0 : 1);
162}
#define NULL
Definition ccmath.h:32
int db_driver(int argc, char *argv[])
Get driver (?)
Main header of GRASS DataBase Management Interface.
#define DB_PROC_SHUTDOWN_DRIVER
Definition dbmi.h:33
#define DB_OK
Definition dbmi.h:69
int(* db_driver_finish)(void)
int(* db_driver_init)(int, char **)
int db__send_procedure_ok(int)
?
int db__recv_procnum(int *)
? (driver only)
void db_clear_error(void)
Clear error status.
int db__send_success(void)
Send success code.
Definition ret_codes.c:24
int db__send_failure(void)
Send failure code.
Definition ret_codes.c:35
int db__send_procedure_not_implemented(int)
?
void db_auto_print_protocol_errors(int)
Set auto print protocol error.
void db__init_driver_state(void)
Initialize driver state.
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.
const char * G_getenv(const char *)
Get environment variable.
Definition env.c:358
void G_set_gisrc_mode(int)
Set where to find/store variables.
Definition env.c:61
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:470
Header file for msvc/fcntl.c.
#define G_GISRC_MODE_MEMORY
Definition gis.h:183
int procnum
Definition procs.h:33
int(* routine)(void)
Definition procs.h:34
#define fdopen
Definition stdio.h:6
#define close
Definition unistd.h:8