File: terminal.c

package info (click to toggle)
links 0.84-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,184 kB
  • ctags: 2,048
  • sloc: ansic: 16,116; sh: 395; makefile: 49; awk: 33
file content (631 lines) | stat: -rw-r--r-- 17,139 bytes parent folder | download
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#include "links.h"

int hard_write(int fd, unsigned char *p, int l)
{
	int w = 1;
	int t = 0;
	while (l > 0 && w) {
		if ((w = write(fd, p, l)) < 0) return -1;
		t += w;
		p += w;
		l -= w;
	}
	return t;
}

struct list_head terminals = {&terminals, &terminals};

void alloc_term_screen(struct terminal *term, int x, int y)
{
	int *s, *t;
	if ((s = mem_realloc(term->screen, x * y * sizeof(int)))) {
		if ((t = mem_realloc(term->last_screen, x * y * sizeof(int)))) {
			memset(t, -1, x * y * sizeof(int));
			term->x = x;
			term->y = y;
			term->last_screen = t;
			memset(s, 0, x * y * sizeof(int));
			term->screen = s;
			term->dirty = 1;
		}
	}
}

void in_term(struct terminal *);
void destroy_terminal(struct terminal *);
void check_if_no_terminal();

void clear_terminal(struct terminal *term)
{
	fill_area(term, 0, 0, term->x, term->y, ' ');
	set_cursor(term, 0, 0);
}

void redraw_terminal_ev(struct terminal *term, int e)
{
	struct window *win;
	struct event ev = {0, 0, 0, 0};
	ev.ev = e;
	ev.x = term->x;
	ev.y = term->y;
	clear_terminal(term);
	term->redrawing = 2;
	foreachback(win, term->windows) win->handler(win, &ev, 0);
	term->redrawing = 0;
}

void redraw_terminal(struct terminal *term)
{
	redraw_terminal_ev(term, EV_REDRAW);
}

void redraw_terminal_all(struct terminal *term)
{
	redraw_terminal_ev(term, EV_RESIZE);
}

void erase_screen(struct terminal *term)
{
	if (term->blocked == -1) hard_write(term->fdout, "\033[2J\033[1;1H", 10);
}

void redraw_terminal_cls(struct terminal *term)
{
	erase_screen(term);
	alloc_term_screen(term, term->x, term->y);
	redraw_terminal_all(term);
}

void redraw_from_window(struct window *win)
{
	struct terminal *term = win->term;
	struct window *end = (void *)&term->windows;
	struct event ev = {EV_REDRAW, 0, 0, 0};
	ev.x = term->x;
	ev.y = term->y;
	if (term->redrawing) return;
	win->term->redrawing = 1;
	for (win = win->prev; win != end; win = win->prev) {
		win->handler(win, &ev, 0);
	}
	term->redrawing = 0;
}

void redraw_below_window(struct window *win)
{
	int tr;
	struct terminal *term = win->term;
	struct window *end = win;
	struct event ev = {EV_REDRAW, 0, 0, 0};
	ev.x = term->x;
	ev.y = term->y;
	if (term->redrawing >= 2) return;
	tr = term->redrawing;
	win->term->redrawing = 2;
	for (win = term->windows.prev; win != end; win = win->prev) {
		win->handler(win, &ev, 0);
	}
	term->redrawing = tr;
}

void add_window_at_pos(struct terminal *term, void (*handler)(struct window *, struct event *, int), void *data, struct window *at)
{
	struct event ev = {EV_INIT, 0, 0, 0};
	struct window *win;
	ev.x = term->x;
	ev.y = term->y;
	if (!(win = mem_alloc(sizeof (struct window)))) {
		mem_free(data);
		return;
	}
	win->handler = handler;
	win->data = data;
	win->term = term;
	win->xp = win->yp = 0;
	add_at_pos(at, win);
	win->handler(win, &ev, 0);
}

