chk.cpp

00001 /*
00002  *  TOPPERS/FDMP Kernel
00003  *      Toyohashi Open Platform for Embedded Real-Time Systems/
00004  *      Function Distributed Multiprocessor Kernel
00005  *
00006  *  Copyright (C) 2005 by Takagi Nobuhisa
00007  * 
00008  *  上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation 
00009  *  によって公表されている GNU General Public License の Version 2 に記
00010  *  述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
00011  *  を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
00012  *  利用と呼ぶ)することを無償で許諾する.
00013  *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
00014  *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
00015  *      スコード中に含まれていること.
00016  *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
00017  *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
00018  *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
00019  *      の無保証規定を掲載すること.
00020  *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
00021  *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
00022  *      と.
00023  *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
00024  *        作権表示,この利用条件および下記の無保証規定を掲載すること.
00025  *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
00026  *        報告すること.
00027  *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
00028  *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
00029  * 
00030  *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
00031  *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
00032  *  含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
00033  *  接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
00034  * 
00035  */
00036 
00037 /*
00038  *  chk.cpp
00039  */
00040 #include "chk.hpp"
00041 #include <iostream>
00042 #include <fstream>
00043 #include <cstdlib>
00044 
00045 namespace
00046 {
00047 
00051   void help_message()
00052   {
00053     static const char message[] =
00054       "Usage: cfg [options] file\n"
00055       "Options:\n"
00056       "  -h   --help              Display this information\n"
00057       "  -lj  --japanese          Use Japanese\n"
00058       "  -le  --english           Use English\n"
00059       "  -m   --module=<file>     Specify the <file> for a load module\n"
00060       "  -cs  --script=<file>     Specify the <file> instead of `kernel_chk.srec\'\n"
00061       "\n";
00062     
00063     std::cerr << _( message ) << std::endl;
00064   }
00065 
00074   void select_language( const toppers::cmdline::option_array& options )
00075   {
00076     if ( options.find( "-lj" ) )
00077     {
00078       set_language( toppers::reporter::Japanese );
00079     }
00080   }
00081 
00090   std::auto_ptr<std::string> parse_cmdline( int argc, char* argv[], toppers::cmdline::option_array& opts )
00091   {
00092     using namespace toppers;
00093     using namespace toppers::itron;
00094 
00095     cmdline cmdl( argc, argv );
00096     cmdline::option_array options;
00097     const char pattern[] = "-h --help "
00098                            "-lj --japanese "
00099                            "-le --english "
00100                            "-m: --module= "
00101                            "-cs: --script= "
00102                            "-cpu: "
00103                            "-system: "
00104                            "-tool: "
00105                            "-obj "
00106                            "-debug";
00107     std::auto_ptr<std::vector<std::string> > sources( cmdl.parse( pattern, options ) );
00108 
00109     options.remap_option( "--help", "-h" );
00110     options.remap_option( "--japanese", "-lj" );
00111     options.remap_option( "--english", "-le" );
00112 
00113     select_language( options );
00114 
00115     if ( options.find( "-h" ) )
00116     {
00117       help_message();
00118       toppers::reporter::terminator x;
00119       throw x;
00120     }
00121 
00122     std::vector<std::string> t;
00123     if ( options.find( "-s", t ) )
00124     {
00125       std::copy( t.begin(), t.end(), std::back_inserter( *sources ) );
00126     }
00127 
00128     if ( !sources->empty() )
00129     {
00130       std::vector<std::string>::const_iterator iter( cmdline::find_illegal_options( sources->begin(), sources->end() ) );
00131       if ( iter == sources->end() )
00132       {
00133         if ( sources->size() > 1 )
00134         {
00135           stop( _( "too many input files" ) );  // 入力ファイルが複数
00136         }
00137       }
00138       else
00139       {
00140         error( _( "illegal option `%1%\'" ) % *iter ); // 不正なオプションが混在
00141       }
00142     }
00143 
00144     std::auto_ptr<std::string> source;
00145     if ( !sources->empty() )
00146     {
00147       source.reset( new std::string( sources->front() ) );
00148     }
00149     opts.swap( options );
00150     return source;
00151   }
00152 
00158   int chk_main( int argc, char** argv )
00159   {
00160     using namespace toppers;
00161     using namespace toppers::itron;
00162 
00163     // コマンドライン引数の解析
00164     cmdline::option_array options;
00165     std::auto_ptr<std::string> source( parse_cmdline( argc, argv, options ) );
00166 
00167     // "kernel_chk.srec"の読み込み
00168     std::string script( "kernel_chk.srec" );
00169     options.find( "-cs", script );
00170     std::ifstream ifs( script.c_str() );
00171     if ( !ifs.is_open() )
00172     {
00173       error( _( "cannot open file `%1%\'" ) % script );
00174     }
00175     s_record chk_srec( ifs );
00176 
00177     // ファクトリの生成
00178     long prid = kernel_chk::read_prid( chk_srec );
00179     boost::shared_ptr<chk_factory> factory( chk_factory::build( options, prid ) );
00180     chk_factory::global( factory );
00181 
00182     boost::shared_ptr<kernel_chk> k_chk( factory->create_kernel_chk() );
00183     k_chk->load( chk_srec );
00184 
00185     // "jsp.syms"および"jsp.srec"相当のファイル名を取得
00186     std::string symbols( "jsp.syms" );
00187     std::string load_module( "jsp.srec" );
00188     std::string opt_m;
00189     if ( options.find( "-m", opt_m ) )
00190     {
00191       std::string::size_type pos = opt_m.find( ',' );
00192       symbols = opt_m.substr( 0, pos );
00193       if ( pos != std::string::npos )
00194       {
00195         load_module = opt_m.substr( pos+1 );
00196       }
00197     }
00198 
00199     // "jsp.syms"および"jsp.srec"の読み込み
00200     std::ifstream ifs_srec( load_module.c_str() );
00201     s_record lm_srec( ifs_srec );
00202     std::ifstream ifs_syms( symbols.c_str() );
00203     nm_symbol syms( ifs_syms );
00204 
00205     // 各カーネルオブジェクトのパラメータチェック
00206     boost::shared_ptr<std::vector<chk_factory::checker_type> > checkers( factory->get_checkers() );
00207     typedef std::vector<chk_factory::checker_type>::const_iterator checker_iterator;
00208     for ( checker_iterator iter( checkers->begin() ), last( checkers->end() ); iter != last; ++iter )
00209     {
00210       if ( !( *iter )( *k_chk, lm_srec, syms ) )
00211       {
00212         return EXIT_FAILURE;
00213       }
00214     }
00215 
00216     std::cout << "check complete" << std::endl;
00217 
00218     return EXIT_SUCCESS;
00219   }
00220 
00221 }
00222 
00223 int main( int argc, char** argv )
00224 {
00225   int status;
00226   try
00227   {
00228     chk_init();
00229     status = chk_main( argc, argv );
00230     if ( status != EXIT_SUCCESS )
00231     {
00232       toppers::error( _( "unknown error" ) );
00233     }
00234   }
00235   catch ( toppers::reporter::terminator& )
00236   {
00237     status = EXIT_FAILURE;
00238   }
00239   catch ( std::exception& e )
00240   {
00241     std::fputs( e.what(), stderr );
00242     std::fputc( '\n', stderr );
00243     status = EXIT_FAILURE;
00244   }
00245   catch ( ... )
00246   {
00247     toppers::stop( _( "internal error" ) );
00248     status = EXIT_FAILURE;
00249   }
00250   return status;
00251 }

Copyright © 2006 by TAKAGI Nobuhisa.
このページは Wed Apr 12 16:31:55 2006 に Doxygen によって生成されました。