summaryrefslogtreecommitdiff
path: root/world/map/npc/commands/_procedures.txt
blob: 732b496fd8e495115843008e55587e0a034e9a22 (plain) (blame)
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
51
52
53
// ARGV Splitter
// takes @args$ and splits it properly so that '@cmd "foo bar" baz' is ['foo bar','baz'] instead of ['"foo','bar"','baz']
// input: @args$ (string)
// output: @argv$ (array) and @argv (array)
function|script|argv_splitter
{
    explode .@fragments$, @args$, " ";
    set .@e, 0;
    set .@total, getarraysize(.@fragments$);
    set .@NULL$, chr(3); // HACK: we use .@NULL$ as a workaround because we can't do "\0"
    goto L_Check;

L_Check:
    setarray .@check$[0], "", .@NULL$, .@NULL$;
    explode .@check$, .@fragments$[.@e], "\""; // check if the fragment contains a quote
    if   (.@check$[0] == "" && .@check$[1] != .@NULL$ && .@check$[1] != "" && .@check$[2] == .@NULL$)
        set .@string$, .@check$[1]; // begin substring
    elif (.@check$[0] != "" && .@check$[1] == "" && .@check$[2] == .@NULL$)
        goto L_EndSubString; // end substring
    elif (.@string$ != "" && .@check$[0] != "" && .@check$[1] == .@NULL$ && .@check$[2] == .@NULL$)
        set .@string$, .@string$ +" "+ .@check$[0]; // part of the substring
    elif (.@check$[2] != .@NULL$) goto L_Set2; // the the argument is quoted but there is no space
    else goto L_Set;
    goto L_CheckAfter;

L_Set:
    setarray @argv$[.@t], .@check$[0]; // not in a substring so push right away
    setarray @argv[.@t], .@check$[0]; // not in a substring so push right away
    set .@t, .@t + 1;
    goto L_CheckAfter;

L_Set2:
    setarray @argv$[.@t], .@check$[1]; // not in a substring so push right away
    setarray @argv[.@t], .@check$[1]; // not in a substring so push right away
    set .@t, .@t + 1;
    goto L_CheckAfter;

L_EndSubString:
    set .@string$, .@string$ + " " + .@check$[0];
    setarray @argv$[.@t], .@string$; // push in the array
    setarray @argv[.@t], .@string$; // push in the array
    set .@t, .@t + 1;
    set .@string$, ""; // clean
    goto L_CheckAfter;

L_CheckAfter:
    set .@e, .@e + 1;
    if (.@e > .@total) goto L_Done; // the @argv$ array is built
    goto L_Check; // not done yet

L_Done:
    return;
}