source: trunk/NewView/StartupUnit.pas@ 54

Last change on this file since 54 was 54, checked in by RBRi, 19 years ago

refactoring to simplify command line handling in MainForm
some tests fixed

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1Unit StartupUnit;
2
3// NewView - a new OS/2 Help Viewer
4// Copyright 2003 Aaron Lawrence (aaronl at consultant dot com)
5// This software is released under the Gnu Public License - see readme.txt
6
7// Code related to startup and finding help files.
8// Shared between NewView.exe and ViewStub.exe
9
10Interface
11
12uses
13 OS2Def,
14 Classes,
15 ACLString,
16 GlobalFilelistUnit,
17 SharedMemoryUnit,
18 CmdLineParameterUnit;
19
20const
21 OWN_HELP_MARKER = '[NVHELP]';
22
23
24function AccessSharedMemory: TSuballocatedSharedMemory;
25
26// Returns true if the program should be started as normal.
27// False if it should immediately exit.
28function Startup: boolean;
29
30// Look for any items that are actually specifiying environment
31// variables, and expand them to the contents of the variables
32Procedure TranslateIPFEnvironmentVars( Items: TStrings;
33 ExpandedItems: TStrings );
34
35// Given a filename, which may or may not contain a path or extension,
36// finds the actual file. This can involve searching
37// the help and bookshelf paths.
38Function FindHelpFile( FileName: string ): string;
39
40var
41 CmdLineParameters: TCmdLineParameters;
42 SharedMemory: TSubAllocatedSharedMemory;
43 GlobalFilelist: TGlobalFilelist;
44
45Implementation
46
47uses
48 Dos,
49 SysUtils,
50 DebugUnit,
51 PMWin,
52 ACLUtility,
53 ACLStringUtility,
54 ACLFileUtility,
55 AStringUtilityUnit,
56 HelpManagerUnit;
57
58// Look for any items that are actually specifiying environment
59// variables, and expand them to the contents of the variables
60Procedure TranslateIPFEnvironmentVars( Items: TStrings;
61 ExpandedItems: TStrings );
62var
63 i: longint;
64 Item: string;
65 EnvironmentVarValue: string;
66begin
67 LogEvent(LogStartup, 'Translating environment vars' );
68 for i := 0 to Items.Count - 1 do
69 begin
70 Item := Items[ i ];
71
72 Item := StrUnQuote( Item ); // remove single quotes
73 Item := StrUnDoubleQuote( Item ); // remove double quotes
74
75 LogEvent(LogStartup, ' Item: ' + Item );
76 EnvironmentVarValue := GetEnv( Uppercase( Item ) );
77 if DosError = 0 then
78 begin
79 // environment var exists - use it's value
80 LogEvent(LogStartup, ' Translated: ' + EnvironmentVarValue );
81 while EnvironmentVarValue <> '' do
82 begin
83 Item := ExtractNextValue( EnvironmentVarValue, '+' );
84 ExpandedItems.Add( Item );
85 end;
86 end
87 else
88 begin
89 // not an environment var
90 ExpandedItems.Add( Item );
91 end;
92 end;
93end;
94
95// Given a filename, which may or may not contain a path or extension,
96// finds the actual file. This can involve searching
97// the help and bookshelf paths.
98Function FindHelpFile( FileName: string ): string;
99var
100 AlternativeFileName: string;
101begin
102 if FileName = OWN_HELP_MARKER then
103 begin
104 Result := GetOwnHelpFileName;
105 exit;
106 end;
107
108 Result := '';
109
110 AlternativeFileName := '';
111 if ExtractFileExt( Filename ) = '' then
112 begin
113 Filename := ChangeFileExt( Filename, '.inf' );
114 AlternativeFileName := ChangeFileExt( Filename, '.hlp' );
115 end;
116
117 if ExtractFilePath( FileName ) <> '' then
118 begin
119 // Path specified; just see if it exists
120
121 // expand out relative paths
122 Filename := ExpandFileName( FileName );
123 AlternativeFilename := ExpandFileName( AlternativeFilename );
124
125 if FileExists( Filename ) then
126 Result := Filename
127 else if FileExists( AlternativeFilename ) then
128 Result := AlternativeFilename;
129
130 end
131 else
132 begin
133 // Path not specified; search current
134 if FileExists( ExpandFileName( FileName ) ) then
135 begin
136 Result := ExpandFileName( FileName );
137 exit;
138 end;
139
140 if FileExists( ExpandFileName( AlternativeFilename ) ) then
141 begin
142 Result := ExpandFileName( AlternativeFilename );
143 exit;
144 end;
145
146 // Search help paths
147
148 if not SearchHelpPaths( FileName,
149 Result,
150 false // don't search our app dir
151 ) then
152 begin
153 // Didn't find as specified or as .inf, try .hlp
154 if AlternativeFilename <> '' then
155 begin
156 if not SearchHelpPaths( AlternativeFileName,
157 Result,
158 false // don't search our app dir
159 ) then
160 begin
161 Result := '';
162 end;
163 end;
164 end;
165 end;
166end;
167
168
169// If another instance already has the files open
170// activate it and return true.
171function FindExistingWindow: HWND;
172var
173 FileItems: TStringList;
174 Filenames: TStringList;
175 FullFilePath: string;
176 i: longint;
177
178 FileWindow: HWND;
179begin
180 Result := NULLHANDLE;
181
182 if length(CmdLineParameters.getFileNames) = 0 then
183 // not loading files; nothing to check
184 exit;
185
186 FileItems := TStringList.Create;
187 Filenames := TStringList.Create;
188
189 StringToList(CmdLineParameters.getFileNames, FileItems, '+' );
190 TranslateIPFEnvironmentVars( FileItems, FileNames );
191
192 for i := 0 to FileNames.Count - 1 do
193 begin
194 FullFilePath := FindHelpFile( Filenames[ i ] );
195 if FullFilePath <> '' then
196 begin
197 FileWindow := GlobalFilelist.FindFile( FullFilePath );
198
199 if FileWindow = NULLHANDLE then
200 begin
201 // not found - stop searching.
202 Result := NULLHANDLE; // no match
203 break;
204 end;
205
206 // found it
207
208 // is it the same as any previous match?
209 if Result <> NULLHANDLE then
210 begin
211 if FileWindow <> Result then
212 begin
213 // no, so we don't have a match.
214 // NOTE: We just excluded something: if the same file is
215 // open in multiple windows then we may not check all combinations
216 Result := NULLHANDLE; // no match
217 break;
218 end;
219 end
220 else
221 begin
222 // no match yet - store this one
223 Result := FileWindow;
224 end;
225
226 end;
227 end;
228
229 Filenames.Destroy;
230 FileItems.Destroy;
231
232end;
233
234function AccessSharedMemory: TSuballocatedSharedMemory;
235begin
236 Result := TSuballocatedSharedMemory.Create( SharedMemName,
237 SharedMemSize,
238 SharedMemReserveSize );
239end;
240
241procedure PostNewViewTextMessage( Window: HWND;
242 MessageType: ULONG;
243 s: string );
244var
245 ps: pchar;
246begin
247 SharedMemory.Allocate( ps, length( s ) + 1 );
248 ps ^ := s;
249 WinPostMsg( Window,
250 MessageType,
251 LONG( ps ),
252 0 );
253end;
254
255function Startup: boolean;
256var
257 tmpCmdLine: String;
258 ExistingWindow: HWND;
259begin
260 // open shared memory
261 SharedMemory := AccessSharedMemory;
262
263 // get access to the system-global filelist
264 GlobalFilelist := TGlobalFilelist.Create;
265
266 // parse parameters into Parameters object
267 tmpCmdLine := nativeOS2GetCmdLineParameter;
268
269 CmdLineParameters := TCmdLineParameters.Create;
270 CmdLineParameters.parseCmdLine(tmpCmdLine);
271
272 ExistingWindow := FindExistingWindow;
273
274 if ExistingWindow <> NULLHANDLE then
275 begin
276 // want to exit without running fully
277 Result := false;
278
279 // destroy global list - nobody else will
280 GlobalFilelist.Destroy;
281
282 WinSetFocus( HWND_DESKTOP, ExistingWindow );
283
284 // if CmdLineParameters.getTopics <> '' then
285 if not CmdLineParameters.getSearchFlag AND not CmdLineParameters.getGlobalSearchFlag then
286 begin
287 PostNewViewTextMessage( ExistingWindow,
288 NHM_SEARCH,
289 CmdLineParameters.getSearchText);
290 end;
291
292 if CmdLineParameters.getGlobalSearchFlag then
293 begin
294 PostNewViewTextMessage( ExistingWindow,
295 NHM_GLOBAL_SEARCH,
296 CmdLineParameters.getSearchText);
297 end;
298
299 if CmdLineParameters.getShowUsageFlag then
300 begin
301 WinPostMsg( ExistingWindow,
302 NHM_SHOW_USAGE,
303 0,
304 0 );
305 end;
306
307 if CmdLineParameters.getHelpManagerFlag then
308 begin
309 // tell the new help manager instance to talk to the
310 // other viewer
311 WinPostMsg( CmdLineParameters.getHelpManagerWindow,
312 NHM_VIEWER_READY,
313 ExistingWindow,
314 0 );
315 end;
316
317 end
318 else
319 begin
320 // run as normal
321 Result := true;
322 end;
323end;
324
325Initialization
326End.
Note: See TracBrowser for help on using the repository browser.