forked from seladb/PcapPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure-windows.bat
executable file
·212 lines (181 loc) · 8.92 KB
/
configure-windows.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
@echo OFF
setlocal
echo.
echo ******************************************
echo PcapPlusPlus Windows configuration script
echo ******************************************
echo.
set PLATFORM_MK=mk\platform.mk
set PCAPPLUSPLUS_MK=mk\PcapPlusPlus.mk
:: initially set MINGW_HOME and WINPCAP_HOME to empty values
set MINGW_HOME=
set WINPCAP_HOME=
:: check the number of arguments: If got at least one argument continue to command-line mode, else continue to wizard mode
if "%1" NEQ "" (
call :GETOPT %1 %2 %3 %4 %5 %6 %7 %8 %9
) else (
call :READ_PARAMS_FROM_USER
)
:: if one of the modes returned with an error, exit script
if "%ERRORLEVEL%" NEQ "0" exit /B 1
:: verify that both variables MINGW_HOME and WINPCAP_HOME are set
if "%MINGW_HOME%"=="" echo MinGW directory was not supplied. Exiting & exit /B 1
if "%WINPCAP_HOME%"=="" echo WinPcap directory was not supplied. Exiting & exit /B 1
:: replace "\" with "/" in MINGW_HOME
set MINGW_HOME=%MINGW_HOME:\=/%
:: remove trailing "/" in MINGW_HOME if exists
if "%MINGW_HOME:~-1%"=="/" set MINGW_HOME=%MINGW_HOME:~,-1%
:: replace "\" with "/" in WINPCAP_HOME
set WINPCAP_HOME=%WINPCAP_HOME:\=/%
:: remove trailing "/" in WINPCAP_HOME if exists
if "%WINPCAP_HOME:~-1%"=="/" set WINPCAP_HOME=%WINPCAP_HOME:~,-1%
:: delete existing platform.mk and PcapPlusPlus.mk if exist
if exist %PLATFORM_MK% (del %PLATFORM_MK%)
if exist %PCAPPLUSPLUS_MK% (del %PCAPPLUSPLUS_MK%)
:: set PcapPlusPlus home varaible as current directory in both platform.mk and PcapPlusPlus.mk
set CUR_DIR=%cd:\=/%
echo PCAPPLUSPLUS_HOME := %CUR_DIR%>> %PLATFORM_MK%
echo. >> %PLATFORM_MK%
echo PCAPPLUSPLUS_HOME := %CUR_DIR%>> %PCAPPLUSPLUS_MK%
echo. >> %PCAPPLUSPLUS_MK%
:: set MinGW and WinPcap locations in platform.mk.win32 and create platform.mk
for /F "tokens=1* delims=]" %%A in ('type "mk\platform.mk.win32"') do (
echo. >>%PLATFORM_MK%
if "%%A" EQU "MINGW_HOME :=" (echo %%A %MINGW_HOME%>>%PLATFORM_MK%) else (if "%%A" EQU "WINPCAP_HOME :=" (echo %%A %WINPCAP_HOME%>>%PLATFORM_MK%) else (echo %%A>>%PLATFORM_MK%))
)
:: copy the content of PcapPlusPlus.mk.common to PcapPlusPlus.mk
type mk\PcapPlusPlus.mk.common >> %PCAPPLUSPLUS_MK%
:: copy the content of PcapPlusPlus.mk.win32 to PcapPlusPlus.mk (append current content)
type mk\PcapPlusPlus.mk.win32 >> %PCAPPLUSPLUS_MK%
:: configuration completed
echo.
echo PcapPlusPlus configuration is complete. Files created (or modified): %PLATFORM_MK%, %PCAPPLUSPLUS_MK%
:: exit script
exit /B 0
:: -------------------------------------------------------------------
:: an implementation of getopt for Windows and specifically for PcapPlusPlus
:: this "function" takes as paramters all command-line arguments given by the user who runs the script
:: then it parses the command-line arguments and calls switch cases per argument
:: it returns with the following exit codes:
:: - exit code 0 if arguments were parsed ok
:: - exit code 1 if an unknown argument was given or none arguments were given at all
:: - exit code 2 if a required parameter was not supplied for one of the switches (for example: -g instead of -g <NUM>)
:: - exit code 3 if one of the command-line arguments asked to exit the script (for example the -h switch displays help and exits)
:GETOPT
:: if no arguments were passed exit with error code 1
if "%1"=="" call :GETOPT_ERROR "No parameters suppplied" & exit /B 1
:GETOPT_START
:: the HAS_PARAM varaible states whether the switch has a parameter, for example '-a 111' means switch '-a' has the parameter '111'
:: initially this variable is set to 0
set HAS_PARAM=0
:: if no command-line arguments are left, exit getopt
if "%1"=="" goto GETOPT_END
:: get the next switch (the one in %1) and call the relevant case for that switch
2>NUL call :CASE%1 %1 %2 %3 %4 %5 %6 %7 %8 %9
:: ERRORLEVEL 3 means the case asked to exit script. Return this error code to the caller
if ERRORLEVEL 3 exit /B 3
:: ERRORLEVEL 2 means the current switch doesn't have a required parameter. Return this error code to the caller
if ERRORLEVEL 2 exit /B 2
:: ERRORLEVEL 1 means the switch is unknown (no case was found for it). Return this error code to the caller
if ERRORLEVEL 1 call :GETOPT_ERROR "Unkown parameter %1" & exit /B 1
:: shift-left the command-line arguments, meaning put %2 in %1, %3 in %2, %4 in %3 and so on. This way %1 always holds the next switch to parse and handle
shift /1
:: if the current switch had a parameter shift again because %1 now has the parameter and we want to get to the next switch
if "%HAS_PARAM%"=="1" shift /1
:: return to GETOPT_START to handle the next switch
goto GETOPT_START
:: handling help switches (-h or --help)
:CASE--help
:CASE-h
:: call the HELP "function"
call :HELP
:: exit with error code 3, meaning ask the caller to exit the script
exit /B 3
:: handling -m or --mingw-home switches
:CASE-m
:CASE--mingw-home
:: this argument must have a parameter. If no parameter was found goto GETOPT_REQUIRED_PARAM and exit
if "%2"=="" goto GETOPT_REQUIRED_PARAM %1
:: verify the MinGW dir supplied by the user exists. If not, exit with error code 3, meaning ask the caller to exit the script
if not exist %2\ call :GETOPT_ERROR "MinGW directory '%2' does not exist" & exit /B 3
:: if all went well, set the MINGW_HOME variable with the directory given by the user
set MINGW_HOME=%2
:: notify GETOPT this switch has a parameter
set HAS_PARAM=1
:: exit ok
exit /B 0
:: handling -w or --winpcap-home switches
:CASE-w
:CASE--winpcap-home
:: this argument must have a parameter. If no parameter was found goto GETOPT_REQUIRED_PARAM and exit
if "%2"=="" goto GETOPT_REQUIRED_PARAM %1
:: verify the WinPcap dir supplied by the user exists. If not, exit with error code 3, meaning ask the caller to exit the script
if not exist %2\ call :GETOPT_ERROR "WinPcap directory '%2' does not exist" & exit /B 3
:: if all went well, set the WINPCAP_HOME variable with the directory given by the user
set WINPCAP_HOME=%2
:: notify GETOPT this switch has a parameter
set HAS_PARAM=1
:: exit ok
exit /B 0
:: a required parameter error case, may get to here if a parameter was missing for a certain switch
:: the parameter for this "function" is the switch that didn't have the parameter
:GETOPT_REQUIRED_PARAM
:: print the error message
echo Required parameter not supplied for switch "%1"
:: exit with error code 2, meaining switch is missing a parameter
exit /B 2
:: both switch cases and getopt may get to here if there was an error that needs to be reported to the user
:: calling this "function" has one parameter which is the error to print to the user
:GETOPT_ERROR
:: reset error level
VER > NUL # reset ERRORLEVEL
:: print the error as was supplied by the user. The %~1 removes quotes if were given
echo %~1
:: exit with error code 1
exit /B 1
:: getopt finished successfully, exit ok
:GETOPT_END
exit /B 0
:: -------------------------------------------------------------------
:: a "function" that implements the wizard mode which reads MinGW home and WinPcap home by displaying a wizard for the user
:READ_PARAMS_FROM_USER
:: get MinGW location from user and verify it exists
echo MinGW is required for compiling PcapPlusPlus.
echo If MinGW is not installed, please download and install it from www.mingw.org/
echo.
:while1
:: ask the user to type MinGW dir
set /p MINGW_HOME= Please specify MinGW installed path: %=%
:: if input dir doesn't exist print an error to the user and go back to previous line
if not exist %MINGW_HOME%\ (echo Directory does not exist!! && goto while1)
echo.
echo.
:: get WinPcap dev pack location from user and verify it exists
echo WinPcap developer's pack is required for compiling PcapPlusPlus.
echo If WinPcap developer's pack is not installed, please download and install it from https://www.winpcap.org/devel.htm
echo.
:while2
:: ask the user to type WinPcap dir
set /p WINPCAP_HOME= Please specify WinPcap developer's pack installed path: %=%
:: if input dir doesn't exist print an error to the user and go back to previous line
if not exist %WINPCAP_HOME%\ (echo Directory does not exist!! && goto while2)
:: both directories were read correctly, return to the caller
exit /B 0
:: -------------------------------------------------------------------
:: a "function" that prints help for this script
:HELP
echo.
echo Help documentation for %~nx0
echo This script has 2 modes of operation:
echo 1) Without any switches. In this case the script will guide you through using wizards
echo 2) With switches, as described below
echo.
echo Basic usage: %~nx0 [-h] -m MINGW_HOME_DIR -w WINPCAP_HOME_DIR
echo.
echo The following switches are recognized:
echo -m^|--mingw-home --Sets MinGW home directory
echo -w^|--winpcap-home --Sets WinPcap home directory
echo -h^|--help --Displays this help message and exits. No further actions are performed
echo.
:: done printing, exit
exit /B 0