Check-in [07c2ea91bc]
Overview
Comment:added [file join], [file split]
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 07c2ea91bc6c90eadee982dc6c4b450b2d5d7501
User & Date: suchenwi on 2013-11-24 08:24:20
Other Links: manifest | tags
Context
2013-11-24
10:21
factored out infovars, added README.txt Leaf check-in: b0688623ce user: suchenwi tags: trunk
08:24
added [file join], [file split] check-in: 07c2ea91bc user: suchenwi tags: trunk
2013-11-23
21:28
added several file commands, fixed backslash substitutions check-in: 4f3db7f982 user: suchenwi tags: trunk
Changes

Modified tcl053.js from [623beb5ebc] to [dc45822eaf].

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    this.incrLevel = function() {
      this.callframe[++this.level] = {};
      return this.level;
    }
    this.decrLevel = function() {
      this.callframe[this.level] = null;
      this.level--;
      if (this.level < 0) throw "Exit application";
      this.result = null;
    }
    this.getCommand = function(name) {
        try {
            return this.commands[name];
        } catch (e) {throw "No such command '"+name+"'";}
    }







|







60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    this.incrLevel = function() {
      this.callframe[++this.level] = {};
      return this.level;
    }
    this.decrLevel = function() {
      this.callframe[this.level] = null;
      this.level--;
      if (this.level < 0) this.level = 0;
      this.result = null;
    }
    this.getCommand = function(name) {
        try {
            return this.commands[name];
        } catch (e) {throw "No such command '"+name+"'";}
    }
265
266
267
268
269
270
271

