-
Notifications
You must be signed in to change notification settings - Fork 22
/
PuTTYNG.ps1
225 lines (195 loc) · 8.75 KB
/
PuTTYNG.ps1
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
213
214
215
216
217
218
219
220
221
222
223
224
225
Function Write-CustomError()
{
<#
.Synopsis
Displays error information to the console
.DESCRIPTION
Writes property information from the current [ErrorRecord] object
in the pipeline to the console
.EXAMPLE
Write-CustomError -UserMessage "Exception occurred at memory location $x" -ErrorObject $_
.EXAMPLE
Write-CustomError -UserMessage "Exception occurred at memory location $x" -ErrorObject $_ -FullDetail
.INPUTS
$Error[0]
.OUTPUTS
[String]
.COMPONENT
adminkitMiscTools
.FUNCTIONALITY
General Utility
#>
[cmdletBinding()]
param(
[Parameter(Mandatory=$False)]
[String]$UserMessage,
[Parameter(Mandatory=$True)]
[Object]$ErrorObject,
[Parameter(Mandatory=$false)]
[Switch]$FullDetail
)
BEGIN
{}
PROCESS
{
if($UserMessage) {
Write-Host "`nERROR: $UserMessage" -ForegroundColor Red
}
if($FullDetail)
{
$ErrorData = $ErrorData + [PSCustomObject]@{AccountUsed=$ENV:USERNAME;
ExceptionMessage=$ErrorObject.ToString();
CategoryInfo=$ErrorObject.CategoryInfo;
ExceptionType=$ErrorObject.Exception.GetType();
ErrorDetails=$ErrorObject.ErrorDetails;
FullyQualifiedErrorId=$ErrorObject.FullyQualifiedErrorId;
InvocationInfo=$ErrorObject.InvocationInfo;
PipelineIterationInfo=$ErrorObject.PipelineIterationInfo;
ScriptStackTrace=$ErrorObject.ScriptStackTrace
TargetObject=$ErrorObject.TargetObject;
}
}
return $ErrorData
}
END
{}
}
clear
write-host "/============================================================================================/"
write-host "/ This will make a some changes in original putty files to make it compatible with mRemoteNG /"
write-host "/============================================================================================/"
write-host ""
$workFolder = "$PSScriptRoot\putty"
#In case its exists, do delete incase of new version
if (Test-Path -LiteralPath $workFolder) {
Write-Host "folder found, deleting to be sure we have clean original version before modifications will be applyed"
Remove-Item -LiteralPath $workFolder -Verbose -Recurse -Force
}
Write-Host "Clonning..."
#clone putty into current directory
try {
Start-Process -FilePath "git.exe" -ArgumentList "clone https://git.tartarus.org/simon/putty.git" -Wait
}
catch {
Write-CustomError -UserMessage 'There was an error' -ErrorObject $_ -FullDetail
}
#Get version of last update
cd putty
$getLastTag = git.exe describe --tags --match="*.*" --abbrev=0 HEAD 2>&1
cd ..
#Second check
if (Test-Path -LiteralPath $workFolder) {
Write-Host "Clone completed, folder exist, starting modification process"
#Add mRemoteNG required changes
$workFile = "$workFolder\cmdline.c"
$newContent = '
#ifdef PUTTYNG
if (!stricmp(p, "-hwndparent")) {
RETURN(2);
hwnd_parent = atoi(value);
return 2;
}
#endif
if (!strcmp(p, "-load")) {'
(Get-Content $workFile).Replace('if (!strcmp(p, "-load")) {', $newContent) | Set-Content $workFile
Write-Host "Code block replaced successfully in '$workFile'."
#===================================================================================================
#Add mRemoteNG required changes
$workFile = "$workFolder\putty.h"
$newContent = 'extern const char *const appname;
#ifdef PUTTYNG
int hwnd_parent;
#define IsZoomed(hWnd) TRUE
#endif // PUTTYNG'
(Get-Content $workFile).Replace('extern const char *const appname;', $newContent) | Set-Content $workFile
Write-Host "Code block replaced successfully in '$workFile'."
#===================================================================================================
#Add mRemoteNG required changes
$workFile = "$workFolder\version.h"
$setNewVersion = $getLastTag.split(".")[0] + "," + $getLastTag.split(".")[1] + ",0,1"
#Change version data
(Get-Content $workFile).Replace('Unidentified build', 'Release '+ $getLastTag + ' mRemoteNG') | Set-Content $workFile
(Get-Content $workFile).Replace('-Unidentified-Local-Build', '-Release-mRemoteNG-Build') | Set-Content $workFile
(Get-Content $workFile).Replace('0,0,0,0', $setNewVersion) | Set-Content $workFile
Write-Host "Code block replaced successfully in '$workFile'."
#===================================================================================================
#Add mRemoteNG required changes
[string]$workFile = "$workFolder\windows\window.c"
[string]$existingBlockStart = 'char *title = dupprintf("%s Fatal Error", appname);'
[string]$existingBlockEnd = 'if (conf_get_int(wgs->conf, CONF_close_on_exit) == FORCE_ON)'
[string]$newBlock = ' win_seat_output(seat,true,"\r\n\r\n",4);
win_seat_output(seat,0,"------------------- ",20);
win_seat_output(seat,0,title,strlen(title));
win_seat_output(seat,0," ------------------\r\n",22);
win_seat_output(seat,0,"- ",2);
win_seat_output(seat,0,msg,strlen(msg));
win_seat_output(seat,0,"\r\n\r\n",4);
//show_mouseptr(wgs, true);
//MessageBox(wgs->term_hwnd, msg, title, MB_ICONERROR | MB_OK);
sfree(title);
'
# Read the content of the file
$fileContent = Get-Content -Path $workFile -Raw
# Find the position of the existing block start
$startIndex = $fileContent.IndexOf($existingBlockStart) + $existingBlockStart.Length + 1;
if ($startIndex -ge 0) {
# Find the end of the existing block (e.g., next empty line or closing brace)
$endIndex = $fileContent.IndexOf($existingBlockEnd, $startIndex)
# Remove the existing block
$modifiedContent = $fileContent.Remove($startIndex, $endIndex - $startIndex)
# Insert the new block after the existing block start
$modifiedContent = $modifiedContent.Insert($startIndex, $newBlock)
# Write the modified content back to the file
Set-Content -Path $workFile -Value $modifiedContent
Write-Host "Code block replaced successfully in '$workFile'."
} else {
Write-Host "Existing block start not found in the file." -ForegroundColor Red
}
#===================================================================================================
#Add mRemoteNG required changes
[string]$workFile = "$workFolder\windows\window.c"
[string]$existingBlockStart = 'char *title = dupprintf("%s Error", appname);'
[string]$existingBlockEnd = 'sfree(title);'
[string]$newBlock = ' win_seat_output(seat,true,"\r\n\r\n",4);
win_seat_output(seat,0,"------------------- ",20);
win_seat_output(seat,0,title,strlen(title));
win_seat_output(seat,0," ------------------\r\n",22);
win_seat_output(seat,0,"- ",2);
win_seat_output(seat,0,msg,strlen(msg));
win_seat_output(seat,0,"\r\n\r\n",4);
//show_mouseptr(wgs, true);
//MessageBox(wgs->term_hwnd, msg, title, MB_ICONERROR | MB_OK);
'
# Read the content of the file
$fileContent = Get-Content -Path $workFile -Raw
# Find the position of the existing block start
$startIndex = $fileContent.IndexOf($existingBlockStart) + $existingBlockStart.Length + 1;
if ($startIndex -ge 0) {
# Find the end of the existing block (e.g., next empty line or closing brace)
$endIndex = $fileContent.IndexOf($existingBlockEnd, $startIndex)
# Remove the existing block
$modifiedContent = $fileContent.Remove($startIndex, $endIndex - $startIndex)
# Insert the new block after the existing block start
$modifiedContent = $modifiedContent.Insert($startIndex, $newBlock)
# Write the modified content back to the file
Set-Content -Path $workFile -Value $modifiedContent
Write-Host "Code block replaced successfully in '$workFile'."
} else {
Write-Host "Existing block start not found in the file." -ForegroundColor Red
}
#===================================================================================================
# run cmake
Write-host "Build has been started"
try {
Start-Process -FilePath "make22.cmd" -Wait
}
catch {
Write-CustomError -UserMessage 'There was an error' -ErrorObject $_ -FullDetail
}
Write-host "Build has been completed"
}
else
{
Write-Host "Cloning seems not succesfull, please check internet connection"
}
write-host ""