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