summaryrefslogtreecommitdiff
path: root/modules/abbrgen/abbrgen.js
blob: ba6248befd9c1c7e1300863cde2fd1cad0d6dc66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var cmn=require("../$common.js"),
    process=require("child_process");

var moddir,uname;
uname=String(process.execSync("uname")).trim();

//PERFORMS NO VALIDATION!
function get_abbreviations(abbr,num,cb){
	var fname=moddir+"/abbreviation_gen_"+uname;
	process.execFile(fname,[abbr,num],{},function(err,stdout,stderr){
		//if(err)throw err;
		if(err){
			console.log(err.toString());
			console.log(err.stack);
			cb([]);
			return;
		}
		cb(stdout.split("\n"));
	});
}

module.exports=function(app,io,_moddir){
	moddir=_moddir;
	app.get("/abbrgen/:abbr",function(req,res){
		var abbr=req.params.abbr;
		res.set("Content-Type","text/plain");
		if(!abbr.match(/^[a-z]+$/i)){
			res.send("ERROR: Invalid input value.");
			return;
		}
		get_abbreviations(abbr,"1",function(answers){
			res.send(answers.join("\n"));
		});
	});
	app.get("/abbrgen/:abbr/:num",function(req,res){
		var abbr=req.params.abbr,num=req.params.num;
		res.set("Content-Type","text/plain");
		if(num>5000){
			res.send("ERROR: Number of abbreviations too large.");
			return;
		}
		if(!abbr.match(/^[a-z]+$/i)||isNaN(+num)||num<0||num%1!=0){
			res.send("ERROR: Invalid input values.");
			return;
		}
		get_abbreviations(abbr,num|0,function(answers){
			res.send(answers.join("\n"));
		});
	});
};