summaryrefslogtreecommitdiff
path: root/fpcsrc/utils/h2pas/h2poptions.pas
blob: 3d8cda212102e7029533e15b4bce295de4b60b05 (plain)
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
{
    Copyright (c) 1998-2000 by Florian Klaempfl

    This program 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 program 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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************}
unit h2poptions;
interface

const
   version = '0.99.16';

var
   inputfilename, outputfilename : string; { Filenames }
   LibFileName, unitname         : string; { external library name }
   CompactMode,
   stripinfo,                 { Don't write info comments to output }
   UseLib,                    { Append external to implementation ?  }
   UseName,                   { Append 'libname name 'funcname ' }
   UsePPOinters,              { Use P instead of ^ for pointers    }
   EnumToConst,               { Write enumeration types as constants }
   Win32headers,              { allows dec_specifier }
   stripcomment,              { strip comments from inputfile }
   PrependTypes,              { Print T in front of type names ?   }
   UseCTypesUnit,             { Use types defined in the ctypes unit}
   createdynlib,              { creates a unit which loads dynamically the imports to proc vars }
   RemoveUnderscore : Boolean;
   usevarparas : boolean;     { generate var parameters, when a pointer }
                              { is passed                               }
   includefile : boolean;     { creates an include file instead of a unit }
   palmpilot : boolean;       { handling of PalmOS SYS_CALLs }
   packrecords: boolean;      { All records should be packed in the file }

{ Helpers }
Function ForceExtension(Const HStr,ext:String):String;
Function MaybeExtension(Const HStr,ext:String):String;

{ Options }
Procedure ProcessOptions;


Implementation


{*****************************************************************************
                                 Helpers
*****************************************************************************}

Function ForceExtension(Const HStr,ext:String):String;
{
  Return a filename which certainly has the extension ext
  (no dot in ext !!)
}
var
  j : longint;
begin
  j:=length(Hstr);
  while (j>0) and (Hstr[j]<>'.') do
   dec(j);
  if j=0 then
   j:=255;
  ForceExtension:=Copy(Hstr,1,j-1)+'.'+Ext;
end;


Function MaybeExtension(Const HStr,ext:String):String;
{
  Return a filename which certainly has the extension ext
  (no dot in ext !!)
}
var
  j : longint;
begin
  j:=length(Hstr);
  while (j>0) and (Hstr[j]<>'.') do
   dec(j);
  if j=0 then
   MaybeExtension:=Hstr+'.'+Ext
  else
   MaybeExtension:=Hstr;
end;

function ExtractFileName(const AFilePath : String): String;
var i : Integer;
begin
  i := Length(AFilePath);
  while (i>0) and (AFilePath[i]<>DirectorySeparator) and
    (AFilePath[i]<>DriveSeparator) do
  begin
    Dec(i);
  end;
  ExtractFileName := Copy(AFilePath,i+1,Length(AFilePath));
end;

{*****************************************************************************
                                Options
*****************************************************************************}

Procedure Usage;
begin
  writeln ('Usage : ',paramstr(0),' [options]  filename');
  writeln ('        Where [options] is one or more of:');
  writeln ('        -d                 Use external;');
  writeln ('        -D                 use external libname name ''func_name'';');
  writeln ('        -e                 change enum type to list of constants');
  writeln ('        -c                 Compact outputmode, less spaces and empty lines');
  WriteLn ('        -C                 Use types in ctypes unit');
  writeln ('        -i                 create include files (no unit header)');
  writeln ('        -l libname         Specify the library name for external');
  writeln ('        -o outputfilename  Specify the outputfilename');
  writeln ('        -p                 Use "P" instead of "^" for pointers');
  writeln ('        -pr                Pack all records (1 byte alignment)');
  writeln ('        -P                 use proc. vars for imports');
  writeln ('        -s                 strip comments from inputfile');
  writeln ('        -S                 strip comments and don''t write info to outputfile.');
  writeln ('        -t                 Prepend typedef type names with T');
  writeln ('        -T                 Prepend typedef type names with T, and remove _');
  writeln ('        -u unitname        Specify the name of the unit.');
  writeln ('        -v                 replace pointer parameters by call by');
  writeln ('                           reference parameters');
  writeln ('        -w                 special for win32 headers');
  writeln ('        -x                 handle SYS_TRAP of PalmOS header files');
  halt (0);
end;


Procedure ProcessOptions;
Var
  cp : string;
  I : longint;

  Function GetNextParam (const Opt,Name : String) : string;
  begin
   if i=paramcount then
    begin
      writeln ('Error : -',Opt,' : ',name,' expected');
      halt(1);
    end
   else
    begin
      GetNextParam:=paramstr(i+1);
      inc(i);
    end;
  end;

begin
  if paramcount=0 then
    Usage;
  inputfilename:='';
  outputfilename:='';
  LibFileName:='';
  UnitName:='';
  CompactMode:=false;
  UseLib:=false;
  UseName:=false;
  StripComment:=false;
  StripInfo:=false;
  UsePPointers:=false;
  UseCTypesUnit := false;
  EnumToCOnst:=false;
  usevarparas:=false;
  palmpilot:=false;
  includefile:=false;
  packrecords:=false;
  createdynlib:=false;
  i:=1;
  while i<=paramcount do
   begin
     cp:=paramstr(i);
     if cp[1]='-' then
      begin
        case cp[2] of
         'c' : CompactMode:=true;
         'C' : UseCTypesUnit := true;
         'e' : EnumToConst :=true;
         'd' : UseLib      :=true;
         'D' : begin
                 UseLib      :=true;
                 usename     :=true;
               end;
         'i' : includefile:=true;
         'l' : LibFileName:=GetNextParam ('l','libname');
         'o' : outputfilename:=GetNextParam('o','outputfilename');
         'P' : createdynlib:=true;
         'p' : begin
                  if (cp[3] = 'r') then
                     begin
                        PackRecords := true;
                     end
                  else
                      UsePPointers:=true;
               end;
         's' : stripcomment:=true;
         'S' : begin
                 stripcomment:=true;
                 stripinfo:=true;
               end;
         't' : PrependTypes:=true;
         'T' : begin
                 PrependTypes:=true;
                 RemoveUnderscore:=true;
               end;
         'u' : UnitName:=GetNextParam ('u','unitname');
         'v' : usevarparas:=true;
         'w' : begin
                  Win32headers:=true;
                  UseLib:=true;
                  usename:=true;
                  usevarparas:=true;
                  LibFileName:='kernel32';
               end;
         'x' : palmpilot:=true;
         else
           Writeln ('Illegal option : ',cp);
         end
      end
     else
      begin { filename }
        if inputfilename<>'' then
         begin
           writeln ('Error : only one filename supported. Found also :',cp);
           halt(1);
         end;
        inputfilename:=MaybeExtension(cp,'h');
        if outputfilename='' then
         outputfilename:=ForceExtension (inputfilename,'pp');
      end;
     inc(i);
   end;
  If inputfilename='' then
    Usage;
  if UnitName='' then
   begin
     UnitName := ExtractFileName(outputfilename);
     i:=pos('.',UnitName)-1;
     if i>0 then
      UnitName:=Copy(UnitName,1,i);
   end;
end;

end.