re PR middle-end/32639 (ptrmem1.C now ICE's on mainline)
[official-gcc.git] / libgfortran / intrinsics / args.c
blob26a6b20f169d5ed476b27132b329aec39f5f0741
1 /* Implementation of the GETARG and IARGC g77, and
2 corresponding F2003, intrinsics.
3 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 Contributed by Bud Davis and Janne Blomqvist.
6 This file is part of the GNU Fortran 95 runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
20 executable.)
22 Libgfortran is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public
28 License along with libgfortran; see the file COPYING. If not,
29 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 Boston, MA 02110-1301, USA. */
32 #include "config.h"
33 #include <string.h>
34 #include "libgfortran.h"
37 /* Get a commandline argument. */
39 extern void getarg_i4 (GFC_INTEGER_4 *, char *, gfc_charlen_type);
40 iexport_proto(getarg_i4);
42 void
43 getarg_i4 (GFC_INTEGER_4 *pos, char *val, gfc_charlen_type val_len)
45 int argc;
46 int arglen;
47 char **argv;
49 get_args (&argc, &argv);
51 if (val_len < 1 || !val )
52 return; /* something is wrong , leave immediately */
54 memset (val, ' ', val_len);
56 if ((*pos) + 1 <= argc && *pos >=0 )
58 arglen = strlen (argv[*pos]);
59 if (arglen > val_len)
60 arglen = val_len;
61 memcpy (val, argv[*pos], arglen);
64 iexport(getarg_i4);
67 /* INTEGER*8 wrapper of getarg. */
69 extern void getarg_i8 (GFC_INTEGER_8 *, char *, gfc_charlen_type);
70 export_proto (getarg_i8);
72 void
73 getarg_i8 (GFC_INTEGER_8 *pos, char *val, gfc_charlen_type val_len)
75 GFC_INTEGER_4 pos4 = (GFC_INTEGER_4) *pos;
76 getarg_i4 (&pos4, val, val_len);
80 /* Return the number of commandline arguments. The g77 info page
81 states that iargc does not include the specification of the
82 program name itself. */
84 extern GFC_INTEGER_4 iargc (void);
85 export_proto(iargc);
87 GFC_INTEGER_4
88 iargc (void)
90 int argc;
91 char **argv;
93 get_args (&argc, &argv);
95 return (argc - 1);
99 /* F2003 intrinsic functions and subroutines related to command line
100 arguments.
102 - function command_argument_count() is converted to iargc by the compiler.
104 - subroutine get_command([command, length, status]).
106 - subroutine get_command_argument(number, [value, length, status]).
109 /* These two status codes are specified in the standard. */
110 #define GFC_GC_SUCCESS 0
111 #define GFC_GC_VALUE_TOO_SHORT -1
113 /* Processor-specific status failure code. */
114 #define GFC_GC_FAILURE 42
117 extern void get_command_argument_i4 (GFC_INTEGER_4 *, char *, GFC_INTEGER_4 *,
118 GFC_INTEGER_4 *, gfc_charlen_type);
119 iexport_proto(get_command_argument_i4);
121 /* Get a single commandline argument. */
123 void
124 get_command_argument_i4 (GFC_INTEGER_4 *number, char *value,
125 GFC_INTEGER_4 *length, GFC_INTEGER_4 *status,
126 gfc_charlen_type value_len)
128 int argc, arglen = 0, stat_flag = GFC_GC_SUCCESS;
129 char **argv;
131 if (number == NULL )
132 /* Should never happen. */
133 runtime_error ("Missing argument to get_command_argument");
135 if (value == NULL && length == NULL && status == NULL)
136 return; /* No need to do anything. */
138 get_args (&argc, &argv);
140 if (*number < 0 || *number >= argc)
141 stat_flag = GFC_GC_FAILURE;
142 else
143 arglen = strlen(argv[*number]);
145 if (value != NULL)
147 if (value_len < 1)
148 stat_flag = GFC_GC_FAILURE;
149 else
150 memset (value, ' ', value_len);
153 if (value != NULL && stat_flag != GFC_GC_FAILURE)
155 if (arglen > value_len)
157 arglen = value_len;
158 stat_flag = GFC_GC_VALUE_TOO_SHORT;
160 memcpy (value, argv[*number], arglen);
163 if (length != NULL)
164 *length = arglen;
166 if (status != NULL)
167 *status = stat_flag;
169 iexport(get_command_argument_i4);
172 /* INTEGER*8 wrapper for get_command_argument. */
174 extern void get_command_argument_i8 (GFC_INTEGER_8 *, char *, GFC_INTEGER_8 *,
175 GFC_INTEGER_8 *, gfc_charlen_type);
176 export_proto(get_command_argument_i8);
178 void
179 get_command_argument_i8 (GFC_INTEGER_8 *number, char *value,
180 GFC_INTEGER_8 *length, GFC_INTEGER_8 *status,
181 gfc_charlen_type value_len)
183 GFC_INTEGER_4 number4;
184 GFC_INTEGER_4 length4;
185 GFC_INTEGER_4 status4;
187 number4 = (GFC_INTEGER_4) *number;
188 get_command_argument_i4 (&number4, value, &length4, &status4, value_len);
189 if (length)
190 *length = length4;
191 if (status)
192 *status = status4;
196 /* Return the whole commandline. */
198 extern void get_command_i4 (char *, GFC_INTEGER_4 *, GFC_INTEGER_4 *,
199 gfc_charlen_type);
200 iexport_proto(get_command_i4);
202 void
203 get_command_i4 (char *command, GFC_INTEGER_4 *length, GFC_INTEGER_4 *status,
204 gfc_charlen_type command_len)
206 int i, argc, arglen, thisarg;
207 int stat_flag = GFC_GC_SUCCESS;
208 int tot_len = 0;
209 char **argv;
211 if (command == NULL && length == NULL && status == NULL)
212 return; /* No need to do anything. */
214 get_args (&argc, &argv);
216 if (command != NULL)
218 /* Initialize the string to blanks. */
219 if (command_len < 1)
220 stat_flag = GFC_GC_FAILURE;
221 else
222 memset (command, ' ', command_len);
225 for (i = 0; i < argc ; i++)
227 arglen = strlen(argv[i]);
229 if (command != NULL && stat_flag == GFC_GC_SUCCESS)
231 thisarg = arglen;
232 if (tot_len + thisarg > command_len)
234 thisarg = command_len - tot_len; /* Truncate. */
235 stat_flag = GFC_GC_VALUE_TOO_SHORT;
237 /* Also a space before the next arg. */
238 else if (i != argc - 1 && tot_len + arglen == command_len)
239 stat_flag = GFC_GC_VALUE_TOO_SHORT;
241 memcpy (&command[tot_len], argv[i], thisarg);
244 /* Add the legth of the argument. */
245 tot_len += arglen;
246 if (i != argc - 1)
247 tot_len++;
250 if (length != NULL)
251 *length = tot_len;
253 if (status != NULL)
254 *status = stat_flag;
256 iexport(get_command_i4);
259 /* INTEGER*8 wrapper for get_command. */
261 extern void get_command_i8 (char *, GFC_INTEGER_8 *, GFC_INTEGER_8 *,
262 gfc_charlen_type);
263 export_proto(get_command_i8);
265 void
266 get_command_i8 (char *command, GFC_INTEGER_8 *length, GFC_INTEGER_8 *status,
267 gfc_charlen_type command_len)
269 GFC_INTEGER_4 length4;
270 GFC_INTEGER_4 status4;
272 get_command_i4 (command, &length4, &status4, command_len);
273 if (length)
274 *length = length4;
275 if (status)
276 *status = status4;