diff --git a/src/modules/LibC.js b/src/modules/LibC.js new file mode 100644 index 0000000..a21261c --- /dev/null +++ b/src/modules/LibC.js @@ -0,0 +1,85 @@ +/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ + +var EXPORTED_SYMBOLS = ["LibC"]; + +const LIB_C = "libc.so.6"; + +const Cu = Components.utils; +const Cc = Components.classes; +const Ci = Components.interfaces; + +Cu.import("resource://gre/modules/ctypes.jsm"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +XPCOMUtils.defineLazyGetter(this, "libc", function() { + var libc = ctypes.open(LIB_C); + if (!libc) + throw "libc is unavailable"; + + return libc; +}); + +XPCOMUtils.defineLazyGetter(this, "FILE", function() { + return ctypes.StructType("FILE"); +}); + +XPCOMUtils.defineLazyGetter(this, "fdopen", function() { + var fdopen = libc.declare( + "fdopen", ctypes.default_abi, FILE.ptr, + ctypes.int, + ctypes.char.ptr + ); + + if (!fdopen) + throw "fdopen is unavailable"; + + return fdopen; +}); + + +XPCOMUtils.defineLazyGetter(this, "puts", function() { + var puts = libc.declare( + "puts", ctypes.default_abi, ctypes.int32_t, + ctypes.char.ptr + ); + + if (!puts) + throw "puts is unavailable"; + + return puts; +}); + +XPCOMUtils.defineLazyGetter(this, "fputs", function() { + var fputs = libc.declare( + "fputs", ctypes.default_abi, ctypes.int32_t, + ctypes.char.ptr, + FILE.ptr + ); + + if (!fputs) + throw "fputs is unavailable"; + + return fputs; +}); + +XPCOMUtils.defineLazyGetter(this, "fflush", function() { + var fflush = libc.declare( + "fflush", ctypes.default_abi, ctypes.int32_t, + FILE.ptr + ); + + if (!fflush) + throw "fflush is unavailable"; + + return fflush; +}); + +var LibC = { + stderr: this.fdopen(2, "a"), + + FILE: FILE, + fdopen: fdopen, + puts: puts, + fputs: fputs, + fflush: fflush, +} diff --git a/src/modules/commons.js b/src/modules/commons.js index 73d35cc..45278a8 100644 --- a/src/modules/commons.js +++ b/src/modules/commons.js @@ -10,6 +10,9 @@ var EXPORTED_SYMBOLS = [ "mozt", "Cc", "Ci" ]; const Cc = Components.classes; const Ci = Components.interfaces; +const Cu = Components.utils; + +Cu.import("resource://moztray/LibC.js"); const FIREFOX_ID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}"; const THUNDERBIRD_ID = "{3550f703-e582-4d05-9a08-453d09bdfdc6}"; @@ -64,6 +67,11 @@ mozt.Debug = { this.dump(str); }, + // dump to terminal (stderr) + debug: function(str) { + LibC.fputs(str + "\n", LibC.stderr); + LibC.fflush(LibC.stderr); + }, }; // build it ! mozt.Debug.init();