File: lp.c

package info (click to toggle)
elvis 2.1i-3
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 4,120 kB
  • ctags: 5,838
  • sloc: ansic: 53,854; sh: 811; makefile: 263
file content (351 lines) | stat: -rw-r--r-- 8,354 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
/* lp.c */
/* Copyright 1995 by Steve Kirkendall */

char id_lp[] = "$Id: lp.c,v 2.28 1997/11/02 18:54:45 steve Exp $";

/* This file contains generic printing code. */

#include "elvis.h"
#ifdef FEATURE_LPR

#if USE_PROTOTYPES
static LPTYPE *findtype(char *name);
#if defined (GUI_WIN32)
static void dummyprt(_CHAR_ ch);
#endif
static void prtchar(_CHAR_ ch);
static void draw(CHAR *p, long qty, _char_ font, long offset);
#endif


/* This is a list of all known printer types.  The first one is the default. */
static LPTYPE	*alltypes[] = {&lpepson, &lppana, &lpibm, &lphp, &lpdumb, &lpcr, &lpbs, &lpps, &lpps2,
#if defined (GUI_WIN32)
 &lpwindows,
#endif
};
static LPTYPE	*type;		/* type of printer being used at the moment */

/* Page formatting variables */
static int	pagenum;	/* page number withing this printing */
static int	linesleft;	/* number of usable lines left on this page */
static int	column;		/* column number, used to implement wrapping */
static WINDOW	prwin;		/* window doing the printing */
static BOOLEAN	anytext;	/* False if blank page, True if any text */
static long	lnum;		/* line number of current line */

/* Output buffering variables */
static CHAR	*iobuf;		/* pointer to the I/O buffer */
static int	iomax;		/* number of CHARS that the iobuf can hold */
static int	ionext;		/* number of CHARS currently in iobuf */

/* This function converts a type name to an LPTYPE pointer.  Returns NULL if
 * unsuccessful.  If the name is NULL, it returns the default LPTYPE.
 */
static LPTYPE *findtype(name)
	char	*name;	/* name of printer type */
{
	int	i;

	/* if name is NULL, return the default type */
	if (name == NULL)
	{
		return alltypes[0];
	}

	/* search for the name in the list */
	for (i = 0; i < QTY(alltypes); i++)
	{
		if (!strcmp(name, alltypes[i]->name))
		{
			return alltypes[i];
		}
	}

	/* failed */
	return NULL;
}

#if defined (GUI_WIN32)
static void dummyprt(ch)
	_CHAR_	ch;	/* character to be sent to printer */
{
}
#endif
/* This function copies a single character to the printer (or file).
 * Internally, is uses a large I/O buffer for the sake of efficiency.
 */
static void prtchar(ch)
	_CHAR_	ch;	/* character to be sent to printer */
{
	assert(ionext >= 0 && ionext < iomax);
	iobuf[ionext++] = ch;
	if (ionext == iomax)
	{
		iowrite(iobuf, ionext);
		ionext = 0;
	}
}

/* This function takes a single character from the mode's image() function
 * and passes it to the LP driver.  This function also performs newline
 * conversion, line wrapping/clipping, and page breaks.
 */
static void draw(p, qty, font, offset)
	CHAR	*p;	/* character to be output */
	long	qty;	/* number of characters to be output */
	_char_	font;	/* font of character */
	long	offset;	/* buffer offset of ch, or -1 if not from buffer */
{
	WINDOW	win;
	long	delta;
	CHAR	ch;
	char	lnumstr[20];

	/* A negative qty indicates that the character *p is to be repeated.
	 */
	if (qty < 0)
	{
		delta = 0;
		qty = -qty;
	}
	else
	{
		delta = 1;
	}

	for ( ; qty > 0; qty--, p += delta)
	{
		ch = *p;

		/* VTAB is treated either as a '\f', or as two '\n's */
		if (ch == '\013' || (o_lplines == 0 && ch == '\f'))
		{
			if (o_lplines == 0 || linesleft >= 5)
			{
				ch = '\n';
				draw(&ch, 1L, font, offset);
			}
			else
			{
				ch = '\f';
			}
		}

		/* maybe output a line number */
		if (column == 0		/* at start of line */
		 && o_lpnumber		/* "lpnumber" option is set */
		 && offset >= -1	/* not doing header or line number */
		 && ch != '\f')		/* character isn't formfeed */
		{
			/* output a line number */
			sprintf(lnumstr, "%6ld  ", lnum);
			draw(toCHAR(lnumstr), 8, 'n', -2L);
		}

		/* if line is too long, then wrap it or clip it */
		if (o_lpcolumns > 0 && column >= o_lpcolumns && ch != '\f' && ch != '\n')
		{
			/* if we're supposed to clip, then just ignore this char */
			if (!o_lpwrap)
			{
				continue;
			}

			/* else insert a newline */
			if (o_lpcrlf)
			{
				(*type->fontch)(font, '\r');
			}
			(*type->fontch)(font, '\n');

			/* that newline has the side-effects of incrementing the line
			 * number and resetting the column number.
			 */
			linesleft--;
			column = 0;
		}

		/* FF is treated specially, below */
		if (ch != '\f')
		{
			/* maybe convert newline to CR-LF */
			if (ch == '\n' && o_lpcrlf)
			{
				(*type->fontch)(font, '\r');
			}

			/* pass the character to the LP driver */
			(*type->fontch)(font, ch);

			/* if the character was newline, then decrement the number of
			 * lines left on this page, and reset the column to 0.
			 */
			if (ch == '\n')
			{
				linesleft--;
				column = 0;
			}
			else /* must have been a normal printable character */
			{
				column++;
				anytext = True;
			}
		}

		if ((ch == '\f' && anytext) || (o_lplines > 0 && linesleft <= 1))
		{
			/* End the line, if it has any characters in it */
			if (column > 0)
			{
				(*type->fontch)('n', '\n');
				column = 0;
				linesleft--;
			}

			/* End this page */
			(*type->page)(linesleft);
			linesleft = o_lplines;

			/* Start the next page by calling the mode's header() function.
			 * This can cause this draw() function to be called recursively.
			 * If the header is larger than the page size, we could
			 * potentially get stuck in a loop.  To avoid this, we will
			 * temporarily set prwin to NULL.
			 */
			pagenum++;
			if (prwin && prwin->md->header)
			{
				win = prwin;
				prwin = NULL;
				(*win->md->header)(win, pagenum, win->mi, draw);
				prwin = win;
			}
			anytext = False;
		}
	}
}

