/* $Id$ */ /* * Copyright (C) 2005-2006 Richard Braun * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** * \file main_cli.cc * \brief CLI interface implementation. * * Implements a command line interface (CLI) to the mtx engine. The CLI is * based on libreadline. */ #include #include #include #include #include #include #include #include #include "mtxsymbol.h" #include "mtxlanguage.h" #include "mtxexception.h" #define PROMPT "mtx_cli> " using namespace std; static bool mtx_readline(istream *input, string &line) { if (input == &cin) { char *tmp; tmp = readline(PROMPT); if (tmp == NULL) return false; add_history(tmp); line = tmp; free(tmp); return true; } else { if (getline(*input, line)) { cout << PROMPT << line << endl; return true; } else return false; } } int main(int argc, char *argv[]) { vector::iterator results_iterator; vector results; MtxLanguage language; ifstream infile; istream *input; string line; cout << MTX_NOTICE << endl; if (argc == 1) { rl_bind_key('\t', rl_insert); input = &cin; } else if (argc == 2) { infile.open(argv[1]); if (!infile.is_open()) { cerr << "error: unable to open file" << endl; return 1; } input = &infile; } else { cout << "usage: mtx_cli [input_file]" << endl; return 1; } while (mtx_readline(input, line)) { try { results = language.parse(line); for (results_iterator = results.begin(); results_iterator != results.end(); results_iterator++) { if (!(*results_iterator).isSpecial()) cout << (*results_iterator).getName() << " = "; cout << (*results_iterator).toString() << endl; } } catch (MtxException &exception) { cout << "error: " << exception.what() << endl; } } if (input == &cin) { clear_history(); cout << endl; } else infile.close(); return EXIT_SUCCESS; }