1 /* $OpenBSD: lib_print.c,v 1.5 2010/01/12 23:22:06 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998-2002,2006 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("$Id: lib_print.c,v 1.5 2010/01/12 23:22:06 nicm 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_1(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 res = onsize + len + offsize + 1; 66 if (switchon == 0 || (mybuf = typeMalloc(char, res)) == 0) { 67 errno = ENOMEM; 68 return (ERR); 69 } 70 71 (void) strlcpy(mybuf, switchon, res); 72 memcpy(mybuf + onsize, data, (unsigned) len); 73 if (offsize) 74 (void) strlcpy(mybuf + onsize + len, prtr_off, res - onsize - len); 75 76 /* 77 * We're relying on the atomicity of UNIX writes here. The 78 * danger is that output from a refresh() might get interspersed 79 * with the printer data after the write call returns but before the 80 * data has actually been shipped to the terminal. If the write(2) 81 * operation is truly atomic we're protected from this. 82 */ 83 res = write(cur_term->Filedes, mybuf, onsize + len + offsize); 84 85 /* 86 * By giving up our scheduler slot here we increase the odds that the 87 * kernel will ship the contiguous clist items from the last write 88 * immediately. 89 */ 90 (void) sleep(0); 91 92 free(mybuf); 93 return (res); 94 } 95