-
Notifications
You must be signed in to change notification settings - Fork 4
/
printer.c
520 lines (406 loc) · 11.4 KB
/
printer.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
* Teradesk. Copyright (c) 1993 - 2002 W. Klaren,
* 2002 - 2003 H. Robbers,
* 2003 - 2013 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 <time.h>
#include <xscncode.h>
#include "resource.h"
#include "desk.h"
#include "error.h"
#include "xfilesys.h"
#include "font.h"
#include "config.h"
#include "window.h"
#include "file.h"
#include "copy.h"
#include "dir.h"
#include "events.h"
#include "printer.h"
#include "viewer.h"
#include "lists.h"
#include "slider.h"
#include "icon.h"
#define PBUFSIZ 1024L /* Should be divisible by 16 !!! */
#define PTIMEOUT 2000 /* 2000 * 1/200s = 10s printer timeout */
XATTR pattr; /* item attributes */
XFILE *printfile = NULL; /* print file; if NULL print to port */
_WORD printmode; /* text, hex, raw */
/*
* Print a character through GEMDOS.
*/
static bool prtchar(char ch)
{
long prttime;
_WORD button, error;
bool ready = FALSE;
bool result = FALSE;
char s;
if (printfile)
{
s = ch;
error = (_WORD) x_fwrite(printfile, &s, 1L);
if (error < 1)
{
xform_error(error);
result = TRUE;
}
} else
{
do
{
prttime = clock() + PTIMEOUT;
while (clock() < prttime && Cprnos() == 0)
(void) Syield();
if (Cprnos() != 0)
{
(void) Cprnout(ch);
result = FALSE;
ready = TRUE;
} else
{
button = alert_printf(2, APRNRESP);
result = TRUE;
ready = (button == 2);
}
} while (!ready);
}
return result;
}
/*
* print_eol() prints CR LF for end of line;
* same as prtchar above, it returns FALSE when OK!
*/
static bool print_eol(void)
{
bool status = FALSE;
if ((status = prtchar((char) 13)) == FALSE) /* CR */
status = prtchar((char) 10); /* LF */
return status;
}
/*
* print_line prints a CR-LF terminated line for directory or hex-dump print
* If the line is longer than plinelen it is wrapped to the next printer line.
* Function returns FALSE if ok, in style with other print functions
*/
static bool print_line(const char *dline /* 0-terminated line to print */
)
{
const char *p = dline; /* address of position in dline string */
_WORD i = 0; /* position in printer line */
bool status = FALSE; /* prtchar print status */
while (!status && (*p != 0))
{
status = prtchar(*p); /* beware: prtchar is false when OK ! */
p++; /* pointer to a char in the buffer */
i++; /* print line length */
if (!status && ((*p == 0) || (i >= options.plinelen))) /* end of line or line too long */
{
i = 0; /* reset linelength counter */
status = print_eol(); /* print CR-LF */
} /* if... */
} /* while... */
return status; /* this one is FALSE if ok, too! */
}
/*
* Print a complete file.
* The file is read PBUFSIZ characters at a time.
* Return 0 if successfull, error code otherwise.
*/
static _WORD print_file(WINDOW *w, /* ponter to window in which the item has been selected */
_WORD item /* item index in the window */
)
{
long l; /* index in buffer[] */
char *buffer; /* file is read into this */
char *name;
_WORD handle;
_WORD i;
_WORD error = 0;
_WORD ll = 0; /* line length counter */
_WORD result = 0;
bool stop = FALSE;
if ((name = itm_fullname(w, item)) == NULL)
return XFATAL;
/* Print a header here */
if (options.cprefs & P_HEADER)
{
if ((stop = print_line(name)) == FALSE) /* print header */
stop = print_eol(); /* print blank line */
if (stop)
{
free(name);
return XFATAL;
}
}
/* Now print the file itself */
buffer = malloc_chk(PBUFSIZ + 1L);
if (buffer != NULL)
{
hourglass_mouse();
if ((handle = x_open(name, O_DENYW | O_RDONLY)) >= 0)
{
long size = 0;
long a = 0;
do
{
/* Read no more than PBUFSIZ bytes (divisible by 16) */
if ((l = x_read(handle, PBUFSIZ, buffer)) >= 0)
{
buffer[l] = 0;
if (printmode == PM_HEX)
{
char tmp[HEXLEN + 2];
ll = 0;
size = size + l;
for (i = 0; i < (((_WORD) l - 1) / 16 + 1); i++)
{
disp_hex(tmp, &buffer[ll], a, size, TRUE);
if ((stop = print_line((const char *) (&tmp))) == TRUE)
break;
ll += 16;
a += 16;
}
} else
{
char *buffi = buffer;
for (i = 0; i < (_WORD) l; i++)
{
/* line wrap & new line handling */
if (printmode == PM_TXT) /* line wrap in text mode */
{
ll++;
if ((*buffi == (char) 13) || (*buffi == (char) 10) || (*buffi == (char) 12))
ll = 0; /* reset linelength counter at CR, LF or FF */
else if (ll >= options.plinelen)
{
ll = 0;
if ((stop = print_eol()) == TRUE)
break;
}
}
if ((stop = prtchar(*buffi)) == TRUE)
break;
buffi++;
}
}
/* Note: AV and termination messages will be processed here */
if (escape_abort(TRUE))
stop = TRUE;
} else
{
error = (_WORD) l;
}
} while ((l == PBUFSIZ) && (stop == FALSE));
x_close(handle);
if (printmode != PM_RAW)
stop = print_eol(); /* print CR-LF at end of file */
} else
error = handle;
if (stop)
result = XABORT;
if (error != 0)
result = xhndl_error(MEPRINT, error, itm_name(w, item));
arrow_mouse();
free(buffer);
} else
result = XFATAL;
free(name);
/* A formfeed at the end */
if (options.cprefs & P_HEADER)
if (prtchar((char) 12))
return XFATAL;
return result;
}
/*
* Check if an item can be printed, depending on item type.
* Drives, folders, network objects or unknown objects can not
* be printed.
* Note: parameter 'list' is locally modified
*/
bool check_print(WINDOW *w, /* poiner to window in which items have been selected */
_WORD n, /* number of selected items */
_WORD *list /* list of item indices */
)
{
_WORD mes, i;
ITMTYPE type;
for (i = 0; i < n; i++)
{
mes = 0;
type = itm_type(w, *list);
switch (type)
{
case ITM_DRIVE:
{
mes = MDRIVE;
break;
}
case ITM_FOLDER:
case ITM_PREVDIR:
{
mes = MFOLDER;
break;
}
default:
{
mes = trash_or_print(type);
break;
}
}
if (mes)
{
alert_cantdo(mes, MNOPRINT);
return FALSE;
}
list++;
}
return TRUE;
}
/*
* Print a list of items selected in a window, or print a directory of an
* open directory window. This routine currently does -not- recurse into
* subdirectories, but prints only files in the current directory level.
* Directory printout is performed by using lines as displayed in windows,
* and so sorting and display of diverse information is the same as for the
* window the directory of which is being printed.
*/
bool print_list(WINDOW *w, /* pointer to window in which itemsh have been selected */
_WORD n, /* number of seleced items */
_WORD *list, /* list of item indices */
long *folders, /* count of selected files */
long *files, /* count of selected files */
LSUM * bytes, /* total size of selected items */
_WORD function /* operation code: CMD_PRINT / CMD_PRINTDIR */
)
{
XATTR attr; /* Enhanced file attributes information */
XLNAME dline; /* sufficiently long string for a complete directory line */
char *path; /* Item's path */
const char *name; /* Item's name */
_WORD *item; /* (pointer to) item index */
_WORD i; /* counter */
_WORD amode; /* attribute finding mode; 0= follow links */
_WORD error; /* error code */
_WORD result; /* TRUE if operation successful */
ITMTYPE type; /* item type (file/folder...) */
ITMTYPE tgttype; /* link target type */
bool printerror = FALSE; /* true if there is an error in printing */
/* If this is a directory printout, then maybe print direcory title */
if (function == CMD_PRINTDIR && (options.cprefs & P_HEADER))
{
strcpy(dline, get_freestring(TDIROF)); /* Get "Directory of " string */
strcat(dline, ((DIR_WINDOW *) w)->title); /* Append window title */
if ((printerror = print_line(dline)) == FALSE)
printerror = print_eol();
}
if (printerror)
return FALSE;
/* Repeat print or dir-line print operation for each item in the list */
item = list;
for (i = 0; i < n; i++)
{
if (*item >= 0)
{
name = itm_name(w, *item);
if ((path = itm_fullname(w, *item)) == NULL)
{
result = copy_error(ENOMEM, name, function);
} else
{
type = itm_type(w, *item);
tgttype = itm_tgttype(w, *item);
amode = 0;
/* Dont follow links for directory printout or network items */
if (function == CMD_PRINTDIR || tgttype == ITM_NETOB)
amode = 1;
/* Now do whatever is needed to "print" an item */
if ((error = itm_attrib(w, *item, amode, &attr)) == 0) /* follow links */
{
if (function == CMD_PRINT)
{
/*
* Only files can be printed, ignore everything else.
* Executable files (programs) are hex-dumped.
*/
*folders = 0;
if (isfileprog(type))
{
_WORD oldmode = printmode;
result = 0;
if (tgttype != ITM_NETOB)
{
if (type == ITM_PROGRAM && printmode == PM_TXT)
printmode = PM_HEX; /* hex-dump */
upd_copyname(NULL, NULL, name);
result = print_file(w, *item);
printmode = oldmode;
}
*files -= 1;
sub_size(bytes, attr.st_size);
upd_copyname(NULL, NULL, empty);
}
} else
{
/* Printing of a directory line (all kinds of items) */
dir_line((DIR_WINDOW *) w, dline, *item);
if (dline[1] == (char) 7)
{
dline[1] = '\\'; /* Mark folders with "\" */
*folders -= 1;
} else
{
*files -= 1;
sub_size(bytes, attr.st_size);
}
printerror = print_line(dline);
if (escape_abort(cfdial_open))
printerror = TRUE;
}
} else
{
result = copy_error(error, name, function);
}
free(path);
/* If something is wrong, get out of the loop */
if (printerror)
result = XFATAL;
/* Update information on the number of folders/files/bytes remaining */
upd_copyinfo(*folders, *files, bytes);
}
/* Check for user abort (ESC key pressed) */
check_opabort(&result);
if (mustabort(result))
break;
}
item++;
}
/* Print directory summary, if needed */
if (!mustabort(result) && (function == CMD_PRINTDIR) && (options.cprefs & P_HEADER))
{
strcpy(dline, ((DIR_WINDOW *) w)->info);
if ((printerror = print_eol()) == FALSE) /* print blank line */
if ((printerror = print_line(dline)) == FALSE) /* print directory total */
if ((printerror = print_eol()) == FALSE) /* print blank line */
printerror = prtchar(12); /* print formfeed */
}
return (result == XFATAL || printerror) ? FALSE : TRUE;
}