-
Notifications
You must be signed in to change notification settings - Fork 36
/
os2.c
615 lines (507 loc) · 12.9 KB
/
os2.c
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*-
* Copyright (c) 2015, 2017, 2020
* KO Myung-Hun <[email protected]>
* Copyright (c) 2017, 2020, 2022
* mirabilos <[email protected]>
*
* Provided that these terms and disclaimer and all copyright notices
* are retained or reproduced in an accompanying document, permission
* is granted to deal in this work without restriction, including un-
* limited rights to use, publicly perform, distribute, sell, modify,
* merge, give away, or sublicence.
*
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
* the utmost extent permitted by applicable law, neither express nor
* implied; without malicious intent or gross negligence. In no event
* may a licensor, author or contributor be held liable for indirect,
* direct, other damage, loss, or other issues arising in any way out
* of dealing in the work, even if advised of the possibility of such
* damage or existence of a defect, except proven that it results out
* of said person's immediate fault when using the work as intended.
*/
#define INCL_KBD
#define INCL_DOS
#include <os2.h>
#include <klibc/startup.h>
#include <process.h>
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/os2.c,v 1.23 2024/06/27 12:52:14 tg Exp $");
struct a_s_arg {
union {
int (*i)(const char *, int);
int (*p)(const char *, void *);
} fn;
union {
int i;
void *p;
} arg;
Wahr isint;
};
static void remove_trailing_dots(char *, size_t);
static int access_stat_ex(const char *, struct a_s_arg *);
static int test_exec_exist(const char *, void *);
static void response(int *, const char ***);
static char *make_response_file(char * const *);
static void add_temp(const char *);
static void cleanup_temps(void);
static void cleanup(void);
#define RPUT(x) do { \
if (new_argc >= new_alloc) { \
new_alloc += 20; \
if (!(new_argv = realloc(new_argv, \
new_alloc * sizeof(char *)))) \
goto exit_out_of_memory; \
} \
new_argv[new_argc++] = (x); \
} while (/* CONSTCOND */ 0)
#define KLIBC_ARG_RESPONSE_EXCLUDE \
(__KLIBC_ARG_DQUOTE | __KLIBC_ARG_WILDCARD | __KLIBC_ARG_SHELL)
/* pre-initio() */
static void
response(int *argcp, const char ***argvp)
{
int i, old_argc, new_argc, new_alloc = 0;
const char **old_argv, **new_argv;
char *line, *l, *p;
FILE *f;
old_argc = *argcp;
old_argv = *argvp;
for (i = 1; i < old_argc; ++i)
if (old_argv[i] &&
!(old_argv[i][-1] & KLIBC_ARG_RESPONSE_EXCLUDE) &&
old_argv[i][0] == '@')
break;
if (i >= old_argc)
/* do nothing */
return;
new_argv = NULL;
new_argc = 0;
for (i = 0; i < old_argc; ++i) {
if (i == 0 || !old_argv[i] ||
(old_argv[i][-1] & KLIBC_ARG_RESPONSE_EXCLUDE) ||
old_argv[i][0] != '@' ||
!(f = fopen(old_argv[i] + 1, "rt")))
RPUT(old_argv[i]);
else {
long filesize;
fseek(f, 0, SEEK_END);
filesize = ftell(f);
fseek(f, 0, SEEK_SET);
line = malloc(filesize + /* type */ 1 + /* NUL */ 1);
if (!line) {
exit_out_of_memory:
SHIKATANAI write(2,
SC("mksh: out of memory reading response file\n"));
exit(255);
}
line[0] = __KLIBC_ARG_NONZERO | __KLIBC_ARG_RESPONSE;
l = line + 1;
while (fgets(l, (filesize + 1) - (l - (line + 1)), f)) {
p = ucstrchr(l, '\n');
if (p) {
/*
* if a line ends with a backslash,
* concatenate with the next line
*/
if (p > l && p[-1] == '\\') {
char *p1;
int count = 0;
for (p1 = p - 1; p1 >= l &&
*p1 == '\\'; p1--)
count++;
if (count & 1) {
l = p + 1;
continue;
}
}
*p = 0;
}
p = strdup(line);
if (!p)
goto exit_out_of_memory;
RPUT(p + 1);
l = line + 1;
}
free(line);
if (ferror(f)) {
SHIKATANAI write(2,
SC("mksh: can't read response file\n"));
exit(255);
}
fclose(f);
}
}
RPUT(NULL);
--new_argc;
*argcp = new_argc;
*argvp = new_argv;
}
/* pre-initio() */
static void
init_extlibpath(void)
{
const char *vars[] = {
"BEGINLIBPATH",
"ENDLIBPATH",
"LIBPATHSTRICT",
NULL
};
char val[512];
int flag;
for (flag = 0; vars[flag]; flag++) {
DosQueryExtLIBPATH(val, flag + 1);
if (val[0])
setenv(vars[flag], val, 1);
}
}
/* pre-initio() */
void
os2_init(int *argcp, const char ***argvp)
{
KBDINFO ki;
response(argcp, argvp);
init_extlibpath();
if (!isatty(STDIN_FILENO))
setmode(STDIN_FILENO, O_BINARY);
if (!isatty(STDOUT_FILENO))
setmode(STDOUT_FILENO, O_BINARY);
if (!isatty(STDERR_FILENO))
setmode(STDERR_FILENO, O_BINARY);
/* ensure ECHO mode is ON so that read command echoes. */
memset(&ki, 0, sizeof(ki));
ki.cb = sizeof(ki);
ki.fsMask |= KEYBOARD_ECHO_ON;
KbdSetStatus(&ki, 0);
atexit(cleanup);
}
void
setextlibpath(const char *name, const char *val)
{
int flag;
char *p, *cp;
if (!strcmp(name, "BEGINLIBPATH"))
flag = BEGIN_LIBPATH;
else if (!strcmp(name, "ENDLIBPATH"))
flag = END_LIBPATH;
else if (!strcmp(name, "LIBPATHSTRICT"))
flag = LIBPATHSTRICT;
else
return;
/* convert slashes to backslashes */
strdupx(cp, val, ATEMP);
for (p = cp; *p; p++) {
if (*p == '/')
*p = '\\';
}
DosSetExtLIBPATH(cp, flag);
afree(cp, ATEMP);
}
/* remove trailing dots */
static void
remove_trailing_dots(char *name, size_t namelen)
{
char *p = name + namelen;
while (--p > name && *p == '.')
/* nothing */;
if (*p != '.' && *p != '/' && *p != '\\' && *p != ':')
p[1] = '\0';
}
/* alias of stat() */
extern int _std_stat(const char *, struct stat *);
/* replacement for stat() of kLIBC which fails if there are trailing dots */
int
stat(const char *name, struct stat *buffer)
{
size_t namelen = strlen(name) + 1;
char nodots[namelen];
memcpy(nodots, name, namelen);
remove_trailing_dots(nodots, namelen);
return (_std_stat(nodots, buffer));
}
/* alias of access() */
extern int _std_access(const char *, int);
/* replacement for access() of kLIBC which fails if there are trailing dots */
int
access(const char *name, int mode)
{
size_t namelen = strlen(name) + 1;
char nodots[namelen];
/*
* On OS/2 kLIBC, X_OK is set only for executable files.
* This prevents scripts from being executed.
*/
if (mode & X_OK)
mode = (mode & ~X_OK) | R_OK;
memcpy(nodots, name, namelen);
remove_trailing_dots(nodots, namelen);
return (_std_access(nodots, mode));
}
#define MAX_X_SUFFIX_LEN 4
static const char *x_suffix_list[] =
{ "", ".exe", ".ksh", ".sh", ".cmd", ".com", ".bat", NULL };
/* call fn() by appending executable extensions */
static int
access_stat_ex(const char *name, struct a_s_arg *action)
{
char *x_name;
const char **x_suffix;
int rc = -1;
size_t x_namelen = strlen(name) + MAX_X_SUFFIX_LEN + 1;
/* otherwise, try to append executable suffixes */
x_name = alloc(x_namelen, ATEMP);
for (x_suffix = x_suffix_list; rc && *x_suffix; x_suffix++) {
strlcpy(x_name, name, x_namelen);
strlcat(x_name, *x_suffix, x_namelen);
rc = action->isint ? action->fn.i(x_name, action->arg.i) :
action->fn.p(x_name, action->arg.p);
}
afree(x_name, ATEMP);
return (rc);
}
/* access()/search_access() version */
int
access_ex(int (*fn)(const char *, int), const char *name, int mode)
{
struct a_s_arg arg;
arg.fn.i = fn;
arg.arg.i = mode;
arg.isint = Ja;
return (access_stat_ex(name, &arg));
}
/* stat()/lstat() version */
int
stat_ex(int (*fn)(const char *, struct stat *),
const char *name, struct stat *buffer)
{
struct a_s_arg arg;
arg.fn.p = fn;
arg.arg.p = buffer;
arg.isint = Nee;
return (access_stat_ex(name, &arg));
}
static int
test_exec_exist(const char *name, void *arg)
{
struct stat sb;
char *real_name;
if (stat(name, &sb) < 0 || !S_ISREG(sb.st_mode))
return (-1);
/*XXX memory leak */
strdupx(real_name, name, ATEMP);
*((char **)arg) = real_name;
return (0);
}
const char *
real_exec_name(const char *name)
{
struct a_s_arg arg;
char *real_name;
arg.fn.p = &test_exec_exist;
arg.arg.p = (void *)(&real_name);
arg.isint = Nee;
return (access_stat_ex(name, &arg) ? name : real_name);
}
/* make a response file to pass a very long command line */
static char *
make_response_file(char * const *argv)
{
char rsp_name_arg[] = "@mksh-rsp-XXXXXX";
char *rsp_name = &rsp_name_arg[1];
int i;
int fd;
char *result;
if ((fd = mkstemp(rsp_name)) == -1)
return (NULL);
/* write all the arguments except a 0th program name */
for (i = 1; argv[i]; i++) {
write(fd, argv[i], strlen(argv[i]));
write(fd, "\n", 1);
}
close(fd);
add_temp(rsp_name);
strdupx(result, rsp_name_arg, ATEMP);
return (result);
}
/* alias of execve() */
extern int _std_execve(const char *, char * const *, char * const *);
/* replacement for execve() of kLIBC */
int
execve(const char *name, char * const *argv, char * const *envp)
{
const char *exec_name;
FILE *fp;
char sign[2];
int pid;
int status;
int fd;
int rc;
int saved_mode;
int saved_errno;
/*
* #! /bin/sh : append .exe
* extproc sh : search sh.exe in PATH
*/
exec_name = search_path(name, path, X_OK, NULL);
if (!exec_name) {
errno = ENOENT;
return (-1);
}
/*-
* kLIBC execve() has problems when executing scripts.
* 1. it fails to execute a script if a directory whose name
* is same as an interpreter exists in a current directory.
* 2. it fails to execute a script not starting with sharpbang.
* 3. it fails to execute a batch file if COMSPEC is set to a shell
* incompatible with cmd.exe, such as /bin/sh.
* And ksh process scripts more well, so let ksh process scripts.
*/
errno = 0;
if (!(fp = fopen(exec_name, "rb")))
errno = ENOEXEC;
if (!errno && fread(sign, 1, sizeof(sign), fp) != sizeof(sign))
errno = ENOEXEC;
if (fp && fclose(fp))
errno = ENOEXEC;
if (!errno &&
!((sign[0] == 'M' && sign[1] == 'Z') ||
(sign[0] == 'N' && sign[1] == 'E') ||
(sign[0] == 'L' && sign[1] == 'X')))
errno = ENOEXEC;
if (errno == ENOEXEC)
return (-1);
/*
* Normal OS/2 programs expect that standard IOs, especially stdin,
* are opened in text mode at the startup. By the way, on OS/2 kLIBC
* child processes inherit a translation mode of a parent process.
* As a result, if stdin is set to binary mode in a parent process,
* stdin of child processes is opened in binary mode as well at the
* startup. In this case, some programs such as sed suffer from CR.
*/
saved_mode = setmode(STDIN_FILENO, O_TEXT);
pid = spawnve(P_NOWAIT, exec_name, argv, envp);
saved_errno = errno;
/* arguments too long? */
if (pid == -1 && saved_errno == EINVAL) {
/* retry with a response file */
char *rsp_name_arg = make_response_file(argv);
if (rsp_name_arg) {
char *rsp_argv[3] = { argv[0], rsp_name_arg, NULL };
pid = spawnve(P_NOWAIT, exec_name, rsp_argv, envp);
saved_errno = errno;
afree(rsp_name_arg, ATEMP);
}
}
/* restore translation mode of stdin */
setmode(STDIN_FILENO, saved_mode);
if (pid == -1) {
cleanup_temps();
errno = saved_errno;
return (-1);
}
/* close all opened handles */
for (fd = 0; fd < NUFILE; fd++) {
if (fcntl(fd, F_GETFD) == -1)
continue;
close(fd);
}
while ((rc = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
/* nothing */;
cleanup_temps();
/* Is this possible? And is this right? */
if (rc == -1)
return (-1);
if (WIFSIGNALED(status))
_exit(ksh_sigmask(WTERMSIG(status)));
_exit(WEXITSTATUS(status));
}
static struct temp *templist = NULL;
static void
add_temp(const char *name)
{
struct temp *tp;
size_t len;
len = strlen(name) + 1U;
tp = alloc(mbccFAMsz(struct temp, tffn, len), APERM);
memcpy(tp->tffn, name, len);
tp->next = templist;
templist = tp;
}
/* alias of unlink() */
extern int _std_unlink(const char *);
/*
* Replacement for unlink() of kLIBC not supporting to remove files used by
* another processes.
*/
int
unlink(const char *name)
{
int rc;
rc = _std_unlink(name);
if (rc == -1 && errno != ENOENT)
add_temp(name);
return (rc);
}
static void
cleanup_temps(void)
{
struct temp *tp;
struct temp **tpnext;
for (tpnext = &templist, tp = templist; tp; tp = *tpnext) {
if (_std_unlink(tp->tffn) == 0 || errno == ENOENT) {
*tpnext = tp->next;
afree(tp, APERM);
} else {
tpnext = &tp->next;
}
}
}
static void
cleanup(void)
{
cleanup_temps();
}
int
getdrvwd(char **cpp, unsigned int drvltr)
{
PBYTE cp;
ULONG sz;
APIRET rc;
ULONG drvno;
if (DosQuerySysInfo(QSV_MAX_PATH_LENGTH, QSV_MAX_PATH_LENGTH,
&sz, sizeof(sz)) != 0) {
errno = EDOOFUS;
return (-1);
}
/* allocate 'X:/' plus sz plus NUL */
cp = aresize1(*cpp, sz, 4, ATEMP);
cp[0] = ksh_toupper(drvltr);
cp[1] = ':';
cp[2] = '/';
drvno = ksh_numuc(cp[0]) + 1;
/* NUL is part of space within buffer passed */
++sz;
if ((rc = DosQueryCurrentDir(drvno, cp + 3, &sz)) == 0) {
/* success! */
*cpp = cp;
return (0);
}
afree(cp, ATEMP);
*cpp = NULL;
switch (rc) {
case 15: /* invalid drive */
errno = ENOTBLK;
break;
case 26: /* not dos disk */
errno = ENODEV;
break;
case 108: /* drive locked */
errno = EDEADLK;
break;
case 111: /* buffer overflow */
errno = ENAMETOOLONG;
break;
default:
errno = EINVAL;
}
return (-1);
}