From 4c8f69b7c55f1a8ba8e8d338c2cee6bac0cc85c4 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Sun, 8 Jul 2012 18:59:46 +0200 Subject: [PATCH 1/5] Use v8 to parse settings.json --- src/node/utils/Settings.js | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index e60446df..aeeb9015 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -24,6 +24,7 @@ var os = require("os"); var path = require('path'); var argv = require('./Cli').argv; var npm = require("npm/lib/npm.js"); +var vm = require('vm'); /* Root path of the installation */ exports.root = path.normalize(path.join(npm.dir, "..")); @@ -45,6 +46,7 @@ exports.dbType = "dirty"; * This setting is passed with dbType to ueberDB to set up the database */ exports.dbSettings = { "filename" : path.join(exports.root, "dirty.db") }; + /** * The default Text of a new pad */ @@ -102,33 +104,25 @@ exports.abiwordAvailable = function() // Discover where the settings file lives var settingsFilename = argv.settings || "settings.json"; -if (settingsFilename.charAt(0) != '/') { - settingsFilename = path.normalize(path.join(root, settingsFilename)); -} +settingsFilename = path.resolve(path.join(root, settingsFilename)); -var settingsStr +var settingsStr; try{ //read the settings sync - settingsStr = fs.readFileSync(settingsFilename).toString(); + settingsStr = fs.readFileSync(settingsFilename); } catch(e){ console.warn('No settings file found. Using defaults.'); - settingsStr = '{}'; } - -//remove all comments -settingsStr = settingsStr.replace(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/gm,"").replace(/#.*/g,"").replace(/\/\/.*/g,""); -//try to parse the settings +// try to parse the settings var settings; -try -{ - settings = JSON.parse(settingsStr); -} -catch(e) -{ - console.error("There is a syntax error in your settings.json file"); - console.error(e.message); - process.exit(1); +try { + if(settingsStr) { + settings = vm.runInContext('exports = '+settingsStr, vm.createContext(), "settings.json"); + } +}catch(e){ + console.warn('There was an error processing your settings.json file. Using defaults.'); + console.warn(e.message); } //loop trough the settings From 885844667887411c8fc2822ab1914b7277bcb4ca Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 10 Jul 2012 21:38:14 +0200 Subject: [PATCH 2/5] Exit on error. --- src/node/utils/Settings.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index aeeb9015..68e58eb2 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -111,7 +111,8 @@ try{ //read the settings sync settingsStr = fs.readFileSync(settingsFilename); } catch(e){ - console.warn('No settings file found. Using defaults.'); + console.error('No settings file found.'); + process.exit(1); } // try to parse the settings @@ -121,8 +122,8 @@ try { settings = vm.runInContext('exports = '+settingsStr, vm.createContext(), "settings.json"); } }catch(e){ - console.warn('There was an error processing your settings.json file. Using defaults.'); - console.warn(e.message); + console.error('There was an error processing your settings.json file: '+e.message); + process.exit(1); } //loop trough the settings @@ -142,8 +143,7 @@ for(var i in settings) //this setting is unkown, output a warning and throw it away else { - console.warn("Unkown Setting: '" + i + "'"); - console.warn("This setting doesn't exist or it was removed"); + console.warn("Unkown Setting: '" + i + "'. This setting doesn't exist or it was removed"); } } From 87f26334d1259f6422c3a2b7d9325b611c72316b Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 10 Jul 2012 21:55:35 +0200 Subject: [PATCH 3/5] Fix typo. --- src/node/utils/Settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 68e58eb2..205b675f 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -143,7 +143,7 @@ for(var i in settings) //this setting is unkown, output a warning and throw it away else { - console.warn("Unkown Setting: '" + i + "'. This setting doesn't exist or it was removed"); + console.warn("Unknown Setting: '" + i + "'. This setting doesn't exist or it was removed"); } } From f09dd0f3fba8630d221dba3a652dad0a229aa509 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 11 Jul 2012 15:34:33 +0200 Subject: [PATCH 4/5] Put toString() back in. --- src/node/utils/Settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 205b675f..1b7c3f46 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -109,7 +109,7 @@ settingsFilename = path.resolve(path.join(root, settingsFilename)); var settingsStr; try{ //read the settings sync - settingsStr = fs.readFileSync(settingsFilename); + settingsStr = fs.readFileSync(settingsFilename).toString(); } catch(e){ console.error('No settings file found.'); process.exit(1); From dc09323d8f6ee5a65f301bb677d9e18a54672322 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 11 Jul 2012 15:36:41 +0200 Subject: [PATCH 5/5] Don't exit if no settings file was found. --- src/node/utils/Settings.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/node/utils/Settings.js b/src/node/utils/Settings.js index 1b7c3f46..dd34ac5e 100644 --- a/src/node/utils/Settings.js +++ b/src/node/utils/Settings.js @@ -111,8 +111,7 @@ try{ //read the settings sync settingsStr = fs.readFileSync(settingsFilename).toString(); } catch(e){ - console.error('No settings file found.'); - process.exit(1); + console.warn('No settings file found. Continuing using defaults!'); } // try to parse the settings