void add_window(struct terminal *term, void (*handler)(struct window *, struct event *, int), void *data)
{
	add_window_at_pos(term, handler, data, (struct window *)&term->windows);
}

void delete_window(struct window *win)
{
	struct event ev = {EV_ABORT, 0, 0, 0};
	win->handler(win, &ev, 1);
	del_from_list(win);
	if (win->data) mem_free(win->data);
	redraw_terminal(win->term);
	mem_free(win);
}

void delete_window_ev(struct window *win, struct event *ev)
{
	struct window *w = win->next;
	delete_window(win);
	if (ev && w->next != w) w->handler(w, ev, 1);
}

void set_window_ptr(struct window *win, int x, int y)
{
	win->xp = x;
	win->yp = y;
}

void get_parent_ptr(struct window *win, int *x, int *y)
{
	if ((void *)win->next != &win->term->windows) {
		*x = win->next->xp;
		*y = win->next->yp;
	} else {
		*x = *y = 0;
	}
}

struct window *get_root_window(struct terminal *term)
{
	if (list_empty(term->windows)) {
		internal("terminal has no windows");
		return NULL;
	}
	return (struct window *)term->windows.prev;
}

struct ewd {
	void (*fn)(void *);
	void *data;
	int b;
};

void empty_window_handler(struct window *win, struct event *ev, int fwd)
{
	struct window *n;
	struct ewd *ewd = win->data;
	int x, y;
	void (*fn)(void *) = ewd->fn;
	void *data = ewd->data;
	if (ewd->b) return;
	switch (ev->ev) {
		case EV_INIT:
		case EV_RESIZE:
		case EV_REDRAW:
			get_parent_ptr(win, &x, &y);
			set_window_ptr(win, x, y);
			return;
		case EV_ABORT:
			fn(data);
			return;
	}
	ewd->b = 1;
	n = win->next;
	delete_window(win);
	fn(data);
	if (n->next != n) n->handler(n, ev, fwd);
}

void add_empty_window(struct terminal *term, void (*fn)(void *), void *data)
{
	struct ewd *ewd;
	if (!(ewd = mem_alloc(sizeof(struct ewd)))) return;
	ewd->fn = fn;
	ewd->data = data;
	ewd->b = 0;
	add_window(term, empty_window_handler, ewd);
}

struct terminal *init_term(int fdin, int fdout, void (*root_window)(struct window *, struct event *, int))
{
	struct terminal *term;
	struct window *win;
	if (!(term = mem_alloc(sizeof (struct terminal)))) {
		check_if_no_terminal();
		return NULL;
	}
	memset(term, 0, sizeof(struct terminal));
	term->fdin = fdin;
	term->fdout = fdout;
	/*term->x = 0;
	term->y = 0;
	term->cx = 0;
	term->cy = 0;*/
	term->lcx = -1;
	term->lcy = -1;
	term->dirty = 1;
	term->redrawing = 0;
	term->blocked = -1;
	term->screen = DUMMY;
	term->last_screen = DUMMY;
	term->term[0] = 0;
	term->input_queue = DUMMY;
	term->qlen = 0;
	init_list(term->windows);
	if (!(win = mem_alloc(sizeof (struct window)))) {
		mem_free(term);
		check_if_no_terminal();
		return NULL;
	}
	win->handler = root_window;
	win->data = NULL;
	win->term = term;
	add_to_list(term->windows, win);
	/*alloc_term_screen(term, 80, 25);*/
	add_to_list(terminals, term);
	set_handlers(fdin, (void (*)(void *))in_term, NULL, (void (*)(void *))destroy_terminal, term);
	return term;
}

