rob08.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Kamil Dudka <xdudka00@stud.fit.vutbr.cz>
00003  *
00004  * This file is part of rob08
00005  *
00006  * rob08 is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * any later version.
00010  *
00011  * rob08 is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with rob08.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include "config.h"
00021 
00022 #include "core.h"
00023 #include "scanner.h"
00024 #include "robIO.h"
00025 #include "term.h"
00026 
00027 #ifndef BUILDING_DOX
00028 #   include <signal.h>
00029 #   include <string>
00030 #   include <strings.h>
00031 #endif
00032 
00033 using namespace StreamDecorator;
00034 using std::string;
00035 
00040 void printUsage(string path) {
00041     std::cout << "Usage: " << path << " PTY_DEVICE" << std::endl;
00042 }
00043 
00044 void signalHandler(int) {
00045     ::bSignal = 1;
00046 }
00047 
00048 int main(int argc, char *argv[]) {
00049 #if CONSOLE_COLOR_OUTPUT
00050     Color::enable(true);
00051 #endif
00052     if (argc != 2) {
00053         // invalid count of arguments
00054         if (argc > 0)
00055             printUsage(argv[0]);
00056         return -1;
00057     }
00058     std::string fileName(argv[1]);
00059     if (fileName == string("--help")) {
00060         // --help
00061         printUsage(argv[0]);
00062         return 0;
00063     }
00064 
00065     // set signal handler
00066     struct sigaction sa;
00067     bzero(&sa, sizeof(sa));
00068     sa.sa_handler = signalHandler;
00069     if (0!= sigaction(SIGUSR1, &sa, NULL))
00070         std::cerr << Error(E_WARNING, argv[0], "can't set SIGUSR1 signal handler") << std::endl;
00071     if (0!= sigaction(SIGPIPE, &sa, NULL))
00072         std::cerr << Error(E_WARNING, argv[0], "can't set SIGPIPE signal handler") << std::endl;
00073 
00074     // open terminal
00075     ITerm *term = TermFactory::createTerm(fileName);
00076     if (!term)
00077         return -1;
00078 
00079     // create scanner
00080     IScanner *scanner = ScannerFactory::createScanner(term, fileName);
00081     if (!scanner) {
00082         delete term;
00083         return -1;
00084     }
00085 
00086     // create core
00087     IListener *core = CoreFactory::createCore(term);
00088     if (!core) {
00089         delete scanner;
00090         delete term;
00091         return -1;
00092     }
00093 
00094     // main loop
00095     Token t;
00096     while (scanner->readNext(t) && core->dispatch(t)) {
00097         if (::bSignal) {
00098             std::cerr << Error(E_NOTE, argv[0], "signal caught in main loop") << std::endl;
00099             break;
00100         }
00101     }
00102 
00103     // check for lexical errors
00104     bool bErr = false;
00105     if (term -> hasError()) {
00106         bErr = true;
00107         std::cerr << Error(E_NOTE, fileName, "terminal error(s) detected") << std::endl;
00108     }
00109 
00110     if (scanner -> hasError()) {
00111         bErr = true;
00112         std::cerr << Error(E_NOTE, fileName, "lexical error(s) detected") << std::endl;
00113     }
00114 
00115     if (core -> hasError()) {
00116         bErr = true;
00117         std::cerr << Error(E_NOTE, fileName, "runtime error(s) detected") << std::endl;
00118     }
00119 
00120     // destroy objects in reverse order
00121     delete core;
00122     delete scanner;
00123     delete term;
00124 
00125     // final application exit code
00126     return bErr ? -1 : 0;
00127 }
00128 

Generated on Fri Jul 10 22:42:01 2009 for rob08 by  doxygen 1.5.4