11.9.12

git svn failure under cygwin

git svn init -s repo_url fails with error message:

child_info_fork::abort: unable to remap cygsvn_client-1-0.dll to same address as parent

Operating system: Windows 7, 64 bits.

Executing /usr/bin/rebaseall from ash did not help. After some research, I downloaded ListDLLs executable from Windows Sysinternals. I opened cygwin, ran git svn. Then I executed listdlls.exe from windows command prompt.

It seems that the base address of some cygwin dlls collide with other dlls:

grep '0x000000006' listed_dlls.txt | sort

0x0000000060000000 0x4da000 C:\Program Files\NVIDIA Corporation\Display\nvxdapix.dll
0x0000000061000000 0x15a000 C:\Program Files\NVIDIA Corporation\Display\nvxdbat.dll
0x0000000061000000 0x480000 C:\cygwin\bin\cygwin1.dll
0x0000000063000000 0x1ac000 C:\Program Files\NVIDIA Corporation\Display\nvxdplcy.dll
...
0x000000006e3b0000 0x8e000 C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_50934f2ebcb7eb57\MSVCP90.dll
0x000000006e400000 0x2d000 C:\cygwin\bin\cygreadline7.dll
0x000000006e440000 0xda000 C:\Program Files (x86)\AVG\AVG2012\avgidpsdkx.dll
...
0x000000006e8f0000 0x2dd000 C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll
0x000000006ea70000 0x41000 C:\cygwin\bin\cygncursesw-10.dll
0x000000006ebd0000 0x683000 C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Data\67065dc691dbf9574b3c8e5ac6ec5246\System.Data.ni.dll
0x000000006eeb0000 0xf000 C:\cygwin\bin\cygintl-8.dll
0x000000006ef00000 0xfc000 C:\cygwin\bin\cygiconv-2.dll
0x000000006f260000 0x3b000 C:\Windows\SysWOW64\rsaenh.dll
...
0x000000006f510000 0x19e000 C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.17514_none_41e6975e2bd6f2b2\comctl32.dll
0x000000006f5b0000 0x18000 C:\cygwin\bin\cyggcc_s-1.dll
0x000000006f6b0000 0xb0000 C:\Windows\SysWOW64\bthprops.cpl
0x000000006f760000 0x113b000 C:\Windows\assembly\NativeImages_v4.0.30319_32\System.ServiceModel\4f8ecf03aa4a4165e6850d1d67dc445f\System.ServiceModel.ni.dll

It seems that 0x0000000050000000 range is free

grep '0x000000005' listed_dlls.txt

<empty output>

Then I started ash.exe as admin and executed:

/usr/bin/rebaseall -b 0x0000000060000000

Cygwin dlls are rebased now to address 0x000000005xxxxxxx and git svn problem disappeared.

4.5.08

Javascript recursive descent parser generator

Here is a recursive descent parser generator for javascript. As always, I'm too lazy to document how it works. I did not test it much. Just play with it if you like.

In the first section, you define the tokens. There is a special token called SKIP, which is ignored.

In the second section, you define the grammar of your language. Some examples:

C = { 
  A A;
  B;
};
means C is A followed by A, or C is B.

You can add types to your productions:

C = { 
  [A_PAIR] A A;
  [JUST_B] B;
};

You can name the symbols in the productions:

C = { 
  [A_PAIR] first:A second:A;
  [JUST_B] value:B;
};
Say that A denotes the token /[a-z]/, than the parse result of "x y" is an object like this:
{ Type: "A_PAIR", first:"x", second:"y" }

You can omit braces if there is only one production for a rule:

C = A A;

You can also inherit the parse result of a sub-rule. So instead of:

C = A A;
X = lbrace value:C rbrace;
then using x.value.first, you can do:
C = A A;
X = lbrace #C rbrace;
then use x.first.

You can also use wildcards in restricted cases:

Alist = A+;
Blist = B*;
in which case the result object will be an array.

22.12.07

Serializing JavaScript objects

I once have written a JavaScript function that dumps a JavaScript object in a human readable format. It was something like JSON, but it also handled objects containing circular references shared object references. I must admit that it was written badly.

After reading this, I thought that maybe it's a good idea to change the previous code for serializing JavaScript objects. I also refactored the code. You can find it below. I didn't test it thoroughly, so be careful.

An example of what it can do:
var x = {};

x.foo  = new Object();
x.self = x;
x.arr  = [x.self];
x.bar  = x.foo;

var s = serialize(x);
var y = deserialize(s);
alert(y == y.self);    // true
alert(y == y.arr[0]);  // true
alert(y.foo == y.bar); // true

alert(s);
// {
//     'foo': {},
//     'self': { _root_: [  ] },
//     'arr': [
//         { _root_: [  ] }
//     ],
//     'bar': { _root_: [ "foo" ] }
// }
Addition: I think that I have to express that better: don't expect it to serialize functions, regular expressions, images (or any other browser object), etc. It's just something like JSON, but it also handles shared object references. Code:
//============================================================================//
//  JavaScript object de/serialization with circular references.              //
//                                                                            //
//  author: Mehmet Yavuz Selim Soyturk                                        //
//  e-mail: Mehmet dot Yavuz dot Selim at gmail dot com                       //
//============================================================================//

Array.prototype.findIf = function(predicate) {
    for(var i in this) {
        if (predicate(this[i]))
            return i;
    }
    return -1;
};
Array.prototype.map = function(func) {
    var len = this.length;
    var result = [];
    for (var i=0; i<len; i++) {
        result[i] = func(this[i]);
    }
    return result;
};
Array.prototype.appended = function(value) {
    var copy = this.slice(0);
    copy[copy.length] = value;
    return copy;
};

