| [18] | 1 | Unit 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 |
|
|---|
| 10 | Interface
|
|---|
| 11 |
|
|---|
| 12 | uses
|
|---|
| 13 | OS2Def,
|
|---|
| 14 | Classes,
|
|---|
| 15 | ACLString,
|
|---|
| 16 | GlobalFilelistUnit,
|
|---|
| [25] | 17 | SharedMemoryUnit,
|
|---|
| 18 | CmdLineParameterUnit;
|
|---|
| [18] | 19 |
|
|---|
| 20 | const
|
|---|
| 21 | OWN_HELP_MARKER = '[NVHELP]';
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | function AccessSharedMemory: TSuballocatedSharedMemory;
|
|---|
| 25 |
|
|---|
| 26 | // Returns true if the program should be started as normal.
|
|---|
| 27 | // False if it should immediately exit.
|
|---|
| 28 | function Startup: boolean;
|
|---|
| 29 |
|
|---|
| 30 | // Look for any items that are actually specifiying environment
|
|---|
| 31 | // variables, and expand them to the contents of the variables
|
|---|
| 32 | Procedure 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.
|
|---|
| 38 | Function FindHelpFile( FileName: string ): string;
|
|---|
| 39 |
|
|---|
| 40 | var
|
|---|
| [25] | 41 | CmdLineParameters: TCmdLineParameters;
|
|---|
| [18] | 42 | SharedMemory: TSubAllocatedSharedMemory;
|
|---|
| 43 | GlobalFilelist: TGlobalFilelist;
|
|---|
| 44 |
|
|---|
| 45 | Implementation
|
|---|
| 46 |
|
|---|
| 47 | uses
|
|---|
| [25] | 48 | Dos,
|
|---|
| 49 | SysUtils,
|
|---|
| [43] | 50 | DebugUnit,
|
|---|
| [25] | 51 | PMWin,
|
|---|
| 52 | ACLUtility,
|
|---|
| 53 | ACLStringUtility,
|
|---|
| 54 | ACLFileUtility,
|
|---|
| 55 | AStringUtilityUnit,
|
|---|
| [18] | 56 | HelpManagerUnit;
|
|---|
| 57 |
|
|---|
| 58 | // Look for any items that are actually specifiying environment
|
|---|
| 59 | // variables, and expand them to the contents of the variables
|
|---|
| 60 | Procedure TranslateIPFEnvironmentVars( Items: TStrings;
|
|---|
| 61 | ExpandedItems: TStrings );
|
|---|
| 62 | var
|
|---|
| 63 | i: longint;
|
|---|
| 64 | Item: string;
|
|---|
| 65 | EnvironmentVarValue: string;
|
|---|
| 66 | begin
|
|---|
| [43] | 67 | LogEvent(LogStartup, 'Translating environment vars' );
|
|---|
| [18] | 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 |
|
|---|
| [43] | 75 | LogEvent(LogStartup, ' Item: ' + Item );
|
|---|
| [18] | 76 | EnvironmentVarValue := GetEnv( Uppercase( Item ) );
|
|---|
| 77 | if DosError = 0 then
|
|---|
| 78 | begin
|
|---|
| 79 | // environment var exists - use it's value
|
|---|
| [43] | 80 | LogEvent(LogStartup, ' Translated: ' + EnvironmentVarValue );
|
|---|
| [18] | 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;
|
|---|
| 93 | end;
|
|---|
| 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.
|
|---|
| 98 | Function FindHelpFile( FileName: string ): string;
|
|---|
| 99 | var
|
|---|
| 100 | AlternativeFileName: string;
|
|---|
| 101 | begin
|
|---|
| 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;
|
|---|
| 166 | end;
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | // If another instance already has the files open
|
|---|
| 170 | // activate it and return true.
|
|---|
| 171 | function FindExistingWindow: HWND;
|
|---|
| 172 | var
|
|---|
| 173 | FileItems: TStringList;
|
|---|
| 174 | Filenames: TStringList;
|
|---|
| 175 | FullFilePath: string;
|
|---|
| 176 | i: longint;
|
|---|
| 177 |
|
|---|
| 178 | FileWindow: HWND;
|
|---|
| 179 | begin
|
|---|
| 180 | Result := NULLHANDLE;
|
|---|
| 181 |
|
|---|
| [25] | 182 | if length(CmdLineParameters.getFileNames) = 0 then
|
|---|
| [18] | 183 | // not loading files; nothing to check
|
|---|
| 184 | exit;
|
|---|
| 185 |
|
|---|
| 186 | FileItems := TStringList.Create;
|
|---|
| 187 | Filenames := TStringList.Create;
|
|---|
| 188 |
|
|---|
| [43] | 189 | StringToList(CmdLineParameters.getFileNames, FileItems, '+' );
|
|---|
| [18] | 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 |
|
|---|
| 232 | end;
|
|---|
| 233 |
|
|---|
| 234 | function AccessSharedMemory: TSuballocatedSharedMemory;
|
|---|
| 235 | begin
|
|---|
| 236 | Result := TSuballocatedSharedMemory.Create( SharedMemName,
|
|---|
| 237 | SharedMemSize,
|
|---|
| 238 | SharedMemReserveSize );
|
|---|
| 239 | end;
|
|---|
| 240 |
|
|---|
| 241 | procedure PostNewViewTextMessage( Window: HWND;
|
|---|
| 242 | MessageType: ULONG;
|
|---|
| 243 | s: string );
|
|---|
| 244 | var
|
|---|
| 245 | ps: pchar;
|
|---|
| 246 | begin
|
|---|
| 247 | SharedMemory.Allocate( ps, length( s ) + 1 );
|
|---|
| 248 | ps ^ := s;
|
|---|
| 249 | WinPostMsg( Window,
|
|---|
| 250 | MessageType,
|
|---|
| 251 | LONG( ps ),
|
|---|
| 252 | 0 );
|
|---|
| 253 | end;
|
|---|
| 254 |
|
|---|
| 255 | function Startup: boolean;
|
|---|
| 256 | var
|
|---|
| [25] | 257 | tmpCmdLine: String;
|
|---|
| [18] | 258 | ExistingWindow: HWND;
|
|---|
| 259 | begin
|
|---|
| 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
|
|---|
| [25] | 267 | tmpCmdLine := nativeOS2GetCmdLineParameter;
|
|---|
| [18] | 268 |
|
|---|
| [25] | 269 | CmdLineParameters := TCmdLineParameters.Create;
|
|---|
| [43] | 270 | CmdLineParameters.parseCmdLine(tmpCmdLine);
|
|---|
| [25] | 271 |
|
|---|
| [18] | 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 |
|
|---|
| [43] | 284 | // if CmdLineParameters.getTopics <> '' then
|
|---|
| 285 | if not CmdLineParameters.getSearchFlag AND not CmdLineParameters.getGlobalSearchFlag then
|
|---|
| [18] | 286 | begin
|
|---|
| 287 | PostNewViewTextMessage( ExistingWindow,
|
|---|
| 288 | NHM_SEARCH,
|
|---|
| [43] | 289 | CmdLineParameters.getSearchText);
|
|---|
| [18] | 290 | end;
|
|---|
| 291 |
|
|---|
| [43] | 292 | if CmdLineParameters.getGlobalSearchFlag then
|
|---|
| [18] | 293 | begin
|
|---|
| 294 | PostNewViewTextMessage( ExistingWindow,
|
|---|
| 295 | NHM_GLOBAL_SEARCH,
|
|---|
| [48] | 296 | CmdLineParameters.getSearchText);
|
|---|
| [18] | 297 | end;
|
|---|
| 298 |
|
|---|
| [25] | 299 | if CmdLineParameters.getShowUsageFlag then
|
|---|
| [18] | 300 | begin
|
|---|
| 301 | WinPostMsg( ExistingWindow,
|
|---|
| 302 | NHM_SHOW_USAGE,
|
|---|
| 303 | 0,
|
|---|
| 304 | 0 );
|
|---|
| 305 | end;
|
|---|
| 306 |
|
|---|
| [25] | 307 | if CmdLineParameters.getHelpManagerFlag then
|
|---|
| [18] | 308 | begin
|
|---|
| 309 | // tell the new help manager instance to talk to the
|
|---|
| 310 | // other viewer
|
|---|
| [25] | 311 | WinPostMsg( CmdLineParameters.getHelpManagerWindow,
|
|---|
| [18] | 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;
|
|---|
| 323 | end;
|
|---|
| 324 |
|
|---|
| 325 | Initialization
|
|---|
| 326 | End.
|
|---|