예제 #1
0
static int pmain(lua_State* L)
{
 struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
 int argc=s->argc;
 char** argv=s->argv;
 const Proto* f;
 int i;
 if (!lua_checkstack(L,argc)) fatal("too many input files");
 for (i=0; i<argc; i++)
 {
  const char* filename=IS("-") ? NULL : argv[i];
  if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
 }
 f=combine(L,argc);
 if (listing) luaU_print(f,listing>1);
 if (dumping)
 {
  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  if (D==NULL) cannot("open");
  lua_lock(L);
  int result=luaU_dump_crosscompile(L,f,writer,D,stripping,target);
  lua_unlock(L);
  if (result==LUA_ERR_CC_INTOVERFLOW) fatal("value too big or small for target integer type");
  if (result==LUA_ERR_CC_NOTINTEGER) fatal("target lua_Number is integral but fractional value found");
  if (ferror(D)) cannot("write");
  if (fclose(D)) cannot("close");
 }
 return 0;
}
예제 #2
0
static int pmain(lua_State* L)
{
 struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
 int argc=s->argc;
 char** argv=s->argv;
 const Proto* f;
 int i;
 if (!lua_checkstack(L,argc)) fatal("too many input files");
 for (i=0; i<argc; i++)
 {
  const char* filename=IS("-") ? NULL : argv[i];
  if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
 }
 f=combine(L,argc);
 if (listing) luaU_print(f,listing>1);
 if (dumping)
 {
  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  if (D==NULL) cannot("open");
  lua_lock(L);
  luaU_dump(L,f,writer,D,stripping);
  lua_unlock(L);
  if (ferror(D)) cannot("write");
  if (fclose(D)) cannot("close");
 }
 return 0;
}
예제 #3
0
파일: luaopswap.c 프로젝트: fightx/luadec
int generateOp2op(const char* opcodes_def) {
    int i = 0, retval = 1;
    char left[32], right[32];
    int readcount = 0, linenum = 0;

    FILE* F = (opcodes_def == NULL) ? stdout : fopen(opcodes_def, "rt");
    if (F == NULL) cannot("open", opcodes_def);

    for (i = 0; i < OP2OP_SIZE; i++) {
        op2op[i] = i;
    }

    while ((readcount = fscanf(F, "%30s %30s\n", left, right)) != EOF) {
        linenum++;
        if (readcount == 2) {
            int li = getOpIndex(left);
            int ri = getOpIndex(right);
            if (0 <= li && li < OP2OP_SIZE && 0 <= ri && ri < OP2OP_SIZE) {
                op2op[li] = ri;
            } else {
                fprintf(stderr, " error in line %d : cannot find OpCode or OpNumber not in range(0 - %d).\n", linenum, OP2OP_SIZE);
                retval = 0;
                break;
            }
        } else if (readcount == 1 || readcount > 2) {
            fprintf(stderr, " error in line %d : One line must have 2 OpCode or OpNumber.\n", linenum);
            retval = 0;
            break;
        }
    }

    if (fclose(F)) cannot("close", opcodes_def);
    return retval;
}
예제 #4
0
int main(int argc, char* argv[])
{
 lua_State* L;
 Proto* f;
 int i=doargs(argc,argv);
 argc-=i; argv+=i;
 if (argc<=0) usage("no input files given",NULL);
 L=lua_open();
 luaB_opentests(L);
 for (i=0; i<argc; i++)
 {
  const char* filename=IS("-") ? NULL : argv[i];
  if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
 }
 f=combine(L,argc);
 if (listing) luaU_print(f);
 if (dumping)
 {
  FILE* D=fopen(output,"wb");
  if (D==NULL) cannot(output,"open","out");
  if (stripping) strip(L,f);
  luaU_dump(L,f,writer,D);
  if (ferror(D)) cannot(output,"write","out");
  fclose(D);
 }
 lua_close(L);
 return 0;
}
예제 #5
0
파일: luaopswap.c 프로젝트: fightx/luadec
int main(int argc, char* argv[]) {
    const char *input_luac, *opcodes_def;
    int i;
    lua_State* L;
    Proto* f;
    FILE* D;

    gargc = argc;
    gargv = argv;
    i = doargs(argc,argv);
    argc -= i;
    argv += i;

    if (argc < 1) {
        usage("need 1 arguments at least", NULL);
    }

    L = lua_open();
    glstate = L;
    //luaB_opentests(L);

    input_luac = argv[0];
    if (luaL_loadfile(L, input_luac) != 0) {
        fatal(lua_tostring(L, -1));
    }
    f = toproto(L, -1);

    if (cmp_gen_lua || cmp_gen_luac) {
        Proto* input_proto = f;
        Proto* allopcodes_proto;
        const char* buff = cmp_gen_lua ? allopcodes_lua : allopcodes_luac;
        int bufflen = cmp_gen_lua ? allopcodes_lua_len : allopcodes_luac_len;
        if (luaL_loadbuffer(L, buff, bufflen, "allopcodes.lua") != 0) {
            fatal(lua_tostring(L, -1));
        }
        allopcodes_proto = toproto(L, -1);
        CompareAndGenOpcodes(input_proto, allopcodes_proto);
    } else {
        opcodes_def = (argv[1]) ? (argv[1]) : OPCODES_TXT;
        if (!generateOp2op(opcodes_def)) {
            fprintf(stderr, "opcodes.txt file: %s format error!", opcodes_def);
            return EXIT_FAILURE;
        }
        swapOpCode(f);

        D = (output == NULL) ? stdout : fopen(output, "wb");
        if (D == NULL) cannot("open", output);
        lua_lock(L);
        luaU_dump(L, f, writer, D, 0);
        lua_unlock(L);
        if (ferror(D)) cannot("write", output);
        if (fclose(D)) cannot("close", output);

        printf("%s generated!\n", output);
    }

    lua_close(L);
    return EXIT_SUCCESS;
}
예제 #6
0
static void load(lua_State *L, const char *name)
{
 Glue t;
 State S;
 FILE *f=fopen(name,"rb");
 if (f==NULL) cannot("open");
 if (fseek(f,-sizeof(t),SEEK_END)!=0) cannot("seek");
 if (fread(&t,sizeof(t),1,f)!=1) cannot("read");
 if (memcmp(t.sig,GLUESIG,GLUELEN)!=0) luaL_error(L,"no Lua program found in %s",name);
 if (fseek(f,t.size1,SEEK_SET)!=0) cannot("seek");
 S.f=f; S.size=t.size2;
 if (lua_load(L,myget,&S,"=",NULL)!=0) lua_error(L);
 fclose(f);
}
예제 #7
0
static int pmain(lua_State* L)
{
 struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
 int argc=s->argc;
 char** argv=s->argv;
 const Proto* f;
 int i;
 if (!lua_checkstack(L,argc)) fatal("too many input files");
 if (execute)
 {
  if (luaL_loadfile(L,execute)!=0) fatal(lua_tostring(L,-1));
  luaL_openlibs(L);
  lua_pushstring(L, execute);
  if (lua_pcall(L, 1, 1, 0)) fatal(lua_tostring(L,-1));
  if (!lua_isfunction(L, -1)) 
  {
   lua_pop(L,1);
   if(argc == 0) return 0;
   execute = NULL;
  }
 }
 for (i=0; i<argc; i++)
 {
  const char* filename=IS("-") ? NULL : argv[i];
  if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
 }
 f=combine(L,argc + (execute ? 1: 0), lookup);
 if (listing) luaU_print(f,listing>1);
 if (dumping)
 {
  int result;
  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  if (D==NULL) cannot("open");
  lua_lock(L);
  if (flash) 
  {
    result=dumpToFlashImage(L,f,writer, D, stripping, address, maxSize);
  } else
  {
    result=luaU_dump_crosscompile(L,f,writer,D,stripping,target);
  }  
  lua_unlock(L);
  if (result==LUA_ERR_CC_INTOVERFLOW) fatal("value too big or small for target integer type");
  if (result==LUA_ERR_CC_NOTINTEGER) fatal("target lua_Number is integral but fractional value found");
  if (ferror(D)) cannot("write");
  if (fclose(D)) cannot("close");
 }
 return 0;
}
예제 #8
0
파일: glue.c 프로젝트: Archs/luajit-aio
int main(int argc, char* argv[])
{
 Glue t= { GLUESIG, 0, 0 };
 FILE* f;
 if (argc<4)
 {
  fprintf(stderr,"usage: glue c.exe prog.lua prog.exe\n");
  return 1;
 }
 f=fopen(argv[3],"wb");
 if (f==NULL) cannot("open",argv[3]);
 t.size1=copy(argv[1],f,argv[3]);
 t.size2=copy(argv[2],f,argv[3]);
 t.sig[GLUETYP]= (argv[4]!=NULL) ? argv[4][0] : 'L';
 if (fwrite(&t,sizeof(t),1,f)!=1) cannot("write",argv[3]);
 if (fclose(f)!=0) cannot("close",argv[3]);
 return 0;
}
예제 #9
0
파일: glue.c 프로젝트: Archs/luajit-aio
static long copy(const char* name, FILE* out, const char* outname)
{
 FILE* f;
 long size;
 f=fopen(name,"rb");
 if (f==NULL) cannot("open",name);
 if (fseek(f,0,SEEK_END)!=0) cannot("seek",name);
 size=ftell(f);
 if (fseek(f,0,SEEK_SET)!=0) cannot("seek",name);
 for (;;)
 {
  char b[BUFSIZ];
  int n=fread(&b,1,sizeof(b),f);
  if (n==0) { if (ferror(f)) cannot("read",name); else break; }
  if (fwrite(&b,n,1,out)!=1) cannot("write",outname);
 }
 fclose(f);
 return size;
}
예제 #10
0
int main(int argc, char const* argv[])
{
    STU m;
    m.age = 10;
    strcpy(m.name,"xiaoyu");    cannot (m.name="xiaoyu")must use strcpy
    printf("age   and    name\n");
    printf("%-5d      %s\n",m.age,m.name);

    return 0;
}
예제 #11
0
static int pmain(lua_State* L)
{
	struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
	int argc=s->argc;
	char** argv=s->argv;
	int i;

	lua_pop(L,1);

	if (!lua_checkstack(L,argc)) fatal("too many input files");
	for (i=0; i<argc; i++)
	{
		const char* filename=IS("-") ? NULL : argv[i];
		if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));

		if (dumping)
		{
			FILE* D= (output==NULL) ? stdout : fopen(output,(i > 0 ? "ab" : "wb"));
			if (D==NULL) cannot("open");

			char* bytecode = 0L;
			size_t len = 0;
			wdata wd = {&len, &bytecode};

			if(lua_dump(L, write_dump, &wd)) fatal("failed to dump bytecode");

			fwrite(bytecode,len,1,D);

			lua_pop(L,3);

			if (ferror(D)) cannot("write");
			if (fclose(D)) cannot("close");
		}

	}
	return 0;
}
예제 #12
0
파일: luac.cpp 프로젝트: aappleby/Lumina
static int pmain(LuaThread* L)
{
 int argc=(int)lua_tointeger(L,1);
 char** argv=(char**)lua_touserdata(L,2);
 const LuaProto* f;
 int i;
 if (!lua_checkstack(L,argc)) fatal("too many input files");
 for (i=0; i<argc; i++)
 {
  const char* filename=IS("-") ? NULL : argv[i];
  if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
 }
 f=combine(L,argc);
 if (listing) luaU_print(f,listing>1);
 if (dumping)
 {
  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  if (D==NULL) cannot("open");
  luaU_dump(L,f,writer,D,stripping);
  if (ferror(D)) cannot("write");
  if (fclose(D)) cannot("close");
 }
 return 0;
}
예제 #13
0
파일: srlua.c 프로젝트: Chingliu/srlua
static void load(lua_State *L, const char *name)
{
 Glue t;
 State S;
 FILE *f=fopen(name,"rb");
 int c;
 if (f==NULL) cannot("open");
 if (fseek(f,-sizeof(t),SEEK_END)!=0) cannot("seek");
 if (fread(&t,sizeof(t),1,f)!=1) cannot("read");
 if (memcmp(t.sig,GLUESIG,GLUELEN)!=0) luaL_error(L,"no Lua program found in %s",name);
 if (fseek(f,t.size1,SEEK_SET)!=0) cannot("seek");
 S.f=f; S.size=t.size2;
 c=getc(f);
 if (c=='#')
  while (--S.size>0 && c!='\n') c=getc(f);
 else
  ungetc(c,f);
#if LUA_VERSION_NUM <= 501
 if (lua_load(L,myget,&S,"=")!=0) lua_error(L);
#else
 if (lua_load(L,myget,&S,"=",NULL)!=0) lua_error(L);
#endif
 fclose(f);
}
예제 #14
0
static int pmain(lua_State* L) {
  struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
  int argc=s->argc;
  char** argv=s->argv;
  Proto* f;
  int scripts=0;
  int i;
  if (!lua_checkstack(L,argc)) fatal("too many input files");
  lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
  luaL_openlibs(L);  /* open libraries */
  lua_gc(L, LUA_GCRESTART, 0);
  /* compile each script from command line into a Lua function. */
  for (i=0; i<argc; i++) {
    const char* filename=IS("-") ? NULL : argv[i];
    if(IS("-L")) break;
    if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
    scripts++;
  }
  /* compile each preload library from the command line into a Lua function. */
  for (i=0; i<preloads; i++) {
    char* filename=preload_libs[i];
    char* p;
    /* try loading library as if it is a normal file. */
    if (luaL_loadfile(L,filename)!=0) {
      /* try pre-loading library with 'require' module loading system. */
      lua_getglobal(L, "require");
      lua_pushstring(L, filename);
      lua_pushboolean(L, 1);
      lua_call(L, 2, 1);
      if (lua_iscfunction(L, -1)) { /* make sure it is not a C-Function. */
        lua_pop(L, 1);
        lua_pushfstring(L, "\nCan't preload C module: '%s'\n", filename);
        lua_concat(L, 2);  /* accumulate with error from luaL_findfile */
        fatal(lua_tostring(L,-1));
      }
      if (!lua_isfunction(L, -1)) { /* did we get an error? */
        lua_pushliteral(L, "\n");
        lua_concat(L, 3);  /* accumulate with error from luaL_findfile */
        fatal(lua_tostring(L,-1));
      } else {
        lua_remove(L, -2); /* remove error from luaL_findfile. */
      }
    } else {
      /* convert filename into package name. */
      p= filename + strlen(filename);
      for(;p >= filename; p--) {
        if(p[0] == '.') { /* Remove file extension. */
          p[0] = '\0';
          continue;
        }
        if(p[0] == '/') { /* Remove file path. */
          preload_libs[i] = p+1;
          break;
        }
      }
    }
  }
  /* generate a new Lua function to combine all of the compiled scripts. */
  f=combine(L, scripts);
  if (listing) luaU_print(f,listing>1);
  if (c_code && !parse_only) {
    FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
    if (D==NULL) cannot("open");
    lua_lock(L);
    slua_dumper_dump(D, output, L, f, stripping);
    lua_unlock(L);
    if (ferror(D)) cannot("write");
    if (fclose(D)) cannot("close");
  }
  if (dumping && !parse_only) {
    FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
    if (D==NULL) cannot("open");
    lua_lock(L);
    luaU_dump(L,f,writer,D,stripping);
    lua_unlock(L);
    if (ferror(D)) cannot("write");
    if (fclose(D)) cannot("close");
  }
  return 0;
}
예제 #15
0
파일: luareplace.c 프로젝트: ttgb/luadec
int main(int argc, char* argv[]) {
	lua_State* L;
	const char *destfile, *destnum, *srcfile, *srcnum;
	Proto *fdestroot, *fsrcroot, *fparent, *fdest, *fsrc;
	int cdest, csrc;
	char *realdestnum = NULL, *realsrcnum = NULL;
	FILE* D;
	int diff, i;

	gargc = argc;
	gargv = argv;
	i = doargs(argc,argv);
	argc -= i;
	argv += i;

	if (printfuncnum && argc < 1) {
		usage("need 1 arguments for -pn at least", NULL);
	}
	if (argc < 4) {
		usage("need 4 arguments at least", NULL);
	}

	L = lua_open();
	glstate = L;
	luaB_opentests(L);

	destfile = argv[0];
	destnum = argv[1];
	if (luaL_loadfile(L, destfile) != 0) {
		fatal(lua_tostring(L, -1));
	}
	fdestroot = toproto(L, -1);

	if (printfuncnum) {
		printf("%d\n",0);
		printFuncStructure(fdestroot, "  0_");
		lua_close(L);
		return EXIT_SUCCESS;
	}

	realdestnum = (char*)calloc(strlen(destnum)+1, sizeof(char));
	fparent = findParentFunction(fdestroot, destnum, &cdest, realdestnum);
	if (cdest < 0) {
		if (realdestnum) { free(realdestnum); realdestnum = NULL; }
		fatal("cannot find dest function");
	}
	if (fparent == NULL) {
		fdest = fdestroot;
	} else {
		fdest = fparent->p[cdest];
	}

	srcfile = argv[2];
	srcnum = argv[3];
	if (luaL_loadfile(L, srcfile) != 0) {
		fatal(lua_tostring(L, -1));
	}
	fsrcroot = toproto(L, -1);
	realsrcnum = (char*)calloc(strlen(srcnum)+1, sizeof(char));
	fsrc = findParentFunction(fsrcroot, srcnum, &csrc, realsrcnum);
	if (csrc < 0) {
		if (realdestnum) { free(realdestnum); realdestnum = NULL; }
		if (realsrcnum) { free(realsrcnum); realsrcnum = NULL; }
		fatal("cannot find src function");
	}
	if (fsrc == NULL) {
		fsrc = fsrcroot;
	} else {
		fsrc = fsrc->p[csrc];
	}

	if (!replace_sub){
		if (fparent == NULL) {
			if (realdestnum) { free(realdestnum); realdestnum = NULL; }
			if (realsrcnum) { free(realsrcnum); realsrcnum = NULL; }
			fatal("cannot use root as dest function");
		}
		fprintf(stderr, "Replacing %s %s with %s %s ...\n", destfile, realdestnum, srcfile, realsrcnum);
		diff = replaceFunction(fparent, cdest, fsrc);
		fprintf(stderr, "1 function replaced ok.\n");
	} else {
		fprintf(stderr, "Replacing sub functions of %s %s with those of %s %s ...\n", destfile, realdestnum, srcfile, realsrcnum);
		diff = replaceSubFunctions(fdest, fsrc);
		fprintf(stderr, "%d function replaced ok.", MIN(fdest->sizep, fsrc->sizep));
		if (fdest->sizep != fsrc->sizep) {
			fprintf(stderr, " The dest has %d sub functions, but the src has %d . Please have a check.\n", fdest->sizep, fsrc->sizep);
		} else {
			fprintf(stderr, "\n");
		}
	}

	if (realdestnum) { free(realdestnum); realdestnum = NULL; }
	if (realsrcnum) { free(realsrcnum); realsrcnum = NULL; }

	if (strict == 1 && diff > 0){
		fatal("strip mode on and incompatible functions found, stop writing output.");
	}

	D = (output == NULL) ? stdout : fopen(output, "wb");
	if (D == NULL) cannot("open");
	lua_lock(L);
	luaU_dump(L, fdestroot, writer, D, 0);
	lua_unlock(L);
	if (ferror(D)) cannot("write");
	if (fclose(D)) cannot("close");

	printf("%s generated!\n", output);

	lua_close(L);
	return EXIT_SUCCESS;
}