Beispiel #1
0
ATerm SSL_fdcopy(ATerm fdinA, ATerm fdoutA)
{
  int fdin, fdout;
  int n; 
  char buf[SSL_COPY_BUFSIZE];

  if(ATgetType(fdinA) != AT_INT || ATgetType(fdinA) != AT_INT)
    _fail(fdinA);

  fdin = ATgetInt((ATermInt)fdinA);
  fdout = ATgetInt((ATermInt)fdoutA);

  while( (n = read(fdin, buf, SSL_COPY_BUFSIZE)) > 0 )
    if(write(fdout, buf, n) != n)
      { 
	ATfprintf(stderr, "SSL_fdcopy: write error\n");
	_fail((ATerm) ATempty);
      }

  if(n < 0)
    {
      ATfprintf(stderr, "SSL_fdcopy: read error\n");
      _fail((ATerm) ATempty);
    }

  return (ATerm) ATempty;
}
ATerm parse_file(char *name)
{
  ATermList decls;
  int res;

  if(name == NULL) {
    yyin = stdin;
    fprintf(stderr, "parsing stdin ...");
  } else {
    yyin = find_file(name);  
    /* add file to list of dependencies */
    ATfprintf(dep_file, "%s ", file_name);
    yylineno = 0;
    if(!yyin) {
      fprintf(stderr, "no such file: %s\n", name);
      exit(1);
    }
    if(!silent)
      fprintf(stderr, "parsing %s ...", file_name);
  }
  if((res = parse()) == 0) {    
    if(!silent)
      ATfprintf(stderr, " succeeded\n", parse_tree);
    if(ATmatch(parse_tree, "Specification([<list>])", &decls))
      {
	return ATmake("Specification([<list>])", 
		      get_imports(decls));
      }
    else
      return parse_tree;    
  } else {    
    ATfprintf(stderr, " parsing %s failed\n\n", name);
    exit(1);
  }
}
Beispiel #3
0
ATerm parse_file(char *name)
{
  ATermList decls;
  int res;

  if(name == NULL) {
    yyin = stdin;
    fprintf(stderr, "parsing stdin ...");
  } else {
    yyin = fopen(name, "r");
    sprintf(file_name, "%s", name);
    /* yyin = find_file(name); */ 
    /* add file to list of dependencies */
    /* ATfprintf(dep_file, "%s ", file_name); */
    yylineno = 0;
    if(!yyin) {
      fprintf(stderr, "no such file: %s\n", name);
      exit(1);
    }
    if(!silent)
      fprintf(stderr, "parsing %s ...", file_name);
  }
  if((res = parse()) == 0) {    
    if(!silent)
      ATfprintf(stderr, " succeeded\n", parse_tree);
    return parse_tree;    
  } else {    
    ATfprintf(stderr, " parsing %s failed\n\n", name);
    exit(1);
  }
}
ATermList get_imports(ATermList decls)
{
  ATerm decl, mod, spec;
  ATermList mods, new_decls;
  
  new_decls = ATmakeList0();
  while(!ATisEmpty(decls)) {
    decl = ATgetFirst(decls);
    decls = ATgetNext(decls);
    if(ATmatch(decl, "Imports([<list>])", &mods)) {
	while(!ATisEmpty(mods)) {
	    mod = ATgetFirst(mods);
	    mods = ATgetNext(mods);
	    if(ATindexOf(imported, mod, 0) == -1) 
	      {
		if(!silent)
		  ATfprintf(stderr, "        importing: %t\n", mod);
		imported = ATinsert(imported, mod);
		sprintf(file_name, "%s.r", t_string(mod));
		spec = parse_file(file_name);
		new_decls = ATconcat(spec_decls(spec), new_decls);
	    }
	    else
	      {
		if(!silent)
		  ATfprintf(stderr, "        importing: %t (done)\n", mod);
	      }
	}
    } else {
	new_decls = ATinsert(new_decls, decl);
    }
  }
  return new_decls;
}
int main (int argc, char * argv [])
{
ATerm out_term;
ATermList in_term;
int i;
ATinit(argc, argv, &(out_term));
init_constructors();
in_term = ATempty;
for ( i = (argc - 1) ; (i >= 0) ; i-- )
{
in_term = ATinsert(in_term, (ATerm) ATmakeAppl0(ATmakeSymbol(argv[i], 0, ATtrue)));
}
SRTS_stratego_initialize();
memset(__tracing_table, 0, (sizeof(unsigned short) * TRACING_TABLE_SIZE));
__tracing_table_counter = 0;
register_strategies();
out_term = main_0_0(NULL, (ATerm) in_term);
if((out_term != NULL))
{
ATfprintf(stdout, "%t\n", out_term);
exit(0);
}
else
{
ATfprintf(stderr, "%s: rewriting failed, trace:\n", argv[0]);
for ( i = 0 ; (__tracing_table[i] && (i < TRACING_TABLE_SIZE)) ; i++ )
ATfprintf(stderr, "\t%s\n", __tracing_table[i]);
exit(1);
}
}
Beispiel #6
0
static void init_module_constant_terms(void)
{
  ATfprintf(stderr, "init_constant_terms (bar.c)()\n");
  ATprotect(&(term_p_0));
  term_p_0 = (ATerm) ATmakeAppl(ATmakeSymbol("a", 0, ATtrue));
  ATfprintf(stderr, "init_constant_terms (bar.c) end()\n");
}
Beispiel #7
0
void print_profile(double duration)
{
  int counter; 
  if(show_stats > 0)
    {
      ATfprintf(stderr, "\n  statistics      :");
      if(duration != 0)
	ATfprintf(stderr, 
		  " %.2f seconds, %d steps, %.2f steps/sec\n",
		  (double)duration, rule_counter, ((double) rule_counter)/duration);
      else
	ATfprintf(stderr, 
		  " %.2f seconds, %d steps\n",
		  duration, rule_counter);  
    }
  if(show_stats > 1) 
    {
      ATfprintf(stderr, "  counts per rule :\n");  
      for(counter = 0; counter < cur_rule_counter; counter++) 
	{ 
	  ATfprintf(stderr, "%17s : %6d\n", rule_counters[counter].name, 
		    rule_counters[counter].count); 
	}
    }
}
Beispiel #8
0
void SG_Usage(FILE *stream, ATbool long_message)
{
  const char usage[] = "Usage:\n\t%s\t-p file [-2?bcdhlmnPtvV] "
  "[-f[adeirp]] [-i file] [-o file] \\"
  "\n\t\t[-s sort]\n";

  ATfprintf(stream, usage, program_name);
  if(long_message) {
    ATfprintf(stream,
              "Parameters:\n"
              "\t-2         : use AsFix2 output format             [%s]\n"
              "\t-A         : ambiguities are treated as errors    [%s]\n"
              "\t-b         : output AsFix in binary format (BAF)  [%s]\n"
              "\t-c         : toggle cycle detection               [%s]\n"
              "\t-d         : toggle debug mode                    [%s]\n"
              "\t-f[adeipr] : toggle filtering, or specific filter [%s]\n"
              "\t  a : associativity                               [%s]\n"
              "\t  d : direct eagerness                            [%s]\n"
              "\t  e : eagerness                                   [%s]\n"
              "\t  i : injection count                             [%s]\n"
              "\t  p : priority                                    [%s]\n"
              "\t  r : reject                                      [%s]\n"
              "\t-h, -?     : display usage information\n"
              "\t-i file    : input from |file|                    [%s]\n"
              "\t-l         : toggle statistics logging            [%s]\n"
              "\t-m         : use AsFix2ME output format           [%s]\n"
              "\t-n         : toggle parse tree creation           [%s]\n"
              "\t-o file    : output to |file|                     [%s]\n"
              "\t-p file    : use parse table |file| (required)    [%s]\n"
              "\t-s symbol  : define start symbol                  [%s]\n"
              "\t-t         : output AsFix in textual format       [%s]\n"
              "\t-v         : toggle verbose mode                  [%s]\n"
              "\t-V         : reveal program version (i.e. %s)\n",
              DEFAULTMODE(!asfix2meflag),
              DEFAULTMODE(ambiguityerrorflag),
              DEFAULTMODE(binaryflag), 
              DEFAULTMODE(cycleflag),
              DEFAULTMODE(debugflag), 
              DEFAULTMODE(filterflag),
              DEFAULTMODE(filter_associativityflag),
              DEFAULTMODE(filter_directeagernessflag),
              DEFAULTMODE(filter_eagernessflag),
              DEFAULTMODE(filter_injectioncountflag),
              DEFAULTMODE(filter_priorityflag),
              DEFAULTMODE(filter_rejectflag),
              input_file_name, 
              DEFAULTMODE(statisticsflag),
              DEFAULTMODE(asfix2meflag),
              DEFAULTMODE(outputflag),
              output_file_name,
              parse_table_name?parse_table_name:"unspecified",
              start_symbol ? start_symbol:"any", 
              DEFAULTMODE(!binaryflag),
              DEFAULTMODE(verboseflag),
              VERSION
    );
  }
}
Beispiel #9
0
void DumpOneAction(FILE *out, parse_table *pt, int s)
{
  actions  val;
  int      c, nxt = 0;

  for(c = 0; c <= SG_CHAR_CLASS_EOF; c++) {
    if((val = SG_LookupAction(pt, SG_SETSTATE(s), c)) && !ATisEmpty(val)) {
      if(!nxt++)
        ATfprintf(out,"%d", s);
      ATfprintf(out,"\t%d\t%t\n", c, val);
    }
  }
}
Beispiel #10
0
static void generateHeader(FILE *file, ATermList terms, ATermList afuns)
{
  if (opt_gen_date) {
    time_t now = time(NULL);
    fprintf(file, "/*\n * Generated at %s", ctime(&now));
    fprintf(file, " */\n\n");
  }

  fprintf(file, "#ifndef __%s_H\n", code_prefix);
  fprintf(file, "#define __%s_H\n\n", code_prefix);
  fprintf(file, "#include <aterm2.h>\n\n");
  fprintf(file, "#include <assert.h>\n\n");

  while (!ATisEmpty(afuns)) {
    ATerm afun, alias, pair = ATgetFirst(afuns);
    afuns = ATgetNext(afuns);

    if (!ATmatch(pair, "[<term>,<term>]", &alias, &afun)) {
      ATfprintf(stderr, "malformed [alias,afun] pair: %t\n", pair);
      exit(1);
    }

    checkAlias(alias);
    checkAFun(afun);

    ATfprintf(file, "extern AFun %t;\n", alias);
  }

  fprintf(file, "\n");

  while (!ATisEmpty(terms)) {
    ATerm term, alias, pair = ATgetFirst(terms);
    terms = ATgetNext(terms);

    if (!ATmatch(pair, "[<term>,<term>]", &alias, &term)) {
      ATfprintf(stderr, "malformed [alias,term] pair: %t\n", pair);
      exit(1);
    }

    checkAlias(alias);

    ATfprintf(file, "extern ATerm %t;\n", alias);
  }

  fprintf(file, "\nextern void init_%s();\n", code_prefix);

  fprintf(file, "\n#endif /* __%s_H */\n", code_prefix);
}
Beispiel #11
0
/* Event handler for tool 'batch' */
ATerm batch_handler(int conn, ATerm term)
{
  ATerm in, out;
  /* We need some temporary variables during matching */
  int i0;
  char *s0;
  ATerm t0;

  if(ATmatch(term, "rec-eval(fromFile)")) {
    return fromFile(conn);
  }
  if(ATmatch(term, "rec-do(toFile(<str>,<int>))", &s0, &i0)) {
    toFile(conn, s0, i0);
    return NULL;
  }
  if(ATmatch(term, "rec-terminate(<term>)", &t0)) {
    rec_terminate(conn, t0);
    return NULL;
  }
  if(ATmatch(term, "rec-do(signature(<term>,<term>))", &in, &out)) {
    ATerm result = batch_checker(conn, in);
    if(!ATmatch(result, "[]"))
      ATfprintf(stderr, "warning: not in input signature:\n\t%\n\tl\n", result);
    return NULL;
  }

  ATerror("tool batch cannot handle term %t", term);
  return NULL; /* Silence the compiler */
}
Beispiel #12
0
/* Event handler for tool 'toolSdf2Sig' */
ATerm toolSdf2Sig_handler(int conn, ATerm term)
{
  ATerm in, out;
  /* We need some temporary variables during matching */
  char *s0, *s1;
  ATerm t0;

  if(ATmatch(term, "rec-eval(rewrite(<term>))", &t0)) {
    return rewrite(conn, t0);
  }
  if(ATmatch(term, "rec-eval(apply-rewrite(<str>,<str>,<term>))", &s0, &s1, &t0)) {
    return apply_rewrite(conn, s0, s1, t0);
  }
  if(ATmatch(term, "rec-terminate(<term>)", &t0)) {
    rec_terminate(conn, t0);
    return NULL;
  }
  if(ATmatch(term, "rec-do(signature(<term>,<term>))", &in, &out)) {
    ATerm result = toolSdf2Sig_checker(conn, in);
    if(!ATmatch(result, "[]"))
      ATfprintf(stderr, "warning: not in input signature:\n\t%\n\tl\n", result);
    return NULL;
  }

  ATerror("tool toolSdf2Sig cannot handle term %t", term);
  return NULL; /* Silence the compiler */
}
Beispiel #13
0
/* Event handler for tool 'restorebrackets' */
ATerm restorebrackets_handler(int conn, ATerm term)
{
  ATerm in, out;
  /* We need some temporary variables during matching */
  ATerm t0, t1;

  if(ATmatch(term, "rec-eval(restore-brackets(<term>,<term>))", &t0, &t1)) {
    return restore_brackets(conn, t0, t1);
  }
  if(ATmatch(term, "rec-terminate(<term>)", &t0)) {
    rec_terminate(conn, t0);
    return NULL;
  }
  if(ATmatch(term, "rec-eval(add-brackets(<term>,<term>))", &t0, &t1)) {
    return add_brackets(conn, t0, t1);
  }
  if(ATmatch(term, "rec-do(signature(<term>,<term>))", &in, &out)) {
    ATerm result = restorebrackets_checker(conn, in);
    if(!ATmatch(result, "[]"))
      ATfprintf(stderr, "warning: not in input signature:\n\t%\n\tl\n", result);
    return NULL;
  }

  ATerror("tool restorebrackets cannot handle term %t", term);
  return NULL; /* Silence the compiler */
}
Beispiel #14
0
ATerm bar_1_0(StrSL sl, StrCL s, ATerm t)
{
  ATfprintf(stderr, "bar_1_0(%t)\n", t);
  t = cl_fun(s)(cl_sl(s),t);
  t = (ATerm)ATmakeAppl2(sym__2, term_p_0, t);
  return(t);
}
Beispiel #15
0
void CAESAR_PRINT_STATE_HEADER(CAESAR_TYPE_FILE fp) {
    ATerm l = (ATerm)MCRLgetListOfPars();
    ATerm v,s;
    while (ATmatch(l,"[v(<term>,<term>),<list>]",
                   &v,&s,&l))
        ATfprintf(fp,"%t:%s \n",MCRLprint(v),ATgetName(ATgetSymbol(s)));
}
int main(int argc, char *argv[])
{ 
  ATerm in_term, out_term;
  int i;

  /* ATfprintf(stderr, "This is %s\n", argv[0]); */

  ATinit(argc, argv, &in_term);

  ATprotectArray(term_stack,   TSIZE);
  ATprotectArray(environment,  ESIZE);

  in_term = ATmake("Nil");
  for(i = argc - 1; i >= 0; i--)
    {
      /* ATfprintf(stderr, " %s", argv[i]); */
      in_term = App2("Cons", ATmakeString(argv[i]), in_term);
    }

  /* ATfprintf(stderr, "\n", argv[i]); */

  out_term = doit(in_term);
  ATfprintf(stderr, "**** rewriting terminated abnormally\n");
  exit(3);
}
Beispiel #17
0
static
void unexpected(ATerm tree, int n, char *str)
{
  ATfprintf(stderr,
   "The %dth argument was expected to be %s\n%a\n", n, str, ATgetAFun(tree));
  exit(1);
}
Beispiel #18
0
static void checkAFun(ATerm afun)
{
  if (ATgetType(afun) != AT_APPL) {
    ATfprintf(stderr, "wrong afun spec: %t\n", afun);
    exit(1);
  }
}
Beispiel #19
0
int ACC_try() {
#ifdef DEBUG
  ATfprintf(stderr, "Trying.\n");
#endif
  //  return localSetChoicePoint();
  return setChoicePoint();
}
Beispiel #20
0
void usage(void)
{
  ATfprintf(stderr, 
	    "usage:\n"
	    "  %s [-silent] [-i file] [-o file] [-b] [-stats] [-help|-h]\n", 
	    program_name);
  exit(1);
}
Beispiel #21
0
static void prettyPrint(ATerm t, FILE *fp)
{
  ATermList ambs;
  int count;
  int i;

  if (ATmatch(t, "ambiguities(<int>,[<list>])",&count,&ambs)) {
    if (count == 0) {
      ATfprintf(fp,"No ambiguities\n");
    }
    else {
      ATfprintf(fp, "%d ambiguity cluster%s:\n\n",count,count > 1 ? "s" : "");
      
      for(i = 1;!ATisEmpty(ambs); ambs = ATgetNext(ambs), i++) {
	ATerm amb = ATgetFirst(ambs);
	ATermList productions;
	ATerm line, col, offset;

	if(ATmatch(amb,"ambiguity("
		       "  position(character(0),"
		       "           line(<term>),"
		       "           col(<term>),"
		       "           char(<term>)),"
		       "  productions([<list>]))",
		   &line, 
		   &col, 
		   &offset, 
		   &productions)) {
	  ATfprintf(fp,"[%d/%d] at (%t:%t):\n", i, count, line, col);
	  for(;!ATisEmpty(productions); productions = ATgetNext(productions)) {
	    char *str = deslash(ATgetFirst(productions));
	    ATfprintf(fp,"  %s\n", str);
	    free(str);
	  }

	  ATfprintf(fp,"\n");
	} else {
	  ATerror("%s: Unexpected term: %t\n",myname,t);
	}
      }
    }
  } else {
    ATerror("%s: Unexpected term: %t\n", myname,t);
    return;
  }
}
Beispiel #22
0
void ACC_throw_failure() {
#ifdef DEBUG
  ATfprintf(stderr, "Throwing failure\n");
#endif
  thrown = AN2_FAILURE;
  //localFail();
  fail();
}
Beispiel #23
0
void ACC_throw_exception(AN2Data data) {
#ifdef DEBUG
  ATfprintf(stderr, "Throwing exception\n");
#endif
  thrown = AN2_EXCEPTION;
  ACC_set_raised_data(data);
  //  localFail();
  fail();
}
Beispiel #24
0
static void char2buf(char c)
{
    if(offset >= bufsize)
        ATfprintf(stderr, "Buffer overflow, ignoring %c\n", c);
    else {
        BUF[offset++] = c;
        BUF[offset] = '\0';
    }
}
Beispiel #25
0
AN2Data ACC_catch_exception() {
#ifdef DEBUG
  ATfprintf(stderr, "Catching exception\n");
#endif
  if (thrown == AN2_EXCEPTION)
    return ACC_get_raised_data();
  //localFail();
  fail();
}  
Beispiel #26
0
/*
 *  This handler is invoked by the toolbus module whenever an event is received
 *  from the toolbus.
 */
