#include #include #include #include #include #include #include #include "disasm.h" #include "CInstrFactory.hpp" using boost::lexical_cast; using boost::bad_lexical_cast; using namespace std; using namespace boost::program_options; namespace po = boost::program_options; std::string progName; CCpu* Iinstr::m_cpu; disasm* Iinstr::m_disasm; int main(int argc, char* argv[]) { progName = argv[0]; ifstream inFile; try { po::options_description desc("Allowed options"); desc.add_options() ("help,h","produce help message") ("file,f",value(), "input file") ; po::positional_options_description p; p.add("file",1); po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); po::notify(vm); if(vm.count("help")) { cout << desc << endl; return EXIT_FAILURE; } if(vm.count("file")) { #ifdef DEBUG cout << "going to open file " << vm["file"].as() << endl; #endif inFile.open(vm["file"].as(), ios::in); if(!inFile) { cerr << "Error opening file " << vm["file"].as() << endl; return EXIT_FAILURE; } } else { cout << "not input file given!" << endl << endl; cout << desc << endl; return EXIT_FAILURE; } } catch(std::exception& ex) { cout << ex.what() << endl; } string dir = "./instr/"; map instr; CInstrFactory instrFab; try { instrFab.loadLibsIntoMap(instr, dir); } catch(std::bad_alloc& e) { cerr << progName << ": bad_alloc caught " << e.what() << endl; exit(EXIT_FAILURE); } std::string str = ""; boost::char_separator sep(";", "", boost::keep_empty_tokens); boost::tokenizer > tokens(str, sep); disasm disasm(instr); while(getline(inFile, str)) { int count = 0; tokens.assign(str); stringstream out; int type = 0; for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) { if(tok_iter == tokens.begin()) { try { type = lexical_cast(*tok_iter); count++; continue; } catch(bad_lexical_cast &) { break; } cout << endl; } switch(type) { case 2: if(count == 1) { cout << "; "; } cout << *tok_iter; break; case 3: if((*tok_iter).size() > 0) { if(count > 1) { cout << endl; } cout << *tok_iter << ":"; } break; case 1: if(count == 1) { out << "[0x" << *tok_iter << "]: "; } else if(count == 2) { out << disasm.decodeToString(*tok_iter); } else if(count == 3) { //code saved in hex-file //cout << *tok_iter ; } else if(count == 4) { /* label */ if((*tok_iter).size() > 0) { cout << *tok_iter << ":" << endl; } cout << out.str(); } else if(count == 5) { if((*tok_iter).size() > 0) { cout << " ;"; } } if(count >= 5) { cout << *tok_iter; } break; default: cerr << "i was to lazy to implement the other format types for now" << endl; } count++; } if(type == 1 && count <= 4) { cout << out.str(); } cout << endl; } inFile.close(); cout << endl; return EXIT_SUCCESS; }