Esempio n. 1
0
void handle_args(int argc, char *argv[])
{
    int i;

    for(i=0; argv[i]; i++) {
        if(streq(argv[i], "-script")) {
            script = argv[++i];
            continue;
        }
        if(streq(argv[i], "-name")) {
            name = argv[++i];
            continue;
        }
        if(streq(argv[i], "-bufsize")) {
            bufsize = atoi(argv[++i]);
            if(bufsize <= 0) {
                ATwarning("warning: illegal buffer size %d\n", bufsize);
                bufsize = DEFAULT_BUF_SIZE;
            }
        }
        if(streq(argv[i], "-TB_TOOL_NAME")) {
            use_toolbus = ATtrue;
        }
    }
    if(!name)
        name = script;

    if(!name)
        name = "noname";

    if(use_toolbus)
        cid = ATBconnect(NULL, NULL, -1, from_toolbus);
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
  ATerm syntax = NULL, extended = NULL;
  char *moduleName = "Main";
  char *input = "-";
  char *output = "-"; 
  int cid;
  int c, toolbus_mode = 0;
  ATerm bottomOfStack;
  name = argv[0];

  /*  Check whether we're a ToolBus process  */
  for(c=1; !toolbus_mode && c<argc; c++) {
    toolbus_mode = !strcmp(argv[c], "-TB_TOOL_NAME"); 
  }
 
  ATinit(argc, argv, &bottomOfStack);
  SDF_initSDFMEApi();

  ASC_initRunTime(INITIAL_TABLE_SIZE);

  register_Add_Eqs_Syntax();
  resolve_Add_Eqs_Syntax();
  init_Add_Eqs_Syntax();

  if(toolbus_mode) {
    #ifndef WIN32 /* Code with Toolbus calls, non Windows */
      ATBinit(argc, argv, &bottomOfStack);  /* Initialize the Aterm library */
      cid = ATBconnect(NULL, NULL, -1, addeqssyntax_handler);
      ATBeventloop();
    #else
      ATwarning("addeqssyntax: Toolbus cannot be used in Windows.\n");
    #endif
  }
  else {
    while ((c = getopt(argc, argv, myarguments)) != -1) {
      switch (c) {
	case 'm':  moduleName=optarg;                      break;
        case 'v':  run_verbose = ATtrue;                   break;
        case 'i':  input=optarg;                           break;
        case 'o':  output=optarg;                          break;
        case 'V':  version(); exit(0);                     break;

        case 'h':
        default:   usage(); exit(0);                       break;
      }
    } 
    argc -= optind;
    argv += optind;

    syntax = ATreadFromNamedFile(input);
   
    extended = addEqsSyntax(moduleName, syntax);  

    ATwriteToNamedBinaryFile(extended, output);
  }
  return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[])         /* main program of hello tool */
{ ATerm bottomOfStack;                   /* marks stack bottom for ATerms */

  ATBinit(argc, argv, &bottomOfStack);  /* initialize ToolBus library */
  if(ATBconnect(NULL, NULL, -1, hello_handler) >= 0){
    ATBeventloop();
  } else {
    fprintf(stderr, "hello: Could not connect to the ToolBus, giving up!\n");
    return -1;
  }
  return 0;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
  ATerm bottomOfStack;

  ATBinit(argc, argv, &bottomOfStack);
  if(ATBconnect(NULL, NULL, -1, printer_handler) >= 0){
    ATBeventloop();
  } else {
    fprintf(stderr, "printer: Could not connect to the ToolBus, giving up!\n");
    return -1;
  }
  return 0;
}                    
Esempio n. 5
0
int main(int argc, char *argv[]) {
  ATerm bottomOfStack;
  int cid;

  ATBinit(argc, argv, &bottomOfStack);
  EM_initEditorManagerApi();

  sessions = ATtableCreate(INITIAL_TABLE_SIZE, TABLE_RESIZE_PERCENTAGE);
  bindings = ATtableCreate(INITIAL_TABLE_SIZE, TABLE_RESIZE_PERCENTAGE);

  cid = ATBconnect(NULL, NULL, -1, editor_manager_handler);

  return ATBeventloop();
}
Esempio n. 6
0
int main(int argc, char *argv[])
{
  ATerm bottomOfStack;
  gethostname(localhost, BUFSIZ);

  ATBinit(argc, argv, &bottomOfStack);
  master_cid = ATBconnect(NULL, NULL, -1, toolbus_adapter_handler);
  fprintf(stderr, "master_cid = %d\n", master_cid);
  if (master_cid >= 0) {
    ATBeventloop();
  } else {
    fprintf(stderr, "%s: Could not connect to the ToolBus, giving up!\n",
	    argv[0]);
    return 1;
  }

  return 0;
}
Esempio n. 7
0
int main(int argc, char *argv[])
{
  ATerm bottomOfStack;
  int i, cid;

  for (i=1; i<argc; i++) {
      if (strcmp(argv[i], "-h") == 0) {
	  usage(argv[0], ATfalse);
      } else if (strcmp(argv[i], "-V") == 0) {
	  version(argv[0]);
      }
  }

  ATBinit(argc, argv, &bottomOfStack);
  SDF_initSDFMEApi();
  PT_initMEPTApi();

  cid = ATBconnect(NULL, NULL, -1, sdf_modules_handler);

  ATBeventloop();

  return 0;
}
Esempio n. 8
0
int main (int argc, char *argv[])
{
  int c; /* option character */
  ATerm bottomOfStack;
  ATerm tree;
  Graph graph;

  char   *input_file_name  = "-";
  char   *output_file_name = "-";

  ATbool characters = ATfalse;
  ATbool productions = ATfalse;
  ATbool layout = ATfalse;
  ATbool literals = ATtrue;
  ATbool binary = ATtrue;
  ATbool sharing = ATfalse;

#ifndef WITHOUT_TOOLBUS
  ATbool use_toolbus = ATfalse;
  int i;
 
  for (i=1; !use_toolbus && i < argc; i++) {
    use_toolbus = !strcmp(argv[i], "-TB_TOOL_NAME");
  }
 
  if (use_toolbus) {
    int cid;
    ATBinit(argc, argv, &bottomOfStack);
    PT_initMEPTApi();
    initGraphApi();
    cid = ATBconnect(NULL, NULL, -1, graph_converter_handler);
    ATBeventloop();
  }
  else
#endif
  {
    while ((c = getopt(argc, argv, myarguments)) != EOF)
      switch (c) {
        case 'b':  binary = ATtrue;              break;
        case 'c':  characters = ATtrue;          break;
        case 'h':  usage();                      exit(0);
        case 'i':  input_file_name  = optarg;    break;
        case 'l':  layout = ATtrue;              break;
        case 'm':  literals = ATfalse;           break;
        case 'o':  output_file_name = optarg;    break;
        case 'p':  productions = ATtrue;         break;
        case 't':  binary = ATfalse;             break;
        case 's':  sharing = ATtrue;             break;
        case 'V':  fprintf(stderr, "%s %s\n", myname, myversion);
                                                 exit(0);
        default :  usage();                      exit(1);
    }

    ATinit(argc, argv, &bottomOfStack);
    PT_initMEPTApi();
    initGraphApi();

    tree = ATreadFromNamedFile(input_file_name);

    if(tree == NULL) {
      ATerror("%s: could not read term from input file %s\n", 
	      myname, input_file_name);
    }

    graph = PT_printAnyToGraph(tree, characters, productions,
			       layout, literals, sharing);

    if (!strcmp(output_file_name,"-")) {
      if (binary) {
        ATwriteToBinaryFile(GraphToTerm(graph), stdout);
      } 
      else {
        ATwriteToTextFile(GraphToTerm(graph), stdout);
      } 
    }
    else {
      if (binary) {
        ATwriteToNamedBinaryFile(GraphToTerm(graph), output_file_name);
      } 
      else {
        ATwriteToNamedTextFile(GraphToTerm(graph), output_file_name);
      } 
    }
  }

  return 0;
}
Esempio n. 9
0
ATerm toolbus_start(int conn, const char *script, ATerm args)
{
  int i, pid, cid;
  char *argv[MAX_ARGS];
  char sockets[2][BUFSIZ];

  WellKnownSocketPort = TB_PORT;
  if (mk_server_ports(0) == TB_ERROR) {
    ATerror("cannot create server ports, giving up!\n");
  } else {
    fprintf(stderr, "server ports created at %d\n", WellKnownSocketPort);
  }

  pid = fork();
  if (pid == -1) {
    ATerror("cannot fork toolbus-adapter, giving up!\n");
  } else if (pid > 0) {
    /* Parent */
    /* connect to child toolbus! */
    int attempts = 0;
    do {
      cid = ATBconnect(NULL, NULL, WellKnownSocketPort,
		       toolbus_adapter_handler);
      if (cid < 0) {
	tb_sleep(0, 500000);
      }
    } while (cid < 0 && attempts++ < MAX_ATTEMPTS);
    if (cid < 0) {
      return ATparse("snd-value(toolbus-error)");
    } else {
      if (cid > max_cid) {
	max_cid = cid;
      }
      return ATmake("snd-value(toolbus-started(<int>))", cid);
    }
  } else {
    /* Child */
    /*{{{  setup arguments */

    int argc = 0;
    ATermList arg_list;

    sprintf(sockets[0], "%d", WellKnownLocalSocket);
    sprintf(sockets[1], "%d", WellKnownGlobalSocket);

    argv[argc++] = TBPROG;
    argv[argc++] = "-TB_USE_SOCKETS";
    argv[argc++] = sockets[0];
    argv[argc++] = sockets[1];

    assert(ATgetType(args) == AT_LIST);
    arg_list = (ATermList)args;

    while (!ATisEmpty(arg_list)) {
      ATerm arg = ATgetFirst(arg_list);
      arg_list = ATgetNext(arg_list);
      if (ATgetType(arg) == AT_APPL) {
	argv[argc++] = ATgetName(ATgetAFun((ATermAppl)arg));
      } else {
	argv[argc] = strdup(ATwriteToString(arg));
	assert(argv[argc]);
	argc++;
      }
    }

    /* Jurgen added this (char*) cast to prevent a compiler warning. 
     * But this code has more problems: we know that updating
     * argv[] arrays is not portable, so the following code is actually
     * wrong:
     */
    argv[argc++] = (char*) script;
    argv[argc] = NULL;

    for (i=0; i<argc; i++) {
      fprintf(stderr, "argv[%d] = %s\n", i, argv[i]);
    }

    /*}}}  */

    if (execv(TBPROG, argv) < 0) {
      perror(TBPROG);
      ATerror("cannot execute toolbus executable, giving up!\n");
    }
  }

  return NULL;
}
Esempio n. 10
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;
}
Esempio n. 11
0
/*
 *  This function is registered as a handler for HTTP methods and will
 *  therefore be invoked for all GET requests (and others).
 *  In this type of request handler, stderr is redirected to a httpd log file
 *  (default: <apache_dir>/logs/error.log)
 *  TODO:
 *    - use apr_... functions (Apache Portable Runtime for Apache 2.0) 
 *      instead of ap_... ones where applicable
 */
