예제 #1
0
파일: streams.c 프로젝트: jnorthrup/yap-6.3
char *Yap_guessFileName(int f, int sno, char *nameb, size_t max) {
#if __linux__
  char path[256];
  if (snprintf(path, 255, "/proc/self/fd/%d", f) && readlink(path, nameb, max))
    return nameb;
#elif __APPLE__
  if (fcntl(f, F_GETPATH, nameb) != -1) {
    return nameb;
  }
#elif __WIN32_
  FILE_NAME_INFO *fni = (FILE_NAME_INFO *)malloc(sizeof(FILE_NAME_INFO) +
                                                 sizeof(WCHAR) * MAXPATHLEN);
  HANDLE handle = (HANDLE)_get_osfhandle(f);
  if (GetFileInformationByHandleEx(handle, FileNameInfo, &fni, max)) {
    int i;
    char *ptr = nameb;
    for (i = 0; i < fni->FileNameLength; i++)
      *ptr = _PL__utf8_put_char(ptr, fni->FileName[i]);
    *ptr = '\0';
    return nameb;
  }
#endif
  if (!StreamName(sno)) {
    return NULL;
  }
  return RepAtom(AtomOfTerm(StreamName(sno)))->StrOfAE;
}
예제 #2
0
char *Yap_guessFileName(FILE *file, int sno, char *nameb, size_t max) {
  size_t maxs = max(255, max);
  if (!nameb) {
    nameb = malloc(maxs + 1);
  }
  if (!file) {
    strncpy(nameb, "memory buffer", maxs);
    return nameb;
  }
  int f = fileno(file);
  if (f < 0) {
    strcpy(nameb, "???");
    return nameb;
  }

#if __linux__
  char path[256];
  if (snprintf(path, 255, "/proc/self/fd/%d", f) && readlink(path, nameb, max))
    return nameb;
#elif __APPLE__
  if (fcntl(f, F_GETPATH, nameb) != -1) {
    return nameb;
  }
#else
  TCHAR path[MAX_PATH + 1];
  if (!GetFullPathName(path, MAX_PATH, path, NULL))
    return NULL;
  else {
    int i;
    unsigned char *ptr = (unsigned char *)nameb;
    for (i = 0; i < strlen(path); i++)
      ptr += put_utf8(ptr, path[i]);
    *ptr = '\0';
    return nameb;
  }
#endif
  if (!StreamName(sno)) {
    return NULL;
  }
  return RepAtom(AtomOfTerm(StreamName(sno)))->StrOfAE;
}
예제 #3
0
파일: iopreds.c 프로젝트: davidvaz/yap-udi
static Term
syntax_error (TokEntry * tokptr, IOSTREAM *st, Term *outp)
{
    CACHE_REGS
    Term info;
    int count = 0, out = 0;
    Int start, err = 0, end;
    Term tf[7];
    Term *error = tf+3;
    CELL *Hi = H;

    /* make sure to globalise variable */
    start = tokptr->TokPos;
    clean_vars(LOCAL_VarTable);
    clean_vars(LOCAL_AnonVarTable);
    while (1) {
        Term ts[2];
        if (H > ASP-1024) {
            tf[3] = TermNil;
            err = 0;
            end = 0;
            /* for some reason moving this earlier confuses gcc on solaris */
            H = Hi;
            break;
        }
        if (tokptr == LOCAL_toktide) {
            err = tokptr->TokPos;
            out = count;
        }
        info = tokptr->TokInfo;
        switch (tokptr->Tok) {
        case Name_tok:
        {
            Term t0[1];
            t0[0] = MkAtomTerm((Atom)info);
            ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomAtom,1),1,t0);
        }
        break;
        case Number_tok:
            ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomNumber,1),1,&(tokptr->TokInfo));
            break;
        case Var_tok:
        {
            Term t[3];
            VarEntry *varinfo = (VarEntry *)info;

            t[0] = MkIntTerm(0);
            t[1] = Yap_StringToList(varinfo->VarRep);
            if (varinfo->VarAdr == TermNil) {
                t[2] = varinfo->VarAdr = MkVarTerm();
            } else {
                t[2] = varinfo->VarAdr;
            }
            ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomGVar,3),3,t);
        }
        break;
        case String_tok:
        {
            Term t0 = Yap_StringToList((char *)info);
            ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomString,1),1,&t0);
        }
        break;
        case WString_tok:
        {
            Term t0 = Yap_WideStringToList((wchar_t *)info);
            ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomString,1),1,&t0);
        }
        break;
        case Error_tok:
        case eot_tok:
            break;
        case Ponctuation_tok:
        {
            char s[2];
            s[1] = '\0';
            if (Ord (info) == 'l') {
                s[0] = '(';
            } else  {
                s[0] = (char)info;
            }
            ts[0] = MkAtomTerm(Yap_LookupAtom(s));
        }
        }
        if (tokptr->Tok == Ord (eot_tok)) {
            *error = TermNil;
            end = tokptr->TokPos;
            break;
        } else if (tokptr->Tok != Ord (Error_tok)) {
            ts[1] = MkIntegerTerm(tokptr->TokPos);
            *error =
                MkPairTerm(Yap_MkApplTerm(FunctorMinus,2,ts),TermNil);
            error = RepPair(*error)+1;
            count++;
        }
        tokptr = tokptr->TokNext;
    }
    /* now we can throw away tokens, so we can unify and possibly overwrite TR */
    Yap_unify(*outp, MkVarTerm());
    if (IsVarTerm(*outp) && (VarOfTerm(*outp) > H || VarOfTerm(*outp) < H0)) {
        tf[0] = Yap_MkNewApplTerm(Yap_MkFunctor(AtomRead,1),1);
    } else {
        tf[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomRead,1),1,outp);
    }
    {
        Term t[3];

        t[0] = MkIntegerTerm(start);
        t[1] = MkIntegerTerm(err);
        t[2] = MkIntegerTerm(end);
        tf[1] = Yap_MkApplTerm(Yap_MkFunctor(AtomBetween,3),3,t);
    }
    tf[2] = MkAtomTerm(AtomHERE);
    tf[4] = MkIntegerTerm(out);
    tf[5] = MkIntegerTerm(err);
    tf[6] = StreamName(st);
    return(Yap_MkApplTerm(FunctorSyntaxError,7,tf));
}