2018-10-06 14:35:17 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2019-01-22 16:38:09 +00:00
|
|
|
* Copyright (C) 2019 KiCad Developers, see CHANGELOG.TXT for contributors.
|
2018-10-06 14:35:17 +00:00
|
|
|
*
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
#include "pcb_parser_tool.h"
|
2018-10-06 14:35:17 +00:00
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
|
|
|
|
#include <wx/cmdline.h>
|
|
|
|
|
|
|
|
#include <class_board_item.h>
|
2018-10-06 14:35:17 +00:00
|
|
|
#include <kicad_plugin.h>
|
|
|
|
#include <pcb_parser.h>
|
|
|
|
#include <richio.h>
|
|
|
|
|
|
|
|
#include <wx/cmdline.h>
|
|
|
|
|
2019-01-28 10:36:54 +00:00
|
|
|
#include <qa_utils/scoped_timer.h>
|
|
|
|
#include <qa_utils/stdstream_line_reader.h>
|
2018-10-06 14:35:17 +00:00
|
|
|
|
|
|
|
using PARSE_DURATION = std::chrono::microseconds;
|
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
|
2018-10-06 14:35:17 +00:00
|
|
|
/**
|
|
|
|
* Parse a PCB or footprint file from the given input stream
|
|
|
|
*
|
|
|
|
* @param aStream the input stream to read from
|
|
|
|
* @return success, duration (in us)
|
|
|
|
*/
|
2019-01-22 16:38:09 +00:00
|
|
|
bool parse( std::istream& aStream, bool aVerbose )
|
2018-10-06 14:35:17 +00:00
|
|
|
{
|
|
|
|
// Take input from stdin
|
|
|
|
STDISTREAM_LINE_READER reader;
|
|
|
|
reader.SetStream( aStream );
|
|
|
|
|
|
|
|
PCB_PARSER parser;
|
|
|
|
|
|
|
|
parser.SetLineReader( &reader );
|
|
|
|
|
|
|
|
BOARD_ITEM* board = nullptr;
|
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
PARSE_DURATION duration{};
|
2018-10-06 14:35:17 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SCOPED_TIMER<PARSE_DURATION> timer( duration );
|
|
|
|
board = parser.Parse();
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& parse_error )
|
|
|
|
{
|
|
|
|
std::cerr << parse_error.Problem() << std::endl;
|
|
|
|
std::cerr << parse_error.Where() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aVerbose )
|
|
|
|
{
|
|
|
|
std::cout << "Took: " << duration.count() << "us" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return board != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
|
|
|
|
{ wxCMD_LINE_SWITCH, "h", "help", _( "displays help on the command line parameters" ).mb_str(),
|
|
|
|
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
|
|
|
|
{ wxCMD_LINE_SWITCH, "v", "verbose", _( "print parsing information" ).mb_str() },
|
|
|
|
{ wxCMD_LINE_PARAM, nullptr, nullptr, _( "input file" ).mb_str(), wxCMD_LINE_VAL_STRING,
|
|
|
|
wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
|
2018-10-06 14:35:17 +00:00
|
|
|
{ wxCMD_LINE_NONE }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
enum PARSER_RET_CODES
|
2018-10-06 14:35:17 +00:00
|
|
|
{
|
2019-01-24 12:12:25 +00:00
|
|
|
PARSE_FAILED = KI_TEST::RET_CODES::TOOL_SPECIFIC,
|
2018-10-06 14:35:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
int pcb_parser_main_func( int argc, char** argv )
|
2018-10-06 14:35:17 +00:00
|
|
|
{
|
|
|
|
#ifdef __AFL_COMPILER
|
|
|
|
__AFL_INIT();
|
|
|
|
#endif
|
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
wxMessageOutput::Set( new wxMessageOutputStderr );
|
2018-10-06 14:35:17 +00:00
|
|
|
wxCmdLineParser cl_parser( argc, argv );
|
|
|
|
cl_parser.SetDesc( g_cmdLineDesc );
|
2019-01-22 16:38:09 +00:00
|
|
|
cl_parser.AddUsageText(
|
|
|
|
_( "This program parses PCB files, either from the "
|
|
|
|
"stdin stream or from the given filenames. This can be used either for "
|
|
|
|
"standalone testing of the parser or for fuzz testing." ) );
|
2018-10-06 14:35:17 +00:00
|
|
|
|
|
|
|
int cmd_parsed_ok = cl_parser.Parse();
|
|
|
|
if( cmd_parsed_ok != 0 )
|
|
|
|
{
|
|
|
|
// Help and invalid input both stop here
|
2019-01-24 12:12:25 +00:00
|
|
|
return ( cmd_parsed_ok == -1 ) ? KI_TEST::RET_CODES::OK : KI_TEST::RET_CODES::BAD_CMDLINE;
|
2018-10-06 14:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const bool verbose = cl_parser.Found( "verbose" );
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
const auto file_count = cl_parser.GetParamCount();
|
|
|
|
|
2019-01-22 16:38:09 +00:00
|
|
|
if( file_count == 0 )
|
2018-10-06 14:35:17 +00:00
|
|
|
{
|
|
|
|
// Parse the file provided on stdin - used by AFL to drive the
|
|
|
|
// program
|
|
|
|
// while (__AFL_LOOP(2))
|
|
|
|
{
|
|
|
|
ok = parse( std::cin, verbose );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Parse 'n' files given on the command line
|
|
|
|
// (this is useful for input minimisation (e.g. afl-tmin) as
|
|
|
|
// well as manual testing
|
|
|
|
for( unsigned i = 0; i < file_count; i++ )
|
|
|
|
{
|
|
|
|
const auto filename = cl_parser.GetParam( i ).ToStdString();
|
|
|
|
|
|
|
|
if( verbose )
|
|
|
|
std::cout << "Parsing: " << filename << std::endl;
|
|
|
|
|
|
|
|
std::ifstream fin;
|
|
|
|
fin.open( filename );
|
|
|
|
|
|
|
|
ok = ok && parse( fin, verbose );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !ok )
|
2019-01-22 16:38:09 +00:00
|
|
|
return PARSER_RET_CODES::PARSE_FAILED;
|
2018-10-06 14:35:17 +00:00
|
|
|
|
2019-01-24 12:12:25 +00:00
|
|
|
return KI_TEST::RET_CODES::OK;
|
2018-10-23 22:35:33 +00:00
|
|
|
}
|
2019-01-22 16:38:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define the tool interface
|
|
|
|
*/
|
2019-01-24 12:12:25 +00:00
|
|
|
KI_TEST::UTILITY_PROGRAM pcb_parser_tool = {
|
2019-01-22 16:38:09 +00:00
|
|
|
"pcb_parser",
|
|
|
|
"Parse a KiCad PCB file",
|
|
|
|
pcb_parser_main_func,
|
|
|
|
};
|