示例#1
0
static int g_iofile(lua_State * L, int f, const char *mode)
{
    if (!lua_isnoneornil(L, 1)) {
        const char *filename = lua_tostring(L, 1);
        if (filename) {
            FILE **pf = newfile(L);
            *pf = fopen(filename, mode);
            if (*pf == NULL) {
                fileerror(L, 1, filename);
            } else {
              if (mode[0]=='r') 
                recorder_record_input(filename);
              else
                recorder_record_output(filename);
            }
        } else {
            tofile(L);          /* check that it's a valid file handle */
            lua_pushvalue(L, 1);
        }
        lua_rawseti(L, LUA_ENVIRONINDEX, f);
    }
    /* return current value */
    lua_rawgeti(L, LUA_ENVIRONINDEX, f);
    return 1;
}
示例#2
0
static int io_open (lua_State *L) {
  const char *filename = luaL_checkstring(L, 1);
  const char *mode = luaL_optstring(L, 2, "r");
  LStream *p = newfile(L);
  int i = 0;
  /* check whether 'mode' matches '[rwa]%+?b?' */
  if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL &&
       (mode[i] != '+' || ++i) &&  /* skip if char is '+' */
       (mode[i] != 'b' || ++i) &&  /* skip if char is 'b' */
       (mode[i] == '\0')))
    return luaL_error(L, "invalid mode " LUA_QS
                         " (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
  p->f = fopen(filename, mode);
  if (p->f == NULL) {
      return luaL_fileresult(L, 0, filename) ;
  } else {
#ifdef WIN32
      _setmode (fileno (p->f), _O_BINARY);
#endif
      if (mode[0]=='r') 
	  recorder_record_input(filename);
      else
	  recorder_record_output(filename);
      return 1;
  }
}
示例#3
0
static int io_open(lua_State * L)
{
    const char *filename = luaL_checkstring(L, 1);
    const char *mode = luaL_optstring(L, 2, "rb");
    FILE **pf = newfile(L);
    *pf = fopen(filename, mode);
    if (*pf == NULL)
        return pushresult(L, 0, filename);
    if (mode[0]=='r') 
        recorder_record_input(filename);
    else
        recorder_record_output(filename);
    return 1;
}
示例#4
0
static void opencheck (lua_State *L, const char *fname, const char *mode) {
  LStream *p = newfile(L);
  p->f = fopen(fname, mode);
  if (p->f == NULL) {
    luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno));
  } else {
#ifdef WIN32
    _setmode (fileno (p->f), _O_BINARY);
#endif
    if (mode[0]=='r') 
       recorder_record_input(fname);
    else
       recorder_record_output(fname);
  }
}
示例#5
0
boolean
open_output (FILE **f_ptr, const_string fopen_mode)
{
    string fname;
    boolean absolute = kpse_absolute_p(nameoffile+1, false);

    /* If we have an explicit output directory, use it. */
    if (output_directory && !absolute) {
        fname = concat3(output_directory, DIR_SEP_STRING, nameoffile + 1);
    } else {
        fname = nameoffile + 1;
    }

    /* Is the filename openable as given?  */
    *f_ptr = fopen (fname, fopen_mode);

    if (!*f_ptr) {
        /* Can't open as given.  Try the envvar.  */
        string texmfoutput = kpse_var_value("TEXMFOUTPUT");

        if (texmfoutput && *texmfoutput && !absolute) {
            if (fname != nameoffile + 1)
                free(fname);
            fname = concat3(texmfoutput, DIR_SEP_STRING, nameoffile+1);
            *f_ptr = fopen(fname, fopen_mode);
        }
    }
    /* If this succeeded, change nameoffile accordingly.  */
    if (*f_ptr) {
        if (fname != nameoffile + 1) {
            free (nameoffile);
            namelength = strlen (fname);
            nameoffile = xmalloc (namelength + 2);
            strcpy (nameoffile + 1, fname);
        }
        recorder_record_output (fname);
    }
    if (fname != nameoffile +1)
        free(fname);
    return *f_ptr != NULL;
}