xref: /openbsd-src/lib/libcurses/tinfo/lib_print.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: lib_print.c,v 1.3 2001/01/22 18:01:53 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 #include <curses.priv.h>
37 
38 #include <term.h>
39 
40 MODULE_ID("$From: lib_print.c,v 1.13 2000/12/10 02:55:07 tom Exp $")
41 
42 NCURSES_EXPORT(int)
43 mcprint(char *data, int len)
44 /* ship binary character data to the printer via mc4/mc5/mc5p */
45 {
46     char *mybuf, *switchon;
47     size_t onsize, offsize, res;
48 
49     errno = 0;
50     if (!cur_term || (!prtr_non && (!prtr_on || !prtr_off))) {
51 	errno = ENODEV;
52 	return (ERR);
53     }
54 
55     if (prtr_non) {
56 	switchon = tparm(prtr_non, len);
57 	onsize = strlen(switchon);
58 	offsize = 0;
59     } else {
60 	switchon = prtr_on;
61 	onsize = strlen(prtr_on);
62 	offsize = strlen(prtr_off);
63     }
64 
65     if ((mybuf = typeMalloc(char, onsize + len + offsize + 1)) == (char *) 0) {
66 	errno = ENOMEM;
67 	return (ERR);
68     }
69 
70     (void) strcpy(mybuf, switchon);
71     memcpy(mybuf + onsize, data, len);
72     if (offsize)
73 	(void) strcpy(mybuf + onsize + len, prtr_off);
74 
75     /*
76      * We're relying on the atomicity of UNIX writes here.  The
77      * danger is that output from a refresh() might get interspersed
78      * with the printer data after the write call returns but before the
79      * data has actually been shipped to the terminal.  If the write(2)
80      * operation is truly atomic we're protected from this.
81      */
82     res = write(cur_term->Filedes, mybuf, onsize + len + offsize);
83 
84     /*
85      * By giving up our scheduler slot here we increase the odds that the
86      * kernel will ship the contiguous clist items from the last write
87      * immediately.
88      */
89     (void) sleep(0);
90 
91     free(mybuf);
92     return (res);
93 }
94