-
Notifications
You must be signed in to change notification settings - Fork 4
/
open.c
479 lines (393 loc) · 11.4 KB
/
open.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Teradesk. Copyright (c) 1993 - 2002 W. Klaren,
* 2002 - 2003 H. Robbers,
* 2003 - 2016 Dj. Vukovic
*
* This file is part of Teradesk.
*
* Teradesk 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.
*
* Teradesk 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 Teradesk; if not, write to the Free Software
* Foundation, Inc.,51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA
*/
#include <library.h>
#include <xdialog.h>
#include "resource.h"
#include "desk.h"
#include "error.h"
#include "open.h"
#include "xfilesys.h"
#include "copy.h"
#include "printer.h"
#include "font.h"
#include "config.h"
#include "window.h" /* before dir.h and viewer.h */
#include "dir.h"
#include "file.h"
#include "viewer.h"
#include "lists.h"
#include "slider.h"
#include "filetype.h"
#include "applik.h"
#include "prgtype.h"
#include "icon.h"
bool onone = FALSE; /* true if only one instance should run */
bool onfile = FALSE; /* true if app is started to open a file */
const char *hyppage = NULL; /* name of the hypertext page to display */
/*
* Handle the Show/Edit/Run... dialog. Return button index.
* If "Show" or "Edit" or "Cancel" is selected, just return the code;
* Otherwise open additional dialog(s).
*/
static _WORD open_dialog(void)
{
XDINFO owinfo;
_WORD thebutton, button = 0; /* assuming that no button will ever have index 0 */
if (chk_xd_open(openw, &owinfo) >= 0)
{
button = xd_form_do(&owinfo, ROOT);
thebutton = button;
switch (button)
{
case OWRUN:
{
prg_info(NULL, empty, 0, (LSTYPE *) & awork);
log_shortname(awork.shname, awork.name);
if (!prgtype_dialog(NULL, 0, (LSTYPE *) & awork, LS_APPL | LS_EDIT))
button = 0;
break;
}
case OWUSE:
{
log_shortname(fwork.filetype, awork.name);
selitem = NULL;
app_install(LS_SELA, &applikations); /* selitem is nonnull only if successful here */
if (!selitem)
button = 0;
}
default:
{
break;
}
}
xd_buttnorm(&owinfo, thebutton);
xd_close(&owinfo);
}
return button;
}
/*
* Open an item selected in a window or else explicitely specified.
* If there is an explicit specification in "theitem", then "inw"
* and "initem" are ignored. Action depends on the type of object
* being detected. Also, if "theitem" is a program, "thecommand"
* may contain a command line, if not NULL
*/
bool item_open(WINDOW *inw, /* window in which the selection is made */
_WORD initem, /* ordinal of the selected item from the window */
_WORD kstate, /* keyboard state while opening the item */
char *theitem, /* explicitely specified full item name */
char *thecommand /* command line if the item is a program */
)
{
WINDOW *w; /* "inw", locally (i.e. maybe changed) */
DIR_WINDOW simw; /* pointer to a simulated dir window */
APPLINFO *appl; /* Pointer to information on the app to run */
char *qline = NULL; /* requoted content of openline */
char *cmline = NULL; /* Command passed to application from "Open" dialog" */
char *realname = NULL; /* link target name */
char *blank; /* pointer to a ' ' in the name */
char *path; /* constructed path of the item to open */
VLNAME epath; /* Path of the item specified in "Open" */
VLNAME ename; /* name of the item specified in "Open" */
_WORD item; /* "initem", locally (i.e. maybe changed) */
_WORD error;
_WORD button; /* index of the button pressed */
bool alternate = FALSE;
bool deselect = FALSE;
ITMTYPE type; /* item type (file, folder, program...) */
item = initem;
autoloc_off();
if ((kstate & K_ALT) != 0)
alternate = TRUE;
if (inw && !theitem)
{
/*
* An item is specified by selection in a window; get its full name
* Note: it is possible that 'realname' be NULL (for trashcan, printer...)
*/
realname = itm_tgtname(inw, initem);
/* Try to divine which type of item this is */
type = itm_tgttype(inw, initem);
/* Is this really needed ? */
if (realname && type != ITM_NOTUSED && !trash_or_print(type))
type = diritem_type((char *) realname);
/* If "Alternate" is pressed a program is treated like ordinary file */
if (alternate && (type == ITM_PROGRAM))
type = ITM_FILE;
} else
{
/* Open a form to explicitely enter item name */
if (!theitem)
{
rsc_title(newfolder, NDTITLE, DTOPENIT);
obj_hide(newfolder[DIRNAME]);
obj_unhide(newfolder[OPENNAME]);
xd_init_shift(&newfolder[OPENNAME], openline);
button = chk_xd_dialog(newfolder, ROOT);
}
/* Note: theitem must come first below */
if (theitem || (button == NEWDIROK))
{
/*
* For some reason if(!theitem){}else{} here
* gives shorter code than if(theitem){}else{}
*/
if (!theitem)
{
/*
* Object specified on a command line is opened.
* First remove leading and trailing blanks from the line
*/
strip_name(openline, openline);
/* Continue only if something remains on the line */
if (strlen(openline) == 0)
return FALSE;
/*
* Try to see if there is a command attached.
* Separate this command from item name by inserting a '0'
* instead of the (first) space between the two.
* If there is no command, 'cmline' will point
* to an empty string.
*/
cmline = (char *) (long) empty; /* first, cmline points to an empty string */
qline = malloc_chk(strlenq(openline));
if (qline == NULL)
return FALSE;
strcpyrq(qline, openline, '"', &blank); /* 34 = double quote */
if (blank)
{
*blank = 0;
cmline = blank + 1; /* now cmline points to after the first blank */
}
/* Unquote item name */
strcpyuq(qline, qline);
/* Find the real name of the item */
if ((realname = x_fllink(qline)) == NULL)
{
free(qline);
return FALSE;
}
/* Restore complete line (for the next opening) */
if (blank)
*blank = ' ';
} else
{
/* Item name is explicitely specified in "theitem" */
if ((realname = x_fllink(theitem)) == NULL)
return FALSE;
if (thecommand && *thecommand)
cmline = thecommand;
}
} else
{
return FALSE;
}
/*
* Try to divine which type of item this is. This is done
* by analyzing the name and by examining the object's
* attributes- if the object does not exist, there will
* be an error warning.
*/
type = diritem_type((char *) realname);
}
/* Is this name (path) too long, or wrong in some other way? */
if (realname && ((error = x_checkname(realname, NULL)) != 0))
{
free(realname);
free(qline);
xform_error(error);
return FALSE;
}
/* Now that the type of the item is known, do something */
switch (type)
{
case ITM_TRASH:
case ITM_PRINTER:
case ITM_NOTUSED:
/* Object is a trah can or a printer (or unknown) and can not be opened */
alert_cantdo(trash_or_print(type), MNOOPEN);
break;
default:
/* Separate "realname" into "ename" and "epath" */
if (type == ITM_NETOB)
{
strcpy(ename, realname);
strcpy(epath, empty);
} else
{
split_path(epath, ename, realname);
}
/*
* Simulate some structures of a directory window so that existing
* routines expecting an item selected in a window can be used
*/
w = (WINDOW *) & simw;
dir_simw(&simw, epath, ename, type);
item = 0;
break;
}
free(realname);
/*
* Object real name and type are determined by now.
* Action according to type of the item follows...
*/
switch (type)
{
case ITM_DRIVE:
/* Object is a disk volume (codes 0 to 25 for check_drive() ) */
if ((path = itm_fullname(w, item)) != NULL)
{
if (check_drive((path[0] & 0xDF) - 'A') == FALSE)
{
free(path);
free(qline);
return FALSE;
} else
{
deselect = dir_add_dwindow(path);
}
} else
{
deselect = FALSE;
}
break;
case ITM_FOLDER:
/* Object is a folder */
if ((path = itm_fullname(w, item)) != NULL)
deselect = dir_add_dwindow(path);
else
deselect = FALSE;
break;
case ITM_PREVDIR:
/* Object is a parent folder */
if ((path = fn_get_path(wd_path(w))) != NULL)
deselect = dir_add_dwindow(path);
else
deselect = FALSE;
break;
case ITM_PROGRAM:
/* Object is a program */
if ((path = itm_fullname(w, item)) != NULL)
{
deselect = app_exec(path, NULL, NULL, (_WORD *) cmline, cmline ? -1 : 0, kstate);
free(path);
}
break;
case ITM_NETOB:
case ITM_FILE:
/* Object is a file */
naap = 0; /* see app_find() */
onfile = TRUE;
deselect = FALSE;
if (!alternate && (appl = app_find(itm_name(w, item), TRUE)) != NULL)
{
deselect = app_exec(NULL, appl, w, &item, 1, kstate);
} else if (naap == 0)
{
/* File type assignment has been bypassed: now show/edit/cancel */
if (theitem)
{
button = OWSHOW;
} else
{
memclr(&awork, sizeof(awork));
awork.name = itm_fullname(w, item);
button = open_dialog();
}
switch (button)
{
case OWSHOW:
{
/* Call the viewer program or open a text window */
if ((deselect = app_specstart(AT_VIEW, w, &item, 1, kstate)) == 0)
deselect = txt_add_window(w, item, kstate, NULL);
break;
}
case OWEDIT:
{
/* Call the editor program */
if ((deselect = app_specstart(AT_EDIT, w, &item, 1, kstate)) == 0)
alert_iprint(TNOEDIT);
break;
}
case OWRUN:
{
/* Run this file as a program/application */
onfile = FALSE;
deselect = app_exec(NULL, &awork, NULL, NULL, 0, kstate);
break;
}
case OWUSE:
{
/* Open this file with the selected application */
deselect = app_exec(NULL, (APPLINFO *) selitem, w, &item, 1, kstate);
break;
}
default:
{
/* Do nothing */
break;
}
}
free(awork.name);
}
onfile = FALSE;
break;
default:
/* Object is a trashcan, printer, or not recognized */
deselect = FALSE;
break;
}
free(qline);
return deselect;
}
/*
* Open a page from the hypertext User manual.
* The hypertext viewer application is detected and a command is sent to it.
* The command will typically be a menu text which is supposed to be
* identical to a page name in the hypertext except for two leading blanks
* and possibly an appended keyboard shortcut. All these will be stripped
* in this routine. Therefore, the "page" string must be at least
* 6 characters long
* Note 1: according to AV-protocol documentation, all filenames
* must be input in capitals with complete paths (at least the documentation
* for ST-Guide says so. How does this comply with unixoid filesystems?
* Maybe should use lowercase after all?)
* Note 2: ST-guide supports a special path "*:\" meaning "all known paths"
*/
void opn_hyphelp(void)
{
APPLINFO *helpprg = app_find("*.HYP", FALSE);
if (helpprg && hyppage)
{
LNAME cmd;
onfile = TRUE;
/* ST-Guide accepts only exactly one blank between filename and pagename */
strcpy(cmd, "*:\\TERADESK.HYP");
if (*hyppage)
{
strcat(cmd, &hyppage[1]);
cmd[strlen(cmd) - 5] = '\0';
}
app_exec(NULL, helpprg, NULL, (_WORD *) cmd, -1, 0);
}
}