KiCad PCB EDA Suite
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 <profile.h>
30
31#include <wx/cmdline.h>
32
33#include <board_item.h>
36#include <richio.h>
38
39
40using PARSE_DURATION = std::chrono::microseconds;
41
42
49bool parse( std::istream& aStream, bool aVerbose )
50{
51 // Take input from stdin
53 reader.SetStream( aStream );
54
55 PCB_PARSER parser( &reader, nullptr, nullptr );
56 BOARD_ITEM* board = nullptr;
57
58 PARSE_DURATION duration{};
59
60 try
61 {
62 PROF_TIMER timer;
63 board = parser.Parse();
64
65 duration = timer.SinceStart<PARSE_DURATION>();
66 }
67 catch( const IO_ERROR& )
68 {
69 }
70
71 if( aVerbose )
72 {
73 std::cout << "Took: " << duration.count() << "us" << std::endl;
74 }
75
76 return board != nullptr;
77}
78
79
80static const wxCmdLineEntryDesc g_cmdLineDesc[] = {
81 { wxCMD_LINE_SWITCH, "h", "help", _( "displays help on the command line parameters" ).mb_str(),
82 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
83 { wxCMD_LINE_SWITCH, "v", "verbose", _( "print parsing information" ).mb_str() },
84 { wxCMD_LINE_PARAM, nullptr, nullptr, _( "input file" ).mb_str(), wxCMD_LINE_VAL_STRING,
85 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE },
86 { wxCMD_LINE_NONE }
87};
88
89
91{
93};
94
95
96int pcb_parser_main_func( int argc, char** argv )
97{
98#ifdef __AFL_COMPILER
99 __AFL_INIT();
100#endif
101
102 wxMessageOutput::Set( new wxMessageOutputStderr );
103 wxCmdLineParser cl_parser( argc, argv );
104 cl_parser.SetDesc( g_cmdLineDesc );
105 cl_parser.AddUsageText( _( "This program parses PCB files, either from the stdin stream or "
106 "from the given filenames. This can be used either for standalone "
107 "testing of the parser or for fuzz testing." ) );
108
109 int cmd_parsed_ok = cl_parser.Parse();
110 if( cmd_parsed_ok != 0 )
111 {
112 // Help and invalid input both stop here
113 return ( cmd_parsed_ok == -1 ) ? KI_TEST::RET_CODES::OK : KI_TEST::RET_CODES::BAD_CMDLINE;
114 }
115
116 const bool verbose = cl_parser.Found( "verbose" );
117 bool ok = true;
118 const size_t file_count = cl_parser.GetParamCount();
119
120 if( file_count == 0 )
121 {
122 // Parse the file provided on stdin - used by AFL to drive the
123 // program
124 // while (__AFL_LOOP(2))
125 {
126 ok = parse( std::cin, verbose );
127 }
128 }
129 else
130 {
131 // Parse 'n' files given on the command line
132 // (this is useful for input minimisation (e.g. afl-tmin) as
133 // well as manual testing
134 for( unsigned i = 0; i < file_count; i++ )
135 {
136 const auto filename = cl_parser.GetParam( i ).ToStdString();
137
138 if( verbose )
139 std::cout << "Parsing: " << filename << std::endl;
140
141 std::ifstream fin;
142 fin.open( filename );
143
144 ok = ok && parse( fin, verbose );
145 }
146 }
147
148 if( !ok )
150
152}
153
154
155static bool registered = UTILITY_REGISTRY::Register( { "pcb_parser",
156 "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:58
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
Read a Pcbnew s-expression formatted LINE_READER object and returns the appropriate BOARD_ITEM object...
Definition: pcb_parser.h:74
BOARD_ITEM * Parse()
Definition: pcb_parser.cpp:681
A small class to help profiling.
Definition: profile.h:47
DURATION SinceStart(bool aSinceLast=false)
Definition: profile.h:133
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)
#define 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