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