main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* This file is part of the GNU plotutils package. */
  2. /* This file is part of the GNU plotutils package. Copyright (C) 1989,
  3. 1996, 1997, 1998, 1999, 2005, 2008, 2009, Free Software Foundation, Inc.
  4. The GNU plotutils package is free software. You may redistribute it
  5. and/or modify it under the terms of the GNU General Public License as
  6. published by the Free Software foundation; either version 2, or (at your
  7. option) any later version.
  8. The GNU plotutils package is distributed in the hope that it will be
  9. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with the GNU plotutils package; see the file COPYING. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. /*
  17. * main() routine for ode, including command-line parser.
  18. */
  19. #include "sys-defines.h"
  20. #include "ode.h"
  21. #include "extern.h"
  22. #include "getopt.h"
  23. #define ARG_NONE 0
  24. #define ARG_REQUIRED 1
  25. #define ARG_OPTIONAL 2
  26. struct option long_options[] =
  27. {
  28. {"input-file", ARG_REQUIRED, NULL, 'f'},
  29. {"precision", ARG_REQUIRED, NULL, 'p'},
  30. /* integration algorithms */
  31. {"adams-moulton", ARG_OPTIONAL, NULL, 'A'}, /* 0 or 1 */
  32. {"euler", ARG_OPTIONAL, NULL, 'E'}, /* 0 or 1 */
  33. {"runge-kutta", ARG_OPTIONAL, NULL, 'R'}, /* 0 or 1 */
  34. /* error bounds */
  35. {"absolute-error-bound", ARG_REQUIRED, NULL, 'e'}, /* 1 or 2 */
  36. {"step-size-bound", ARG_REQUIRED, NULL, 'h'}, /* 1 or 2 */
  37. {"relative-error-bound", ARG_REQUIRED, NULL, 'r'}, /* 1 or 2 */
  38. {"suppress-error-bound", ARG_NONE, NULL, 's'},
  39. {"title", ARG_NONE, NULL, 't'},
  40. /* Long options with no equivalent short option alias */
  41. {"version", ARG_NONE, NULL, 'V' << 8},
  42. {"help", ARG_NONE, NULL, 'h' << 8},
  43. {NULL, 0, 0, 0}
  44. };
  45. /* null-terminated list of options that we don't show to the user */
  46. int hidden_options[] = { 0 };
  47. /* forward references */
  48. static void fatal (const char *s);
  49. /*
  50. * fatal error message
  51. */
  52. static void
  53. fatal (const char *s)
  54. {
  55. fprintf (stderr, "%s: %s\n", progname, s);
  56. exit (EXIT_FAILURE);
  57. }
  58. int
  59. main (int argc, char *argv[])
  60. {
  61. int option;
  62. int opt_index;
  63. int errcnt = 0; /* errors encountered */
  64. bool show_version = false; /* remember to show version message */
  65. bool show_usage = false; /* remember whether to output usage message. */
  66. double local_tstep, local_hmax;
  67. FILE *infile = NULL;
  68. for ( ; ; )
  69. {
  70. option = getopt_long (argc, argv, "e:f:h:p:r:stA::E::R::V", long_options, &opt_index);
  71. if (option == 0)
  72. option = long_options[opt_index].val;
  73. switch (option)
  74. {
  75. /* ----------- options with no argument --------------*/
  76. case 's': /* Suppress error bound, ARG NONE */
  77. sflag = true;
  78. break;
  79. case 't': /* Title, ARG NONE */
  80. tflag = true;
  81. if (!pflag)
  82. {
  83. prec = 6;
  84. fwd = 13;
  85. }
  86. break;
  87. case 'V' << 8: /* Version, ARG NONE */
  88. show_version = true;
  89. break;
  90. case 'h' << 8: /* Help, ARG NONE */
  91. show_usage = true;
  92. break;
  93. /*----------- options with a single argument --------------*/
  94. case 'f': /* File name, ARG REQUIRED */
  95. filename = xstrdup (optarg);
  96. break;
  97. case 'p': /* Precision, ARG REQUIRED */
  98. pflag = true;
  99. if (sscanf (optarg, "%d", &prec) <= 0)
  100. fatal ("-p: bad argument");
  101. prec--;
  102. if (prec <= 0 || prec > 18)
  103. fatal ("-p: argument must be in the range 2..19");
  104. fwd = prec + 7;
  105. if (fwd < 9)
  106. fwd = 9;
  107. break;
  108. /*----------- options with 0 or 1 arguments --------------*/
  109. case 'A': /* Adams-Moulton */
  110. algorithm = A_ADAMS_MOULTON;
  111. if (optind >= argc)
  112. break;
  113. /* try to parse next arg as a float */
  114. if (sscanf (argv[optind], "%lf", &local_tstep) <= 0)
  115. break;
  116. tstep = local_tstep;
  117. optind++; /* tell getopt we recognized timestep */
  118. conflag = true;
  119. break;
  120. case 'E': /* Euler */
  121. algorithm = A_EULER;
  122. conflag = true;
  123. tstep = 0.1;
  124. if (optind >= argc)
  125. break;
  126. /* try to parse next arg as a float */
  127. if (sscanf (argv[optind], "%lf", &local_tstep) <= 0)
  128. break;
  129. tstep = local_tstep;
  130. optind++; /* tell getopt we recognized timestep */
  131. break;
  132. case 'R': /* Runge-Kutta-Fehlberg */
  133. algorithm = A_RUNGE_KUTTA_FEHLBERG;
  134. if (optind >= argc)
  135. break;
  136. /* try to parse next arg as a float */
  137. if (sscanf (argv[optind], "%lf", &local_tstep) <= 0)
  138. break;
  139. tstep = local_tstep;
  140. optind++; /* tell getopt we recognized timestep */
  141. conflag = true;
  142. break;
  143. /*----------- options with 1 or 2 arguments --------------*/
  144. case 'h': /* Step Size Bound(s) */
  145. if (sscanf (optarg, "%lf", &hmin) <= 0)
  146. fatal ("-h: bad argument");
  147. if (hmin < HMIN)
  148. fatal ("-h: value too small");
  149. if (optind >= argc)
  150. break;
  151. /* try to parse next arg as a float */
  152. if (sscanf (argv [optind], "%lf", &local_hmax) <= 0)
  153. break;
  154. hmax = local_hmax;
  155. optind++; /* tell getopt we recognized hmax */
  156. hflag = true;
  157. break;
  158. case 'r': /* Relative Error Bound(s) */
  159. rflag = true;
  160. if (sscanf (optarg, "%lf", &ssmax) <= 0)
  161. fatal ("-r: bad argument");
  162. if (ssmax < HMIN)
  163. fatal ("-r: max value too small");
  164. if (optind >= argc)
  165. break;
  166. /* try to parse next arg as a float */
  167. if (sscanf (argv [optind], "%lf", &ssmin) <= 0)
  168. {
  169. ssmin = ssmax * SCALE;
  170. break;
  171. }
  172. optind++; /* tell getopt we recognized ssmin */
  173. break;
  174. case 'e': /* Absolute Error Bound(s) */
  175. eflag = true;
  176. if (sscanf (optarg, "%lf", &abmax) <= 0)
  177. fatal ("-e: bad argument");
  178. if (abmax < HMIN)
  179. fatal ("-e: max value too small");
  180. if (optind >= argc)
  181. /* try to parse next arg as a float */
  182. break;
  183. if (sscanf (argv [optind], "%lf", &abmin) <= 0)
  184. {
  185. abmin = abmax * SCALE;
  186. break;
  187. }
  188. optind++; /* tell getopt we recognized abmin */
  189. break;
  190. /*---------------- End of options ----------------*/
  191. default: /* Default, unknown option */
  192. errcnt++;
  193. break;
  194. } /* endswitch */
  195. if ((option == EOF))
  196. {
  197. errcnt--;
  198. break; /* break out of option processing */
  199. }
  200. } /* endwhile */
  201. if (optind < argc) /* too many arguments */
  202. {
  203. fprintf (stderr, "%s: there are too many arguments\n", progname);
  204. errcnt++;
  205. }
  206. if (errcnt > 0)
  207. {
  208. fprintf (stderr, "Try `%s --help' for more information\n", progname);
  209. return EXIT_FAILURE;
  210. }
  211. if (show_version)
  212. {
  213. display_version (progname, written, copyright);
  214. return EXIT_SUCCESS;
  215. }
  216. if (show_usage)
  217. {
  218. display_usage (progname, hidden_options, NULL, 0);
  219. return EXIT_SUCCESS;
  220. }
  221. /* Some sanity checks on user-supplied options. */
  222. if (algorithm == A_EULER && (eflag || rflag))
  223. fatal ("-E [Euler] illegal with -e or -r");
  224. /* DO IT */
  225. if (filename != NULL)
  226. {
  227. infile = fopen (filename, "r");
  228. if (infile == NULL)
  229. {
  230. fprintf (stderr, "%s: %s: %s\n", progname, filename, strerror(errno));
  231. return EXIT_FAILURE;
  232. }
  233. yyin = infile;
  234. /* will switch later to stdin, in yywrap() */
  235. }
  236. else
  237. {
  238. yyin = stdin;
  239. filename = "";
  240. }
  241. yyparse();
  242. return EXIT_SUCCESS;
  243. }