This file is indexed.

/usr/lib/lazarus/0.9.30.4/ide/helpfpcmessages.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
{
 /***************************************************************************
                            helpfpcmessages.pas
                            -------------------


 ***************************************************************************/

 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 ***************************************************************************
 
  Author: Mattias Gaertner
   
  Abstract:
    Help items for FPC messages.
}
unit HelpFPCMessages;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, Dialogs, FileUtil, TextTools, MacroIntf,
  LazarusIDEStrConsts, LazConfigStorage, HelpIntfs, IDEHelpIntf, LazHelpIntf,
  LazHelpHTML;
  
const
  lihcFPCMessages = 'FreePascal Compiler messages';
  lihFPCMessagesURL = 'http://wiki.lazarus.freepascal.org/';

type

  { TFPCMessagesHelpDatabase }

  TFPCMessagesHelpDatabase = class(THTMLHelpDatabase)
  private
    FFPCTranslationFile: string;
    FDefaultNode: THelpNode;
    FFoundComment: string;
    FLastMessage: string;
    procedure SetFPCTranslationFile(const AValue: string);
    procedure SetFoundComment(const AValue: string);
    procedure SetLastMessage(const AValue: string);
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    function GetNodesForMessage(const AMessage: string; MessageParts: TStrings;
                                var ListOfNodes: THelpNodeQueryList;
                                var ErrMsg: string): TShowHelpResult; override;
    function ShowHelp(Query: THelpQuery; BaseNode, NewNode: THelpNode;
                      QueryItem: THelpQueryItem;
                      var ErrMsg: string): TShowHelpResult; override;
    procedure Load(Storage: TConfigStorage); override;
    procedure Save(Storage: TConfigStorage); override;
    property DefaultNode: THelpNode read FDefaultNode;
    property LastMessage: string read FLastMessage write SetLastMessage;
    property FoundComment: string read FFoundComment write SetFoundComment;
  published
    property FPCTranslationFile: string read FFPCTranslationFile
                                        write SetFPCTranslationFile;
  end;

var
  FPCMessagesHelpDB: THelpDatabase;
  
procedure CreateFPCMessagesHelpDB;
function AddFPCMessageHelpItem(const Title, URL, RegularExpression: string
                               ): THelpDBIRegExprMessage;

function FindFPCMessageComment(const CommentFile, Msg: string;
  ExtractText: boolean): string;
procedure ParseFPCMessagesFile(Lines: TStrings;
  const SearchMessage: string; var FoundComment: string);

implementation

procedure CreateFPCMessagesHelpDB;
var
  FPCHelp: TFPCMessagesHelpDatabase;
  StartNode: THelpNode;
begin
  FPCMessagesHelpDB:=HelpDatabases.CreateHelpDatabase(lihcFPCMessages,
                                                 TFPCMessagesHelpDatabase,true);
  FPCHelp:=FPCMessagesHelpDB as TFPCMessagesHelpDatabase;
  FPCHelp.DefaultBaseURL:=lihFPCMessagesURL;

  // HTML nodes
  StartNode:=THelpNode.CreateURLID(FPCHelp,'FreePascal Compiler messages',
          'file://Build_messages#FreePascal_Compiler_messages',lihcFPCMessages);
  FPCHelp.TOCNode:=THelpNode.Create(FPCHelp,StartNode);// once as TOC
  FPCHelp.RegisterItemWithNode(StartNode);// and once as normal page

  // register messages
  AddFPCMessageHelpItem('Can''t find unit',
                        'FPC_message:_Can_not_find_unit',': Can''t find unit ');
  AddFPCMessageHelpItem('Wrong number of parameters specified',
                        'FPC_message:_Wrong_number_of_parameters_specified',
                        ': Wrong number of parameters specified');
  AddFPCMessageHelpItem('cannot find -l',
                        'Linker_message:_cannot_find_-l',': cannot find -l');
end;

function AddFPCMessageHelpItem(const Title, URL, RegularExpression: string
  ): THelpDBIRegExprMessage;
begin
  Result:=THelpDBIRegExprMessage.Create(
    THelpNode.CreateURL(FPCMessagesHelpDB,Title,'file://'+URL),
    RegularExpression,'I');
  FPCMessagesHelpDB.RegisterItem(Result);
end;

function FindFPCMessageComment(const CommentFile, Msg: string;
  ExtractText: boolean): string;
var
  sl: TStringList;
  p: Integer;
  TagStart: LongInt;