void in_term(struct terminal *term)
{
	struct event *ev;
	int r;
	unsigned char *iq;
	if (!(iq = mem_realloc(term->input_queue, term->qlen + ALLOC_GR))) {
		destroy_terminal(term);
		return;
	}
	term->input_queue = iq;
	if ((r = read(term->fdin, iq + term->qlen, ALLOC_GR)) == -1) {
		error("ERROR: error on terminal: could not read event");
		destroy_terminal(term);
		return;
	}
	term->qlen += r;
	test_queue:
	if (term->qlen < sizeof(struct event)) return;
	ev = (struct event *)iq;
	r = sizeof(struct event);
	if (ev->ev != EV_INIT && ev->ev != EV_RESIZE && ev->ev != EV_REDRAW && ev->ev != EV_KBD && ev->ev != EV_MOUSE && ev->ev != EV_ABORT) {
		error("ERROR: error on terminal: bad event %d", ev->ev);
		goto mm;
	}
	if (ev->ev == EV_INIT) {
		int init_len;
		if (term->qlen < sizeof(struct event) + MAX_TERM_LEN + 2 * sizeof(int)) return;
		init_len = *(int *)(iq + sizeof(struct event) + MAX_TERM_LEN + sizeof(int));
		if (term->qlen < sizeof(struct event) + MAX_TERM_LEN + 2 * sizeof(int) + init_len) return;
		memcpy(term->term, iq + sizeof(struct event), MAX_TERM_LEN);
		term->term[MAX_TERM_LEN - 1] = 0;
		term->xwin = *(int *)(iq + sizeof(struct event) + MAX_TERM_LEN);
		ev->b = (long)(iq + sizeof(struct event) + MAX_TERM_LEN + sizeof(int));
		r = sizeof(struct event) + MAX_TERM_LEN + 2 * sizeof(int) + init_len;
	}
	if (ev->ev == EV_REDRAW || ev->ev == EV_RESIZE || ev->ev == EV_INIT) {
		struct window *win;
		alloc_term_screen(term, ev->x, ev->y);
		clear_terminal(term);
		erase_screen(term);
		term->redrawing = 1;
		foreachback(win, term->windows) win->handler(win, ev, 0);
		term->redrawing = 0;
	}
	if (ev->ev == EV_KBD || ev->ev == EV_MOUSE) ((struct window *)&term->windows)->next->handler(term->windows.next, ev, 0);
	if (ev->ev == EV_ABORT) destroy_terminal(term);
	/*redraw_screen(term);*/
	mm:
	if (term->qlen == r) term->qlen = 0;
	else memmove(iq, iq + r, term->qlen -= r);
	goto test_queue;
}

inline int getcompcode(int c)
{
	return (c<<1 | (c&4)>>2) & 7;
}

unsigned char frame_dumb[48] =	"   ||||++||++++++--|-+||++--|-+----++++++++     ";
unsigned char frame_vt100[48] =	"aaaxuuukkuxkjjjkmvwtqnttmlvwtqnvvwwmmllnnjla    ";
unsigned char frame_restrict[48] = {
	0, 0, 0, 0, 0, 179, 186, 186,
	205, 0, 0, 0, 0, 186, 205, 0,
	0, 0, 0, 0, 0, 0, 179, 186,
	0, 0, 0, 0, 0, 0, 0, 205,
	196, 205, 196, 186, 205, 205, 186, 186,
	179, 0, 0, 0, 0, 0, 0, 0,
};