static int http_req_handler( request_rec *http_req )
{
  /* toolbus initialization */
  ATerm BottomOfStack;          /* used by ATBinit(), don't move */
  char* tb_init_argv[] =           /* used to set up TB connection */
    {
      "this_is_a_spacefiller_dummy", 
      "-TB_TOOL_NAME", TB_TOOL_NAME_VALUE 
    };
  int tb_init_argc = 4;
  int tb_conn;              /* set by ATBconnect() */
  
  /* toolbus communication */
  ATerm tb_event_to_send;
  ATerm tb_args;
    
  char args_ascii[SEND_BUF_SIZE];  
  
  /* these are used for control flow in the toolbus section */
  int count;

  /* init toolbus  module */
  ATBinit( tb_init_argc, tb_init_argv, &BottomOfStack );
 
  /* update global variable so the toolbus handler can use it too */
  global_http_req = http_req;
    
  /* print some debugging info to the log file */
  fprintf( stderr, "mod_toolbus: invoked with pid=%d, ppid=%d\n",
      (int) getpid(), (int) getppid() );
  fflush( stderr );
  
  /* check if this is a toolbus request, otherwise we don't handle it */
  if( strcmp( global_http_req->uri, MOD_TOOLBUS_NAME_URI ) ) 
  {
    return DECLINED;
  }

    ap_set_content_type( global_http_req, "text/html" );
    if( global_http_req->header_only ) { return OK; }

  #ifdef VERBOSE
    dump_http_req_fields( global_http_req );
  #endif
  
  /*
   *  read event to send from HTTP request arguments, validate
   *  filter, and prepare it to be sent to the toolbus
   */
  utf8_to_ascii(args_ascii , global_http_req->args );
  fprintf( stderr, "args: %s\n", global_http_req->args  );
  fprintf( stderr, "send_buf_ascii %s\n", args_ascii );

  if ((tb_args = parseHTTPArgs(args_ascii)) == NULL) {
    ap_rprintf(global_http_req, "parse error in %s\n", args_ascii);
    return OK;
  }

  /*
   *  The toolbus part starts here.
   *  We connect to the toolbus and send an event to it.
   */    
  fprintf( stderr, "mod_toolbus: connecting to Toolbus...\n" );
  fflush( stderr );
  
  /*
   *  Check if our flag is nonzero. It should only be set by our 
   *  toolbus_handler(), which has not yet been called at this point. 
   *  If it is nonzero, something is wrong, and we may have encountered some
   *  race conditions (i.e. thread-unsafety).
   */
  if( global_is_tb_session_done > 0 )
  {
    mod_toolbus_fatal( 
      "global_is_tb_session_done >0 before toolbus_handler() called." );  
  }
  

  /* connect to toolbus and install our handler: toolbus_handler() */
  if( (tb_conn = ATBconnect( NULL, NULL, TOOLBUS_PORT, toolbus_handler )) >= 0 )
  {
    count = 1;        /* send one event */
    while( global_is_tb_session_done == 0 )
    {
      /* check if data is waiting on the toolbus connection */
      if( ATBpeekOne( tb_conn ) )
      {
        fprintf( stderr, "mod_toolbus: received another event.\n" );
        fflush(stderr );
        /* invoke handler "toolbus_handler()" via ATBhandleOne() */
        if( ATBhandleOne( tb_conn ) < 0 )
        {
          fprintf( stderr, 
            "mod_toolbus: ATBhandleOne() returned error\n" );
          fflush(stderr );
          return -1;
        }
      }
      else  /* no data waiting on tb_conn so we can send some */
      {
        if( count ) /* send an event to the toolbus */
        { 
          ATBwriteTerm(tb_conn, ATmake("snd-event(request(<term>))", tb_args));
          ATfprintf(stderr, "\nsending event to toolbus: %t\n",
		    ATmake( "snd-event(request(<term>))", tb_args ) );
          fflush(stderr);
          count --;
        } 
      }
    }
    fprintf( stderr, "mod_toolbus: disconnecting from toolbus...\n" );
    fflush( stderr );
    ATBdisconnect( tb_conn );
    
  } 
  else
  {
    fprintf( stderr, "mod_toolbus: could not connect to toolbus\n" );
    fflush( stderr );
  }  
     
  fprintf( stderr,"mod_toolbus: leaving handler\n");
  fflush( stderr );

  return OK;
}
Esempio n. 12
0
int main (int argc, char *argv[])
{
  int c; 
  ATerm bottomOfStack;
  ATerm input = NULL;
  ATerm output = NULL;
  ATbool textual = ATfalse;
  char *input_file_name = "-";
  char *output_file_name = "-";
  int i;
  ATbool use_toolbus = ATfalse;

  ATinit(argc, argv, &bottomOfStack);
  RS_initRStoreApi();
  PRS_initParsedRStoreApi();

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

  if (use_toolbus) {
    int cid;
    ATBinit(argc, argv, &bottomOfStack);

    cid = ATBconnect(NULL, NULL, -1, lower_rstore_handler);
    ATBeventloop();
  }
  else {
    while ((c = getopt(argc, argv, myarguments)) != EOF)
      switch (c) {
	case 'h':  usage();                      exit(0);
	case 'i':  input_file_name  = optarg;    break;
	case 'o':  output_file_name = optarg;    break;
	case 't':  textual = ATtrue; break;
	case 'V':  fprintf(stderr, "%s %s\n", myname, myversion);
		   exit(0);
	default :  usage();                      exit(1);
      }

    input = ATreadFromNamedFile(input_file_name);

    if(input == NULL) {
      ATerror("%s: could not read term from input file %s\n", 
	      myname, input_file_name);
      return 1;
    }

    if (PRS_isValidRStore((PRS_RStoreFromTerm(input)))) {
      output = (ATerm) RS_lowerRStore(PRS_RStoreFromTerm(input));
    }
    else if (PRS_isValidStart(PRS_StartFromTerm(input))) {
      PRS_RStore tmp = PRS_getStartTopRStore(PRS_StartFromTerm(input));
      output = (ATerm) RS_lowerRStore(tmp);
    }

    if(output != NULL) {
      if (textual) {
	ATwriteToNamedTextFile(output, output_file_name);
      }
      else {
	ATwriteToNamedSAFFile(output, output_file_name);
      }
    }
    else {
      ATwarning("%s: something went wrong\n", myname);
      return 1;
    }
  }

  return 0;
}
Esempio n. 13
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;
}
Esempio n. 14
0
int main(int argc, char *argv[]) 
{
  ATerm bottomOfStack;
  ATerm at_tree;
  char *ATlibArgv[] = {"pandora", "-at-termtable", "21"};
  PT_ParseTree tree; 
  PT_Tree ptText;
  BOX_Start box;
  char *input = "-";
  char *output = "-";
  ATbool boxOutput = ATfalse;
  ATbool bytesOutput = ATfalse;
  int c;
  int i;
  ATbool useToolbus = ATfalse;

  ATinit(3, ATlibArgv, &bottomOfStack); 
  ASC_initRunTime(INITIAL_TABLE_SIZE);
  PT_initMEPTApi(); 
  PTPT_initPTMEPTApi();
  BOX_initBoxApi();
  ERR_initErrorManager("pandora", "all");

  register_Box_to_Text();
  resolve_Box_to_Text();
  init_Box_to_Text();


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

  if (useToolbus) {
    int cid;
    ATBinit(argc, argv, &bottomOfStack);
    cid = ATBconnect(NULL, NULL, -1, pandora_handler);
    ATBeventloop();
  }
  else {
    while ((c = getopt(argc, argv, myarguments)) != -1) {
      switch (c) {
	case 'b':  boxOutput=ATtrue; break;
	case 'B':  bytesOutput=ATtrue; break;
	case 'i':  input=optarg; break;
	case 'o':  output=optarg; break;
	case 'v':  run_verbose = ATtrue; break;
	case 'V':  version(); exit(0);
	case 'h':  usage(); exit(0);
	default:   usage(); exit(1); 
      }
    }

    at_tree = ATreadFromNamedFile(input);

    if (at_tree != NULL) {
      tree = PT_ParseTreeFromTerm(at_tree);

      if (run_verbose) {
	ATwarning("pandora: mapping parse trees to "
		  "BOX using default rules\n");
      }

      box = pandora(tree);

      if (!boxOutput) {      
	if (run_verbose) {
	  ATwarning("pandora: rendering BOX to list of characters\n");
	}

	ptText = toText(PT_ParseTreeFromTerm(BOX_StartToTerm(box)));


	if (run_verbose) {
	  ATwarning("pandora: yielding characters to output\n");
	}

	if (!strcmp(output, "-")) {
	  if (!bytesOutput) {
	    PT_yieldTreeToFile(ptText, stdout, ATfalse);
	  }
	  else {
	    ATwriteToNamedBinaryFile((ATerm) 
				     PT_makeValidParseTreeFromTree(ptText), 
				     "-");
	  }
	}
	else {
	  if (!bytesOutput) {
	    FILE *fp = fopen(output, "wb");
	    if (fp != NULL) {
	      PT_yieldTreeToFile(ptText, fp, ATfalse);
	    }
	    else {
	      ATerror("Could not open %s for writing.\n", output);
	    }
	  }
	  else {
	    ATwriteToNamedBinaryFile((ATerm) 
				     PT_makeValidParseTreeFromTree(ptText), 
				     output);
	  }
	}
      }
      else {
	PT_ParseTree ptBox = PT_ParseTreeFromTerm(BOX_StartToTerm(box));
	ATwriteToNamedSharedTextFile((ATerm) ptBox, output);
      }
    }
    else {
      ATwarning("Failed to read ATerm from file: %s\n", input); 
      return 1; 
    }
  }

  return 0;
}