Beispiel #1
0
/* Add the IN->OUT mapping to TBL.  OUT is potentially stored in the table.
   IN is used only here, so it need not be kept live afterwards.  */
static void
add_bytes (struct convtable *tbl, const struct charseq *in, struct charseq *out)
{
  int n = 0;
  unsigned int byte;

  assert (in->nbytes > 0);

  byte = ((unsigned char *) in->bytes)[n];
  while (n + 1 < in->nbytes)
    {
      if (is_term (tbl, byte) || tbl->val[byte].sub == NULL)
	{
	  /* Note that we simply ignore a definition for a byte sequence
	     which is also the prefix for a longer one.  */
	  clear_term (tbl, byte);
	  tbl->val[byte].sub =
	    (struct convtable *) xcalloc (1, sizeof (struct convtable));
	}

      tbl = tbl->val[byte].sub;

      byte = ((unsigned char *) in->bytes)[++n];
    }

  /* Only add the new sequence if there is none yet and the byte sequence
     is not part of an even longer one.  */
  if (! is_term (tbl, byte) && tbl->val[byte].sub == NULL)
    {
      set_term (tbl, byte);
      tbl->val[byte].out = out;
    }
}
Beispiel #2
0
IC u32	IReader::advance_term_string()
{
	u32 sz		= 0;
	char *src 	= (char *) data;
	while (!eof()) {
        Pos++;
        sz++;
		if (!eof()&&is_term(src[Pos])) 
		{
        	while(!eof() && is_term(src[Pos])) 
				Pos++;
			break;
		}
	}
    return sz;
}
Beispiel #3
0
static int
process_block (struct convtable *tbl, char *addr, size_t len, FILE *output)
{
  size_t n = 0;

  while (n < len)
    {
      struct convtable *cur = tbl;
      unsigned char *curp = (unsigned char *) addr;
      unsigned int byte = *curp;
      int cnt;
      struct charseq *out;

      while (! is_term (cur, byte))
	if (cur->val[byte].sub == NULL)
	  {
	    /* This is an invalid sequence.  Skip the first byte if we are
	       ignoring errors.  Otherwise punt.  */
	    if (! omit_invalid)
	      {
		error (0, 0, _("illegal input sequence at position %Zd"), n);
		return -1;
	      }

	    n -= curp - (unsigned char *) addr;

	    byte = *(curp = (unsigned char *) ++addr);
	    if (++n >= len)
	      /* All converted.  */
	      return 0;

	    cur = tbl;
	  }
	else
	  {
	    cur = cur->val[byte].sub;

	    if (++n >= len)
	      {
		error (0, 0, _("\
incomplete character or shift sequence at end of buffer"));
		return -1;
	      }

	    byte = *++curp;
	  }

      /* We found a final byte.  Write the output bytes.  */
      out = cur->val[byte].out;
      for (cnt = 0; cnt < out->nbytes; ++cnt)
	fputc_unlocked (out->bytes[cnt], output);

      addr = (char *) curp + 1;
      ++n;
    }
/* PUBLIC */
void sb_xml_write_clause_jmap(String_buf sb, Topform c, I3list map)
{
  sb_append(sb, "\n  <clause id=\"");
  sb_append_id(sb, c->id, map);
  sb_append(sb, "\"");
  if (c->justification && c->justification->type == GOAL_JUST)
    sb_append(sb, " type=\"goal\"");
  else if (c->justification && c->justification->type == DENY_JUST)
    sb_append(sb, " type=\"deny\"");
  else if (c->justification && c->justification->type == INPUT_JUST)
    sb_append(sb, " type=\"assumption\"");
  else if (c->justification && c->justification->type == CLAUSIFY_JUST)
    sb_append(sb, " type=\"clausify\"");
  else if (c->justification && c->justification->type == EXPAND_DEF_JUST)
    sb_append(sb, " type=\"expand_def\"");
  sb_append(sb, ">\n");
  if (c->compressed)
    sb_append(sb, "    <literal>clause_is_compressed</literal>\n");
  else {
    Literals lit;
    Term atts;
    if (c->literals == NULL) {
      sb_append(sb, "    <literal><![CDATA[\n      ");
      sb_append(sb, false_sym());
      sb_append(sb, "\n    ]]></literal>\n");
    }
    else {
      for (lit = c->literals; lit; lit = lit->next) {
	Term t = literal_to_term(lit);
	sb_append(sb, "    <literal><![CDATA[\n      ");
	sb_write_term(sb, t);
	sb_append(sb, "\n    ]]></literal>\n");
	zap_term(t);
      }
    }
    atts = attributes_to_term(c->attributes, attrib_sym());
    if (atts) {
      Term t = atts;
      while (is_term(t, attrib_sym(), 2)) {
	sb_append(sb, "    <attribute><![CDATA[\n      ");
	sb_write_term(sb, ARG(t,0));
	sb_append(sb, "\n    ]]></attribute>\n");
	t = ARG(t,1);
      }
      sb_append(sb, "    <attribute><![CDATA[\n      ");
      sb_write_term(sb, t);
      sb_append(sb, "\n    ]]></attribute>\n");
      zap_term(atts);
    }
  }
  sb_xml_write_just(sb, c->justification, map);
  
  sb_append(sb, "  </clause>\n");
}  /* sb_xml_write_clause_jmap */
static
Term next_interp(FILE *fp, BOOL mace4_file)
{
  if (mace4_file) {
    if (read_to_line(fp, "= MODEL ="))
      return read_term(fp, stderr);
    else
      return NULL;
  }
  else {
    Term t = read_term(fp, stderr);
    if (t == NULL)
      return NULL;
    else if (is_term(t, "terms", 1) ||
	     is_term(t, "list", 1) ||
	     end_of_list_term(t)) {
      zap_term(t);
      return next_interp(fp, FALSE);
    }
    else
      return t;
  }
}  /* next_interp */
/* PUBLIC */
BOOL term_to_int(Term t, int *result)
{
  if (CONSTANT(t)) {
    return str_to_int(sn_to_str(SYMNUM(t)), result); 
  }
  else if (is_term(t, "-", 1)) {
    if (!CONSTANT(ARG(t,0)))
      return FALSE;
    else {
      if (str_to_int(sn_to_str(SYMNUM(ARG(t,0))), result)) {
	*result = -(*result);
	return TRUE;
      }
      else return FALSE;
    }
  }
  else
    return FALSE;
}  /* term_to_int */
/* PUBLIC */
void flag_handler(FILE *fout, Term t, int unknown_action)
{
  int flag = str_to_flag_id(sn_to_str(SYMNUM(ARG(t,0))));
  if (flag == -1) {
    if (unknown_action == KILL_UNKNOWN) {
      fwrite_term_nl(fout, t);
      fwrite_term_nl(stderr, t);
      fatal_error("flag not recognized");
    }
    else if (unknown_action == WARN_UNKNOWN) {
      bell(stderr);
      fprintf(fout,   "WARNING, flag not recognized: ");
      fwrite_term_nl(fout,   t);
      fprintf(stderr, "WARNING, flag not recognized: ");
      fwrite_term_nl(stderr, t);
    }
    else if (unknown_action == NOTE_UNKNOWN) {
      fprintf(fout,   "NOTE: flag not recognized: ");
      fwrite_term_nl(fout,   t);
    }
  }
  else
    update_flag(fout, flag, is_term(t, "set", 1));
}  /* flag_handler */
static
Plist read_tptp_file(FILE *fp)
{
  Plist formulas = NULL;
  Term t = read_term(fp, stderr);

  while (t != NULL) {
    if (is_term(t, "include", 1) || is_term(t, "include", 2))
      formulas = plist_cat(formulas, tptp_include(t));
    else if (is_term(t, "cnf", 3) || is_term(t, "cnf", 4))
      formulas = plist_prepend(formulas, tptp_formula(t));
    else if (is_term(t, "fof", 3) || is_term(t, "fof", 4))
      formulas = plist_prepend(formulas, tptp_formula(t));
    else {
      p_term(t);
      fatal_error("read_tptp_file: unknown term");
    }

    zap_term(t);
    t = read_term(stdin, stderr);
  }
  return reverse_plist(formulas);
}  /* read_tptp_file */
int main(int argc, char **argv)
{
  FILE *head_fp;
  Ilist just;
  Plist demodulators, p;
  Mindex idx;
  Term t;
  int rewritten = 0;
  BOOL verbose = string_member("verbose", argv, argc);;

  if (string_member("help", argv, argc) ||
      string_member("-help", argv, argc) ||
      argc < 2) {
    printf("\n%s, version %s, %s\n",PROGRAM_NAME,PROGRAM_VERSION,PROGRAM_DATE);
    printf("%s", Help_string);
    exit(1);
  }

  init_standard_ladr();

  head_fp = fopen(argv[1], "r");
  if (head_fp == NULL)
    fatal_error("demodulator file can't be opened for reading");

  t = read_commands(head_fp, stderr, verbose, KILL_UNKNOWN);

  if (!is_term(t, "clauses", 1) && !is_term(t, "formulas", 1))
    fatal_error("formulas(demodulators) not found");

  /* Read list of demodulators. */

  demodulators = read_clause_list(head_fp, stderr, TRUE);

  fclose(head_fp);

  /* AC-canonicalize and index the demodulators. */

  if (assoc_comm_symbols() || comm_symbols())
    idx = mindex_init(DISCRIM_WILD, BACKTRACK_UNIF, 0);
  else
    idx = mindex_init(DISCRIM_BIND, ORDINARY_UNIF, 0);

  for (p = demodulators; p != NULL; p = p->next) {
    /* assume positive equality unit */
    Topform d = p->v;
    Literals lit = d->literals;
    Term alpha = lit->atom->args[0];
    mark_oriented_eq(lit->atom);  /* don not check for termination */
    if (assoc_comm_symbols())
      ac_canonical(lit->atom, -1);
    mindex_update(idx, alpha, INSERT);
  }

  if (verbose)
    fwrite_clause_list(stdout, demodulators, "demodulators", CL_FORM_BARE);

  /* Read and demodulate terms. */

  t = read_term(stdin, stderr);

  while (t != NULL) {
    rewritten++;
    if (verbose) {
      fprintf(stdout, "\nBefore:   "); fwrite_term_nl(stdout, t);
    }

    if (assoc_comm_symbols())
      ac_canonical(t, -1);
    just = NULL;
    t = demodulate(t, idx, &just, FALSE);

    if (verbose)
      fprintf(stdout, "After:    ");

    fwrite_term_nl(stdout, t);
    fflush(stdout);

    zap_ilist(just);
    zap_term(t);
    t = read_term(stdin, stderr);
  }

  printf("%% %s %s: rewrote %d terms with %d rewrite steps in %.2f seconds.\n",
	 PROGRAM_NAME, argv[1], rewritten, demod_rewrites(), user_seconds());
    
  exit(0);

}  /* main */
/* PUBLIC */
Term read_commands(FILE *fin, FILE *fout, BOOL echo, int unknown_action)
{
  Term t = read_term(fin, fout);
  BOOL go = (t != NULL);

  while (go) {
    BOOL already_echoed = FALSE;
    /************************************************************ set, clear */
    if (is_term(t, "set", 1) || is_term(t, "clear", 1)) {
      if (echo) {
	fwrite_term_nl(fout, t);
	already_echoed = TRUE;
      }
      flag_handler(fout, t, unknown_action);
    }
    else if (is_term(t, "assign", 2)) {
      /************************************************************** assign */
      if (echo) {
	fwrite_term_nl(fout, t);
	already_echoed = TRUE;
      }
      parm_handler(fout, t, unknown_action);
    }
    else if (is_term(t, "assoc_comm", 1) ||
             is_term(t, "commutative", 1)) {
      /************************************************************ AC, etc. */
      Term f = ARG(t,0);
      if (!CONSTANT(f)) {
	bell(stderr);
	fwrite_term_nl(fout, t);
	fwrite_term_nl(stderr, t);
	fatal_error("argument must be symbol only");
      }
      else {
	if (is_term(t, "assoc_comm", 1))
	  set_assoc_comm(sn_to_str(SYMNUM(f)), TRUE);
	else
	  set_commutative(sn_to_str(SYMNUM(f)), TRUE);
      }
    }
    else if (is_term(t, "op", 3)) {
      /****************************************************************** op */
      /* e.g., op(300, infix, +); */
      Term prec_term = ARG(t,0);
      Term type_term = ARG(t,1);
      Term symb_term = ARG(t,2);
      int prec;
      BOOL ok = term_to_int(prec_term, &prec);
      if (!ok || prec < MIN_PRECEDENCE || prec > MAX_PRECEDENCE) {
	bell(stderr);
	fwrite_term_nl(fout, t);
	fwrite_term_nl(stderr, t);
	fatal_error("bad precedence in op command");
      }
      else if (proper_listterm(symb_term)) {
	while (cons_term(symb_term)) {
	  process_op(fout, t, prec, type_term, ARG(symb_term, 0));
	  symb_term = ARG(symb_term, 1);
	}
      }
      else
	process_op(fout, t, prec, type_term, symb_term);
    }
    else if (is_term(t, "lex", 1)) {
      /***************************************************************** lex */
      Plist p = listterm_to_tlist(ARG(t,0));
      if (p == NULL) {
	bell(stderr);
	fwrite_term_nl(fout, t);
	fwrite_term_nl(stderr, t);
	fatal_error("lex command must contain a proper list, e.g., [a,b,c]");
      }
      else {
	preliminary_precedence(p);
	zap_plist(p);
      }
    }
    else {
      /******************************************************** unrecognized */
      /* return this unknown term */
      go = FALSE;
    }

    if (go) {
      if (echo && !already_echoed)
	fwrite_term_nl(fout, t);
      zap_term(t);
      t = read_term(fin, fout);
      go = (t != NULL);
    }
  }
  return t;
}  /* read_commands */
Beispiel #11
0
size_t
csv_parse(struct csv_parser *p, const void *s, size_t len, void (*cb1)(void *, size_t, void *), void (*cb2)(int c, void *), void *data)
{
  unsigned const char *us = s;  /* Access input data as array of unsigned char */
  unsigned char c;              /* The character we are currently processing */
  size_t pos = 0;               /* The number of characters we have processed in this call */

  /* Store key fields into local variables for performance */
  unsigned char delim = p->delim_char;
  unsigned char quote = p->quote_char;
  int (*is_space)(unsigned char) = p->is_space;
  int (*is_term)(unsigned char) = p->is_term;
  int quoted = p->quoted;
  int pstate = p->pstate;
  size_t spaces = p->spaces;
  size_t entry_pos = p->entry_pos;


  if (!p->entry_buf && pos < len) {
    /* Buffer hasn't been allocated yet and len > 0 */
    if (csv_increase_buffer(p) != 0) { 
      p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos;
      return pos;
    }
  }

  while (pos < len) {
    /* Check memory usage, increase buffer if neccessary */
    if (entry_pos == ((p->options & CSV_APPEND_NULL) ? p->entry_size - 1 : p->entry_size) ) {
      if (csv_increase_buffer(p) != 0) {
        p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos;
        return pos;
      }
    }

    c = us[pos++];

    switch (pstate) {
      case ROW_NOT_BEGUN:
      case FIELD_NOT_BEGUN:
        if ((is_space ? is_space(c) : c == CSV_SPACE || c == CSV_TAB) && c!=delim) { /* Space or Tab */
          continue;
        } else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) { /* Carriage Return or Line Feed */
          if (pstate == FIELD_NOT_BEGUN) {
            SUBMIT_FIELD(p);
            SUBMIT_ROW(p, (unsigned char)c); 
          } else {  /* ROW_NOT_BEGUN */
            /* Don't submit empty rows by default */
            if (p->options & CSV_REPALL_NL) {
              SUBMIT_ROW(p, (unsigned char)c);
            }
          }
          continue;
        } else if (c == delim) { /* Comma */
          SUBMIT_FIELD(p);
          break;
        } else if (c == quote) { /* Quote */
          pstate = FIELD_BEGUN;
          quoted = 1;
        } else {               /* Anything else */
          pstate = FIELD_BEGUN;
          quoted = 0;
          SUBMIT_CHAR(p, c);
        }
        break;
      case FIELD_BEGUN:
        if (c == quote) {         /* Quote */
          if (quoted) {
            SUBMIT_CHAR(p, c);
            pstate = FIELD_MIGHT_HAVE_ENDED;
          } else {
            /* STRICT ERROR - double quote inside non-quoted field */
            if (p->options & CSV_STRICT) {
              p->status = CSV_EPARSE;
              p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos;
              return pos-1;
            }
            SUBMIT_CHAR(p, c);
            spaces = 0;
          }
        } else if (c == delim) {  /* Comma */
          if (quoted) {
            SUBMIT_CHAR(p, c);
          } else {
            SUBMIT_FIELD(p);
          }
        } else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) {  /* Carriage Return or Line Feed */
          if (!quoted) {
            SUBMIT_FIELD(p);
            SUBMIT_ROW(p, (unsigned char)c);
          } else {
            SUBMIT_CHAR(p, c);
          }
        } else if (!quoted && (is_space? is_space(c) : c == CSV_SPACE || c == CSV_TAB)) { /* Tab or space for non-quoted field */
            SUBMIT_CHAR(p, c);
            spaces++;
        } else {  /* Anything else */
          SUBMIT_CHAR(p, c);
          spaces = 0;
        }
        break;
      case FIELD_MIGHT_HAVE_ENDED:
        /* This only happens when a quote character is encountered in a quoted field */
        if (c == delim) {  /* Comma */
          entry_pos -= spaces + 1;  /* get rid of spaces and original quote */
          SUBMIT_FIELD(p);
        } else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) {  /* Carriage Return or Line Feed */
          entry_pos -= spaces + 1;  /* get rid of spaces and original quote */
          SUBMIT_FIELD(p);
          SUBMIT_ROW(p, (unsigned char)c);
        } else if (is_space ? is_space(c) : c == CSV_SPACE || c == CSV_TAB) {  /* Space or Tab */
          SUBMIT_CHAR(p, c);
          spaces++;
        } else if (c == quote) {  /* Quote */
          if (spaces) {
            /* STRICT ERROR - unescaped double quote */
            if (p->options & CSV_STRICT) {
              p->status = CSV_EPARSE;
              p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos;
              return pos-1;
            }
            spaces = 0;
            SUBMIT_CHAR(p, c);
          } else {
            /* Two quotes in a row */
            pstate = FIELD_BEGUN;
          }
        } else {  /* Anything else */
          /* STRICT ERROR - unescaped double quote */
          if (p->options & CSV_STRICT) {
            p->status = CSV_EPARSE;
            p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos;
            return pos-1;
          }
          pstate = FIELD_BEGUN;
          spaces = 0;
          SUBMIT_CHAR(p, c);
        }
        break;
     default:
       break;
    }
  }
  p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos;
  return pos;
}
/* PUBLIC */
BOOL false_term(Term t)
{
  return is_term(t, false_sym(), 0);
}  /* false_term */
/* PUBLIC */
BOOL true_term(Term t)
{
  return is_term(t, true_sym(), 0);
}  /* true_term */
Beispiel #14
0
/* Takes as input an arbitrary path.  Fixes up the path by:
 1. Removing consecutive slashes
 2. Removing trailing slashes
 3. Making the path absolute if it wasn't already
 4. Removing "." in the path
 5. Removing ".." entries in the path (and the directory above them)
 */
