KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_parser_tool.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <cstdio>
27#include <string>
28#include <common.h>
29#include <core/profile.h>
30
31#include <wx/cmdline.h>
32#include <wx/msgout.h>
33
34#include <board_item.h>
37#include <richio.h>
39
40
41using PARSE_DURATION = std::chrono::microseconds;
42
43
50bool parse( std::istream& aStream, bool aVerbose )
51{
52 // Take input from stdin
54 reader.SetStream( aStream );
55
56 PCB_IO_KICAD_SEXPR_PARSER parser( &reader, nullptr, nullptr );
57 BOARD_ITEM* board = nullptr;
58
59 PARSE_DURATION duration{};
60
61 try
62 {
63 PROF_TIMER timer;
64 board = parser.Parse();
65
66 duration = timer.SinceStart<PARSE_DURATION>();
67 }
68 catch( const IO_ERROR& )
69 {
70 }
71
72 if( aVerbose )
73 {
74 std::cout << "Took: " << duration.count() << "us" << std::endl;
75 }
76
77 return board != nullptr;
78}
79
80
81static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
82 { wxCMD_LINE_SWITCH, "h", "help", _( "displays help on the command line parameters" ).mb_str(),
83 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
84 { wxCMD_LINE_SWITCH, "v", "verbose", _( "print parsing information" ).mb_str() },
85 { wxCMD_LINE_PARAM, nullptr, nullptr, _( "input file" ).mb_str(), wxCMD_LINE_VAL_STRING,
86 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
87 { wxCMD_LINE_NONE }
88};
89
90
92{
94};
95
96
97int pcb_parser_main_func( int argc, char** argv )
98{
99#ifdef __AFL_COMPILER
100 __AFL_INIT();
101#endif
102
103 wxMessageOutput::Set( new wxMessageOutputStderr );
104 wxCmdLineParser cl_parser( argc, argv );
105 cl_parser.SetDesc( g_cmdLineDesc );
106 cl_parser.AddUsageText( _( "This program parses PCB files, either from the stdin stream or "
107 "from the given filenames. This can be used either for standalone "
108 "testing of the parser or for fuzz testing." ) );
109
110 int cmd_parsed_ok = cl_parser.Parse();
111 if( cmd_parsed_ok != 0 )
112 {
113 // Help and invalid input both stop here
114 return ( cmd_parsed_ok == -1 ) ? KI_TEST::RET_CODES::OK : KI_TEST::RET_CODES::BAD_CMDLINE;
115 }
116
117 const bool verbose = cl_parser.Found( "verbose" );
118 bool ok = true;
119 const size_t file_count = cl_parser.GetParamCount();
120
121 if( file_count == 0 )
122 {
123 // Parse the file provided on stdin - used by AFL to drive the
124 // program
125 // while (__AFL_LOOP(2))
126 {
127 ok = parse( std::cin, verbose );
128 }
129 }
130 else
131 {
132 // Parse 'n' files given on the command line
133 // (this is useful for input minimisation (e.g. afl-tmin) as
134 // well as manual testing
135 for( unsigned i = 0; i < file_count; i++ )
136 {
137 const auto filename = cl_parser.GetParam( i ).ToStdString();
138
139 if( verbose )
140 std::cout << "Parsing: " << filename << std::endl;
141
142 std::ifstream fin;
143 fin.open( filename );
144
145 ok = ok && parse( fin, verbose );
146 }
147 }
148
149 if( !ok )
150 return PARSER_RET_CODES::PARSE_FAILED;
151
153}
154
155
156static bool registered = UTILITY_REGISTRY::Register( { "pcb_parser",
157 "Parse a KiCad PCB file",
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
A small class to help profiling.
Definition: profile.h:49
DURATION SinceStart(bool aSinceLast=false)
Definition: profile.h:135
LINE_READER that wraps a given std::istream instance.
void SetStream(std::istream &aStream)
Set the stream for this line reader.
static bool Register(const KI_TEST::UTILITY_PROGRAM &aProgInfo)
Register a utility program factory function against an ID string.
The common library.
#define _(s)
@ OK
Tool exited OK.
@ TOOL_SPECIFIC
Tools can define their own statuses from here onwards.
@ BAD_CMDLINE
The command line was not correct for the tool.
Pcbnew s-expression file format parser definition.
static bool registered
std::chrono::microseconds PARSE_DURATION
bool parse(std::istream &aStream, bool aVerbose)
Parse a PCB or footprint file from the given input stream.
static const wxCmdLineEntryDesc g_cmdLineDesc[]
@ PARSE_FAILED
int pcb_parser_main_func(int argc, char **argv)
PARSER_RET_CODES