-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.c
465 lines (396 loc) · 9.27 KB
/
file.c
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* HypView - (c) 2001 - 2006 Philipp Donze
* 2006 - Philipp Donze & Odd Skancke
*
* A replacement hypertext viewer
*
* This file is part of HypView.
*
* HypView 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.
*
* HypView 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 HypView; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#ifdef __GNUC__
#include <mintbind.h>
#include <fcntl.h>
#include "stat.h"
#else
#include <tos.h>
#endif
#include <gem.h>
#include "diallib.h"
#include "defs.h"
extern WINDOW_DATA *Win;
short LoadFile(DOCUMENT *doc, short handle)
{
short type;
type = HypLoad(doc, handle);
if(type == F_UNKNOWN)
type = AsciiLoad(doc, handle);
return type;
}
static short
find_file(char *path, char *real_path)
{
WINDOW_DATA *win = Win;
long ret;
char *list = path_list;
char *dst;
/* Falls schon eine Datei/ein Fenster geoeffnet wurde */
if (win)
{
DOCUMENT *doc = win->data;
char *filename;
/* Die neue Datei in dessen Pfad suchen */
strcpy(real_path, doc->path);
filename = strrchr(real_path, '\\');
if (!filename)
filename = real_path;
else
filename++;
strcpy(filename, path);
ret=Fopen(real_path, O_RDONLY);
/* Debug("1: Trying at %s => %ld",real_path,ret);
*/ if (ret >= 0)
{
Fclose((short)ret);
return TRUE;
}
/* Versuche die Datei als Link aufzuschluesseln */
ret = Freadlink(DL_PATHMAX, real_path, doc->path);
if (ret == 0)
{
/* Debug("=> %ld: '%s' is at '%s'",ret,doc->path,real_path);
*/
filename = strrchr(real_path, '\\');
if (!filename)
filename = real_path;
else
filename++;
strcpy(filename, path);
ret = Fopen(real_path, O_RDONLY);
/* Debug("2: Trying at %s => %ld",real_path,ret);
*/ if (ret >= 0)
{
Fclose((short)ret);
return TRUE;
}
}
}
/* Pfad, so wie er gegeben ist ausprobieren */
ret=Fopen(path, O_RDONLY);
/* Debug("3: Trying at %s => %ld",path,ret);
*/
if (ret >= 0)
{
if (path[1] == ':') /* Handelt es sich um einen absoluten Pfad? */
strcpy(real_path, path);
else
{
/* Absoluten Pfad ermitteln */
real_path[0] = Dgetdrv() + 'A';
real_path[1] = ':';
Dgetpath(&real_path[2], 0);
dst =& real_path[strlen(real_path)];
*dst++ = dir_separator;
strcat(real_path, path);
}
Fclose((short)ret);
return TRUE;
}
if (path[1] == ':') /* Handelt es sich um einen absoluten Pfad? */
{ /* Dann Dateinamen extrahieren */
dst = strrchr(path, '\\');
if (dst)
strcpy (path, dst + 1);
}
/* Pfad mit Pfad aus der Pfadliste kombiniert probieren */
while (*list)
{
dst = real_path;
while (*list && (*list != ';'))
*dst++ = *list++;
*dst-- = 0;
list++;
if (*dst != dir_separator)
{
dst++;
*dst++ = dir_separator;
*dst = 0;
}
strcat(real_path, path);
ret = Fopen(real_path, O_RDONLY);
/* Debug("4. Trying at %s => %ld",real_path,ret);
*/
if (ret>=0)
{
Fclose((short)ret);
return TRUE;
}
}
return FALSE;
}
static DOCUMENT *
open_file(char *path)
{
DOCUMENT *doc = NULL;
long ret;
short handle;
char *ptr;
ret = Fopen(path, O_RDONLY);
if (ret < 0)
{
FileErrorNr(path, ret);
return NULL;
}
handle = (short)ret;
/* Nicht gefunden? --> neues Dokument erstellen */
doc = (DOCUMENT *)Malloc(sizeof(DOCUMENT) + strlen(path) + 1);
if (!doc)
{
form_alert(1, string_addr[DI_MEMORY_ERROR]); /* Fehler melden */
Fclose(handle);
return NULL;
}
memset(doc, 0, sizeof(DOCUMENT));
doc->path = (char *)((long)doc + sizeof(DOCUMENT));
strcpy(doc->path, path);
ptr = strrchr(doc->path, dir_separator);
if (!ptr++)
ptr = doc->path;
doc->filename = ptr;
doc->lines = 0;
doc->columns = 0;
doc->window_title = doc->path;
doc->buttons = 0;
doc->data = NULL;
doc->next = NULL;
doc->popup = NULL;
doc->type = -1;
doc->autolocator = NULL;
doc->selection.valid = FALSE;
/* Hier folgt die typ-spezifische Lade-Routine */
if(LoadFile(doc, handle) < 0)
{
FileError(doc->filename, "format not recognized");
Mfree(doc);
doc = NULL;
}
else
{
XATTR attr;
/* Aenderungsdatum und -zeit sichern */
if (Fxattr(0/*FXATTR_RESOLVE*/, doc->path, &attr) < 0L)
{
doc->mtime = 0;
doc->mdate = 0;
}
else
{
doc->mtime = attr.mtime;
doc->mdate = attr.mdate;
}
}
Fclose(handle);
return doc;
}
/* Entfernt die Datei <doc> aus dem Speicher und aus der Liste.
<doc> muss (!!) ein Zeiger auf ein Dokument in der Liste sein. */
void CloseFile(DOCUMENT *doc)
{
/* Hier folgt die typ-spezifische Aufrum-Arbeit.*/
if (doc->data)
doc->closeProc(doc);
if (doc->autolocator)
Mfree(doc->autolocator);
Mfree(doc);
}
/* Oeffnet eine Datei in einem neuen Fenster */
short OpenFileNW(char *path, char *chapter, long node)
{
DOCUMENT *doc;
char real_path[DL_PATHMAX];
/* Leere Zeichenkette (=kein Pfad) */
if (!*path)
return FALSE;
/*
Debug("OpenFileNW(<%s>,Chapter <%s>)",path,(chapter?chapter:"N/A"));
*/
graf_mouse(BUSYBEE, NULL);
/* Falls der Suchpfad mit "*:\" beginnt (=> HYP-Datei): entfernen */
if (strncmp(path, "*:\\", 3) == 0)
path += 3;
if (!find_file(path, real_path))
{
graf_mouse(ARROW, NULL);
FileError(path, "not found");
return FALSE;
}
/* Datei laden/initialisieren */
doc = open_file(real_path);
if (doc)
{
doc->gotoNodeProc(doc, chapter, node);
/* neues Fenster anlegen? */
OpenWindow(HelpWindow, NAME|CLOSER|FULLER|MOVER|SIZER|
UPARROW|DNARROW|VSLIDE|LFARROW|RTARROW|HSLIDE|
SMALLER, doc->path, -1, -1, doc);
}
else
{
graf_mouse(ARROW, NULL);
return FALSE;
}
return TRUE;
}
/* Oeffnet eine Datei im gleichen Fenster falls <new_win> < 2 */
short OpenFileSW(char *path, char *chapter, short new_win)
{
WINDOW_DATA *win = Win;
DOCUMENT *doc = NULL;
char real_path[DL_PATHMAX];
/* Leere Zeichenkette (=kein Pfad) */
if (!*path)
return FALSE;
/* Debug("OpenFileSW(<%s>,Chapter <%s>,new win %d)",path,(chapter?chapter:"N/A"),new_win);
*/
graf_mouse(BUSYBEE, NULL);
/* Falls der Suchpfad mit "*:\" beginnt (=> HYP-Datei): entfernen */
if (strncmp(path, "*:\\", 3) == 0)
path+=3;
if (!find_file(path, real_path))
{
graf_mouse(ARROW, NULL);
FileError(path, "not found");
return FALSE;
}
/* Nur wenn ntig die Datei neu Laden */
if (new_win == 1)
{
DOCUMENT *doc2;
/* Gibt es ein Fenster, dass die gewnschte Datei darstellt? */
Win = (WINDOW_DATA *)all_list;
while (Win)
{
if(Win->type == WIN_WINDOW)
{
doc2 = Win->data;
if (strcmp(doc2->path, real_path) == 0)
{
AddHistoryEntry(Win);
doc = doc2;
break;
}
}
Win = Win->next;
}
win = Win;
}
else if (win)
{
DOCUMENT *prev_doc = win->data;
AddHistoryEntry(win);
/* Datei als aktuelles Dokument im Fenster geladen? */
if (strcmp(prev_doc->path, real_path) == 0)
{
doc = prev_doc;
win->data = prev_doc->next;
prev_doc->next = NULL;
}
else
{
prev_doc->closeProc(prev_doc);
doc = prev_doc->next;
while (doc)
{
if (strcmp(doc->path, real_path) == 0)
{
prev_doc->next = doc->next;
doc->next = NULL;
break;
}
prev_doc = doc;
doc = doc->next;
}
}
}
if (!doc) /* Wenn noetig Datei laden/initialisieren */
doc = open_file(real_path);
if (doc)
{
if (!doc->data)
{
long ret;
/* Datei erneut laden */
ret = Fopen(doc->path, O_RDONLY);
if(ret >= 0)
LoadFile(doc, (short)ret);
else
FileErrorNr(doc->filename, ret);
Fclose((short)ret);
}
doc->gotoNodeProc(doc, chapter, 0);
/* Noch kein Fenster offen? */
if(!win)
OpenWindow(HelpWindow, NAME|CLOSER|FULLER|MOVER|SIZER|UPARROW|DNARROW|
VSLIDE|LFARROW|RTARROW|HSLIDE|SMALLER, doc->path,
-1, -1, doc);
else
{
if(win->status & WIS_ICONIFY)
UniconifyWindow(win);
doc->next = win->data;
Win = win;
ReInitWindow(win, doc);
wind_set(win->whandle, WF_TOP, 0, 0, 0, 0);
graf_mouse(ARROW, NULL);
}
}
else
{
graf_mouse(ARROW,NULL);
return FALSE;
}
return TRUE;
}
/* Verifies the current documents file modification time/date and reloads the
* document if necessary. This behaviour is enabled by the CHECK_TIME option. */
void CheckFiledate(DOCUMENT *doc)
{
XATTR attr;
long ret;
ret = Fxattr(0/*FXATTR_RESOLVE*/, doc->path, &attr);
if (ret == 0)
{
/* Modification time or date has changed ?*/
if (attr.mtime != doc->mtime || attr.mdate != doc->mdate)
{
long node = 0;
graf_mouse(BUSYBEE, NULL); /* We are busy... */
node = doc->getNodeProc(doc); /* Remember current node */
doc->closeProc(doc); /* Close document */
/* Reload file */
ret = Fopen(doc->path, O_RDONLY);
if (ret >= 0)
LoadFile(doc, (short)ret);
else
FileErrorNr(doc->filename, ret);
Fclose((short)ret);
/* jump to previously active node */
doc->gotoNodeProc(doc, NULL, node);
ReInitWindow(Win, doc);
graf_mouse(ARROW, NULL); /* We are done. */
}
}
}