File: driver.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (117 lines) | stat: -rw-r--r-- 2,416 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include "gis.h"
#include "dbmi.h"
#include "procs.h"
#define	DB_DRIVER_C
#include "dbstubs.h"

extern char *getenv();

/*!
 \fn int db_driver
 \brief 
 \return 
 \param 
*/
int
db_driver(int argc, 
    char *argv[])
{
    int stat;
    int procnum;
    int i;
    int rfd, wfd;
    FILE *send, *recv;
    char *modestr;

    /* Read and set enviroment variables, see dbmi_client/start.c */
    if ( (modestr = getenv ( "GRASS_DB_DRIVER_GISRC_MODE" )) ) {
	int mode;
	mode = atoi ( modestr );
		
	if ( mode == G_GISRC_MODE_MEMORY ) {
	    G_set_gisrc_mode ( G_GISRC_MODE_MEMORY );
	    G__setenv( "DEBUG", getenv ( "DEBUG" ) );
	    G__setenv( "GISDBASE", getenv ( "GISDBASE" ) );
	    G__setenv( "LOCATION_NAME", getenv ( "LOCATION_NAME" ) );
	    G__setenv( "MAPSET", getenv ( "MAPSET" ) );
	    G_debug (3, "Driver GISDBASE set to '%s'", G_getenv ( "GISDBASE" ) );
	}
    }

    send = stdout;
    recv = stdin;

/* THIS CODE IS FOR DEBUGGING WITH CODECENTER */
/**********************************************/
    if (argc == 3)
    {
	rfd = wfd = -1;
	sscanf (argv[1], "%d", &rfd);
	sscanf (argv[2], "%d", &wfd);
	send = fdopen (wfd, "w");
	if (send == NULL)
	{
	    db_syserror(argv[1]);
	    exit(1);
	}
	recv = fdopen (rfd, "r");
	if (recv == NULL)
	{
	    db_syserror(argv[2]);
	    exit(1);
	}
    }
/**********************************************/

    db_clear_error();
    db_auto_print_errors(0);
    db_auto_print_protocol_errors(1);
    db__init_driver_state();

#ifndef USE_BUFFERED_IO
    setbuf (recv, NULL);
    setbuf (send, NULL);
#endif
    db__set_protocol_fds (send, recv);

    if(db_driver_init (argc, argv) == DB_OK)
	db__send_success();
    else
    {
	db__send_failure();
	exit(1);
    }


    stat = DB_OK;
    /* get the procedure number */
    while (db__recv_procnum (&procnum) == DB_OK)
    {
	db_clear_error();

    /* find this procedure */
	for (i = 0; procedure[i].routine; i++)
	    if (procedure[i].procnum == procnum)
		break;

     /* if found, call it */
	if (procedure[i].routine)
	{
	    if((stat = db__send_procedure_ok(procnum)) != DB_OK)
		break; /* while loop */
	    if((stat = (*procedure[i].routine)()) != DB_OK)
		break;
	}
	else if ((stat = db__send_procedure_not_implemented(procnum)) != DB_OK)
	    break;
    }

    db_driver_finish();

    exit (stat == DB_OK ? 0 : 1);
}