File: print.c

package info (click to toggle)
slrn 1.0.3+dfsg-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,412 kB
  • sloc: ansic: 39,852; sh: 3,777; perl: 467; makefile: 229; sed: 24
file content (434 lines) | stat: -rw-r--r-- 9,207 bytes parent folder | download | duplicates (2)
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
/* -*- mode: C; mode: fold; -*- */
/*
 This file is part of SLRN.

 Copyright (c) 1994, 1999, 2007-2016 John E. Davis <jed@jedsoft.org>

 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.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "slrnfeat.h"

#include <stdio.h>
#include <string.h>

#ifdef __WIN32__
# include <windows.h>
#endif

#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <slang.h>

#include "jdmacros.h"
#include "print.h"
#include "misc.h"
#include "util.h"
#include "snprintf.h"
#include "strutil.h"
#include "common.h"

char *Slrn_Printer_Name;

/* I am using prefixes such as popen_ for the benefit of those without a
 * folding editor.
 */
#ifdef USE_NOTEPAD_PRINT_CODE
# define np_open_printer slrn_open_printer
# define np_close_printer slrn_close_printer
# define np_write_to_printer write_to_printer
# ifdef __WIN32__
#  define NOTEPAD_PRINT_CMD	"start /m notepad /p %s"
# else
#  ifdef VMS
#   define NOTEPAD_PRINT_CMD	"PRINT/QUEUE=SYS$PRINT %s"
#  else
#   ifdef __os2__
#    define NOTEPAD_PRINT_CMD	"print %s"
#   else
#    define NOTEPAD_PRINT_CMD	"lpr %s"
#   endif /* not OS/2 */
#  endif /* not VMS */
# endif /* not WIN32 */
#else
# ifdef __WIN32__
#  define USE_WIN32_PRINT_CODE	1
#  define win32_open_printer slrn_open_printer
#  define win32_close_printer slrn_close_printer
#  define win32_write_to_printer write_to_printer
# else
#  ifdef __unix__
#   define USE_POPEN_PRINT_CODE	1
#   define popen_open_printer slrn_open_printer
#   define popen_close_printer slrn_close_printer
#   define popen_write_to_printer write_to_printer
#  else
#   define USE_DUMMY_PRINT_CODE	1
#   define dummy_open_printer slrn_open_printer
#   define dummy_close_printer slrn_close_printer
#   define dummy_write_to_printer write_to_printer
#  endif
# endif
#endif

#ifdef USE_WIN32_PRINT_CODE /*{{{*/

# ifdef HAVE_WINSPOOL_H
#  include <winspool.h>
# else
#if 0
extern WINBOOL WINAPI OpenPrinterA(LPTSTR, LPHANDLE, LPVOID);
extern WINBOOL WINAPI ClosePrinter(HANDLE);
extern WINBOOL WINAPI EndPagePrinter(HANDLE);
#endif
extern DWORD WINAPI StartDocPrinterA(HANDLE, DWORD, LPBYTE);
extern WINBOOL WINAPI EndDocPrinter(HANDLE);
extern WINBOOL WINAPI StartPagePrinter(HANDLE);
extern WINBOOL WINAPI WritePrinter(HANDLE, LPVOID, DWORD, LPDWORD);
# endif

struct _Slrn_Print_Type
{
   int error_status;
   HANDLE h;
};

int win32_close_printer (Slrn_Print_Type *p)
{
   if ((p == NULL)
       || (p->h == INVALID_HANDLE_VALUE))
     return 0;

   EndDocPrinter (p->h);
   ClosePrinter (p->h);
   slrn_free ((char *) p);
   return 0;
}

Slrn_Print_Type *win32_open_printer (void)
{
   char printer_name_buf [256];
   HANDLE h;
   static DOC_INFO_1 doc_info =
     {
	"slrn-print-file",	       /* name of the document */
	NULL,			       /* name of output file to use */
	"RAW"			       /* data type */
     };
   Slrn_Print_Type *p;
   char *printer_name;

   if (NULL == (p = (Slrn_Print_Type *) SLmalloc (sizeof (Slrn_Print_Type))))
     return NULL;

   memset ((char *) p, 0, sizeof(Slrn_Print_Type));

   if (NULL == (printer_name = Slrn_Printer_Name))
     {
	(void) GetProfileString("windows", "device", "LPT1,,", printer_name_buf, sizeof(printer_name_buf));
	printer_name = slrn_strbyte (printer_name_buf, ',');
	if (NULL != printer_name)
	  *printer_name = 0;

	printer_name = printer_name_buf;
     }

   if (FALSE == OpenPrinterA (printer_name, &h, NULL))
     {
	slrn_error (_("OpenPrinterA failed: %lu"), GetLastError ());
	slrn_free ((char *) p);
	return NULL;
     }

   if (FALSE == StartDocPrinterA (h, 1, (unsigned char *) &doc_info))
     {
	slrn_error (_("StartDocPrinterA failed: %lu"), GetLastError ());
	ClosePrinter (h);
	slrn_free ((char *) p);
	return NULL;
     }

   p->h = h;
   return p;
}

static int win32_write_to_printer (Slrn_Print_Type *p, char *buf, unsigned int len)
{
   DWORD nlen;

   if ((p == NULL) || (p->h == INVALID_HANDLE_VALUE) || (p->error_status != 0))
     return -1;

   if (FALSE == WritePrinter (p->h, buf, len, &nlen))
     {
	p->error_status = 1;
	slrn_error (_("Write to printer failed: %lu"), GetLastError ());
	return -1;
     }

   return 0;
}

/*}}}*/
#endif				       /* USE_WIN32_PRINT_CODE */

