Back to index

tetex-bin  3.0
lib_print.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2000,2002 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 #include <curses.priv.h>
00035 
00036 #include <term.h>
00037 
00038 MODULE_ID("$Id: lib_print.c,v 1.15 2002/08/17 23:41:34 tom Exp $")
00039 
00040 NCURSES_EXPORT(int)
00041 mcprint(char *data, int len)
00042 /* ship binary character data to the printer via mc4/mc5/mc5p */
00043 {
00044     char *mybuf, *switchon;
00045     size_t onsize, offsize, res;
00046 
00047     errno = 0;
00048     if (!cur_term || (!prtr_non && (!prtr_on || !prtr_off))) {
00049        errno = ENODEV;
00050        return (ERR);
00051     }
00052 
00053     if (prtr_non) {
00054        switchon = tparm(prtr_non, len);
00055        onsize = strlen(switchon);
00056        offsize = 0;
00057     } else {
00058        switchon = prtr_on;
00059        onsize = strlen(prtr_on);
00060        offsize = strlen(prtr_off);
00061     }
00062 
00063     if (switchon == 0
00064        || (mybuf = typeMalloc(char, onsize + len + offsize + 1)) == 0) {
00065        errno = ENOMEM;
00066        return (ERR);
00067     }
00068 
00069     (void) strcpy(mybuf, switchon);
00070     memcpy(mybuf + onsize, data, (unsigned) len);
00071     if (offsize)
00072        (void) strcpy(mybuf + onsize + len, prtr_off);
00073 
00074     /*
00075      * We're relying on the atomicity of UNIX writes here.  The
00076      * danger is that output from a refresh() might get interspersed
00077      * with the printer data after the write call returns but before the
00078      * data has actually been shipped to the terminal.  If the write(2)
00079      * operation is truly atomic we're protected from this.
00080      */
00081     res = write(cur_term->Filedes, mybuf, onsize + len + offsize);
00082 
00083     /*
00084      * By giving up our scheduler slot here we increase the odds that the
00085      * kernel will ship the contiguous clist items from the last write
00086      * immediately.
00087      */
00088     (void) sleep(0);
00089 
00090     free(mybuf);
00091     return (res);
00092 }