ATerm toolbus_handler( int tb_conn, ATerm input )
{
  ATerm rv = NULL;  /* return value, gets sent back to the toolbus */
  ATerm myterm;    /* general purpose */
  ATerm myterm2;    /* general purpose */
  char html_buf[HTML_BUF_SIZE];
  char *html_text;
    
  ATfprintf( stderr, "mod_toolbus: toolbus_handler(): received: %t\n", 
    input );
  fflush( stderr );
  
  /*
   *  match input from toolbus
   */
   
  /* toolbus asked for a signature check */ 
  if(ATmatch(input, "rec-do(signature(<term>,<term>))", &myterm, &myterm2))
  {
    ATfprintf( stderr, "mod_toolbus: toolbus_handler(): signature check\n" );
    fflush( stderr );
    rv =  NULL;                         /* we don't do a signature check */
  }
  /* receive ack of an event we sent (that's how we initiated our session) */
  else if( ATmatch( input, "rec-ack-event(<term>)", &myterm ) )
  {
    ATfprintf( stderr, "mod_toolbus: toolbus_handler(): rec-ack-event: %t\n",
      myterm );
    fflush( stderr );
    rv = NULL;
  }
  /* toolbus asked us to display an HTML page encoded as an ATerm  */
  else if( ATmatch( input, "rec-do(reply-html(<term>))", &myterm ) )
  {
    ATfprintf(stderr, "mod_toolbus: toolbus_handler(): rec-do(reply-html())\n" );
    ATparseHTML( myterm, html_buf, HTML_BUF_SIZE );
    ap_rprintf( global_http_req, html_buf );  
    global_is_tb_session_done ++;  /* after this we're done */
    rv = NULL;
  }
  else if( ATmatch( input, "rec-do(reply-text(<str>))", &html_text ) )
  {
    ATfprintf(stderr, "mod_toolbus: toolbus_handler(): rec-do(reply-text())\n" );
    ap_rprintf( global_http_req, html_text );  
    global_is_tb_session_done ++;  /* after this we're done */
    rv = NULL;
  }
  else   /* default response */
  {
    global_is_tb_session_done ++;
    ATfprintf( stderr, "mod_toolbus: toolbus_handler(): event: default\n" );
    fflush( stderr );
    rv = NULL;
  }
  ATfprintf( stderr, "mod_toolbus: toolbus_handler(): leaving...\n" );
  fflush( stderr );
  return rv;
}
ATerm SSL_getPlaceholder(ATerm t) {
    int type = ATgetType(t);
    if(type != AT_PLACEHOLDER) {
      ATfprintf(stderr, "[error] SSL_getPlaceholder: not a placeholder: %t \n", t);
      _fail(t);
    } else {
	ATermPlaceholder ph = (ATermPlaceholder) t;
	ATerm result = ATgetPlaceholder(ph);
	return result;
    }
}
Beispiel #28
0
AN2Data ACC_catch_failure() {
#ifdef DEBUG
  ATfprintf(stderr, "Catching failure\n");
#endif
  if (thrown == AN2_FAILURE)
 /* This is strange: without returning anything I get a segmentation
  * fault in the choice point library (_memcpy_by8). */
    return NULL;
  //localFail();
  fail();
}
Beispiel #29
0
void HFdump(struct HFnode *tree, int d){
   int i;

   if(tree!=NULL){
      if (tree->low==NULL && tree->high==NULL){
         if (tree->term==NULL)
            ATfprintf(stderr," (%d) Term NULL\n", tree->frequency);
         else
            ATfprintf(stderr," (%d) Term %t\n", tree->frequency, tree->term);
      } else {
         fprintf(stderr," (%ld)\n", tree->frequency);
         for(i=0;i<d;i++)fprintf(stderr," ");
         fprintf(stderr,"0");
         HFdump(tree->low,d+1);
         for(i=0;i<d;i++)fprintf(stderr," ");
         fprintf(stderr,"1");
         HFdump(tree->high,d+1);
      }
   }

}
Beispiel #30
0
ATerm SSL_read_term_from_string(ATerm str_term) {
    if(!ATisString(str_term)) return NULL;
    const char* str = AT_getString(str_term);
    ATerm result = ATreadFromString(str);

    if(result == NULL) {
        ATfprintf(stderr, "not a valid term\n");
        _fail(str_term);
    }

    return result;
}