aboutsummaryrefslogtreecommitdiffstats
path: root/toolutils.c
blob: b7f2680291fd774ef54963977199d7a0829ae38d (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
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
/* toolutils.c */
/*****************************************************************************/
/* AS-Portierung                                                             */
/*                                                                           */
/* Unterroutinen fuer die AS-Tools                                           */
/*                                                                           */
/* Historie: 31. 5.1996 Grundsteinlegung                                     */
/*           27.10.1997 Routinen aus P2... heruebergenommen                  */
/*           27. 3.1999 Granularitaet SC144xx                                */
/*           30. 5.1999 Adresswildcard-Funktion                              */
/*                                                                           */
/*****************************************************************************/

#include "stdinc.h"
#include <string.h>

#include "strutil.h"
#include "cmdarg.h"
#include "stdhandl.h"
#include "ioerrs.h"

#include "nls.h"
#include "nlmessages.h"
#include "tools.rsc"

#include "toolutils.h"

LongWord Magic=0x1b34244d;

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

static Boolean DoFilter;
static int FilterCnt;
static Byte FilterBytes[100];

static char *InfoMessCopyright="(C) 1992,1998 Alfred Arnold";

Word FileID=0x1489;       /* Dateiheader Eingabedateien */
char *OutName="STDOUT";   /* Pseudoname Output */

static TMsgCat MsgCat;

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

	void WrCopyRight(char *Msg)
BEGIN
   printf("%s\n%s\n",Msg,InfoMessCopyright);
END

        void DelSuffix(char *Name)
BEGIN
   char *p,*z,*Part;

   p=Nil;
   for (z=Name; *z!='\0'; z++) if (*z=='\\') p=z;
   Part=(p!=Nil)?(p):(Name); Part=strchr(Part,'.');
   if (Part!=Nil) *Part='\0';
END

        void AddSuffix(char *s, char *Suff)
BEGIN
   char *p,*z,*Part;

   p=Nil;
   for (z=s; *z!='\0'; z++) if (*z=='\\') p=z;
   Part=(p!=Nil)?(p):(s);
   if (strchr(Part,'.')==Nil) strmaxcat(s,Suff,255);
END


	void FormatError(char *Name, char *Detail)
BEGIN
   fprintf(stderr,"%s%s%s (%s)\n",catgetmessage(&MsgCat,Num_FormatErr1aMsg),
                                  Name,catgetmessage(&MsgCat,Num_FormatErr1bMsg),Detail);
   fprintf(stderr,"%s\n",catgetmessage(&MsgCat,Num_FormatErr2Msg));
   exit(3);
END

	void ChkIO(char *Name)
BEGIN
   int io;

   io=errno;

   if (io==0) return;

   fprintf(stderr,"%s%s%s\n",catgetmessage(&MsgCat,Num_IOErrAHeaderMsg),Name,catgetmessage(&MsgCat,Num_IOErrBHeaderMsg));

   fprintf(stderr,"%s.\n",GetErrorMsg(io));

   fprintf(stderr,"%s\n",catgetmessage(&MsgCat,Num_ErrMsgTerminating));

   exit(2);
END

	Word Granularity(Byte Header)
BEGIN
   switch (Header)
    BEGIN
     case 0x09:
     case 0x76:
     case 0x7d:
      return 4;
     case 0x70:
     case 0x71:
     case 0x72:
     case 0x74:
     case 0x75:
     case 0x77:
     case 0x12:
     case 0x3b:
     case 0x6d:
      return 2;
     default:
      return 1;
    END
END

	void ReadRecordHeader(Byte *Header, Byte* Segment, Byte *Gran,
                              char *Name, FILE *f)
BEGIN
   if (fread(Header,1,1,f)!=1) ChkIO(Name);
   if ((*Header!=FileHeaderEnd) AND (*Header!=FileHeaderStartAdr))
    if (*Header==FileHeaderDataRec)
     BEGIN
      if (fread(Header,1,1,f)!=1) ChkIO(Name);
      if (fread(Segment,1,1,f)!=1) ChkIO(Name);
      if (fread(Gran,1,1,f)!=1) ChkIO(Name);
     END
    else
     BEGIN
      *Segment=SegCode;
      *Gran=Granularity(*Header);
     END
END

        void WriteRecordHeader(Byte *Header, Byte *Segment, Byte *Gran,
                               char *Name, FILE *f)
BEGIN
   Byte h;

   if ((*Header==FileHeaderEnd) OR (*Header==FileHeaderStartAdr))
    BEGIN
     if (fwrite(Header,1,1,f)!=1) ChkIO(Name);
    END
   else if ((*Segment!=SegCode) OR (*Gran!=Granularity(*Header)))
    BEGIN
     h=FileHeaderDataRec;
     if (fwrite(&h,1,1,f)) ChkIO(Name);
     if (fwrite(Header,1,1,f)) ChkIO(Name);
     if (fwrite(Segment,1,1,f)) ChkIO(Name);
     if (fwrite(Gran,1,1,f)) ChkIO(Name);
    END
   else
    BEGIN
     if (fwrite(Header,1,1,f)) ChkIO(Name);
    END
END

	CMDResult CMD_FilterList(Boolean Negate, char *Arg)
BEGIN
   Byte FTemp;
   Boolean err;
   char *p;
   int Search;
   String Copy;

   if (*Arg=='\0') return CMDErr;
   strmaxcpy(Copy,Arg,255);

   do
    BEGIN
     p=strchr(Copy,','); if (p!=Nil) *p='\0';
     FTemp=ConstLongInt(Copy,&err);
     if (NOT err) return CMDErr;

     for (Search=0; Search<FilterCnt; Search++)
      if (FilterBytes[Search]==FTemp) break;

     if ((Negate) AND (Search<FilterCnt))
      FilterBytes[Search]=FilterBytes[--FilterCnt];

     else if ((NOT Negate) AND (Search>=FilterCnt))
      FilterBytes[FilterCnt++]=FTemp;

     if (p!=Nil) strcpy(Copy,p+1);
    END
   while (p!=Nil);

   DoFilter=(FilterCnt!=0);

   return CMDArg;
END

	Boolean FilterOK(Byte Header)
BEGIN
   int z;

   if (DoFilter)
    BEGIN
     for (z=0; z<FilterCnt; z++)
      if (Header==FilterBytes[z]) return True;
     return False;
    END
   else return True;
END

        Boolean RemoveOffset(char *Name, LongWord *Offset)
BEGIN
   int z,Nest;
   Boolean err;

   *Offset=0;
   if (Name[strlen(Name)-1]==')')
    BEGIN
     z=strlen(Name)-2; Nest=0;
     while ((z>=0) AND (Nest>=0))
      BEGIN
       switch (Name[z])
        BEGIN
         case '(':Nest--; break;
         case ')':Nest++; break;
        END
       if (Nest!=-1) z--;
      END
     if (Nest!=-1) return False;
     else
      BEGIN
       Name[strlen(Name)-1]='\0';
       *Offset=ConstLongInt(Name+z+1,&err);
       Name[z]='\0';
       return err;
      END
    END
   else return True;
END

	void EraseFile(char *FileName, LongWord Offset)
BEGIN
   if (Offset==0); /* satisfy some compilers */
   
   if (unlink(FileName)==-1) ChkIO(FileName);
END

	void toolutils_init(char *ProgPath)
BEGIN
   Word z;
   LongWord XORVal;

   opencatalog(&MsgCat,"tools.msg",ProgPath,MsgId1,MsgId2);

   FilterCnt=0; DoFilter=False;
   for (z=0; z<strlen(InfoMessCopyright); z++)
    BEGIN
     XORVal=InfoMessCopyright[z];
     XORVal=XORVal << (((z+1)%4)*8);
     Magic=Magic^XORVal;
    END
END

	Boolean AddressWildcard(char *addr)
BEGIN
   return ((strcmp(addr, "$") == 0) OR (strcasecmp(addr, "0x") == 0));
END

#ifdef CKMALLOC
#undef malloc
#undef realloc

        void *ckmalloc(size_t s)
BEGIN
   void *tmp=malloc(s);
   if (tmp==NULL) 
    BEGIN
     fprintf(stderr,"allocation error(malloc): out of memory");
     exit(255);
    END
   return tmp;
END

        void *ckrealloc(void *p, size_t s)
BEGIN
   void *tmp=realloc(p,s);
   if (tmp==NULL)
    BEGIN
     fprintf(stderr,"allocation error(realloc): out of memory");
     exit(255);
    END
   return tmp;
END
#endif