Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • owl.c

    rcc305b5 ra2a8833  
    160160 * was ignored due to user settings or otherwise.
    161161 */
    162 static int owl_process_message(owl_message *m) {
     162int owl_process_message(owl_message *m) {
    163163  const owl_filter *f;
    164164  /* if this message it on the puntlist, nuke it and continue */
     
    245245}
    246246
    247 static gboolean owl_process_messages_prepare(GSource *source, int *timeout) {
    248   *timeout = -1;
    249   return owl_global_messagequeue_pending(&g);
    250 }
    251 
    252 static gboolean owl_process_messages_check(GSource *source) {
    253   return owl_global_messagequeue_pending(&g);
    254 }
    255 
    256247/*
    257248 * 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.
    258250 */
    259 static gboolean owl_process_messages_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
     251int owl_process_messages(owl_ps_action *d, void *p)
     252{
    260253  int newmsgs=0;
    261254  int followlast = owl_global_should_followlast(&g);
     
    281274    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
    282275  }
    283   return TRUE;
    284 }
    285 
    286 static GSourceFuncs owl_process_messages_funcs = {
    287   owl_process_messages_prepare,
    288   owl_process_messages_check,
    289   owl_process_messages_dispatch,
    290   NULL
    291 };
    292 
    293 void owl_process_input_char(owl_input j)
    294 {
    295   int ret;
    296 
    297   owl_global_set_lastinputtime(&g, time(NULL));
    298   ret = owl_keyhandler_process(owl_global_get_keyhandler(&g), j);
    299   if (ret!=0 && ret!=1) {
    300     owl_function_makemsg("Unable to handle keypress");
    301   }
     276  return newmsgs;
    302277}
    303278
     
    359334}
    360335
    361 static void sig_handler_main_thread(void *data) {
    362   int sig = GPOINTER_TO_INT(data);
    363 
    364   owl_function_debugmsg("Got signal %d", sig);
    365   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     */
    366342    owl_function_resize();
    367   } 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) {
    368348    owl_function_quit();
    369   } else if (sig == SIGINT && owl_global_take_interrupt(&g)) {
    370     owl_input in;
    371     in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR];
    372     owl_process_input_char(in);
    373   }
    374 }
    375 
    376 static void sig_handler(const siginfo_t *siginfo, void *data) {
    377   /* If it was an interrupt, set a flag so we can handle it earlier if
    378    * needbe. sig_handler_main_thread will check the flag to make sure
    379    * no one else took it. */
    380   if (siginfo->si_signo == SIGINT) {
    381     owl_global_add_interrupt(&g);
    382   }
    383   /* Send a message to the main thread. */
    384   owl_select_post_task(sig_handler_main_thread,
    385                        GINT_TO_POINTER(siginfo->si_signo),
    386                        NULL, g_main_context_default());
    387 }
    388 
    389 #define CHECK_RESULT(s, syscall) \
    390   G_STMT_START {                 \
    391     if ((syscall) != 0) {        \
    392       perror((s));               \
    393       exit(1);                   \
    394     }                            \
    395   } 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}
    396368
    397369void owl_register_signal_handlers(void) {
    398   struct sigaction sig_ignore = { .sa_handler = SIG_IGN };
    399   struct sigaction sig_default = { .sa_handler = SIG_DFL };
    400   sigset_t sigset;
    401   int ret, i;
    402   const int signals[] = { SIGABRT, SIGBUS, SIGCHLD, SIGFPE, SIGHUP, SIGILL,
    403                           SIGINT, SIGQUIT, SIGSEGV, SIGTERM, SIGWINCH };
    404 
    405   /* Sanitize our signals; the mask and dispositions from our parent
    406    * aren't really useful. Signal list taken from equivalent code in
    407    * Chromium. */
    408   CHECK_RESULT("sigemptyset", sigemptyset(&sigset));
    409   if ((ret = pthread_sigmask(SIG_SETMASK, &sigset, NULL)) != 0) {
    410     errno = ret;
    411     perror("pthread_sigmask");
    412   }
    413   for (i = 0; i < G_N_ELEMENTS(signals); i++) {
    414     CHECK_RESULT("sigaction", sigaction(signals[i], &sig_default, NULL));
    415   }
    416 
    417   /* Turn off SIGPIPE; we check the return value of write. */
    418   CHECK_RESULT("sigaction", sigaction(SIGPIPE, &sig_ignore, NULL));
    419 
    420   /* Register some signals with the signal thread. */
    421   CHECK_RESULT("sigaddset", sigaddset(&sigset, SIGWINCH));
    422   CHECK_RESULT("sigaddset", sigaddset(&sigset, SIGTERM));
    423   CHECK_RESULT("sigaddset", sigaddset(&sigset, SIGHUP));
    424   CHECK_RESULT("sigaddset", sigaddset(&sigset, SIGINT));
    425   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);
    426385}
    427386
     
    465424  }
    466425  bread = read(rfd, buf, navail);
    467   if (bread == -1)
    468     return;
    469 
    470   err = g_strdup_printf("[stderr]\n%.*s", bread, buf);
     426  if (buf[navail-1] != '\0') {
     427    buf[navail] = '\0';
     428  }
     429
     430  err = g_strdup_printf("[stderr]\n%s", buf);
    471431
    472432  owl_function_log_err(err);
     
    475435
    476436#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
     447   * the current screen uses too many colorpairs, this draws
     448   * everything twice. But this is unlikely; COLOR_PAIRS is 64 with
     449   * 8+1 colors, and 256^2 with 256+1 colors. (+1 for default.) */
     450  cpmgr = owl_global_get_colorpair_mgr(&g);
     451  if (cpmgr->overflow) {
     452    owl_function_debugmsg("colorpairs: color shortage; reset pairs and redraw. COLOR_PAIRS = %d", COLOR_PAIRS);
     453    owl_fmtext_reset_colorpairs(cpmgr);
     454    owl_function_full_redisplay();
     455    owl_window_redraw_scheduled();
     456  }
     457  return 0;
     458}
     459
    477460
    478461int main(int argc, char **argv, char **env)
     
    484467  const char *dir;
    485468  owl_options opts;
    486   GSource *source;
    487469
    488470  if (!GLIB_CHECK_VERSION (2, 12, 0))
     
    499481  g.load_initial_subs = opts.load_initial_subs;
    500482
     483  owl_register_signal_handlers();
    501484  owl_start_curses();
    502485
     
    510493  owl_global_set_haveaim(&g);
    511494
    512   owl_register_signal_handlers();
    513 
    514495  /* register STDIN dispatch; throw away return, we won't need it */
    515496  owl_select_add_io_dispatch(STDIN_FILENO, OWL_IO_READ, &owl_process_input, NULL, NULL);
     
    570551  owl_function_debugmsg("startup: executing perl startup, if applicable");
    571552  perlout = owl_perlconfig_execute("BarnOwl::Hooks::_startup();");
    572   g_free(perlout);
     553  if (perlout) g_free(perlout);
    573554
    574555  /* welcome message */
     
    603584  owl_global_push_context(&g, OWL_CTX_INTERACTIVE|OWL_CTX_RECV, NULL, "recv", NULL);
    604585
    605   source = owl_window_redraw_source_new();
    606   g_source_attach(source, NULL);
    607   g_source_unref(source);
    608 
    609   source = g_source_new(&owl_process_messages_funcs, sizeof(GSource));
    610   g_source_attach(source, NULL);
    611   g_source_unref(source);
    612 
    613   owl_log_init();
     586  owl_select_add_pre_select_action(owl_refresh_pre_select_action, NULL, NULL);
     587  owl_select_add_pre_select_action(owl_process_messages, NULL, NULL);
     588  owl_select_add_pre_select_action(owl_errsignal_pre_select_action, NULL, NULL);
    614589
    615590  owl_function_debugmsg("startup: entering main loop");
     
    618593  /* Shut down everything. */
    619594  owl_zephyr_shutdown();
    620   owl_signal_shutdown();
    621595  owl_shutdown_curses();
    622   owl_log_shutdown();
    623596  return 0;
    624597}
Note: See TracChangeset for help on using the changeset viewer.