[b03c714] | 1 | /* Copyright (c) 2006-2011 The BarnOwl Developers. All rights reserved. |
---|
[81001c0] | 2 | * Copyright (c) 2004 James Kretchmar. All rights reserved. |
---|
[7d4fbcd] | 3 | * |
---|
[81001c0] | 4 | * This program is free software. You can redistribute it and/or |
---|
| 5 | * modify under the terms of the Sleepycat License. See the COPYING |
---|
| 6 | * file included with the distribution for more information. |
---|
[7d4fbcd] | 7 | */ |
---|
| 8 | |
---|
[f271129] | 9 | #include "owl.h" |
---|
[7d4fbcd] | 10 | #include <stdio.h> |
---|
[f350fc3] | 11 | #include <getopt.h> |
---|
[38cf544c] | 12 | #include <sys/stat.h> |
---|
[34509d5] | 13 | #include <locale.h> |
---|
[4d9e311c] | 14 | #include <unistd.h> |
---|
[e8c6d8f] | 15 | |
---|
[2a2bb60] | 16 | #if OWL_STDERR_REDIR |
---|
[e8b95f8] | 17 | #ifdef HAVE_SYS_IOCTL_H |
---|
[2a2bb60] | 18 | #include <sys/ioctl.h> |
---|
[e8b95f8] | 19 | #endif |
---|
| 20 | #ifdef HAVE_SYS_FILIO_H |
---|
| 21 | #include <sys/filio.h> |
---|
| 22 | #endif |
---|
[2a2bb60] | 23 | int stderr_replace(void); |
---|
| 24 | #endif |
---|
| 25 | |
---|
[b2b0773] | 26 | owl_global g; |
---|
| 27 | |
---|
[4e4847c] | 28 | typedef struct _owl_options { |
---|
[799f36c] | 29 | bool load_initial_subs; |
---|
| 30 | char *configfile; |
---|
| 31 | char *tty; |
---|
| 32 | char *confdir; |
---|
| 33 | bool debug; |
---|
| 34 | } owl_options; |
---|
| 35 | |
---|
[499224d] | 36 | void usage(FILE *file) |
---|
[4e4847c] | 37 | { |
---|
[499224d] | 38 | fprintf(file, "Barnowl version %s\n", OWL_VERSION_STRING); |
---|
| 39 | fprintf(file, "Usage: barnowl [-n] [-d] [-D] [-v] [-h] [-c <configfile>] [-s <confdir>] [-t <ttyname>]\n"); |
---|
| 40 | fprintf(file, " -n,--no-subs don't load zephyr subscriptions\n"); |
---|
| 41 | fprintf(file, " -d,--debug enable debugging\n"); |
---|
| 42 | fprintf(file, " -v,--version print the Barnowl version number and exit\n"); |
---|
| 43 | fprintf(file, " -h,--help print this help message\n"); |
---|
| 44 | fprintf(file, " -s,--config-dir specify an alternate config dir (default ~/.owl)\n"); |
---|
| 45 | fprintf(file, " -c,--config-file specify an alternate config file (default ~/.owl/init.pl)\n"); |
---|
| 46 | fprintf(file, " -t,--tty set the tty name\n"); |
---|
[4e4847c] | 47 | } |
---|
| 48 | |
---|
[799f36c] | 49 | /* TODO: free owl_options after init is done? */ |
---|
[4e4847c] | 50 | void owl_parse_options(int argc, char *argv[], owl_options *opts) { |
---|
[f350fc3] | 51 | static const struct option long_options[] = { |
---|
| 52 | { "no-subs", 0, 0, 'n' }, |
---|
| 53 | { "config-file", 1, 0, 'c' }, |
---|
| 54 | { "config-dir", 1, 0, 's' }, |
---|
| 55 | { "tty", 1, 0, 't' }, |
---|
| 56 | { "debug", 0, 0, 'd' }, |
---|
| 57 | { "version", 0, 0, 'v' }, |
---|
| 58 | { "help", 0, 0, 'h' }, |
---|
| 59 | { NULL, 0, NULL, 0} |
---|
| 60 | }; |
---|
[799f36c] | 61 | char c; |
---|
| 62 | |
---|
[f350fc3] | 63 | while((c = getopt_long(argc, argv, "nc:t:s:dDvh", |
---|
| 64 | long_options, NULL)) != -1) { |
---|
[799f36c] | 65 | switch(c) { |
---|
| 66 | case 'n': |
---|
| 67 | opts->load_initial_subs = 0; |
---|
| 68 | break; |
---|
| 69 | case 'c': |
---|
[d4927a7] | 70 | opts->configfile = g_strdup(optarg); |
---|
[799f36c] | 71 | break; |
---|
| 72 | case 's': |
---|
[d4927a7] | 73 | opts->confdir = g_strdup(optarg); |
---|
[799f36c] | 74 | break; |
---|
| 75 | case 't': |
---|
[d4927a7] | 76 | opts->tty = g_strdup(optarg); |
---|
[799f36c] | 77 | break; |
---|
| 78 | case 'd': |
---|
| 79 | opts->debug = 1; |
---|
| 80 | break; |
---|
| 81 | case 'v': |
---|
[b8a3e00] | 82 | printf("This is BarnOwl version %s\n", OWL_VERSION_STRING); |
---|
[799f36c] | 83 | exit(0); |
---|
| 84 | case 'h': |
---|
[499224d] | 85 | usage(stdout); |
---|
| 86 | exit(0); |
---|
[799f36c] | 87 | default: |
---|
[499224d] | 88 | usage(stderr); |
---|
[799f36c] | 89 | exit(1); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
[4e4847c] | 94 | void owl_start_color(void) { |
---|
[7967433] | 95 | start_color(); |
---|
| 96 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
| 97 | use_default_colors(); |
---|
| 98 | #endif |
---|
| 99 | |
---|
| 100 | /* define simple color pairs */ |
---|
| 101 | if (has_colors() && COLOR_PAIRS>=8) { |
---|
| 102 | int bg = COLOR_BLACK; |
---|
| 103 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
| 104 | bg = -1; |
---|
| 105 | #endif |
---|
| 106 | init_pair(OWL_COLOR_BLACK, COLOR_BLACK, bg); |
---|
| 107 | init_pair(OWL_COLOR_RED, COLOR_RED, bg); |
---|
| 108 | init_pair(OWL_COLOR_GREEN, COLOR_GREEN, bg); |
---|
| 109 | init_pair(OWL_COLOR_YELLOW, COLOR_YELLOW, bg); |
---|
| 110 | init_pair(OWL_COLOR_BLUE, COLOR_BLUE, bg); |
---|
| 111 | init_pair(OWL_COLOR_MAGENTA, COLOR_MAGENTA, bg); |
---|
| 112 | init_pair(OWL_COLOR_CYAN, COLOR_CYAN, bg); |
---|
| 113 | init_pair(OWL_COLOR_WHITE, COLOR_WHITE, bg); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
[4e4847c] | 117 | void owl_start_curses(void) { |
---|
[7967433] | 118 | struct termios tio; |
---|
| 119 | /* save initial terminal settings */ |
---|
[7488f27] | 120 | tcgetattr(STDIN_FILENO, owl_global_get_startup_tio(&g)); |
---|
[7967433] | 121 | |
---|
[7488f27] | 122 | tcgetattr(STDIN_FILENO, &tio); |
---|
[7967433] | 123 | tio.c_iflag &= ~(ISTRIP|IEXTEN); |
---|
[7488f27] | 124 | tio.c_cc[VQUIT] = fpathconf(STDIN_FILENO, _PC_VDISABLE); |
---|
| 125 | tio.c_cc[VSUSP] = fpathconf(STDIN_FILENO, _PC_VDISABLE); |
---|
[ab9bf01] | 126 | tio.c_cc[VSTART] = fpathconf(STDIN_FILENO, _PC_VDISABLE); |
---|
| 127 | tio.c_cc[VSTOP] = fpathconf(STDIN_FILENO, _PC_VDISABLE); |
---|
[7488f27] | 128 | tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio); |
---|
[7967433] | 129 | |
---|
| 130 | /* screen init */ |
---|
| 131 | initscr(); |
---|
| 132 | cbreak(); |
---|
| 133 | noecho(); |
---|
| 134 | |
---|
| 135 | owl_start_color(); |
---|
| 136 | } |
---|
| 137 | |
---|
[de6f317] | 138 | void owl_shutdown_curses(void) { |
---|
| 139 | endwin(); |
---|
| 140 | /* restore terminal settings */ |
---|
[7488f27] | 141 | tcsetattr(STDIN_FILENO, TCSAFLUSH, owl_global_get_startup_tio(&g)); |
---|
[de6f317] | 142 | } |
---|
[7967433] | 143 | |
---|
[4e4847c] | 144 | /* |
---|
| 145 | * Process a new message passed to us on the message queue from some |
---|
| 146 | * protocol. This includes adding it to the message list, updating the |
---|
| 147 | * view and scrolling if appropriate, logging it, and so on. |
---|
| 148 | * |
---|
| 149 | * Either a pointer is kept to the message internally, or it is freed |
---|
| 150 | * if unneeded. The caller no longer ``owns'' the message's memory. |
---|
| 151 | * |
---|
| 152 | * Returns 1 if the message was added to the message list, and 0 if it |
---|
| 153 | * was ignored due to user settings or otherwise. |
---|
| 154 | */ |
---|
[6b4033f] | 155 | static int owl_process_message(owl_message *m) { |
---|
[4e4847c] | 156 | const owl_filter *f; |
---|
| 157 | /* if this message it on the puntlist, nuke it and continue */ |
---|
| 158 | if (owl_global_message_is_puntable(&g, m)) { |
---|
[91634ec] | 159 | owl_message_delete(m); |
---|
[4e4847c] | 160 | return 0; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /* login or logout that should be ignored? */ |
---|
| 164 | if (owl_global_is_ignorelogins(&g) |
---|
| 165 | && owl_message_is_loginout(m)) { |
---|
[91634ec] | 166 | owl_message_delete(m); |
---|
[4e4847c] | 167 | return 0; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if (!owl_global_is_displayoutgoing(&g) |
---|
| 171 | && owl_message_is_direction_out(m)) { |
---|
[91634ec] | 172 | owl_message_delete(m); |
---|
[4e4847c] | 173 | return 0; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /* add it to the global list */ |
---|
| 177 | owl_messagelist_append_element(owl_global_get_msglist(&g), m); |
---|
| 178 | /* add it to any necessary views; right now there's only the current view */ |
---|
| 179 | owl_view_consider_message(owl_global_get_current_view(&g), m); |
---|
| 180 | |
---|
| 181 | if(owl_message_is_direction_in(m)) { |
---|
| 182 | /* let perl know about it*/ |
---|
| 183 | owl_perlconfig_getmsg(m, NULL); |
---|
| 184 | |
---|
| 185 | /* do we need to autoreply? */ |
---|
| 186 | if (owl_global_is_zaway(&g) && !owl_message_get_attribute_value(m, "isauto")) { |
---|
| 187 | if (owl_message_is_type_zephyr(m)) { |
---|
| 188 | owl_zephyr_zaway(m); |
---|
| 189 | } else if (owl_message_is_type_aim(m)) { |
---|
| 190 | if (owl_message_is_private(m)) { |
---|
| 191 | owl_function_send_aimawymsg(owl_message_get_sender(m), owl_global_get_zaway_msg(&g)); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /* ring the bell if it's a personal */ |
---|
| 197 | if (!strcmp(owl_global_get_personalbell(&g), "on")) { |
---|
| 198 | if (!owl_message_is_loginout(m) && |
---|
| 199 | !owl_message_is_mail(m) && |
---|
| 200 | owl_message_is_personal(m)) { |
---|
| 201 | owl_function_beep(); |
---|
| 202 | } |
---|
| 203 | } else if (!strcmp(owl_global_get_personalbell(&g), "off")) { |
---|
| 204 | /* do nothing */ |
---|
| 205 | } else { |
---|
| 206 | f=owl_global_get_filter(&g, owl_global_get_personalbell(&g)); |
---|
| 207 | if (f && owl_filter_message_match(f, m)) { |
---|
| 208 | owl_function_beep(); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /* if it matches the alert filter, do the alert action */ |
---|
| 213 | f=owl_global_get_filter(&g, owl_global_get_alert_filter(&g)); |
---|
| 214 | if (f && owl_filter_message_match(f, m)) { |
---|
[c809f5e] | 215 | owl_function_command_norv(owl_global_get_alert_action(&g)); |
---|
[4e4847c] | 216 | } |
---|
| 217 | |
---|
| 218 | /* if it's a zephyr login or logout, update the zbuddylist */ |
---|
| 219 | if (owl_message_is_type_zephyr(m) && owl_message_is_loginout(m)) { |
---|
| 220 | if (owl_message_is_login(m)) { |
---|
| 221 | owl_zbuddylist_adduser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
| 222 | } else if (owl_message_is_logout(m)) { |
---|
| 223 | owl_zbuddylist_deluser(owl_global_get_zephyr_buddylist(&g), owl_message_get_sender(m)); |
---|
| 224 | } else { |
---|
| 225 | owl_function_error("Internal error: received login notice that is neither login nor logout"); |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /* let perl know about it */ |
---|
| 231 | owl_perlconfig_newmsg(m, NULL); |
---|
| 232 | /* log the message if we need to */ |
---|
| 233 | owl_log_message(m); |
---|
[044f19f] | 234 | /* redraw the sepbar; TODO: don't violate layering */ |
---|
| 235 | owl_global_sepbar_dirty(&g); |
---|
[4e4847c] | 236 | |
---|
| 237 | return 1; |
---|
| 238 | } |
---|
| 239 | |
---|
[6b4033f] | 240 | static gboolean owl_process_messages_prepare(GSource *source, int *timeout) { |
---|
| 241 | *timeout = -1; |
---|
| 242 | return owl_global_messagequeue_pending(&g); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | static gboolean owl_process_messages_check(GSource *source) { |
---|
| 246 | return owl_global_messagequeue_pending(&g); |
---|
| 247 | } |
---|
| 248 | |
---|
[26255f0] | 249 | /* |
---|
| 250 | * Process any new messages we have waiting in the message queue. |
---|
| 251 | */ |
---|
[6b4033f] | 252 | static gboolean owl_process_messages_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) { |
---|
[26255f0] | 253 | int newmsgs=0; |
---|
[6c81223] | 254 | int followlast = owl_global_should_followlast(&g); |
---|
[26255f0] | 255 | owl_message *m; |
---|
| 256 | |
---|
| 257 | /* Grab incoming messages. */ |
---|
| 258 | while (owl_global_messagequeue_pending(&g)) { |
---|
| 259 | m = owl_global_messagequeue_popmsg(&g); |
---|
| 260 | if (owl_process_message(m)) |
---|
| 261 | newmsgs = 1; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | if (newmsgs) { |
---|
| 265 | /* follow the last message if we're supposed to */ |
---|
[6c81223] | 266 | if (followlast) |
---|
[3b17b57] | 267 | owl_function_lastmsg(); |
---|
[26255f0] | 268 | |
---|
| 269 | /* do the newmsgproc thing */ |
---|
| 270 | owl_function_do_newmsgproc(); |
---|
| 271 | |
---|
| 272 | /* redisplay if necessary */ |
---|
| 273 | /* this should be optimized to not run if the new messages won't be displayed */ |
---|
| 274 | owl_mainwin_redisplay(owl_global_get_mainwin(&g)); |
---|
| 275 | } |
---|
[6b4033f] | 276 | return TRUE; |
---|
[26255f0] | 277 | } |
---|
| 278 | |
---|
[6b4033f] | 279 | static GSourceFuncs owl_process_messages_funcs = { |
---|
| 280 | owl_process_messages_prepare, |
---|
| 281 | owl_process_messages_check, |
---|
| 282 | owl_process_messages_dispatch, |
---|
| 283 | NULL |
---|
| 284 | }; |
---|
| 285 | |
---|
[117b2ba] | 286 | void owl_process_input_char(owl_input j) |
---|
| 287 | { |
---|
| 288 | int ret; |
---|
| 289 | |
---|
| 290 | owl_global_set_lastinputtime(&g, time(NULL)); |
---|
| 291 | ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j); |
---|
| 292 | if (ret!=0 && ret!=1) { |
---|
| 293 | owl_function_makemsg("Unable to handle keypress"); |
---|
| 294 | } |
---|
[26255f0] | 295 | } |
---|
| 296 | |
---|
[bbb7876] | 297 | gboolean owl_process_input(GIOChannel *source, GIOCondition condition, void *data) |
---|
[4e4847c] | 298 | { |
---|
[bbb7876] | 299 | owl_global *g = data; |
---|
[4e4847c] | 300 | owl_input j; |
---|
| 301 | |
---|
| 302 | while (1) { |
---|
[bbb7876] | 303 | j.ch = wgetch(g->input_pad); |
---|
| 304 | if (j.ch == ERR) return TRUE; |
---|
[4e4847c] | 305 | |
---|
| 306 | j.uch = '\0'; |
---|
| 307 | if (j.ch >= KEY_MIN && j.ch <= KEY_MAX) { |
---|
| 308 | /* This is a curses control character. */ |
---|
| 309 | } |
---|
| 310 | else if (j.ch > 0x7f && j.ch < 0xfe) { |
---|
| 311 | /* Pull in a full utf-8 character. */ |
---|
| 312 | int bytes, i; |
---|
| 313 | char utf8buf[7]; |
---|
| 314 | memset(utf8buf, '\0', 7); |
---|
| 315 | |
---|
| 316 | utf8buf[0] = j.ch; |
---|
| 317 | |
---|
| 318 | if ((j.ch & 0xc0) && (~j.ch & 0x20)) bytes = 2; |
---|
| 319 | else if ((j.ch & 0xe0) && (~j.ch & 0x10)) bytes = 3; |
---|
| 320 | else if ((j.ch & 0xf0) && (~j.ch & 0x08)) bytes = 4; |
---|
| 321 | else if ((j.ch & 0xf8) && (~j.ch & 0x04)) bytes = 5; |
---|
| 322 | else if ((j.ch & 0xfc) && (~j.ch & 0x02)) bytes = 6; |
---|
| 323 | else bytes = 1; |
---|
| 324 | |
---|
| 325 | for (i = 1; i < bytes; i++) { |
---|
[bbb7876] | 326 | int tmp = wgetch(g->input_pad); |
---|
[4e4847c] | 327 | /* If what we got was not a byte, or not a continuation byte */ |
---|
| 328 | if (tmp > 0xff || !(tmp & 0x80 && ~tmp & 0x40)) { |
---|
| 329 | /* ill-formed UTF-8 code unit subsequence, put back the |
---|
| 330 | char we just got. */ |
---|
| 331 | ungetch(tmp); |
---|
| 332 | j.ch = ERR; |
---|
| 333 | break; |
---|
| 334 | } |
---|
| 335 | utf8buf[i] = tmp; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | if (j.ch != ERR) { |
---|
| 339 | if (g_utf8_validate(utf8buf, -1, NULL)) { |
---|
| 340 | j.uch = g_utf8_get_char(utf8buf); |
---|
| 341 | } |
---|
| 342 | else { |
---|
| 343 | j.ch = ERR; |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | else if (j.ch <= 0x7f) { |
---|
| 348 | j.uch = j.ch; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | owl_process_input_char(j); |
---|
| 352 | } |
---|
[bbb7876] | 353 | return TRUE; |
---|
[4e4847c] | 354 | } |
---|
| 355 | |
---|
[ba12b44] | 356 | static void sig_handler_main_thread(void *data) { |
---|
[81db142] | 357 | int sig = GPOINTER_TO_INT(data); |
---|
| 358 | |
---|
[3535a6e] | 359 | owl_function_debugmsg("Got signal %d", sig); |
---|
| 360 | if (sig == SIGWINCH) { |
---|
[4e4847c] | 361 | owl_function_resize(); |
---|
[3535a6e] | 362 | } else if (sig == SIGTERM || sig == SIGHUP) { |
---|
[4e4847c] | 363 | owl_function_quit(); |
---|
[47128d9] | 364 | } else if (sig == SIGINT && owl_global_take_interrupt(&g)) { |
---|
[3535a6e] | 365 | owl_input in; |
---|
| 366 | in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR]; |
---|
| 367 | owl_process_input_char(in); |
---|
[4e4847c] | 368 | } |
---|
| 369 | } |
---|
| 370 | |
---|
[1d21d9f] | 371 | static void sig_handler(const siginfo_t *siginfo, void *data) { |
---|
[47128d9] | 372 | /* If it was an interrupt, set a flag so we can handle it earlier if |
---|
| 373 | * needbe. sig_handler_main_thread will check the flag to make sure |
---|
| 374 | * no one else took it. */ |
---|
[1d21d9f] | 375 | if (siginfo->si_signo == SIGINT) { |
---|
[47128d9] | 376 | owl_global_add_interrupt(&g); |
---|
[8402a093] | 377 | } |
---|
[81db142] | 378 | /* Send a message to the main thread. */ |
---|
[1d21d9f] | 379 | owl_select_post_task(sig_handler_main_thread, |
---|
[44976fe] | 380 | GINT_TO_POINTER(siginfo->si_signo), |
---|
| 381 | NULL, g_main_context_default()); |
---|
[8402a093] | 382 | } |
---|
| 383 | |
---|
[a7fac14] | 384 | #define OR_DIE(s, syscall) \ |
---|
[6bd485e] | 385 | G_STMT_START { \ |
---|
[a7fac14] | 386 | if ((syscall) == -1) { \ |
---|
[6bd485e] | 387 | perror((s)); \ |
---|
| 388 | exit(1); \ |
---|
| 389 | } \ |
---|
| 390 | } G_STMT_END |
---|
| 391 | |
---|
[4e4847c] | 392 | void owl_register_signal_handlers(void) { |
---|
[6bd485e] | 393 | struct sigaction sig_ignore = { .sa_handler = SIG_IGN }; |
---|
| 394 | struct sigaction sig_default = { .sa_handler = SIG_DFL }; |
---|
[3535a6e] | 395 | sigset_t sigset; |
---|
[6bd485e] | 396 | int ret, i; |
---|
[a7fac14] | 397 | const int reset_signals[] = { SIGABRT, SIGBUS, SIGCHLD, SIGFPE, SIGILL, |
---|
| 398 | SIGQUIT, SIGSEGV, }; |
---|
| 399 | /* Don't bother resetting watched ones because owl_signal_init will. */ |
---|
| 400 | const int watch_signals[] = { SIGWINCH, SIGTERM, SIGHUP, SIGINT, }; |
---|
[6bd485e] | 401 | |
---|
| 402 | /* Sanitize our signals; the mask and dispositions from our parent |
---|
| 403 | * aren't really useful. Signal list taken from equivalent code in |
---|
| 404 | * Chromium. */ |
---|
[a7fac14] | 405 | OR_DIE("sigemptyset", sigemptyset(&sigset)); |
---|
[6bd485e] | 406 | if ((ret = pthread_sigmask(SIG_SETMASK, &sigset, NULL)) != 0) { |
---|
| 407 | errno = ret; |
---|
| 408 | perror("pthread_sigmask"); |
---|
[a7fac14] | 409 | exit(1); |
---|
[6bd485e] | 410 | } |
---|
[a7fac14] | 411 | for (i = 0; i < G_N_ELEMENTS(reset_signals); i++) { |
---|
| 412 | OR_DIE("sigaction", sigaction(reset_signals[i], &sig_default, NULL)); |
---|
[6bd485e] | 413 | } |
---|
[4e4847c] | 414 | |
---|
[e2cc848] | 415 | /* Turn off SIGPIPE; we check the return value of write. */ |
---|
[a7fac14] | 416 | OR_DIE("sigaction", sigaction(SIGPIPE, &sig_ignore, NULL)); |
---|
[e2cc848] | 417 | |
---|
| 418 | /* Register some signals with the signal thread. */ |
---|
[a7fac14] | 419 | owl_signal_init(watch_signals, G_N_ELEMENTS(watch_signals), |
---|
| 420 | sig_handler, NULL); |
---|
[4e4847c] | 421 | } |
---|
| 422 | |
---|
| 423 | #if OWL_STDERR_REDIR |
---|
| 424 | |
---|
| 425 | /* Replaces stderr with a pipe so that we can read from it. |
---|
| 426 | * Returns the fd of the pipe from which stderr can be read. */ |
---|
| 427 | int stderr_replace(void) |
---|
| 428 | { |
---|
| 429 | int pipefds[2]; |
---|
| 430 | if (0 != pipe(pipefds)) { |
---|
| 431 | perror("pipe"); |
---|
[4d9e311c] | 432 | owl_function_debugmsg("stderr_replace: pipe FAILED"); |
---|
[4e4847c] | 433 | return -1; |
---|
| 434 | } |
---|
[4d9e311c] | 435 | owl_function_debugmsg("stderr_replace: pipe: %d,%d", pipefds[0], pipefds[1]); |
---|
| 436 | if (-1 == dup2(pipefds[1], STDERR_FILENO)) { |
---|
| 437 | owl_function_debugmsg("stderr_replace: dup2 FAILED (%s)", strerror(errno)); |
---|
[4e4847c] | 438 | perror("dup2"); |
---|
| 439 | return -1; |
---|
| 440 | } |
---|
| 441 | return pipefds[0]; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | /* Sends stderr (read from rfd) messages to the error console */ |
---|
[2244836] | 445 | gboolean stderr_redirect_handler(GIOChannel *source, GIOCondition condition, void *data) |
---|
[4e4847c] | 446 | { |
---|
| 447 | int navail, bread; |
---|
| 448 | char buf[4096]; |
---|
[2244836] | 449 | int rfd = g_io_channel_unix_get_fd(source); |
---|
[4e4847c] | 450 | char *err; |
---|
| 451 | |
---|
[2244836] | 452 | /* TODO: Use g_io_channel_read_line? We'd have to be careful about |
---|
| 453 | * blocking on the read. */ |
---|
| 454 | |
---|
| 455 | if (rfd<0) return TRUE; |
---|
[4e4847c] | 456 | if (-1 == ioctl(rfd, FIONREAD, &navail)) { |
---|
[2244836] | 457 | return TRUE; |
---|
[4e4847c] | 458 | } |
---|
| 459 | /*owl_function_debugmsg("stderr_redirect: navail = %d\n", navail);*/ |
---|
[2244836] | 460 | if (navail <= 0) return TRUE; |
---|
[4e4847c] | 461 | if (navail > sizeof(buf)-1) { |
---|
| 462 | navail = sizeof(buf)-1; |
---|
| 463 | } |
---|
| 464 | bread = read(rfd, buf, navail); |
---|
[6476c0e] | 465 | if (bread == -1) |
---|
[2244836] | 466 | return TRUE; |
---|
[4e4847c] | 467 | |
---|
[6476c0e] | 468 | err = g_strdup_printf("[stderr]\n%.*s", bread, buf); |
---|
[4e4847c] | 469 | |
---|
| 470 | owl_function_log_err(err); |
---|
[ddbbcffa] | 471 | g_free(err); |
---|
[2244836] | 472 | return TRUE; |
---|
[4e4847c] | 473 | } |
---|
| 474 | |
---|
| 475 | #endif /* OWL_STDERR_REDIR */ |
---|
| 476 | |
---|
[de22c3d] | 477 | int main(int argc, char **argv, char **env) |
---|
| 478 | { |
---|
[d3941a0] | 479 | int argc_copy; |
---|
| 480 | char **argv_copy; |
---|
[65b2173] | 481 | char *perlout, *perlerr; |
---|
[1fdab04] | 482 | const owl_style *s; |
---|
[e19eb97] | 483 | const char *dir; |
---|
[799f36c] | 484 | owl_options opts; |
---|
[4cc49bc] | 485 | GSource *source; |
---|
[bbb7876] | 486 | GIOChannel *channel; |
---|
[cf0cc64] | 487 | |
---|
[d3941a0] | 488 | argc_copy = argc; |
---|
| 489 | argv_copy = g_strdupv(argv); |
---|
[34509d5] | 490 | |
---|
| 491 | setlocale(LC_ALL, ""); |
---|
[799f36c] | 492 | |
---|
| 493 | memset(&opts, 0, sizeof opts); |
---|
| 494 | opts.load_initial_subs = 1; |
---|
| 495 | owl_parse_options(argc, argv, &opts); |
---|
| 496 | g.load_initial_subs = opts.load_initial_subs; |
---|
[7d4fbcd] | 497 | |
---|
[7967433] | 498 | owl_start_curses(); |
---|
[38cf544c] | 499 | |
---|
[bd3f232] | 500 | /* owl global init */ |
---|
[7d4fbcd] | 501 | owl_global_init(&g); |
---|
[799f36c] | 502 | if (opts.debug) owl_global_set_debug_on(&g); |
---|
| 503 | if (opts.confdir) owl_global_set_confdir(&g, opts.confdir); |
---|
[b20de4f] | 504 | owl_function_debugmsg("startup: first available debugging message"); |
---|
[d3941a0] | 505 | owl_global_set_startupargs(&g, argc_copy, argv_copy); |
---|
| 506 | g_strfreev(argv_copy); |
---|
[09489b89] | 507 | owl_global_set_haveaim(&g); |
---|
[2a2bb60] | 508 | |
---|
[3535a6e] | 509 | owl_register_signal_handlers(); |
---|
| 510 | |
---|
[18fdd5f9] | 511 | /* register STDIN dispatch; throw away return, we won't need it */ |
---|
[bbb7876] | 512 | channel = g_io_channel_unix_new(STDIN_FILENO); |
---|
| 513 | g_io_add_watch(channel, G_IO_IN | G_IO_HUP | G_IO_ERR, &owl_process_input, &g); |
---|
| 514 | g_io_channel_unref(channel); |
---|
[52a0f14] | 515 | owl_zephyr_initialize(); |
---|
[596c22df] | 516 | |
---|
[2a2bb60] | 517 | #if OWL_STDERR_REDIR |
---|
[a0a5179] | 518 | /* Do this only after we've started curses up... */ |
---|
[4d9e311c] | 519 | if (isatty(STDERR_FILENO)) { |
---|
| 520 | owl_function_debugmsg("startup: doing stderr redirection"); |
---|
| 521 | channel = g_io_channel_unix_new(stderr_replace()); |
---|
| 522 | g_io_add_watch(channel, G_IO_IN | G_IO_HUP | G_IO_ERR, &stderr_redirect_handler, NULL); |
---|
| 523 | g_io_channel_unref(channel); |
---|
| 524 | } |
---|
[cf0cc64] | 525 | #endif |
---|
[2a2bb60] | 526 | |
---|
[38cf544c] | 527 | /* create the owl directory, in case it does not exist */ |
---|
[a0a5179] | 528 | owl_function_debugmsg("startup: creating owl directory, if not present"); |
---|
[b363d83] | 529 | dir=owl_global_get_confdir(&g); |
---|
[38cf544c] | 530 | mkdir(dir, S_IRWXU); |
---|
[5145235] | 531 | |
---|
[ced25d1] | 532 | /* set the tty, either from the command line, or by figuring it out */ |
---|
[a0a5179] | 533 | owl_function_debugmsg("startup: setting tty name"); |
---|
[799f36c] | 534 | if (opts.tty) { |
---|
| 535 | owl_global_set_tty(&g, opts.tty); |
---|
[61e79a9] | 536 | } else { |
---|
[1915073] | 537 | char *tty = owl_util_get_default_tty(); |
---|
| 538 | owl_global_set_tty(&g, tty); |
---|
[ddbbcffa] | 539 | g_free(tty); |
---|
[61e79a9] | 540 | } |
---|
[7d4fbcd] | 541 | |
---|
[b6c067a] | 542 | /* Initialize perl */ |
---|
| 543 | owl_function_debugmsg("startup: processing config file"); |
---|
[2a17b63] | 544 | |
---|
| 545 | owl_global_pop_context(&g); |
---|
[07b59ea] | 546 | owl_global_push_context(&g, OWL_CTX_READCONFIG, NULL, NULL, NULL); |
---|
[2a17b63] | 547 | |
---|
[799f36c] | 548 | perlerr=owl_perlconfig_initperl(opts.configfile, &argc, &argv, &env); |
---|
[b6c067a] | 549 | if (perlerr) { |
---|
| 550 | endwin(); |
---|
| 551 | fprintf(stderr, "Internal perl error: %s\n", perlerr); |
---|
| 552 | fflush(stderr); |
---|
| 553 | printf("Internal perl error: %s\n", perlerr); |
---|
| 554 | fflush(stdout); |
---|
| 555 | exit(1); |
---|
| 556 | } |
---|
| 557 | |
---|
[eb6cedc] | 558 | owl_global_complete_setup(&g); |
---|
| 559 | |
---|
[04b16f8] | 560 | owl_global_setup_default_filters(&g); |
---|
[7d4fbcd] | 561 | |
---|
| 562 | /* set the current view */ |
---|
[a0a5179] | 563 | owl_function_debugmsg("startup: setting the current view"); |
---|
[7967433] | 564 | owl_view_create(owl_global_get_current_view(&g), "main", |
---|
| 565 | owl_global_get_filter(&g, "all"), |
---|
| 566 | owl_global_get_style_by_name(&g, "default")); |
---|
[7d4fbcd] | 567 | |
---|
[96f8e5b] | 568 | /* AIM init */ |
---|
[a0a5179] | 569 | owl_function_debugmsg("startup: doing AIM initialization"); |
---|
[96f8e5b] | 570 | owl_aim_init(); |
---|
| 571 | |
---|
[ced25d1] | 572 | /* execute the startup function in the configfile */ |
---|
[a0a5179] | 573 | owl_function_debugmsg("startup: executing perl startup, if applicable"); |
---|
[0337203] | 574 | perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();"); |
---|
[3b8a563] | 575 | g_free(perlout); |
---|
[212764e] | 576 | |
---|
[252a5c2] | 577 | /* welcome message */ |
---|
[a0a5179] | 578 | owl_function_debugmsg("startup: creating splash message"); |
---|
[28fa23c] | 579 | owl_function_adminmsg("", |
---|
| 580 | "-----------------------------------------------------------------------\n" |
---|
[b8a3e00] | 581 | "Welcome to BarnOwl version " OWL_VERSION_STRING ".\n" |
---|
[28fa23c] | 582 | "To see a quick introduction, type ':show quickstart'. \n" |
---|
[ca6a47e] | 583 | "Press 'h' for on-line help. \n" |
---|
[28fa23c] | 584 | " \n" |
---|
| 585 | "BarnOwl is free software. Type ':show license' for more \n" |
---|
| 586 | "information. ^ ^ \n" |
---|
| 587 | " OvO \n" |
---|
| 588 | "Please report any bugs or suggestions to bug-barnowl@mit.edu ( ) \n" |
---|
| 589 | "-----------------------------------------------------------------m-m---\n" |
---|
| 590 | ); |
---|
[252a5c2] | 591 | |
---|
[389d487] | 592 | owl_function_debugmsg("startup: setting context interactive"); |
---|
| 593 | |
---|
| 594 | owl_global_pop_context(&g); |
---|
| 595 | owl_global_push_context(&g, OWL_CTX_INTERACTIVE|OWL_CTX_RECV, NULL, "recv", NULL); |
---|
| 596 | |
---|
[687c674] | 597 | /* process the startup file */ |
---|
| 598 | owl_function_debugmsg("startup: processing startup file"); |
---|
| 599 | owl_function_source(NULL); |
---|
| 600 | |
---|
[39dc159] | 601 | owl_function_debugmsg("startup: set style for the view: %s", owl_global_get_default_style(&g)); |
---|
[72c210f] | 602 | s = owl_global_get_style_by_name(&g, owl_global_get_default_style(&g)); |
---|
| 603 | if(s) |
---|
| 604 | owl_view_set_style(owl_global_get_current_view(&g), s); |
---|
| 605 | else |
---|
| 606 | owl_function_error("No such style: %s", owl_global_get_default_style(&g)); |
---|
[a0a5179] | 607 | |
---|
[4cc49bc] | 608 | source = owl_window_redraw_source_new(); |
---|
| 609 | g_source_attach(source, NULL); |
---|
| 610 | g_source_unref(source); |
---|
| 611 | |
---|
[6b4033f] | 612 | source = g_source_new(&owl_process_messages_funcs, sizeof(GSource)); |
---|
| 613 | g_source_attach(source, NULL); |
---|
| 614 | g_source_unref(source); |
---|
[26255f0] | 615 | |
---|
[cc305b5] | 616 | owl_log_init(); |
---|
| 617 | |
---|
[a0a5179] | 618 | owl_function_debugmsg("startup: entering main loop"); |
---|
[3ecd78b] | 619 | owl_select_run_loop(); |
---|
[de6f317] | 620 | |
---|
| 621 | /* Shut down everything. */ |
---|
| 622 | owl_zephyr_shutdown(); |
---|
[08e9842] | 623 | owl_signal_shutdown(); |
---|
[de6f317] | 624 | owl_shutdown_curses(); |
---|
[cc305b5] | 625 | owl_log_shutdown(); |
---|
[3ecd78b] | 626 | return 0; |
---|
[7d4fbcd] | 627 | } |
---|