driver.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include <grass/gis.h>
  23. #include <grass/dbmi.h>
  24. #include "procs.h"
  25. #define DB_DRIVER_C
  26. #include "dbstubs.h"
  27. extern char *getenv();
  28. /*!
  29. \brief Get driver (?)
  30. \param argc, argv arguments
  31. \return 0 on success
  32. \return 1 on failure
  33. */
  34. int 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. /* Read and set environment variables, see dbmi_client/start.c */
  43. if ((modestr = getenv("GRASS_DB_DRIVER_GISRC_MODE"))) {
  44. int mode;
  45. mode = atoi(modestr);
  46. if (mode == G_GISRC_MODE_MEMORY) {
  47. G_set_gisrc_mode(G_GISRC_MODE_MEMORY);
  48. G_setenv_nogisrc("DEBUG", getenv("DEBUG"));
  49. G_setenv_nogisrc("GISDBASE", getenv("GISDBASE"));
  50. G_setenv_nogisrc("LOCATION_NAME", getenv("LOCATION_NAME"));
  51. G_setenv_nogisrc("MAPSET", getenv("MAPSET"));
  52. G_debug(3, "Driver GISDBASE set to '%s'", G_getenv("GISDBASE"));
  53. }
  54. }
  55. #ifdef __MINGW32__
  56. /* TODO: */
  57. /* We should close everything except stdin, stdout but _fcloseall()
  58. * closes open streams not file descriptors. _getmaxstdio too big number.
  59. *
  60. * Because the pipes were created just before this driver was started
  61. * the file descriptors should not be above a closed descriptor
  62. * until it was run from a multithread application and some descriptors
  63. * were closed in the mean time.
  64. * Also Windows documentation does not say that new file descriptor is
  65. * the lowest available.
  66. */
  67. {
  68. int err_count = 0;
  69. int cfd = 3;
  70. while (1) {
  71. if (close(cfd) == -1)
  72. err_count++;
  73. /* no good reason for 10 */
  74. if (err_count > 10)
  75. break;
  76. cfd++;
  77. }
  78. }
  79. _setmode(_fileno(stdin), _O_BINARY);
  80. _setmode(_fileno(stdout), _O_BINARY);
  81. #endif
  82. send = stdout;
  83. recv = stdin;
  84. /* THIS CODE IS FOR DEBUGGING WITH CODECENTER */
  85. /**********************************************/
  86. if (argc == 3) {
  87. rfd = wfd = -1;
  88. sscanf(argv[1], "%d", &rfd);
  89. sscanf(argv[2], "%d", &wfd);
  90. send = fdopen(wfd, "w");
  91. if (send == NULL) {
  92. db_syserror(argv[1]);
  93. exit(1);
  94. }
  95. recv = fdopen(rfd, "r");
  96. if (recv == NULL) {
  97. db_syserror(argv[2]);
  98. exit(1);
  99. }
  100. }
  101. /**********************************************/
  102. db_clear_error();
  103. db_auto_print_errors(1);
  104. db_auto_print_protocol_errors(1);
  105. db__init_driver_state();
  106. #ifndef USE_BUFFERED_IO
  107. setbuf(recv, NULL);
  108. setbuf(send, NULL);
  109. #endif
  110. db__set_protocol_fds(send, recv);
  111. if (db_driver_init(argc, argv) == DB_OK)
  112. db__send_success();
  113. else {
  114. db__send_failure();
  115. exit(1);
  116. }
  117. stat = DB_OK;
  118. /* get the procedure number */
  119. while (db__recv_procnum(&procnum) == DB_OK) {
  120. if (procnum == DB_PROC_SHUTDOWN_DRIVER) {
  121. db__send_procedure_ok(procnum);
  122. break;
  123. }
  124. db_clear_error();
  125. /* find this procedure */
  126. for (i = 0; procedure[i].routine; i++)
  127. if (procedure[i].procnum == procnum)
  128. break;
  129. /* if found, call it */
  130. if (procedure[i].routine) {
  131. if ((stat = db__send_procedure_ok(procnum)) != DB_OK)
  132. break; /* while loop */
  133. if ((stat = (*procedure[i].routine) ()) != DB_OK)
  134. break;
  135. }
  136. else if ((stat =
  137. db__send_procedure_not_implemented(procnum)) != DB_OK)
  138. break;
  139. }
  140. db_driver_finish();
  141. exit(stat == DB_OK ? 0 : 1);
  142. }