예제 #1
0
파일: mirb.c 프로젝트: rubiojr/mruby
static int
parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
{
  static const struct _args args_zero = { 0 };

  *args = args_zero;

  for (argc--,argv++; argc > 0; argc--,argv++) {
    char *item;
    if (argv[0][0] != '-') break;

    item = argv[0] + 1;
    switch (*item++) {
    case 'v':
      if (!args->verbose) mrb_show_version(mrb);
      args->verbose = 1;
      break;
    case '-':
      if (strcmp((*argv) + 2, "version") == 0) {
        mrb_show_version(mrb);
        exit(EXIT_SUCCESS);
      }
      else if (strcmp((*argv) + 2, "verbose") == 0) {
        args->verbose = 1;
        break;
      }
      else if (strcmp((*argv) + 2, "copyright") == 0) {
        mrb_show_copyright(mrb);
        exit(EXIT_SUCCESS);
      }
    default:
      return EXIT_FAILURE;
    }
  }
  return EXIT_SUCCESS;
}
예제 #2
0
파일: mrbc.c 프로젝트: CaptainJet/mruby
static int
parse_args(mrb_state *mrb, int argc, char **argv, struct mrbc_args *args)
{
  char *outfile = NULL;
  static const struct mrbc_args args_zero = { 0 };
  int i;

  *args = args_zero;
  args->argc = argc;
  args->argv = argv;
  args->prog = argv[0];

  for (i=1; i<argc; i++) {
    if (argv[i][0] == '-') {
      switch ((argv[i])[1]) {
      case 'o':
        if (args->outfile) {
          fprintf(stderr, "%s: an output file is already specified. (%s)\n",
                  args->prog, outfile);
          return -1;
        }
        if (argv[i][2] == '\0' && argv[i+1]) {
          i++;
          args->outfile = get_outfilename(mrb, argv[i], "");
        }
        else {
          args->outfile = get_outfilename(mrb, argv[i] + 2, "");
        }
        break;
      case 'B':
        if (argv[i][2] == '\0' && argv[i+1]) {
          i++;
          args->initname = argv[i];
        }
        else {
          args->initname = argv[i]+2;
        }
        if (*args->initname == '\0') {
          fprintf(stderr, "%s: function name is not specified.\n", args->prog);
          return -1;
        }
        break;
      case 'c':
        args->check_syntax = 1;
        break;
      case 'v':
        if (!args->verbose) mrb_show_version(mrb);
        args->verbose = 1;
        break;
      case 'g':
        args->debug_info = 1;
        break;
      case '-':
        if (argv[i][1] == '\n') {
          return i;
        }
        if (strcmp(argv[i] + 2, "version") == 0) {
          mrb_show_version(mrb);
          exit(EXIT_SUCCESS);
        }
        else if (strcmp(argv[i] + 2, "verbose") == 0) {
          args->verbose = 1;
          break;
        }
        else if (strcmp(argv[i] + 2, "copyright") == 0) {
          mrb_show_copyright(mrb);
          exit(EXIT_SUCCESS);
        }
        return -1;
      default:
        return i;
      }
    }
    else {
      break; 
    }
  }
  return i;
}
예제 #3
0
파일: mruby.c 프로젝트: charliesome/mruby
static int
parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
{
  char **origargv = argv;
  static const struct _args args_zero = { 0 };

  *args = args_zero;

  for (argc--,argv++; argc > 0; argc--,argv++) {
    char *item;
    if (argv[0][0] != '-') break;

    if (strlen(*argv) <= 1) {
      argc--; argv++;
      args->rfp = stdin;
      break;
    }

    item = argv[0] + 1;
    switch (*item++) {
    case 'b':
      args->mrbfile = 1;
      break;
    case 'c':
      args->check_syntax = 1;
      break;
    case 'e':
      if (item[0]) {
        goto append_cmdline;
      }
      else if (argc > 1) {
        argc--; argv++;
        item = argv[0];
append_cmdline:
        if (!args->cmdline) {
          char *buf;

          buf = (char *)mrb_malloc(mrb, strlen(item)+1);
          strcpy(buf, item);
          args->cmdline = buf;
        }
        else {
          args->cmdline = (char *)mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2);
          strcat(args->cmdline, "\n");
          strcat(args->cmdline, item);
        }
      }
      else {
        printf("%s: No code specified for -e\n", *origargv);
        return 0;
      }
      break;
    case 'v':
      mrb_show_version(mrb);
      args->verbose = 1;
      break;
    case '-':
      if (strcmp((*argv) + 2, "version") == 0) {
        mrb_show_version(mrb);
	exit(0);
      }
      else if (strcmp((*argv) + 2, "verbose") == 0) {
        args->verbose = 1;
        break;
      }
      else if (strcmp((*argv) + 2, "copyright") == 0) {
        mrb_show_copyright(mrb);
	exit(0);
      }
      else return -3;
      return 0;
    default:
      return -4;
    }
  }

  if (args->rfp == NULL && args->cmdline == NULL) {
    if (*argv == NULL) args->rfp = stdin;
    else {
      args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
      if (args->rfp == NULL) {
	printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
	return 0;
      }
      args->fname = 1;
      args->cmdline = argv[0];
      argc--; argv++;
    }
  }
  args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
  memcpy(args->argv, argv, (argc+1) * sizeof(char*));
  args->argc = argc;

  return 0;
}
예제 #4
0
파일: mrbc.c 프로젝트: chancelab/mruby
static int
parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
{
  char *infile = NULL;
  char *outfile = NULL;
  char **origargv = argv;
  int result = 0;
  static const struct _args args_zero = { 0 };

  *args = args_zero;
  args->ext = RITEBIN_EXT;

  for (argc--,argv++; argc > 0; argc--,argv++) {
    if (**argv == '-') {
      if (strlen(*argv) == 1) {
        args->filename = infile = "-";
        args->rfp = stdin;
        break;
      }

      switch ((*argv)[1]) {
      case 'o':
        if (outfile) {
          printf("%s: An output file is already specified. (%s)\n",
                 *origargv, outfile);
          result = -5;
          goto exit;
        }
        outfile = get_outfilename((*argv) + 2, "");
        break;
      case 'B':
        args->ext = C_EXT;
        args->initname = (*argv) + 2;
        if (*args->initname == '\0') {
          printf("%s: Function name is not specified.\n", *origargv);
          result = -2;
          goto exit;
        }
        break;
      case 'c':
        args->check_syntax = 1;
        break;
      case 'v':
        mrb_show_version(mrb);
        args->verbose = 1;
        break;
      case '-':
        if (strcmp((*argv) + 2, "version") == 0) {
          mrb_show_version(mrb);
          exit(0);
        }
        else if (strcmp((*argv) + 2, "verbose") == 0) {
          args->verbose = 1;
          break;
        }
        else if (strcmp((*argv) + 2, "copyright") == 0) {
          mrb_show_copyright(mrb);
          exit(0);
        }
        result = -3;
        goto exit;
      default:
        break;
      }
    }
    else if (args->rfp == NULL) {
      args->filename = infile = *argv;
      if ((args->rfp = fopen(infile, "r")) == NULL) {
        printf("%s: Cannot open program file. (%s)\n", *origargv, infile);
        goto exit;
      }
    }
  }

  if (infile == NULL) {
    result = -4;
    goto exit;
  }
  if (!args->check_syntax) {
    if (outfile == NULL) {
      if (strcmp("-", infile) == 0) {
        outfile = infile;
      }
      else {
        outfile = get_outfilename(infile, args->ext);
      }
    }
    if (strcmp("-", outfile) == 0) {
      args->wfp = stdout;
    }
    else if ((args->wfp = fopen(outfile, "wb")) == NULL) {
      printf("%s: Cannot open output file. (%s)\n", *origargv, outfile);
      result = -1;
      goto exit;
    }
  }
 exit:
  if (outfile && infile != outfile) free(outfile);
  return result;
}
예제 #5
0
파일: mrdb.c 프로젝트: Asmod4n/mruby
static int
parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
{
  char **origargv = argv;
  static const struct _args args_zero = { 0 };

  *args = args_zero;

  for (argc--,argv++; argc > 0; argc--,argv++) {
    char *item;
    if (argv[0][0] != '-') break;

    item = argv[0] + 1;
    switch (*item++) {
    case 'b':
      args->mrbfile = TRUE;
      break;
    case 'd':
      if (item[0]) {
        goto append_srcpath;
      }
      else if (argc > 1) {
        argc--; argv++;
        item = argv[0];
append_srcpath:
        if (!args->srcpath) {
          size_t buflen;
          char *buf;

          buflen = strlen(item) + 1;
          buf = (char *)mrb_malloc(mrb, buflen);
          memcpy(buf, item, buflen);
          args->srcpath = buf;
        }
        else {
          size_t srcpathlen;
          size_t itemlen;

          srcpathlen = strlen(args->srcpath);
          itemlen = strlen(item);
          args->srcpath =
            (char *)mrb_realloc(mrb, args->srcpath, srcpathlen + itemlen + 2);
          args->srcpath[srcpathlen] = '\n';
          memcpy(args->srcpath + srcpathlen + 1, item, itemlen + 1);
        }
      }
      else {
        printf("%s: No path specified for -d\n", *origargv);
        return EXIT_SUCCESS;
      }
      break;
    case '-':
      if (strcmp((*argv) + 2, "version") == 0) {
        mrb_show_version(mrb);
        exit(EXIT_SUCCESS);
      }
      else if (strcmp((*argv) + 2, "copyright") == 0) {
        mrb_show_copyright(mrb);
        exit(EXIT_SUCCESS);
      }
    default:
      return EXIT_FAILURE;
    }
  }

  if (args->rfp == NULL) {
    if (*argv == NULL) {
      printf("%s: Program file not specified.\n", *origargv);
      return EXIT_FAILURE;
    }
    else {
      args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r");
      if (args->rfp == NULL) {
        printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
        return EXIT_FAILURE;
      }
      args->fname = argv[0];
      argc--; argv++;
    }
  }
  args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
  memcpy(args->argv, argv, (argc+1) * sizeof(char*));
  args->argc = argc;

  return EXIT_SUCCESS;
}
예제 #6
0
파일: mrbc.c 프로젝트: nurse/mruby
static int
parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
{
  char *infile = NULL;
  char *outfile = NULL;
  char **origargv = argv;

  memset(args, 0, sizeof(*args));
  args->ext = RITEBIN_EXT;

  for (argc--,argv++; argc > 0; argc--,argv++) {
    if (**argv == '-') {
      if (strlen(*argv) <= 1)
        return -1;

      switch ((*argv)[1]) {
      case 'o':
        outfile = get_outfilename((*argv) + 2, "");
        break;
      case 'B':
      case 'C':
        args->ext = C_EXT;
        args->initname = (*argv) + 2;
        if (*args->initname == '\0') {
          printf("%s: Function name is not specified.\n", *origargv);
          return -2;
        }
        args->dump_type = ((*argv)[1] == 'B') ? DUMP_TYPE_BIN : DUMP_TYPE_CODE;
        break;
      case 'c':
        args->check_syntax = 1;
        break;
      case 'v':
        mrb_show_version(mrb);
        args->verbose = 1;
        break;
      case '-':
        if (strcmp((*argv) + 2, "version") == 0) {
          mrb_show_version(mrb);
	  exit(0);
        }
        else if (strcmp((*argv) + 2, "verbose") == 0) {
          args->verbose = 1;
          break;
        }
        else if (strcmp((*argv) + 2, "copyright") == 0) {
          mrb_show_copyright(mrb);
	  exit(0);
        }
        else return -3;
        return 0;
      }
    }
    else if (args->rfp == NULL) {
      infile = *argv;
      if ((args->rfp = fopen(infile, "r")) == NULL) {
        printf("%s: Cannot open program file. (%s)\n", *origargv, infile);
        return 0;
      }
    }
  }

  if (infile == NULL)
    return -4;
  if (args->check_syntax)
    return 0;

  if (outfile == NULL)
    outfile = get_outfilename(infile, args->ext);

  if ((args->wfp = fopen(outfile, "wb")) == NULL) {
    printf("%s: Cannot open output file. (%s)\n", *origargv, outfile);
    return 0;
  }

  return 0;
}
예제 #7
0
파일: mruby.c 프로젝트: Zyxwvu/mruby
static int
parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
{
  char **origargv = argv;

  memset(args, 0, sizeof(*args));

  for (argc--,argv++; argc > 0; argc--,argv++) {
    if (argv[0][0] != '-') break;

    if (strlen(*argv) <= 1)
      return -1;

    switch ((*argv)[1]) {
    case 'b':
      args->mrbfile = 1;
      break;
    case 'c':
      args->check_syntax = 1;
      break;
    case 'e':
      if (argc > 1) {
        argc--; argv++;
        if (!args->cmdline) {
          char *buf;

          buf = mrb_malloc(mrb, strlen(argv[0])+1);
          strcpy(buf, argv[0]);
          args->cmdline = buf;
        }
        else {
          args->cmdline = mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(argv[0])+2);
          strcat(args->cmdline, "\n");
          strcat(args->cmdline, argv[0]);
        }
      }
      else {
        printf("%s: No code specified for -e\n", *origargv);
        return 0;
      }
      break;
    case 'v':
      mrb_show_version(mrb);
      args->verbose = 1;
      break;
    case '-':
      if (strcmp((*argv) + 2, "version") == 0) {
        mrb_show_version(mrb);
	exit(0);
      }
      else if (strcmp((*argv) + 2, "verbose") == 0) {
        args->verbose = 1;
        break;
      }
      else if (strcmp((*argv) + 2, "copyright") == 0) {
        mrb_show_copyright(mrb);
	exit(0);
      }
      else return -3;
      return 0;
    }
  }


  if (args->rfp == NULL && args->cmdline == NULL) {
    if (*argv == NULL) args->rfp = stdin;
    else if ((args->rfp = fopen(*argv, args->mrbfile ? "rb" : "r")) == NULL) {
      printf("%s: Cannot open program file. (%s)\n", *origargv, *argv);
      return 0;
    }
  }
  args->argv = mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1));
  memcpy(args->argv, argv, (argc+1) * sizeof(char*));
  args->argc = argc;

  return 0;
}