blob: e7d8535ef603064326e4f1b5c2b77990b8ddb6de [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2021,2023 Thomas E. Dickey *
3 * Copyright 1998-2011,2012 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
Steve Kondikae271bc2015-11-15 02:50:53 +010033 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053035 ****************************************************************************/
36
37#include <curses.priv.h>
38
Steve Kondikae271bc2015-11-15 02:50:53 +010039#ifndef CUR
40#define CUR SP_TERMTYPE
41#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053042
micky3879b9f5e72025-07-08 18:04:53 -040043MODULE_ID("$Id: lib_print.c,v 1.31 2023/06/10 12:44:41 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053044
45NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010046NCURSES_SP_NAME(mcprint) (NCURSES_SP_DCLx char *data, int len)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047/* ship binary character data to the printer via mc4/mc5/mc5p */
48{
Steve Kondikae271bc2015-11-15 02:50:53 +010049 int result;
micky3879b9f5e72025-07-08 18:04:53 -040050 char *mybuf = NULL, *switchon;
Steve Kondikae271bc2015-11-15 02:50:53 +010051 size_t onsize, offsize;
52 size_t need;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053053
54 errno = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +010055 if (!HasTInfoTerminal(SP_PARM)
56 || len <= 0
57 || (!prtr_non && (!prtr_on || !prtr_off))) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053058 errno = ENODEV;
59 return (ERR);
60 }
61
62 if (prtr_non) {
micky3879b9f5e72025-07-08 18:04:53 -040063 switchon = TIPARM_1(prtr_non, len);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053064 onsize = strlen(switchon);
65 offsize = 0;
66 } else {
67 switchon = prtr_on;
68 onsize = strlen(prtr_on);
69 offsize = strlen(prtr_off);
70 }
71
Steve Kondikae271bc2015-11-15 02:50:53 +010072 need = onsize + (size_t) len + offsize;
73
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053074 if (switchon == 0
Steve Kondikae271bc2015-11-15 02:50:53 +010075 || (mybuf = typeMalloc(char, need + 1)) == 0) {
micky3879b9f5e72025-07-08 18:04:53 -040076 free(mybuf);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053077 errno = ENOMEM;
78 return (ERR);
79 }
80
Steve Kondikae271bc2015-11-15 02:50:53 +010081 _nc_STRCPY(mybuf, switchon, need);
82 memcpy(mybuf + onsize, data, (size_t) len);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053083 if (offsize)
Steve Kondikae271bc2015-11-15 02:50:53 +010084 _nc_STRCPY(mybuf + onsize + len, prtr_off, need);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053085
86 /*
87 * We're relying on the atomicity of UNIX writes here. The
88 * danger is that output from a refresh() might get interspersed
89 * with the printer data after the write call returns but before the
90 * data has actually been shipped to the terminal. If the write(2)
91 * operation is truly atomic we're protected from this.
92 */
micky3879b9f5e72025-07-08 18:04:53 -040093 result = (int) write(SP_PARM->_ofd, mybuf, need);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053094
95 /*
96 * By giving up our scheduler slot here we increase the odds that the
97 * kernel will ship the contiguous clist items from the last write
98 * immediately.
99 */
micky3879b9f5e72025-07-08 18:04:53 -0400100#ifndef _NC_WINDOWS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530101 (void) sleep(0);
Steve Kondikae271bc2015-11-15 02:50:53 +0100102#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530103 free(mybuf);
Steve Kondikae271bc2015-11-15 02:50:53 +0100104 return (result);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530105}
Steve Kondikae271bc2015-11-15 02:50:53 +0100106
107#if NCURSES_SP_FUNCS && !defined(USE_TERM_DRIVER)
108NCURSES_EXPORT(int)
109mcprint(char *data, int len)
110{
111 return NCURSES_SP_NAME(mcprint) (CURRENT_SCREEN, data, len);
112}
113#endif