/* This function performs printing. */
RESULT lp(win, top, bottom, force)
	WINDOW	win;	/* window where print request was generated */
	MARK	top;	/* start of text to be printed */
	MARK	bottom;	/* end of text to be printed */
	BOOLEAN	force;	/* allow an existing file to be clobbered? */
{
	long	oldcurs;
	MARK	next;
	CHAR	*origdisplay = NULL;
	char	*out;
	char	rwa;
	
	/* convert the value of o_lptype to an LPTYPE pointer */
	type = findtype(tochar8(o_lptype));
	if (!type)
	{
		msg(MSG_ERROR, "bad lptype");
		return RESULT_ERROR;
	}

	/* Switch to the bufdisplay display mode, if not that way already */
	if (CHARcmp(o_display(win), o_bufdisplay(markbuffer(top))))
	{
		origdisplay = CHARdup(o_display(win));
		if (!dispset(win, tochar8(o_bufdisplay(markbuffer(top)))))
		{
			safefree(origdisplay);
			return RESULT_ERROR;
		}
	}

	/* Call the mode's setup function.  Pretend the cursor is at the
	 * top of the print region, so setup() doesn't try to scroll.
	 */
	next = (*win->md->setup)(win, top, markoffset(top), bottom, win->mi);

	/* open file or filter program */
	if (type->spooled)
	{
		rwa = 'w';
		out = tochar8(o_lpout);
		if (!out || !*out)
		{
			msg(MSG_ERROR, "must set lpout");
			goto Error;
		}
		if (out[0] == '>' && out[1] == '>')
			out += 2, rwa = 'a';
		if (!ioopen(out, rwa, True, force, 'b'))
		{
			goto Error;
		}
	}

	/* allocate the I/O buffer */
	iomax = 1024;
	iobuf = (CHAR *)safealloc(iomax, sizeof(CHAR));
	ionext = 0;

	/* initialize the LP driver */
#if defined (GUI_WIN32)
	if (strcmp (o_lptype, "windows") == 0)
	    (*type->before)(type->minorno, dummyprt);
	else
#endif
		(*type->before)(type->minorno, prtchar);

	/* start the first page */
	pagenum = 1;
	linesleft = o_lplines;
	column = 0;
	prwin = win;
	if (win->md->header)
	{
		(*win->md->header)(win, pagenum, win->mi, draw);
	}
	anytext = False;

	/* generate image lines, and send them to the printer.  Temporarily
	 * move the cursor to the end of the buffer so it doesn't affect
	 * the appearance of the text.
	 */
	oldcurs = markoffset(win->cursor);
	marksetoffset(win->cursor, o_bufchars(markbuffer(win->cursor)));
	while (markoffset(next) < markoffset(bottom))
	{
		lnum = markline(next);
		next = (*win->md->image)(win, next, win->mi, draw);
	}
	marksetoffset(win->cursor, oldcurs);

	/* end the printout */
	(*type->after)(linesleft);

	/* end the I/O */
	if (ionext > 0)
	{
		iowrite(iobuf, ionext);
	}
	safefree(iobuf);
	msg(MSG_INFO, "[d]$1 pages", (long)pagenum);
	if (type->spooled && ioclose())
		goto Error;

	/* clean up and return success */
	if (origdisplay)
	{
		(void)dispset(win, tochar8(origdisplay));
		safefree(origdisplay);
	}
	return RESULT_COMPLETE;

Error:
	/* clean up and return failure */
	if (origdisplay)
	{
		(void)dispset(win, tochar8(origdisplay));
		safefree(origdisplay);
	}
	return RESULT_ERROR;
}
#endif /* FEATURE_LPR */