This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Trouble Sending Printer Codes from Perl to Printer


I'm adapting a working linux program to cygwin. The process has been relatively painless except for this issue which has delayed me way past deadline.

I need to output text and printer codes (which may include \000) from a perl program to a printer which may be on a parallel port or a USB port. In the past I have done this on linux using something like the following.

    system(qq/echo -en "$data_str" | lpr -oraw/);
    ...or whatever

That does not work in cygwin. It would work. But on cygwin echo behaves differently in a perl system() command from the way it works from the bash command line.

Attempting to locate the problem, I've done some experimenting (illustrations are given below). The problem appears to be with the material before the pipe in the code line above. Experimenting with text and selective codes indicates that piping to lpr works fine (even though cygwin lpr does not recognize (or, apparently, need) the -oraw switch).

So my focus is on getting the 'echo -switches text' part to work.

Before I get to the extended illustration of the problem, let me say I am open to some other method of accomplishing my purpose. I'd try sending the codes and data directly to the port in question. But IIRC on Linux, squirting data out a USB port was very problematic. That's why I ended up piping to lpr. If you suggest directly addressing the (PAR or USB) printer port, please offer a working snippet.

Back to the challenge at hand. Here are some illustrative attempts with interpolated comments...

<BASH TRANSCRIPT>
$ echo -en "hello\nworld"
hello
world
dvergin@GatewayM275 /c/bin
$     #Good! The basic approach works on the command line.
      #Control codes are interpreted and no trailing newline.
      #So I'll try the same thing from perl letting the echo
      # command convert the control char.
      #   (Note different approach farther down)

$ perl -e 'system(q/echo -en "hello\nworld"/)'
-en hello\nworld

dvergin@GatewayM275 /c/bin
$     #Ack! The -en option is treated as text to be echoed.
      #However...

$ perl -e 'system(q/echo -e "hello\nworld"/)'
hello
world

dvergin@GatewayM275 /c/bin
$     #...works. But I still have a trailing newline.
      #If I have to choose, how about...

$ perl -e 'system(q/echo -n "hello\nworld"/)'
hello\nworld
dvergin@GatewayM275 /c/bin
$     #Hmmph. The -n alone works to suppress trailing newline.
      #Maybe I can separate the -e and -n.

$ perl -e 'system(q/echo -e -n "hello\nworld"/)'
-n hello
world

dvergin@GatewayM275 /c/bin
$     #No joy. Only the first switch is recognized.
      #How about if I convert to control codes ^in perl^
      # and give ^that^ to the echo command.
      #  ( Note the switch from q// to qq// ).

$ perl -e 'system(qq/echo -n "hello\nworld"/)'
hello
world
dvergin@GatewayM275 /c/bin
$     #This looks promising. I'm using only the -n and it works.
      #The \n is being converted in perl and the echo handles it fine.
      #What about other codes...

$ perl -e 'system(qq/echo -n "hello\001\nworld"/)'
hello<smiley>
world
dvergin@GatewayM275 /c/bin
$     #Yes!! Just one more step. The dreaded \000.

$ perl -e 'system(qq/echo -n "hello\000\nworld"/)'
Syntax error: Unterminated quoted string

dvergin@GatewayM275 /c/bin
$     #No good. Maybe I can escape the \000 char.

$ perl -e 'system(qq/echo -n "hello\\\000\nworld"/)'
Syntax error: Unterminated quoted string

</BASH TRANSCRIPT>

So as far as I can tell I'm stuck. The mid-stream \000 char seems to be treated by bash as a string terminator and is immune to escaping with '\'.

I'm about ready to put the needed printer codes into little files and cat them to lpr. But that's soooo kludgy.

Any help appreciated. Here are the criteria again:

    Send text and printer codes (including \000) with
    no trailing newline from perl program to printer
    which may be on either a parallel or a USB port.

Seems like the sort of thing one ought to be able to do. <grin>

TIA
David


-- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]