-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
354 lines (326 loc) · 7.51 KB
/
utils.c
File metadata and controls
354 lines (326 loc) · 7.51 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* $Id: utils.c,v 1.2 2022/07/29 02:09:54 dave Exp dave $ $
rtutils.c - Misc utilties used by rtpip
Copyright (C) 2008 David Shepperd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rtpip.h"
/**
* @file rtutils.c
* Misc utilities used by rtpip.
*/
/**
* RT11 file dates are stored in aun signed short as:
* (month<<10) | (day<<5) | ((year-72)&31)
* where month is 1-12 for Jan-Dec
* day is 1-31
* year is tens and units digits of year. It is not Y2K
* complient; wraps at 1999 back to base year 1972).
* However, there is another option specified, but it appears
* RT11 v5.3 doesn't support it. The top two bits of the date
* can specify the base year:
* 0 - 1972
* 1 - 2004
* 2 - 2036
* 3 - 2068
*
*/
static const char *const months[] = {
"NUL",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
"?C?",
"?D?",
"?E?",
"?F?"
};
/* Radix50 chars pack 3 ASCII characters to an unsigned 16 bit number. */
static const char r50[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.%0123456789????????????????????????";
/**
* Convert a single ascii character to a rad50 integer.
* @param src - ascii character
*
* @return - character converted to rad50. Unconvertable characters
* are changed to rad50 space (0).
*/
int char2r50(char src)
{
int tmp;
if ( src != ' ' )
{
if ( src == '$' )
{
return R50_DOLLAR; /* 27 */
}
if ( src == '.' )
{
return R50_DOT; /* 28 */
}
if ( src == '%' )
{
return R50_PERCENT; /* 29 */
}
if ( src >= '0' && src <= '9' )
{
return src - '0' + 30; /* 30-39 */
}
tmp = toupper(src);
if ( tmp >= 'A' && tmp <= 'Z' )
{
return tmp - 'A' + 1; /* 1-26 */
}
}
return 0; /* 0 */
}
/**
* Convert 3 rad50 chars to ascii.
* @param ans - Pointer to 4 byte array into which ascii is deposited.
* @param src - rad50 input
* @return nothing
*/
void fromRad50(char ans[4], unsigned short src)
{
int ii;
ii = src / (050 * 050); /* modulo 40*40 goes into byte 0 */
ans[0] = r50[ii & 63];
src -= ii * 050 * 050;
ii = src / 050; /* modulo 40 goes into byte 1 */
ans[1] = r50[ii & 63];
src -= ii * 050;
ans[2] = r50[src & 63]; /* LSBs goes into 2 */
ans[3] = 0; /* null terminate the string */
}
/**
* Squeeze out spaces from converted string.
* @param ptr - pointer to null terminated ascii string.
* @return nothing.
* Spaces will have been squeezed out of string.
*/
void sqzSpaces(char *ptr)
{
char *dst = ptr;
for (; *ptr; ++ptr )
{
if ( *ptr != ' ' )
{
if ( dst == ptr )
++dst;
else
*dst++ = *ptr;
}
}
*dst = 0;
}
/**
* Get a Y/N/Q response to a query
* @param msg - prompt message.
* @param def - 0=default yes, 1=default no, 2=default quit
* @return 0 if yes, 1 if no, 2 if quit.
*/
int getYN(const char *prompt, int def)
{
char yn[10];
const char *defmsg;
if ( prompt )
{
fputs(prompt, stdout);
}
if ( def == YN_YES )
{
defmsg = " [Y]/N/Q: ";
}
else if ( def == YN_NO )
{
defmsg = " Y/[N]/Q: ";
}
else
{
defmsg = " Y/N/[Q]: ";
}
fputs(defmsg, stdout);
fflush(stdout);
yn[0] = 0;
fgets(yn, sizeof(yn), stdin);
if ( !yn[0] || yn[0] == '\n' )
return def;
if ( yn[0] == 'Y' || yn[0] == 'y' )
return YN_YES;
if ( yn[0] == 'Q' || yn[0] == 'q' )
return YN_QUIT;
return YN_NO;
}
char* dateStr(char outStr[INSTR_LEN], unsigned short date)
{
int yr = (date & 31), decade = (date >> 14) & 3;
switch (decade)
{
case 0:
yr += 1972;
break;
case 1:
yr += 2004;
break;
case 2:
yr += 2036;
break;
case 3:
yr += 2068;
break;
}
snprintf(outStr, INSTR_LEN, "%2d-%3s-%4d",
(date >> 5) & 31,
months[((date >> 10) & 15)],
yr);
return outStr;
}
#if 0
/** mkOFBuf - create or expand output buffer
* @param ihp - pointer to input details
* @param need - pointer to size of output needed
* @return 0 on success, 1 on error
**/
int mkOFBuf(InHandle_t *ihp, int *need)
{
if ( !ihp->outFileBuf || *need > ihp->outFileBufSize )
{
ihp->outFileBuf = (char *)realloc(ihp->outFileBuf, *need);
if ( !ihp->outFileBuf )
{
fprintf(stderr, "Error mallocing %d buffer for input file '%s'.\n",
ihp->outFileBufSize, ihp->argFN);
return 1;
}
ihp->outFileBufSize = *need;
*need = ((ihp->outFileBufSize + (ihp->outFileBufSize + 7) / 8) + BLKSIZ - 1) & -BLKSIZ;
}
return 0;
}
#endif
int cvtName(Options_t *options, const char *fileName)
{
char *cp;
const char *ccp;
int retv, cc, r50;
retv = strlen(fileName);
if ( retv > options->iHandle.argFNLen )
{
options->iHandle.argFN = (char *)malloc(retv + 1);
if ( !options->iHandle.argFN )
{
fprintf(stderr, "Unable to allocate %d bytes for filename '%s': %s\n",
retv, fileName, strerror(errno));
return 1;
}
options->iHandle.argFNLen = retv;
}
/* First trim off just the filename and convert it to uppercase. */
ccp = strrchr(fileName, '/');
if ( !ccp )
ccp = fileName;
else
++ccp;
strcpy(options->iHandle.argFN, ccp);
cp = options->iHandle.argFN;
retv = 0;
options->iHandle.iNameR50[0] = 0;
options->iHandle.iNameR50[1] = 0;
options->iHandle.iNameR50[2] = 0;
for ( retv = 0; (cc = *cp); ++cp )
{
/* If lowercase, make it upper */
if ( tolower(cc) )
cc = *cp = toupper(cc);
/* convert character to RAD50 */
r50 = char2r50(cc);
/* If it returns as r50 space, it's illegal */
if ( !r50 )
break;
/* if it's a dot, it is the filename/filetype separator. It doesn't get included */
if ( r50 == R50_DOT )
{
/* if we've already seen a dot, then the name is illegal */
if ( retv >= 7 )
break;
/* else jump to filetype conversion */
retv = 7;
continue;
}
switch (retv)
{
case 0:
options->iHandle.iNameR50[0] = r50 * 050 * 050;
++retv;
continue;
case 1:
options->iHandle.iNameR50[0] += r50 * 050;
++retv;
continue;
case 2:
options->iHandle.iNameR50[0] += r50;
++retv;
continue;
case 3:
options->iHandle.iNameR50[1] = r50 * 050 * 050;
++retv;
continue;
case 4:
options->iHandle.iNameR50[1] += r50 * 050;
++retv;
continue;
case 5:
options->iHandle.iNameR50[1] += r50;
++retv;
continue;
case 7:
options->iHandle.iNameR50[2] = r50 * 050 * 050;
++retv;
continue;
case 8:
options->iHandle.iNameR50[2] += r50 * 050;
++retv;
continue;
case 9:
options->iHandle.iNameR50[2] += r50;
++retv;
continue;
/* If there's more than 9 characters, it's illegal */
default:
break;
}
break;
}
/* If there's more stuff in the filenamoptions->iHandle.iNameR50[0],e, then it's illegal */
if ( *cp )
{
fprintf(stderr, "Filename '%s' is incompatible with RT11 name convention.\n",
fileName);
return 1;
}
if ( (options->cmdOpts & CMDOPT_DBG_NORMAL) )
{
printf("cvt_name: Converted '%s' to '%s': 0x%4X 0x%04X 0x%04X\n",
fileName, options->iHandle.argFN,
options->iHandle.iNameR50[0],
options->iHandle.iNameR50[1],
options->iHandle.iNameR50[2]);
}
return 0;
}