int main(int argc, char *argv[])
{
  int debug = 1;
  int ch;
  char *file_path = NULL;

  static struct option long_options[] = {
                { "test", no_argument, 0, 't' },
                { "input", no_argument, 0, 'i' },
                { "debug", no_argument, 0, 'd' },
                { "help",  no_argument, 0, 'h' },
                { "version", no_argument, 0, 'v' },
    { 0 },
    { 0 }
  };
  while (1) {
    int option_index = 0;
    ch = getopt_long(argc, argv, "hvdtD:i:",
        long_options, &option_index);
    if (ch == -1) break;
    switch (ch) {
    case 'h':
      usage();
      exit(0);
      break;
    case 'v':
      fprintf(stdout, "%s\n", PACKAGE_VERSION);
      exit(0);
      break;
    case 'd':
      debug++;
      break;
    case 'D':
      log_accept(optarg);
      break;
    case 'i':
      file_path = optarg;
      break;
    case 't':
      log_init(2, __progname);
      test();
      exit(0);
      break;
    default:
      fprintf(stderr, "unknown option `%c'\n", ch);
      usage();
      exit(1);
    }
  }

  // Setup program
  log_init(debug, __progname);
  log_info("main", "hello world!");
  description();

  // Create abstract types variables
  seq_ptr seq = NULL;
  stack_ptr stack = NULL;

  // Initialize and solve exercise
  if(file_path == NULL) {
    seq_prepare(&seq, TMP_SEQUENCE);
    prompt_user(&seq);
    seq_init(&seq);
  } else {
    seq_prepare(&seq, file_path);
  }
  stack_create(&stack);
  solve_exercise(&seq, &stack);
  printf("[OUTPUT] The result for the algorithm is the following:\n");
  stack_output(&stack);

  // Clean up and return success
  if(file_path == NULL) remove(TMP_SEQUENCE);
  return EXIT_SUCCESS;
}
Beispiel #2
0
int
main(int argc, char *argv[])
{
    int debug = 1;
    int dev = 0;
    int ch;

    struct hm_cfg cfgs[MAX_DEBUGFS_CONFIGS];

    int devs = debugfs_get_config(cfgs);

    struct hm_cfg cfg = {
        .path = cfgs[dev].path,
        .rate = atoi(HM_DEFAULT_RATE),
        .width = cfgs[dev].width,
        .min = hm_min_value(HM_DEFAULT_MIN),
        .max = hm_max_value(HM_DEFAULT_MAX)
    };

    static struct option long_options[] = {
        { "debug", no_argument, 0, 'd' },
        { "help",  no_argument, 0, 'h' },
        { "version", no_argument, 0, 'v' },
        { "dbgfs", required_argument, 0, 'f' },
        { "path", required_argument, 0, 'p' },
        { "rate", required_argument, 0, 'r' },
        { "width", required_argument, 0, 'w' },
        { "min", required_argument, 0, 'm' },
        { "max", required_argument, 0, 'M' },
        { "values", no_argument, 0, 'V' },
        { "scan", no_argument, 0, 's' },
        { "gray", no_argument, 0, 'g' },
        { 0 }
    };

    int index_option;
    unsigned long uval;
    char *end;
    while ((ch = getopt_long(argc, argv, "ghvdD:f:p:r:w:m:M:Vs",
                             long_options, &index_option)) != -1) {
        switch (ch) {
        case 'h':
            usage();
            exit(0);
            break;
        case 'v':
            fprintf(stdout, "%s\n", PACKAGE_VERSION);
            exit(0);
            break;
        case 'd':
            debug++;
            break;
        case 'D':
            log_accept(optarg);
            break;
        case 'f':
            dev = atoi(optarg);
            cfg.path = cfgs[dev].path;
            cfg.width = cfgs[dev].width;
            break;
        case 'p':
            cfg.path = optarg;
            cfg.width = atoi(HM_DEFAULT_WIDTH);

            if (devs == 0)
                devs = 1;
            break;
        case 'r':
            errno = 0;
            uval = strtoul(optarg, &end, 10);
            if ((errno == ERANGE && (uval == ULONG_MAX || uval == 0)) ||
                (errno != 0 && uval == 0) || *end != '\0') {
                fprintf(stderr, "rate should be an unsigned integer, not `%s'\n",
                        optarg);
                usage();
                exit(1);
            }
            cfg.rate = uval;
            break;
        case 'w':
            errno = 0;
            uval = strtoul(optarg, &end, 10);
            if ((errno == ERANGE && (uval == ULONG_MAX || uval == 0)) ||
                (errno != 0 && uval == 0) || *end != '\0') {
                fprintf(stderr, "width should be an unsigned integer, not `%s'\n",
                        optarg);
                usage();
                exit(1);
            }
            cfg.width = uval;
            break;
        case 'm':
            cfg.min = hm_min_value(optarg);
            break;
        case 'M':
            cfg.max = hm_max_value(optarg);
            break;
        case 'V':
            cfg.values = true;
            break;
        case 's':
            print_debugfs_devices(cfgs, devs);
            exit(0);
            break;
        case 'g':
            cfg.gray = true;
            break;
        default:
            usage();
            exit(1);
        }
    }

    cfg.auto_min = (cfg.min == INT_MAX);
    cfg.auto_max = (cfg.max == INT_MIN);

    log_init(debug, __progname);

    if (devs == 0)
        fatal("heatmap", "No data path");

    /* Setup signals */
    struct sigaction actterm;
    sigemptyset(&actterm.sa_mask);
    actterm.sa_flags = 0;
    actterm.sa_handler = hm_terminate;
    if (sigaction(SIGTERM, &actterm, NULL) < 0)
        fatal("heatmap", "unable to register SIGTERM");
    struct sigaction actwinch;
    sigemptyset(&actwinch.sa_mask);
    actwinch.sa_flags = 0;
    actwinch.sa_handler = hm_resize;
    if (sigaction(SIGWINCH, &actwinch, NULL) < 0)
        fatal("heatmap", "unable to register SIGWINCH");
    initscr();
    cbreak();
    noecho();
    curs_set(0);
    hm_display_init(&cfg);

    int err = 0;
    do {
        if (cfg.rate > 0) {
            const struct timespec ts = {
                .tv_sec = 1 / cfg.rate,
                .tv_nsec = ((cfg.rate > 1)?(1000 * 1000 * 1000 / cfg.rate):0)
            };
            nanosleep(&ts, NULL);
        }

        int *data;
        size_t len;
        len = hm_retrieve_data(&cfg, &data);
        if (len == 0) {
            if (err++ > 5) {
                endwin();
                fatalx("heatmap", "unable to retrieve data");
            }
            continue;
        }
        err = 0;
        if (resize) {
            resize = false;
            endwin();
            refresh();
            clear();
        }
        hm_display_data(&cfg, data, len);
        free(data);
    } while (!stop);

    endwin();

    return EXIT_SUCCESS;
}