示例#1
0
int main(int argc, char* argv[])
{
  char* path_to_config;
  char* path_to_errors;
  int   is_batch_mode;
  int key = 0;
  int list_scr = 0;
  struct process_list* proc_list;
  screen_t* screen = NULL;
  int screen_num = 0;
  int q;
  int paranoia_level;

  /* Check OS to make sure we can run. */
  paranoia_level = check();

  init_options(&options);
  options.paranoia_level = paranoia_level;

  path_to_config = get_path_to_config(argc, argv);
  path_to_errors = get_path_to_error(argc, argv);
  is_batch_mode = get_batch_mode(argc, argv);

  init_errors(is_batch_mode, path_to_errors);

  q = read_config(path_to_config, &options);
  if (q == 0) {
    debug_printf("Config file successfully parsed.\n");
    options.config_file = 1;
  }
  else
    debug_printf("Could not parse config file.\n");

  /* Parse command line arguments. */
  parse_command_line(argc, argv, &options, &list_scr, &screen_num);


  /* Add default screens */
  if (options.default_screen == 1)
    init_screen();

  /* Remove unused but declared counters */
  tamp_counters();

  if (list_scr) {
    list_screens();
    delete_screens();
    exit(0);
  }

  if (options.spawn_pos) {
    /* monitor only spawned process */
    int child = spawn(argv + options.spawn_pos);
    options.only_pid = child;
    options.idle = 1;
  }

  do {
    if (screen_num >= 0)
      screen = get_screen(screen_num);
    else
      screen = get_screen_by_name(argv[-screen_num]);

    if (!screen) {
      fprintf(stderr, "No such screen.\n");
      exit(EXIT_FAILURE);
    }

    /* initialize the list of processes, and then run */
    proc_list = init_proc_list();

    if (options.spawn_pos) {
      options.spawn_pos = 0;  /* do this only once */
      new_processes(proc_list, screen, &options);
      start_child();
    }

    if (options.batch) {
      batch_mode(proc_list, screen);
      key = 'q';
    }
#ifdef HAVE_LIBCURSES
    else {
      key = live_mode(proc_list, screen);
      if ((key == '+')  || (key == KEY_RIGHT)) {
        screen_num = (screen_num + 1) % get_num_screens();
        active_col = 0;
        done_proc_list(proc_list);
        free(header);
      }
      if ((key == '-') || (key == KEY_LEFT)) {
        int n = get_num_screens();
        screen_num = (screen_num + n - 1) % n;
        active_col = 0;
        done_proc_list(proc_list);
        free(header);
      }
      if ((key == 'u') || (key == 'K') || (key == 'p')) {
        done_proc_list(proc_list);
      }
    }
#endif
  } while (key != 'q');

  /* done, free memory (makes valgrind happy) */
  close_error();
  delete_screens();
  done_proc_list(proc_list);
  free_options(&options);
  return 0;
}
示例#2
0
/*
 * Main program
 */
gint main(gint argc, gchar *argv[])
{
    gint semid, rc;
    RADContext *ctxt;

    ctxt = g_new0(RADContext, 1);
    if (ctxt == NULL)
    {
        fprintf(stderr, "Unable to allocate memory\n");
        return(-ENOMEM);
    }

    rc = load_options(ctxt, argc, argv);
    if (rc < 0)
        print_usage(rc);
    else if (rc > 0)
        print_usage(0);

    semid = rad_get_lock(ctxt->main_filename);
    if (semid < 0)
    {
        fprintf(stderr, "rastadel: Unable to acquire lock\n");
        return(semid);
    }

    ctxt->main.doc = rad_parse_file(ctxt->main_filename);
    if (ctxt->main.doc == NULL)
    {
        fprintf(stderr, "rastadel: Unable to parse file \"%s\"\n",
                ctxt->main_filename);
        rc = -ENOENT;
        goto out;
    }

    rc = rad_validate_doc(ctxt->main.doc, "RASTA",
                          "file:" _RASTA_DATA_DIR
                          G_DIR_SEPARATOR_S RASTA_DTD);
    if (rc != 0)
    {
        fprintf(stderr, "rastadel: File \"%s\" is invalid\n",
                ctxt->main_filename);
        goto out;
    }

    rc = rad_validate_file(ctxt);
    if (rc != 0)
    {
        fprintf(stderr, "rastadel: File \"%s\" is invalid\n",
                ctxt->main_filename);
        goto out;
    }

    ctxt->delta.doc = rad_parse_file(ctxt->delta_filename);
    if (ctxt->delta.doc == NULL)
    {
        fprintf(stderr, "rastadel: Unable to parse file \"%s\"\n",
                ctxt->delta_filename);
        rc = -ENOENT;
        goto out;
    }

    rc = rad_validate_doc(ctxt->delta.doc, "RASTAMODIFY",
                          "file:" _RASTA_DATA_DIR
                          G_DIR_SEPARATOR_S RASTAMODIFY_DTD);
    if (rc != 0)
    {
        fprintf(stderr, "rastadel: File \"%s\" is invalid\n",
                ctxt->delta_filename);
        goto out;
    }

    rc = validate_deletion(ctxt);
    if (rc != 0)
    {
        fprintf(stderr, "rastadel: File \"%s\" is invalid\n",
                ctxt->delta_filename);
        goto out;
    }

    rc = delete_screens(ctxt);
    if (rc != 0)
    {
        fprintf(stderr, "rastadel: Unable to delete screens\n");
        goto out;
    }
    
    rc = delete_paths(ctxt);
    if (rc != 0)
    {
        fprintf(stderr, "rastadel: Unable to delete paths\n");
        goto out;
    }

    rc = rad_validate_doc(ctxt->main.doc, "RASTA",
                          "file:" _RASTA_DATA_DIR
                          G_DIR_SEPARATOR_S RASTA_DTD);
    if (rc != 0)
    {
        fprintf(stderr,
                "rastadel: Deletions create an invalid document\n");
        goto out;
    }

    rc = rad_save_file(ctxt);
    if (rc != 0)
        fprintf(stderr, "rastadel: Unable to save new copy of \"%s\"\n",
                ctxt->main_filename);

out:
    rad_drop_lock(semid);
    rad_clean_context(ctxt);

    return(rc);
}  /* main() */