ckucns.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720
  1. #include "ckcsym.h"
  2. char *connv = "CONNECT Command for UNIX:select(), 9.0.139, 1 Mar 2010";
  3. /* C K U C N S -- Terminal connection to remote system, for UNIX */
  4. /*
  5. Author: Frank da Cruz <fdc@columbia.edu>,
  6. Columbia University Academic Information Systems, New York City.
  7. Copyright (C) 1985, 2010,
  8. Trustees of Columbia University in the City of New York.
  9. All rights reserved. See the C-Kermit COPYING.TXT file or the
  10. copyright text in the ckcmai.c module for disclaimer and permissions.
  11. */
  12. /*
  13. This version of the UNIX CONNECT module uses select(), which is required for
  14. Kerberos encryption. Thus it can be used only on UNIX systems that support
  15. select() on both TCP/IP and serial connections. A separate module that uses
  16. a completely portable fork() structure can be used on systems where select()
  17. is not available or does not work as required.
  18. */
  19. #include "ckcdeb.h" /* Common things first */
  20. #ifndef NOLOCAL
  21. #ifdef OSF13
  22. #ifdef CK_ANSIC
  23. #ifdef _NO_PROTO
  24. #undef _NO_PROTO
  25. #endif /* _NO_PROTO */
  26. #endif /* CK_ANSIC */
  27. #endif /* OSF13 */
  28. #include <errno.h> /* Error numbers */
  29. #ifndef NOTIMEH
  30. #include <time.h> /* For FD_blah */
  31. #ifdef SYSTIMEH /* (IRIX 5.3) */
  32. #include <sys/time.h>
  33. #endif /* SYSTIMEH */
  34. #endif /* NOTIMEH */
  35. #ifdef BSD42HACK /* Why is this necessary? */
  36. #ifndef DCLTIMEVAL
  37. #define DCLTIMEVAL
  38. #endif /* DCLTIMEVAL */
  39. #endif /* BSD42HACK */
  40. /* Kermit-specific includes */
  41. #include "ckcasc.h" /* ASCII characters */
  42. #include "ckcker.h" /* Kermit things */
  43. #include "ckucmd.h" /* For xxesc() prototype */
  44. #include "ckcnet.h" /* Network symbols */
  45. #ifndef NOCSETS
  46. #include "ckcxla.h" /* Character set translation */
  47. #endif /* NOCSETS */
  48. #ifdef BEBOX
  49. #include <kernel/OS.h>
  50. #include <socket.h>
  51. #include <stdio.h>
  52. #endif /* BEBOX */
  53. #include <signal.h> /* Signals */
  54. /* All the following is for select()... */
  55. #ifdef CKTIDLE /* Timeouts only for SET TERM IDLE */
  56. #ifndef DCLTIMEVAL
  57. #ifdef UNIXWARE
  58. #ifndef UW7
  59. #define DCLTIMEVAL
  60. #endif /* UW7 */
  61. #endif /* UNIXWARE */
  62. #endif /* DCLTIMEVAL */
  63. #ifdef DCLTIMEVAL /* Declare timeval ourselves */
  64. struct timeval {
  65. long tv_sec;
  66. long tv_usec;
  67. };
  68. #else /* !DCLTIMEVAL */
  69. #ifndef NOSYSTIMEBH
  70. #ifdef SYSTIMEBH
  71. #include <sys/timeb.h>
  72. #endif /* SYSTIMEBH */
  73. #endif /* NOSYSTIMEBH */
  74. #endif /* DCLTIMEVAL */
  75. #endif /* CKTIDLE */
  76. #ifndef SCO_OSR504
  77. #ifdef SELECT_H
  78. #include <sys/select.h>
  79. #endif /* SELECT_H */
  80. #endif /* SCO_OSR504 */
  81. #ifndef FD_SETSIZE
  82. #ifdef CK_FORWARD_X
  83. #define FD_SETSIZE 256
  84. #else
  85. #define FD_SETSIZE 32
  86. #endif /* CK_FORWARD_X */
  87. #endif /* FD_SETSIZE */
  88. #ifdef HPUX
  89. #ifndef HPUX10
  90. #ifndef HPUX1100
  91. /* The three interior args to select() are (int *) rather than (fd_set *) */
  92. #ifndef INTSELECT
  93. #define INTSELECT
  94. #endif /* INTSELECT */
  95. #endif /* HPUX1100 */
  96. #endif /* HPUX10 */
  97. #endif /* HPUX */
  98. /* Internal function prototypes */
  99. #ifdef NEWFTP
  100. #endif /* NEWFTP */
  101. _PROTOTYP( VOID ttflux, (void) );
  102. _PROTOTYP( VOID doesc, (char) );
  103. _PROTOTYP( int hconne, (void) );
  104. #ifndef NOSHOW
  105. _PROTOTYP( VOID shomdm, (void) );
  106. #endif /* NOSHOW */
  107. _PROTOTYP( static int kbget, (void) );
  108. _PROTOTYP( static int ckcputf, (void) );
  109. /* External variables */
  110. extern struct ck_p ptab[];
  111. extern int local, escape, duplex, parity, flow, seslog, sessft, debses,
  112. mdmtyp, ttnproto, cmask, cmdmsk, network, nettype, sosi, tnlm,
  113. xitsta, what, ttyfd, ttpipe, quiet, backgrd, pflag, tt_crd, tt_lfd,
  114. tn_nlm, ttfdflg,
  115. tt_escape, justone, carrier, ttpty, hwparity;
  116. #ifndef NODIAL
  117. extern int dialmhu, dialsta;
  118. #endif /* NODIAL */
  119. #ifdef CKLEARN
  120. extern FILE * learnfp;
  121. extern int learning;
  122. static ULONG learnt1;
  123. static char learnbuf[LEARNBUFSIZ] = { NUL, NUL };
  124. static int learnbc = 0;
  125. static int learnbp = 0;
  126. static int learnst = 0;
  127. #endif /* CKLEARN */
  128. extern long speed;
  129. extern char ttname[], sesfil[], myhost[], *ccntab[];
  130. #ifdef TNCODE
  131. extern int tn_b_nlm, tn_rem_echo;
  132. #endif /* TNCODE */
  133. #ifdef CK_TRIGGER
  134. extern char * tt_trigger[], * triggerval;
  135. #endif /* CK_TRIGGER */
  136. #ifdef CKTIDLE
  137. extern int tt_idlelimit, tt_idleact;
  138. extern char * tt_idlestr;
  139. static int idlelimit = 0;
  140. #endif /* CKTIDLE */
  141. extern int cx_status; /* CONNECT status code */
  142. extern int nopush;
  143. #ifdef CK_APC
  144. extern int apcactive; /* Application Program Command (APC) */
  145. extern int apcstatus; /* items ... */
  146. static int apclength = 0;
  147. #ifdef DCMDBUF
  148. extern char *apcbuf;
  149. #else
  150. extern char apcbuf[];
  151. #endif /* DCMDBUF */
  152. static int apcbuflen = APCBUFLEN - 2;
  153. extern int protocol;
  154. #endif /* CK_APC */
  155. #ifndef NOXFER
  156. extern int autodl; /* Auto download */
  157. #endif /* NOXFER */
  158. #ifdef CK_AUTODL
  159. extern CHAR ksbuf[];
  160. extern CHAR stchr;
  161. extern int kstartactive;
  162. #endif /* CK_AUTODL */
  163. #ifdef CK_ENCRYPTION
  164. extern int me_auth;
  165. #endif /* CK_ENCRYPTION */
  166. #ifdef CK_XYZ
  167. #ifdef XYZ_INTERNAL
  168. static int zmdlok = 1; /* Zmodem autodownloads available */
  169. #else
  170. static int zmdlok = 0; /* Depends on external protocol def */
  171. #endif /* XYZ_INTERNAL */
  172. #else
  173. static int zmdlok = 0; /* Not available at all */
  174. #endif /* CK_XYZ */
  175. #ifndef NOSETKEY /* Keyboard mapping */
  176. extern KEY *keymap; /* Single-character key map */
  177. extern MACRO *macrotab; /* Key macro pointer table */
  178. static MACRO kmptr = NULL; /* Pointer to current key macro */
  179. #endif /* NOSETKEY */
  180. /* Global variables local to this module */
  181. static int
  182. active = 0,
  183. quitnow = 0, /* <esc-char>Q was typed */
  184. dohangup = 0, /* <esc-char>H was typed */
  185. inshift = 0, /* SO/SI shift states */
  186. outshift = 0;
  187. static char ecbuf[10], *ecbp; /* Escape char buffer & pointer */
  188. #ifdef CK_SMALL
  189. #define IBUFL 1536 /* Input buffer length */
  190. #else
  191. #define IBUFL 4096
  192. #endif /* CK_SMALL */
  193. static int obc = 0; /* Output buffer count */
  194. #ifndef OXOS
  195. #define OBUFL 1024 /* Output buffer length */
  196. #else
  197. #define OBUFL IBUFL
  198. #endif /* OXOS */
  199. #ifdef BIGBUFOK
  200. #define TMPLEN 4096 /* Temporary message buffer length */
  201. #else
  202. #define TMPLEN 200
  203. #endif /* BIGBUFOK */
  204. #ifdef DYNAMIC
  205. static char *ibuf = NULL, *obuf = NULL, *temp = NULL; /* Buffers */
  206. #else
  207. static char ibuf[IBUFL], obuf[OBUFL], temp[TMPLEN];
  208. #endif /* DYNAMIC */
  209. #ifdef TNCODE
  210. static char tnopt[4];
  211. #endif /* TNCODE */
  212. #ifdef DYNAMIC
  213. static char *ibp; /* Input buffer pointer */
  214. #else
  215. static char *ibp = ibuf; /* Input buffer pointer */
  216. #endif /*DYNAMIC */
  217. static int ibc = 0; /* Input buffer count */
  218. #ifdef DYNAMIC
  219. static char *obp; /* Output buffer pointer */
  220. #else
  221. static char *obp = obuf; /* Output buffer pointer */
  222. #endif /* DYNAMIC */
  223. /* Character-set items */
  224. static int unicode = 0;
  225. #ifndef NOCSETS
  226. #ifdef CK_ANSIC /* ANSI C prototypes... */
  227. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  228. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  229. static CHAR (*sxo)(CHAR); /* Local translation functions */
  230. static CHAR (*rxo)(CHAR); /* for output (sending) terminal chars */
  231. static CHAR (*sxi)(CHAR); /* and for input (receiving) terminal chars. */
  232. static CHAR (*rxi)(CHAR);
  233. #else /* Not ANSI C... */
  234. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(); /* Character set */
  235. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(); /* translation functions. */
  236. static CHAR (*sxo)(); /* Local translation functions */
  237. static CHAR (*rxo)(); /* for output (sending) terminal chars */
  238. static CHAR (*sxi)(); /* and for input (receiving) terminal chars. */
  239. static CHAR (*rxi)();
  240. #endif /* CK_ANSIC */
  241. extern int language; /* Current language. */
  242. static int langsv; /* For remembering language setting. */
  243. extern struct csinfo fcsinfo[]; /* File character set info. */
  244. extern int tcsr, tcsl; /* Terminal character sets, remote & local. */
  245. static int tcs; /* Intermediate ("transfer") character set. */
  246. static int tcssize = 0; /* Size of tcs */
  247. #ifdef UNICODE /* UTF-8 support */
  248. #ifdef CK_ANSIC
  249. extern int (*xl_ufc[MAXFCSETS+1])(USHORT); /* Unicode to FCS */
  250. extern USHORT (*xl_fcu[MAXFCSETS+1])(CHAR); /* FCS to Unicode */
  251. extern int (*xuf)(USHORT); /* Translation function UCS to FCS */
  252. extern USHORT (*xfu)(CHAR); /* Translation function FCS to UCS */
  253. #else
  254. extern int (*xl_ufc[MAXFCSETS+1])();
  255. extern USHORT (*xl_fcu[MAXFCSETS+1])();
  256. extern int (*xuf)();
  257. extern USHORT (*xfu)();
  258. #endif /* CK_ANSIC */
  259. #endif /* UNICODE */
  260. #endif /* NOCSETS */
  261. static int printing = 0;
  262. /*
  263. We do not need to parse and recognize escape sequences if we are being built
  264. without character-set support AND without APC support.
  265. */
  266. #ifdef NOESCSEQ
  267. #ifdef XPRINT
  268. #undef XPRINT
  269. #endif /* XPRINT */
  270. #else /* NOESCSEQ not defined from outside */
  271. #ifdef NOCSETS /* No character sets */
  272. #ifndef CK_APC /* No APC */
  273. #ifndef XPRINT /* No transparent printing */
  274. #define NOESCSEQ /* So no escape sequence recognizer */
  275. #endif /* XPRINT */
  276. #endif /* CK_APC */
  277. #endif /* NOCSETS */
  278. #endif /* NOESCSEQ */
  279. /* inesc[] and oldesc[] made global 2010/03/01 for INPUT command */
  280. static int escseq = 0; /* 1 = Recognizer is active */
  281. /* static */ int inesc[2] = { 0, 0 }; /* State of sequence recognizer */
  282. /* static */ int oldesc[2] = { -1, -1 }; /* Previous state of recognizer */
  283. #ifdef NOESCSEQ
  284. #define ES_NORMAL 0 /* Normal, not in an escape sequence */
  285. #define chkaes(x,y) 0
  286. #else
  287. /*
  288. As of C-Kermit 5A(178), the CONNECT command skips past ANSI escape sequences
  289. to avoid translating the characters within them. This allows the CONNECT
  290. command to work correctly with a host that uses a 7-bit ISO 646 national
  291. character set, in which characters like '[' would normally be converted to
  292. accented letters, ruining the terminal's interpretation (and generation)
  293. of escape sequences.
  294. As of 5A(190), the CONNECT command responds to APC escape sequences
  295. (ESC _ text ESC \) if the user SETs TERMINAL APC ON or UNCHECKED, and the
  296. program was built with CK_APC defined.
  297. Non-ANSI/ISO-compliant escape sequences are not handled. */
  298. /* States for the escape-sequence recognizer. */
  299. #define ES_NORMAL 0 /* Normal, not in an escape sequence */
  300. #define ES_GOTESC 1 /* Current character is ESC */
  301. #define ES_ESCSEQ 2 /* Inside an escape sequence */
  302. #define ES_GOTCSI 3 /* Inside a control sequence */
  303. #define ES_STRING 4 /* Inside DCS,OSC,PM, or APC string */
  304. #define ES_TERMIN 5 /* 1st char of string terminator */
  305. /*
  306. ANSI escape sequence handling. Only the 7-bit form is treated, because
  307. translation is not a problem in the 8-bit environment, in which all GL
  308. characters are ASCII and no translation takes place. So we don't check
  309. for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.
  310. Here is the ANSI sequence recognizer state table, followed by the code
  311. that implements it.
  312. Definitions:
  313. CAN = Cancel 01/08 Ctrl-X
  314. SUB = Substitute 01/10 Ctrl-Z
  315. DCS = Device Control Sequence 01/11 05/00 ESC P
  316. CSI = Control Sequence Introducer 01/11 05/11 ESC [
  317. ST = String Terminator 01/11 05/12 ESC \
  318. OSC = Operating System Command 01/11 05/13 ESC ]
  319. PM = Privacy Message 01/11 05/14 ESC ^
  320. APC = Application Program Command 01/11 05/15 ESC _
  321. ANSI escape sequence recognizer:
  322. State Input New State ; Commentary
  323. NORMAL (start) ; Start in NORMAL state
  324. (any) CAN NORMAL ; ^X cancels
  325. (any) SUB NORMAL ; ^Z cancels
  326. NORMAL ESC GOTESC ; Begin escape sequence
  327. NORMAL other ; NORMAL control or graphic character
  328. GOTESC ESC ; Start again
  329. GOTESC [ GOTCSI ; CSI
  330. GOTESC P STRING ; DCS introducer, consume through ST
  331. GOTESC ] STRING ; OSC introducer, consume through ST
  332. GOTESC ^ STRING ; PM introducer, consume through ST
  333. GOTESC _ STRING ; APC introducer, consume through ST
  334. GOTESC 0..~ NORMAL ; 03/00 through 17/14 = Final character
  335. GOTESC other ESCSEQ ; Intermediate or ignored control character
  336. ESCSEQ ESC GOTESC ; Start again
  337. ESCSEQ 0..~ NORMAL ; 03/00 through 17/14 = Final character
  338. ESCSEQ other ; Intermediate or ignored control character
  339. GOTCSI ESC GOTESC ; Start again
  340. GOTCSI @..~ NORMAL ; 04/00 through 17/14 = Final character
  341. GOTCSI other ; Intermediate char or ignored control char
  342. STRING ESC TERMIN ; Maybe have ST
  343. STRING other ; Consume all else
  344. TERMIN \ NORMAL ; End of string
  345. TERMIN other STRING ; Still in string
  346. */
  347. #ifdef XPRINT /* Transparent print support */
  348. /*
  349. We can't just print each byte as it comes in because then the printer-off
  350. sequence would be sent to the printer. Thus we have to buffer up escape
  351. sequences and print them only when they are complete AND we know they are
  352. not the printer-off sequence. All printing is done via zsoutx(ZMFILE,s,n).
  353. This allows for strings that contain NULs. Don't mix calls to zsoutx() with
  354. calls to zchout(), or the output will be scrambled. Also note that when
  355. printing a saved-up escape sequence, we never print its final character
  356. because that will be printed in the mainline code, upon return from
  357. chkaes(). Note that the printer-on sequence is passed to the screen; this
  358. is unavoidable, since we don't know what it is until after we get to the
  359. end, and for screen display purposes we can't buffer up escape sequences
  360. for numerous reasons. Therefore we also must output the printer-off
  361. sequence, otherwise a real terminal or emulator will be stuck in print mode.
  362. */
  363. extern int tt_print;
  364. #define ESCBUFLEN 63
  365. static char escbuf[ESCBUFLEN+1] = { NUL, NUL };
  366. static int escbufc = 0;
  367. static int dontprint = 0;
  368. VOID
  369. printon() { /* Turn printing on */
  370. int x, pp;
  371. char * p;
  372. extern int printpipe, noprinter;
  373. extern char * printername;
  374. if (noprinter) {
  375. debug(F110,"PRINTER ON NOPRINTER","",0);
  376. return;
  377. }
  378. p = printername;
  379. pp = printpipe;
  380. if (!p) p = "";
  381. if (!*p) {
  382. #ifdef ANYBSD
  383. p = "lpr";
  384. #else
  385. p = "lp";
  386. #endif /* ANYBSD */
  387. pp = 1;
  388. debug(F110,"PRINTER DEFAULT",p,0);
  389. }
  390. debug(F111,"PRINTER ON",p,pp);
  391. if (pp) { /* Printing to pipe */
  392. x = zxcmd(ZMFILE,p);
  393. } else { /* Append to file */
  394. struct filinfo xx;
  395. xx.bs = 0; xx.cs = 0; xx.rl = 0; xx.org = 0; xx.cc = 0;
  396. xx.typ = 0; xx.dsp = XYFZ_A; xx.os_specific = NUL;
  397. xx.lblopts = 0;
  398. x = zopeno(ZMFILE,p,NULL,&xx);
  399. }
  400. debug(F101,"PRINTER OPEN","",x);
  401. printing = 1;
  402. }
  403. VOID
  404. printoff() { /* Turn printing off */
  405. int x;
  406. extern int noprinter;
  407. if (noprinter) {
  408. printing = 0;
  409. debug(F100,"PRINTER OFF NOPRINTER","",0);
  410. return;
  411. }
  412. debug(F100,"PRINTER OFF","",0);
  413. if (printing) {
  414. x = zclose(ZMFILE);
  415. debug(F101,"PRINTER CLOSE","",x);
  416. printing = 0;
  417. }
  418. }
  419. #endif /* XPRINT */
  420. /*
  421. C H K A E S -- Check ANSI Escape Sequence.
  422. Call with EACH character in input stream.
  423. src = 0 means c is incoming from remote; 1 = char from keyboard.
  424. Sets global inesc[src] variable according to escape sequence state.
  425. Returns 0 normally, 1 if an APC sequence is to be executed.
  426. Handles transparent printing internally.
  427. */
  428. int
  429. #ifdef CK_ANSIC
  430. chkaes(char c, int src)
  431. #else
  432. chkaes(c,src) char c; int src;
  433. #endif /* CK_ANSIC */
  434. /* chkaes */ {
  435. debug(F111,"chkaes entry inesc",ckitoa(src),inesc[src]);
  436. debug(F101,"chkaes c","",c);
  437. if (src < 0 || src > 1) /* Don't allow bad args. */
  438. return(0);
  439. oldesc[src] = inesc[src]; /* Remember previous state */
  440. #ifdef XPRINT
  441. if (inesc[src] && !src) { /* Save up escape seq for printing */
  442. if (!c) return(0); /* Ignore NULs */
  443. if (escbufc < ESCBUFLEN) {
  444. escbuf[escbufc++] = c;
  445. escbuf[escbufc] = NUL;
  446. debug(F111,"ESCBUF 1",escbuf,escbufc);
  447. } else { /* Buffer overrun */
  448. if (printing && escbufc) /* Print what's there so far */
  449. zsoutx(ZMFILE,escbuf,escbufc);
  450. escbufc = 1; /* clear it out */
  451. escbuf[0] = c; /* and start off fresh buffer */
  452. escbuf[1] = NUL; /* with this character. */
  453. }
  454. }
  455. #endif /* XPRINT */
  456. if (c == CAN || c == SUB) { /* CAN and SUB cancel any sequence */
  457. #ifdef XPRINT
  458. if (!src) {
  459. if (printing && escbufc > 1)
  460. zsoutx(ZMFILE,escbuf,escbufc-1);
  461. escbufc = 0; /* Clear buffer */
  462. escbuf[0] = NUL;
  463. }
  464. #endif /* XPRINT */
  465. inesc[src] = ES_NORMAL;
  466. } else /* Otherwise */
  467. switch (inesc[src]) { /* enter state switcher */
  468. case ES_NORMAL: /* NORMAL state */
  469. if (c == ESC) { /* Got an ESC */
  470. inesc[src] = ES_GOTESC; /* Change state to GOTESC */
  471. #ifdef XPRINT
  472. if (!src) {
  473. escbufc = 1; /* Clear escape sequence buffer */
  474. escbuf[0] = c; /* and deposit the ESC */
  475. escbuf[1] = NUL;
  476. debug(F111,"ESCBUF 2",escbuf,escbufc);
  477. }
  478. #endif /* XPRINT */
  479. }
  480. break; /* Otherwise stay in NORMAL state */
  481. case ES_GOTESC: /* GOTESC state - prev char was ESC*/
  482. if (c == '[') { /* Left bracket after ESC is CSI */
  483. inesc[src] = ES_GOTCSI; /* Change to GOTCSI state */
  484. } else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, ], ^, or _ */
  485. inesc[src] = ES_STRING; /* Switch to STRING-absorption state */
  486. #ifdef XPRINT
  487. debug(F111,"ESCBUF STRING",escbuf,escbufc);
  488. #endif /* XPRINT */
  489. #ifdef CK_APC
  490. /* If APC not disabled */
  491. if (!src && c == '_' && (apcstatus & APC_ON)) {
  492. debug(F100,"CONNECT APC begin","",0);
  493. apcactive = APC_REMOTE; /* Set APC-Active flag */
  494. apclength = 0; /* and reset APC buffer pointer */
  495. }
  496. #endif /* CK_APC */
  497. } else if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
  498. inesc[src] = ES_NORMAL; /* Back to normal */
  499. #ifdef XPRINT
  500. if (!src) {
  501. if (printing && escbufc > 1) {
  502. /* Dump esc seq buf to printer */
  503. zsoutx(ZMFILE,escbuf,escbufc-1);
  504. debug(F111,"ESCBUF PRINT 1",escbuf,escbufc);
  505. }
  506. escbufc = 0; /* Clear parameter buffer */
  507. escbuf[0] = NUL;
  508. }
  509. #endif /* XPRINT */
  510. } else if (c != ESC) { /* ESC in an escape sequence... */
  511. inesc[src] = ES_ESCSEQ; /* starts a new escape sequence */
  512. }
  513. break; /* Intermediate or ignored ctrl char */
  514. case ES_ESCSEQ: /* ESCSEQ -- in an escape sequence */
  515. if (c > 057 && c < 0177) { /* Final character '0' thru '~' */
  516. inesc[src] = ES_NORMAL; /* Return to NORMAL state. */
  517. #ifdef XPRINT
  518. if (!src) {
  519. if (printing && escbufc > 1) {
  520. zsoutx(ZMFILE,escbuf,escbufc-1);
  521. debug(F111,"ESCBUF PRINT 2",escbuf,escbufc);
  522. }
  523. escbufc = 0; /* Clear escseq buffer */
  524. escbuf[0] = NUL;
  525. }
  526. #endif /* XPRINT */
  527. } else if (c == ESC) { /* ESC ... */
  528. inesc[src] = ES_GOTESC; /* starts a new escape sequence */
  529. }
  530. break; /* Intermediate or ignored ctrl char */
  531. case ES_GOTCSI: /* GOTCSI -- In a control sequence */
  532. if (c > 077 && c < 0177) { /* Final character '@' thru '~' */
  533. #ifdef XPRINT
  534. if (!src && tt_print) { /* Printer enabled? */
  535. if (c == 'i') { /* Final char is "i"? */
  536. char * p = (char *) (escbuf + escbufc - 4);
  537. if (!strncmp(p, "\033[5i", 4)) { /* Turn printer on */
  538. printon();
  539. } else if (!strncmp(p, "\033[4i", 4)) { /* Or off... */
  540. int i;
  541. printoff(); /* Turn off printer. */
  542. dontprint = 1;
  543. for (i = 0; i < escbufc; i++) /* And output the */
  544. ckcputc(escbuf[i]); /* sequence. */
  545. } else if (printing && escbufc > 1) {
  546. zsoutx(ZMFILE,escbuf,escbufc-1);
  547. debug(F011,"ESCBUF PRINT 3",escbuf,escbufc);
  548. }
  549. } else if (printing && escbufc > 1) {
  550. zsoutx(ZMFILE,escbuf,escbufc-1);
  551. debug(F111,"ESCBUF PRINT 4",escbuf,escbufc);
  552. }
  553. }
  554. if (!src) {
  555. escbufc = 0; /* Clear esc sequence buffer */
  556. escbuf[0] = NUL;
  557. }
  558. #endif /* XPRINT */
  559. inesc[src] = ES_NORMAL; /* Return to NORMAL. */
  560. } else if (c == ESC) { /* ESC ... */
  561. inesc[src] = ES_GOTESC; /* starts over. */
  562. }
  563. break;
  564. case ES_STRING: /* Inside a string */
  565. if (c == ESC) /* ESC may be 1st char of terminator */
  566. inesc[src] = ES_TERMIN; /* Go see. */
  567. #ifdef CK_APC
  568. else if (apcactive) { /* If in APC */
  569. if (apclength < apcbuflen) { /* and there is room... */
  570. apcbuf[apclength++] = c; /* deposit this character. */
  571. } else { /* Buffer overrun */
  572. apcactive = 0; /* Discard what we got */
  573. apclength = 0; /* and go back to normal */
  574. apcbuf[0] = 0; /* Not pretty, but what else */
  575. inesc[src] = ES_NORMAL; /* can we do? (ST might not come) */
  576. }
  577. }
  578. #endif /* CK_APC */
  579. break; /* Absorb all other characters. */
  580. case ES_TERMIN: /* Maybe a string terminator */
  581. if (c == '\\') { /* which must be backslash */
  582. inesc[src] = ES_NORMAL; /* If so, back to NORMAL */
  583. #ifdef XPRINT
  584. if (!src) {
  585. if (printing && escbufc > 1) { /* If printing... */
  586. /* Print esc seq buffer */
  587. zsoutx(ZMFILE,escbuf,escbufc-1);
  588. debug(F111,"ESCBUF PRINT 5",escbuf,escbufc);
  589. }
  590. escbufc = 0; /* Clear escseq buffer */
  591. escbuf[0] = NUL;
  592. }
  593. #endif /* XPRINT */
  594. #ifdef CK_APC
  595. if (!src && apcactive) { /* If it was an APC string, */
  596. debug(F101,"CONNECT APC terminated","",c);
  597. apcbuf[apclength] = NUL; /* terminate it and then ... */
  598. return(1);
  599. }
  600. #endif /* CK_APC */
  601. } else { /* It's not a backslash so... */
  602. inesc[src] = ES_STRING; /* back to string absorption. */
  603. #ifdef CK_APC
  604. if (apcactive) { /* In APC string */
  605. if (apclength+1 < apcbuflen) { /* If enough room */
  606. apcbuf[apclength++] = ESC; /* deposit the Esc */
  607. apcbuf[apclength++] = c; /* and this character too. */
  608. } else { /* Buffer overrun */
  609. apcactive = 0;
  610. apclength = 0;
  611. apcbuf[0] = 0;
  612. inesc[src] = ES_NORMAL;
  613. }
  614. }
  615. #endif /* CK_APC */
  616. }
  617. } /* switch() */
  618. debug(F111,"chkaes exit inesc",ckitoa(src),inesc[src]);
  619. return(0);
  620. }
  621. #endif /* NOESCSEQ */
  622. VOID
  623. #ifdef CK_ANSIC
  624. LOGCHAR(char c)
  625. #else
  626. LOGCHAR(c) char c;
  627. #endif /* CK_ANSIC */
  628. /* LOGCHAR */ { /* Log character c to session log */
  629. /* but skip over escape sequences if session log is text */
  630. if (escseq) {
  631. if ((sessft == XYFT_T) && (debses == 0) &&
  632. (inesc[0] != ES_NORMAL || oldesc[0] != ES_NORMAL))
  633. return;
  634. }
  635. logchar(c);
  636. }
  637. /* C K C P U T C -- C-Kermit CONNECT Put Character to Screen */
  638. /*
  639. Output is buffered to avoid slow screen writes on fast connections.
  640. */
  641. static int
  642. ckcputf() { /* Dump the console output buffer */
  643. int x = 0;
  644. if (obc > 0) /* If we have any characters, */
  645. x = conxo(obc,obuf); /* dump them, */
  646. obp = obuf; /* reset the pointer */
  647. obc = 0; /* and the counter. */
  648. return(x); /* Return conxo's return code */
  649. }
  650. /*
  651. NOTE: This is probably the right place for character-set translation,
  652. rather than down below in the mainline code. ckcputc() would act like
  653. xpnbyte() in ckcfns.c, and ckcgetc() would act like xgnbyte(). This
  654. would shield the rest of the code from all the complexities of many-to-one
  655. and one-to-many conversions, and would allow handling of Kanji and other
  656. CJK sets along with UTF-8 and the rest.
  657. */
  658. int
  659. ckcputc(c) int c; {
  660. int x;
  661. *obp++ = c & 0xff; /* Deposit the character */
  662. obc++; /* Count it */
  663. if (ibc == 0 || /* If input buffer about empty */
  664. obc == OBUFL) { /* or output buffer full */
  665. debug(F101,"CONNECT CKCPUTC obc","",obc);
  666. x = conxo(obc,obuf); /* dump the buffer, */
  667. obp = obuf; /* reset the pointer */
  668. obc = 0; /* and the counter. */
  669. return(x); /* Return conxo's return code */
  670. } else return(0);
  671. }
  672. /* C K C G E T C -- C-Kermit CONNECT Get Character */
  673. /*
  674. Buffered read from communication device.
  675. Returns the next character, refilling the buffer if necessary.
  676. On error, returns ttinc's return code (see ttinc() description).
  677. Dummy argument for compatible calling conventions with ttinc()
  678. so a pointer to this function can be passed to tn_doop().
  679. */
  680. int
  681. ckcgetc(dummy) int dummy; {
  682. int c, n;
  683. #ifdef CK_SSL
  684. extern int ssl_active_flag, tls_active_flag;
  685. #endif /* CK_SSL */
  686. #ifdef CK_ENCRYPTION
  687. /* No buffering for possibly encrypted connections */
  688. if (network && IS_TELNET() && TELOPT_ME(TELOPT_AUTHENTICATION))
  689. return(ttinc(0));
  690. #endif /* CK_ENCRYPTION */
  691. #ifdef CK_SSL
  692. if (ssl_active_flag || tls_active_flag)
  693. return(ttinc(0));
  694. #endif /* CK_SSL */
  695. if (ibc < 1) { /* Need to refill buffer? */
  696. ibc = 0; /* Yes, reset count */
  697. ibp = ibuf; /* and buffer pointer */
  698. c = ttinc(0); /* Read one character, blocking */
  699. if (c < 0) { /* If error, return error code */
  700. return(c);
  701. } else { /* Otherwise, got one character */
  702. *ibp++ = c; /* Advance buffer pointer */
  703. ibc++; /* and count. */
  704. }
  705. if ((n = ttchk()) > 0) { /* Any more waiting? */
  706. if (n > (IBUFL - ibc)) /* Get them all at once. */
  707. n = IBUFL - ibc; /* Don't overflow buffer */
  708. if ((n = ttxin(n,(CHAR *)ibp)) > 0) {
  709. ibc += n; /* Advance counter */
  710. }
  711. } else if (n < 0) { /* Error? */
  712. return(n); /* Return the error code */
  713. }
  714. ibp = ibuf; /* Point to beginning of buffer */
  715. }
  716. c = *ibp++ & 0xff; /* Get next character from buffer */
  717. ibc--; /* Reduce buffer count */
  718. /* debug(F000,"CKCGETC","",c); */
  719. return(c); /* Return the character */
  720. }
  721. /*
  722. Keyboard handling, buffered for speed, which is needed when C-Kermit is
  723. in CONNECT mode between two other computers that are transferring data.
  724. */
  725. static char *kbp; /* Keyboard input buffer pointer */
  726. static int kbc; /* Keyboard input buffer count */
  727. #ifdef CK_SMALL /* Keyboard input buffer length */
  728. #define KBUFL 32 /* Small for PDP-11 UNIX */
  729. #else
  730. #define KBUFL 257 /* Regular kernel size for others */
  731. #endif /* CK_SMALL */
  732. #ifdef DYNAMIC
  733. static char *kbuf = NULL;
  734. #else
  735. static char kbuf[KBUFL];
  736. #endif /* DYNAMIC */
  737. /* Macro for reading keystrokes. */
  738. #define CONGKS() (((--kbc)>=0) ? ((int)(*kbp++) & 0377) : kbget())
  739. /*
  740. Note that we call read() directly here, normally a no-no, but in this case
  741. we know it's UNIX and we're only doing what coninc(0) would have done,
  742. except we're reading a block of characters rather than just one. There is,
  743. at present, no conxin() analog to ttxin() for chunk reads, and instituting
  744. one would only add function-call overhead as it would only be a wrapper for
  745. a read() call anyway.
  746. Another note: We stick in this read() till the user types something.
  747. But we know they already did, since select() said so. Therefore something
  748. would need to be mighty wrong before we get stuck here.
  749. */
  750. static int /* Keyboard buffer filler */
  751. kbget() {
  752. #ifdef EINTR
  753. int tries = 10; /* If read() is interrupted, */
  754. int ok = 0;
  755. while (tries-- > 0) { /* try a few times... */
  756. #endif /* EINTR */
  757. kbc = conchk(); /* How many chars waiting? */
  758. debug(F101,"kbget kbc","",kbc);
  759. if (kbc < 1)
  760. kbc = 1; /* If none or dunno, wait for one. */
  761. else if (kbc > KBUFL) /* If too many, */
  762. kbc = KBUFL; /* only read this many. */
  763. if ((kbc = read(0, kbuf, kbc)) < 1) { /* Now read it/them. */
  764. debug(F101,"CONNECT kbget errno","",errno); /* Got an error. */
  765. #ifdef EINTR
  766. if (errno == EINTR) /* Interrupted system call. */
  767. continue; /* Try again, up to limit. */
  768. else /* Something else. */
  769. #endif /* EINTR */
  770. return(-1); /* Pass along read() error. */
  771. }
  772. #ifdef EINTR
  773. else { ok = 1; break; }
  774. }
  775. if (!ok) return(-1);
  776. #endif /* EINTR */
  777. kbp = kbuf; /* Adjust buffer pointer, */
  778. kbc--; /* count, */
  779. return((int)(*kbp++) & 0377); /* and return first character. */
  780. }
  781. #ifdef BEBOX
  782. /*
  783. * CreateSocketPair --
  784. *
  785. * This procedure creates a connected socket pair
  786. *
  787. * Results:
  788. * 0 if OK, the error if not OK.
  789. *
  790. * Side effects:
  791. * None
  792. */
  793. int
  794. socketpair(int *pair) {
  795. int servsock;
  796. int val;
  797. struct sockaddr_in serv_addr, cli_addr;
  798. extern char myipaddr[];
  799. debug(F110,"socketpair",myipaddr,0);
  800. if (myipaddr[0] == 0)
  801. getlocalipaddr();
  802. servsock = socket(AF_INET, SOCK_STREAM, 0);
  803. if (servsock == 0) {
  804. return h_errno;
  805. }
  806. debug(F111,"socketpair","socket",servsock);
  807. memset(&serv_addr, 0, sizeof(serv_addr));
  808. serv_addr.sin_family = AF_INET;
  809. serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  810. serv_addr.sin_port = htons(0);
  811. val = sizeof(serv_addr);
  812. if (bind(servsock, (struct sockaddr *) &serv_addr, val) < 0) {
  813. closesocket(servsock);
  814. return h_errno;
  815. }
  816. debug(F111,"socketpair","bind",0);
  817. listen(servsock, 1);
  818. debug(F111,"socketpair","listen",0);
  819. if (getsockname(servsock, (struct sockaddr *) &serv_addr, &val) < 0) {
  820. closesocket(servsock);
  821. return h_errno;
  822. }
  823. debug(F111,"socketpair","getsockname",0);
  824. pair[0] = socket(AF_INET, SOCK_STREAM, 0);
  825. if (pair[0] == 0) {
  826. closesocket(servsock);
  827. return h_errno;
  828. }
  829. debug(F111,"socketpair","socket",pair[0]);
  830. memset(&cli_addr, 0, sizeof(cli_addr));
  831. cli_addr.sin_family = AF_INET;
  832. cli_addr.sin_addr.s_addr = inet_addr(myipaddr[0]?myipaddr:"127.0.0.1");
  833. cli_addr.sin_port = serv_addr.sin_port;
  834. if (connect(pair[0],(struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) {
  835. closesocket(pair[0]);
  836. closesocket(servsock);
  837. return h_errno;
  838. }
  839. debug(F111,"socketpair","connect",0);
  840. pair[1] = accept(servsock, (struct sockaddr *) &serv_addr, &val);
  841. if (pair[1] == 0) {
  842. closesocket(pair[0]);
  843. closesocket(servsock);
  844. return h_errno;
  845. }
  846. debug(F111,"socketpair","accept",pair[1]);
  847. closesocket(servsock);
  848. debug(F111,"socketpair","closesocket",0);
  849. return 0;
  850. }
  851. long
  852. kbdread(void * param) {
  853. int sock = (int) param;
  854. char ch;
  855. int rc = 0;
  856. debug(F111,"kbdread","sock",sock);
  857. while (rc >= 0) {
  858. rc = read(fileno(stdin), &ch, 1); /* Read a character. */
  859. if (rc > 0) {
  860. rc = send(sock,&ch,1,0);
  861. /* debug(F000,"kbdread","send()",ch); */
  862. printf("\r\ngot: %c rc = %d\r\n",ch,rc);
  863. } else
  864. msleep(100);
  865. }
  866. debug(F110,"kbdread","terminating",0);
  867. return(rc);
  868. }
  869. #endif /* BEBOX */
  870. #ifdef CKLEARN
  871. static VOID
  872. learnchar(c) int c; { /* Learned script keyboard character */
  873. int cc;
  874. char xbuf[8];
  875. if (!learning || !learnfp)
  876. return;
  877. switch (learnst) { /* Learn state... */
  878. case 0: /* Neutral */
  879. case 1: /* Net */
  880. if (learnbc > 0) { /* Have net characters? */
  881. char buf[LEARNBUFSIZ];
  882. int i, j, n;
  883. ULONG t;
  884. t = (ULONG) time(0); /* Calculate INPUT timeout */
  885. j = t - learnt1;
  886. j += (j / 4) > 0 ? (j / 4) : 1; /* Add some slop */
  887. if (j < 2) j = 2; /* 2 seconds minimum */
  888. fputs("\nINPUT ",learnfp); /* Give INPUT command for them */
  889. fputs(ckitoa(j),learnfp);
  890. fputs(" {",learnfp);
  891. learnt1 = t;
  892. n = LEARNBUFSIZ;
  893. if (learnbc < LEARNBUFSIZ) { /* Circular buffer */
  894. n = learnbc; /* hasn't wrapped yet. */
  895. learnbp = 0;
  896. }
  897. j = 0; /* Copy to linear buffer */
  898. for (i = 0; i < n; i++) { /* Number of chars in circular buf */
  899. cc = learnbuf[(learnbp + i) % LEARNBUFSIZ];
  900. /* Later account for prompts that end with a newline? */
  901. if (cc == CR && j > 0) {
  902. if (buf[j-1] != LF)
  903. j = 0;
  904. }
  905. buf[j++] = cc;
  906. }
  907. for (i = 0; i < j; i++) { /* Now copy out the buffer */
  908. cc = buf[i]; /* interpreting control chars */
  909. if (cc == 0) { /* We don't INPUT NULs */
  910. continue;
  911. } else if (cc < SP || /* Controls need quoting */
  912. (cc > 126 && cc < 160)) {
  913. ckmakmsg(xbuf,8,"\\{",ckitoa((int)cc),"}",NULL);
  914. fputs(xbuf,learnfp);
  915. } else { /* Plain character */
  916. putc(cc,learnfp);
  917. }
  918. }
  919. fputs("}\nIF FAIL STOP 1 INPUT timeout",learnfp);
  920. learnbc = 0;
  921. }
  922. learnbp = 0;
  923. fputs("\nPAUSE 1\nOUTPUT ",learnfp); /* Emit OUTPUT and fall thru */
  924. case 2: /* Already in Keyboard state */
  925. if (c == 0) {
  926. fputs("\\N",learnfp);
  927. } else if (c == -7) {
  928. fputs("\\B",learnfp);
  929. } else if (c == -8) {
  930. fputs("\\L",learnfp);
  931. } else if (c < SP || (c > 126 && c < 160)) {
  932. ckmakmsg(xbuf,8,"\\{",ckitoa((int)c),"}",NULL);
  933. fputs(xbuf,learnfp);
  934. } else {
  935. putc(c,learnfp);
  936. }
  937. }
  938. }
  939. #endif /* CKLEARN */
  940. static int printbar = 0;
  941. #define OUTXBUFSIZ 15
  942. static CHAR inxbuf[OUTXBUFSIZ+1]; /* Host-to-screen expansion buffer */
  943. static int inxcount = 0; /* and count */
  944. static CHAR outxbuf[OUTXBUFSIZ+1]; /* Keyboard-to-host expansion buf */
  945. static int outxcount = 0; /* and count */
  946. int
  947. conect() {
  948. int rc = 0; /* Return code: 0 = fail, 1 = OK */
  949. int i, x = 0, prev = -1; /* Reason code in cx_status */
  950. #ifdef CKLEARN
  951. int crflag = 0;
  952. #endif /* CKLEARN */
  953. register int c = -1, c2, csave; /* Characters */
  954. #ifdef TNCODE
  955. int tx; /* For Telnet negotiations */
  956. #endif /* TNCODE */
  957. int apcrc = 0; /* For APC and transparent print */
  958. int n, kbin, scrnout; /* select() items... */
  959. fd_set in, out, err; /* File descriptor sets */
  960. int gotnet = 0; /* Flag for net ready to read */
  961. int gotkbd = 0; /* Flag for keyboard ready to read */
  962. int oldprt = 0; /* Used with printing */
  963. int msgflg = 0;
  964. char cbuf[2]; /* Ditto */
  965. #ifdef BEBOX
  966. int tid = 0; /* Thread ID */
  967. int pair[2]; /* Socket Pair */
  968. CHAR ch;
  969. CHAR buf[64];
  970. #endif /* BEBOX */
  971. cx_status = CSX_INTERNAL;
  972. debok = 1;
  973. #ifdef BEBOX
  974. {
  975. /* Create a socket pair to be used for the keyboard input */
  976. if (socketpair(pair)) {
  977. debug(F110,"conect","unable to create socket pair",0);
  978. return(-1);
  979. }
  980. debug(F111,"connect","socket pair[0]",pair[0]);
  981. debug(F111,"connect","socket pair[1]",pair[1]);
  982. /* Assign one end of the socket to kbin */
  983. kbin = pair[0];
  984. tid = spawn_thread(kbdread,
  985. "Kbd to Socket Pair",
  986. B_NORMAL_PRIORITY,
  987. (void *)pair[1]
  988. );
  989. resume_thread(tid);
  990. debug(F110,"connect","tid",tid);
  991. }
  992. #else /* BEBOX */
  993. kbin = fileno(stdin); /* stdin file descriptor */
  994. #endif /* BEBOX */
  995. scrnout = fileno(stdout); /* stdout file descriptor */
  996. #ifdef CK_TRIGGER
  997. makestr(&triggerval,NULL); /* Reset trigger */
  998. #endif /* CK_TRIGGER */
  999. #ifdef XPRINT
  1000. escbufc = 0; /* Reset esc-sequence buffer */
  1001. escbuf[0] = NUL;
  1002. #endif /* XPRINT */
  1003. cbuf[1] = NUL;
  1004. ttimoff(); /* Turn off any timer interrupts */
  1005. if (!local) { /* Be sure we're not in remote mode */
  1006. #ifdef NETCONN
  1007. #ifdef NEWFTP
  1008. if (ftpisconnected())
  1009. printf("Sorry, you can't CONNECT to an FTP server\n");
  1010. else
  1011. #endif /* NEWFTP */
  1012. printf("Sorry, you must SET LINE or SET HOST first\n");
  1013. #else
  1014. printf("Sorry, you must SET LINE first\n");
  1015. #endif /* NETCONN */
  1016. return(0);
  1017. }
  1018. if (speed < 0L && network == 0 && ttfdflg == 0) {
  1019. printf("Sorry, you must SET SPEED first\n");
  1020. return(0);
  1021. }
  1022. #ifdef TCPSOCKET
  1023. if (network && !ttpipe && (nettype != NET_TCPB && nettype != NET_PTY)) {
  1024. printf("Sorry, network type not supported\n");
  1025. return(0);
  1026. }
  1027. #endif /* TCPSOCKET */
  1028. #ifdef DYNAMIC
  1029. if (!ibuf) {
  1030. if (!(ibuf = malloc(IBUFL+1))) { /* Allocate input line buffer */
  1031. printf("Sorry, CONNECT input buffer can't be allocated\n");
  1032. return(0);
  1033. } else {
  1034. ibp = ibuf;
  1035. ibc = 0;
  1036. }
  1037. }
  1038. if (!obuf) {
  1039. if (!(obuf = malloc(OBUFL+1))) { /* Allocate output line buffer */
  1040. printf("Sorry, CONNECT output buffer can't be allocated\n");
  1041. return(0);
  1042. } else {
  1043. obp = obuf;
  1044. obc = 0;
  1045. }
  1046. }
  1047. if (!kbuf) {
  1048. if (!(kbuf = malloc(KBUFL+1))) { /* Allocate keyboard input buffer */
  1049. printf("Sorry, CONNECT keyboard buffer can't be allocated\n");
  1050. return(0);
  1051. }
  1052. }
  1053. if (!temp) {
  1054. if (!(temp = malloc(TMPLEN+1))) { /* Allocate temporary buffer */
  1055. printf("Sorry, CONNECT temporary buffer can't be allocated\n");
  1056. return(0);
  1057. }
  1058. }
  1059. #else
  1060. obp = obuf;
  1061. obc = 0;
  1062. #endif /* DYNAMIC */
  1063. kbp = kbuf; /* Always clear these. */
  1064. *kbp = NUL; /* No need to preserve them between */
  1065. kbc = 0; /* CONNECT sessions. */
  1066. #ifdef DEBUG
  1067. if (deblog) {
  1068. debug(F101,"CONNECT conect entry ttyfd","",ttyfd);
  1069. debug(F101,"CONNECT conect entry ibc","",ibc);
  1070. debug(F101,"CONNECT conect entry obc","",obc);
  1071. debug(F101,"CONNECT conect entry kbc","",kbc);
  1072. #ifdef CK_TRIGGER
  1073. debug(F110,"CONNECT conect trigger",tt_trigger[0],0);
  1074. #endif /* CK_TRIGGER */
  1075. if (ttyfd > -1) {
  1076. n = ttchk();
  1077. debug(F101,"CONNECT conect entry ttchk","",n);
  1078. }
  1079. }
  1080. #endif /* DEBUG */
  1081. if (ttyfd < 0) { /* If communication device not open */
  1082. #ifdef TTLEBUF
  1083. int n = le_inbuf();
  1084. debug(F111,"CONNECT le_inbuf()","ttyfd < 0",n);
  1085. if (n > 0) {
  1086. while (n--) {
  1087. CHAR ch;
  1088. le_getchar(&ch);
  1089. conoc(ch);
  1090. }
  1091. return(0);
  1092. }
  1093. #endif /* TTLEBUF */
  1094. debug(F101,"CONNECT ttnproto","",ttnproto);
  1095. debug(F111,"CONNECT opening",ttname,0); /* Open it now */
  1096. if (ttopen(ttname,
  1097. &local,
  1098. network ? -nettype : mdmtyp,
  1099. 0
  1100. ) < 0) {
  1101. ckmakmsg(temp,TMPLEN,"Sorry, can't open ",ttname,NULL,NULL);
  1102. perror(temp);
  1103. debug(F110,"CONNECT open failure",ttname,0);
  1104. return(0);
  1105. }
  1106. #ifdef IKS_OPTION
  1107. /* If peer is in Kermit server mode, return now. */
  1108. if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start) {
  1109. cx_status = CSX_IKSD;
  1110. return(0);
  1111. }
  1112. #endif /* IKS_OPTION */
  1113. }
  1114. dohangup = 0; /* Hangup not requested yet */
  1115. msgflg = !quiet
  1116. #ifdef CK_APC
  1117. && !apcactive
  1118. #endif /* CK_APC */
  1119. ;
  1120. if (msgflg) {
  1121. #ifdef NETCONN
  1122. if (network) {
  1123. #ifdef CK_ENCRYPTION
  1124. extern int me_encrypt, u_encrypt;
  1125. if (ck_tn_encrypting() && ck_tn_decrypting())
  1126. printf("SECURE connection to host %s",ttname);
  1127. else
  1128. #endif /* CK_ENCRYPTION */
  1129. if (ttpipe || ttpty)
  1130. printf("\r\nConnecting via command \"%s\"",ttname);
  1131. else
  1132. printf("\r\nConnecting to host %s",ttname);
  1133. } else {
  1134. #endif /* NETCONN */
  1135. printf("\r\nConnecting to %s",ttname);
  1136. if (speed > -1L) printf(", speed %ld",speed);
  1137. #ifdef NETCONN
  1138. }
  1139. #endif /* NETCONN */
  1140. if (tt_escape) {
  1141. printf("\r\n");
  1142. shoesc(escape);
  1143. printf("Type the escape character followed by C to get back,\r\n");
  1144. printf("or followed by ? to see other options.\r\n");
  1145. } else {
  1146. printf(".\r\n\nESCAPE CHARACTER IS DISABLED\r\n\n");
  1147. }
  1148. if (seslog) {
  1149. extern int slogts;
  1150. char * s = "";
  1151. switch (sessft) {
  1152. case XYFT_D:
  1153. s = "debug"; break;
  1154. case XYFT_T:
  1155. s = slogts ? "timestamped-text" : "text"; break;
  1156. default:
  1157. s = "binary";
  1158. }
  1159. printf("Session Log: %s, %s (%d) \r\n",sesfil,s,sessft);
  1160. }
  1161. if (debses) printf("Debugging Display...)\r\n");
  1162. }
  1163. /* Condition console terminal and communication line */
  1164. if (conbin((char)escape) < 0) {
  1165. printf("Sorry, can't condition console terminal\n");
  1166. fflush(stdout);
  1167. return(0);
  1168. }
  1169. debug(F101,"CONNECT cmask","",cmask);
  1170. debug(F101,"CONNECT cmdmsk","",cmdmsk);
  1171. debug(F101,"CONNECT speed before ttvt","",speed);
  1172. if ((n = ttvt(speed,flow)) < 0) { /* Enter "virtual terminal" mode */
  1173. if (!network) {
  1174. debug(F101,"CONNECT ttvt","",n);
  1175. tthang(); /* Hang up and close the device. */
  1176. ttclos(0);
  1177. dologend();
  1178. if (ttopen(ttname, /* Open it again... */
  1179. &local,
  1180. network ? -nettype : mdmtyp,
  1181. 0
  1182. ) < 0) {
  1183. cx_status = CSX_INTERNAL;
  1184. ckmakmsg(temp,TMPLEN,"Sorry, can't reopen ",ttname,NULL,NULL);
  1185. perror(temp);
  1186. return(0);
  1187. }
  1188. #ifdef IKS_OPTION
  1189. if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start) {
  1190. cx_status = CSX_IKSD;
  1191. return(0);
  1192. }
  1193. #endif /* IKS_OPTION */
  1194. if (ttvt(speed,flow) < 0) { /* Try virtual terminal mode again. */
  1195. conres(); /* Failure this time is fatal. */
  1196. printf("Sorry, Can't condition communication line\n");
  1197. cx_status = CSX_INTERNAL;
  1198. return(0);
  1199. }
  1200. }
  1201. }
  1202. debug(F101,"CONNECT ttvt ok, escape","",escape);
  1203. /* Despite ttvt() this is still needed in HP-UX */
  1204. /* because of the HP-9000 <RESET> key.*/
  1205. signal(SIGINT, SIG_IGN);
  1206. signal(SIGQUIT, SIG_IGN);
  1207. debug(F101,"CONNECT carrier-watch","",carrier);
  1208. if ((!network
  1209. #ifdef TN_COMPORT
  1210. || istncomport()
  1211. #endif /* TN_COMPORT */
  1212. ) && (carrier != CAR_OFF)) {
  1213. int x;
  1214. x = ttgmdm();
  1215. debug(F100,"CONNECT ttgmdm","",x);
  1216. if ((x > -1) && !(x & BM_DCD)) {
  1217. #ifndef NOHINTS
  1218. extern int hints;
  1219. #endif /* NOHINTS */
  1220. debug(F100,"CONNECT ttgmdm CD test fails","",x);
  1221. conres();
  1222. printf("?Carrier required but not detected.\n");
  1223. #ifndef NOHINTS
  1224. cx_status = CSX_CARRIER;
  1225. if (!hints)
  1226. return(0);
  1227. printf("***********************************\n");
  1228. printf(" Hint: To CONNECT to a serial device that\n");
  1229. printf(" is not presenting the Carrier Detect signal,\n");
  1230. printf(" first tell C-Kermit to:\n\n");
  1231. printf(" SET CARRIER-WATCH OFF\n\n");
  1232. printf("***********************************\n\n");
  1233. #endif /* NOHINTS */
  1234. return(0);
  1235. }
  1236. debug(F100,"CONNECT ttgmdm ok","",0);
  1237. }
  1238. /* Now we are connected. */
  1239. if (msgflg || printbar)
  1240. printf("----------------------------------------------------\r\n");
  1241. fflush(stdout);
  1242. #ifndef NOCSETS
  1243. /* Set up character set translations */
  1244. unicode = 0; /* Assume Unicode won't be involved */
  1245. tcs = 0; /* "Transfer" or "Other" charset */
  1246. sxo = rxo = NULL; /* Initialize byte-to-byte functions */
  1247. sxi = rxi = NULL;
  1248. if (tcsr != tcsl) { /* Remote and local sets differ... */
  1249. #ifdef UNICODE
  1250. if (tcsr == FC_UTF8 || /* Remote charset is UTF-8 */
  1251. tcsl == FC_UTF8) { /* or local one is. */
  1252. xuf = xl_ufc[tcsl]; /* Incoming Unicode to local */
  1253. if (xuf || tcsl == FC_UTF8) {
  1254. tcs = (tcsr == FC_UTF8) ? tcsl : tcsr; /* The "other" set */
  1255. xfu = xl_fcu[tcs]; /* Local byte to remote Unicode */
  1256. if (xfu)
  1257. unicode = (tcsr == FC_UTF8) ? 1 : 2;
  1258. }
  1259. tcssize = fcsinfo[tcs].size; /* Size of other character set. */
  1260. } else {
  1261. #endif /* UNICODE */
  1262. tcs = gettcs(tcsr,tcsl); /* Get intermediate set. */
  1263. sxo = xls[tcs][tcsl]; /* translation function */
  1264. rxo = xlr[tcs][tcsr]; /* pointers for output functions */
  1265. sxi = xls[tcs][tcsr]; /* and for input functions. */
  1266. rxi = xlr[tcs][tcsl];
  1267. #ifdef UNICODE
  1268. }
  1269. #endif /* UNICODE */
  1270. }
  1271. /*
  1272. This is to prevent use of zmstuff() and zdstuff() by translation functions.
  1273. They only work with disk i/o, not with communication i/o. Luckily Russian
  1274. translation functions don't do any stuffing...
  1275. */
  1276. langsv = language;
  1277. #ifndef NOCYRIL
  1278. if (language != L_RUSSIAN)
  1279. #endif /* NOCYRIL */
  1280. language = L_USASCII;
  1281. #ifdef COMMENT
  1282. #ifdef DEBUG
  1283. if (deblog) {
  1284. debug(F101,"CONNECT tcs","",tcs);
  1285. debug(F101,"CONNECT tcsl","",tcsl);
  1286. debug(F101,"CONNECT tcsr","",tcsr);
  1287. debug(F101,"CONNECT fcsinfo[tcsl].size","",fcsinfo[tcsl].size);
  1288. debug(F101,"CONNECT fcsinfo[tcsr].size","",fcsinfo[tcsr].size);
  1289. debug(F101,"CONNECT unicode","",unicode);
  1290. }
  1291. #endif /* DEBUG */
  1292. #endif /* COMMENT */
  1293. #ifdef CK_XYZ
  1294. #ifndef XYZ_INTERNAL
  1295. {
  1296. extern int binary; /* See about ZMODEM autodownloads */
  1297. char * s;
  1298. s = binary ? ptab[PROTO_Z].p_b_rcmd : ptab[PROTO_Z].p_t_rcmd;
  1299. if (!s) s = "";
  1300. zmdlok = (*s != NUL); /* OK if we have external commands */
  1301. }
  1302. #endif /* XYZ_INTERNAL */
  1303. #endif /* CK_XYZ */
  1304. #ifndef NOESCSEQ
  1305. /*
  1306. We need to activate the escape-sequence recognition feature when:
  1307. (a) translation is elected, AND
  1308. (b) the local and/or remote set is a 7-bit set other than US ASCII;
  1309. Or:
  1310. SET SESSION-LOG is TEXT (so we can strip escape sequences out of the log);
  1311. Or:
  1312. SET TERMINAL APC is not OFF (handled in the next statement).
  1313. */
  1314. escseq = (tcs != TC_TRANSP) && /* Not transparent */
  1315. (fcsinfo[tcsl].size == 128 || fcsinfo[tcsr].size == 128) && /* 7 bits */
  1316. (fcsinfo[tcsl].code != FC_USASCII); /* But not ASCII */
  1317. #endif /* NOESCSEQ */
  1318. #endif /* NOCSETS */
  1319. #ifndef NOESCSEQ
  1320. if (!escseq) { /* 2009/10/22 */
  1321. if (seslog && !sessft)
  1322. escseq = 1;
  1323. }
  1324. #ifdef CK_APC
  1325. escseq = escseq || (apcstatus & APC_ON);
  1326. apcactive = 0; /* An APC command is not active */
  1327. apclength = 0; /* ... */
  1328. #endif /* CK_APC */
  1329. #ifdef XPRINT
  1330. escseq |= tt_print;
  1331. #endif /* XPRINT */
  1332. /* Initial state of recognizer */
  1333. inesc[0] = ES_NORMAL; /* Remote to screen */
  1334. inesc[1] = ES_NORMAL; /* Keyboard to remote */
  1335. debug(F101,"CONNECT escseq","",escseq);
  1336. #endif /* NOESCSEQ */
  1337. if (ttyfd > -1) { /* (just in case...) */
  1338. what = W_CONNECT; /* Keep track of what we're doing */
  1339. active = 1;
  1340. }
  1341. #ifdef CKLEARN
  1342. if (learning) { /* Learned script active... */
  1343. learnbp = 0; /* INPUT buffer pointer */
  1344. learnbc = 0; /* INPUT buffer count */
  1345. learnst = 0; /* State (0 = neutral, none) */
  1346. learnt1 = (ULONG) time(0);
  1347. }
  1348. #endif /* CKLEARN */
  1349. #ifdef CKTIDLE
  1350. idlelimit = tt_idlelimit;
  1351. #endif /* CKTIDLE */
  1352. while (active) { /* Big loop... */
  1353. debug(F100,"CONNECT top of loop","",0);
  1354. FD_ZERO(&in); /* Clear select() structs */
  1355. FD_ZERO(&out);
  1356. FD_ZERO(&err);
  1357. gotkbd = 0;
  1358. gotnet = ttpeek(); /* Something sitting in ckutio buf */
  1359. debug(F101,"CONNECT ttpeek","",gotnet);
  1360. if (
  1361. #ifndef NOSETKEY
  1362. !kmptr /* Check for key macro active */
  1363. #else
  1364. 1
  1365. #endif /* NOSETKEY */
  1366. ) {
  1367. if (obc) { /* No key macro - set up for select */
  1368. FD_SET(ttyfd, &out); /* Have stuff to send to net */
  1369. } else {
  1370. FD_SET(kbin, &in); /* Need to read stuff from keyboard */
  1371. }
  1372. #ifdef BEBOX
  1373. if (!(ibc || gotnet > 0))
  1374. FD_SET(ttyfd, &in); /* Need to read stuff from net */
  1375. #else /* BEBOX */
  1376. if (ibc || gotnet > 0) {
  1377. FD_SET(scrnout, &out); /* Have stuff to put on screen */
  1378. } else {
  1379. FD_SET(ttyfd, &in); /* Need to read stuff from net */
  1380. }
  1381. #endif /* BEBOX */
  1382. FD_SET(ttyfd, &err);
  1383. #ifdef CK_FORWARD_X
  1384. fwdx_init_fd_set(&in);
  1385. #endif /* CK_FORWARD_X */
  1386. /* Wait till the first one of the above is ready for i/o */
  1387. /* or TERM IDLE-SEND is active and we time out. */
  1388. errno = 0;
  1389. #ifdef CKTIDLE
  1390. /* This really could be moved out of the loop... */
  1391. if (idlelimit) { /* Idle timeout set */
  1392. struct timeval tv;
  1393. if (idlelimit > 0) { /* Positive = sec */
  1394. tv.tv_sec = (long) idlelimit;
  1395. tv.tv_usec = 0L;
  1396. } else { /* Negative = millisec */
  1397. long u = (0 - idlelimit);
  1398. tv.tv_sec = u / 1000L;
  1399. tv.tv_usec = ((u % 1000L) * 1000L);
  1400. }
  1401. #ifdef INTSELECT
  1402. c = select(FD_SETSIZE,(int *)&in,(int *)&out,(int *)&err, &tv);
  1403. #else
  1404. c = select(FD_SETSIZE, &in, &out, &err, &tv);
  1405. #endif /* INTSELECT */
  1406. } else
  1407. #endif /* CKTIDLE */
  1408. #ifdef INTSELECT
  1409. c = select(FD_SETSIZE, (int *)&in, (int *)&out, (int *)&err, 0);
  1410. #else
  1411. c = select(FD_SETSIZE, &in, &out, &err, 0);
  1412. #endif /* INTSELECT */
  1413. if (c < 1) {
  1414. #ifdef CKTIDLE
  1415. if (c == 0) { /* Timeout */
  1416. debug(F101,"CONNECT select() timeout","",tt_idleact);
  1417. switch (tt_idleact) {
  1418. case IDLE_HANG: { /* Hang up */
  1419. int x = 0;
  1420. #ifndef NODIAL
  1421. if (dialmhu)
  1422. x = mdmhup();
  1423. if (x < 1)
  1424. #endif /* NODIAL */
  1425. tthang(); /* fall thru deliberately... */
  1426. }
  1427. case IDLE_RET: /* Return to command mode */
  1428. cx_status = CSX_IDLE;
  1429. active = 0;
  1430. continue;
  1431. case IDLE_OUT: /* OUTPUT a string */
  1432. if (tt_idlestr) {
  1433. int len = strlen(tt_idlestr);
  1434. if (len > 0)
  1435. ttol((CHAR *)tt_idlestr,len);
  1436. else
  1437. ttoc(NUL); /* No string, send a NUL */
  1438. } else
  1439. ttoc(NUL); /* No string, send a NUL */
  1440. continue;
  1441. case IDLE_EXIT: /* Exit from Kermit */
  1442. doexit(GOOD_EXIT,xitsta);
  1443. #ifdef TNCODE
  1444. case IDLE_TAYT: /* Send Telnet Are You There? */
  1445. if (network && IS_TELNET()) {
  1446. tnopt[0] = (CHAR) IAC;
  1447. tnopt[1] = (CHAR) TN_AYT;
  1448. tnopt[2] = NUL;
  1449. if (ttol((CHAR *)tnopt,2) < 0)
  1450. active = 0;
  1451. }
  1452. continue;
  1453. case IDLE_TNOP: /* Send Telnet NOP */
  1454. if (network && IS_TELNET()) {
  1455. tnopt[0] = (CHAR) IAC;
  1456. tnopt[1] = (CHAR) TN_NOP;
  1457. tnopt[2] = NUL;
  1458. if (ttol((CHAR *)tnopt,2) < 0)
  1459. active = 0;
  1460. }
  1461. continue;
  1462. #endif /* TNCODE */
  1463. }
  1464. }
  1465. #endif /* CKTIDLE */
  1466. debug(F101,"CONNECT select() errno","",errno);
  1467. /* A too-big first arg to select() gets EBADF */
  1468. #ifdef EINTR
  1469. if (c == -1) {
  1470. if (errno == EINTR) {
  1471. continue;
  1472. }
  1473. }
  1474. #endif /* EINTR */
  1475. sleep(1);
  1476. continue;
  1477. }
  1478. #ifndef BEBOX
  1479. #ifdef DEBUG
  1480. if (FD_ISSET(scrnout, &out)) {
  1481. debug(F100,"CONNECT SELECT scrnout","",0);
  1482. }
  1483. #endif /* DEBUG */
  1484. #endif /* BEBOX */
  1485. #ifdef CK_FORWARD_X
  1486. fwdx_check_sockets(&in);
  1487. #endif /* CK_FORWARD_X */
  1488. if (FD_ISSET(ttyfd, &in)) { /* Read from net? */
  1489. debug(F110,"CONNECT SELECT ttyfd","in",0);
  1490. FD_CLR(ttyfd, &in);
  1491. gotnet = 1; /* Net is ready */
  1492. }
  1493. if (FD_ISSET(kbin, &in)) { /* Read from keyboard? */
  1494. debug(F100,"CONNECT SELECT kbin","",0);
  1495. FD_CLR(kbin, &in);
  1496. gotkbd = 1; /* Keyboard is ready */
  1497. }
  1498. if (FD_ISSET(ttyfd, &err)) {
  1499. debug(F110,"CONNECT SELECT ttyfd","err",0);
  1500. FD_CLR(ttyfd, &err);
  1501. #ifdef NETPTY
  1502. #ifdef HAVE_PTYTRAP
  1503. /* Special handling for HP-UX pty i/o */
  1504. if (ttpty) {
  1505. if (pty_trap_handler(ttyfd) > 0) {
  1506. ttclos(0);
  1507. goto conret1;
  1508. }
  1509. continue;
  1510. }
  1511. #endif /* HAVE_PTYTRAP */
  1512. #endif /* NETPTY */
  1513. gotnet = 1; /* Net is ready (don't set if pty) */
  1514. }
  1515. }
  1516. #ifdef DEBUG
  1517. if (deblog) {
  1518. debug(F101,"CONNECT gotkbd","",gotkbd);
  1519. debug(F101,"CONNECT kbc","",kbc);
  1520. #ifdef COMMENT
  1521. #ifndef NOSETKEY
  1522. debug(F101,"CONNECT kmptr","",kmptr);
  1523. #endif /* NOSETKEY */
  1524. #endif /* COMMENT */
  1525. }
  1526. #endif /* DEBUG */
  1527. while (gotkbd || kbc > 0 /* If we have keyboard chars */
  1528. #ifndef NOSETKEY
  1529. || kmptr
  1530. #endif /* NOSETKEY */
  1531. ) {
  1532. #ifndef NOSETKEY
  1533. if (kmptr) { /* Have current macro? */
  1534. debug(F100,"CONNECT kmptr non NULL","",0);
  1535. if ((c = (CHAR) *kmptr++) == NUL) { /* Get char from it */
  1536. debug(F100,"CONNECT macro empty, continuing","",0);
  1537. kmptr = NULL; /* If no more chars, */
  1538. continue; /* Reset pointer and continue */
  1539. }
  1540. debug(F000,"CONNECT char from macro","",c);
  1541. } else { /* No macro... */
  1542. #endif /* NOSETKEY */
  1543. #ifdef BEBOX
  1544. {
  1545. int rc = 0;
  1546. if ((rc = recv(kbin,buf,1,0)) > 0)
  1547. c = buf[0];
  1548. else
  1549. c = -1;
  1550. debug(F111,"recv","rc",rc);
  1551. printf("\r\nrecv: %c rc=%d\r\n",buf[0],rc);
  1552. }
  1553. #else /* BEBOX */
  1554. c = CONGKS(); /* Yes, read from keyboard */
  1555. #endif /* BEBOX */
  1556. gotkbd = 0; /* Turn off select() result flag */
  1557. #ifndef NOSETKEY
  1558. }
  1559. #endif /* NOSETKEY */
  1560. if (c == -1) {
  1561. #ifdef EINTR
  1562. if (errno == EINTR)
  1563. continue;
  1564. #endif /* EINTR */
  1565. cx_status = CSX_IOERROR;
  1566. conoc(BEL);
  1567. goto conret0;
  1568. }
  1569. c &= cmdmsk; /* Do any requested masking */
  1570. #ifndef NOSETKEY
  1571. /*
  1572. Note: kmptr is NULL if we got character c from the keyboard, and it is
  1573. not NULL if it came from a macro. In the latter case, we must avoid
  1574. expanding it again.
  1575. */
  1576. if (!kmptr && macrotab[c]) { /* Macro definition for c? */
  1577. debug(F000,"CONNECT macro key",macrotab[c],c);
  1578. kmptr = macrotab[c]; /* Yes, set up macro pointer */
  1579. continue; /* and restart the loop, */
  1580. } else c = keymap[c]; /* else use single-char keymap */
  1581. #endif /* NOSETKEY */
  1582. if (
  1583. #ifndef NOSETKEY
  1584. !kmptr &&
  1585. #endif /* NOSETKEY */
  1586. (tt_escape && ((c & 0xff) == escape))) { /* Escape char? */
  1587. debug(F000,"CONNECT got escape","",c);
  1588. #ifdef BEBOX
  1589. if (recv(kbin,buf,1,0)>=0)
  1590. c = buf[0];
  1591. else
  1592. c = -1;
  1593. #else /* BEBOX */
  1594. c = CONGKS() & 0x7f; /* Read argument */
  1595. #endif /* BEBOX */
  1596. doesc((char) c); /* Handle it */
  1597. continue; /* Back to loop */
  1598. }
  1599. csave = c; /* Save it before translation */
  1600. /* for local echoing. */
  1601. #ifdef CKLEARN
  1602. crflag = (c == CR); /* Remember if it was CR. */
  1603. #endif /* CKLEARN */
  1604. #ifndef NOCSETS
  1605. if (inesc[1] == ES_NORMAL) { /* If not inside escape seq.. */
  1606. /* Translate character sets */
  1607. #ifdef UNICODE
  1608. int x;
  1609. if (unicode == 1) { /* Remote is UTF-8 */
  1610. outxcount = b_to_u((CHAR)c,outxbuf,OUTXBUFSIZ,tcssize);
  1611. outxbuf[outxcount] = NUL;
  1612. } else if (unicode == 2) { /* Local is UTF-8 */
  1613. x = u_to_b((CHAR)c);
  1614. if (x < 0)
  1615. continue;
  1616. outxbuf[0] = (unsigned)(x & 0xff);
  1617. outxcount = 1;
  1618. outxbuf[outxcount] = NUL;
  1619. } else {
  1620. #endif /* UNICODE */
  1621. if (sxo) c = (*sxo)((char)c); /* Local-intermediate */
  1622. if (rxo) c = (*rxo)((char)c); /* Intermediate-remote */
  1623. outxbuf[0] = c;
  1624. outxcount = 1;
  1625. outxbuf[outxcount] = NUL;
  1626. #ifdef UNICODE
  1627. }
  1628. #endif /* UNICODE */
  1629. } else {
  1630. outxbuf[0] = c;
  1631. outxcount = 1;
  1632. outxbuf[outxcount] = NUL;
  1633. }
  1634. if (escseq)
  1635. apcrc = chkaes((char)c,1);
  1636. #else /* NOCSETS */
  1637. outxbuf[0] = c;
  1638. outxcount = 1;
  1639. outxbuf[outxcount] = NUL;
  1640. #endif /* NOCSETS */
  1641. debug(F111,"OUTXBUF",outxbuf,outxcount);
  1642. for (i = 0; i < outxcount; i++) {
  1643. c = outxbuf[i];
  1644. /*
  1645. If Shift-In/Shift-Out is selected and we have a 7-bit connection,
  1646. handle shifting here.
  1647. */
  1648. if (sosi) { /* Shift-In/Out selected? */
  1649. if (cmask == 0177) { /* In 7-bit environment? */
  1650. if (c & 0200) { /* 8-bit character? */
  1651. if (outshift == 0) { /* If not shifted, */
  1652. ttoc(dopar(SO)); /* shift. */
  1653. outshift = 1;
  1654. }
  1655. } else {
  1656. if (outshift == 1) { /* 7-bit character */
  1657. ttoc(dopar(SI)); /* If shifted, */
  1658. outshift = 0; /* unshift. */
  1659. }
  1660. }
  1661. }
  1662. if (c == SO) outshift = 1; /* User typed SO */
  1663. if (c == SI) outshift = 0; /* User typed SI */
  1664. }
  1665. c &= cmask; /* Apply Kermit-to-host mask now. */
  1666. if (c == '\015') { /* Carriage Return */
  1667. int stuff = -1;
  1668. if (tnlm) { /* TERMINAL NEWLINE ON */
  1669. stuff = LF; /* Stuff LF */
  1670. #ifdef TNCODE
  1671. } else if (network && /* TELNET NEWLINE ON/OFF/RAW */
  1672. IS_TELNET()) {
  1673. switch (!TELOPT_ME(TELOPT_BINARY) ? tn_nlm : tn_b_nlm){
  1674. case TNL_CRLF:
  1675. stuff = LF;
  1676. break;
  1677. case TNL_CRNUL:
  1678. stuff = NUL;
  1679. break;
  1680. }
  1681. #endif /* TNCODE */
  1682. }
  1683. if (stuff > -1) {
  1684. ttoc(dopar('\015')); /* Send CR */
  1685. if (duplex) conoc('\015'); /* Maybe echo CR */
  1686. c = stuff; /* Char to stuff */
  1687. csave = c;
  1688. }
  1689. }
  1690. #ifdef TNCODE
  1691. /* If user types the 0xff character (TELNET IAC), it must be doubled. */
  1692. else /* Not CR */
  1693. if ((dopar((CHAR) c) == IAC) && /* IAC (0xff) */
  1694. network && IS_TELNET()) { /* Send one now */
  1695. ttoc((char)IAC); /* and the other one just below. */
  1696. }
  1697. #endif /* TNCODE */
  1698. /* Send the character */
  1699. x = ttoc((char)dopar((CHAR) c));
  1700. if (x > -1) {
  1701. #ifdef CKLEARN
  1702. if (learning) { /* Learned script active */
  1703. if (crflag) { /* User typed CR */
  1704. learnchar(CR); /* Handle CR */
  1705. learnst = 0; /* Shift to Neutral */
  1706. } else {
  1707. learnchar(c); /* Not CR */
  1708. learnst = 2; /* Change state to Keyboard */
  1709. }
  1710. }
  1711. #endif /* CKLEARN */
  1712. if (duplex) { /* If half duplex, must echo */
  1713. if (debses)
  1714. conol(dbchr(csave)); /* the original char */
  1715. else /* not the translated one */
  1716. conoc((char)csave);
  1717. if (seslog) { /* And maybe log it too */
  1718. c2 = csave;
  1719. if (sessft == 0 && csave == '\r')
  1720. c2 = '\n';
  1721. LOGCHAR((char)c2);
  1722. }
  1723. }
  1724. } else {
  1725. perror("\r\nCan't send character");
  1726. cx_status = CSX_IOERROR;
  1727. active = 0;
  1728. break;
  1729. }
  1730. }
  1731. }
  1732. if (FD_ISSET(ttyfd, &out)) {
  1733. FD_CLR(ttyfd, &out);
  1734. }
  1735. while (gotnet > 0 || ibc > 0) {
  1736. gotnet = 0;
  1737. prev = c;
  1738. c = ckcgetc(0); /* Get next character */
  1739. if (c < 0) { /* Failed... */
  1740. ckcputf(); /* Flush CONNECT output buffer */
  1741. if (msgflg) {
  1742. printf("\r\nCommunications disconnect ");
  1743. #ifdef COMMENT
  1744. if (c == -3
  1745. #ifdef ultrix
  1746. /* This happens on Ultrix if there's no carrier */
  1747. && errno != EIO
  1748. #endif /* ultrix */
  1749. #ifdef UTEK
  1750. /* This happens on UTEK if there's no carrier */
  1751. && errno != EWOULDBLOCK
  1752. #endif /* UTEK */
  1753. )
  1754. perror("\r\nCan't read character");
  1755. #endif /* COMMENT */
  1756. }
  1757. #ifdef NOSETBUF
  1758. fflush(stdout);
  1759. #endif /* NOSETBUF */
  1760. dologend();
  1761. tthang(); /* Hang up the connection */
  1762. debug(F111,"CONNECT i/o error 1",ck_errstr(),errno);
  1763. cx_status = CSX_HOSTDISC;
  1764. goto conret0;
  1765. }
  1766. #ifdef TNCODE
  1767. tx = 0;
  1768. if ((c == NUL) && network && IS_TELNET()) {
  1769. if (prev == CR) { /* Discard <NUL> of <CR><NUL> if peer */
  1770. if (!TELOPT_U(TELOPT_BINARY)) { /* not in binary mode */
  1771. debug(F111,"CONNECT NUL",ckitoa(prev),c);
  1772. ckcputf(); /* Flush screen output buffer */
  1773. break;
  1774. }
  1775. }
  1776. }
  1777. debug(F111,"CONNECT","c",c);
  1778. debug(F111,"CONNECT","network",network);
  1779. debug(F111,"CONNECT","IS_TELNET",IS_TELNET());
  1780. if ((c == IAC) && network && IS_TELNET()) {
  1781. #ifdef CK_ENCRYPTION
  1782. int x_auth = TELOPT_ME(TELOPT_AUTHENTICATION);
  1783. #else
  1784. int x_auth = 0;
  1785. #endif /* CK_ENCRYPTION */
  1786. int me_bin = TELOPT_ME(TELOPT_BINARY);
  1787. int u_bin = TELOPT_U(TELOPT_BINARY);
  1788. debug(F100,"CONNECT got IAC","",0);
  1789. ckcputf(); /* Dump screen-output buffer */
  1790. if ((tx = tn_doop((CHAR)(c & 0xff),duplex,ckcgetc)) == 0) {
  1791. if (me_bin != TELOPT_ME(TELOPT_BINARY)) {
  1792. me_bin = TELOPT_ME(TELOPT_BINARY);
  1793. } else if (u_bin != TELOPT_U(TELOPT_BINARY)) {
  1794. u_bin = TELOPT_U(TELOPT_BINARY);
  1795. #ifdef CK_ENCRYPTION
  1796. /*
  1797. Here we have to push back any bytes we have read using block reads, so we
  1798. can read them again using single-character reads, so they can be decrypted
  1799. in case there was a switch to encryption in the block. Note that we can't
  1800. handle switches in the encryption state itself this way -- which would be
  1801. nice, since it would eliminate the need for single-character reads. Why?
  1802. Because if a series of characters has already been decrypted that shouldn't
  1803. have been, then (a) it's ruined, and (b) so is the state of the decryption
  1804. machine. Too bad.
  1805. */
  1806. } else if (TELOPT_ME(TELOPT_AUTHENTICATION) != 0 &&
  1807. TELOPT_ME(TELOPT_AUTHENTICATION) != x_auth
  1808. ) {
  1809. if (ttpushback((CHAR *)ibp,ibc) > -1) {
  1810. ibc = 0;
  1811. ibp = ibuf;
  1812. }
  1813. #endif /* CK_ENCRYPTION */
  1814. }
  1815. continue;
  1816. } else if (tx == -1) { /* I/O error */
  1817. if (msgflg)
  1818. printf("\r\nCommunications disconnect ");
  1819. #ifdef NOSETBUF
  1820. fflush(stdout);
  1821. #endif /* NOSETBUF */
  1822. dologend();
  1823. debug(F111,"CONNECT i/o error 2",ck_errstr(),errno);
  1824. cx_status = CSX_IOERROR;
  1825. goto conret0;
  1826. } else if (tx == -2) { /* I/O error */
  1827. if (msgflg)
  1828. printf("\r\nConnection closed by peer");
  1829. #ifdef NOSETBUF
  1830. fflush(stdout);
  1831. #endif /* NOSETBUF */
  1832. dologend();
  1833. debug(F111,"CONNECT i/o error 3",ck_errstr(),errno);
  1834. cx_status = CSX_IOERROR;
  1835. goto conret0;
  1836. } else if (tx == -3) { /* I/O error */
  1837. if (msgflg)
  1838. printf("\r\nConnection closed due to telnet policy");
  1839. #ifdef NOSETBUF
  1840. fflush(stdout);
  1841. #endif /* NOSETBUF */
  1842. dologend();
  1843. debug(F111,"CONNECT i/o error 4",ck_errstr(),errno);
  1844. cx_status = CSX_IOERROR;
  1845. goto conret0;
  1846. } else if ((tx == 1) && (!duplex)) { /* ECHO change */
  1847. duplex = 1; /* Turn on local echo */
  1848. continue;
  1849. } else if ((tx == 2) && (duplex)) { /* ECHO change */
  1850. duplex = 0;
  1851. continue;
  1852. } else if (tx == 3) { /* Quoted IAC */
  1853. c = parity ? 127 : 255;
  1854. }
  1855. #ifdef IKS_OPTION
  1856. else if (tx == 4) { /* IKS State Change */
  1857. if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start &&
  1858. !tcp_incoming
  1859. ) {
  1860. /* here we need to print a msg that the other */
  1861. /* side is in SERVER mode and that REMOTE */
  1862. /* commands should be used. And CONNECT mode */
  1863. /* should be ended. */
  1864. cx_status = CSX_IKSD;
  1865. active = 0;
  1866. }
  1867. }
  1868. #endif /* IKS_OPTION */
  1869. else if (tx == 6) {
  1870. /* DO LOGOUT was received */
  1871. if (msgflg)
  1872. printf("\r\nRemote Logout ");
  1873. #ifdef NOSETBUF
  1874. fflush(stdout);
  1875. #endif /* NOSETBUF */
  1876. debug(F100,"CONNECT Remote Logout","",0);
  1877. cx_status = CSX_TRIGGER;
  1878. goto conret0;
  1879. } else
  1880. continue; /* Negotiation OK, get next char. */
  1881. } else if (parity)
  1882. c &= 0x7f;
  1883. /* I'm echoing for the remote */
  1884. if (TELOPT_ME(TELOPT_ECHO) && tn_rem_echo)
  1885. ttoc((char)c);
  1886. #endif /* TNCODE */
  1887. #ifdef CKLEARN
  1888. /* Learned script: Record incoming chars if not in Keyboard state */
  1889. if (learning && learnst != 2) { /* Learned script active */
  1890. learnbuf[learnbp++] = c; /* Save for INPUT command */
  1891. if (learnbp >= LEARNBUFSIZ) /* in circular buffer */
  1892. learnbp = 0; /* wrapping if at end. */
  1893. learnbc++; /* Count this byte. */
  1894. learnst = 1; /* State is Net. */
  1895. }
  1896. #endif /* CKLEARN */
  1897. if (debses) { /* Output character to screen */
  1898. char *s; /* Debugging display... */
  1899. s = dbchr(c); /* Make char into string */
  1900. while (*s) { /* Output each char from string */
  1901. ckcputc(*s);
  1902. if (seslog) /* And maybe log it. */
  1903. LOGCHAR((char)*s);
  1904. s++;
  1905. }
  1906. } else { /* Regular display ... */
  1907. c &= cmask; /* Apply Kermit-to-remote mask */
  1908. if (seslog && sessft) /* If binary session log */
  1909. LOGCHAR((char)c); /* log the character now. */
  1910. #ifndef NOXFER
  1911. #ifdef CK_AUTODL
  1912. /*
  1913. Autodownload. Check for Kermit S packet prior to translation, since that
  1914. can change the packet and make it unrecognizable (as when the terminal
  1915. character set is an ISO 646 one)... Ditto for Zmodem start packet.
  1916. */
  1917. if (autodl /* Autodownload enabled? */
  1918. #ifdef IKS_OPTION
  1919. || TELOPT_SB(TELOPT_KERMIT).kermit.me_start
  1920. #endif /* IKS_OPTION */
  1921. ) {
  1922. int k = 0;
  1923. if (kstartactive || c == stchr /* Kermit S or I packet? */
  1924. #ifdef COMMENT
  1925. || adl_kmode == ADLSTR /* Not used in C-Kermit */
  1926. #endif /* COMMENT */
  1927. )
  1928. k = kstart((CHAR)c);
  1929. #ifdef CK_XYZ
  1930. if (!k && zmdlok) /* Or an "sz" start? */
  1931. k = zstart((CHAR)c);
  1932. #endif /* CK_XYZ */
  1933. if (k) {
  1934. int ksign = 0;
  1935. debug(F101,"CONNECT autodownload k","",k);
  1936. if (k < 0) { /* Minus-Protocol? */
  1937. #ifdef NOSERVER
  1938. goto noserver; /* Need server mode for this */
  1939. #else
  1940. ksign = 1; /* Remember */
  1941. k = 0 - k; /* Convert to actual protocol */
  1942. justone = 1; /* Flag for protocol module */
  1943. #endif /* NOSERVER */
  1944. } else
  1945. justone = 0;
  1946. k--; /* Adjust [kz]start's return value */
  1947. if (k == PROTO_K
  1948. #ifdef CK_XYZ
  1949. || k == PROTO_Z
  1950. #endif /* CK_XYZ */
  1951. ) {
  1952. /* Damage the packet so that it doesn't trigger */
  1953. /* autodownload detection downstream. */
  1954. if (k == PROTO_K) {
  1955. int i, len = strlen((char *)ksbuf);
  1956. for (i = 0; i < len; i++)
  1957. ckcputc(BS);
  1958. }
  1959. #ifdef CK_XYZ
  1960. else {
  1961. int i;
  1962. for (i = 0; i < 3; i++)
  1963. ckcputc(CAN);
  1964. }
  1965. #endif /* CK_XYZ */
  1966. #ifndef NOICP
  1967. /* sprintf is safe here (builtin keywords) */
  1968. sprintf(apcbuf,
  1969. "set proto %s, %s, set proto %s",
  1970. ptab[k].p_name,
  1971. ksign ? "server" : "receive",
  1972. ptab[protocol].p_name
  1973. );
  1974. apclength = strlen(apcbuf);
  1975. debug(F111,"CONNECT ksbuf",ksbuf,k);
  1976. debug(F110,"CONNECT autodownload",apcbuf,0);
  1977. apcactive = APC_LOCAL;
  1978. ckcputf(); /* Force screen update */
  1979. cx_status = CSX_APC;
  1980. goto conret1;
  1981. #else
  1982. /*
  1983. Here's another way that doesn't require APC, but then we'll have to change
  1984. all the other CONNECT modules, and then the mainline code that calls them.
  1985. */
  1986. {
  1987. extern char sstate;
  1988. sstate = ksign ? 'x' : 'v';
  1989. proto();
  1990. }
  1991. #endif /* NOICP */
  1992. }
  1993. }
  1994. }
  1995. #ifdef NOSERVER
  1996. noserver:
  1997. #endif /* NOSERVER */
  1998. #endif /* CK_AUTODL */
  1999. #endif /* NOXFER */
  2000. if (sosi) { /* Handle SI/SO */
  2001. if (c == SO) { /* Shift Out */
  2002. inshift = 1;
  2003. continue;
  2004. } else if (c == SI) { /* Shift In */
  2005. inshift = 0;
  2006. continue;
  2007. }
  2008. if (inshift) c |= 0200;
  2009. }
  2010. inxbuf[0] = c; /* In case there is no translation */
  2011. inxcount = 1; /* ... */
  2012. #ifndef NOCSETS
  2013. if (inesc[0] == ES_NORMAL /* If not in an escape sequence */
  2014. && !printing /* and not in transparent print */
  2015. ) { /* Translate character sets */
  2016. #ifdef UNICODE
  2017. int x;
  2018. if (unicode == 1) { /* Remote is UTF-8 */
  2019. x = u_to_b((CHAR)c);
  2020. if (x == -1)
  2021. continue;
  2022. else if (x == -2) { /* LS or PS */
  2023. inxbuf[0] = CR;
  2024. inxbuf[1] = LF;
  2025. inxcount = 2;
  2026. } else if (x == -9) { /* UTF-8 error */
  2027. inxbuf[0] = '?';
  2028. inxbuf[1] = u_to_b2();
  2029. inxcount = 2;
  2030. } else {
  2031. inxbuf[0] = (unsigned)(x & 0xff);
  2032. }
  2033. c = inxbuf[0];
  2034. } else if (unicode == 2) { /* Local is UTF-8 */
  2035. inxcount = b_to_u((CHAR)c,inxbuf,OUTXBUFSIZ,tcssize);
  2036. c = inxbuf[0];
  2037. } else {
  2038. #endif /* UNICODE */
  2039. if (sxi) c = (*sxi)((CHAR)c);
  2040. if (rxi) c = (*rxi)((CHAR)c);
  2041. inxbuf[0] = c;
  2042. #ifdef UNICODE
  2043. }
  2044. #endif /* UNICODE */
  2045. }
  2046. #endif /* NOCSETS */
  2047. #ifndef NOESCSEQ
  2048. if (escseq) { /* If handling escape sequences */
  2049. oldprt = printing; /* remember printer state */
  2050. apcrc = chkaes((char)c,0); /* and update escseq state. */
  2051. if (printing && !oldprt) /* If printer was turned on */
  2052. continue; /* don't print final char of escseq */
  2053. }
  2054. #ifdef CK_APC
  2055. /*
  2056. If we are handling APCs, we have several possibilities at this point:
  2057. 1. Ordinary character to be written to the screen.
  2058. 2. An Esc; we can't write it because it might be the beginning of an APC.
  2059. 3. The character following an Esc, in which case we write Esc, then char,
  2060. but only if we have not just entered an APC sequence.
  2061. */
  2062. if (escseq && (apcstatus & APC_ON)) {
  2063. if (inesc[0] == ES_GOTESC) /* Don't write ESC yet */
  2064. continue;
  2065. else if (oldesc[0] == ES_GOTESC && !apcactive) {
  2066. ckcputc(ESC); /* Write saved ESC */
  2067. if (seslog && !sessft) LOGCHAR((char)ESC);
  2068. } else if (apcrc) { /* We have an APC */
  2069. debug(F111,"CONNECT APC complete",apcbuf,apclength);
  2070. ckcputf(); /* Force screen update */
  2071. cx_status = CSX_APC;
  2072. goto conret1;
  2073. }
  2074. }
  2075. #endif /* CK_APC */
  2076. #endif /* NOESCSEQ */
  2077. debug(F111,"INXBUF",inxbuf,inxcount);
  2078. for (i = 0; i < inxcount; i++) { /* Loop thru */
  2079. c = inxbuf[i]; /* input expansion buffer... */
  2080. if (
  2081. #ifdef CK_APC
  2082. !apcactive && /* Don't display APC sequences */
  2083. #endif /* CK_APC */
  2084. !printing /* or transparent print material */
  2085. ) {
  2086. c &= cmdmsk; /* Apply command mask. */
  2087. /* Handle bare carriage returns and linefeeds */
  2088. if (c == CR && tt_crd) { /* SET TERM CR-DISPLA CRLF? */
  2089. ckcputc(c); /* Yes, output CR */
  2090. if (seslog && !sessft) LOGCHAR((char)c);
  2091. c = LF; /* and insert a linefeed */
  2092. }
  2093. if (c == LF && tt_lfd) { /* SET TERM CR-DISPLA CRLF? */
  2094. ckcputc(CR); /* Yes, output CR */
  2095. if (seslog && !sessft) LOGCHAR((char)CR);
  2096. }
  2097. #ifndef NOESCSEQ
  2098. if (dontprint) { /* Do transparent printing. */
  2099. dontprint = 0;
  2100. continue;
  2101. } else
  2102. #endif /* NOESCSEQ */
  2103. ckcputc(c); /* Write character to screen */
  2104. }
  2105. if (seslog && !sessft) { /* Handle session log. */
  2106. LOGCHAR((char)c);
  2107. }
  2108. #ifdef XPRINT
  2109. if (printing && !inesc[0]) {
  2110. /* zchout() can't be used because */
  2111. /* it's buffered differently. */
  2112. cbuf[0] = c;
  2113. zsoutx(ZMFILE,(char *)cbuf,1);
  2114. }
  2115. #endif /* XPRINT */
  2116. #ifdef CK_TRIGGER
  2117. /* Check for trigger string */
  2118. if (tt_trigger[0]) {
  2119. int i;
  2120. if ((i = autoexitchk((CHAR)c)) > -1) {
  2121. makestr(&triggerval,tt_trigger[i]);
  2122. ckcputf(); /* Force screen update */
  2123. #ifdef NOSETBUF
  2124. fflush(stdout); /* I mean really force it */
  2125. #endif /* NOSETBUF */
  2126. cx_status = CSX_TRIGGER;
  2127. goto conret1;
  2128. }
  2129. }
  2130. #endif /* CK_TRIGGER */
  2131. }
  2132. }
  2133. }
  2134. #ifndef BEBOX
  2135. if (FD_ISSET(scrnout, &out)) {
  2136. FD_CLR(scrnout, &out);
  2137. }
  2138. #endif /* BEBOX */
  2139. } /* End of big loop */
  2140. conret1: /* Come here to succeed */
  2141. rc = 1;
  2142. conret0: /* Common exit point */
  2143. #ifdef BEBOX
  2144. {
  2145. long ret_val;
  2146. closesocket(pair[0]);
  2147. closesocket(pair[1]);
  2148. x = kill(tid,SIGKILLTHR); /* Kill thread */
  2149. wait_for_thread (tid, &ret_val);
  2150. }
  2151. #endif /* BEBOX */
  2152. #ifdef CKLEARN
  2153. if (learning && learnfp)
  2154. fputs("\n",learnfp);
  2155. #endif /* CKLEARN */
  2156. conres();
  2157. if (dohangup > 0) {
  2158. #ifdef NETCONN
  2159. if (network
  2160. #ifdef TNCODE
  2161. && !TELOPT_ME(TELOPT_COMPORT)
  2162. #endif /* TNCODE */
  2163. )
  2164. ttclos(0);
  2165. #endif /* NETCONN */
  2166. #ifndef COMMENT
  2167. /*
  2168. This is bad because if they said SET MODEM HANGUP-METHOD MODEM-COMMAND,
  2169. they mean it -- we shouldn't fall back on tthang() if mdmhup() fails,
  2170. because maybe they have some special kind of connection. On the other
  2171. hand, making this change prevents dialing from working at all in some
  2172. cases. Further study needed.
  2173. */
  2174. #ifndef NODIAL
  2175. if (dohangup > 1) /* User asked for it */
  2176. if (mdmhup() < 1) /* Maybe hang up via modem */
  2177. #endif /* NODIAL */
  2178. tthang(); /* And make sure we don't hang up */
  2179. #else
  2180. if (!network) { /* Serial connection. */
  2181. #ifndef NODIAL
  2182. if (dialmhu) /* Hang up the way they said to. */
  2183. mdmhup();
  2184. else
  2185. #endif /* NODIAL */
  2186. tthang();
  2187. }
  2188. #endif /* COMMENT */
  2189. dologend();
  2190. dohangup = 0; /* again unless requested again. */
  2191. }
  2192. if (quitnow) /* Exit now if requested. */
  2193. doexit(GOOD_EXIT,xitsta);
  2194. if (msgflg
  2195. #ifdef CK_APC
  2196. && !apcactive
  2197. #endif /* CK_APC */
  2198. )
  2199. printf("(Back at %s)", *myhost ? myhost : "local UNIX system");
  2200. #ifdef CK_APC
  2201. if (!apcactive)
  2202. #endif /* CK_APC */
  2203. printf("\n");
  2204. what = W_NOTHING; /* So console modes set right. */
  2205. #ifndef NOCSETS
  2206. language = langsv; /* Restore language */
  2207. #endif /* NOCSETS */
  2208. #ifdef CK_APC
  2209. debug(F101,"CONNECT exit apcactive","",apcactive);
  2210. debug(F101,"CONNECT exit justone","",justone);
  2211. #endif /* CK_APC */
  2212. if (msgflg) {
  2213. #ifdef CK_APC
  2214. if (apcactive == APC_LOCAL)
  2215. printf("\n");
  2216. #endif /* CK_APC */
  2217. printf("----------------------------------------------------\n");
  2218. printbar = 1;
  2219. } else
  2220. printbar = 0;
  2221. fflush(stdout);
  2222. return(rc);
  2223. }
  2224. /* H C O N N E -- Give help message for connect. */
  2225. #define CXM_SER 1 /* Serial connections only */
  2226. #define CXM_NET 2 /* Network only (but not Telnet) */
  2227. #define CXM_TEL 4 /* Telnet only */
  2228. static struct hmsgtab {
  2229. char * hmsg;
  2230. int hflags;
  2231. } hlpmsg[] = {
  2232. {" ? or H for this message", 0},
  2233. {" 0 (zero) to send the NUL (0) character", 0},
  2234. {" B to send a BREAK signal (0.275sec)", CXM_SER},
  2235. #ifdef NETCONN
  2236. {" B to send a network BREAK", CXM_NET},
  2237. {" B to send a Telnet BREAK", CXM_TEL},
  2238. #endif /* NETCONN */
  2239. #ifdef CK_LBRK
  2240. {" L to send a Long BREAK (1.5sec)", CXM_SER},
  2241. #endif /* CK_LBRK */
  2242. #ifdef NETCONN
  2243. {" I to send a network interrupt packet", CXM_NET},
  2244. {" I to send a Telnet Interrupt request", CXM_TEL},
  2245. #ifdef TNCODE
  2246. {" A to send Telnet Are-You-There?", CXM_TEL},
  2247. #endif /* TNCODE */
  2248. #endif /* NETCONN */
  2249. {" U to hangup and close the connection", 0},
  2250. {" Q to hangup and quit Kermit", 0},
  2251. {" S for status", 0},
  2252. #ifdef NOPUSH
  2253. {" ! to push to local shell (disabled)", 0},
  2254. {" Z to suspend (disabled)", 0},
  2255. #else
  2256. {" ! to push to local shell", 0},
  2257. #ifdef NOJC
  2258. {" Z to suspend (disabled)", 0},
  2259. #else
  2260. {" Z to suspend", 0},
  2261. #endif /* NOJC */
  2262. #endif /* NOPUSH */
  2263. {" \\ backslash code:", 0},
  2264. {" \\nnn decimal character code", 0},
  2265. {" \\Onnn octal character code", 0},
  2266. {" \\Xhh hexadecimal character code;", 0},
  2267. {" terminate with Carriage Return.", 0},
  2268. {" Type the escape character again to send the escape character itself,",
  2269. 0},
  2270. {" or press the space-bar to resume the CONNECT session.", 0},
  2271. {NULL, 0}
  2272. };
  2273. int
  2274. hconne() {
  2275. int c, i, cxtype;
  2276. if (network)
  2277. cxtype = IS_TELNET() ? CXM_TEL : CXM_NET;
  2278. else
  2279. cxtype = CXM_SER;
  2280. conol("\r\n----------------------------------------------------\r\n");
  2281. conoll("Press:");
  2282. conol(" C to return to ");
  2283. conoll(*myhost ? myhost : "the C-Kermit prompt");
  2284. for (i = 0; hlpmsg[i].hmsg; i++) {
  2285. if (!(hlpmsg[i].hflags) || (hlpmsg[i].hflags == cxtype))
  2286. conoll(hlpmsg[i].hmsg);
  2287. }
  2288. conol("Press a key>"); /* Prompt for command. */
  2289. c = CONGKS() & 0177; /* Get character, strip any parity. */
  2290. /* No key mapping or translation here */
  2291. if (c != CMDQ)
  2292. conoll("");
  2293. conoll("----------------------------------------------------");
  2294. return(c); /* Return it. */
  2295. }
  2296. /* D O E S C -- Process an escape character argument */
  2297. VOID
  2298. #ifdef CK_ANSIC
  2299. doesc(char c)
  2300. #else
  2301. doesc(c) char c;
  2302. #endif /* CK_ANSIC */
  2303. /* doesc */ {
  2304. CHAR d;
  2305. debug(F101,"CONNECT doesc","",c);
  2306. while (1) {
  2307. if (c == escape) { /* Send escape character */
  2308. d = dopar((CHAR) c); ttoc((char) d); return;
  2309. } else /* Or else look it up below. */
  2310. if (isupper(c)) c = tolower(c);
  2311. switch(c) {
  2312. case 'c': /* Escape back to prompt */
  2313. case '\03':
  2314. cx_status = CSX_ESCAPE;
  2315. #ifdef NOICP
  2316. conoll("");
  2317. conoll("");
  2318. conoll(
  2319. " WARNING: This version of C-Kermit has no command processor to escape"
  2320. );
  2321. conoll(
  2322. " back to. To return to your local system, log out from the remote and/or"
  2323. );
  2324. conoll(
  2325. " use the escape character followed by the letter U to close (hang Up) the"
  2326. );
  2327. conoll(
  2328. " connection. Resuming your session..."
  2329. );
  2330. conoll("");
  2331. return;
  2332. #else
  2333. active = 0; conol("\r\n"); return;
  2334. #endif /* NOICP */
  2335. case 'b': /* Send a BREAK signal */
  2336. case '\02':
  2337. #ifdef CKLEARN
  2338. learnchar(-7);
  2339. #endif /* CKLEARN */
  2340. ttsndb(); return;
  2341. #ifdef NETCONN
  2342. case 'i': /* Send Interrupt */
  2343. case '\011':
  2344. #ifdef TCPSOCKET
  2345. if (network && IS_TELNET()) { /* TELNET */
  2346. temp[0] = (CHAR) IAC; /* I Am a Command */
  2347. temp[1] = (CHAR) TN_IP; /* Interrupt Process */
  2348. temp[2] = NUL;
  2349. ttol((CHAR *)temp,2);
  2350. } else
  2351. #endif /* TCPSOCKET */
  2352. conoc(BEL);
  2353. return;
  2354. #ifdef TCPSOCKET
  2355. case 'a': /* "Are You There?" */
  2356. case '\01':
  2357. if (network && IS_TELNET()) {
  2358. temp[0] = (CHAR) IAC; /* I Am a Command */
  2359. temp[1] = (CHAR) TN_AYT; /* Are You There? */
  2360. temp[2] = NUL;
  2361. ttol((CHAR *)temp,2);
  2362. } else conoc(BEL);
  2363. return;
  2364. #endif /* TCPSOCKET */
  2365. #endif /* NETCONN */
  2366. #ifdef CK_LBRK
  2367. case 'l': /* Send a Long BREAK signal */
  2368. #ifdef CKLEARN
  2369. learnchar(-8);
  2370. #endif /* CKLEARN */
  2371. ttsndlb(); return;
  2372. #endif /* CK_LBRK */
  2373. case 'u': /* Hangup */
  2374. /* case '\010': */ /* No, too dangerous */
  2375. cx_status = CSX_USERDISC;
  2376. dohangup = 2; active = 0; conol("\r\nHanging up "); return;
  2377. case 'q': /* Quit */
  2378. cx_status = CSX_USERDISC;
  2379. dohangup = 2; quitnow = 1; active = 0; conol("\r\n"); return;
  2380. case 's': /* Status */
  2381. conoll("");
  2382. conoll("----------------------------------------------------");
  2383. #ifdef PTYORPIPE
  2384. if (ttpipe)
  2385. ckmakmsg(temp,TMPLEN," Pipe: \"",ttname,"\"",NULL);
  2386. else if (ttpty)
  2387. ckmakmsg(temp,TMPLEN," Pty: \"",ttname,"\"",NULL);
  2388. else
  2389. #endif /* PTYORPIPE */
  2390. ckmakmsg(temp,
  2391. TMPLEN,
  2392. " ",
  2393. (network ? "Host" : "Device"),
  2394. ": ",
  2395. ttname
  2396. );
  2397. conoll(temp);
  2398. /* The following sprintf's are safe, temp[] size is at least 200 */
  2399. if (!network && speed >= 0L) {
  2400. sprintf(temp,"Speed %ld", speed);
  2401. conoll(temp);
  2402. }
  2403. sprintf(temp," Terminal echo: %s", duplex ? "local" : "remote");
  2404. conoll(temp);
  2405. sprintf(temp," Terminal bytesize: %d", (cmask == 0177) ? 7 : 8);
  2406. conoll(temp);
  2407. sprintf(temp," Command bytesize: %d", (cmdmsk == 0177) ? 7 : 8);
  2408. conoll(temp);
  2409. if (hwparity)
  2410. sprintf(temp," Parity[hardware]: %s",parnam(hwparity));
  2411. else
  2412. sprintf(temp," Parity: %s", parnam(parity));
  2413. conoll(temp);
  2414. #ifndef NOXFER
  2415. sprintf(temp," Autodownload: %s", autodl ? "on" : "off");
  2416. conoll(temp);
  2417. #endif /* NOXFER */
  2418. ckmakmsg(temp, /* (would not be safe for sprintf) */
  2419. TMPLEN,
  2420. " Session log: ",
  2421. *sesfil ? sesfil : "(none)",
  2422. NULL,
  2423. NULL
  2424. );
  2425. conoll(temp);
  2426. #ifndef NOSHOW
  2427. if (!network) shomdm();
  2428. #endif /* NOSHOW */
  2429. #ifdef CKLOGDIAL
  2430. {
  2431. long z;
  2432. z = dologshow(0);
  2433. if (z > -1L) {
  2434. sprintf(temp," Elapsed time: %s",hhmmss(z));
  2435. conoll(temp);
  2436. }
  2437. }
  2438. #endif /* CKLOGDIAL */
  2439. conoll("----------------------------------------------------");
  2440. return;
  2441. case 'h': /* Help */
  2442. case '?': /* Help */
  2443. c = hconne(); continue;
  2444. case '0': /* Send a null */
  2445. c = '\0'; d = dopar((CHAR) c); ttoc((char) d); return;
  2446. case 'z': case '\032': /* Suspend */
  2447. #ifndef NOPUSH
  2448. if (!nopush)
  2449. stptrap(0);
  2450. else
  2451. conoc(BEL);
  2452. #else
  2453. conoc(BEL);
  2454. #endif /* NOPUSH */
  2455. return;
  2456. case '@': /* Start inferior command processor */
  2457. case '!':
  2458. #ifndef NOPUSH
  2459. if (!nopush) {
  2460. conres(); /* Put console back to normal */
  2461. zshcmd(""); /* Fork a shell. */
  2462. if (conbin((char)escape) < 0) {
  2463. cx_status = CSX_INTERNAL;
  2464. printf("Error resuming CONNECT session\n");
  2465. active = 0;
  2466. }
  2467. } else conoc(BEL);
  2468. #else
  2469. conoc(BEL);
  2470. #endif /* NOPUSH */
  2471. return;
  2472. case SP: /* Space, ignore */
  2473. return;
  2474. default: /* Other */
  2475. if (c == CMDQ) { /* Backslash escape */
  2476. int x;
  2477. ecbp = ecbuf;
  2478. *ecbp++ = c;
  2479. while (((c = (CONGKS() & cmdmsk)) != '\r') && (c != '\n'))
  2480. *ecbp++ = c;
  2481. *ecbp = NUL; ecbp = ecbuf;
  2482. x = xxesc(&ecbp); /* Interpret it */
  2483. if (x >= 0) { /* No key mapping here */
  2484. c = dopar((CHAR) x);
  2485. ttoc((char) c);
  2486. return;
  2487. } else { /* Invalid backslash code. */
  2488. conoc(BEL);
  2489. return;
  2490. }
  2491. }
  2492. conoc(BEL); return; /* Invalid esc arg, beep */
  2493. }
  2494. }
  2495. }
  2496. #endif /* NOLOCAL */