#define PRINT_CHAR(p)									\
{											\
	unsigned ch = term->screen[p];							\
	unsigned char c = ch & 0xff;							\
	unsigned char A = ch >> 8 & 0x7f;						\
	if (s->mode == TERM_LINUX) {							\
		if (s->m11_hack) {							\
			if (ch >> 15 != mode) {						\
				if (!(mode = ch >> 15)) add_to_str(&a, &l, "\033[10m");	\
				else add_to_str(&a, &l, "\033[11m");			\
			}								\
		}									\
		if (s->restrict_852 && (ch >> 15) && c >= 176 && c < 224) {		\
			if (frame_restrict[c - 176]) c = frame_restrict[c - 176];	\
		}									\
	} else if (s->mode == TERM_VT100) {						\
		if (ch >> 15 != mode) {							\
			if (!(mode = ch >> 15)) add_to_str(&a, &l, "\x0f");		\
			else add_to_str(&a, &l, "\x0e");				\
		}									\
		if (mode && c >= 176 && c < 224) c = frame_vt100[c - 176];		\
	} else if (s->mode == TERM_DUMB && (ch >> 15) && c >= 176 && c < 224) c = frame_dumb[c - 176];\
	if (!(A & 0100) && (A >> 3) == (A & 7)) A = (A & 070) | 7 * !(A & 020);		\
	if (A != attrib) {								\
		attrib = A;								\
		add_to_str(&a, &l, "\033[0");						\
		if (s->col) {								\
			unsigned char m[4];						\
			m[0] = ';'; m[1] = '3'; m[3] = 0;				\
			m[2] = (attrib & 7) + '0';					\
			add_to_str(&a, &l, m);						\
			m[1] = '4';							\
			m[2] = (attrib >> 3 & 7) + '0';					\
			add_to_str(&a, &l, m);						\
		} else if (getcompcode(attrib & 7) < getcompcode(attrib >> 3 & 7))	\
			add_to_str(&a, &l, ";7");					\
		if (attrib & 0100) add_to_str(&a, &l, ";1");				\
		add_to_str(&a, &l, "m");						\
	}										\
	if (c >= ' ' && c != 127/* && c != 155*/) add_chr_to_str(&a, &l, c);		\
	else if (!c || c == 1) add_chr_to_str(&a, &l, ' ');				\
	else add_chr_to_str(&a, &l, '.');						\
	cx++;										\
}											\

void redraw_all_terminals()
{
	struct terminal *term;
	foreach(term, terminals) redraw_screen(term);
}

void redraw_screen(struct terminal *term)
{
	int x, y, p = 0;
	int cx = -1, cy = -1;
	unsigned char *a;
	int attrib = -1;
	int mode = -1;
	int l = 0;
	struct term_spec *s;
	if (!term->dirty || term->blocked != -1) return;
	if (!(a = init_str())) return;
	s = get_term_spec(term->term);
	for (y = 0; y < term->y; y++)
		for (x = 0; x < term->x; x++, p++) {
			if (y == term->y - 1 && x == term->x - 1) break;
			if (term->screen[p] == term->last_screen[p]) continue;
			if ((term->screen[p] & 0x3800) == (term->last_screen[p] & 0x3800) && ((term->screen[p] & 0xff) == 0 || (term->screen[p] & 0xff) == 1 || (term->screen[p] & 0xff) == ' ') && ((term->last_screen[p] & 0xff) == 0 || (term->last_screen[p] & 0xff) == 1 || (term->last_screen[p] & 0xff) == ' ')) continue;
			if (cx == x && cy == y) goto pc;/*PRINT_CHAR(p)*/
			else if (cy == y && x - cx < 10) {
				int i;
				for (i = x - cx; i >= 0; i--) PRINT_CHAR(p - i);
			} else {
				add_to_str(&a, &l, "\033[");
				add_num_to_str(&a, &l, y + 1);
				add_to_str(&a, &l, ";");
				add_num_to_str(&a, &l, x + 1);
				add_to_str(&a, &l, "H");
				cx = x; cy = y;
				pc:
				PRINT_CHAR(p);
			}
		}
	if (l) {
		add_to_str(&a, &l, "\033[0m");
		if (s->mode == TERM_LINUX && s->m11_hack) add_to_str(&a, &l, "\033[10m");
		if (s->mode == TERM_VT100) add_to_str(&a, &l, "\x0f");
	}
	if (l || term->cx != term->lcx || term->cy != term->lcy) {
		term->lcx = term->cx;
		term->lcy = term->cy;
		add_to_str(&a, &l, "\033[");
		add_num_to_str(&a, &l, term->cy + 1);
		add_to_str(&a, &l, ";");
		add_num_to_str(&a, &l, term->cx + 1);
		add_to_str(&a, &l, "H");
	}
	hard_write(term->fdout, a, l);
	mem_free(a);
	memcpy(term->last_screen, term->screen, term->x * term->y * sizeof(int));
	term->dirty = 0;
}

