args.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Implementation of the GETARG and IARGC g77, and
  2. corresponding F2003, intrinsics.
  3. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. Contributed by Bud Davis and Janne Blomqvist.
  5. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public
  8. License as published by the Free Software Foundation; either
  9. version 3 of the License, or (at your option) any later version.
  10. Libgfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgfortran.h"
  22. #include <string.h>
  23. /* Get a commandline argument. */
  24. extern void getarg_i4 (GFC_INTEGER_4 *, char *, gfc_charlen_type);
  25. iexport_proto(getarg_i4);
  26. void
  27. getarg_i4 (GFC_INTEGER_4 *pos, char *val, gfc_charlen_type val_len)
  28. {
  29. int argc;
  30. int arglen;
  31. char **argv;
  32. get_args (&argc, &argv);
  33. if (val_len < 1 || !val )
  34. return; /* something is wrong , leave immediately */
  35. memset (val, ' ', val_len);
  36. if ((*pos) + 1 <= argc && *pos >=0 )
  37. {
  38. arglen = strlen (argv[*pos]);
  39. if (arglen > val_len)
  40. arglen = val_len;
  41. memcpy (val, argv[*pos], arglen);
  42. }
  43. }
  44. iexport(getarg_i4);
  45. /* INTEGER*8 wrapper of getarg. */
  46. extern void getarg_i8 (GFC_INTEGER_8 *, char *, gfc_charlen_type);
  47. export_proto (getarg_i8);
  48. void
  49. getarg_i8 (GFC_INTEGER_8 *pos, char *val, gfc_charlen_type val_len)
  50. {
  51. GFC_INTEGER_4 pos4 = (GFC_INTEGER_4) *pos;
  52. getarg_i4 (&pos4, val, val_len);
  53. }
  54. /* Return the number of commandline arguments. The g77 info page
  55. states that iargc does not include the specification of the
  56. program name itself. */
  57. extern GFC_INTEGER_4 iargc (void);
  58. export_proto(iargc);
  59. GFC_INTEGER_4
  60. iargc (void)
  61. {
  62. int argc;
  63. char **argv;
  64. get_args (&argc, &argv);
  65. return (argc - 1);
  66. }
  67. /* F2003 intrinsic functions and subroutines related to command line
  68. arguments.
  69. - function command_argument_count() is converted to iargc by the compiler.
  70. - subroutine get_command([command, length, status]).
  71. - subroutine get_command_argument(number, [value, length, status]).
  72. */
  73. /* These two status codes are specified in the standard. */
  74. #define GFC_GC_SUCCESS 0
  75. #define GFC_GC_VALUE_TOO_SHORT -1
  76. /* Processor-specific status failure code. */
  77. #define GFC_GC_FAILURE 42
  78. extern void get_command_argument_i4 (GFC_INTEGER_4 *, char *, GFC_INTEGER_4 *,
  79. GFC_INTEGER_4 *, gfc_charlen_type);
  80. iexport_proto(get_command_argument_i4);
  81. /* Get a single commandline argument. */
  82. void
  83. get_command_argument_i4 (GFC_INTEGER_4 *number, char *value,
  84. GFC_INTEGER_4 *length, GFC_INTEGER_4 *status,
  85. gfc_charlen_type value_len)
  86. {
  87. int argc, arglen = 0, stat_flag = GFC_GC_SUCCESS;
  88. char **argv;
  89. if (number == NULL )
  90. /* Should never happen. */
  91. runtime_error ("Missing argument to get_command_argument");
  92. if (value == NULL && length == NULL && status == NULL)
  93. return; /* No need to do anything. */
  94. get_args (&argc, &argv);
  95. if (*number < 0 || *number >= argc)
  96. stat_flag = GFC_GC_FAILURE;
  97. else
  98. arglen = strlen(argv[*number]);
  99. if (value != NULL)
  100. {
  101. if (value_len < 1)
  102. stat_flag = GFC_GC_FAILURE;
  103. else
  104. memset (value, ' ', value_len);
  105. }
  106. if (value != NULL && stat_flag != GFC_GC_FAILURE)
  107. {
  108. if (arglen > value_len)
  109. stat_flag = GFC_GC_VALUE_TOO_SHORT;
  110. memcpy (value, argv[*number], arglen <= value_len ? arglen : value_len);
  111. }
  112. if (length != NULL)
  113. *length = arglen;
  114. if (status != NULL)
  115. *status = stat_flag;
  116. }
  117. iexport(get_command_argument_i4);
  118. /* INTEGER*8 wrapper for get_command_argument. */
  119. extern void get_command_argument_i8 (GFC_INTEGER_8 *, char *, GFC_INTEGER_8 *,
  120. GFC_INTEGER_8 *, gfc_charlen_type);
  121. export_proto(get_command_argument_i8);
  122. void
  123. get_command_argument_i8 (GFC_INTEGER_8 *number, char *value,
  124. GFC_INTEGER_8 *length, GFC_INTEGER_8 *status,
  125. gfc_charlen_type value_len)
  126. {
  127. GFC_INTEGER_4 number4;
  128. GFC_INTEGER_4 length4;
  129. GFC_INTEGER_4 status4;
  130. number4 = (GFC_INTEGER_4) *number;
  131. get_command_argument_i4 (&number4, value, &length4, &status4, value_len);
  132. if (length)
  133. *length = length4;
  134. if (status)
  135. *status = status4;
  136. }
  137. /* Return the whole commandline. */
  138. extern void get_command_i4 (char *, GFC_INTEGER_4 *, GFC_INTEGER_4 *,
  139. gfc_charlen_type);
  140. iexport_proto(get_command_i4);
  141. void
  142. get_command_i4 (char *command, GFC_INTEGER_4 *length, GFC_INTEGER_4 *status,
  143. gfc_charlen_type command_len)
  144. {
  145. int i, argc, arglen, thisarg;
  146. int stat_flag = GFC_GC_SUCCESS;
  147. int tot_len = 0;
  148. char **argv;
  149. if (command == NULL && length == NULL && status == NULL)
  150. return; /* No need to do anything. */
  151. get_args (&argc, &argv);
  152. if (command != NULL)
  153. {
  154. /* Initialize the string to blanks. */
  155. if (command_len < 1)
  156. stat_flag = GFC_GC_FAILURE;
  157. else
  158. memset (command, ' ', command_len);
  159. }
  160. for (i = 0; i < argc ; i++)
  161. {
  162. arglen = strlen(argv[i]);
  163. if (command != NULL && stat_flag == GFC_GC_SUCCESS)
  164. {
  165. thisarg = arglen;
  166. if (tot_len + thisarg > command_len)
  167. {
  168. thisarg = command_len - tot_len; /* Truncate. */
  169. stat_flag = GFC_GC_VALUE_TOO_SHORT;
  170. }
  171. /* Also a space before the next arg. */
  172. else if (i != argc - 1 && tot_len + arglen == command_len)
  173. stat_flag = GFC_GC_VALUE_TOO_SHORT;
  174. memcpy (&command[tot_len], argv[i], thisarg);
  175. }
  176. /* Add the legth of the argument. */
  177. tot_len += arglen;
  178. if (i != argc - 1)
  179. tot_len++;
  180. }
  181. if (length != NULL)
  182. *length = tot_len;
  183. if (status != NULL)
  184. *status = stat_flag;
  185. }
  186. iexport(get_command_i4);
  187. /* INTEGER*8 wrapper for get_command. */
  188. extern void get_command_i8 (char *, GFC_INTEGER_8 *, GFC_INTEGER_8 *,
  189. gfc_charlen_type);
  190. export_proto(get_command_i8);
  191. void
  192. get_command_i8 (char *command, GFC_INTEGER_8 *length, GFC_INTEGER_8 *status,
  193. gfc_charlen_type command_len)
  194. {
  195. GFC_INTEGER_4 length4;
  196. GFC_INTEGER_4 status4;
  197. get_command_i4 (command, &length4, &status4, command_len);
  198. if (length)
  199. *length = length4;
  200. if (status)
  201. *status = status4;
  202. }