Ejemplo n.º 1
0
int asc_support_main(ATerm *bottomOfStack, int argc, char *argv[], void (*register_all)(), void (*resolve_all)(), void (*init_all)(), unsigned const char* tableBaf, size_t tableSize, ATbool parseInput, ATBhandler handler) {

  PT_ParseTree pt = NULL;
  PT_Tree asfix;
  PT_Tree trm;
  PT_ParseTree rpt = NULL;
  ATerm reduct;
  const char *outputFilename;
  int numberOfInputs;

  ATinit(argc, argv, bottomOfStack);
  initApis();

  register_all();
  resolve_all();
  init_all();

  initParsetable(tableBaf, tableSize);
  SGLR_initialize();

  OPT_initialize();
  ASC_initializeDefaultOptions();

  /*  Check whether we're a ToolBus process  */
  if (toolbusMode(argc, argv)) {
    ATBinit(argc, argv, bottomOfStack);
    ATBconnect(NULL, NULL, -1, asf_toolbus_handler);
    ATBeventloop();
  }
  else {    
    handleOptions(argc, argv, parseInput);

    numberOfInputs = ASC_getNumberOfParseTrees();
    outputFilename = ASC_getOutputFilename();

    if (!streq(ASC_getPrefixFunction(),"")) {
      pt = applyFunction((const char*) ASC_getPrefixFunction(), 
			 (const char*) ASC_getResultNonTermName(), numberOfInputs, inputs);
    } 
    else {
      if (numberOfInputs == 0) {
	pt = parsetreeFromFile("-", parseInput);
      }
      else if (numberOfInputs == 1) {
	pt = inputs[0];
      }
      else if (numberOfInputs != 1) {
	ATerror("Can only process one argument if no -f and -r option "
		"are supplied.\n"
		"Did a -s argument eat up your -f or -r option?\n");
	return 1;
      }
    }

    if (PT_isValidParseTree(pt)) {
      trm = PT_getParseTreeTop(pt);

      if (ASC_getVerboseFlag()) { ATfprintf(stderr,"Reducing ...\n"); }

      reduct = innermost(trm);

      if (ASC_getVerboseFlag()) { ATfprintf(stderr,"Reducing finished.\n"); }
      if (ASC_getStatsFlag())  { printStats(); }

      if (ASC_getOutputFlag()) {
	asfix = toasfix(reduct);
	rpt = PT_makeParseTreeTop(asfix, 0);

	if (parseInput) {
	  FILE *fp = NULL;

	  if (!strcmp(outputFilename, "-")) {
	    fp = stdout;
	  }
	  else {
	    fp = fopen(outputFilename, "wb");
	  }

	  if (fp != NULL) {
	    PT_yieldParseTreeToFile(rpt, fp, ATfalse);
	  }
	  else {
	    ATerror("asc-main: unable to open %s for writing\n", outputFilename);
	  }
	}
	else {
	  if (ASC_getBafmodeFlag()) {
	    ATwriteToNamedBinaryFile(PT_ParseTreeToTerm(rpt),outputFilename);
	  }
	  else {
	    ATwriteToNamedTextFile(PT_ParseTreeToTerm(rpt),outputFilename);
	  }
	}
      }
    }
  }

  return 0;
}
Ejemplo n.º 2
0
int asc_support_main(ATerm *bottomOfStack, int argc, char *argv[], 
		     void (*register_all)(), 
		     void (*resolve_all)(),
		     void (*init_all)()
#ifdef TOOLBUS
		     , ATBhandler handler
#endif
		     )
{
  PT_ParseTree pt;
  PT_ParseTree asfix;
  PT_Tree trm;
  ATerm t; 
  ATerm reduct;
#ifdef TOOLBUS
  ATbool toolbus_mode = ATfalse;
#endif
  ATbool produce_output = ATtrue;
  ATbool run_verbose = ATfalse;
  ATbool printstats = ATfalse;
  ATbool bafmode = ATtrue;
  char *inputs[MAX_ARGS] = { "-" };
  int nInputs = 0;
  char *output = "-";
  char *function = "";
  char *result = "";
  int i;

  ATinit(argc, argv, bottomOfStack);
  PT_initMEPTApi();
  ASF_initASFMEApi();
  ASC_initRunTime(INITIAL_TABLE_SIZE);

  register_all();
  resolve_all();
  init_all();

#ifdef TOOLBUS
  /*  Check whether we're a ToolBus process  */

  for(i=1; !toolbus_mode && i<argc; i++) {
    if(!strcmp(argv[i], "-TB_TOOL_NAME")) {
      toolbus_mode = ATtrue;
    }
  }

  if (toolbus_mode) {
     ATBinit(argc, argv, bottomOfStack);
     ATBconnect(NULL, NULL, -1, handler);
     ATBeventloop();
  }
  else {
#endif
  for(i=1; i<argc; i++) {
    if(streq(argv[i], "-v")) {
      run_verbose = ATtrue;
    } 
    else if(streq(argv[i], "-h")) {
      usage(argv[0]);
    } 
    else if(streq(argv[i], "-f")) {
      function = argv[++i];
    }
    else if(streq(argv[i], "-i")) {
      if (nInputs <= MAX_ARGS) {
        inputs[nInputs++] = strdup(argv[++i]);
      }
      else {
	ATerror("%s: Maximum number of %d \'-i\' arguments exceeded.\n", 
		argv[0], MAX_ARGS);
      }
    } 
    else if(streq(argv[i], "-o")) {
      output = argv[++i];
    }
    else if(streq(argv[i], "-r")) {
      result = argv[++i];
    }
    else if(streq(argv[i], "-s")) {
      printstats = ATtrue;
    }
    else if(streq(argv[i], "-t")) {
      bafmode = ATfalse;
    }
    else if(streq(argv[i], "-m")) {
      produce_output = ATfalse;
    }
  }


  if (!streq(function,"")) {
    PT_Args args = PT_makeArgsEmpty();
    
    if (streq(inputs[0],"-")) {
      nInputs++;
    }

    for (--nInputs; nInputs >= 0; nInputs--) {
      PT_ParseTree parseTree = PT_makeParseTreeFromTerm(
                                 ATreadFromNamedFile(inputs[nInputs]));

      if (parseTree == NULL) {
	ATerror("Unable to read in %s\n", inputs[nInputs]);
	exit(1);
      }

      args = PT_makeArgsList(PT_getParseTreeTree(parseTree), args);
    }

    pt = PT_applyFunctionToArgsParseTree(function, result, args);
  } 
  else {
    if (nInputs != 1 && (!streq(inputs[0], "-"))) {
      ATerror("Can only process one argument if no -f and -r option "
	      "are supplied.\n");
      return 1;
    }

    t = ATreadFromNamedFile(inputs[0]);
    pt = PT_makeParseTreeFromTerm(t);
  }

  if (PT_isValidParseTree(pt)) {
    trm = PT_getParseTreeTree(pt);
    
    if(run_verbose) {
      ATfprintf(stderr,"Reducing ...\n");
    }
    
    reduct = innermost(trm);
    
    if(run_verbose) {
      ATfprintf(stderr,"Reducing finished.\n");
    }

    if (printstats) {
      struct rusage usage;
      FILE *file;
      char buf[BUFSIZ];
      int size, resident, shared, trs, lrs, drs, dt;

      sprintf(buf, "/proc/%d/statm", getpid());
      file = fopen(buf, "r");
      if (file) {
	fscanf(file, "%d %d %d %d %d %d %d",
	       &size, &resident, &shared, &trs, &lrs, &drs, &dt);
	/*
	fprintf(stderr, "size     = %d kb\n", size*4);
	fprintf(stderr, "resident = %d kb\n", resident*4);
	fprintf(stderr, "shared   = %d kb\n", shared*4);
	fprintf(stderr, "trs      = %d kb\n", trs*4);
	fprintf(stderr, "lrs      = %d kb\n", lrs*4);
	*/
	fprintf(stderr, "memory used : %d kb\n", drs*4);
	/*fprintf(stderr, "dt       = %d kb\n", dt*4);*/
      } else {
	fprintf(stderr, "could not open %s\n", buf);
	perror("");
      }
      if (getrusage(RUSAGE_SELF, &usage) == -1) {
	perror("rusage");
      } else {
	/*fprintf(stderr, "rusage statistics (see 'man getrusage')\n");*/
	fprintf(stderr, "utime       : %ld.%06d sec.\n",
		(long)usage.ru_utime.tv_sec, (int)usage.ru_utime.tv_usec);
	fprintf(stderr, "stime       : %ld.%06d sec.\n",
		(long)usage.ru_stime.tv_sec, (int)usage.ru_stime.tv_usec);
#if 0
	/* The following rusage stats do not work yet */
	fprintf(stderr, "maxrss   : %ld\n", usage.ru_maxrss);
	fprintf(stderr, "ixrss    : %ld\n", usage.ru_ixrss);
	fprintf(stderr, "idrss    : %ld\n", usage.ru_idrss);
	fprintf(stderr, "isrss    : %ld\n", usage.ru_isrss);
	fprintf(stderr, "minflt   : %ld\n", usage.ru_minflt);
	fprintf(stderr, "majflt   : %ld\n", usage.ru_majflt);
	fprintf(stderr, "nswap    : %ld\n", usage.ru_nswap);
	fprintf(stderr, "\n");
	fprintf(stderr, "inblock  : %ld\n", usage.ru_nswap);
	fprintf(stderr, "oublock  : %ld\n", usage.ru_nswap);
	fprintf(stderr, "msgsnd   : %ld\n", usage.ru_nswap);
	fprintf(stderr, "msgrcv   : %ld\n", usage.ru_nswap);
	fprintf(stderr, "nsignals : %ld\n", usage.ru_nswap);
	fprintf(stderr, "nvcsw    : %ld\n", usage.ru_nswap);
	fprintf(stderr, "nivcsw   : %ld\n", usage.ru_nswap);
#endif
      }
    }
 
    if (produce_output) {
      asfix = toasfix(reduct);
      if (bafmode) {
	ATwriteToNamedBinaryFile(PT_makeTermFromParseTree(asfix),output);
      }
      else {
	ATwriteToNamedTextFile(PT_makeTermFromParseTree(asfix),output);
      }

    }
  }

#ifdef TOOLBUS
  }
#endif

  return 0;
}