void destroy_terminal(struct terminal *term)
{
	while ((term->windows.next) != &term->windows) delete_window(term->windows.next);
	mem_free(term->screen);
	mem_free(term->last_screen);
	set_handlers(term->fdin, NULL, NULL, NULL, NULL);
	mem_free(term->input_queue);
	if (term->blocked != -1) {
		close(term->blocked);
		set_handlers(term->blocked, NULL, NULL, NULL, NULL);
	}
	del_from_list(term);
	mem_free(term);
	check_if_no_terminal(); /* !!! FIXME */
}

void destroy_all_terminals()
{
	struct terminal *term;
	while ((void *)(term = terminals.next) != &terminals) destroy_terminal(term);
}

void check_if_no_terminal()
{
	if (terminals.next != &terminals) return;
	terminate = 1;
}

void free_term_specs()
{
	free_list(term_specs);
}

struct list_head term_specs = {&term_specs, &term_specs};

struct term_spec dumb_term = { NULL, NULL, "", 0, 0, 0, 0, 0 };

struct term_spec *get_term_spec(unsigned char *term)
{
	struct term_spec *t;
	foreach(t, term_specs) if (!strcasecmp(t->term, term)) return t;
	return &dumb_term;
}

struct term_spec *new_term_spec(unsigned char *term)
{
	struct term_spec *t;
	foreach(t, term_specs) if (!strcasecmp(t->term, term)) return t;
	if (!(t = mem_alloc(sizeof(struct term_spec)))) return NULL;
	memcpy(t, &dumb_term, sizeof(struct term_spec));
	if (strlen(term) < MAX_TERM_LEN) strcpy(t->term, term);
	else memcpy(t->term, term, MAX_TERM_LEN - 1), t->term[MAX_TERM_LEN - 1] = 0;
	add_to_list(term_specs, t);
	return t;
}

void set_char(struct terminal *t, int x, int y, unsigned c)
{
	t->dirty = 1;
	if (x >= 0 && x < t->x && y >= 0 && y < t->y) t->screen[x + t->x * y] = c;
}

unsigned get_char(struct terminal *t, int x, int y)
{
	if (x >= t->x) x = t->x - 1;
	if (x < 0) x = 0;
	if (y >= t->y) y = t->y - 1;
	if (y < 0) y = 0;
	return t->screen[x + t->x * y];
}

void set_color(struct terminal *t, int x, int y, unsigned c)
{
	t->dirty = 1;
	if (x >= 0 && x < t->x && y >= 0 && y < t->y) t->screen[x + t->x * y] = (t->screen[x + t->x * y] & 0x80ff) | (c & ~0x80ff);
}

void set_only_char(struct terminal *t, int x, int y, unsigned c)
{
	t->dirty = 1;
	if (x >= 0 && x < t->x && y >= 0 && y < t->y) t->screen[x + t->x * y] = (t->screen[x + t->x * y] & ~0x80ff) | (c & 0x80ff);
}

void set_line(struct terminal *t, int x, int y, int l, chr *line)
{
	int i;
	t->dirty = 1;
	for (i = x >= 0 ? 0 : -x; i < (x+l <= t->x ? l : t->x-x); i++)
		t->screen[x+i + t->x * y] = line[i];
}

void set_line_color(struct terminal *t, int x, int y, int l, unsigned c)
{
	int i;
	t->dirty = 1;
	for (i = x >= 0 ? 0 : -x; i < (x+l <= t->x ? l : t->x-x); i++)
		t->screen[x+i + t->x * y] = (t->screen[x+i + t->x * y] & 0x80fff) | (c & ~0x80ff);
}

