150276Speter/****************************************************************************
2166124Srafan * Copyright (c) 1998-2002,2006 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
3050276Speter *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3150276Speter *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
3250276Speter ****************************************************************************/
3350276Speter
3450276Speter#include <curses.priv.h>
3550276Speter
3650276Speter#include <term.h>
3750276Speter
38166124SrafanMODULE_ID("$Id: lib_print.c,v 1.16 2006/11/26 00:26:34 tom Exp $")
3950276Speter
4076726SpeterNCURSES_EXPORT(int)
4176726Spetermcprint(char *data, int len)
4250276Speter/* ship binary character data to the printer via mc4/mc5/mc5p */
4350276Speter{
4476726Speter    char *mybuf, *switchon;
4576726Speter    size_t onsize, offsize, res;
4650276Speter
4750276Speter    errno = 0;
4876726Speter    if (!cur_term || (!prtr_non && (!prtr_on || !prtr_off))) {
4950276Speter	errno = ENODEV;
5076726Speter	return (ERR);
5150276Speter    }
5250276Speter
5376726Speter    if (prtr_non) {
54166124Srafan	switchon = TPARM_1(prtr_non, len);
5550276Speter	onsize = strlen(switchon);
5650276Speter	offsize = 0;
5776726Speter    } else {
5850276Speter	switchon = prtr_on;
5950276Speter	onsize = strlen(prtr_on);
6050276Speter	offsize = strlen(prtr_off);
6150276Speter    }
6250276Speter
63166124Srafan    if (switchon == 0
64166124Srafan	|| (mybuf = typeMalloc(char, onsize + len + offsize + 1)) == 0) {
6550276Speter	errno = ENOMEM;
6676726Speter	return (ERR);
6750276Speter    }
6850276Speter
6950276Speter    (void) strcpy(mybuf, switchon);
7097049Speter    memcpy(mybuf + onsize, data, (unsigned) len);
7150276Speter    if (offsize)
7276726Speter	(void) strcpy(mybuf + onsize + len, prtr_off);
7350276Speter
7450276Speter    /*
7550276Speter     * We're relying on the atomicity of UNIX writes here.  The
7650276Speter     * danger is that output from a refresh() might get interspersed
7750276Speter     * with the printer data after the write call returns but before the
7850276Speter     * data has actually been shipped to the terminal.  If the write(2)
7950276Speter     * operation is truly atomic we're protected from this.
8050276Speter     */
8150276Speter    res = write(cur_term->Filedes, mybuf, onsize + len + offsize);
8250276Speter
8350276Speter    /*
8450276Speter     * By giving up our scheduler slot here we increase the odds that the
8550276Speter     * kernel will ship the contiguous clist items from the last write
8650276Speter     * immediately.
8750276Speter     */
8850276Speter    (void) sleep(0);
8950276Speter
9050276Speter    free(mybuf);
9176726Speter    return (res);
9250276Speter}
93