upload test: Display filename.
[elinks.git] / src / terminal / terminal.c
blobb22ecd9ca8beebaec553256ba88ab123643cc17d
1 /** Terminal interface - low-level displaying implementation.
2 * @file */
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
16 #include "elinks.h"
18 #include "bookmarks/bookmarks.h"
19 #include "config/options.h"
20 #include "main/main.h"
21 #include "main/module.h"
22 #include "main/object.h"
23 #include "main/select.h"
24 #include "osdep/osdep.h"
25 #include "osdep/signals.h"
26 #include "session/session.h"
27 #include "terminal/draw.h"
28 #include "terminal/event.h"
29 #include "terminal/hardio.h"
30 #include "terminal/kbd.h"
31 #include "terminal/screen.h"
32 #include "terminal/terminal.h"
33 #include "terminal/window.h"
34 #include "util/error.h"
35 #include "util/memory.h"
36 #include "util/string.h"
37 #include "viewer/text/textarea.h"
40 INIT_LIST_OF(struct terminal, terminals);
42 static void check_if_no_terminal(void);
44 void
45 redraw_terminal(struct terminal *term)
47 struct term_event ev;
49 set_redraw_term_event(&ev, term->width, term->height);
50 term_send_event(term, &ev);
53 void
54 redraw_terminal_cls(struct terminal *term)
56 struct term_event ev;
58 set_resize_term_event(&ev, term->width, term->height);
59 term_send_event(term, &ev);
62 void
63 cls_redraw_all_terminals(void)
65 struct terminal *term;
67 foreach (term, terminals)
68 redraw_terminal_cls(term);
71 struct terminal *
72 init_term(int fdin, int fdout)
74 unsigned char name[MAX_TERM_LEN + 9] = "terminal.";
75 struct terminal *term = mem_calloc(1, sizeof(*term));
77 if (!term) {
78 check_if_no_terminal();
79 return NULL;
82 term->screen = init_screen();
83 if (!term->screen) {
84 mem_free(term);
85 return NULL;
88 init_list(term->windows);
90 term->fdin = fdin;
91 term->fdout = fdout;
92 term->master = (term->fdout == get_output_handle());
93 term->blocked = -1;
95 get_terminal_name(name + 9);
96 term->spec = get_opt_rec(config_options, name);
97 object_lock(term->spec);
99 add_to_list(terminals, term);
101 set_handlers(fdin, (select_handler_T) in_term, NULL,
102 (select_handler_T) destroy_terminal, term);
103 return term;
106 void
107 redraw_all_terminals(void)
109 struct terminal *term;
111 foreach (term, terminals)
112 redraw_screen(term);
115 void
116 destroy_terminal(struct terminal *term)
118 #ifdef CONFIG_BOOKMARKS
119 bookmark_auto_save_tabs(term);
120 #endif
122 free_textarea_data(term);
124 /* delete_window doesn't update term->current_tab, but it
125 calls redraw_terminal, which requires term->current_tab
126 to be valid if there are any tabs left. So set a value
127 that will be valid for that long. */
128 term->current_tab = 0;
130 while (!list_empty(term->windows))
131 delete_window(term->windows.next);
133 /* mem_free_if(term->cwd); */
134 mem_free_if(term->title);
135 if (term->screen) done_screen(term->screen);
137 clear_handlers(term->fdin);
138 mem_free_if(term->interlink);
140 if (term->blocked != -1) {
141 close(term->blocked);
142 clear_handlers(term->blocked);
145 del_from_list(term);
146 close(term->fdin);
148 if (term->fdout != 1) {
149 if (term->fdout != term->fdin) close(term->fdout);
150 } else {
151 unhandle_terminal_signals(term);
152 free_all_itrms();
153 #ifndef NO_FORK_ON_EXIT
154 if (!list_empty(terminals)) {
155 if (fork()) exit(0);
157 #endif
160 object_unlock(term->spec);
161 mem_free(term);
162 check_if_no_terminal();
165 void
166 destroy_all_terminals(void)
168 while (!list_empty(terminals))
169 destroy_terminal(terminals.next);
172 static void
173 check_if_no_terminal(void)
175 program.terminate = list_empty(terminals)
176 && !get_opt_bool("ui.sessions.keep_session_active", NULL);
179 void
180 exec_thread(unsigned char *path, int p)
182 int plen = strlen(path + 1) + 2;
184 #if defined(HAVE_SETPGID) && !defined(CONFIG_OS_BEOS) && !defined(HAVE_BEGINTHREAD)
185 if (path[0] == TERM_EXEC_NEWWIN) setpgid(0, 0);
186 #endif
187 exe(path + 1);
188 if (path[plen]) unlink(path + plen);
191 void
192 close_handle(void *h)
194 close((long) h);
195 clear_handlers((long) h);
198 static void
199 unblock_terminal(struct terminal *term)
201 close_handle((void *) (long) term->blocked);
202 term->blocked = -1;
203 set_handlers(term->fdin, (select_handler_T) in_term, NULL,
204 (select_handler_T) destroy_terminal, term);
205 unblock_itrm();
206 redraw_terminal_cls(term);
207 if (term->textarea_data) /* XXX */
208 textarea_edit(1, term, NULL, NULL, NULL);
212 static void
213 exec_on_master_terminal(struct terminal *term,
214 unsigned char *path, int plen,
215 unsigned char *delete, int dlen,
216 enum term_exec fg)
218 int blockh;
219 int param_size = plen + dlen + 2 /* 2 null char */ + 1 /* fg */;
220 unsigned char *param = fmem_alloc(param_size);
222 if (!param) return;
224 param[0] = fg;
225 memcpy(param + 1, path, plen + 1);
226 memcpy(param + 1 + plen + 1, delete, dlen + 1);
228 if (fg == TERM_EXEC_FG) block_itrm();
230 blockh = start_thread((void (*)(void *, int)) exec_thread,
231 param, param_size);
232 fmem_free(param);
233 if (blockh == -1) {
234 if (fg == TERM_EXEC_FG) unblock_itrm();
235 return;
238 if (fg == TERM_EXEC_FG) {
239 term->blocked = blockh;
240 set_handlers(blockh,
241 (select_handler_T) unblock_terminal,
242 NULL,
243 (select_handler_T) unblock_terminal,
244 term);
245 set_handlers(term->fdin, NULL, NULL,
246 (select_handler_T) destroy_terminal,
247 term);
249 } else {
250 set_handlers(blockh, close_handle, NULL,
251 close_handle, (void *) (long) blockh);
255 static void
256 exec_on_slave_terminal( struct terminal *term,
257 unsigned char *path, int plen,
258 unsigned char *delete, int dlen,
259 enum term_exec fg)
261 int data_size = plen + dlen + 1 /* 0 */ + 1 /* fg */ + 2 /* 2 null char */;
262 unsigned char *data = fmem_alloc(data_size);
264 if (!data) return;
266 data[0] = 0;
267 data[1] = fg;
268 memcpy(data + 2, path, plen + 1);
269 memcpy(data + 2 + plen + 1, delete, dlen + 1);
270 hard_write(term->fdout, data, data_size);
271 fmem_free(data);
274 void
275 exec_on_terminal(struct terminal *term, unsigned char *path,
276 unsigned char *delete, enum term_exec fg)
278 if (path) {
279 if (!*path) return;
280 } else {
281 path = "";
284 #ifdef NO_FG_EXEC
285 fg = TERM_EXEC_BG;
286 #endif
288 if (term->master) {
289 if (!*path) {
290 dispatch_special(delete);
291 return;
294 /* TODO: Should this be changed to allow TERM_EXEC_NEWWIN
295 * in a blocked terminal? There is similar code in
296 * in_sock(). --KON, 2007 */
297 if (fg != TERM_EXEC_BG && is_blocked()) {
298 unlink(delete);
299 return;
302 exec_on_master_terminal(term,
303 path, strlen(path),
304 delete, strlen(delete),
305 fg);
306 } else {
307 exec_on_slave_terminal( term,
308 path, strlen(path),
309 delete, strlen(delete),
310 fg);
314 void
315 exec_shell(struct terminal *term)
317 unsigned char *sh;
319 if (!can_open_os_shell(term->environment)) return;
321 sh = get_shell();
322 if (sh && *sh)
323 exec_on_terminal(term, sh, "", TERM_EXEC_FG);
327 void
328 do_terminal_function(struct terminal *term, unsigned char code,
329 unsigned char *data)
331 int data_len = strlen(data);
332 unsigned char *x_data = fmem_alloc(data_len + 1 /* code */ + 1 /* null char */);
334 if (!x_data) return;
335 x_data[0] = code;
336 memcpy(x_data + 1, data, data_len + 1);
337 exec_on_terminal(term, NULL, x_data, TERM_EXEC_BG);
338 fmem_free(x_data);
341 void
342 set_terminal_title(struct terminal *term, unsigned char *title)
344 if (term->title && !strcmp(title, term->title)) return;
345 mem_free_set(&term->title, stracpy(title));
346 do_terminal_function(term, TERM_FN_TITLE, title);
349 static int terminal_pipe[2];
352 check_terminal_pipes(void)
354 return c_pipe(terminal_pipe);
357 void
358 close_terminal_pipes(void)
360 close(terminal_pipe[0]);
361 close(terminal_pipe[1]);
364 struct terminal *
365 attach_terminal(int in, int out, int ctl, void *info, int len)
367 struct terminal *term;
369 if (set_nonblocking_fd(terminal_pipe[0]) < 0) return NULL;
370 if (set_nonblocking_fd(terminal_pipe[1]) < 0) return NULL;
371 handle_trm(in, out, out, terminal_pipe[1], ctl, info, len, 0);
373 term = init_term(terminal_pipe[0], out);
374 if (!term) {
375 close_terminal_pipes();
376 return NULL;
379 return term;
382 static struct module *terminal_submodules[] = {
383 &terminal_screen_module,
384 NULL
387 struct module terminal_module = struct_module(
388 /* Because this module is listed in main_modules rather than
389 * in builtin_modules, its name does not appear in the user
390 * interface and so need not be translatable. */
391 /* name: */ "Terminal",
392 /* options: */ NULL,
393 /* hooks: */ NULL,
394 /* submodules: */ terminal_submodules,
395 /* data: */ NULL,
396 /* init: */ NULL,
397 /* done: */ NULL