//============================================================================//
//  SERIALIZATION                                                             //
//============================================================================//

function indented(n) {
    var s = '';
    for(var i=0; i<n; i++) s += '    ';
    return s;
}

function serializePrimitive(value) {
    if (typeof value == 'string')
        return '"' + value + '"';
    else
        return '' + value;
}

function serializePrimitiveArray(arr) {
    var s = "[ ";
    s += arr.map(serializePrimitive).join(', ');
    s += " ]";
    return s;
}

function serializeArray(arr, seen, indices, depth) {
    if (arr.length == 0)
        return '[]';

    seen[seen.length] = {obj: arr, indices: indices};
    
    var result = '[\n';
    for (var i=0; i<arr.length; i++) {
        result += indented(depth + 1);
        result += serializeAny(arr[i], seen, indices.appended(i), depth + 1);
        result += (i == arr.length - 1) ? '' : ', ';
        result += '\n';
    }
    result += indented(depth) + ']';

    return result;
}

function serializeObject(obj, seen, indices, depth) {
    seen[seen.length] = {obj: obj, indices: indices};
    
    var result = '{\n';
    var count = 0;
    for (var i in obj) {
        if (typeof obj[i] != 'function') {
            count++;
            result += indented(depth+1);
            result += "'" + i + "': ";
            result += serializeAny(obj[i], seen, indices.appended(i), depth + 1);
            result += ", \n"; // bad hack, see next
        }
    }
    if (count > 0) {
        result = result.substring(0, result.length-3) + '\n'; // bad hack
        return result + indented(depth) + '}';
    }
    else {
        return '{}';
    }
}


function serializeAny(value, seen, indices, depth) {
    var t = typeof(value);
    var prevIndex;
    
    if (t == 'function') {
        throw new Error("Cannot serialize function. Keys from root: " + 
            serializePrimitiveArray(indices));
    }
    else if (t != 'object') {
        return serializePrimitive(value);
    }
    else if ( (prevIndex = seen.findIf
                    ( function(obj) { return obj.obj == value } )
              ) >= 0) {
        return '{ _root_: ' + serializePrimitiveArray(seen[prevIndex].indices) + ' }';
    }
    else if (value.constructor == Array) {
        return serializeArray(value, seen, indices, depth);
    }
    else {
        return serializeObject(value, seen, indices, depth);
    }
};

function serialize(obj) {
   return serializeAny(obj, [], [], 0);
}


//============================================================================//
//  DESERIALIZATION                                                           //
//============================================================================//

function followIndices(obj, indices) {
    for (var i=0; i<indices.length; i++)
        obj = obj[indices[i]];
        
    return obj;
}

function replaceRootRefs(obj, root) {
    if (typeof obj != 'object' || obj == null)
        return;
    
    for (var i in obj) {
        var prop = obj[i];
        if (typeof prop == 'object' && prop != null) {
            if ('_root_' in prop)
                obj[i] = followIndices(root, prop._root_);
            replaceRootRefs(prop, root);
        }
    }
}

function deserialize(str) {
    var obj = eval( '(' + str + ')' );
    if (str.indexOf('_root_') < 0)
        return obj;
    
    replaceRootRefs(obj, obj);
    return obj;
}

15.12.07

Kablosuz aglari dinlemek icin

Interface'i monitor moda getir:
$ sudo iwconfig eth1 mode monitor
Wireshark'i calistir:
$ sudo wireshark
Dinle ve interface'i eski haline getir:
$ sudo iwconfig eth1 mode managed

13.12.07

latex, html, plain text

Converting latex to html, if the tex file has utf8 encoding:
latex2html -html_version 4,unicode document.tex
If you want only one page:
latex2html -split 0 -html_version 4,unicode document.tex
Converting html to plain tex:
elinks -dump document.html

16.11.07

Output buffer unwinding for PHP?

PHP 5 supports exception handling. When an exception occurs, PHP does stack unwinding so that it restores the state of the script... does it? No. An important state of the script does not get restored: the output. Would it not be a good idea if PHP also did some sort of output buffer unwinding? That is the idea:
<?php

exceptional_ob_start();

echo "Begin\n";
try {
    echo "Exception\n";
    throw new Exception();
} catch (Exception $e) {}
echo "End\n";

exceptional_ob_end_flush();
    
?>
And that is the only output:
Begin
End
I am not a PHP (nor webscripting) guy so I may be wrong, but I think that something like that would be very useful. It is maybe even needed for making PHP exceptions useful.
 

18.8.07

Currying in JavaScript

We can make our functions curried in JavaScript:
function curry(f, needed_len) {
    if (needed_len == undefined)
        needed_len = f.length;
        
    var curried = function() {
        var curried_args = arguments;
        if (curried_args.length >= needed_len) {
            return f.apply(this, arguments);
        }
        else {
            var curry_result_function = function() {
                var args = [];
                for(var i=0; i<curried_args.length; i++)
                    args[args.length] = curried_args[i];
                
                for(var i=0; i<arguments.length; i++)
                    args[args.length] = arguments[i];

                return f.apply(this, args);
            };
            return curry(curry_result_function, needed_len-curried_args.length);
        }
    };
    return curried;
}
And usage:
var curried_add = curry(function (a, b, c) {
    return a + b + c;
});

print(curried_add(1)(2)(3));
print(curried_add(1)()(2)()(3));
print(curried_add(1,2,3));
print(curried_add(1)(2,3));
print(curried_add(1,2)(3));
See also next pages for other implementations: http://www.dustindiaz.com/javascript-curry http://www.svendtofte.com/code/curried_javascript/
 
Note: after some more googling about currying in JavaScript, it's very interesting to see that http://www.coryhudson.com/blog/2007/03/10/javascript-currying-redux/ contains an almost identical implementation.