272
273
274
275
276
277
278
279
280
281
	throw 'key "'+key+'" not known in dictionary';
      });
    this.registerSubCommand("dict", "keys", function (interp, args) {
	if(args.length < 2 || args.length > 3) 
	  throw 'wrong # args: should be "dict keys dictionary ?globPattern?"';
	var dict    = args[1].toList();
	var pattern = ".*";

	if(args.length == 3) pattern = "^"+args[2].toString().replace(/\*/g,".*");
	var res  = [];
	for (var i=0;i < dict.length;i+=2) {
	  if(dict[i].toString().match(pattern)) res.push(dict[i]);
	}
	return res;
      });
    this.registerSubCommand("dict", "set", function (interp, args) {
	this.arity(args, 4);
	var name  = args[1];







>
|

|







265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	throw 'key "'+key+'" not known in dictionary';
      });
    this.registerSubCommand("dict", "keys", function (interp, args) {
	if(args.length < 2 || args.length > 3) 
	  throw 'wrong # args: should be "dict keys dictionary ?globPattern?"';
	var dict    = args[1].toList();
	var pattern = ".*";
	if(args.length == 3) 
	  pattern = "^"+args[2].toString().replace(/\*/g,".*");
	var res  = [];
	for (var i = 0; i < dict.length; i+=2) {
	  if(dict[i].toString().match(pattern)) res.push(dict[i]);
	}
	return res;
      });
    this.registerSubCommand("dict", "set", function (interp, args) {
	this.arity(args, 4);
	var name  = args[1];
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422












423
424
425
426
427
428
429
430
431
432






433
434
435
436
437
438
439
	return stat.atime.getTime()/1000;
      })
    this.registerSubCommand("file", "dirname", function (interp, args) {
        this.arity(args, 2);
	return interp.dirname(args[1].toString());
     });
    this.dirname = function(p) { // also used in [glob]
      //require("path"); //not working :(
      //return path.dirname(p.toString());
      if(p == ".") p = process.cwd();
      p = p.split("/"); 
      p.pop();
      if(p == "") return("/");
      return p.join("/");
    };
    this.registerSubCommand("file", "exists", function (interp, args) {
        this.arity(args, 2);
	var file = args[1].toString();
	try {var fd = fs.openSync(file,"r");} catch(e) {return 0;}
	fs.closeSync(fd);
	return 1;
     });
    this.registerSubCommand("file", "extension", function (interp, args) {
        this.arity(args, 2);
	var fn  = args[1].toString();
	var res = fn.split(".").pop();
	res = (res == fn)? "" : "."+res;
	return res;
     });












    this.registerSubCommand("file", "mtime", function (interp, args) {
        this.arity(args, 2);
	var stat = fs.statSync(args[1].toString());
	return stat.mtime.getTime()/1000;
      })
    this.registerSubCommand("file", "size", function (interp, args) {
        this.arity(args, 2);
	var stat = fs.statSync(args[1].toString());
	return stat.size;
      })






    this.registerSubCommand("file", "tail", function (interp, args) {
        this.arity(args, 2);
	return args[1].toString().split("/").pop();
      });
    this.registerCommand("for", function (interp, args) {
        this.arity(args, 5);
        interp.eval(args[1].toString());







|
|
<
<
<
<
<















>
>
>
>
>
>
>
>
>
>
>
>




|




|
>
>
>
>
>
>







395
396
397
398
399
400
401
402
403





404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
	return stat.atime.getTime()/1000;
      })
    this.registerSubCommand("file", "dirname", function (interp, args) {
        this.arity(args, 2);
	return interp.dirname(args[1].toString());
     });
    this.dirname = function(p) { // also used in [glob]
      var path = require("path");
      return path.dirname(p.toString());





    };
    this.registerSubCommand("file", "exists", function (interp, args) {
        this.arity(args, 2);
	var file = args[1].toString();
	try {var fd = fs.openSync(file,"r");} catch(e) {return 0;}
	fs.closeSync(fd);
	return 1;
     });
    this.registerSubCommand("file", "extension", function (interp, args) {
        this.arity(args, 2);
	var fn  = args[1].toString();
	var res = fn.split(".").pop();
	res = (res == fn)? "" : "."+res;
	return res;
     });
    this.registerSubCommand("file", "join", function (interp, args) {
        this.arity(args, 2, Infinity);
	args.shift();
	var res = "", sep = "";
	for (var arg in args) {
	  var part = args[arg].toString();
	  if(part.match("^[/]")) 
	    res = part; else res = res+sep+part;
	  sep = "/";
	}
	return res;
      });
    this.registerSubCommand("file", "mtime", function (interp, args) {
        this.arity(args, 2);
	var stat = fs.statSync(args[1].toString());
	return stat.mtime.getTime()/1000;
      });
    this.registerSubCommand("file", "size", function (interp, args) {
        this.arity(args, 2);
	var stat = fs.statSync(args[1].toString());
	return stat.size;
      });
    this.registerSubCommand("file", "split", function (interp, args) {
        this.arity(args, 2);
	var path = args[1].toString().split("/");
	if(path[0] == "") path[0] = "/";
	return path;
      });
    this.registerSubCommand("file", "tail", function (interp, args) {
        this.arity(args, 2);
	return args[1].toString().split("/").pop();
      });
    this.registerCommand("for", function (interp, args) {
        this.arity(args, 5);
        interp.eval(args[1].toString());
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
	//interp.buf = "";
	//while(interp.buf == "") {
	//interp.timeout = setTimeout(function(){}, 10000);
	//  if(interp.getsing==0) break;
	//}
	return; // result will be in interp.buf when done
     });
   this.gets = function(char) {
     try {
       if(char.match(/foo[\r\n]/)) {
	 this.getsing = 0;
	 puts("received: "+this.buf);
       } else {
	 puts("<"+char+">"+this.getsing);
	 this.buf += char;
       }
     } catch(e) {puts(e)};
   }
   this.registerCommand("glob", function (interp, args) {
	this.arity(args, 2, Infinity);
	args.shift();
	var res    = [];
	var prefix = "";
	var dir    = ".";
	for (var arg in args) {







|









|







509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
	//interp.buf = "";
	//while(interp.buf == "") {
	//interp.timeout = setTimeout(function(){}, 10000);
	//  if(interp.getsing==0) break;
	//}
	return; // result will be in interp.buf when done
     });
/*   this.gets = function(char) {
     try {
       if(char.match(/foo[\r\n]/)) {
	 this.getsing = 0;
	 puts("received: "+this.buf);
       } else {
	 puts("<"+char+">"+this.getsing);
	 this.buf += char;
       }
     } catch(e) {puts(e)};
   }*/
   this.registerCommand("glob", function (interp, args) {
	this.arity(args, 2, Infinity);
	args.shift();
	var res    = [];
	var prefix = "";
	var dir    = ".";
	for (var arg in args) {

Modified test_tcljs.tcl from [744eb2cb5e] to [93e8b14455].

98
99
100
101
102
103
104




105
106
107
108
109
110
111
e.g. {set x -44; expr {-$x}} -> 44
e.g. {expr 1<<3} -> 8

e.g. {file dirname foo/bar/grill}  -> foo/bar
e.g. {file dirname /foo/bar/grill} -> /foo/bar
e.g. {file extension foo.txt}      -> .txt
e.g. {file extension Makefile}     -> ""




e.g. {file tail foo/bar/grill}     -> grill

set forres ""
e.g. {for {set i 0} {$i < 5} {incr i} {append forres $i}; set forres} -> 01234
e.g. {foreach i {a b c d e} {append foreachres $i}; set foreachres}   -> abcde

e.g. {format %x 255} -> ff







>
>
>
>







98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
e.g. {set x -44; expr {-$x}} -> 44
e.g. {expr 1<<3} -> 8

e.g. {file dirname foo/bar/grill}  -> foo/bar
e.g. {file dirname /foo/bar/grill} -> /foo/bar
e.g. {file extension foo.txt}      -> .txt
e.g. {file extension Makefile}     -> ""
e.g. {file join a b c}             -> a/b/c
e.g. {file join a /b c}            -> /b/c
e.g. {file split a/b/c}            -> {a b c}
e.g. {file split /a/b/c}           -> {/ a b c}
e.g. {file tail foo/bar/grill}     -> grill

set forres ""
e.g. {for {set i 0} {$i < 5} {incr i} {append forres $i}; set forres} -> 01234
e.g. {foreach i {a b c d e} {append foreachres $i}; set foreachres}   -> abcde

e.g. {format %x 255} -> ff