1
+ Set-StrictMode - Version Latest
2
+ $Win32Macro = ' WIN32_FIXME'
3
+ $sourceRoot = ' C:\openssh\Win32-OpenSSH'
4
+
5
+ [int ]$g_code = 0
6
+ [int ]$g_win32 = 0
7
+ [int ]$g_unix = 0
8
+
9
+ function AnalyzeFile ($file , [bool ]$log )
10
+ {
11
+ $file = Join-Path $sourceRoot $file
12
+ if ($log ) { Write-Host - ForegroundColor Gray $file }
13
+ $content = Get-Content $file
14
+ [int ]$commentlines = 0 # comments
15
+ [int ]$emptylines = 0 # emptylines
16
+ [int ]$code = 0 # all code lines
17
+ [int ]$win32 = 0 # win32 only lines
18
+ [int ]$win32substituted = 0 # lines in win32 block that have a corresponding Unix block (#ifdef with #else)
19
+ [int ]$unix = 0 ; # unix only lines
20
+ [int ]$unixsubstituted = 0 # lines in unix block that have a corresponding Win32 block (#ifdef with #else)
21
+ [int ]$total = 0
22
+ [int ]$nestedmacros = 0 # tracks nested macro blocks inside a win32 or a unix block
23
+ [bool ]$incommentblock = $false
24
+ [bool ]$inWin32block = $false
25
+ [bool ]$inUnixblock = $false
26
+ [int ]$currentblockcode = 0
27
+ [bool ]$insubstitutedblock = $false
28
+
29
+
30
+ foreach ($linestr in $content )
31
+ {
32
+ $total ++
33
+ $line = [String ]$linestr
34
+ $line = $line.Trim ()
35
+ # skip if line is empty
36
+ if ($line.Length -gt 0 )
37
+ {
38
+ if ($incommentblock )
39
+ {
40
+ $commentlines ++
41
+ if ($line.EndsWith (' */' )) {$incommentblock = $false }
42
+ }
43
+ else
44
+ {
45
+ if ($line.StartsWith (' //' )) {$commentlines ++ }
46
+ elseif ($line.StartsWith (' /*' ))
47
+ {
48
+ if (! ($line.EndsWith (' */' ))) { $incommentblock = $true }
49
+ $commentlines ++
50
+ }
51
+ else
52
+ {
53
+ $code ++
54
+ if ($inWin32block )
55
+ {
56
+ $win32 ++
57
+ $currentblockcode ++
58
+ # keep skipping inner #ifdefs
59
+ if ($line.StartsWith (' #ifdef' )) {$nestedmacros ++ }
60
+
61
+ if ($line.EndsWith (' #endif' ) -or $line.EndsWith (' #else' ))
62
+ {
63
+ if ($nestedmacros -eq 0 )
64
+ {
65
+ $inWin32block = $false
66
+ if ($line.EndsWith (' #else' ))
67
+ {
68
+ $inUnixblock = $true
69
+ $insubstitutedblock = $true
70
+ $win32substituted += $currentblockcode
71
+ }
72
+ elseif ($insubstitutedblock )
73
+ {
74
+ $win32substituted += $currentblockcode
75
+ $insubstitutedblock = $false
76
+ }
77
+ $currentblockcode = 0
78
+ }
79
+ else
80
+ {
81
+ if ($line.EndsWith (' #endif' )) {$nestedmacros -- }
82
+ }
83
+ }
84
+ }
85
+ elseif ($inUnixblock )
86
+ {
87
+ $unix ++
88
+ $currentblockcode ++
89
+ # keep skipping inner #ifdefs
90
+ if ($line.StartsWith (' #ifdef' )) {$nestedmacros ++ }
91
+
92
+ if ($line.EndsWith (' #endif' ) -or $line.EndsWith (' #else' ))
93
+ {
94
+ if ($nestedmacros -eq 0 )
95
+ {
96
+ $inUnixblock = $false
97
+ if ($line.EndsWith (' #else' ))
98
+ {
99
+ $inWin32block = $true
100
+ $insubstitutedblock = $true
101
+ $unixsubstituted += $currentblockcode
102
+ }
103
+ elseif ($insubstitutedblock )
104
+ {
105
+ $unixsubstituted += $currentblockcode
106
+ $insubstitutedblock = $false
107
+ }
108
+
109
+ $currentblockcode = 0
110
+ }
111
+ else
112
+ {
113
+ if ($line.EndsWith (' #endif' )) {$nestedmacros -- }
114
+ }
115
+ }
116
+ }
117
+ else
118
+ {
119
+ if ($line.StartsWith (' #ifdef' ) -and $line.Contains ($Win32Macro ))
120
+ {
121
+ $inWin32block = $true
122
+ $currentblockcode = 0
123
+ }
124
+ if ($line.StartsWith (' #ifndef' ) -and $line.Contains ($Win32Macro ))
125
+ {
126
+ $inUnixblock = $true
127
+ $currentblockcode = 0 ;
128
+ }
129
+ }
130
+
131
+ }
132
+ }
133
+ }
134
+ else {$emptylines ++ }
135
+ }
136
+
137
+ if ($log )
138
+ {
139
+ Write-Host - ForegroundColor Yellow " Comments " $commentlines
140
+ Write-Host - ForegroundColor Green " Blank " $emptylines
141
+ Write-Host - ForegroundColor Cyan " Code " $code
142
+ Write-Host - ForegroundColor DarkMagenta " Total " $total " check(" ($commentlines + $emptylines + $code )" )"
143
+ Write-Host - ForegroundColor Cyan " Win32 " $win32
144
+ Write-Host - ForegroundColor Cyan " Unix " $unix
145
+ Write-Host - ForegroundColor Cyan " Win32sub " $win32substituted
146
+ Write-Host - ForegroundColor Cyan " Unixsub " $unixsubstituted
147
+ }
148
+
149
+ $global :g_code += $code
150
+ $global :g_win32 += $win32
151
+ $global :g_unix += $unix
152
+
153
+ }
154
+
155
+
156
+ function AnalyzeProject ($project , [bool ]$log )
157
+ {
158
+ if ($log ) { Write-Host " Project: " $project }
159
+ $projectName = $project
160
+ $projectroot = Join-Path $sourceRoot ' contrib\win32\openssh'
161
+ $project = Join-Path $projectroot $project
162
+ $project = $project + ' .vcxproj'
163
+
164
+ $global :g_code = 0
165
+ $global :g_win32 = 0
166
+ $global :g_unix = 0
167
+
168
+ $c = Get-Content $project
169
+ foreach ($ln in $c ){
170
+ $l = [String ]$ln
171
+ $l = $l.Trim ()
172
+
173
+ if ($l.StartsWith (' <ClCompile Include="$(OpenSSH-Src-Path)' ))
174
+ {
175
+ $l = $l.Replace (' <ClCompile Include="$(OpenSSH-Src-Path)' , ' ' )
176
+ $l = $l.Substring (0 , $l.IndexOf (' "' ))
177
+ AnalyzeFile $l $log
178
+ }
179
+ }
180
+
181
+ if ($log )
182
+ {
183
+ Write-Host " Total Code " $global :g_code
184
+ Write-Host " Win32 Code " $global :g_win32
185
+ Write-Host " Unix Code " $global :g_unix
186
+ }
187
+
188
+ Write-Host $projectName " " (100 - ($global :g_unix * 100 / ($global :g_code - $global :g_win32 ))) " %"
189
+
190
+ }
191
+
192
+
193
+ AnalyzeProject libssh
194
+ AnalyzeProject scp
195
+ AnalyzeProject sftp
196
+ AnalyzeProject sftp- server
197
+ AnalyzeProject ssh
198
+ AnalyzeProject ssh- add
199
+ AnalyzeProject ssh- agent
200
+ AnalyzeProject sshd
0 commit comments