// File: clementino1971[P]ICC065-protoTerminal/protoTerminal.cpp
// Function: main
int main(int argc, char *argv[]){

    //Define Functions here	
    mapa["ls"] = &ls;
    mapa["cd"] = &cd;

    //Strings
    string currCommand;

    //Defining delimiters
    possible_delimiters.insert('\'');
    possible_delimiters.insert('"');

    //Defining escapable special characters
    special_characters.insert('\\');
    special_characters.insert('\'');
    special_characters.insert('"');


    while(TERMINAL){

        //Get current dir
        getcwd(dir,sizeof(dir));

        //Vector to store the words in a command
        vector<string> command;

        //A print because is nice
        cout << dir << "> ";

        // Readind the command and put the words in vector
        getline(cin, currCommand);
        if(cin.eof()) TERMINAL=false;
        istringstream ss(currCommand);

        do {
            string word;
            ss >> word;
            char delimiter=balance(word.c_str());
            while(delimiter) {
                if(!ss) {
                    cout << "> ";
                    getline(cin,currCommand);
                    ss.clear();
                    ss.str(currCommand);
                } 
                string next_word;
                ss >> next_word;
                delimiter=balance(next_word.c_str(),delimiter);
                word=word+" "+next_word;
            }
            string new_word = clean_backwards(word);
            if(!new_word.empty())command.push_back(new_word);
        } while(ss);

        //Call the function that treat the string and run the command
        recursive_run(command);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x430, %rsp            # imm = 0x430
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x388(%rbp)
callq 0x34f0
movq -0x388(%rbp), %rdx
leaq 0x5ba8(%rip), %rsi      # 0xa02f
leaq -0x30(%rbp), %rdi
callq 0x5140
jmp 0x4492
leaq 0xae5f(%rip), %rdi      # 0xf2f8
leaq -0x30(%rbp), %rsi
callq 0x52f0
movq %rax, -0x390(%rbp)
jmp 0x44ab
movq -0x390(%rbp), %rax
leaq 0x4de7(%rip), %rcx      # 0x92a0
movq %rcx, (%rax)
leaq -0x30(%rbp), %rdi
callq 0x31a0
leaq -0x31(%rbp), %rdi
callq 0x32f0
leaq -0x69(%rbp), %rdi
movq %rdi, -0x398(%rbp)
callq 0x34f0
movq -0x398(%rbp), %rdx
leaq 0x5b3b(%rip), %rsi      # 0xa027
leaq -0x68(%rbp), %rdi
callq 0x5140
jmp 0x44f7
leaq 0xadfa(%rip), %rdi      # 0xf2f8
leaq -0x68(%rbp), %rsi
callq 0x52f0
movq %rax, -0x3a0(%rbp)
jmp 0x4510
movq -0x3a0(%rbp), %rax
leaq 0x5142(%rip), %rcx      # 0x9660
movq %rcx, (%rax)
leaq -0x68(%rbp), %rdi
callq 0x31a0
leaq -0x69(%rbp), %rdi
callq 0x32f0
leaq -0x90(%rbp), %rdi
callq 0x33c0
movb $0x27, -0x91(%rbp)
leaq 0xaddb(%rip), %rdi      # 0xf328
leaq -0x91(%rbp), %rsi
callq 0x53d0
movb %dl, -0x3a9(%rbp)
movq %rax, -0x3a8(%rbp)
jmp 0x4568
movb -0x3a9(%rbp), %al
movq -0x3a8(%rbp), %rcx
movq %rcx, -0xa8(%rbp)
movb %al, -0xa0(%rbp)
movb $0x22, -0xa9(%rbp)
leaq 0xad98(%rip), %rdi      # 0xf328
leaq -0xa9(%rbp), %rsi
callq 0x53d0
movb %dl, -0x3b9(%rbp)
movq %rax, -0x3b8(%rbp)
jmp 0x45ab
movb -0x3b9(%rbp), %al
movq -0x3b8(%rbp), %rcx
movq %rcx, -0xc0(%rbp)
movb %al, -0xb8(%rbp)
movb $0x5c, -0xc1(%rbp)
leaq 0xad85(%rip), %rdi      # 0xf358
leaq -0xc1(%rbp), %rsi
callq 0x53d0
movb %dl, -0x3c9(%rbp)
movq %rax, -0x3c8(%rbp)
jmp 0x45ee
movb -0x3c9(%rbp), %al
movq -0x3c8(%rbp), %rcx
movq %rcx, -0xd8(%rbp)
movb %al, -0xd0(%rbp)
movb $0x27, -0xd9(%rbp)
leaq 0xad42(%rip), %rdi      # 0xf358
leaq -0xd9(%rbp), %rsi
callq 0x53d0
movb %dl, -0x3d9(%rbp)
movq %rax, -0x3d8(%rbp)
jmp 0x4631
movb -0x3d9(%rbp), %al
movq -0x3d8(%rbp), %rcx
movq %rcx, -0xf0(%rbp)
movb %al, -0xe8(%rbp)
movb $0x22, -0xf1(%rbp)
leaq 0xacff(%rip), %rdi      # 0xf358
leaq -0xf1(%rbp), %rsi
callq 0x53d0
movb %dl, -0x3e9(%rbp)
movq %rax, -0x3e8(%rbp)
jmp 0x4674
movb -0x3e9(%rbp), %al
movq -0x3e8(%rbp), %rcx
movq %rcx, -0x108(%rbp)
movb %al, -0x100(%rbp)
testb $0x1, 0xac33(%rip)      # 0xf2c8
je 0x4b56
leaq 0xacee(%rip), %rdi      # 0xf390
movq %rdi, -0x400(%rbp)
movl $0x1000, %esi           # imm = 0x1000
callq 0x3160
leaq -0x120(%rbp), %rdi
callq 0x5420
movq -0x400(%rbp), %rsi
movq 0xa8fb(%rip), %rdi      # 0xefc8
callq 0x3280
movq %rax, -0x3f8(%rbp)
jmp 0x46db
movq -0x3f8(%rbp), %rdi
leaq 0x5949(%rip), %rsi      # 0xa032
callq 0x3280
jmp 0x46f0
movq 0xa8e9(%rip), %rdi      # 0xefe0
leaq -0x90(%rbp), %rsi
callq 0x3090
jmp 0x4705
movq 0xa8d4(%rip), %rdi      # 0xefe0
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3510
movb %al, -0x401(%rbp)
jmp 0x4723
movb -0x401(%rbp), %al
testb $0x1, %al
jne 0x4732
jmp 0x47c2
movb $0x0, 0xab8f(%rip)      # 0xf2c8
jmp 0x47c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4761
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x31a0
leaq -0x31(%rbp), %rdi
callq 0x32f0
jmp 0x4b7a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4792
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x31a0
leaq -0x69(%rbp), %rdi
callq 0x32f0
jmp 0x4b7a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4b6e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4b48
leaq -0x2a0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x8, %edx
callq 0x34b0
jmp 0x47dc
jmp 0x47de
leaq -0x2c0(%rbp), %rdi
movq %rdi, -0x410(%rbp)
callq 0x33c0
movq -0x410(%rbp), %rsi
leaq -0x2a0(%rbp), %rdi
callq 0x33a0
jmp 0x4806
leaq -0x2c0(%rbp), %rdi
callq 0x3100
movq %rax, %rdi
xorl %esi, %esi
callq 0x4200
movb %al, -0x411(%rbp)
jmp 0x4824
movb -0x411(%rbp), %al
movb %al, -0x2c1(%rbp)
cmpb $0x0, -0x2c1(%rbp)
je 0x49d9
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x2a0(%rbp,%rax), %rdi
callq 0x3130
movb %al, -0x412(%rbp)
jmp 0x485d
movb -0x412(%rbp), %al
testb $0x1, %al
jne 0x4869
jmp 0x48d7
movq 0xa758(%rip), %rdi      # 0xefc8
leaq 0x57bb(%rip), %rsi      # 0xa032
callq 0x3280
jmp 0x487e
movq 0xa75b(%rip), %rdi      # 0xefe0
leaq -0x90(%rbp), %rsi
callq 0x3090
jmp 0x4893
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x2a0(%rbp,%rax), %rdi
xorl %esi, %esi
callq 0x34c0
jmp 0x48af
leaq -0x2a0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3320
jmp 0x48c4
jmp 0x48d7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4b08
leaq -0x2e8(%rbp), %rdi
movq %rdi, -0x420(%rbp)
callq 0x33c0
movq -0x420(%rbp), %rsi
leaq -0x2a0(%rbp), %rdi
callq 0x33a0
jmp 0x48ff
leaq -0x2e8(%rbp), %rdi
callq 0x3100
movq %rax, %rdi
movsbl -0x2c1(%rbp), %esi
callq 0x4200
movb %al, -0x421(%rbp)
jmp 0x4922
movb -0x421(%rbp), %al
movb %al, -0x2c1(%rbp)
leaq 0x56fe(%rip), %rdx      # 0xa033
leaq -0x328(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
callq 0x5490
jmp 0x494a
leaq -0x308(%rbp), %rdi
leaq -0x328(%rbp), %rsi
leaq -0x2e8(%rbp), %rdx
callq 0x5440
jmp 0x4966
leaq -0x2c0(%rbp), %rdi
leaq -0x308(%rbp), %rsi
callq 0x3360
leaq -0x308(%rbp), %rdi
callq 0x31a0
leaq -0x328(%rbp), %rdi
callq 0x31a0
leaq -0x2e8(%rbp), %rdi
callq 0x31a0
jmp 0x4830
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x49c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x328(%rbp), %rdi
callq 0x31a0
leaq -0x2e8(%rbp), %rdi
callq 0x31a0
jmp 0x4b08
leaq -0x368(%rbp), %rdi
leaq -0x2c0(%rbp), %rsi
callq 0x3110
jmp 0x49ee
leaq -0x348(%rbp), %rdi
leaq -0x368(%rbp), %rsi
callq 0x42d0
jmp 0x4a03
leaq -0x368(%rbp), %rdi
callq 0x31a0
leaq -0x348(%rbp), %rdi
callq 0x34a0
testb $0x1, %al
jne 0x4a70
leaq -0x120(%rbp), %rdi
leaq -0x348(%rbp), %rsi
callq 0x5510
jmp 0x4a34
jmp 0x4a70
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x368(%rbp), %rdi
callq 0x31a0
jmp 0x4b08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x348(%rbp), %rdi
callq 0x31a0
jmp 0x4b08
leaq -0x348(%rbp), %rdi
callq 0x31a0
leaq -0x2c0(%rbp), %rdi
callq 0x31a0
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x2a0(%rbp,%rax), %rdi
callq 0x3390
movb %al, -0x422(%rbp)
jmp 0x4aa8
movb -0x422(%rbp), %al
testb $0x1, %al
jne 0x47de
jmp 0x4ab8
leaq -0x380(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x4e10
jmp 0x4acd
xorl %eax, %eax
movl %eax, %esi
leaq -0x380(%rbp), %rdi
callq 0x38e0
jmp 0x4adf
leaq -0x380(%rbp), %rdi
callq 0x4f00
leaq -0x2a0(%rbp), %rdi
callq 0x3140
leaq -0x120(%rbp), %rdi
callq 0x4f00
jmp 0x468e
leaq -0x2c0(%rbp), %rdi
callq 0x31a0
jmp 0x4b3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4b3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x380(%rbp), %rdi
callq 0x4f00
leaq -0x2a0(%rbp), %rdi
callq 0x3140
leaq -0x120(%rbp), %rdi
callq 0x4f00
jmp 0x4b6e
leaq -0x90(%rbp), %rdi
callq 0x31a0
movl -0x4(%rbp), %eax
addq $0x430, %rsp            # imm = 0x430
popq %rbp
retq
leaq -0x90(%rbp), %rdi
callq 0x31a0
movq -0x40(%rbp), %rdi
callq 0x34e0
nopw %cs:(%rax,%rax)
nopl (%rax)