#ifdef USE_NOTEPAD_PRINT_CODE /*{{{*/

struct _Slrn_Print_Type
{
   int error_status;
   FILE *fp;
   char file [SLRN_MAX_PATH_LEN];
};

int np_close_printer (Slrn_Print_Type *p)
{
   char *cmd;

   if ((p == NULL) || (p->fp == NULL))
     return 0;

   (void) slrn_fclose (p->fp);
   cmd = slrn_strdup_printf (NOTEPAD_PRINT_CMD, p->file);
   slrn_posix_system (cmd, 1);
   slrn_free (cmd);
   (void) slrn_delete_file (p->file);

   slrn_free ((char *) p);
   return 0;
}

Slrn_Print_Type *np_open_printer (void)
{
   Slrn_Print_Type *p;

   if (NULL == (p = (Slrn_Print_Type *) SLmalloc (sizeof (Slrn_Print_Type))))
     return NULL;

   memset ((char *) p, 0, sizeof(Slrn_Print_Type));

   if (NULL == (p->fp = slrn_open_tmpfile (p->file, sizeof (p->file))))
     {
	slrn_error (_("Failed to open tmp file"));
	slrn_free ((char *) p);
	return NULL;
     }

   return p;
}

static int np_write_to_printer (Slrn_Print_Type *p, char *buf, unsigned int len)
{
   if ((p == NULL) || (p->fp == NULL) || (p->error_status))
     return -1;

   if (len != fwrite (buf, 1, len, p->fp))
     {
	p->error_status = 1;
	slrn_error (_("Write to printer failed"));
	return -1;
     }

   return 0;
}

/*}}}*/
#endif

#ifdef USE_POPEN_PRINT_CODE /*{{{*/
struct _Slrn_Print_Type
{
   int error_status;
   FILE *fp;
};

int popen_close_printer (Slrn_Print_Type *p)
{
   int code;

   if ((p == NULL) || (p->fp == NULL))
     return 0;

   code = _slrn_pclose (p->fp);

   slrn_free ((char *) p);

   if (code == 0) return 0;

   slrn_error (_("Printer process returned error code %d"), code);
   return -1;
}

Slrn_Print_Type *popen_open_printer (void)
{
   char *print_cmd;
   Slrn_Print_Type *p;
   char print_cmd_buf[1024];

   if (NULL == (print_cmd = Slrn_Printer_Name))
     {
	print_cmd = getenv ("PRINTER");
	if ((print_cmd == NULL) || (*print_cmd == 0))
	  print_cmd = "lpr";
	else
	  {
	     slrn_snprintf (print_cmd_buf, sizeof (print_cmd_buf), "lpr -P%s",
			    print_cmd);
	     print_cmd = print_cmd_buf;
	  }
     }

   if (NULL == (p = (Slrn_Print_Type *) SLmalloc (sizeof (Slrn_Print_Type))))
     return NULL;

   memset ((char *) p, 0, sizeof(Slrn_Print_Type));

   if (NULL == (p->fp = popen (print_cmd, "w")))
     {
	slrn_error (_("Failed to open process %s"), print_cmd);
	popen_close_printer (p);
	return NULL;
     }

   return p;
}

static int popen_write_to_printer (Slrn_Print_Type *p, char *buf, unsigned int len)
{
   if ((p == NULL) || (p->fp == NULL) || (p->error_status))
     return -1;

   if (len != fwrite (buf, 1, len, p->fp))
     {
	p->error_status = 1;
	slrn_error (_("Error writing to print process"));
	return -1;
     }

   return 0;
}

/*}}}*/
#endif				       /* USE_POPEN_PRINT_CODE */

#ifdef USE_DUMMY_PRINT_CODE /*{{{*/
struct _Slrn_Print_Type
{
   int error_status;
};

Slrn_Print_Type *dummy_open_printer (void)
{
   slrn_error (_("Printer not supported"));
   return NULL;
}

static int dummy_write_to_printer (Slrn_Print_Type *p, char *buf, unsigned int len)
{
   return -1;
}

int dummy_close_printer (Slrn_Print_Type *p)
{
   return -1;
}

/*}}}*/
#endif				       /* USE_DUMMY_PRINT_CODE */

int slrn_write_to_printer (Slrn_Print_Type *p, char *buf, unsigned int len)
{
   char *b, *bmax;

   /* Map \n to \r\n */
   b = buf;
   bmax = b + len;

   while (b < bmax)
     {
	if (*b != '\n')
	  {
	     b++;
	     continue;
	  }

	len = (unsigned int) (b - buf);
	if (len)
	  {
	     if (*(b - 1) == '\r')
	       {
		  b++;
		  continue;
	       }

	     if (-1 == write_to_printer (p, buf, len))
	       return -1;
	  }

	if (-1 == write_to_printer (p, "\r\n", 2))
	  return -1;

	b++;
	buf = b;
     }

   len = (unsigned int) (b - buf);
   if (len)
     return write_to_printer (p, buf, len);

   return 0;
}

int slrn_print_file (char *file)
{
   Slrn_Print_Type *p;
   FILE *fp;
   char line [1024];

   if (file == NULL) return -1;

   fp = fopen (file, "r");
   if (fp == NULL)
     {
	slrn_error (_("Unable to open %s for printing"), file);
	return -1;
     }

   p = slrn_open_printer ();
   if (p == NULL)
     {
	fclose (fp);
	return -1;
     }

   while (NULL != fgets (line, sizeof (line), fp))
     {
	if (-1 == slrn_write_to_printer (p, line, strlen(line)))
	  {
	     fclose (fp);
	     slrn_close_printer (p);
	     return -1;
	  }
     }

   fclose (fp);

   return slrn_close_printer (p);
}