| 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 | GlobalFilelistUnit, | 
|---|
| 16 | SharedMemoryUnit, | 
|---|
| 17 | CmdLineParameterUnit; | 
|---|
| 18 |  | 
|---|
| 19 | const | 
|---|
| 20 | OWN_HELP_MARKER = '[NVHELP]'; | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | function AccessSharedMemory: TSuballocatedSharedMemory; | 
|---|
| 24 |  | 
|---|
| 25 | // Look for any items that are actually specifiying environment | 
|---|
| 26 | // variables, and expand them to the contents of the variables | 
|---|
| 27 | Procedure TranslateIPFEnvironmentVars( Items: TStrings; | 
|---|
| 28 | ExpandedItems: TStrings ); | 
|---|
| 29 |  | 
|---|
| 30 | // Given a filename, which may or may not contain a path or extension, | 
|---|
| 31 | // finds the actual file. This can involve searching | 
|---|
| 32 | // the help and bookshelf paths. | 
|---|
| 33 | Function FindHelpFile( FileName: string ): string; | 
|---|
| 34 | Procedure PostNewViewTextMessage( Window: HWND; | 
|---|
| 35 | MessageType: ULONG; | 
|---|
| 36 | s: string ); | 
|---|
| 37 |  | 
|---|
| 38 | var | 
|---|
| 39 | SharedMemory: TSubAllocatedSharedMemory; | 
|---|
| 40 | GlobalFilelist: TGlobalFilelist; | 
|---|
| 41 |  | 
|---|
| 42 | Implementation | 
|---|
| 43 |  | 
|---|
| 44 | uses | 
|---|
| 45 | Dos, | 
|---|
| 46 | SysUtils, | 
|---|
| 47 | DebugUnit, | 
|---|
| 48 | PMWin, | 
|---|
| 49 | ACLStringUtility, | 
|---|
| 50 | HelpManagerUnit, | 
|---|
| 51 | FileUtilsUnit; | 
|---|
| 52 |  | 
|---|
| 53 | // Look for any items that are actually specifiying environment | 
|---|
| 54 | // variables, and expand them to the contents of the variables | 
|---|
| 55 | Procedure TranslateIPFEnvironmentVars( Items: TStrings; | 
|---|
| 56 | ExpandedItems: TStrings ); | 
|---|
| 57 | var | 
|---|
| 58 | i: longint; | 
|---|
| 59 | Item: string; | 
|---|
| 60 | EnvironmentVarValue: string; | 
|---|
| 61 | begin | 
|---|
| 62 | LogEvent(LogStartup, 'Translating environment vars' ); | 
|---|
| 63 | for i := 0 to Items.Count - 1 do | 
|---|
| 64 | begin | 
|---|
| 65 | Item := Items[ i ]; | 
|---|
| 66 |  | 
|---|
| 67 | Item := StrUnQuote( Item ); // remove single quotes | 
|---|
| 68 | Item := StrUnDoubleQuote( Item ); // remove double quotes | 
|---|
| 69 |  | 
|---|
| 70 | LogEvent(LogStartup, '  Item: ' + Item ); | 
|---|
| 71 | EnvironmentVarValue := GetEnv( Uppercase( Item ) ); | 
|---|
| 72 | if DosError = 0 then | 
|---|
| 73 | begin | 
|---|
| 74 | // environment var exists - use it's value | 
|---|
| 75 | LogEvent(LogStartup, '    Translated: ' + EnvironmentVarValue ); | 
|---|
| 76 | while EnvironmentVarValue <> '' do | 
|---|
| 77 | begin | 
|---|
| 78 | Item := ExtractNextValue( EnvironmentVarValue, '+' ); | 
|---|
| 79 | ExpandedItems.Add( Item ); | 
|---|
| 80 | end; | 
|---|
| 81 | end | 
|---|
| 82 | else | 
|---|
| 83 | begin | 
|---|
| 84 | // not an environment var | 
|---|
| 85 | ExpandedItems.Add( Item ); | 
|---|
| 86 | end; | 
|---|
| 87 | end; | 
|---|
| 88 | end; | 
|---|
| 89 |  | 
|---|
| 90 | // Given a filename, which may or may not contain a path or extension, | 
|---|
| 91 | // finds the actual file. This can involve searching | 
|---|
| 92 | // the help and bookshelf paths. | 
|---|
| 93 | Function FindHelpFile( FileName: string ): string; | 
|---|
| 94 | var | 
|---|
| 95 | AlternativeFileName: string; | 
|---|
| 96 | begin | 
|---|
| 97 | Result := ''; | 
|---|
| 98 |  | 
|---|
| 99 | AlternativeFileName := ''; | 
|---|
| 100 | if ExtractFileExt( Filename ) = '' then | 
|---|
| 101 | begin | 
|---|
| 102 | Filename := ChangeFileExt( Filename, '.inf' ); | 
|---|
| 103 | AlternativeFileName := ChangeFileExt( Filename, '.hlp' ); | 
|---|
| 104 | end; | 
|---|
| 105 |  | 
|---|
| 106 | if ExtractFilePath( FileName ) <> '' then | 
|---|
| 107 | begin | 
|---|
| 108 | // Path specified; just see if it exists | 
|---|
| 109 |  | 
|---|
| 110 | // expand out relative paths | 
|---|
| 111 | Filename := ExpandFileName( FileName ); | 
|---|
| 112 | AlternativeFilename := ExpandFileName( AlternativeFilename ); | 
|---|
| 113 |  | 
|---|
| 114 | if FileExists( Filename ) then | 
|---|
| 115 | Result := Filename | 
|---|
| 116 | else if FileExists( AlternativeFilename ) then | 
|---|
| 117 | Result := AlternativeFilename; | 
|---|
| 118 |  | 
|---|
| 119 | end | 
|---|
| 120 | else | 
|---|
| 121 | begin | 
|---|
| 122 | // Path not specified; search current | 
|---|
| 123 | if FileExists( ExpandFileName( FileName ) ) then | 
|---|
| 124 | begin | 
|---|
| 125 | Result := ExpandFileName( FileName ); | 
|---|
| 126 | exit; | 
|---|
| 127 | end; | 
|---|
| 128 |  | 
|---|
| 129 | if FileExists( ExpandFileName( AlternativeFilename ) ) then | 
|---|
| 130 | begin | 
|---|
| 131 | Result := ExpandFileName( AlternativeFilename ); | 
|---|
| 132 | exit; | 
|---|
| 133 | end; | 
|---|
| 134 |  | 
|---|
| 135 | // Search help paths | 
|---|
| 136 |  | 
|---|
| 137 | if not SearchHelpPaths( FileName, | 
|---|
| 138 | Result, | 
|---|
| 139 | false // don't search our app dir | 
|---|
| 140 | ) then | 
|---|
| 141 | begin | 
|---|
| 142 | // Didn't find as specified or as .inf, try .hlp | 
|---|
| 143 | if AlternativeFilename <> '' then | 
|---|
| 144 | begin | 
|---|
| 145 | if not SearchHelpPaths( AlternativeFileName, | 
|---|
| 146 | Result, | 
|---|
| 147 | false // don't search our app dir | 
|---|
| 148 | ) then | 
|---|
| 149 | begin | 
|---|
| 150 | Result := ''; | 
|---|
| 151 | end; | 
|---|
| 152 | end; | 
|---|
| 153 | end; | 
|---|
| 154 | end; | 
|---|
| 155 | end; | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 | function AccessSharedMemory: TSuballocatedSharedMemory; | 
|---|
| 159 | begin | 
|---|
| 160 | Result := TSuballocatedSharedMemory.Create( SharedMemName, | 
|---|
| 161 | SharedMemSize, | 
|---|
| 162 | SharedMemReserveSize ); | 
|---|
| 163 | end; | 
|---|
| 164 |  | 
|---|
| 165 | procedure PostNewViewTextMessage( Window: HWND; | 
|---|
| 166 | MessageType: ULONG; | 
|---|
| 167 | s: string ); | 
|---|
| 168 | var | 
|---|
| 169 | ps: pchar; | 
|---|
| 170 | begin | 
|---|
| 171 | SharedMemory.Allocate( ps, length( s ) + 1 ); | 
|---|
| 172 | ps ^ := s; | 
|---|
| 173 | WinPostMsg( Window, | 
|---|
| 174 | MessageType, | 
|---|
| 175 | LONG( ps ), | 
|---|
| 176 | 0 ); | 
|---|
| 177 | end; | 
|---|
| 178 |  | 
|---|
| 179 |  | 
|---|
| 180 | Initialization | 
|---|
| 181 | End. | 
|---|