driver.c 3.7 KB

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