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
);
71 /** Get the terminal in which message boxes should be displayed, if
72 * there is no specific reason to use some other terminal. This
73 * returns NULL if all terminals have been closed. (ELinks keeps
74 * running anyway if ui.sessions.keep_session_active is true.) */
76 get_default_terminal(void)
78 if (list_empty(terminals
))
81 return terminals
.next
;
85 init_term(int fdin
, int fdout
)
87 unsigned char name
[MAX_TERM_LEN
+ 9] = "terminal.";
88 struct terminal
*term
= mem_calloc(1, sizeof(*term
));
91 check_if_no_terminal();
95 term
->screen
= init_screen();
101 init_list(term
->windows
);
105 term
->master
= (term
->fdout
== get_output_handle());
108 get_terminal_name(name
+ 9);
109 term
->spec
= get_opt_rec(config_options
, name
);
110 object_lock(term
->spec
);
112 /* It's a new terminal, so assume the user is using it right now,
113 * and sort it to the front of the list. */
114 add_to_list(terminals
, term
);
116 set_handlers(fdin
, (select_handler_T
) in_term
, NULL
,
117 (select_handler_T
) destroy_terminal
, term
);
121 /** Get the codepage of a terminal. The UTF-8 I/O option does not
124 * @todo Perhaps cache the value in struct terminal?
126 * @bug Bug 1064: If the charset has been set as "System", this should
127 * apply the locale environment variables of the slave ELinks process,
128 * not those of the master ELinks process that parsed the configuration
129 * file. That is why the parameter points to struct terminal and not
130 * merely to its option tree (term->spec).
132 * @see get_translation_table(), get_cp_mime_name() */
134 get_terminal_codepage(const struct terminal
*term
)
136 return get_opt_codepage_tree(term
->spec
, "charset", NULL
);
140 redraw_all_terminals(void)
142 struct terminal
*term
;
144 foreach (term
, terminals
)
149 destroy_terminal(struct terminal
*term
)
151 #ifdef CONFIG_BOOKMARKS
152 bookmark_auto_save_tabs(term
);
154 detach_downloads_from_terminal(term
);
156 free_textarea_data(term
);
158 /* delete_window doesn't update term->current_tab, but it
159 calls redraw_terminal, which requires term->current_tab
160 to be valid if there are any tabs left. So set a value
161 that will be valid for that long. */
162 term
->current_tab
= 0;
164 while (!list_empty(term
->windows
))
165 delete_window(term
->windows
.next
);
167 /* mem_free_if(term->cwd); */
168 mem_free_if(term
->title
);
169 if (term
->screen
) done_screen(term
->screen
);
171 clear_handlers(term
->fdin
);
172 mem_free_if(term
->interlink
);
174 if (term
->blocked
!= -1) {
175 close(term
->blocked
);
176 clear_handlers(term
->blocked
);
182 if (term
->fdout
!= 1) {
183 if (term
->fdout
!= term
->fdin
) close(term
->fdout
);
185 unhandle_terminal_signals(term
);
187 #ifndef NO_FORK_ON_EXIT
188 if (!list_empty(terminals
)) {
194 object_unlock(term
->spec
);
196 check_if_no_terminal();
200 destroy_all_terminals(void)
202 while (!list_empty(terminals
))
203 destroy_terminal(terminals
.next
);
207 check_if_no_terminal(void)
209 program
.terminate
= list_empty(terminals
)
210 && !get_opt_bool("ui.sessions.keep_session_active", NULL
);
214 exec_thread(unsigned char *path
, int p
)
216 int plen
= strlen(path
+ 1) + 2;
218 #if defined(HAVE_SETPGID) && !defined(CONFIG_OS_BEOS) && !defined(HAVE_BEGINTHREAD)
219 if (path
[0] == TERM_EXEC_NEWWIN
) setpgid(0, 0);
222 if (path
[plen
]) unlink(path
+ plen
);
226 close_handle(void *h
)
229 clear_handlers((long) h
);
233 unblock_terminal(struct terminal
*term
)
235 close_handle((void *) (long) term
->blocked
);
237 set_handlers(term
->fdin
, (select_handler_T
) in_term
, NULL
,
238 (select_handler_T
) destroy_terminal
, term
);
240 redraw_terminal_cls(term
);
241 if (term
->textarea_data
) /* XXX */
242 textarea_edit(1, term
, NULL
, NULL
, NULL
);
245 #ifndef CONFIG_FASTMEM
247 assert_terminal_ptr_not_dangling(const struct terminal
*suspect
)
249 struct terminal
*term
;
254 foreach (term
, terminals
) {
259 assertm(0, "Dangling pointer to struct terminal");
261 #endif /* !CONFIG_FASTMEM */
264 exec_on_master_terminal(struct terminal
*term
,
265 unsigned char *path
, int plen
,
266 unsigned char *delete, int dlen
,
270 int param_size
= plen
+ dlen
+ 2 /* 2 null char */ + 1 /* fg */;
271 unsigned char *param
= fmem_alloc(param_size
);
276 memcpy(param
+ 1, path
, plen
+ 1);
277 memcpy(param
+ 1 + plen
+ 1, delete, dlen
+ 1);
279 if (fg
== TERM_EXEC_FG
) block_itrm();
281 blockh
= start_thread((void (*)(void *, int)) exec_thread
,
285 if (fg
== TERM_EXEC_FG
) unblock_itrm();
289 if (fg
== TERM_EXEC_FG
) {
290 term
->blocked
= blockh
;
292 (select_handler_T
) unblock_terminal
,
294 (select_handler_T
) unblock_terminal
,
296 set_handlers(term
->fdin
, NULL
, NULL
,
297 (select_handler_T
) destroy_terminal
,
301 set_handlers(blockh
, close_handle
, NULL
,
302 close_handle
, (void *) (long) blockh
);
307 exec_on_slave_terminal( struct terminal
*term
,
308 unsigned char *path
, int plen
,
309 unsigned char *delete, int dlen
,
312 int data_size
= plen
+ dlen
+ 1 /* 0 */ + 1 /* fg */ + 2 /* 2 null char */;
313 unsigned char *data
= fmem_alloc(data_size
);
319 memcpy(data
+ 2, path
, plen
+ 1);
320 memcpy(data
+ 2 + plen
+ 1, delete, dlen
+ 1);
321 hard_write(term
->fdout
, data
, data_size
);
326 exec_on_terminal(struct terminal
*term
, unsigned char *path
,
327 unsigned char *delete, enum term_exec fg
)
341 dispatch_special(delete);
345 /* TODO: Should this be changed to allow TERM_EXEC_NEWWIN
346 * in a blocked terminal? There is similar code in
347 * in_sock(). --KON, 2007 */
348 if (fg
!= TERM_EXEC_BG
&& is_blocked()) {
353 exec_on_master_terminal(term
,
355 delete, strlen(delete),
358 exec_on_slave_terminal( term
,
360 delete, strlen(delete),
366 exec_shell(struct terminal
*term
)
370 if (!can_open_os_shell(term
->environment
)) return;
374 exec_on_terminal(term
, sh
, "", TERM_EXEC_FG
);
379 do_terminal_function(struct terminal
*term
, unsigned char code
,
382 int data_len
= strlen(data
);
383 unsigned char *x_data
= fmem_alloc(data_len
+ 1 /* code */ + 1 /* null char */);
387 memcpy(x_data
+ 1, data
, data_len
+ 1);
388 exec_on_terminal(term
, NULL
, x_data
, TERM_EXEC_BG
);
392 /** @return negative on error; zero or positive on success. */
394 set_terminal_title(struct terminal
*term
, unsigned char *title
)
398 unsigned char *converted
= NULL
;
400 if (term
->title
&& !strcmp(title
, term
->title
)) return 0;
402 /* In which codepage was the title parameter given? */
403 from_cp
= get_terminal_codepage(term
);
405 /* In which codepage does the terminal want the title? */
406 if (get_opt_bool_tree(term
->spec
, "latin1_title", NULL
))
407 to_cp
= get_cp_index("ISO-8859-1");
408 else if (get_opt_bool_tree(term
->spec
, "utf_8_io", NULL
))
409 to_cp
= get_cp_index("UTF-8");
413 if (from_cp
!= to_cp
) {
414 struct conv_table
*convert_table
;
416 convert_table
= get_translation_table(from_cp
, to_cp
);
417 if (!convert_table
) return -1;
418 converted
= convert_string(convert_table
, title
, strlen(title
),
419 to_cp
, CSM_NONE
, NULL
, NULL
, NULL
);
420 if (!converted
) return -1;
423 mem_free_set(&term
->title
, stracpy(title
));
424 do_terminal_function(term
, TERM_FN_TITLE_CODEPAGE
,
425 get_cp_mime_name(to_cp
));
426 do_terminal_function(term
, TERM_FN_TITLE
,
427 converted
? converted
: title
);
428 mem_free_if(converted
);
432 static int terminal_pipe
[2];
435 check_terminal_pipes(void)
437 return c_pipe(terminal_pipe
);
441 close_terminal_pipes(void)
443 close(terminal_pipe
[0]);
444 close(terminal_pipe
[1]);
448 attach_terminal(int in
, int out
, int ctl
, void *info
, int len
)
450 struct terminal
*term
;
452 if (set_nonblocking_fd(terminal_pipe
[0]) < 0) return NULL
;
453 if (set_nonblocking_fd(terminal_pipe
[1]) < 0) return NULL
;
454 handle_trm(in
, out
, out
, terminal_pipe
[1], ctl
, info
, len
, 0);
456 term
= init_term(terminal_pipe
[0], out
);
458 close_terminal_pipes();
465 static struct module
*terminal_submodules
[] = {
466 &terminal_screen_module
,
470 struct module terminal_module
= struct_module(
471 /* Because this module is listed in main_modules rather than
472 * in builtin_modules, its name does not appear in the user
473 * interface and so need not be translatable. */
474 /* name: */ "Terminal",
477 /* submodules: */ terminal_submodules
,