void fill_area(struct terminal *t, int x, int y, int xw, int yw, unsigned c)
{
	int i,j;
	t->dirty = 1;
	for (j = y >= 0 ? 0 : -y; j < yw && y+j < t->y; j++)
		for (i = x >= 0 ? 0 : -x; i < xw && x+i < t->x; i++) 
			t->screen[x+i + t->x*(y+j)] = c;
}

int p1[] = { 218, 191, 192, 217, 179, 196 };
int p2[] = { 201, 187, 200, 188, 186, 205 };

void draw_frame(struct terminal *t, int x, int y, int xw, int yw, unsigned c, int w)
{
	int *p = w > 1 ? p2 : p1;
	c |= ATTR_FRAME;
	set_char(t, x, y, c+p[0]);
	set_char(t, x+xw-1, y, c+p[1]);
	set_char(t, x, y+yw-1, c+p[2]);
	set_char(t, x+xw-1, y+yw-1, c+p[3]);
	fill_area(t, x, y+1, 1, yw-2, c+p[4]);
	fill_area(t, x+xw-1, y+1, 1, yw-2, c+p[4]);
	fill_area(t, x+1, y, xw-2, 1, c+p[5]);
	fill_area(t, x+1, y+yw-1, xw-2, 1, c+p[5]);
}

void print_text(struct terminal *t, int x, int y, int l, unsigned char *text, unsigned c)
{
	for (; l-- && *text; text++, x++) set_char(t, x, y, *text + c);
}

void set_cursor(struct terminal *term, int x, int y)
{
	term->dirty = 1;
	if (x >= term->x) x = term->x - 1;
	if (y >= term->y) y = term->y - 1;
	if (x < 0) x = 0;
	if (y < 0) y = 0;
	term->cx = x;
	term->cy = y;
}

void exec_thread(unsigned char *path, int p)
{
	exe(path);
	close(p);
	if (path[strlen(path) + 1]) unlink(path + strlen(path) + 1);
}

void close_handle(void *p)
{
	int h = (int)p;
	close(h);
	set_handlers(h, NULL, NULL, NULL, NULL);
}

void unblock_terminal(struct terminal *term)
{
	close_handle((void *)term->blocked);
	term->blocked = -1;
	set_handlers(term->fdin, (void (*)(void *))in_term, NULL, (void (*)(void *))destroy_terminal, term);
	unblock_itrm(term->fdin);
	redraw_terminal_cls(term);
}

void exec_on_terminal(struct terminal *term, unsigned char *path, unsigned char *delete, int fg)
{
#ifdef NO_FG_EXEC
	fg = 0;
#endif
	if (fg && term->blocked != -1) {
		unlink(delete);
		return;
	}
	if (term->fdout == 1) {
		int blockh;
		unsigned char *param;
		if (!(param = mem_alloc(strlen(path) + strlen(delete) + 2))) return;
		strcpy(param, path);
		strcpy(param + strlen(path) + 1, delete);
		if ((blockh = start_thread((void (*)(void *, int))exec_thread, param, strlen(path) + strlen(delete) + 2)) == -1) {
			mem_free(param);
			return;
		}
		mem_free(param);
		if (fg) {
			term->blocked = blockh;
			set_handlers(blockh, (void (*)(void *))unblock_terminal, NULL, (void (*)(void *))unblock_terminal, term);
			set_handlers(term->fdin, NULL, NULL, (void (*)(void *))destroy_terminal, term);
			block_itrm(term->fdin);
		} else {
			set_handlers(blockh, close_handle, NULL, close_handle, (void *)blockh);
		}
	} else {
		char x = 0;
		hard_write(term->fdout, &x, 1);
		x = fg;
		hard_write(term->fdout, &x, 1);
		hard_write(term->fdout, path, strlen(path) + 1);
		hard_write(term->fdout, path, strlen(delete) + 1);
	}
}