annotate libgfortran/intrinsics/args.c @ 158:494b0b89df80 default tip

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