forked from pyrite-dev/pmake
things
This commit is contained in:
parent
c89f96ce8a
commit
d951a0bceb
15 changed files with 381 additions and 90 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,5 +1,7 @@
|
||||||
/pmake*
|
/pmake*
|
||||||
*.o
|
*.o
|
||||||
|
/Makefile
|
||||||
|
/*.make
|
||||||
/src/compiler/*.c
|
/src/compiler/*.c
|
||||||
/src/action/*.c
|
/src/action/*.c
|
||||||
/src/js/*.c
|
/src/js/*.c
|
||||||
|
|
|
||||||
21
GNUmakefile
21
GNUmakefile
|
|
@ -13,6 +13,11 @@ ifeq ($(TARGET),NetBSD)
|
||||||
UNIX = 1
|
UNIX = 1
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(TARGET),Windows)
|
||||||
|
MINGW = 1
|
||||||
|
WINDOWS = 1
|
||||||
|
endif
|
||||||
|
|
||||||
### Stuff
|
### Stuff
|
||||||
ifeq ($(UNIX),1)
|
ifeq ($(UNIX),1)
|
||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
@ -23,11 +28,22 @@ ifeq ($(UNIX),1)
|
||||||
EXE =
|
EXE =
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(WINDOWS),1)
|
||||||
|
EXE = .exe
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(MINGW),1)
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -c
|
||||||
|
LDFLAGS =
|
||||||
|
LIBS =
|
||||||
|
endif
|
||||||
|
|
||||||
### Common flags
|
### Common flags
|
||||||
CFLAGS += -Iexternal/duktape -Iexternal/stb
|
CFLAGS += -Iexternal/duktape -Iexternal/stb
|
||||||
|
|
||||||
### Rules
|
### Rules
|
||||||
OBJS = src/main.o src/js.o
|
OBJS = src/main.o src/js.o src/js_console.o src/js_process.o src/js_fs.o
|
||||||
OBJS += external/duktape/duktape.o external/stb/stb_ds.o
|
OBJS += external/duktape/duktape.o external/stb/stb_ds.o
|
||||||
|
|
||||||
NAME = rev | cut -d/ -f1 | rev | cut -d. -f1
|
NAME = rev | cut -d/ -f1 | rev | cut -d. -f1
|
||||||
|
|
@ -85,5 +101,6 @@ pmake$(EXE): $(OBJS)
|
||||||
$(CC) $(CFLAGS) -o $@ $<
|
$(CC) $(CFLAGS) -o $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
rm -f pmake pmake.exe
|
||||||
find . -name "*.o" >/dev/null && rm -f $(shell find . -name "*.o")
|
find . -name "*.o" >/dev/null && rm -f $(shell find . -name "*.o")
|
||||||
rm -f src/compiler/*.c
|
rm -f src/compiler/*.c src/action/*.c src/js/*.c
|
||||||
|
|
|
||||||
4
mk/js.mk
generated
4
mk/js.mk
generated
|
|
@ -6,10 +6,6 @@ OBJS += src/js/cmdline.o
|
||||||
src/js/cmdline.c: src/js/cmdline.js
|
src/js/cmdline.c: src/js/cmdline.js
|
||||||
xxd -njs_cmdline -i src/js/cmdline.js > src/js/cmdline.c
|
xxd -njs_cmdline -i src/js/cmdline.js > src/js/cmdline.c
|
||||||
|
|
||||||
OBJS += src/action/gmake.o
|
|
||||||
src/action/gmake.c: src/action/gmake.js
|
|
||||||
xxd -naction_gmake -i src/action/gmake.js > src/action/gmake.c
|
|
||||||
|
|
||||||
OBJS += src/compiler/watcom.o
|
OBJS += src/compiler/watcom.o
|
||||||
src/compiler/watcom.c: src/compiler/watcom.js
|
src/compiler/watcom.c: src/compiler/watcom.js
|
||||||
xxd -ncompiler_watcom -i src/compiler/watcom.js > src/compiler/watcom.c
|
xxd -ncompiler_watcom -i src/compiler/watcom.js > src/compiler/watcom.c
|
||||||
|
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
const gmake = pmake.inherit(pmake.Action);
|
|
||||||
|
|
||||||
gmake.target = "gmake";
|
|
||||||
gmake.description = "GNU Make";
|
|
||||||
|
|
||||||
pmake.register(gmake);
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
const GCC = pmake.inherit(pmake.Compiler);
|
(function() {
|
||||||
|
const GCC = new pmake.POSIXCompiler();
|
||||||
|
|
||||||
GCC.target = "GCC";
|
GCC.target = "GCC";
|
||||||
GCC.description = "GCC/Clang";
|
GCC.description = "GCC/Clang";
|
||||||
|
|
||||||
pmake.register(GCC);
|
pmake.register(GCC);
|
||||||
|
})();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
const Watcom = pmake.inherit(pmake.Compiler);
|
(function() {
|
||||||
|
const Watcom = new pmake.Compiler();
|
||||||
|
|
||||||
Watcom.target = "Watcom";
|
Watcom.target = "Watcom";
|
||||||
Watcom.description = "Open Watcom";
|
Watcom.description = "Open Watcom";
|
||||||
|
|
||||||
pmake.register(Watcom);
|
pmake.register(Watcom);
|
||||||
|
})();
|
||||||
|
|
|
||||||
3
src/config.h
generated
3
src/config.h
generated
|
|
@ -5,8 +5,7 @@
|
||||||
X(watcom, compiler) \
|
X(watcom, compiler) \
|
||||||
X(gcc, compiler)
|
X(gcc, compiler)
|
||||||
|
|
||||||
#define ACTIONS \
|
#define ACTIONS
|
||||||
X(gmake, action)
|
|
||||||
|
|
||||||
#define JS \
|
#define JS \
|
||||||
COMPILERS \
|
COMPILERS \
|
||||||
|
|
|
||||||
89
src/js.c
89
src/js.c
|
|
@ -4,12 +4,7 @@ duk_context* js_ctx;
|
||||||
|
|
||||||
LOAD(js_core);
|
LOAD(js_core);
|
||||||
LOAD(js_cmdline);
|
LOAD(js_cmdline);
|
||||||
|
LOAD(js_end);
|
||||||
static duk_ret_t js_console_log(duk_context* self) {
|
|
||||||
printf("%s\n", duk_to_string(self, 0));
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void js_argv_init(int argc, char** argv) {
|
static void js_argv_init(int argc, char** argv) {
|
||||||
int arr;
|
int arr;
|
||||||
|
|
@ -28,57 +23,15 @@ static void js_argv_init(int argc, char** argv) {
|
||||||
duk_pop(js_ctx);
|
duk_pop(js_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void js_console_init(void) {
|
|
||||||
int obj;
|
|
||||||
|
|
||||||
duk_push_global_object(js_ctx);
|
|
||||||
obj = duk_push_object(js_ctx);
|
|
||||||
|
|
||||||
duk_push_c_function(js_ctx, js_console_log, 1);
|
|
||||||
duk_put_prop_string(js_ctx, obj, "log");
|
|
||||||
|
|
||||||
duk_put_prop_string(js_ctx, 0, "console");
|
|
||||||
|
|
||||||
duk_pop(js_ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
static duk_ret_t js_process_exit(duk_context* self) {
|
|
||||||
duk_idx_t argc = duk_get_top(self);
|
|
||||||
int ex;
|
|
||||||
|
|
||||||
if(argc > 1) {
|
|
||||||
return duk_error(self, DUK_ERR_TYPE_ERROR, "needs 0 or 1 argument(s)");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(argc == 1) {
|
|
||||||
ex = duk_to_number(self, 0);
|
|
||||||
} else {
|
|
||||||
ex = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
exit(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void js_process_init(void) {
|
|
||||||
int obj;
|
|
||||||
|
|
||||||
duk_push_global_object(js_ctx);
|
|
||||||
obj = duk_push_object(js_ctx);
|
|
||||||
|
|
||||||
duk_push_c_function(js_ctx, js_process_exit, DUK_VARARGS);
|
|
||||||
duk_put_prop_string(js_ctx, obj, "exit");
|
|
||||||
|
|
||||||
duk_put_prop_string(js_ctx, 0, "process");
|
|
||||||
|
|
||||||
duk_pop(js_ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
int js_init(int argc, char** argv) {
|
int js_init(int argc, char** argv) {
|
||||||
|
int st;
|
||||||
|
|
||||||
js_ctx = duk_create_heap_default();
|
js_ctx = duk_create_heap_default();
|
||||||
|
|
||||||
js_argv_init(argc, argv);
|
js_argv_init(argc, argv);
|
||||||
js_console_init();
|
js_console_init();
|
||||||
js_process_init();
|
js_process_init();
|
||||||
|
js_fs_init();
|
||||||
|
|
||||||
if(duk_peval_lstring(js_ctx, js_core, js_core_len) != 0) {
|
if(duk_peval_lstring(js_ctx, js_core, js_core_len) != 0) {
|
||||||
printf("error: js/core.js: %s\n", duk_safe_to_string(js_ctx, -1));
|
printf("error: js/core.js: %s\n", duk_safe_to_string(js_ctx, -1));
|
||||||
|
|
@ -93,6 +46,8 @@ int js_init(int argc, char** argv) {
|
||||||
JS;
|
JS;
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
|
if((st = js_run("pmake.js")) != 0) return st;
|
||||||
|
|
||||||
if(duk_peval_lstring(js_ctx, js_cmdline, js_cmdline_len) != 0) {
|
if(duk_peval_lstring(js_ctx, js_cmdline, js_cmdline_len) != 0) {
|
||||||
printf("error: js/cmdline.js: %s\n", duk_safe_to_string(js_ctx, -1));
|
printf("error: js/cmdline.js: %s\n", duk_safe_to_string(js_ctx, -1));
|
||||||
return 1;
|
return 1;
|
||||||
|
|
@ -101,6 +56,38 @@ int js_init(int argc, char** argv) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int js_run(const char* file) {
|
||||||
|
FILE* f;
|
||||||
|
char* buffer;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
if((f = fopen(file, "rb")) == NULL) {
|
||||||
|
printf("No PMake script (pmake.js) found!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
len = ftell(f);
|
||||||
|
fseek(f, 0, SEEK_SET);
|
||||||
|
|
||||||
|
buffer = malloc(len);
|
||||||
|
fread(buffer, 1, len, f);
|
||||||
|
|
||||||
|
if(duk_peval_lstring(js_ctx, buffer, len) != 0) {
|
||||||
|
printf("error: %s: %s\n", file, duk_safe_to_string(js_ctx, -1));
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void js_uninit(void) {
|
void js_uninit(void) {
|
||||||
duk_destroy_heap(js_ctx);
|
duk_destroy_heap(js_ctx);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
const PADDING = 16;
|
const PADDING = 24;
|
||||||
var action;
|
|
||||||
|
|
||||||
for(var i = 1; i < ARGV.length; i++) {
|
for(var i = 1; i < ARGV.length; i++) {
|
||||||
if(ARGV[i] == "--help") {
|
if(ARGV[i] == "--help") {
|
||||||
|
|
@ -7,10 +6,42 @@ for(var i = 1; i < ARGV.length; i++) {
|
||||||
|
|
||||||
console.log("PMake " + pmake.VERSION + " - Pyrite's build script generator");
|
console.log("PMake " + pmake.VERSION + " - Pyrite's build script generator");
|
||||||
console.log("");
|
console.log("");
|
||||||
console.log("Usage: " + ARGV[0] + " [options] action [arguments]");
|
console.log("Usage: " + ARGV[0] + " [options]");
|
||||||
|
|
||||||
|
keys = Object.keys(pmake.options);
|
||||||
|
for(var i = 0; i < keys.length; i++) {
|
||||||
|
console.log("");
|
||||||
|
console.log("OPTIONS - " + keys[i]);
|
||||||
|
|
||||||
|
var keys2 = Object.keys(pmake.options[keys[i]]);
|
||||||
|
for(var j = 0; j < keys2.length; j++) {
|
||||||
|
const opt = pmake.options[keys[i]][keys2[j]];
|
||||||
|
var arg = "";
|
||||||
|
var opts = [];
|
||||||
|
|
||||||
|
if(opt.option) {
|
||||||
|
arg = "=" + opt.option;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(" --" + keys2[j] + arg + " ".repeat(PADDING - keys2[j].length - 2 - arg.length) + opt.description);
|
||||||
|
if(!opt.options) {
|
||||||
|
} else if((typeof opt.options) == "function") {
|
||||||
|
opts = opt.options();
|
||||||
|
} else {
|
||||||
|
opts = opt.options;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(opts.length > 0) {
|
||||||
|
for(var k = 0; k < opts.length; k++) {
|
||||||
|
console.log(" " + opts[k]);
|
||||||
|
}
|
||||||
|
console.log("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.log("");
|
console.log("");
|
||||||
console.log("ACTIONS");
|
console.log("ACTIONS");
|
||||||
|
|
||||||
keys = Object.keys(pmake.actions);
|
keys = Object.keys(pmake.actions);
|
||||||
for(var i = 0; i < keys.length; i++) {
|
for(var i = 0; i < keys.length; i++) {
|
||||||
console.log(" " + keys[i] + " ".repeat(PADDING - keys[i].length) + pmake.actions[keys[i]].description);
|
console.log(" " + keys[i] + " ".repeat(PADDING - keys[i].length) + pmake.actions[keys[i]].description);
|
||||||
|
|
@ -19,16 +50,34 @@ for(var i = 1; i < ARGV.length; i++) {
|
||||||
} else if(ARGV[i].startsWith("--")) {
|
} else if(ARGV[i].startsWith("--")) {
|
||||||
console.log("Error: invalid option '" + ARGV[i].substr(2) + "'");
|
console.log("Error: invalid option '" + ARGV[i].substr(2) + "'");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} else if(pmake.actions[ARGV[i]]) {
|
|
||||||
action = ARGV[i];
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
console.log("Error: no such action '" + ARGV[i] + "'");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!action) {
|
(function(){
|
||||||
console.log("Type '" + ARGV[0] + " --help' for help");
|
function make(output){
|
||||||
process.exit(1);
|
var mk = new pmake.File(output, "w");
|
||||||
|
|
||||||
|
mk.write("# This file was auto-generated. Do not edit manually!\n");
|
||||||
|
mk.write("\n");
|
||||||
|
mk.write("SILENT = @\n");
|
||||||
|
mk.write("\n");
|
||||||
|
|
||||||
|
if(output == "Makefile"){
|
||||||
|
var targets = pmake.resolveProjects(pmake.projects);
|
||||||
|
for(var i = 0; i < targets.length; i++){
|
||||||
|
mk.write(targets[i].target + ":\n");
|
||||||
|
mk.write(" @$(MAKE) -f " + targets[i].target + ".make\n");
|
||||||
|
mk.write("\n");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
}
|
||||||
|
|
||||||
|
mk.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
make("Makefile");
|
||||||
|
var targets = pmake.resolveProjects(pmake.projects);
|
||||||
|
for(var i = 0; i < targets.length; i++){
|
||||||
|
make(targets[i].target + ".make");
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
const pmake = {
|
const pmake = {
|
||||||
VERSION : "0.0",
|
VERSION : "0.0",
|
||||||
actions : {},
|
compilers : {},
|
||||||
compilers : {}
|
options : {},
|
||||||
|
projects : []
|
||||||
};
|
};
|
||||||
|
|
||||||
pmake.inherit = function(base) {
|
pmake.inherit = function(base, call) {
|
||||||
const func = function() {};
|
const func = (function() {});
|
||||||
|
|
||||||
Object.setPrototypeOf(func.prototype, base.prototype);
|
Object.setPrototypeOf(func.prototype, base.prototype);
|
||||||
|
|
||||||
|
|
@ -25,15 +26,77 @@ pmake.inherited = function(base, parent) {
|
||||||
};
|
};
|
||||||
|
|
||||||
pmake.register = function(base) {
|
pmake.register = function(base) {
|
||||||
if(pmake.inherited(base, pmake.Compiler)) {
|
if(base instanceof pmake.Compiler) {
|
||||||
pmake.compilers[base.target] = base;
|
pmake.compilers[base.target] = base;
|
||||||
} else if(pmake.inherited(base, pmake.Action)) {
|
} else if(base instanceof pmake.Option) {
|
||||||
pmake.actions[base.target] = base;
|
if(!pmake.options[base.category]) pmake.options[base.category] = {};
|
||||||
|
|
||||||
|
pmake.options[base.category][base.target] = base;
|
||||||
|
} else if(base instanceof pmake.Project) {
|
||||||
|
pmake.projects.push(base);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pmake.Compiler = function() {
|
pmake.resolveProjects = function(arr){
|
||||||
|
var r = [];
|
||||||
|
|
||||||
|
for(var i = 0; i < arr.length; i++){
|
||||||
|
if(arr[i] instanceof pmake.Project){
|
||||||
|
r = r.concat(pmake.resolveProjects(arr[i].libraries));
|
||||||
|
r.push(arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
};
|
};
|
||||||
|
|
||||||
pmake.Action = function() {
|
(function() {
|
||||||
|
const Compiler = function() {};
|
||||||
|
|
||||||
|
const POSIXCompiler = pmake.inherit(Compiler);
|
||||||
|
|
||||||
|
const Option = function() {};
|
||||||
|
|
||||||
|
const Project = function(target) {
|
||||||
|
this.target = target;
|
||||||
|
this.files = [];
|
||||||
|
this.libraries = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Project.prototype.generate = function() {
|
||||||
|
};
|
||||||
|
|
||||||
|
const File = function(filename, mode) {
|
||||||
|
this.fp = fs.open(filename, mode);
|
||||||
|
};
|
||||||
|
|
||||||
|
File.prototype.write = function(string) {
|
||||||
|
fs.write(this.fp, string);
|
||||||
|
};
|
||||||
|
|
||||||
|
File.prototype.close = function() {
|
||||||
|
fs.close(this.fp);
|
||||||
|
};
|
||||||
|
|
||||||
|
pmake.Compiler = Compiler;
|
||||||
|
pmake.POSIXCompiler = POSIXCompiler;
|
||||||
|
pmake.Option = Option;
|
||||||
|
pmake.Project = Project;
|
||||||
|
pmake.File = File;
|
||||||
|
})();
|
||||||
|
|
||||||
|
/*** Option ***/
|
||||||
|
(function() {
|
||||||
|
const Compiler = new pmake.Option();
|
||||||
|
|
||||||
|
Compiler.category = "General";
|
||||||
|
Compiler.target = "compiler";
|
||||||
|
Compiler.description = "Choose a compiler set; one of:";
|
||||||
|
Compiler.option = "VALUE";
|
||||||
|
|
||||||
|
Compiler.options = function() {
|
||||||
|
return Object.keys(pmake.compilers);
|
||||||
|
};
|
||||||
|
|
||||||
|
pmake.register(Compiler);
|
||||||
|
})();
|
||||||
|
|
|
||||||
21
src/js_console.c
Normal file
21
src/js_console.c
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include "pmake.h"
|
||||||
|
|
||||||
|
static duk_ret_t js_console_log(duk_context* self) {
|
||||||
|
printf("%s\n", duk_to_string(self, 0));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void js_console_init(void) {
|
||||||
|
int obj;
|
||||||
|
|
||||||
|
duk_push_global_object(js_ctx);
|
||||||
|
obj = duk_push_object(js_ctx);
|
||||||
|
|
||||||
|
duk_push_c_function(js_ctx, js_console_log, 1);
|
||||||
|
duk_put_prop_string(js_ctx, obj, "log");
|
||||||
|
|
||||||
|
duk_put_prop_string(js_ctx, 0, "console");
|
||||||
|
|
||||||
|
duk_pop(js_ctx);
|
||||||
|
}
|
||||||
119
src/js_fs.c
Normal file
119
src/js_fs.c
Normal file
|
|
@ -0,0 +1,119 @@
|
||||||
|
#include "pmake.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <glob.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static duk_ret_t js_fs_glob(duk_context* self) {
|
||||||
|
int top = duk_get_top(self);
|
||||||
|
int i;
|
||||||
|
int arr = duk_push_array(self);
|
||||||
|
int c = 0;
|
||||||
|
|
||||||
|
for(i = 0; i < top; i++) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
WIN32_FIND_DATA ffd;
|
||||||
|
HANDLE h;
|
||||||
|
const char* p = duk_to_string(self, i);
|
||||||
|
|
||||||
|
if((h = FindFirstFile(p, &ffd)) != INVALID_HANDLE_VALUE) {
|
||||||
|
do {
|
||||||
|
char* s = malloc(strlen(p) + 1 + strlen(ffd.cFileName) + 1);
|
||||||
|
char* l;
|
||||||
|
|
||||||
|
strcpy(s, p);
|
||||||
|
|
||||||
|
if((l = strchr(s, '*')) != NULL) {
|
||||||
|
l[0] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(s[strlen(s) - 1] == '/' || s[strlen(s) - 1] == '\\') {
|
||||||
|
} else if(strchr(p, '/') != NULL) {
|
||||||
|
strcat(s, "/");
|
||||||
|
} else {
|
||||||
|
strcat(s, "\\");
|
||||||
|
}
|
||||||
|
strcat(s, ffd.cFileName);
|
||||||
|
|
||||||
|
duk_push_string(self, s);
|
||||||
|
duk_put_prop_index(self, arr, c);
|
||||||
|
c++;
|
||||||
|
|
||||||
|
free(s);
|
||||||
|
} while(FindNextFile(h, &ffd) != 0);
|
||||||
|
|
||||||
|
FindClose(h);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
glob_t g;
|
||||||
|
|
||||||
|
if(glob(duk_to_string(self, i), 0, NULL, &g) == 0) {
|
||||||
|
int j;
|
||||||
|
|
||||||
|
for(j = 0; j < g.gl_matchc; j++) {
|
||||||
|
duk_push_string(self, g.gl_pathv[j]);
|
||||||
|
duk_put_prop_index(self, arr, c);
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
|
||||||
|
globfree(&g);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static duk_ret_t js_fs_open(duk_context* self) {
|
||||||
|
const char* path = duk_to_string(self, 0);
|
||||||
|
const char* mode = duk_to_string(self, 1);
|
||||||
|
FILE* f = fopen(path, mode);
|
||||||
|
|
||||||
|
if(f == NULL) return 0;
|
||||||
|
|
||||||
|
duk_push_pointer(self, f);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static duk_ret_t js_fs_write(duk_context* self) {
|
||||||
|
FILE* f = duk_to_pointer(self, 0);
|
||||||
|
const char* str = duk_to_string(self, 1);
|
||||||
|
|
||||||
|
fwrite(str, 1, strlen(str), f);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static duk_ret_t js_fs_close(duk_context* self) {
|
||||||
|
FILE* f = duk_to_pointer(self, 0);
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void js_fs_init(void) {
|
||||||
|
int obj;
|
||||||
|
|
||||||
|
duk_push_global_object(js_ctx);
|
||||||
|
obj = duk_push_object(js_ctx);
|
||||||
|
|
||||||
|
duk_push_c_function(js_ctx, js_fs_glob, DUK_VARARGS);
|
||||||
|
duk_put_prop_string(js_ctx, obj, "glob");
|
||||||
|
|
||||||
|
duk_push_c_function(js_ctx, js_fs_open, 2);
|
||||||
|
duk_put_prop_string(js_ctx, obj, "open");
|
||||||
|
|
||||||
|
duk_push_c_function(js_ctx, js_fs_write, 2);
|
||||||
|
duk_put_prop_string(js_ctx, obj, "write");
|
||||||
|
|
||||||
|
duk_push_c_function(js_ctx, js_fs_close, 1);
|
||||||
|
duk_put_prop_string(js_ctx, obj, "close");
|
||||||
|
|
||||||
|
duk_put_prop_string(js_ctx, 0, "fs");
|
||||||
|
|
||||||
|
duk_pop(js_ctx);
|
||||||
|
}
|
||||||
32
src/js_process.c
Normal file
32
src/js_process.c
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
#include "pmake.h"
|
||||||
|
|
||||||
|
static duk_ret_t js_process_exit(duk_context* self) {
|
||||||
|
duk_idx_t argc = duk_get_top(self);
|
||||||
|
int ex;
|
||||||
|
|
||||||
|
if(argc > 1) {
|
||||||
|
return duk_error(self, DUK_ERR_TYPE_ERROR, "needs 0 or 1 argument(s)");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc == 1) {
|
||||||
|
ex = duk_to_number(self, 0);
|
||||||
|
} else {
|
||||||
|
ex = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
void js_process_init(void) {
|
||||||
|
int obj;
|
||||||
|
|
||||||
|
duk_push_global_object(js_ctx);
|
||||||
|
obj = duk_push_object(js_ctx);
|
||||||
|
|
||||||
|
duk_push_c_function(js_ctx, js_process_exit, DUK_VARARGS);
|
||||||
|
duk_put_prop_string(js_ctx, obj, "exit");
|
||||||
|
|
||||||
|
duk_put_prop_string(js_ctx, 0, "process");
|
||||||
|
|
||||||
|
duk_pop(js_ctx);
|
||||||
|
}
|
||||||
|
|
@ -5,5 +5,7 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
if((st = js_init(argc, argv)) != 0) return st;
|
if((st = js_init(argc, argv)) != 0) return st;
|
||||||
|
|
||||||
|
js_uninit();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,14 @@ JS;
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
/* js.c */
|
/* js.c */
|
||||||
extern duk_context* js_context;
|
extern duk_context* js_ctx;
|
||||||
int js_init(int argc, char** argv);
|
int js_init(int argc, char** argv);
|
||||||
|
int js_run(const char* file);
|
||||||
void js_uninit(void);
|
void js_uninit(void);
|
||||||
|
|
||||||
|
/* js_*.c */
|
||||||
|
void js_console_init(void);
|
||||||
|
void js_process_init(void);
|
||||||
|
void js_fs_init(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue