1 /** Terminal interface - low-level displaying implementation.
11 #include <sys/types.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);
45 redraw_terminal(struct terminal
*term
)
49 set_redraw_term_event(&ev
, term
->width
, term
->height
);
50 term_send_event(term
, &ev
);
54 redraw_terminal_cls(struct terminal
*term
)
58 set_resize_term_event(&ev
, term
->width
, term
->height
);
59 term_send_event(term
, &ev
);
63 cls_redraw_all_terminals(void)
65 struct terminal
*term
;
67 foreach (term
, terminals
)
68 redraw_terminal_cls(term
);
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
));
78 check_if_no_terminal();
82 term
->screen
= init_screen();
88 init_list(term
->windows
);
92 term
->master
= (term
->fdout
== get_output_handle());
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
);
106 /** Get the codepage of a terminal. The UTF-8 I/O option does not
109 * @todo Perhaps cache the value in struct terminal?
111 * @bug Bug 1064: If the charset has been set as "System", this should
112 * apply the locale environment variables of the slave ELinks process,
113 * not those of the master ELinks process that parsed the configuration
114 * file. That is why the parameter points to struct terminal and not
115 * merely to its option tree (term->spec).
117 * @see get_translation_table(), get_cp_mime_name() */
119 get_terminal_codepage(const struct terminal
*term
)
121 return get_opt_codepage_tree(term
->spec
, "charset");
125 redraw_all_terminals(void)
127 struct terminal
*term
;
129 foreach (term
, terminals
)
134 destroy_terminal(struct terminal
*term
)
136 #ifdef CONFIG_BOOKMARKS
137 bookmark_auto_save_tabs(term
);
139 detach_downloads_from_terminal(term
);
141 /* delete_window doesn't update term->current_tab, but it
142 calls redraw_terminal, which requires term->current_tab
143 to be valid if there are any tabs left. So set a value
144 that will be valid for that long. */
145 term
->current_tab
= 0;
147 while (!list_empty(term
->windows
))
148 delete_window(term
->windows
.next
);
150 /* mem_free_if(term->cwd); */
151 mem_free_if(term
->title
);
152 if (term
->screen
) done_screen(term
->screen
);
154 clear_handlers(term
->fdin
);
155 mem_free_if(term
->interlink
);
157 if (term
->blocked
!= -1) {
158 close(term
->blocked
);
159 clear_handlers(term
->blocked
);
165 if (term
->fdout
!= 1) {
166 if (term
->fdout
!= term
->fdin
) close(term
->fdout
);
168 unhandle_terminal_signals(term
);
170 #ifndef NO_FORK_ON_EXIT
171 if (!list_empty(terminals
)) {
177 object_unlock(term
->spec
);
179 check_if_no_terminal();
183 destroy_all_terminals(void)
185 while (!list_empty(terminals
))
186 destroy_terminal(terminals
.next
);
190 check_if_no_terminal(void)
192 program
.terminate
= list_empty(terminals
)
193 && !get_opt_bool("ui.sessions.keep_session_active");
197 exec_thread(unsigned char *path
, int p
)
199 int plen
= strlen(path
+ 1) + 2;
201 #if defined(HAVE_SETPGID) && !defined(CONFIG_OS_BEOS) && !defined(HAVE_BEGINTHREAD)
202 if (path
[0] == TERM_EXEC_NEWWIN
) setpgid(0, 0);
205 if (path
[plen
]) unlink(path
+ plen
);
209 close_handle(void *h
)
212 clear_handlers((long) h
);
216 unblock_terminal(struct terminal
*term
)
218 close_handle((void *) (long) term
->blocked
);
220 set_handlers(term
->fdin
, (select_handler_T
) in_term
, NULL
,
221 (select_handler_T
) destroy_terminal
, term
);
223 redraw_terminal_cls(term
);
224 if (textarea_editor
) /* XXX */
225 textarea_edit(1, NULL
, NULL
, NULL
, NULL
);
228 #ifndef CONFIG_FASTMEM
230 assert_terminal_ptr_not_dangling(const struct terminal
*suspect
)
232 struct terminal
*term
;
237 foreach (term
, terminals
) {
242 assertm(0, "Dangling pointer to struct terminal");
244 #endif /* !CONFIG_FASTMEM */
247 exec_on_master_terminal(struct terminal
*term
,
248 unsigned char *path
, int plen
,
249 unsigned char *delete, int dlen
,
253 int param_size
= plen
+ dlen
+ 2 /* 2 null char */ + 1 /* fg */;
254 unsigned char *param
= fmem_alloc(param_size
);
259 memcpy(param
+ 1, path
, plen
+ 1);
260 memcpy(param
+ 1 + plen
+ 1, delete, dlen
+ 1);
262 if (fg
== TERM_EXEC_FG
) block_itrm();
264 blockh
= start_thread((void (*)(void *, int)) exec_thread
,
268 if (fg
== TERM_EXEC_FG
) unblock_itrm();
272 if (fg
== TERM_EXEC_FG
) {
273 term
->blocked
= blockh
;
275 (select_handler_T
) unblock_terminal
,
277 (select_handler_T
) unblock_terminal
,
279 set_handlers(term
->fdin
, NULL
, NULL
,
280 (select_handler_T
) destroy_terminal
,
284 set_handlers(blockh
, close_handle
, NULL
,
285 close_handle
, (void *) (long) blockh
);
290 exec_on_slave_terminal( struct terminal
*term
,
291 unsigned char *path
, int plen
,
292 unsigned char *delete, int dlen
,
295 int data_size
= plen
+ dlen
+ 1 /* 0 */ + 1 /* fg */ + 2 /* 2 null char */;
296 unsigned char *data
= fmem_alloc(data_size
);
302 memcpy(data
+ 2, path
, plen
+ 1);
303 memcpy(data
+ 2 + plen
+ 1, delete, dlen
+ 1);
304 hard_write(term
->fdout
, data
, data_size
);
309 exec_on_terminal(struct terminal
*term
, unsigned char *path
,
310 unsigned char *delete, enum term_exec fg
)
324 dispatch_special(delete);
328 /* TODO: Should this be changed to allow TERM_EXEC_NEWWIN
329 * in a blocked terminal? There is similar code in
330 * in_sock(). --KON, 2007 */
331 if (fg
!= TERM_EXEC_BG
&& is_blocked()) {
336 exec_on_master_terminal(term
,
338 delete, strlen(delete),
341 exec_on_slave_terminal( term
,
343 delete, strlen(delete),
349 exec_shell(struct terminal
*term
)
353 if (!can_open_os_shell(term
->environment
)) return;
357 exec_on_terminal(term
, sh
, "", TERM_EXEC_FG
);
362 do_terminal_function(struct terminal
*term
, unsigned char code
,
365 int data_len
= strlen(data
);
366 unsigned char *x_data
= fmem_alloc(data_len
+ 1 /* code */ + 1 /* null char */);
370 memcpy(x_data
+ 1, data
, data_len
+ 1);
371 exec_on_terminal(term
, NULL
, x_data
, TERM_EXEC_BG
);
376 set_terminal_title(struct terminal
*term
, unsigned char *title
)
378 if (term
->title
&& !strcmp(title
, term
->title
)) return;
379 mem_free_set(&term
->title
, stracpy(title
));
380 do_terminal_function(term
, TERM_FN_TITLE
, title
);
383 static int terminal_pipe
[2];
386 check_terminal_pipes(void)
388 return c_pipe(terminal_pipe
);
392 close_terminal_pipes(void)
394 close(terminal_pipe
[0]);
395 close(terminal_pipe
[1]);
399 attach_terminal(int in
, int out
, int ctl
, void *info
, int len
)
401 struct terminal
*term
;
403 if (set_nonblocking_fd(terminal_pipe
[0]) < 0) return NULL
;
404 if (set_nonblocking_fd(terminal_pipe
[1]) < 0) return NULL
;
405 handle_trm(in
, out
, out
, terminal_pipe
[1], ctl
, info
, len
, 0);
407 term
= init_term(terminal_pipe
[0], out
);
409 close_terminal_pipes();
416 static struct module
*terminal_submodules
[] = {
417 &terminal_screen_module
,
421 struct module terminal_module
= struct_module(
422 /* Because this module is listed in main_modules rather than
423 * in builtin_modules, its name does not appear in the user
424 * interface and so need not be translatable. */
425 /* name: */ "Terminal",
428 /* submodules: */ terminal_submodules
,