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
|
/*
Copyright (c) 2001 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* $XFree86: xc/programs/luit/luit.c,v 1.11 2003/09/08 14:25:30 eich Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <signal.h>
#ifdef SVR4
#include <stropts.h>
#endif
#include <X11/fonts/fontenc.h>
#include "luit.h"
#include "sys.h"
#include "other.h"
#include "charset.h"
#include "iso2022.h"
static Iso2022Ptr inputState = NULL, outputState = NULL;
static char *child_argv0 = NULL;
static char *locale_name = NULL;
int ilog = -1;
int olog = -1;
int verbose = 0;
int converter = 0;
int exitOnChild = 0;
volatile int sigwinch_queued = 0;
volatile int sigchld_queued = 0;
static int convert(int, int);
static int condom(int, char**);
static void
ErrorF(char *f, ...)
{
va_list args;
va_start(args, f);
vfprintf(stderr, f, args);
va_end(args);
}
static void
FatalError(char *f, ...)
{
va_list args;
va_start(args, f);
vfprintf(stderr, f, args);
va_end(args);
exit(1);
}
static void
help(void)
{
fprintf(stderr,
"luit\n"
" [ -h ] [ -list ] [ -v ] [ -argv0 name ]\n"
" [ -gl gn ] [-gr gk] "
"[ -g0 set ] [ -g1 set ] "
"[ -g2 set ] [ -g3 set ]\n"
" [ -encoding encoding ] "
"[ +oss ] [ +ols ] [ +osl ] [ +ot ]\n"
" [ -kgl gn ] [-kgr gk] "
"[ -kg0 set ] [ -kg1 set ] "
"[ -kg2 set ] [ -kg3 set ]\n"
" [ -k7 ] [ +kss ] [ +kssgr ] [ -kls ]\n"
" [ -c ] [ -x ] [ -ilog filename ] [ -olog filename ] [ -- ]\n"
" [ program [ args ] ]\n");
}
static int
parseOptions(int argc, char **argv)
{
int i = 1;
while(i < argc) {
if(argv[i][0] != '-' && argv[i][0] != '+') {
break;
} else if(!strcmp(argv[i], "--")) {
i++;
break;
} else if(!strcmp(argv[i], "-v")) {
verbose++;
i++;
} else if(!strcmp(argv[i], "-h")) {
help();
exit(0);
} else if(!strcmp(argv[i], "-list")) {
reportCharsets();
exit(0);
} else if(!strcmp(argv[i], "+oss")) {
outputState->outputFlags &= ~OF_SS;
i++;
} else if(!strcmp(argv[i], "+ols")) {
outputState->outputFlags &= ~OF_LS;
i++;
} else if(!strcmp(argv[i], "+osl")) {
outputState->outputFlags &= ~OF_SELECT;
i++;
} else if(!strcmp(argv[i], "+ot")) {
outputState->outputFlags = OF_PASSTHRU;
i++;
} else if(!strcmp(argv[i], "-k7")) {
inputState->inputFlags &= ~IF_EIGHTBIT;
i++;
} else if(!strcmp(argv[i], "+kss")) {
inputState->inputFlags &= ~IF_SS;
i++;
} else if(!strcmp(argv[1], "+kssgr")) {
inputState->inputFlags &= ~IF_SSGR;
i++;
} else if(!strcmp(argv[i], "-kls")) {
inputState->inputFlags |= IF_LS;
i++;
} else if(!strcmp(argv[i], "-g0")) {
if(i + 1 >= argc)
FatalError("-g0 requires an argument\n");
G0(outputState) = getCharsetByName(argv[i + 1]);
i += 2;
} else if(!strcmp(argv[i], "-g1")) {
if(i + 1 >= argc)
FatalError("-g1 requires an argument\n");
G1(outputState) = getCharsetByName(argv[i + 1]);
i += 2;
} else if(!strcmp(argv[i], "-g2")) {
if(i + 1 >= argc)
FatalError("-g2 requires an argument\n");
G2(outputState) = getCharsetByName(argv[i + 1]);
i += 2;
} else if(!strcmp(argv[i], "-g3")) {
if(i + 1 >= argc)
FatalError("-g3 requires an argument\n");
G3(outputState) = getCharsetByName(argv[i + 1]);
i += 2;
} else if(!strcmp(argv[i], "-gl")) {
int j;
if(i + 1 >= argc)
FatalError("-gl requires an argument\n");
if(strlen(argv[i + 1]) != 2 ||
argv[i + 1][0] != 'g')
j = -1;
else
j = argv[i + 1][1] - '0';
if(j < 0 || j > 3)
FatalError("The argument of -gl "
"should be one of g0 through g3,\n"
"not %s\n", argv[i + 1]);
else
outputState->glp = &outputState->g[j];
i += 2;
} else if(!strcmp(argv[i], "-gr")) {
int j;
if(i + 1 >= argc)
FatalError("-gr requires an argument\n");
if(strlen(argv[i + 1]) != 2 ||
argv[i + 1][0] != 'g')
j = -1;
else
j = argv[i + 1][1] - '0';
if(j < 0 || j > 3)
FatalError("The argument of -gl "
"should be one of g0 through g3,\n"
"not %s\n", argv[i + 1]);
else
outputState->grp = &outputState->g[j];
i += 2;
} else if(!strcmp(argv[i], "-kg0")) {
if(i + 1 >= argc)
FatalError("-kg0 requires an argument\n");
G0(inputState) = getCharsetByName(argv[i + 1]);
i += 2;
} else if(!strcmp(argv[i], "-kg1")) {
if(i + 1 >= argc)
FatalError("-kg1 requires an argument\n");
G1(inputState) = getCharsetByName(argv[i + 1]);
i += 2;
} else if(!strcmp(argv[i], "-kg2")) {
if(i + 1 >= argc)
FatalError("-kg2 requires an argument\n");
G2(inputState) = getCharsetByName(argv[i + 1]);
i += 2;
} else if(!strcmp(argv[i], "-kg3")) {
if(i + 1 >= argc)
FatalError("-kg3 requires an argument\n");
G3(inputState) = getCharsetByName(argv[i + 1]);
i += 2;
} else if(!strcmp(argv[i], "-kgl")) {
int j;
if(i + 1 >= argc)
FatalError("-kgl requires an argument\n");
if(strlen(argv[i + 1]) != 2 ||
argv[i + 1][0] != 'g')
j = -1;
else
j = argv[i + 1][1] - '0';
if(j < 0 || j > 3)
FatalError("The argument of -kgl "
"should be one of g0 through g3,\n"
"not %s\n", argv[i + 1]);
else
inputState->glp = &inputState->g[j];
i += 2;
} else if(!strcmp(argv[i], "-kgr")) {
int j;
if(i + 1 >= argc)
FatalError("-kgl requires an argument\n");
if(strlen(argv[i + 1]) != 2 ||
argv[i + 1][0] != 'g')
j = -1;
else
j = argv[i + 1][1] - '0';
if(j < 0 || j > 3)
FatalError("The argument of -kgl "
"should be one of g0 through g3,\n"
"not %s\n", argv[i + 1]);
else
inputState->grp = &inputState->g[j];
i += 2;
} else if(!strcmp(argv[i], "-argv0")) {
if(i + 1 >= argc)
FatalError("-argv0 requires an argument\n");
child_argv0 = argv[i + 1];
i += 2;
} else if(!strcmp(argv[i], "-x")) {
exitOnChild = 1;
i++;
} else if(!strcmp(argv[i], "-c")) {
converter = 1;
i++;
} else if(!strcmp(argv[i], "-ilog")) {
if(i + 1 >= argc)
FatalError("-ilog requires an argument\n");
ilog = open(argv[i + 1], O_WRONLY | O_CREAT | O_TRUNC, 0777);
if(ilog < 0) {
perror("Couldn't open input log");
exit(1);
}
i += 2;
} else if(!strcmp(argv[i], "-olog")) {
if(i + 1 >= argc)
FatalError("-olog requires an argument\n");
olog = open(argv[i + 1], O_WRONLY | O_CREAT | O_TRUNC, 0777);
if(olog < 0) {
perror("Couldn't open output log");
exit(1);
}
i += 2;
} else if(!strcmp(argv[i], "-encoding")) {
int rc;
if(i + 1 >= argc)
FatalError("-encoding requires an argument\n");
rc = initIso2022(NULL, argv[i + 1], outputState);
if(rc < 0)
FatalError("Couldn't init output state\n");
i += 2;
} else {
FatalError("Unknown option %s\n", argv[i]);
}
}
return i;
}
static int
parseArgs(int argc, char **argv, char *argv0,
char **path_return, char ***argv_return)
{
char *path = NULL;
char **child_argv = NULL;
if(argc <= 0) {
char *shell;
shell = getenv("SHELL");
if(shell) {
path = malloc(strlen(shell) + 1);
if(!path)
goto bail;
strcpy(path, shell);
} else {
path = malloc(strlen("/bin/sh") + 1);
if(!path)
goto bail;
strcpy(path, "/bin/sh");
}
child_argv = malloc(2 * sizeof(char*));
if(!child_argv)
goto bail;
if(argv0)
child_argv[0] = argv0;
else
child_argv[0] = my_basename(path);
child_argv[1] = NULL;
} else {
path = malloc(strlen(argv[0]) + 1);
if(!path)
goto bail;
strcpy(path, argv[0]);
child_argv = malloc((argc + 1) * sizeof(char*));
if(!child_argv) {
goto bail;
}
if(child_argv0)
child_argv[0] = argv0;
else
child_argv[0] = my_basename(argv[0]);
memcpy(child_argv + 1, argv + 1, (argc - 1) * sizeof(char*));
child_argv[argc] = NULL;
}
*path_return = path;
*argv_return = child_argv;
return 0;
bail:
if(path)
free(path);
if(argv)
free(argv);
return -1;
}
int
main(int argc, char **argv)
{
int rc;
int i;
char *l;
l = setlocale(LC_ALL, "");
if(!l)
ErrorF("Warning: couldn't set locale.\n");
inputState = allocIso2022();
if(!inputState)
FatalError("Couldn't create input state\n");
outputState = allocIso2022();
if(!outputState)
FatalError("Couldn't create output state\n");
if(l) {
locale_name = setlocale(LC_CTYPE, NULL);
} else {
locale_name = getenv("LC_ALL");
if(locale_name == NULL) {
locale_name = getenv("LC_CTYPE");
if(locale_name == NULL) {
locale_name = getenv("LANG");
}
}
}
if(locale_name == NULL) {
ErrorF("Couldn't get locale name -- using C\n");
locale_name = "C";
}
rc = initIso2022(locale_name, NULL, outputState);
if(rc < 0)
FatalError("Couldn't init output state\n");
i = parseOptions(argc, argv);
if(i < 0)
FatalError("Couldn't parse options\n");
rc = mergeIso2022(inputState, outputState);
if(rc < 0)
FatalError("Couldn't init input state\n");
if(converter)
return convert(0, 1);
else
return condom(argc - i, argv + i);
}
static int
convert(int ifd, int ofd)
{
int rc, i;
unsigned char buf[BUFFER_SIZE];
rc = droppriv();
if(rc < 0) {
perror("Couldn't drop priviledges");
exit(1);
}
while(1) {
i = read(ifd, buf, BUFFER_SIZE);
if(i <= 0) {
if(i < 0) {
perror("Read error");
exit(1);
}
break;
}
copyOut(outputState, ofd, buf, i);
}
return 0;
}
static int
condom(int argc, char **argv)
{
int pty;
int pid;
char *line;
char *path;
char **child_argv;
int rc;
rc = parseArgs(argc, argv, child_argv0,
&path, &child_argv);
if(rc < 0)
FatalError("Couldn't parse arguments\n");
rc = allocatePty(&pty, &line);
if(rc < 0) {
perror("Couldn't allocate pty");
exit(1);
}
rc = droppriv();
if(rc < 0) {
perror("Couldn't drop priviledges");
exit(1);
}
pid = fork();
if(pid < 0) {
perror("Couldn't fork");
exit(1);
}
if(pid == 0) {
close(pty);
child(line, path, child_argv);
} else {
free(child_argv);
free(path);
free(line);
parent(pid, pty);
}
return 0;
}
void
child(char *line, char *path, char **argv)
{
int tty;
int pgrp;
close(0);
close(1);
close(2);
pgrp = setsid();
if(pgrp < 0) {
kill(getppid(), SIGABRT);
exit(1);
}
tty = openTty(line);
if(tty < 0) {
kill(getppid(), SIGABRT);
exit(1);
}
if(tty != 0)
dup2(tty, 0);
if(tty != 1)
dup2(tty, 1);
if(tty != 2)
dup2(tty, 2);
if(tty > 2)
close(tty);
execvp(path, argv);
perror("Couldn't exec");
exit(1);
}
static void
sigwinchHandler(int sig) {
sigwinch_queued = 1;
}
static void
sigchldHandler(int sig)
{
sigchld_queued = 1;
}
void
parent(int pid, int pty)
{
unsigned char buf[BUFFER_SIZE];
int i;
int val;
int rc;
if(verbose) {
reportIso2022(outputState);
}
#ifdef SIGWINCH
installHandler(SIGWINCH, sigwinchHandler);
#endif
installHandler(SIGCHLD, sigchldHandler);
rc = copyTermios(0, pty);
if(rc < 0)
FatalError("Couldn't copy terminal settings\n");
rc = setRawTermios();
if(rc < 0)
FatalError("Couldn't set terminal to raw\n");
val = fcntl(0, F_GETFL, 0);
if(val >= 0) {
fcntl(0, F_SETFL, val | O_NONBLOCK);
}
val = fcntl(pty, F_GETFL, 0);
if(val >= 0) {
fcntl(pty, F_SETFL, val | O_NONBLOCK);
}
setWindowSize(0, pty);
for(;;) {
rc = waitForInput(0, pty);
if(sigwinch_queued) {
sigwinch_queued = 0;
setWindowSize(0, pty);
}
if(sigchld_queued && exitOnChild)
break;
if(rc > 0) {
if(rc & 2) {
i = read(pty, buf, BUFFER_SIZE);
if((i == 0) || ((i < 0) && (errno != EAGAIN)))
break;
if(i > 0)
copyOut(outputState, 0, buf, i);
}
if(rc & 1) {
i = read(0, buf, BUFFER_SIZE);
if((i == 0) || ((i < 0) && (errno != EAGAIN)))
break;
if(i > 0)
copyIn(inputState, pty, buf, i);
}
}
}
restoreTermios();
}
|