Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • owl.c

    r1d21d9f r3b8a563  
    4141  char *confdir;
    4242  bool debug;
    43   bool rm_debug;
    4443} owl_options;
    4544
     
    5049  fprintf(stderr, "  -n,--no-subs        don't load zephyr subscriptions\n");
    5150  fprintf(stderr, "  -d,--debug          enable debugging\n");
    52   fprintf(stderr, "  -D,--remove-debug   enable debugging and delete previous debug file\n");
    5351  fprintf(stderr, "  -v,--version        print the Barnowl version number and exit\n");
    5452  fprintf(stderr, "  -h,--help           print this help message\n");
     
    6664    { "tty",             1, 0, 't' },
    6765    { "debug",           0, 0, 'd' },
    68     { "remove-debug",    0, 0, 'D' },
    6966    { "version",         0, 0, 'v' },
    7067    { "help",            0, 0, 'h' },
     
    8885      opts->tty = g_strdup(optarg);
    8986      break;
    90     case 'D':
    91       opts->rm_debug = 1;
    92       /* fallthrough */
    9387    case 'd':
    9488      opts->debug = 1;
     
    166160 * was ignored due to user settings or otherwise.
    167161 */
    168 static int owl_process_message(owl_message *m) {
     162int owl_process_message(owl_message *m) {
    169163  const owl_filter *f;
    170164  /* if this message it on the puntlist, nuke it and continue */
     
    251245}
    252246
    253 static gboolean owl_process_messages_prepare(GSource *source, int *timeout) {
    254   *timeout = -1;
    255   return owl_global_messagequeue_pending(&g);
    256 }
    257 
    258 static gboolean owl_process_messages_check(GSource *source) {
    259   return owl_global_messagequeue_pending(&g);
    260 }
    261 
    262247/*
    263248 * Process any new messages we have waiting in the message queue.
     249 * Returns 1 if any messages were added to the message list, and 0 otherwise.
    264250 */
    265 static gboolean owl_process_messages_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
     251int owl_process_messages(owl_ps_action *d, void *p)
     252{
    266253  int newmsgs=0;
    267254  int followlast = owl_global_should_followlast(&g);
     
    287274    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
    288275  }
    289   return TRUE;
    290 }
    291 
    292 static GSourceFuncs owl_process_messages_funcs = {
    293   owl_process_messages_prepare,
    294   owl_process_messages_check,
    295   owl_process_messages_dispatch,
    296   NULL
    297 };
    298 
    299 void owl_process_input_char(owl_input j)
    300 {
    301   int ret;
    302 
    303   owl_global_set_lastinputtime(&g, time(NULL));
    304   ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
    305   if (ret!=0 && ret!=1) {
    306     owl_function_makemsg("Unable to handle keypress");
    307   }
     276  return newmsgs;
    308277}
    309278
     
    365334}
    366335
    367 static void sig_handler_main_thread(void *data) {
    368   int sig = GPOINTER_TO_INT(data);
    369 
    370   owl_function_debugmsg("Got signal %d", sig);
    371   if (sig == SIGWINCH) {
     336void sig_handler(int sig, siginfo_t *si, void *data)
     337{
     338  if (sig==SIGWINCH) {
     339    /* we can't inturrupt a malloc here, so it just sets a flag
     340     * schedulding a resize for later
     341     */
    372342    owl_function_resize();
    373   } else if (sig == SIGTERM || sig == SIGHUP) {
     343  } else if (sig==SIGPIPE || sig==SIGCHLD) {
     344    /* Set a flag and some info that we got the sigpipe
     345     * so we can record that we got it and why... */
     346    owl_global_set_errsignal(&g, sig, si);
     347  } else if (sig==SIGTERM || sig==SIGHUP) {
    374348    owl_function_quit();
    375   } else if (sig == SIGINT && owl_global_take_interrupt(&g)) {
    376     owl_input in;
    377     in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR];
    378     owl_process_input_char(in);
    379   }
    380 }
    381 
    382 static void sig_handler(const siginfo_t *siginfo, void *data) {
    383   /* If it was an interrupt, set a flag so we can handle it earlier if
    384    * needbe. sig_handler_main_thread will check the flag to make sure
    385    * no one else took it. */
    386   if (siginfo->si_signo == SIGINT) {
    387     owl_global_add_interrupt(&g);
    388   }
    389   /* Send a message to the main thread. */
    390   owl_select_post_task(sig_handler_main_thread,
    391                        GINT_TO_POINTER(siginfo->si_signo), NULL);
    392 }
    393 
    394 #define CHECK_RESULT(s, syscall) \
    395   G_STMT_START {                 \
    396     if ((syscall) != 0) {        \
    397       perror((s));               \
    398       exit(1);                   \
    399     }                            \
    400   } G_STMT_END
     349  }
     350}
     351
     352void sigint_handler(int sig, siginfo_t *si, void *data)
     353{
     354  owl_global_set_interrupted(&g);
     355}
     356
     357static int owl_errsignal_pre_select_action(owl_ps_action *a, void *data)
     358{
     359  siginfo_t si;
     360  int signum;
     361  if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
     362    owl_function_error("Got unexpected signal: %d %s  (code: %d band: %ld  errno: %d)",
     363        signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
     364        si.si_code, si.si_band, si.si_errno);
     365  }
     366  return 0;
     367}
    401368
    402369void owl_register_signal_handlers(void) {
    403   struct sigaction sig_ignore = { .sa_handler = SIG_IGN };
    404   struct sigaction sig_default = { .sa_handler = SIG_DFL };
    405   sigset_t sigset;
    406   int ret, i;
    407   const int signals[] = { SIGABRT, SIGBUS, SIGCHLD, SIGFPE, SIGHUP, SIGILL,
    408                           SIGINT, SIGQUIT, SIGSEGV, SIGTERM, SIGWINCH };
    409 
    410   /* Sanitize our signals; the mask and dispositions from our parent
    411    * aren't really useful. Signal list taken from equivalent code in
    412    * Chromium. */
    413   CHECK_RESULT("sigemptyset", sigemptyset(&sigset));
    414   if ((ret = pthread_sigmask(SIG_SETMASK, &sigset, NULL)) != 0) {
    415     errno = ret;
    416     perror("pthread_sigmask");
    417   }
    418   for (i = 0; i < G_N_ELEMENTS(signals); i++) {
    419     CHECK_RESULT("sigaction", sigaction(signals[i], &sig_default, NULL));
    420   }
    421 
    422   /* Turn off SIGPIPE; we check the return value of write. */
    423   CHECK_RESULT("sigaction", sigaction(SIGPIPE, &sig_ignore, NULL));
    424 
    425   /* Register some signals with the signal thread. */
    426   CHECK_RESULT("sigaddset", sigaddset(&sigset, SIGWINCH));
    427   CHECK_RESULT("sigaddset", sigaddset(&sigset, SIGTERM));
    428   CHECK_RESULT("sigaddset", sigaddset(&sigset, SIGHUP));
    429   CHECK_RESULT("sigaddset", sigaddset(&sigset, SIGINT));
    430   owl_signal_init(&sigset, sig_handler, NULL);
     370  struct sigaction sigact;
     371
     372  /* signal handler */
     373  /*sigact.sa_handler=sig_handler;*/
     374  sigact.sa_sigaction=sig_handler;
     375  sigemptyset(&sigact.sa_mask);
     376  sigact.sa_flags=SA_SIGINFO;
     377  sigaction(SIGWINCH, &sigact, NULL);
     378  sigaction(SIGALRM, &sigact, NULL);
     379  sigaction(SIGPIPE, &sigact, NULL);
     380  sigaction(SIGTERM, &sigact, NULL);
     381  sigaction(SIGHUP, &sigact, NULL);
     382
     383  sigact.sa_sigaction=sigint_handler;
     384  sigaction(SIGINT, &sigact, NULL);
    431385}
    432386
     
    481435
    482436#endif /* OWL_STDERR_REDIR */
     437
     438static int owl_refresh_pre_select_action(owl_ps_action *a, void *data)
     439{
     440  owl_colorpair_mgr *cpmgr;
     441
     442  /* if a resize has been scheduled, deal with it */
     443  owl_global_check_resize(&g);
     444  /* update the terminal if we need to */
     445  owl_window_redraw_scheduled();
     446  /* On colorpair shortage, reset and redraw /everything/. NOTE: if we
     447   * still overflow, this be useless work. With 8-colors, we get 64
     448   * pairs. With 256-colors, we get 32768 pairs with ext-colors
     449   * support and 256 otherwise. */
     450  cpmgr = owl_global_get_colorpair_mgr(&g);
     451  if (cpmgr->overflow) {
     452    owl_function_debugmsg("colorpairs: used all %d pairs; reset pairs and redraw.",
     453                          owl_util_get_colorpairs());
     454    owl_fmtext_reset_colorpairs(cpmgr);
     455    owl_function_full_redisplay();
     456    owl_window_redraw_scheduled();
     457  }
     458  return 0;
     459}
     460
    483461
    484462int main(int argc, char **argv, char **env)
     
    490468  const char *dir;
    491469  owl_options opts;
    492   GSource *source;
    493470
    494471  if (!GLIB_CHECK_VERSION (2, 12, 0))
     
    505482  g.load_initial_subs = opts.load_initial_subs;
    506483
     484  owl_register_signal_handlers();
    507485  owl_start_curses();
    508486
    509487  /* owl global init */
    510488  owl_global_init(&g);
    511   if (opts.rm_debug) unlink(OWL_DEBUG_FILE);
    512489  if (opts.debug) owl_global_set_debug_on(&g);
    513490  if (opts.confdir) owl_global_set_confdir(&g, opts.confdir);
     
    516493  g_strfreev(argv_copy);
    517494  owl_global_set_haveaim(&g);
    518 
    519   owl_register_signal_handlers();
    520495
    521496  /* register STDIN dispatch; throw away return, we won't need it */
     
    577552  owl_function_debugmsg("startup: executing perl startup, if applicable");
    578553  perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();");
    579   if (perlout) g_free(perlout);
     554  g_free(perlout);
    580555
    581556  /* welcome message */
     
    610585  owl_global_push_context(&g, OWL_CTX_INTERACTIVE|OWL_CTX_RECV, NULL, "recv", NULL);
    611586
    612   source = owl_window_redraw_source_new();
    613   g_source_attach(source, NULL);
    614   g_source_unref(source);
    615 
    616   source = g_source_new(&owl_process_messages_funcs, sizeof(GSource));
    617   g_source_attach(source, NULL);
    618   g_source_unref(source);
     587  owl_select_add_pre_select_action(owl_refresh_pre_select_action, NULL, NULL);
     588  owl_select_add_pre_select_action(owl_process_messages, NULL, NULL);
     589  owl_select_add_pre_select_action(owl_errsignal_pre_select_action, NULL, NULL);
    619590
    620591  owl_function_debugmsg("startup: entering main loop");
     
    623594  /* Shut down everything. */
    624595  owl_zephyr_shutdown();
    625   owl_signal_shutdown();
    626596  owl_shutdown_curses();
    627597  return 0;
Note: See TracChangeset for help on using the changeset viewer.