void _fixpath(const char *in, char *out) {
    const char *ip = in;
    char *op = out;

    /* Convert ~ to the HOME environment variable */
    if (*ip == '~' && (is_slash(ip[1]) || !ip[1])) {
        const char *home = getenv("HOME");
        if (home) {
            strcpy(op, home);
            op += strlen(op);
            ip++;
            if (!*ip)
                return;
        }
    }

    /* Convert relative path to absolute */
    if (!is_slash(*ip)) {
        getcurdir(0, op);
        op += strlen(op);
    }

#if defined(TVOSf_QNX4)
    /* Skip the first slashes, which are a node number part */
    /* Full QNX4 pathname is //node/dirpath/filename        */
    if ((ip==in) && (is_slash(*ip)) && (is_slash(*(ip+1))))
    {
        *op=*ip;
        ip++;
        op++;
    }
#endif // TVOSf_QNX4
    /* Step through the input path */
    while (*ip) {
        /* Skip input slashes */
        if (is_slash(*ip)) {
            ip++;
            continue;
        }

        /* Skip "." and output nothing */
        if (*ip == '.' && is_term(*(ip + 1))) {
            ip++;
            continue;
        }

        /* Skip ".." and remove previous output directory */
        if (*ip == '.' && *(ip + 1) == '.' && is_term(*(ip + 2))) {
            ip += 2;
            /* Don't back up over root '/' */
            if (op > out)
                /* This requires "/" to follow drive spec */
                while (!is_slash(*--op))
                    ;
            continue;
        }

        /* Copy path component from in to out */
        *op++ = '/';
        while (!is_term(*ip))
            *op++ = *ip++;
    }

    /* If root directory, insert trailing slash */
    if (op == out)
        *op++ = '/';

    /* Null terminate the output */
    *op = '\0';

}
/* PUBLIC */
BOOL is_constant(Term t, char *str)
{
  return is_term(t, str, 0);
}  /* is_constant */
Beispiel #16
0
/* Takes as input an arbitrary path.  Fixes up the path by:
 1. Removing consecutive slashes
 2. Removing trailing slashes
 3. Making the path absolute if it wasn't already
 4. Removing "." in the path
 5. Removing ".." entries in the path (and the directory above them)
 6. Adding a drive specification if one wasn't there
 7. Converting all slashes to '/'
 */
