/*1Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.2All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are6met:78- Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.1011- Redistributions in binary form must reproduce the above copyright12notice, this list of conditions and the following disclaimer in13the documentation and/or other materials provided with the14distribution.1516- Neither the name of The Numerical ALgorithms Group Ltd. nor the17names of its contributors may be used to endorse or promote products18derived from this software without specific prior written permission.1920THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER24OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,26PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR27PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF28LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING29NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS30SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233#include "openaxiom-c-macros.h"3435/*36Here is some code taken from Nick Simicich. It takes an escape sequence37from the child, and if I am actually talking to an HFT device, it38translates that escape sequence into an ioctl call.39*/404142#if 04344#include "edible.h"45#include "sys/devinfo.h"46#include <sys/ioctl.h>4748typedef union {49struct hfintro *hf;50struct hfctlreq *re;51char *c;52} Argument;5354emuhft(Argument arg, int tty, int ptc, int len)55{56/* What does it do? */57/* 1. There are a number of ioctl's associated with the HFT terminal. */58/* 2. When an HFT terminal is being emulated over a PTY, the */59/* IOCTL cannot be executed directly on the server end of the PTY. */60/* 3. A system defined structure is set up such that the program */61/* at the end of the PTY can issue the ioctl as an escape */62/* sequence and get its response as an escape sequence. */63/* 4. This is badly broken, even stupid. If the protocol is */64/* defined, and everyone is supposed to use it, then the HFT */65/* should react directly to it. But No.... */66/* 5. Furthermore, our terminal itself might be a pty. In that */67/* case, we have to transmit the data just as we got it to the */68/* other PTY, instead of executing the IOCTL. */6970static union {71struct hfintro hfi;72struct hfctlack ackn;73char charvector[1024]; /* Spacer to make sure that response can be74* moved here */75} aa;7677extern int errno;7879#ifdef DEBUG80dstream(arg.c, stderr, NULL, "From emuhft (input)");81#endif8283if (len > 1000) {84fprintf(stderr, "Unreasonable value for len %d\n", len);85return -1;86}8788if (ioctl(tty, IOCTYPE, 0) != (DD_PSEU << 8)) { /* is it a pty ? */89switch (arg.re->hf_request) {90case HFQUERY:{91struct hfquery hfqur;92int i;9394hfqur.hf_resplen = iiret(arg.re->hf_rsp_len);95if (hfqur.hf_resplen > 0) {96hfqur.hf_resp = aa.charvector + sizeof aa.ackn;97if (hfqur.hf_resplen > (sizeof aa.charvector - sizeof98aa.ackn)) {99errno = ENOMEM;100perror("Can't store HFQUERY response");101return -1;102}103}104else105hfqur.hf_resp = NULL;106107hfqur.hf_cmd = arg.c + 3 + ciret(arg.hf->hf_len);108hfqur.hf_cmdlen = iiret(arg.re->hf_arg_len);109i = ioctl(tty, HFQUERY, &hfqur); /* The meat of the110* matter */111aa.hfi.hf_esc = HFINTROESC;112aa.hfi.hf_lbr = HFINTROLBR;113aa.hfi.hf_ex = HFINTROEX;114icmove(sizeof aa.ackn - 3, aa.hfi.hf_len);115aa.hfi.hf_typehi = HFCTLACKCH;116aa.hfi.hf_typelo = HFCTLACKCL;117if (i == -1)118aa.ackn.hf_retcode = errno;119else120aa.ackn.hf_retcode = 0;121aa.ackn.hf_sublen = arg.re->hf_sublen;122aa.ackn.hf_subtype = arg.re->hf_subtype;123aa.ackn.hf_request = iiret(arg.re->hf_request);124aa.ackn.hf_arg_len = hfqur.hf_resplen;125if (-1 == write(ptc, aa.charvector, (sizeof aa.ackn) +126hfqur.hf_resplen)) {127perror("write of HFQUERY acknowledgement failed");128return (-1);129}130#ifdef DEBUG131dstream(aa.charvector, stderr, NULL, "From emuhft (hfquery ack)");132#endif133break;134}135case HFSKBD:{136struct hfbuf hfkey;137int i;138139hfkey.hf_bufp = arg.c + 3 + ciret(arg.hf->hf_len);140hfkey.hf_buflen = iiret(arg.re->hf_arg_len);141i = ioctl(tty, HFSKBD, &hfkey); /* The meat of the matter */142aa.hfi.hf_esc = HFINTROESC;143aa.hfi.hf_lbr = HFINTROLBR;144aa.hfi.hf_ex = HFINTROEX;145icmove(sizeof aa.ackn - 3, aa.hfi.hf_len);146aa.hfi.hf_typehi = HFCTLACKCH;147aa.hfi.hf_typelo = HFCTLACKCL;148if (i == -1)149aa.ackn.hf_retcode = errno;150else151aa.ackn.hf_retcode = 0;152aa.ackn.hf_sublen = arg.re->hf_sublen;153aa.ackn.hf_subtype = arg.re->hf_subtype;154aa.ackn.hf_request = iiret(arg.re->hf_request);155aa.ackn.hf_arg_len = 0;156if (-1 == write(ptc, aa.charvector, sizeof aa.ackn)) {157perror("write of HFSKEY acknowledgement failed");158return (-1);159}160#ifdef DEBUG161dstream(aa.charvector, stderr, NULL, "From emuhft (HFSKEY ack)");162#endif163break;164}165default:{166aa.hfi.hf_esc = HFINTROESC;167aa.hfi.hf_lbr = HFINTROLBR;168aa.hfi.hf_ex = HFINTROEX;169icmove(sizeof aa.ackn - 3, aa.hfi.hf_len);170aa.hfi.hf_typehi = HFCTLACKCH;171aa.hfi.hf_typelo = HFCTLACKCL;172aa.ackn.hf_retcode = EINVAL;173aa.ackn.hf_sublen = arg.re->hf_sublen;174aa.ackn.hf_subtype = arg.re->hf_subtype;175aa.ackn.hf_request = iiret(arg.re->hf_request);176aa.ackn.hf_arg_len = 0;177if (-1 == write(ptc, aa.charvector, sizeof aa.ackn)) {178perror("write of default acknowledgement failed");179return (-1);180}181#ifdef DEBUG182dstream(aa.charvector, stderr, NULL, "From emuhft (default ack)");183#endif184185break;186}187}188}189else {190/* Well, if we get here, we are a pseudo-device ourselves. So */191/* we will just send on the request that we got. we are in a */192/* unique situation. We believe that both ptc and tty are as */193/* transparent as we can get them, so we don't have to worry. */194/* We will just write the request to the tty, which we */195/* believe is a pty, and sooner or later, the ack will come */196/* back. */197if (-1 == write(tty, arg.c, len)) {198perror("write of control sequence to pty failed");199fprintf(stderr, "tty = %d, len = %d\n", tty, len);200return (-1);201}202#ifdef DEBUG203dstream(arg.c, stderr, NULL, "From emuhft (on pty transfer)");204fprintf(stderr, "tty = %d, len = %d\r\n", tty, len);205fflush(stderr);206#endif207208}209return 0;210}211212213214#endif215216static int _ThatsAll_(int x)217{218return x;219}220221222