1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
pub const PRMLEN: i32 = 80;
const DPRMPT: &[u8] = b"Filename? ";
const OSTAT: &[u8] = b"OLD";
const NSTAT: &[u8] = b"NEW";
const FNMLEN: i32 = 1000;
const ACTLEN: i32 = 10;
struct SaveVars {
BADCHR: Vec<u8>,
FIRST: bool,
}
impl SaveInit for SaveVars {
fn new() -> Self {
let mut BADCHR = vec![b' '; 162 as usize];
let mut FIRST: bool = false;
FIRST = true;
Self { BADCHR, FIRST }
}
}
//$Procedure GETFNM_1 ( Get a filename from standard input )
pub fn GETFNM_1(
PRMPT: &[u8],
FSTAT: &[u8],
FNAME: &mut [u8],
VALID: &mut bool,
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let save = ctx.get_vars::<SaveVars>();
let save = &mut *save.borrow_mut();
let mut MYFNAM = [b' '; FNMLEN as usize];
let mut MYPRMT = [b' '; PRMLEN as usize];
let mut NAMBUF = [b' '; FNMLEN as usize];
let mut OLDACT = [b' '; ACTLEN as usize];
let mut STATUS = [b' '; 3 as usize];
let mut I: i32 = 0;
let mut LENGTH: i32 = 0;
let mut TRYAGN: bool = false;
let mut MYVLID: bool = false;
//
// SPICELIB Functions
//
//
// Local Parameters
//
//
// Maximum length of a filename.
//
//
// Length of an error action
//
//
// Local Variables
//
//
// Saved Variables
//
//
// Initial Values
//
//
// Standard SPICE error handling.
//
if spicelib::RETURN(ctx) {
return Ok(());
} else {
spicelib::CHKIN(b"GETFNM_1", ctx)?;
}
//
// We are going to be signalling errors and resetting the error
// handling, so we need to be in RETURN mode. First we get the
// current mode and save it, then we set the mode to return. Upon
// leaving the subroutine, we will restore the error handling mode
// that was in effect when we entered.
//
spicelib::ERRACT(b"GET", &mut OLDACT, ctx)?;
spicelib::ERRACT(b"SET", &mut b"RETURN".clone(), ctx)?;
//
// If this is the first time this routine has been called,
// initialize the ``bad character'' string.
//
if save.FIRST {
save.FIRST = false;
{
let m1__: i32 = 0;
let m2__: i32 = 32;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
fstr::assign(
fstr::substr_mut(&mut save.BADCHR, (I + 1)..=(I + 1)),
&intrinsics::CHAR(I),
);
I += m3__;
}
}
{
let m1__: i32 = 1;
let m2__: i32 = 129;
let m3__: i32 = 1;
I = m1__;
for _ in 0..((m2__ - m1__ + m3__) / m3__) as i32 {
fstr::assign(
fstr::substr_mut(&mut save.BADCHR, (33 + I)..=(33 + I)),
&intrinsics::CHAR((126 + I)),
);
I += m3__;
}
}
}
//
// Left justify and convert the file status to upper case for
// comparisons.
//
spicelib::LJUST(FSTAT, &mut STATUS);
spicelib::UCASE(&STATUS.clone(), &mut STATUS, ctx);
//
// Check to see if we have a valid status for the filename.
//
if (fstr::ne(&STATUS, OSTAT) && fstr::ne(&STATUS, NSTAT)) {
spicelib::SETMSG(b"The file status \'#\' was not valid. The file status must have a value of \'NEW\' or \'OLD\'.", ctx);
spicelib::ERRCH(b"#", &STATUS, ctx);
spicelib::SIGERR(b"SPICE(INVALIDARGUMENT)", ctx)?;
spicelib::CHKOUT(b"GETFNM_1", ctx)?;
return Ok(());
}
//
// Store the input value for the prompt into our local value. We do
// this for pedantic Fortran compilers that issue warnings for
// CHARACTER*(*) variables used with concatenation.
//
fstr::assign(&mut MYPRMT, PRMPT);
//
// Read in a potential filename, and test it for validity.
//
TRYAGN = true;
while TRYAGN {
//
// Set the value of the valid flag to .TRUE.. We assume that the
// name entered will be a valid one.
//
MYVLID = true;
//
// Get the filename.
//
if fstr::eq(&MYPRMT, b" ") {
spicelib::PROMPT(DPRMPT, &mut MYFNAM, ctx)?;
} else {
spicelib::PROMPT(
&fstr::concat(fstr::substr(&MYPRMT, 1..=spicelib::LASTNB(&MYPRMT)), b" "),
&mut MYFNAM,
ctx,
)?;
}
if spicelib::FAILED(ctx) {
MYVLID = false;
}
//
// Translate the name if it's an environment variable.
//
EXPFNM_2(&MYFNAM, &mut NAMBUF, ctx)?;
fstr::assign(&mut MYFNAM, &NAMBUF);
if spicelib::FAILED(ctx) {
MYVLID = false;
}
if MYVLID {
if fstr::eq(&MYFNAM, b" ") {
MYVLID = false;
spicelib::SETMSG(b"The filename entered was blank.", ctx);
spicelib::SIGERR(b"SPICE(BLANKFILENAME)", ctx)?;
}
}
if MYVLID {
//
// Left justify the filename.
//
spicelib::LJUST(&MYFNAM.clone(), &mut MYFNAM);
//
// Check for bad characters in the filename.
//
LENGTH = spicelib::LASTNB(&MYFNAM);
I = spicelib::CPOS(fstr::substr(&MYFNAM, 1..=LENGTH), &save.BADCHR, 1);
if (I > 0) {
MYVLID = false;
spicelib::SETMSG(
b"The filename entered contains non printing characters or embedded blanks.",
ctx,
);
spicelib::SIGERR(b"SPICE(ILLEGALCHARACTER)", ctx)?;
}
}
if MYVLID {
//
// We know that the filename that was entered was nonblank and
// had no bad characters. So, now we take care of the status
// question.
//
if fstr::eq(&STATUS, OSTAT) {
if !spicelib::EXISTS(fstr::substr(&MYFNAM, 1..=spicelib::RTRIM(&MYFNAM)), ctx)? {
MYVLID = false;
spicelib::SETMSG(b"A file with the name \'#\' does not exist.", ctx);
spicelib::ERRCH(b"#", &MYFNAM, ctx);
spicelib::SIGERR(b"SPICE(FILEDOESNOTEXIST)", ctx)?;
}
} else if fstr::eq(&STATUS, NSTAT) {
if spicelib::EXISTS(fstr::substr(&MYFNAM, 1..=spicelib::RTRIM(&MYFNAM)), ctx)? {
MYVLID = false;
spicelib::SETMSG(b"A file with the name \'#\' already exists.", ctx);
spicelib::ERRCH(b"#", &MYFNAM, ctx);
spicelib::SIGERR(b"SPICE(FILEALREADYEXISTS)", ctx)?;
}
}
}
if MYVLID {
TRYAGN = false;
} else {
spicelib::WRITLN(b" ", 6, ctx)?;
CNFIRM(b"Try again? (Yes/No) ", &mut TRYAGN, ctx)?;
spicelib::WRITLN(b" ", 6, ctx)?;
if TRYAGN {
spicelib::RESET(ctx);
}
}
}
//
// At this point, we have done the best we can. If the status
// was new, we might still have an invalid filename, but the
// exact reasons for its invalidity are system dependent, and
// therefore hard to test.
//
*VALID = MYVLID;
if *VALID {
fstr::assign(FNAME, fstr::substr(&MYFNAM, 1..=spicelib::RTRIM(&MYFNAM)));
}
//
// Restore the error action.
//
spicelib::ERRACT(b"SET", &mut OLDACT, ctx)?;
spicelib::CHKOUT(b"GETFNM_1", ctx)?;
Ok(())
}