void
_fixpath(const char *in, char *out)
{
    int drive_number = 0;
    const char *ip = in;
    char *op = out;
    int unc = CLY_IsUNC(in); /* is a UNC pathname */

    /* Add drive specification to output string */
    if (((*ip >= 'a' && *ip <= 'z') ||
            (*ip >= 'A' && *ip <= 'Z'))
            && (*(ip + 1) == ':'))
    {
        if (*ip >= 'a' && *ip <= 'z')
        {
            drive_number = *ip - 'a';
            *op++ = *ip++;
        }
        else
        {
            drive_number = *ip - 'A';
            *op++ = (char)(drive_number + 'a');
            ++ip;
        }
        *op++ = *ip++;
    }
    else if (!unc)
    {
        drive_number = __fp_getdisk();
        *op++ = (char)(drive_number + 'a');
        *op++ = ':';
    }

    /* Convert relative path to absolute */
    if (!is_slash(*ip))
    {
        *op++ = '/';
        op = __fp_getcurdir(op, drive_number);
    }

    /* Handle UNC path */
    if (unc)
    {
        *op++ = *ip++;
    }

    /* Step through the input path */
    while (*ip)
    {
        /* Skip input slashes */
        if (is_slash(*ip))
        {
            ip++;
            continue;
        }

        /* Skip "." and output nothing */
        if (*ip == '.' && is_term(*(ip + 1)))
        {
            ip++;
            continue;
        }

        /* Skip ".." and remove previous output directory */
        if (*ip == '.' && *(ip + 1) == '.' && is_term(*(ip + 2)))
        {
            ip += 2;
            /* Don't back up over drive spec */
            if (op > out + 2)
            {
                /* This requires "/" to follow drive spec */
                --op;
                while (!is_slash(*op)) --op;
            }
            continue;
        }

        /* Copy path component from in to out */
        *op++ = '/';
        while (!is_term(*ip)) *op++ = *ip++;
    }

    /* If root directory, insert trailing slash */
    if (op == out + 2) *op++ = '/';

    /* Null terminate the output */
    *op = '\0';

    /* Convert backslashes to slashes */
    for (op = out; *op; op++)
        if (*op == '\\') *op = '/';
}
Beispiel #17
0
int CLY_IsUNC(const char* path)
{
  if (!is_slash(path[0]) || !is_slash(path[1]) || is_term(path[2]))
    return 0;
  return 1;
}