begin
  Result:='';
  sl:=TStringList.Create;
  try
    sl.LoadFromFile(UTF8ToSys(CommentFile));
    ParseFPCMessagesFile(sl,Msg,Result);
    if ExtractText and (Result<>'') then begin
      p:=1;
      while (p<length(Result)) do begin
        case Result[p] of
        '\':
          begin
            TagStart:=p;
            inc(p);
            if (p<=length(Result)) and (Result[p]='\') then begin
              inc(p);
            end else begin
              // remove tag
              while (p<=length(Result)) and (Result[p] in ['a'..'z','A'..'Z'])
              do
                inc(p);
              Result:=copy(Result,1,TagStart-1)+copy(Result,p,length(Result));
              p:=TagStart;
            end;
          end;
        '{','}':
          begin
            // remove brackets
            Result:=copy(Result,1,p-1)+copy(Result,p+1,length(Result));
          end;
        else
          inc(p);
        end;
      end;
    end;
  finally
    sl.Free;
  end;
end;

procedure ParseFPCMessagesFile(Lines: TStrings;
  const SearchMessage: string; var FoundComment: string);
var
  i: integer;
  Line: string;

  procedure Error(const ErrMsg: string);
  begin
    raise Exception.Create('Line='+IntToStr(i+1)+': '+ErrMsg+' in "'+Line+'"');
  end;

  function CompareTextWithSearchMessage(const Command: string;
    TypeStart, TxtStart: integer): boolean;
  var
    RegularExpression: String;
    p: Integer;
  begin
    Result:=false;
    if TxtStart>length(Command) then exit;
    if TypeStart>length(Command) then exit;
    
    RegularExpression:=copy(Command,TxtStart,length(Command));
    // replace all $d variables with (.*)
    p:=length(RegularExpression);
    while (p>0) do begin
      if (RegularExpression[p]='$') then begin
        if (p<length(RegularExpression))
        and (RegularExpression[p+1] in ['1'..'9']) then begin
          RegularExpression:=copy(RegularExpression,1,p-1)+'(.*)'
                           +copy(RegularExpression,p+2,length(RegularExpression));
        end else begin
          RegularExpression:=copy(RegularExpression,1,p-1)+'\'
                           +copy(RegularExpression,p,length(RegularExpression));
        end;
      end;
      dec(p);
    end;
    case Command[TypeStart] of
    'F': RegularExpression:='Fatal: '+RegularExpression;
    'E': RegularExpression:='Error: '+RegularExpression;
    'N': RegularExpression:='Note: '+RegularExpression;
    'I': RegularExpression:='Info: '+RegularExpression;
    'H': RegularExpression:='Hint: '+RegularExpression;
    end;

    try
      Result:=REMatches(SearchMessage,RegularExpression);
    except
      on E: Exception do begin
        WriteLn('CompareTextWithSearchMessage RegExpr Error ',E.Message,' RegularExpression="'+RegularExpression+'"');
        exit;
      end;
    end;
    // debugging:
    //if System.Pos('is assigned but never used',Command)>0 then WriteLn('CompareTextWithSearchMessage "',RegularExpression,'" Result=',Result);
  end;

var
  Command: String;
  CommandLine: Integer;

  procedure ErrorInCommand(const ErrMsg: string);
  begin
    raise Exception.Create('Line='+IntToStr(CommandLine+1)+': '+ErrMsg+' in "'+Command+'"');
  end;

var
  EqualPos: LongInt;
  Comment: String;
  BracketLevel: Integer;
  x: Integer;
  PartStart: Integer;
  TypeStart: LongInt;
  TxtStart: LongInt;
begin
  FoundComment:='';
  Comment:='';
  Command:='';
  CommandLine:=0;
  BracketLevel:=0;
  for i:=0 to Lines.Count-1 do begin
    Line:=Lines[i];
    if (Trim(Line)='') or (Line[1]='#') then continue;

    // example:
    //   general_t_compilername=01000_T_Compiler: $1
    //   % When the \var{-vt} switch is used, this line tells you what compiler
    //   % is used.
    if Line[1]='%' then begin
      if BracketLevel>0 then
        Error('unclosed bracket in lines before');
      Comment:=Comment+copy(Line,2,length(Line));
    end else begin
      if BracketLevel=0 then begin
        // end old message
        if Command<>'' then begin
          if CompareByte(Command[1],'option_',7)=0 then begin
            // option
          end else begin
            // read '='
            EqualPos:=System.Pos('=',Command);
            if EqualPos<1 then ErrorInCommand('missing =');
            // read number
            PartStart:=EqualPos+1;
            TypeStart:=PartStart;
            while (TypeStart<=length(Command)) and (Command[TypeStart]<>'_') do
              inc(TypeStart);
            // read type
            inc(TypeStart);
            TxtStart:=TypeStart;
            while (TxtStart<=length(Command)) and (Command[TxtStart]<>'_') do
              inc(TxtStart);
            // read text
            inc(TxtStart);
            if SearchMessage<>'' then begin
              if CompareTextWithSearchMessage(Command,TypeStart,TxtStart) then
              begin
                FoundComment:=Trim(Comment);
                exit;
              end;
            end;
          end;
        end;

        // start a new message
        Comment:='';
        Command:=Line;
        CommandLine:=i;
      end else begin
        // continue command
        Command:=Command+Line;
      end;
      // update BracketLevel
      for x:=1 to length(Line) do begin
        case Line[x] of
        '[': inc(BracketLevel);
        ']':
          if BracketLevel>0 then
            dec(BracketLevel)
          else
            Error('closing a bracket, which was not opened.');
        end;
      end;
    end;
  end;
end;

{ TFPCMessagesHelpDatabase }

procedure TFPCMessagesHelpDatabase.SetFoundComment(const AValue: string);
begin
  if FFoundComment=AValue then exit;
  FFoundComment:=AValue;
end;

procedure TFPCMessagesHelpDatabase.SetFPCTranslationFile(const AValue: string);
begin
  if FFPCTranslationFile=AValue then exit;
  FFPCTranslationFile:=AValue;
end;

procedure TFPCMessagesHelpDatabase.SetLastMessage(const AValue: string);
begin
  if FLastMessage=AValue then exit;
  FLastMessage:=AValue;
end;

constructor TFPCMessagesHelpDatabase.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  FDefaultNode:=THelpNode.CreateURL(Self,'FPC messages: Appendix',
     'http://lazarus-ccr.sourceforge.net/fpcdoc/user/userap3.html#x81-168000C');
end;

destructor TFPCMessagesHelpDatabase.Destroy;
begin
  FreeAndNil(FDefaultNode);
  inherited Destroy;
end;

function TFPCMessagesHelpDatabase.GetNodesForMessage(const AMessage: string;
  MessageParts: TStrings; var ListOfNodes: THelpNodeQueryList;
  var ErrMsg: string): TShowHelpResult;
var
  Filename: String;
begin
  Result:=inherited GetNodesForMessage(AMessage, MessageParts, ListOfNodes,
                                       ErrMsg);
  if (ListOfNodes<>nil) and (ListOfNodes.Count>0) then exit;

  // no node found -> add default node
  LastMessage:=AMessage;
  Filename:='$(FPCSrcDir)';
  IDEMacros.SubstituteMacros(Filename);
  //DebugLn('TFPCMessagesHelpDatabase.GetNodesForMessage Filename="',Filename,'"');
  if (Filename<>'') then begin
    Filename:=AppendPathDelim(Filename)
              +SetDirSeparators('compiler/msg/');
    // TODO: use the same language as the compiler
    if FPCTranslationFile<>'' then
      Filename:=Filename+FPCTranslationFile
    else
      Filename:=Filename+'errore.msg';
    if FileExistsUTF8(Filename) then begin
      FoundComment:=FindFPCMessageComment(Filename,AMessage,true);
      if FoundComment<>'' then begin
        Result:=shrSuccess;
        CreateNodeQueryListAndAdd(DefaultNode,nil,ListOfNodes,true);
        //DebugLn('TFPCMessagesHelpDatabase.GetNodesForMessage ',FoundComment);
      end;
    end;
  end;
end;

function TFPCMessagesHelpDatabase.ShowHelp(Query: THelpQuery; BaseNode,
  NewNode: THelpNode; QueryItem: THelpQueryItem; var ErrMsg: string
  ): TShowHelpResult;
begin
  if NewNode=DefaultNode then begin
    if FoundComment<>'' then begin
      Result:=shrSuccess;
      MessageDlg(lisHFMHelpForFreePascalCompilerMessage, FoundComment,
                 mtInformation,[mbOk],0);
    end else begin
      Result:=shrHelpNotFound;
    end;
  end else begin
    Result:=inherited ShowHelp(Query, BaseNode, NewNode, QueryItem, ErrMsg);
  end;
end;

procedure TFPCMessagesHelpDatabase.Load(Storage: TConfigStorage);
begin
  inherited Load(Storage);
  FPCTranslationFile:=Storage.GetValue('FPCTranslationFile/Value','');
end;

procedure TFPCMessagesHelpDatabase.Save(Storage: TConfigStorage);
begin
  inherited Save(Storage);
  Storage.SetDeleteValue('FPCTranslationFile/Value',FPCTranslationFile,'');
end;

end.