view regen/src/regen.c @ 15:b48915bffa79 draft default tip

commit search.py
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sun, 01 Jul 2012 00:31:22 +0900
parents 1e0cd7fade8b
children
line wrap: on
line source

#include "re.h"
#include "util.h"
#include "codegen.h"
#include "generator.h"
#include <unistd.h>
#include <getopt.h>

void initialize(REGEX *, CODEGEN*, int, char *[]);
extern void parse(REGEX *, CODEGEN*);
void compile(REGEX *, CODEGEN*);
extern void codegen(REGEX *, CODEGEN*);
void finalize(REGEX *, CODEGEN*);

int
main(int argc, char *argv[])
{
  REGEX* r = (REGEX *)xcalloc(sizeof(REGEX), 1, "regex");
  CODEGEN* g = (CODEGEN *)xcalloc(sizeof(CODEGEN), 1, "codegen");
  initialize(r, g, argc, argv);
  parse(r, g);
  codegen(r, g);
  compile(r, g);
  finalize(r, g);
  return 0;
}

void
initialize(REGEX *r, CODEGEN *g, int argc, char *argv[])
{
  int opt, len;
  FILE *fp;

  static char buf[1024];
  g->output = stdout;
  g->enable_filter = TRUE;
  g->enable_predict = TRUE;
  g->gen_table_lookup = TRUE;
  set_gen_function(r, g);

  g->genroot = getenv("REGENROOT");
  if (g->genroot == NULL) {
    exitmsg("$REGENROOT not defined.");
  }

  while ((opt=getopt(argc, argv, "dlbglso:f:")) != -1) {
    switch (opt) {
    case 'f':
      {
        fp = xfopen(optarg, "r");
        fgets(buf, sizeof(buf), fp);
        r->regex = buf;
        if (buf[strlen(buf)-1] == '\n') {
          buf[strlen(buf)-1] = '\0';
        }
        fclose(fp);
      }
      break;
    case 'o':
        fp = xfopen(optarg, "w+");
        g->output = fp;
      break;
    case 's':
      g->gen_table_lookup = FALSE;
      break;
    case 'l':
      set_gen_label(r, g);
      break;
    case 'b':
      set_gen_cbc(r, g);
      break;
    case 'd':
      set_gen_dot(r, g);
      break;
    case 'g':
      r->debug = TRUE;
      break;
    default:
      exit(1);
    }
  }

  if (r->regex == NULL) {
    if (optind >= argc) {
      exitmsg("USAGE: regen [options] regexp\n");
    }
    r->regex = argv[optind];
  }

  len = strlen(r->regex);
  r->regex_end = r->regex+len;
  r->escaped_regex = escape(r->regex);
  r->maxid = 1;
  r->lit_expr = (EXPR **)xmalloc(len*sizeof(EXPR *), "regexp base set");
  r->involve = (UCHARP)xcalloc(sizeof(UCHAR), NCHAR, "involve");
}

void
compile(REGEX *r, CODEGEN *g)
{
}

void
finalize(REGEX *r, CODEGEN *g)
{
  //FIXME: this code make corrupted double-linked list. (why?)
  //fclose(g->output);
}