<commit_before>#include "bcm2835/rpiemulator.h"

#include <getopt.h>

/**
 * Prints the command line options
 */
static void
cmdline_usage(int argc, char **argv)
{
  printf("%s [options] image\n", argc > 0 ? argv[0] : "emulate");
  printf("Options:\n");
  printf("  --quiet         Does not dump CPU state\n");
  printf("  --graphics      Emulate framebuffer\n");
  printf("  --memory=size   Specify memory size in bytes\n");
  printf("  --addr=addr     Specify kernel start address\n");
  printf("  --help          Print this message\n");
}

/**
 * Parses command line arguments using getopt
 * @param opt   Reference to the options struct
 * @param argc  Number of command line arguments
 * @param argv  Argument values
 * @param usage Pointer to usage value
 * @return Nonzero if arguments are valid
 */
static int
cmdline_parse(remu::EmulatorOptions &opt, int argc, char **argv, int *usage)
{
  struct option options[] =
  {
    { "help",      no_argument,        usage,             1 },
    { "quiet",     no_argument,        &opt.quiet,        1 },
    { "nes",       no_argument,        &opt.nes_enabled,  1 },
    { "memory",    required_argument, 0,                 'm' },
    { "addr",      required_argument, 0,                 'a' },
    { "gpio-test", required_argument, 0,                 'i' },
    { 0, 0, 0, 0 }
  };

  int c, index;

  /* Default args */
  opt.mem_size = 256 * 1024 * 1024;
  opt.start_addr = 0x10000;

  /* Read all arguments */
  while ((c = getopt_long(argc, argv, "vghsm:a:", options, &index)) != -1)
  {
    switch (c)
    {
      case 'q': opt.quiet = 1; break;
      case 'h': *usage = 1;    break;
      case 'm':
      {
        /* Handle prefixes */
        int length = strlen(optarg);
        switch (optarg[length - 1])
        {
          case 'm': case 'M':
          {
            optarg[length - 1] = '\0';
            sscanf(optarg, "%zu", &opt.mem_size);
            opt.mem_size *= 1024 * 1024;
            break;
          }
          case 'k': case 'K':
          {
            optarg[length - 1] = '\0';
            sscanf(optarg, "%zu", &opt.mem_size);
            opt.mem_size *= 1024;
            break;
          }
          default:
          {
            sscanf(optarg, "%zu", &opt.mem_size);
          }
        }
        break;
      }
      case 'a':
      {
        sscanf(optarg, "%u", &opt.start_addr);
        break;
      }
      case 'i':
      {
        sscanf(optarg, "%u", &opt.gpio_test_offset);
        break;
      }
      case 0:
      {
        /* Flag set */
        break;
      }
      case '?':
      {
        /* Error */
        return 0;
      }
    }
  }

  /* Read image source */
  opt.image = optind >= argc ? NULL : argv[optind];
  return 1;
}

/**
 * Checks the validity of the arguments
 * @param opt  Reference to the options struct
 * @param argc Number of command line arguments
 * @param argv Argument values
 * @return Nonzero if arguments are valid
 */
static bool
cmdline_check(remu::EmulatorOptions &opt)
{
  /* Image source */
  if (!opt.image)
  {
    fprintf(stderr, "No kernel image specified.\n");
    return false;
  }

  /* Memory size at least 64kb */
  if (opt.mem_size < 0x10000)
  {
    fprintf(stderr, "Must specify a minimum of 64kb of memory.\n");
    return false;<commit_msg>Convert cmdline_parse to return bool<commit_after>#include "bcm2835/rpiemulator.h"

#include <getopt.h>

/**
 * Prints the command line options
 */
static void
cmdline_usage(int argc, char **argv)
{
  printf("%s [options] image\n", argc > 0 ? argv[0] : "emulate");
  printf("Options:\n");
  printf("  --quiet         Does not dump CPU state\n");
  printf("  --graphics      Emulate framebuffer\n");
  printf("  --memory=size   Specify memory size in bytes\n");
  printf("  --addr=addr     Specify kernel start address\n");
  printf("  --help          Print this message\n");
}

/**
 * Parses command line arguments using getopt
 * @param opt   Reference to the options struct
 * @param argc  Number of command line arguments
 * @param argv  Argument values
 * @param usage Pointer to usage value
 * @return true if arguments are valid
 */
static bool
cmdline_parse(remu::EmulatorOptions &opt, int argc, char **argv, int *usage)
{
  struct option options[] =
  {
    { "help",      no_argument,        usage,             1 },
    { "quiet",     no_argument,        &opt.quiet,        1 },
    { "nes",       no_argument,        &opt.nes_enabled,  1 },
    { "memory",    required_argument, 0,                 'm' },
    { "addr",      required_argument, 0,                 'a' },
    { "gpio-test", required_argument, 0,                 'i' },
    { 0, 0, 0, 0 }
  };

  int c, index;

  /* Default args */
  opt.mem_size = 256 * 1024 * 1024;
  opt.start_addr = 0x10000;

  /* Read all arguments */
  while ((c = getopt_long(argc, argv, "vghsm:a:", options, &index)) != -1)
  {
    switch (c)
    {
      case 'q': opt.quiet = 1; break;
      case 'h': *usage = 1;    break;
      case 'm':
      {
        /* Handle prefixes */
        int length = strlen(optarg);
        switch (optarg[length - 1])
        {
          case 'm': case 'M':
          {
            optarg[length - 1] = '\0';
            sscanf(optarg, "%zu", &opt.mem_size);
            opt.mem_size *= 1024 * 1024;
            break;
          }
          case 'k': case 'K':
          {
            optarg[length - 1] = '\0';
            sscanf(optarg, "%zu", &opt.mem_size);
            opt.mem_size *= 1024;
            break;
          }
          default:
          {
            sscanf(optarg, "%zu", &opt.mem_size);
          }
        }
        break;
      }
      case 'a':
      {
        sscanf(optarg, "%u", &opt.start_addr);
        break;
      }
      case 'i':
      {
        sscanf(optarg, "%u", &opt.gpio_test_offset);
        break;
      }
      case 0:
      {
        /* Flag set */
        break;
      }
      case '?':
      {
        /* Error */
        return false;
      }
    }
  }

  /* Read image source */
  opt.image = optind >= argc ? NULL : argv[optind];
  return true;
}

/**
 * Checks the validity of the arguments
 * @param opt  Reference to the options struct
 * @param argc Number of command line arguments
 * @param argv Argument values
 * @return Nonzero if arguments are valid
 */
static bool
cmdline_check(remu::EmulatorOptions &opt)
{
  /* Image source */
  if (!opt.image)
  {
    fprintf(stderr, "No kernel image specified.\n");
    return false;
  }

  /* Memory size at least 64kb */
  if (opt.mem_size < 0x10000)
  {
    fprintf(stderr, "Must specify a minimum of 64kb of memory.\n");
    return false;
{'fraction_non_alphanumeric': 0.14082804472650348, 'fraction_numerical': 0.01964339679661529, 'mean_word_length': 1.810615711252654, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5779258', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2111, 'n_words': 603}