main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /****************************************************************************
  2. *
  3. * MODULE: r.mapcalc
  4. * AUTHOR(S): Michael Shapiro, CERL (original contributor)
  5. * rewritten 2002: Glynn Clements <glynn gclements.plus.com>
  6. * PURPOSE:
  7. * COPYRIGHT: (C) 1999-2007 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <grass/glocale.h>
  20. #include "mapcalc.h"
  21. /****************************************************************************/
  22. int overflow_occurred;
  23. int overwrite_flag;
  24. volatile int floating_point_exception;
  25. volatile int floating_point_exception_occurred;
  26. /****************************************************************************/
  27. static expr_list *result;
  28. /****************************************************************************/
  29. static RETSIGTYPE handle_fpe(int n)
  30. {
  31. floating_point_exception = 1;
  32. floating_point_exception_occurred = 1;
  33. }
  34. static void pre_exec(void)
  35. {
  36. #ifndef __MINGW32__
  37. #ifdef SIGFPE
  38. struct sigaction act;
  39. act.sa_handler = &handle_fpe;
  40. act.sa_flags = 0;
  41. sigemptyset(&act.sa_mask);
  42. sigaction(SIGFPE, &act, NULL);
  43. #endif
  44. #endif
  45. floating_point_exception_occurred = 0;
  46. overflow_occurred = 0;
  47. }
  48. static void post_exec(void)
  49. {
  50. #ifndef __MINGW32__
  51. #ifdef SIGFPE
  52. struct sigaction act;
  53. act.sa_handler = SIG_DFL;
  54. act.sa_flags = 0;
  55. sigemptyset(&act.sa_mask);
  56. sigaction(SIGFPE, &act, NULL);
  57. #endif
  58. #endif
  59. }
  60. /****************************************************************************/
  61. static expr_list *parse_file(const char *filename)
  62. {
  63. expr_list *res;
  64. FILE *fp;
  65. if (strcmp(filename, "-") == 0)
  66. return parse_stream(stdin);
  67. fp = fopen(filename, "r");
  68. if (!fp)
  69. G_fatal_error(_("Unable to open input file <%s>"), filename);
  70. res = parse_stream(fp);
  71. fclose(fp);
  72. return res;
  73. }
  74. /****************************************************************************/
  75. int main(int argc, char **argv)
  76. {
  77. struct GModule *module;
  78. struct Option *expr, *file;
  79. int all_ok;
  80. G_gisinit(argv[0]);
  81. module = G_define_module();
  82. G_add_keyword(_("raster"));
  83. G_add_keyword(_("algebra"));
  84. module->description = _("Raster map calculator.");
  85. module->overwrite = 1;
  86. expr = G_define_option();
  87. expr->key = "expression";
  88. expr->type = TYPE_STRING;
  89. expr->required = NO;
  90. expr->description = _("Expression to evaluate");
  91. expr->guisection = _("Expression");
  92. file = G_define_standard_option(G_OPT_F_INPUT);
  93. file->key = "file";
  94. file->required = NO;
  95. file->description = _("File containing expression(s) to evaluate");
  96. file->guisection = _("Expression");
  97. if (argc == 1)
  98. {
  99. char **p = G_malloc(3 * sizeof(char *));
  100. p[0] = argv[0];
  101. p[1] = G_store("file=-");
  102. p[2] = NULL;
  103. argv = p;
  104. argc = 2;
  105. }
  106. if (G_parser(argc, argv))
  107. exit(EXIT_FAILURE);
  108. overwrite_flag = module->overwrite;
  109. if (expr->answer && file->answer)
  110. G_fatal_error(_("file= and expression= are mutually exclusive"));
  111. if (expr->answer)
  112. result = parse_string(expr->answer);
  113. else if (file->answer)
  114. result = parse_file(file->answer);
  115. else
  116. result = parse_stream(stdin);
  117. if (!result)
  118. G_fatal_error(_("parse error"));
  119. pre_exec();
  120. execute(result);
  121. post_exec();
  122. all_ok = 1;
  123. if (floating_point_exception_occurred) {
  124. G_warning(_("Floating point error(s) occured in the calculation"));
  125. all_ok = 0;
  126. }
  127. if (overflow_occurred) {
  128. G_warning(_("Overflow occured in the calculation"));
  129. all_ok = 0;
  130. }
  131. return all_ok ? EXIT_SUCCESS : EXIT_FAILURE;
  132. }
  133. /****************************************************************************/