2010-09-03 21:50:35 -07:00
|
|
|
#include <cstring>
|
2009-07-26 10:07:13 +00:00
|
|
|
#include "ASTree.h"
|
|
|
|
|
2010-09-03 21:50:35 -07:00
|
|
|
#ifdef WIN32
|
|
|
|
# define PATHSEP '\\'
|
|
|
|
#else
|
|
|
|
# define PATHSEP '/'
|
|
|
|
#endif
|
|
|
|
|
2009-07-26 10:07:13 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-04-26 14:44:10 -07:00
|
|
|
const char* infile = nullptr;
|
|
|
|
for (int arg = 1; arg < argc; ++arg) {
|
|
|
|
if (strcmp(argv[arg], "-o") == 0) {
|
|
|
|
if (arg + 1 < argc) {
|
|
|
|
const char* filename = argv[++arg];
|
|
|
|
FILE* outfile = fopen(filename, "w");
|
|
|
|
if (!outfile) {
|
|
|
|
fprintf(stderr, "Error opening file '%s' for writing\n",
|
|
|
|
argv[arg]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pyc_output = outfile;
|
|
|
|
} else {
|
|
|
|
fputs("Option '-o' requires a filename\n", stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[arg], "--help") == 0 || strcmp(argv[arg], "-h") == 0) {
|
|
|
|
fprintf(stderr, "Usage: %s [options] input.pyc\n\n", argv[0]);
|
|
|
|
fputs("Options:\n", stderr);
|
|
|
|
fputs(" -o <filename> Write output to <filename> (default: stdout)\n", stderr);
|
|
|
|
fputs(" --help Show this help text and then exit\n", stderr);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
infile = argv[arg];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!infile) {
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs("No input file specified\n", stderr);
|
2009-07-26 10:07:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PycModule mod;
|
2018-01-28 14:33:26 -08:00
|
|
|
try {
|
2022-04-26 14:44:10 -07:00
|
|
|
mod.loadFromFile(infile);
|
2018-01-28 14:33:26 -08:00
|
|
|
} catch (std::exception& ex) {
|
2022-04-26 14:44:10 -07:00
|
|
|
fprintf(stderr, "Error loading file %s: %s\n", infile, ex.what());
|
2018-01-28 14:33:26 -08:00
|
|
|
return 1;
|
|
|
|
}
|
2009-08-03 23:13:50 +00:00
|
|
|
if (!mod.isValid()) {
|
2022-04-26 14:44:10 -07:00
|
|
|
fprintf(stderr, "Could not load file %s\n", infile);
|
2009-08-03 23:13:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-04-26 14:44:10 -07:00
|
|
|
const char* dispname = strrchr(infile, PATHSEP);
|
|
|
|
dispname = (dispname == NULL) ? infile : dispname + 1;
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs("# Source Generated with Decompyle++\n", pyc_output);
|
2011-10-23 19:04:06 -07:00
|
|
|
fprintf(pyc_output, "# File: %s (Python %d.%d%s)\n\n", dispname, mod.majorVer(), mod.minorVer(),
|
|
|
|
(mod.majorVer() < 3 && mod.isUnicode()) ? " Unicode" : "");
|
2018-01-28 14:33:26 -08:00
|
|
|
try {
|
|
|
|
decompyle(mod.code(), &mod);
|
|
|
|
} catch (std::exception& ex) {
|
2022-04-26 14:44:10 -07:00
|
|
|
fprintf(stderr, "Error decompyling %s: %s\n", infile, ex.what());
|
2018-01-28 14:33:26 -08:00
|
|
|
return 1;
|
|
|
|
}
|
2009-07-26 10:07:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|