ckucon.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691
  1. #include "ckcsym.h"
  2. char *connv = "CONNECT Command for UNIX:fork(), 9.0.117, 14 Jul 2011";
  3. /* C K U C O N -- 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, 2011,
  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. NOTE: This module has been superseded on most platforms by ckucns.c, which
  12. uses select() rather than fork() for multiplexing its i/o. This module
  13. is still needed for platforms that do not support select(), and also for
  14. its X.25 support. Although the two modules share large amounts of code,
  15. their structure is radically different and therefore attempts at merging
  16. them have so far been unsuccessful. (November 1998.)
  17. Special thanks to Eduard Vopicka, Prague University of Economics,
  18. Czech Republic, for valuable contributions to this module in July 1994,
  19. and to Neal P. Murphy of the Motorola Cellular Infrastructure Group in 1996
  20. for rearranging the code to allow operation on the BeBox, yet still work
  21. in regular UNIX.
  22. */
  23. #include "ckcdeb.h" /* Common things first */
  24. #ifndef NOLOCAL
  25. #ifdef BEOSORBEBOX
  26. static double time_started = 0.0;
  27. #include <kernel/OS.h>
  28. _PROTOTYP( static long concld, (void *) );
  29. #else
  30. _PROTOTYP( static VOID concld, (void) );
  31. #endif /* BEOSORBEBOX */
  32. #ifdef NEXT
  33. #undef NSIG
  34. #include <sys/wait.h> /* For wait() */
  35. #endif /* NEXT */
  36. #include <signal.h> /* Signals */
  37. #ifndef HPUXPRE65
  38. #include <errno.h> /* Error number symbols */
  39. #else
  40. #ifndef ERRNO_INCLUDED
  41. #include <errno.h> /* Error number symbols */
  42. #endif /* ERRNO_INCLUDED */
  43. #endif /* HPUXPRE65 */
  44. #ifdef ZILOG /* Longjumps */
  45. #include <setret.h>
  46. #else
  47. #include <setjmp.h>
  48. #endif /* ZILOG */
  49. #include "ckcsig.h"
  50. /* Kermit-specific includes */
  51. #include "ckcasc.h" /* ASCII characters */
  52. #include "ckcker.h" /* Kermit things */
  53. #include "ckucmd.h" /* For xxesc() prototype */
  54. #include "ckcnet.h" /* Network symbols */
  55. #ifndef NOCSETS
  56. #include "ckcxla.h" /* Character set translation */
  57. #endif /* NOCSETS */
  58. /* Internal function prototypes */
  59. _PROTOTYP( VOID ttflux, (void) );
  60. _PROTOTYP( VOID doesc, (char) );
  61. _PROTOTYP( VOID logchar, (char) );
  62. _PROTOTYP( int hconne, (void) );
  63. #ifndef NOSHOW
  64. _PROTOTYP( VOID shomdm, (void) );
  65. #endif /* NOSHOW */
  66. _PROTOTYP( static int kbget, (void) );
  67. _PROTOTYP( static int pipemsg, (int) );
  68. _PROTOTYP( static int ckcputf, (void) );
  69. _PROTOTYP( static VOID ck_sndmsg, (void) );
  70. /*
  71. For inter-fork signaling. Normally we use SIGUSR1, except on SCO, where
  72. we use SIGUSR2 because SIGUSR1 is used by the system. You can define
  73. CK_FORK_SIG to be whatever other signal you might want to use at compile
  74. time. We don't use SIGUSR2 everywhere because in some systems, like
  75. UnixWare, the default action for SIGUSR2 is to kill the process that gets it.
  76. */
  77. #ifndef CK_FORK_SIG
  78. #ifndef SIGUSR1 /* User-defined signals */
  79. #define SIGUSR1 30
  80. #endif /* SIGUSR1 */
  81. #ifndef SIGUSR2
  82. #define SIGUSR2 31
  83. #endif /* SIGUSR2 */
  84. #ifdef M_UNIX
  85. #define CK_FORK_SIG SIGUSR2 /* SCO - use SIGUSR2 */
  86. #else
  87. #define CK_FORK_SIG SIGUSR1 /* Others - use SIGUSR1 */
  88. #endif /* M_UNIX */
  89. #endif /* CK_FORK_SIG */
  90. /* External variables */
  91. extern struct ck_p ptab[];
  92. extern int local, escape, duplex, parity, flow, seslog, sessft, debses,
  93. mdmtyp, ttnproto, cmask, cmdmsk, network, nettype, deblog, sosi, tnlm,
  94. xitsta, what, ttyfd, ttpipe, quiet, backgrd, pflag, tt_crd, tt_lfd,
  95. tn_nlm, ttfdflg,
  96. tt_escape, justone, carrier, hwparity;
  97. extern long speed;
  98. extern char ttname[], sesfil[], myhost[], *ccntab[];
  99. #ifdef TNCODE
  100. extern int tn_b_nlm, tn_rem_echo;
  101. #endif /* TNCODE */
  102. #ifdef CK_TRIGGER
  103. extern char * tt_trigger[], * triggerval;
  104. #endif /* CK_TRIGGER */
  105. extern int nopush;
  106. #ifdef CK_APC
  107. extern int apcactive; /* Application Program Command (APC) */
  108. extern int apcstatus; /* items ... */
  109. static int apclength = 0;
  110. #ifdef DCMDBUF
  111. extern char *apcbuf;
  112. #else
  113. extern char apcbuf[];
  114. #endif /* DCMDBUF */
  115. static int apcbuflen = APCBUFLEN - 2;
  116. extern int protocol; /* Auto download */
  117. #endif /* CK_APC */
  118. extern int autodl;
  119. #ifdef CK_AUTODL
  120. extern CHAR ksbuf[];
  121. #endif /* CK_AUTODL */
  122. #ifdef CK_XYZ
  123. #ifdef XYZ_INTERNAL
  124. static int zmdlok = 1; /* Zmodem autodownloads available */
  125. #else
  126. static int zmdlok = 0; /* Depends on external protocol def */
  127. #endif /* XYZ_INTERNAL */
  128. #else
  129. static int zmdlok = 0; /* Not available at all */
  130. #endif /* CK_XYZ */
  131. #ifndef NOSETKEY /* Keyboard mapping */
  132. extern KEY *keymap; /* Single-character key map */
  133. extern MACRO *macrotab; /* Key macro pointer table */
  134. static MACRO kmptr = NULL; /* Pointer to current key macro */
  135. #endif /* NOSETKEY */
  136. /* Global variables local to this module */
  137. static int
  138. quitnow = 0, /* <esc-char>Q was typed */
  139. jbset = 0, /* Flag whether jmp buf is set. */
  140. dohangup = 0, /* <esc-char>H was typed */
  141. sjval, /* Setjump return value */
  142. goterr = 0, /* Fork/pipe creation error flag */
  143. inshift = 0, /* SO/SI shift states */
  144. outshift = 0;
  145. int active = 0; /* Lower fork active flag */
  146. static PID_T parent_id = (PID_T)0; /* Process ID of keyboard fork */
  147. static char ecbuf[10], *ecbp; /* Escape char buffer & pointer */
  148. #ifdef CK_SMALL
  149. #define IBUFL 1536 /* Input buffer length */
  150. #else
  151. #define IBUFL 4096
  152. #endif /* CK_SMALL */
  153. static int obc = 0; /* Output buffer count */
  154. #ifndef OXOS
  155. #define OBUFL 1024 /* Output buffer length */
  156. #else
  157. #define OBUFL IBUFL
  158. #endif /* OXOS */
  159. #ifdef BIGBUFOK
  160. #define TMPLEN 4096 /* Temporary message buffer length */
  161. #else
  162. #define TMPLEN 200
  163. #endif /* BIGBUFOK */
  164. #ifdef DYNAMIC
  165. static char *ibuf = NULL, *obuf = NULL, *temp = NULL; /* Buffers */
  166. #else
  167. static char ibuf[IBUFL], obuf[OBUFL], temp[TMPLEN];
  168. #endif /* DYNAMIC */
  169. #ifdef DYNAMIC
  170. static char *ibp; /* Input buffer pointer */
  171. #else
  172. static char *ibp = ibuf; /* Input buffer pointer */
  173. #endif /*DYNAMIC */
  174. static int ibc = 0; /* Input buffer count */
  175. #ifdef DYNAMIC
  176. static char *obp; /* Output buffer pointer */
  177. #else
  178. static char *obp = obuf; /* Output buffer pointer */
  179. #endif /* DYNAMIC */
  180. /* X.25 items */
  181. #ifdef ANYX25
  182. static char *p; /* General purpose pointer */
  183. char x25ibuf[MAXIX25]; /* Input buffer */
  184. char x25obuf[MAXOX25]; /* Output buffer */
  185. int ibufl; /* Length of input buffer */
  186. int obufl; /* Length of output buffer */
  187. unsigned char tosend = 0;
  188. int linkid, lcn;
  189. static int dox25clr = 0;
  190. #ifndef IBMX25
  191. extern CHAR padparms[];
  192. #endif /* IBMX25 */
  193. #endif /* ANYX25 */
  194. static int xpipe[2] = {-1, -1}; /* Pipe descriptor for child-parent messages */
  195. static PID_T pid = (PID_T) 0; /* Process ID of child */
  196. /* Character-set items */
  197. static int unicode = 0;
  198. static int escseq = 0; /* 1 = Recognizer is active */
  199. int inesc = 0; /* State of sequence recognizer */
  200. int oldesc = -1; /* Previous state of recognizer */
  201. #define OUTXBUFSIZ 15
  202. static CHAR inxbuf[OUTXBUFSIZ+1]; /* Host-to-screen expansion buffer */
  203. static int inxcount = 0; /* and count */
  204. static CHAR outxbuf[OUTXBUFSIZ+1]; /* Keyboard-to-host expansion buf */
  205. static int outxcount = 0; /* and count */
  206. #ifndef NOCSETS
  207. #ifdef CK_ANSIC /* ANSI C prototypes... */
  208. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* Character set */
  209. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(CHAR); /* translation functions */
  210. static CHAR (*sxo)(CHAR); /* Local translation functions */
  211. static CHAR (*rxo)(CHAR); /* for output (sending) terminal chars */
  212. static CHAR (*sxi)(CHAR); /* and for input (receiving) terminal chars. */
  213. static CHAR (*rxi)(CHAR);
  214. #else /* Not ANSI C... */
  215. extern CHAR (*xls[MAXTCSETS+1][MAXFCSETS+1])(); /* Character set */
  216. extern CHAR (*xlr[MAXTCSETS+1][MAXFCSETS+1])(); /* translation functions. */
  217. static CHAR (*sxo)(); /* Local translation functions */
  218. static CHAR (*rxo)(); /* for output (sending) terminal chars */
  219. static CHAR (*sxi)(); /* and for input (receiving) terminal chars. */
  220. static CHAR (*rxi)();
  221. #endif /* CK_ANSIC */
  222. extern int language; /* Current language. */
  223. static int langsv; /* For remembering language setting. */
  224. extern struct csinfo fcsinfo[]; /* File character set info. */
  225. extern int tcsr, tcsl; /* Terminal character sets, remote & local. */
  226. static int tcs; /* Intermediate ("transfer") character set. */
  227. static int tcssize = 0; /* Size of tcs */
  228. #ifdef UNICODE /* UTF-8 support */
  229. #ifdef CK_ANSIC
  230. extern int (*xl_ufc[MAXFCSETS+1])(USHORT); /* Unicode to FCS */
  231. extern USHORT (*xl_fcu[MAXFCSETS+1])(CHAR); /* FCS to Unicode */
  232. extern int (*xuf)(USHORT); /* Translation function UCS to FCS */
  233. extern USHORT (*xfu)(CHAR); /* Translation function FCS to UCS */
  234. #else
  235. extern int (*xl_ufc[MAXFCSETS+1])();
  236. extern USHORT (*xl_fcu[MAXFCSETS+1])();
  237. extern int (*xuf)();
  238. extern USHORT (*xfu)();
  239. #endif /* CK_ANSIC */
  240. #endif /* UNICODE */
  241. #endif /* NOCSETS */
  242. /*
  243. We do not need to parse and recognize escape sequences if we are being built
  244. without character-set support AND without APC support.
  245. */
  246. #ifdef NOCSETS /* No character sets */
  247. #ifndef CK_APC /* No APC */
  248. #ifndef NOESCSEQ
  249. #define NOESCSEQ /* So no escape sequence recognizer */
  250. #endif /* NOESCSEQ */
  251. #endif /* CK_APC */
  252. #endif /* NOCSETS */
  253. /* Child process events and messages */
  254. #define CEV_NO 0 /* No event */
  255. #define CEV_HUP 1 /* Communications hangup */
  256. #define CEV_PAD 2 /* X.25 - change PAD parameters */
  257. #define CEV_DUP 3 /* Toggle duplex */
  258. #define CEV_APC 4 /* Execute APC */
  259. #ifdef TNCODE
  260. #define CEV_MEBIN 5 /* Change of me_binary */
  261. #define CEV_UBIN 6 /* Change of u_binary */
  262. #endif /* TNCODE */
  263. #define CEV_ADL 7 /* Autodownload */
  264. #define CEV_AUL 8 /* Autoupload */
  265. #define CEV_TRI 9 /* Trigger string */
  266. #ifdef NOESCSEQ
  267. #define chkaes(x) 0
  268. #else
  269. /*
  270. As of edit 178, the CONNECT command skips past ANSI escape sequences to
  271. avoid translating the characters within them. This allows the CONNECT
  272. command to work correctly with a host that uses a 7-bit ISO 646 national
  273. character set, in which characters like '[' would normally be translated
  274. into accented characters, ruining the terminal's interpretation (and
  275. generation) of escape sequences.
  276. As of edit 190, the CONNECT command responds to APC escape sequences
  277. (ESC _ text ESC \) if the user SETs TERMINAL APC ON or UNCHECKED, and the
  278. program was built with CK_APC defined.
  279. Non-ANSI/ISO-compliant escape sequences are not handled.
  280. */
  281. /* States for the escape-sequence recognizer. */
  282. #define ES_NORMAL 0 /* Normal, not in an escape sequence */
  283. #define ES_GOTESC 1 /* Current character is ESC */
  284. #define ES_ESCSEQ 2 /* Inside an escape sequence */
  285. #define ES_GOTCSI 3 /* Inside a control sequence */
  286. #define ES_STRING 4 /* Inside DCS,OSC,PM, or APC string */
  287. #define ES_TERMIN 5 /* 1st char of string terminator */
  288. /*
  289. ANSI escape sequence handling. Only the 7-bit form is treated, because
  290. translation is not a problem in the 8-bit environment, in which all GL
  291. characters are ASCII and no translation takes place. So we don't check
  292. for the 8-bit single-character versions of CSI, DCS, OSC, APC, or ST.
  293. Here is the ANSI sequence recognizer state table, followed by the code
  294. that implements it.
  295. Definitions:
  296. CAN = Cancel 01/08 Ctrl-X
  297. SUB = Substitute 01/10 Ctrl-Z
  298. DCS = Device Control Sequence 01/11 05/00 ESC P
  299. CSI = Control Sequence Introducer 01/11 05/11 ESC [
  300. ST = String Terminator 01/11 05/12 ESC \
  301. OSC = Operating System Command 01/11 05/13 ESC ]
  302. PM = Privacy Message 01/11 05/14 ESC ^
  303. APC = Application Program Command 01/11 05/15 ESC _
  304. ANSI escape sequence recognizer:
  305. State Input New State ; Commentary
  306. NORMAL (start) ; Start in NORMAL state
  307. (any) CAN NORMAL ; ^X cancels
  308. (any) SUB NORMAL ; ^Z cancels
  309. NORMAL ESC GOTESC ; Begin escape sequence
  310. NORMAL other ; NORMAL control or graphic character
  311. GOTESC ESC ; Start again
  312. GOTESC [ GOTCSI ; CSI
  313. GOTESC P STRING ; DCS introducer, consume through ST
  314. GOTESC ] STRING ; OSC introducer, consume through ST
  315. GOTESC ^ STRING ; PM introducer, consume through ST
  316. GOTESC _ STRING ; APC introducer, consume through ST
  317. GOTESC 0..~ NORMAL ; 03/00 through 17/14 = Final character
  318. GOTESC other ESCSEQ ; Intermediate or ignored control character
  319. ESCSEQ ESC GOTESC ; Start again
  320. ESCSEQ 0..~ NORMAL ; 03/00 through 17/14 = Final character
  321. ESCSEQ other ; Intermediate or ignored control character
  322. GOTCSI ESC GOTESC ; Start again
  323. GOTCSI @..~ NORMAL ; 04/00 through 17/14 = Final character
  324. GOTCSI other ; Intermediate char or ignored control char
  325. STRING ESC TERMIN ; Maybe have ST
  326. STRING other ; Consume all else
  327. TERMIN \ NORMAL ; End of string
  328. TERMIN other STRING ; Still in string
  329. */
  330. /*
  331. chkaes() -- Check ANSI Escape Sequence.
  332. Call with EACH character in input stream.
  333. Sets global inesc variable according to escape sequence state.
  334. Returns 0 normally, 1 if an APC sequence is to be executed.
  335. */
  336. int
  337. #ifdef CK_ANSIC
  338. chkaes(char c)
  339. #else
  340. chkaes(c) char c;
  341. #endif /* CK_ANSIC */
  342. /* chkaes */ {
  343. oldesc = inesc; /* Remember previous state */
  344. if (c == CAN || c == SUB) /* CAN and SUB cancel any sequence */
  345. inesc = ES_NORMAL;
  346. else /* Otherwise */
  347. switch (inesc) { /* enter state switcher */
  348. case ES_NORMAL: /* NORMAL state */
  349. if (c == ESC) /* Got an ESC */
  350. inesc = ES_GOTESC; /* Change state to GOTESC */
  351. break; /* Otherwise stay in NORMAL state */
  352. case ES_GOTESC: /* GOTESC state */
  353. if (c == '[') /* Left bracket after ESC is CSI */
  354. inesc = ES_GOTCSI; /* Change to GOTCSI state */
  355. else if (c == 'P' || (c > 0134 && c < 0140)) { /* P, [, ^, or _ */
  356. inesc = ES_STRING; /* Switch to STRING-absorption state */
  357. #ifdef CK_APC
  358. if (c == '_' && pid == 0 && /* APC handled in child only */
  359. (apcstatus & APC_ON)) { /* and only if not disabled. */
  360. debug(F100,"CONNECT APC begin","",0);
  361. apcactive = APC_REMOTE; /* Set APC-Active flag */
  362. apclength = 0; /* and reset APC buffer pointer */
  363. }
  364. #endif /* CK_APC */
  365. } else if (c > 057 && c < 0177) /* Final character '0' thru '~' */
  366. inesc = ES_NORMAL; /* Back to normal */
  367. else if (c != ESC) /* ESC in an escape sequence... */
  368. inesc = ES_ESCSEQ; /* starts a new escape sequence */
  369. break; /* Intermediate or ignored ctrl char */
  370. case ES_ESCSEQ: /* ESCSEQ -- in an escape sequence */
  371. if (c > 057 && c < 0177) /* Final character '0' thru '~' */
  372. inesc = ES_NORMAL; /* Return to NORMAL state. */
  373. else if (c == ESC) /* ESC ... */
  374. inesc = ES_GOTESC; /* starts a new escape sequence */
  375. break; /* Intermediate or ignored ctrl char */
  376. case ES_GOTCSI: /* GOTCSI -- In a control sequence */
  377. if (c > 077 && c < 0177) /* Final character '@' thru '~' */
  378. inesc = ES_NORMAL; /* Return to NORMAL. */
  379. else if (c == ESC) /* ESC ... */
  380. inesc = ES_GOTESC; /* starts over. */
  381. break; /* Intermediate or ignored ctrl char */
  382. case ES_STRING: /* Inside a string */
  383. if (c == ESC) /* ESC may be 1st char of terminator */
  384. inesc = ES_TERMIN; /* Go see. */
  385. #ifdef CK_APC
  386. else if (apcactive && (apclength < apcbuflen)) /* If in APC, */
  387. apcbuf[apclength++] = c; /* deposit this character. */
  388. else { /* Buffer overrun */
  389. apcactive = 0; /* Discard what we got */
  390. apclength = 0; /* and go back to normal */
  391. apcbuf[0] = 0; /* Not pretty, but what else */
  392. inesc = ES_NORMAL; /* can we do? (ST might not come) */
  393. }
  394. #endif /* CK_APC */
  395. break; /* Absorb all other characters. */
  396. case ES_TERMIN: /* May have a string terminator */
  397. if (c == '\\') { /* which must be backslash */
  398. inesc = ES_NORMAL; /* If so, back to NORMAL */
  399. #ifdef CK_APC
  400. if (apcactive) { /* If it was an APC string, */
  401. debug(F101,"CONNECT APC terminated","",c);
  402. apcbuf[apclength] = NUL; /* terminate it and then ... */
  403. return(1);
  404. }
  405. #endif /* CK_APC */
  406. } else { /* Otherwise */
  407. inesc = ES_STRING; /* Back to string absorption. */
  408. #ifdef CK_APC
  409. if (apcactive && (apclength+1 < apcbuflen)) { /* In APC string */
  410. apcbuf[apclength++] = ESC; /* deposit the Esc character */
  411. apcbuf[apclength++] = c; /* and this character too */
  412. }
  413. #endif /* CK_APC */
  414. }
  415. }
  416. return(0);
  417. }
  418. #endif /* NOESCSEQ */
  419. /* Connect state parent/child communication signal handlers */
  420. /* Routines used by the child process */
  421. static int
  422. pipemsg(n) int n; { /* Send message ID to parent */
  423. int code = n & 255;
  424. return(write(xpipe[1], &code, sizeof(code)));
  425. }
  426. /* Environment pointer for CK_FORK_SIG signal handling in child... */
  427. #ifdef CK_POSIX_SIG
  428. static sigjmp_buf sig_env;
  429. #else
  430. static jmp_buf sig_env;
  431. #endif /* CK_POSIX_SIG */
  432. static SIGTYP /* CK_FORK_SIG handling in child ... */
  433. forkint(foo) int foo; {
  434. /* It is important to disable CK_FORK_SIG before longjmp */
  435. signal(CK_FORK_SIG, SIG_IGN); /* Set to ignore CK_FORK_SIG */
  436. debug(F100,"CONNECT forkint - CK_FORK_SIG", "", 0);
  437. /* Force return from ck_sndmsg() */
  438. cklongjmp(sig_env, 1);
  439. /* NOTREACHED */
  440. }
  441. static VOID
  442. ck_sndmsg() { /* Executed by child only ... */
  443. debug(F100,"CONNECT ck_sndmsg, active", "", active);
  444. if (
  445. #ifdef CK_POSIX_SIG
  446. sigsetjmp(sig_env,1)
  447. #else
  448. setjmp(sig_env)
  449. #endif /* CK_POSIX_SIG */
  450. == 0) {
  451. debug(F100,"CONNECT ck_sndmsg signaling parent","",0);
  452. signal(CK_FORK_SIG, forkint); /* Set up signal handler */
  453. kill(parent_id, CK_FORK_SIG); /* Kick the parent */
  454. debug(F100,"ck_sndmsg pausing","",0);
  455. for (;;) pause(); /* Wait for CK_FORK_SIG or SIGKILL */
  456. /* NOTREACHED */
  457. }
  458. /* We come here from forkint() via [sig]cklongjmp(sig_env,1) */
  459. debug(F100,"CONNECT ck_sndmsg is parent - returning", "", 0);
  460. }
  461. /* Routines used by the parent process */
  462. #ifdef CK_POSIX_SIG /* Environment pointer for CONNECT errors */
  463. static sigjmp_buf con_env;
  464. #else
  465. static jmp_buf con_env;
  466. #endif /* CK_POSIX_SIG */
  467. /*
  468. pipeint() handles CK_FORK_SIG signals from the lower (port) fork.
  469. It reads a function code from the pipe that connects the two forks,
  470. then reads additional data from the pipe, then handles it.
  471. */
  472. static SIGTYP
  473. pipeint(arg) int arg; { /* Dummy argument */
  474. int code, cx, x, i /* , n */ ;
  475. #ifndef NOCCTRAP
  476. extern ckjmpbuf cmjbuf;
  477. #endif /* NOCCTRAP */
  478. /*
  479. IMPORTANT: At this point, the child fork is waiting for CK_FORK_SIG
  480. (eventually for SIGKILL) inside of ck_sndmsg(). So we can't get any
  481. subsequent CK_FORK_SIG from child before we send it CK_FORK_SIG.
  482. */
  483. signal(CK_FORK_SIG, SIG_IGN); /* Ignore CK_FORK_SIG now */
  484. debug(F101,"CONNECT pipeint arg","",arg);
  485. read(xpipe[0], &code, sizeof(code)); /* Get function code from pipe */
  486. debug(F101,"CONNECT pipeint code","",code);
  487. cx = code & 255; /* 8-bit version of function code */
  488. #ifndef NOCCTRAP
  489. #ifndef NOICP
  490. #define USECCJMPBUF
  491. #endif /* NOICP */
  492. #endif /* NOCCTRAP */
  493. /*
  494. Read info passed back up to us by the lower fork, depending on the function
  495. requested. The same number of items must be read from the pipe in the same
  496. order as the lower fork put them there. Trying to read something that's not
  497. there makes the program hang uninterruptibly. Pay close attention -- notice
  498. how we fall through some of the cases rather than break; that's deliberate.
  499. */
  500. switch (cx) {
  501. #ifdef CK_TRIGGER
  502. case CEV_TRI: /* Trigger string */
  503. debug(F100,"CONNECT trigger","",0);
  504. read(xpipe[0], (char *)&i, sizeof(i)); /* Trigger index */
  505. debug(F101,"CONNECT trigger index","",i);
  506. makestr(&triggerval,tt_trigger[i]); /* Make a copy of the trigger */
  507. debug(F110,"CONNECT triggerval",triggerval,0);
  508. read(xpipe[0], (char *)&ibc, sizeof(ibc)); /* Copy child's */
  509. debug(F101,"CONNECT trigger ibc (upper)","",ibc); /* input buffer. */
  510. if (ibc > 0) {
  511. read(xpipe[0], (char *)&ibp, sizeof(ibp));
  512. read(xpipe[0], ibp, ibc);
  513. }
  514. /* Fall thru... */
  515. #endif /* CK_TRIGGER */
  516. case CEV_HUP:
  517. /*
  518. The CEV_HUP case is executed when the other side has hung up on us.
  519. In some cases, this happens before we have had a chance to execute the
  520. setjmp(con_env,1) call, and in that case we'd better not take the longjmp!
  521. A good example is when you TELNET to port 13 on the local host; it prints
  522. its asctime() string (26 chars) and then closes the connection.
  523. */
  524. #ifdef CK_TRIGGER
  525. if (cx == CEV_TRI)
  526. sjval = CEV_TRI; /* Set global variable. */
  527. else
  528. #endif /* CK_TRIGGER */
  529. sjval = CEV_HUP;
  530. if (jbset) { /* jmp_buf is initialized */
  531. cklongjmp(con_env,sjval); /* so do the right thing. */
  532. } else {
  533. int x = 0;
  534. #ifdef USECCJMPBUF
  535. /* jmp_buf not init'd yet a close approximation... */
  536. #ifdef CK_TRIGGER
  537. if (cx == CEV_HUP)
  538. #endif /* CK_TRIGGER */
  539. ttclos(0); /* Close our end of the connection */
  540. if (pid) {
  541. debug(F101,"CONNECT trigger killing pid","",pid);
  542. #ifdef BEOSORBEBOX
  543. {
  544. long ret_val;
  545. x = kill(pid,SIGKILLTHR); /* Kill lower fork */
  546. wait_for_thread (pid, &ret_val);
  547. }
  548. #else
  549. #ifdef Plan9
  550. x = kill(pid, SIGKILL); /* (should always use this really) */
  551. #else
  552. x = kill(pid,9); /* Kill lower fork (history) */
  553. #endif /* Plan9 */
  554. wait((WAIT_T *)0); /* Wait till gone. */
  555. if (x < 0) {
  556. printf("ERROR: Failure to kill pid %ld: %s, errno=%d\n",
  557. (long) pid, ck_errstr(), errno);
  558. debug(F111,"CONNECT error killing stale pid",
  559. ck_errstr(),errno);
  560. }
  561. pid = (PID_T) 0;
  562. #endif /* BEOSORBEBOX */
  563. }
  564. conres(); /* Reset the console. */
  565. if (!quiet) {
  566. printf("\r\n(Back at %s)\r\n",
  567. *myhost ? myhost :
  568. #ifdef UNIX
  569. "local UNIX system"
  570. #else
  571. "local system"
  572. #endif /* UNIX */
  573. );
  574. }
  575. what = W_NOTHING; /* So console modes are set right. */
  576. printf("\r\n"); /* prevent prompt-stomping */
  577. cklongjmp(cmjbuf,0); /* Do what the Ctrl-C handler does */
  578. #else
  579. printf("\r\nLongjump failure - fatal\r\n");
  580. doexit(GOOD_EXIT,-1); /* Better than dumping core... */
  581. #endif /* USECCJMPBUF */
  582. }
  583. #ifdef USECCJMPBUF
  584. #undef USECCJMPBUF
  585. #endif /* USECCJMPBUF */
  586. case CEV_DUP: /* Child sends duplex change */
  587. read(xpipe[0], (char *)&duplex, sizeof(duplex));
  588. debug(F101,"CONNECT pipeint duplex","",duplex);
  589. break;
  590. #ifdef TNCODE
  591. case CEV_MEBIN: /* Child sends me_binary change */
  592. read(xpipe[0],
  593. (char *)&TELOPT_ME(TELOPT_BINARY),
  594. sizeof(TELOPT_ME(TELOPT_BINARY))
  595. );
  596. debug(F101,"CONNECT pipeint me_binary","",TELOPT_ME(TELOPT_BINARY));
  597. break;
  598. case CEV_UBIN: /* Child sends u_binary change */
  599. read(xpipe[0],
  600. (char *)&TELOPT_U(TELOPT_BINARY),
  601. sizeof(TELOPT_U(TELOPT_BINARY))
  602. );
  603. debug(F101,"CONNECT pipeint u_binary","",TELOPT_U(TELOPT_BINARY));
  604. break;
  605. #endif /* TNCODE */
  606. #ifdef CK_APC
  607. case CEV_AUL: /* Autoupload */
  608. justone = 1;
  609. debug(F100,"CONNECT autoupload at parent","",0);
  610. #ifdef CK_AUTODL
  611. case CEV_ADL: /* Autodownload */
  612. apcactive = APC_LOCAL;
  613. if (!justone) debug(F100,"CONNECT autodownload at parent","",0);
  614. /* Copy child's Kermit packet if any */
  615. read(xpipe[0], (char *)&x, sizeof(x));
  616. debug(F101,"CONNECT trigger ibc (upper)","",ibc);
  617. if (x > 0)
  618. read(xpipe[0], (char *)ksbuf, x+1);
  619. #endif /* CK_AUTODL */
  620. case CEV_APC: /* Application Program Command */
  621. read(xpipe[0], (char *)&apclength, sizeof(apclength));
  622. read(xpipe[0], apcbuf, apclength+1); /* Include trailing zero byte */
  623. debug(F111,"CONNECT APC at parent",apcbuf,apclength);
  624. read(xpipe[0], (char *)&ibc, sizeof(ibc)); /* Copy child's */
  625. if (ibc > 0) { /* input buffer. */
  626. read(xpipe[0], (char *)&ibp, sizeof(ibp));
  627. read(xpipe[0], ibp, ibc);
  628. }
  629. obc = 0; obp = obuf; *obuf = NUL; /* Because port fork flushed */
  630. sjval = CEV_APC;
  631. cklongjmp(con_env,sjval);
  632. /* NOTREACHED */
  633. #endif /* CK_APC */
  634. #ifdef SUNX25
  635. case CEV_PAD: /* X.25 PAD parameter change */
  636. debug(F100,"CONNECT pipeint PAD change","",0);
  637. read(xpipe[0],padparms,MAXPADPARMS);
  638. sjval = CEV_PAD; /* Set global variable. */
  639. #ifdef COMMENT /* We might not need to do this... */
  640. cklongjmp(con_env,sjval);
  641. /* NOTREACHED */
  642. #else /* COMMENT */
  643. break;
  644. #endif /* COMMENT */
  645. #endif /* SUNX25 */
  646. }
  647. signal(CK_FORK_SIG, pipeint); /* Set up signal handler */
  648. kill(pid, CK_FORK_SIG); /* Signal the port fork ... */
  649. }
  650. /* C K C P U T C -- C-Kermit CONNECT Put Character to Screen */
  651. /*
  652. Output is buffered to avoid slow screen writes on fast connections.
  653. NOTE: These could (easily?) become macros ...
  654. */
  655. static int
  656. ckcputf() { /* Dump the output buffer */
  657. int x = 0;
  658. if (obc > 0) /* If we have any characters, */
  659. x = conxo(obc,obuf); /* dump them, */
  660. obp = obuf; /* reset the pointer */
  661. obc = 0; /* and the counter. */
  662. return(x); /* Return conxo's return code */
  663. }
  664. int
  665. ckcputc(c) int c; {
  666. int x;
  667. *obp++ = c & 0xff; /* Deposit the character */
  668. obc++; /* Count it */
  669. if (ibc == 0 || /* If input buffer about empty */
  670. obc == OBUFL) { /* or output buffer full */
  671. debug(F101,"CONNECT CKCPUTC obc","",obc);
  672. x = conxo(obc,obuf); /* dump the buffer, */
  673. obp = obuf; /* reset the pointer */
  674. obc = 0; /* and the counter. */
  675. return(x); /* Return conxo's return code */
  676. } else return(0);
  677. }
  678. /* C K C G E T C -- C-Kermit CONNECT Get Character */
  679. /*
  680. Buffered read from communication device.
  681. Returns the next character, refilling the buffer if necessary.
  682. On error, returns ttinc's return code (see ttinc() description).
  683. Dummy argument for compatible calling conventions with ttinc().
  684. NOTE: We don't have a macro for this because we have to pass
  685. a pointer to this function as an argument to tn_doop().
  686. */
  687. int
  688. ckcgetc(dummy) int dummy; {
  689. int c, n;
  690. #ifdef CK_SSL
  691. extern int ssl_active_flag, tls_active_flag;
  692. #endif /* CK_SSL */
  693. #ifdef CK_ENCRYPTION
  694. /* No buffering for possibly encrypted connections */
  695. if (network && IS_TELNET() && TELOPT_ME(TELOPT_AUTHENTICATION))
  696. return(ttinc(0));
  697. #endif /* CK_ENCRYPTION */
  698. #ifdef CK_SSL
  699. if (ssl_active_flag || tls_active_flag)
  700. return(ttinc(0));
  701. #endif /* CK_SSL */
  702. #ifdef COMMENT
  703. /* too much */
  704. debug(F101,"CONNECT CKCGETC 1 ibc","",ibc); /* Log */
  705. #endif /* COMMENT */
  706. if (ibc < 1) { /* Need to refill buffer? */
  707. ibc = 0; /* Yes, reset count */
  708. ibp = ibuf; /* and buffer pointer */
  709. /* debug(F100,"CONNECT CKCGETC 1 calling ttinc(0)","",0); */
  710. #ifdef COMMENT
  711. /*
  712. This check is not worth the overhead. Scenario: ttchk() returns 0, so we
  713. fall through to the blocking ttinc(). While in ttinc(), the connection is
  714. lost. But the read() that ttinc() calls does not notice, and never returns.
  715. This happens at least in HP-UX, and can be seen when we turn off the modem.
  716. */
  717. if (!network && (carrier != CAR_OFF))
  718. if ((n = ttchk()) < 0) /* Make sure connection is not lost */
  719. return(n);
  720. #endif /* COMMENT */
  721. c = ttinc(0); /* Read one character, blocking */
  722. /* debug(F101,"CONNECT CKCGETC 1 ttinc(0)","",c); */
  723. if (c < 0) { /* If error, return error code */
  724. return(c);
  725. } else { /* Otherwise, got one character */
  726. *ibp++ = c; /* Advance buffer pointer */
  727. ibc++; /* and count. */
  728. }
  729. if ((n = ttchk()) > 0) { /* Any more waiting? */
  730. if (n > (IBUFL - ibc)) /* Get them all at once. */
  731. n = IBUFL - ibc; /* Don't overflow buffer */
  732. if ((n = ttxin(n,(CHAR *)ibp)) > 0) {
  733. #ifdef CK_ENCRYPTION
  734. if (TELOPT_U(TELOPT_ENCRYPTION))
  735. ck_tn_decrypt(ibp,n);
  736. #endif /* CK_ENCRYPTION */
  737. ibc += n; /* Advance counter */
  738. }
  739. } else if (n < 0) { /* Error? */
  740. return(n); /* Return the error code */
  741. }
  742. debug(F101,"CONNECT CKCGETC 2 ibc","",ibc); /* Log how many */
  743. ibp = ibuf; /* Point to beginning of buffer */
  744. }
  745. c = *ibp++ & 0xff; /* Get next character from buffer */
  746. ibc--; /* Reduce buffer count */
  747. return(c); /* Return the character */
  748. }
  749. /*
  750. Keyboard handling, buffered for speed, which is needed when C-Kermit is
  751. in CONNECT mode between two other computers that are transferring data.
  752. */
  753. static char *kbp; /* Keyboard input buffer pointer */
  754. static int kbc; /* Keyboard input buffer count */
  755. #ifdef CK_SMALL /* Keyboard input buffer length */
  756. #define KBUFL 32 /* Small for PDP-11 UNIX */
  757. #else
  758. #define KBUFL 257 /* Regular kernel size for others */
  759. #endif /* CK_SMALL */
  760. #ifdef DYNAMIC
  761. static char *kbuf = NULL;
  762. #else
  763. static char kbuf[KBUFL];
  764. #endif /* DYNAMIC */
  765. /* Macro for reading keystrokes. */
  766. #define CONGKS() (((--kbc)>=0) ? ((int)(*kbp++) & 0377) : kbget())
  767. /*
  768. Note that we call read() directly here, normally a no-no, but in this case
  769. we know it's UNIX and we're only doing what coninc(0) would have done,
  770. except we're reading a block of characters rather than just one. There is,
  771. at present, no conxin() analog to ttxin() for chunk reads, and instituting
  772. one would only add function-call overhead as it would only be a wrapper for
  773. a read() call anyway.
  774. */
  775. /*
  776. Another note: We stick in this read() till the user types something.
  777. But the other (lower) fork is running too, and on TELNET connections,
  778. it will signal us to indicate echo-change negotiations, and this can
  779. interrupt the read(). Some UNIXes automatically restart the interrupted
  780. system call, others return from it with errno == EINTR.
  781. */
  782. static int /* Keyboard buffer filler */
  783. kbget() {
  784. #ifdef EINTR
  785. int tries = 10; /* If read() is interrupted, */
  786. int ok = 0;
  787. while (tries-- > 0) { /* try a few times... */
  788. #endif /* EINTR */
  789. if ((kbc = conchk()) < 1) /* How many chars waiting? */
  790. kbc = 1; /* If none or dunno, wait for one. */
  791. else if (kbc > KBUFL) /* If too many, */
  792. kbc = KBUFL; /* only read this many. */
  793. if ((kbc = read(0, kbuf, kbc)) < 1) { /* Now read it/them. */
  794. debug(F101,"CONNECT kbget errno","",errno); /* Got an error. */
  795. #ifdef EINTR
  796. if (errno == EINTR) /* Interrupted system call. */
  797. continue; /* Try again, up to limit. */
  798. else /* Something else. */
  799. #endif /* EINTR */
  800. return(-1); /* Pass along read() error. */
  801. }
  802. #ifdef EINTR
  803. else { ok = 1; break; }
  804. }
  805. if (!ok) return(-1);
  806. #endif /* EINTR */
  807. kbp = kbuf; /* Adjust buffer pointer, */
  808. kbc--; /* count, */
  809. return((int)(*kbp++) & 0377); /* and return first character. */
  810. }
  811. /* C O N C L D -- Interactive terminal connection child function */
  812. static
  813. #ifdef BEOSORBEBOX
  814. long
  815. #else
  816. VOID
  817. #endif /* BEOSORBEBOX */
  818. concld (
  819. #ifdef BEOSORBEBOX
  820. void *bevoid
  821. #endif /* BEOSORBEBOX */
  822. ) {
  823. int n; /* General purpose counter */
  824. int i; /* For loops... */
  825. int c = -1; /* c is a character, but must be signed
  826. integer to pass thru -1, which is the
  827. modem disconnection signal, and is
  828. different from the character 0377 */
  829. int prev;
  830. #ifdef TNCODE
  831. int tx; /* tn_doop() return code */
  832. #endif /* TNCODE */
  833. #ifdef CK_TRIGGER
  834. int ix; /* Trigger index */
  835. #endif /* CK_TRIGGER */
  836. #ifndef NOESCSEQ
  837. int apcrc;
  838. #endif /* NOESCSEQ */
  839. #ifdef COMMENT
  840. int conret = 0; /* Return value from conect() */
  841. jbchksum = -1L;
  842. #endif /* COMMENT */
  843. jbset = 0; /* jmp_buf not set yet, don't use it */
  844. debug(F101,"CONNECT concld entry","",CK_FORK_SIG);
  845. /* *** */ /* Inferior reads, prints port input */
  846. if (priv_can()) { /* Cancel all privs */
  847. printf("?setuid error - fatal\n");
  848. doexit(BAD_EXIT,-1);
  849. }
  850. signal(SIGINT, SIG_IGN); /* In case these haven't been */
  851. signal(SIGQUIT, SIG_IGN); /* inherited from above... */
  852. signal(CK_FORK_SIG, SIG_IGN); /* CK_FORK_SIG not expected yet */
  853. inshift = outshift = 0; /* Initial SO/SI shift state. */
  854. { /* Wait for parent's setup */
  855. int i;
  856. while ((i = read(xpipe[0], &c, 1)) <= 0) {
  857. if (i < 0) {
  858. debug(F101,"CONNECT concld setup error","",i);
  859. debug(F111,"CONNECT concld setup error",ck_errstr(),errno);
  860. pipemsg(CEV_HUP); /* Read error - hangup */
  861. ck_sndmsg(); /* Send and wait to be killed */
  862. /* NOTREACHED */
  863. } /* Restart interrupted read() */
  864. }
  865. }
  866. close(xpipe[0]); xpipe[0] = -1; /* Child - prevent future reads */
  867. #ifdef DEBUG
  868. if (deblog) {
  869. debug(F100,"CONNECT starting port fork","",0);
  870. debug(F101,"CONNECT port fork ibc","",ibc);
  871. debug(F101,"CONNECT port fork obc","",obc);
  872. }
  873. #endif /* DEBUG */
  874. what = W_CONNECT;
  875. while (1) { /* Fresh read, wait for a character. */
  876. #ifdef ANYX25
  877. if (network && (nettype == NET_SX25)) {
  878. bzero(x25ibuf,sizeof(x25ibuf)) ;
  879. if ((ibufl = ttxin(MAXIX25,(CHAR *)x25ibuf)) < 0) {
  880. #ifndef IBMX25
  881. if (ibufl == -2) { /* PAD parms changes */
  882. pipemsg(CEV_PAD);
  883. write(xpipe[1],padparms,MAXPADPARMS);
  884. ck_sndmsg();
  885. } else {
  886. #endif /* IBMX25 */
  887. if (!quiet)
  888. printf("\r\nCommunications disconnect ");
  889. dologend();
  890. pipemsg(CEV_HUP);
  891. ck_sndmsg(); /* Wait to be killed */
  892. /* NOTREACHED */
  893. #ifndef IBMX25
  894. }
  895. #endif /* IBMX25 */
  896. /* pause(); <--- SHOULD BE OBSOLETE NOW! */
  897. /* BECAUSE pause() is done inside of ck_sndmsg() */
  898. }
  899. if (debses) { /* Debugging output */
  900. p = x25ibuf ;
  901. while (ibufl--) { c = *p++; conol(dbchr(c)); }
  902. } else {
  903. if (seslog && sessft) /* Binary session log */
  904. logchar((char)c); /* Log char before translation */
  905. if (sosi
  906. #ifndef NOCSETS
  907. || tcsl != tcsr
  908. #endif /* NOCSETS */
  909. ) { /* Character at a time */
  910. for (i = 1; i < ibufl; i++) {
  911. c = x25ibuf[i] & cmask;
  912. if (sosi) { /* Handle SI/SO */
  913. if (c == SO) {
  914. inshift = 1;
  915. continue;
  916. } else if (c == SI) {
  917. inshift = 0;
  918. continue;
  919. }
  920. if (inshift)
  921. c |= 0200;
  922. }
  923. #ifndef NOCSETS
  924. if (inesc == ES_NORMAL) {
  925. #ifdef UNICODE
  926. int x;
  927. if (unicode == 1) { /* Remote is UTF-8 */
  928. x = u_to_b((CHAR)c);
  929. if (x == -1)
  930. continue;
  931. else if (x == -2) { /* LS or PS */
  932. inxbuf[0] = CR;
  933. inxbuf[1] = LF;
  934. inxcount = 2;
  935. } else {
  936. inxbuf[0] = (unsigned)(x & 0xff);
  937. }
  938. c = inxbuf[0];
  939. } else if (unicode == 2) { /* Local is UTF-8 */
  940. inxcount =
  941. b_to_u((CHAR)c,inxbuf,OUTXBUFSIZ,tcssize);
  942. c = inxbuf[0];
  943. } else {
  944. #endif /* UNICODE */
  945. if (sxi) c = (*sxi)((CHAR)c);
  946. if (rxi) c = (*rxi)((CHAR)c);
  947. inxbuf[0] = c;
  948. #ifdef UNICODE
  949. }
  950. #endif /* UNICODE */
  951. }
  952. #endif /* NOCSETS */
  953. c &= cmdmsk; /* Apply command mask. */
  954. conoc(c); /* Output to screen */
  955. if (seslog && !sessft) /* and session log */
  956. logchar(c);
  957. }
  958. } else { /* All at once */
  959. for (i = 1; i < ibufl; i++)
  960. x25ibuf[i] &= (cmask & cmdmsk);
  961. conxo(ibufl,x25ibuf);
  962. if (seslog) zsoutx(ZSFILE,x25ibuf,ibufl);
  963. }
  964. }
  965. continue;
  966. } else { /* Not X.25... */
  967. #endif /* ANYX25 */
  968. /*
  969. Get the next communication line character from our internal buffer.
  970. If the buffer is empty, refill it.
  971. */
  972. prev = c; /* Remember previous character */
  973. c = ckcgetc(0); /* Get next character */
  974. /* debug(F101,"CONNECT c","",c); */
  975. if (c < 0) { /* Failed... */
  976. debug(F101,"CONNECT disconnect ibc","",ibc);
  977. debug(F101,"CONNECT disconnect obc","",obc);
  978. ckcputf(); /* Flush CONNECT output buffer */
  979. if (!quiet) {
  980. printf("\r\nCommunications disconnect ");
  981. #ifdef COMMENT
  982. if ( c == -3
  983. #ifdef ultrix
  984. /* This happens on Ultrix if there's no carrier */
  985. && errno != EIO
  986. #endif /* ultrix */
  987. #ifdef UTEK
  988. /* This happens on UTEK if there's no carrier */
  989. && errno != EWOULDBLOCK
  990. #endif /* UTEK */
  991. )
  992. perror("\r\nCan't read character");
  993. #endif /* COMMENT */
  994. }
  995. #ifdef NOSETBUF
  996. fflush(stdout);
  997. #endif /* NOSETBUF */
  998. tthang(); /* Hang up the connection */
  999. debug(F111,"CONNECT concld i/o error",ck_errstr(),errno);
  1000. pipemsg(CEV_HUP);
  1001. ck_sndmsg(); /* Wait to be killed */
  1002. }
  1003. #ifdef COMMENT
  1004. /* too much... */
  1005. debug(F101,"CONNECT ** PORT","",c); /* Got character c OK. */
  1006. #endif /* COMMENT */
  1007. #ifdef TNCODE
  1008. /* Handle TELNET negotiations... */
  1009. if ((c == NUL) && network && IS_TELNET()) {
  1010. if (prev == CR) /* Discard <NUL> of <CR><NUL> */
  1011. if (!TELOPT_U(TELOPT_BINARY))
  1012. continue;
  1013. }
  1014. if ((c == IAC) && network && IS_TELNET()) {
  1015. int me_bin = TELOPT_ME(TELOPT_BINARY);
  1016. int u_bin = TELOPT_U(TELOPT_BINARY);
  1017. debug(F100,"CONNECT got IAC","",0);
  1018. ckcputf(); /* Dump screen-output buffer */
  1019. if ((tx = tn_doop((CHAR)(c & 0xff),duplex,ckcgetc)) == 0) {
  1020. if (me_bin != TELOPT_ME(TELOPT_BINARY)) {
  1021. debug(F101,
  1022. "CONNECT TELNET me_bin",
  1023. "",
  1024. TELOPT_ME(TELOPT_BINARY)
  1025. );
  1026. pipemsg(CEV_MEBIN); /* Tell parent */
  1027. write(xpipe[1],
  1028. &TELOPT_ME(TELOPT_BINARY),
  1029. sizeof(TELOPT_ME(TELOPT_BINARY))
  1030. );
  1031. ck_sndmsg(); /* Tell the parent fork */
  1032. } else if (u_bin != TELOPT_U(TELOPT_BINARY)) {
  1033. debug(F101,
  1034. "CONNECT TELNET u_bin",
  1035. "",
  1036. TELOPT_U(TELOPT_BINARY)
  1037. );
  1038. pipemsg(CEV_UBIN); /* Tell parent */
  1039. write(xpipe[1],
  1040. &TELOPT_U(TELOPT_BINARY),
  1041. sizeof(TELOPT_U(TELOPT_BINARY))
  1042. );
  1043. ck_sndmsg(); /* Tell the parent fork */
  1044. }
  1045. continue;
  1046. } else if (tx == -1) { /* I/O error */
  1047. if (!quiet)
  1048. printf("\r\nCommunications disconnect ");
  1049. #ifdef NOSETBUF
  1050. fflush(stdout);
  1051. #endif /* NOSETBUF */
  1052. dologend();
  1053. debug(F111,"CONNECT concld i/o error 2",ck_errstr(),errno);
  1054. pipemsg(CEV_HUP);
  1055. ck_sndmsg(); /* Wait to be killed */
  1056. /* NOTREACHED */
  1057. } else if (tx == -3) { /* I/O error */
  1058. if (!quiet)
  1059. printf("\r\nConnection closed due to telnet policy ");
  1060. #ifdef NOSETBUF
  1061. fflush(stdout);
  1062. #endif /* NOSETBUF */
  1063. dologend();
  1064. debug(F111,"CONNECT concld i/o error 2",ck_errstr(),errno);
  1065. pipemsg(CEV_HUP);
  1066. ck_sndmsg(); /* Wait to be killed */
  1067. /* NOTREACHED */
  1068. } else if (tx == -2) { /* I/O error */
  1069. if (!quiet)
  1070. printf("\r\nConnection closed by peer ");
  1071. #ifdef NOSETBUF
  1072. fflush(stdout);
  1073. #endif /* NOSETBUF */
  1074. dologend();
  1075. debug(F111,"CONNECT concld i/o error 2",ck_errstr(),errno);
  1076. pipemsg(CEV_HUP);
  1077. ck_sndmsg(); /* Wait to be killed */
  1078. /* NOTREACHED */
  1079. } else if ((tx == 1) && (!duplex)) { /* ECHO change */
  1080. duplex = 1; /* Turn on local echo */
  1081. debug(F101,"CONNECT TELNET duplex change","",duplex);
  1082. pipemsg(CEV_DUP); /* Tell parent */
  1083. write(xpipe[1], &duplex, sizeof(duplex));
  1084. ck_sndmsg(); /* Tell the parent fork */
  1085. continue;
  1086. } else if ((tx == 2) && (duplex)) { /* ECHO change */
  1087. duplex = 0;
  1088. debug(F101,"CONNECT TELNET duplex change","",duplex);
  1089. pipemsg(CEV_DUP);
  1090. write(xpipe[1], &duplex, sizeof(duplex));
  1091. ck_sndmsg();
  1092. continue;
  1093. } else if (tx == 3) { /* Quoted IAC */
  1094. c = parity ? 127 : 255;
  1095. }
  1096. #ifdef IKS_OPTION
  1097. else if (tx == 4) { /* IKS State Change */
  1098. if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start &&
  1099. !tcp_incoming
  1100. ) {
  1101. /* here we need to print a msg that the other */
  1102. /* side is in SERVER mode and that REMOTE */
  1103. /* commands should be used. And CONNECT mode */
  1104. /* should be ended. */
  1105. active = 0;
  1106. }
  1107. }
  1108. #endif /* IKS_OPTION */
  1109. else if (tx == 6) {
  1110. /* DO LOGOUT received */
  1111. if (!quiet)
  1112. printf("\r\nRemote Logout ");
  1113. #ifdef NOSETBUF
  1114. fflush(stdout);
  1115. #endif /* NOSETBUF */
  1116. debug(F100,"CONNECT Remote Logout","",0);
  1117. pipemsg(CEV_HUP);
  1118. ck_sndmsg(); /* Wait to be killed */
  1119. /* NOTREACHED */
  1120. } else
  1121. continue; /* Negotiation OK, get next char. */
  1122. } else if (parity)
  1123. c &= 0x7f;
  1124. if (TELOPT_ME(TELOPT_ECHO) && tn_rem_echo)
  1125. ttoc(c); /* I'm echoing for the remote */
  1126. #endif /* TNCODE */
  1127. if (debses) { /* Output character to screen */
  1128. char *s; /* Debugging display... */
  1129. s = dbchr(c);
  1130. while (*s)
  1131. ckcputc(*s++);
  1132. } else { /* Regular display ... */
  1133. c &= cmask; /* Apply Kermit-to-remote mask */
  1134. #ifdef CK_AUTODL
  1135. /*
  1136. Autodownload. Check for Kermit S packet prior to translation, since that
  1137. can change the packet and make it unrecognizable (as when the terminal
  1138. character set is an ISO 646 one)... Ditto for Zmodem start packet.
  1139. */
  1140. if (autodl /* Autodownload enabled? */
  1141. #ifdef IKS_OPTION
  1142. || TELOPT_SB(TELOPT_KERMIT).kermit.me_start
  1143. #endif /* IKS_OPTION */
  1144. ) {
  1145. int k;
  1146. k = kstart((CHAR)c); /* Kermit S or I packet? */
  1147. #ifdef CK_XYZ
  1148. if (!k && zmdlok) /* Or an "sz" start? */
  1149. k = zstart((CHAR)c);
  1150. #endif /* CK_XYZ */
  1151. if (k) {
  1152. int ksign = 0;
  1153. debug(F101,"CONNECT autodownload k","",k);
  1154. if (k < 0) { /* Minus-Protocol? */
  1155. #ifdef NOSERVER
  1156. goto noserver; /* Need server mode for this */
  1157. #else
  1158. ksign = 1; /* Remember */
  1159. k = 0 - k; /* Convert to actual protocol */
  1160. justone = 1; /* Flag for protocol module */
  1161. #endif /* NOSERVER */
  1162. } else
  1163. justone = 0;
  1164. k--; /* Adjust [kz]start's return value */
  1165. if (k == PROTO_K
  1166. #ifdef CK_XYZ
  1167. || k == PROTO_Z
  1168. #endif /* CK_XYZ */
  1169. ) {
  1170. /* Now damage the packet so that it does not */
  1171. /* trigger autodownload detection on subsquent */
  1172. /* links. */
  1173. if (k == PROTO_K) {
  1174. int i, len = strlen((char*)ksbuf);
  1175. for (i = 0; i < len; i++)
  1176. ckcputc(BS);
  1177. }
  1178. #ifdef CK_XYZ
  1179. else {
  1180. int i;
  1181. for (i = 0; i < 3; i++)
  1182. ckcputc(CAN);
  1183. }
  1184. #endif /* CK_XYZ */
  1185. /* Notify parent */
  1186. pipemsg(justone ? CEV_AUL : CEV_ADL);
  1187. /*
  1188. Send our memory back up to the top fork thru the pipe.
  1189. CAREFUL -- Write this stuff in the same order it is to be read!
  1190. */
  1191. /* Copy our Kermit packet to the parent fork */
  1192. n = (int) strlen((char *)ksbuf);
  1193. write(xpipe[1], (char *)&n, sizeof(n));
  1194. if (n > 0)
  1195. write(xpipe[1], (char *)ksbuf, n+1);
  1196. debug(F111,"CONNECT autodownload ksbuf",ksbuf,n);
  1197. debug(F101,"CONNECT autodownload justone","",
  1198. justone);
  1199. /* Construct the APC command */
  1200. sprintf(apcbuf,
  1201. "set proto %s, %s, set proto %s",
  1202. ptab[k].p_name,
  1203. ksign ? "server" : "receive",
  1204. ptab[protocol].p_name
  1205. );
  1206. apclength = strlen(apcbuf);
  1207. debug(F111,"CONNECT ksbuf",ksbuf,k);
  1208. debug(F110,"CONNECT autodownload",apcbuf,0);
  1209. apcactive = APC_LOCAL;
  1210. ckcputf(); /* Force screen update */
  1211. /* Write buffer including trailing NUL byte */
  1212. debug(F101,"CONNECT write xpipe apclength","",
  1213. apclength);
  1214. write(xpipe[1],
  1215. (char *)&apclength,
  1216. sizeof(apclength)
  1217. );
  1218. debug(F110,"CONNECT write xpipe apcbuf",apcbuf,0);
  1219. write(xpipe[1], apcbuf, apclength+1);
  1220. /* Copy our input buffer to the parent fork */
  1221. debug(F101,"CONNECT autodownload complete ibc",
  1222. "",ibc);
  1223. debug(F101,"CONNECT autodownload complete obc",
  1224. "",obc);
  1225. write(xpipe[1], (char *)&ibc, sizeof(ibc));
  1226. if (ibc > 0) {
  1227. write(xpipe[1], (char *)&ibp, sizeof(ibp));
  1228. write(xpipe[1], ibp, ibc);
  1229. }
  1230. ck_sndmsg(); /* Wait to be killed */
  1231. /* NOTREACHED */
  1232. }
  1233. }
  1234. }
  1235. #ifdef NOSERVER
  1236. noserver:
  1237. #endif /* NOSERVER */
  1238. #endif /* CK_AUTODL */
  1239. if (sosi) { /* Handle SI/SO */
  1240. if (c == SO) { /* Shift Out */
  1241. inshift = 1;
  1242. continue;
  1243. } else if (c == SI) { /* Shift In */
  1244. inshift = 0;
  1245. continue;
  1246. }
  1247. if (inshift) c |= 0200;
  1248. }
  1249. inxbuf[0] = c; /* In case there is no translation */
  1250. inxcount = 1; /* ... */
  1251. #ifndef NOCSETS
  1252. if (inesc == ES_NORMAL) { /* If not in an escape sequence */
  1253. #ifdef UNICODE
  1254. int x; /* Translate character sets */
  1255. CHAR ch;
  1256. ch = c;
  1257. if (unicode == 1) { /* Remote is UTF-8 */
  1258. x = u_to_b(ch);
  1259. if (x < 0)
  1260. continue;
  1261. inxbuf[0] = (unsigned)(x & 0xff);
  1262. c = inxbuf[0];
  1263. } else if (unicode == 2) { /* Local is UTF-8 */
  1264. inxcount = b_to_u(ch,inxbuf,OUTXBUFSIZ,tcssize);
  1265. c = inxbuf[0];
  1266. } else {
  1267. #endif /* UNICODE */
  1268. if (sxi) c = (*sxi)((CHAR)c);
  1269. if (rxi) c = (*rxi)((CHAR)c);
  1270. inxbuf[0] = c;
  1271. #ifdef UNICODE
  1272. }
  1273. #endif /* UNICODE */
  1274. }
  1275. #endif /* NOCSETS */
  1276. #ifndef NOESCSEQ
  1277. if (escseq) /* If handling escape sequences */
  1278. apcrc = chkaes((char)c); /* update our state */
  1279. #ifdef CK_APC
  1280. /*
  1281. If we are handling APCs, we have several possibilities at this point:
  1282. 1. Ordinary character to be written to the screen.
  1283. 2. An Esc; we can't write it because it might be the beginning of an APC.
  1284. 3. The character following an Esc, in which case we write Esc, then char,
  1285. but only if we have not just entered an APC sequence.
  1286. */
  1287. if (escseq && (apcstatus & APC_ON)) {
  1288. if (inesc == ES_GOTESC) /* Don't write ESC yet */
  1289. continue;
  1290. else if (oldesc == ES_GOTESC && !apcactive) {
  1291. ckcputc(ESC); /* Write saved ESC */
  1292. if (seslog && !sessft)
  1293. logchar((char)ESC);
  1294. } else if (apcrc) { /* We have an APC */
  1295. debug(F111,"CONNECT APC complete",apcbuf,apclength);
  1296. ckcputf(); /* Force screen update */
  1297. pipemsg(CEV_APC); /* Notify parent */
  1298. write(xpipe[1],
  1299. (char *)&apclength,
  1300. sizeof(apclength)
  1301. );
  1302. /* Write buffer including trailing NUL byte */
  1303. write(xpipe[1], apcbuf, apclength+1);
  1304. /* Copy our input buffer to the parent fork */
  1305. debug(F101,"CONNECT APC complete ibc","",ibc);
  1306. debug(F101,"CONNECT APC complete obc","",obc);
  1307. write(xpipe[1], (char *)&ibc, sizeof(ibc));
  1308. if (ibc > 0) {
  1309. write(xpipe[1], (char *)&ibp, sizeof(ibp));
  1310. write(xpipe[1], ibp, ibc);
  1311. }
  1312. ck_sndmsg(); /* Wait to be killed */
  1313. /* NOTREACHED */
  1314. }
  1315. }
  1316. #endif /* CK_APC */
  1317. #endif /* NOESCSEQ */
  1318. for (i = 0; i < inxcount; i++) { /* Loop thru */
  1319. c = inxbuf[i]; /* input expansion buffer... */
  1320. if (
  1321. #ifdef CK_APC
  1322. !apcactive /* Ignore APC sequences */
  1323. #else
  1324. 1
  1325. #endif /* CK_APC */
  1326. ) {
  1327. c &= cmdmsk; /* Apply command mask. */
  1328. if (c == CR && tt_crd) { /* SET TERM CR-DISPLA CRLF? */
  1329. ckcputc(c); /* Yes, output CR */
  1330. if (seslog && !sessft)
  1331. logchar((char)c);
  1332. c = LF; /* and insert a linefeed */
  1333. }
  1334. if (c == LF && tt_lfd) { /* SET TERM CR-DISPLA CRLF? */
  1335. ckcputc(CR); /* Yes, output CR */
  1336. if (seslog && !sessft) logchar((char)CR);
  1337. }
  1338. ckcputc(c); /* Write character to screen */
  1339. }
  1340. if (seslog && !sessft) /* Handle session log */
  1341. logchar((char)c);
  1342. #ifdef CK_TRIGGER
  1343. /* Check for trigger string */
  1344. if (tt_trigger[0]) if ((ix = autoexitchk((CHAR)c)) > -1) {
  1345. ckcputf(); /* Force screen update */
  1346. #ifdef NOSETBUF
  1347. fflush(stdout); /* I mean really force it */
  1348. #endif /* NOSETBUF */
  1349. pipemsg(CEV_TRI); /* Send up trigger indication */
  1350. write(xpipe[1], (char *)&ix, sizeof(ix)); /* index */
  1351. write(xpipe[1], (char *)&ibc, sizeof(ibc));
  1352. if (ibc > 0) {
  1353. write(xpipe[1], (char *)&ibp, sizeof(ibp));
  1354. write(xpipe[1], ibp, ibc);
  1355. }
  1356. debug(F100,"CONNECT concld trigger","",0);
  1357. ck_sndmsg(); /* Wait to be killed */
  1358. active = 0; /* Shouldn't be necessary... */
  1359. break;
  1360. }
  1361. /* NOTREACHED */
  1362. #endif /* CK_TRIGGER */
  1363. }
  1364. }
  1365. #ifdef ANYX25
  1366. }
  1367. #endif /* ANYX25 */
  1368. }
  1369. }
  1370. /* C O N E C T -- Interactive terminal connection */
  1371. int
  1372. conect() {
  1373. int n; /* General purpose counter */
  1374. int i; /* For loops... */
  1375. int c; /* c is a character, but must be signed
  1376. integer to pass thru -1, which is the
  1377. modem disconnection signal, and is
  1378. different from the character 0377 */
  1379. int c2; /* A copy of c */
  1380. int csave; /* Another copy of c */
  1381. #ifndef NOESCSEQ
  1382. int apcrc;
  1383. #endif /* NOESCSEQ */
  1384. int conret = 0; /* Return value from conect() */
  1385. int msgflg = 0;
  1386. /* jbchksum = -1L; */
  1387. jbset = 0; /* jmp_buf not set yet, don't use it */
  1388. debok = 1;
  1389. debug(F101,"CONNECT fork signal","",CK_FORK_SIG);
  1390. debug(F101,"CONNECT entry pid","",pid);
  1391. msgflg = !quiet
  1392. #ifdef CK_APC
  1393. && !apcactive
  1394. #endif /* CK_APC */
  1395. ;
  1396. /*
  1397. The following is to handle a fork left behind if we exit CONNECT mode
  1398. without killing it, and then return to CONNECT mode. This happened in
  1399. HP-UX, where the Reset key would raise SIGINT even though SIGINT was set to
  1400. SIG_IGN. The code below fixes the symptom; the real fix is in the main
  1401. SIGINT handler (if SIGINT shows up during CONNECT, just return rather than
  1402. taking the longjmp).
  1403. */
  1404. if (pid) { /* This should be 0 */
  1405. int x = 0;
  1406. debug(F101,"CONNECT entry killing stale pid","",pid);
  1407. printf("WARNING: Old CONNECT fork seems to be active.\n");
  1408. printf("Attempting to remove it...");
  1409. #ifdef BEOSORBEBOX
  1410. {
  1411. long ret_val;
  1412. x = kill(pid,SIGKILLTHR); /* Kill lower fork */
  1413. wait_for_thread (pid, &ret_val);
  1414. }
  1415. #else
  1416. #ifdef Plan9
  1417. x = kill(pid,SIGKILL); /* Kill lower fork */
  1418. #else
  1419. x = kill(pid,9);
  1420. #endif /* Plan9 */
  1421. #endif /* BEOSORBEBOX */
  1422. wait((WAIT_T *)0); /* Wait till gone. */
  1423. if (x < 0) {
  1424. printf("ERROR: Failure to kill pid %d: %s, errno=%d\n",
  1425. (int) pid, ck_errstr(), errno);
  1426. debug(F111,"CONNECT error killing stale pid",ck_errstr(),pid);
  1427. }
  1428. pid = (PID_T) 0;
  1429. printf("\n");
  1430. }
  1431. signal(CK_FORK_SIG, SIG_IGN); /* Initial CK_FORK_SIG handling, */
  1432. /*
  1433. The following ttimoff() call should not be necessary, but evidently there
  1434. are cases where a timer is left active and then goes off, taking a longjmp
  1435. to nowhere after the program's stack has changed. In any case, this is
  1436. safe because the CONNECT module uses no timer of any kind, and no other timer
  1437. should be armed while Kermit is in CONNECT mode.
  1438. */
  1439. ttimoff(); /* Turn off any timer interrupts */
  1440. #ifdef CK_TRIGGER
  1441. makestr(&triggerval,NULL); /* Reset trigger */
  1442. #endif /* CK_TRIGGER */
  1443. if (!local) {
  1444. #ifdef NETCONN
  1445. printf("Sorry, you must SET LINE or SET HOST first\n");
  1446. #else
  1447. printf("Sorry, you must SET LINE first\n");
  1448. #endif /* NETCONN */
  1449. goto conret0;
  1450. }
  1451. if (speed < 0L && network == 0 && ttfdflg == 0) {
  1452. printf("Sorry, you must SET SPEED first\n");
  1453. goto conret0;
  1454. }
  1455. #ifdef TCPSOCKET
  1456. if (network && (nettype != NET_TCPB)
  1457. #ifdef SUNX25
  1458. && (nettype != NET_SX25)
  1459. #endif /* SUNX25 */
  1460. #ifdef IBMX25
  1461. && (nettype != NET_IX25)
  1462. #endif /* IBMX25 */
  1463. #ifdef NETCMD
  1464. && (nettype != NET_CMD)
  1465. #endif /* NETCMD */
  1466. #ifdef NETPTY
  1467. && (nettype != NET_PTY)
  1468. #endif /* NETPTY */
  1469. ) {
  1470. printf("Sorry, network type not supported\n");
  1471. goto conret0;
  1472. }
  1473. #endif /* TCPSOCKET */
  1474. #ifdef DYNAMIC
  1475. if (!ibuf) {
  1476. if (!(ibuf = malloc(IBUFL+1))) { /* Allocate input line buffer */
  1477. printf("Sorry, CONNECT input buffer can't be allocated\n");
  1478. goto conret0;
  1479. } else {
  1480. ibp = ibuf;
  1481. ibc = 0;
  1482. }
  1483. }
  1484. if (!obuf) {
  1485. if (!(obuf = malloc(OBUFL+1))) { /* Allocate output line buffer */
  1486. printf("Sorry, CONNECT output buffer can't be allocated\n");
  1487. goto conret0;
  1488. } else {
  1489. obp = obuf;
  1490. obc = 0;
  1491. }
  1492. }
  1493. if (!kbuf) {
  1494. if (!(kbuf = malloc(KBUFL+1))) { /* Allocate keyboard input buffer */
  1495. printf("Sorry, CONNECT keyboard buffer can't be allocated\n");
  1496. goto conret0;
  1497. }
  1498. }
  1499. if (!temp) {
  1500. if (!(temp = malloc(TMPLEN+1))) { /* Allocate temporary buffer */
  1501. printf("Sorry, CONNECT temporary buffer can't be allocated\n");
  1502. goto conret0;
  1503. }
  1504. }
  1505. #else
  1506. #ifdef COMMENT
  1507. ibp = ibuf;
  1508. ibc = 0;
  1509. #endif /* COMMENT */
  1510. obp = obuf;
  1511. obc = 0;
  1512. #endif /* DYNAMIC */
  1513. kbp = kbuf; /* Always clear these. */
  1514. *kbp = NUL; /* No need to preserve them between */
  1515. kbc = 0; /* CONNECT sessions. */
  1516. #ifdef DEBUG
  1517. if (deblog) {
  1518. debug(F101,"CONNECT conect entry ttyfd","",ttyfd);
  1519. debug(F101,"CONNECT conect entry ibc","",ibc);
  1520. debug(F101,"CONNECT conect entry obc","",obc);
  1521. debug(F101,"CONNECT conect entry kbc","",kbc);
  1522. #ifdef CK_TRIGGER
  1523. debug(F110,"CONNECT conect trigger",tt_trigger[0],0);
  1524. #endif /* CK_TRIGGER */
  1525. if (ttyfd > -1) {
  1526. n = ttchk();
  1527. debug(F101,"CONNECT conect entry ttchk","",n);
  1528. }
  1529. }
  1530. #endif /* DEBUG */
  1531. if (ttyfd < 0) { /* If communication device not open */
  1532. debug(F101,"CONNECT ttnproto","",ttnproto);
  1533. debug(F111,"CONNECT opening",ttname,0); /* Open it now */
  1534. if (ttopen(ttname,
  1535. &local,
  1536. network ? -nettype : mdmtyp,
  1537. 0
  1538. ) < 0) {
  1539. ckmakmsg(temp,TMPLEN,"Sorry, can't open ",ttname,NULL,NULL);
  1540. perror(temp);
  1541. debug(F110,"CONNECT open failure",ttname,0);
  1542. goto conret0;
  1543. }
  1544. #ifdef IKS_OPTION
  1545. /* If peer is in Kermit server mode, return now. */
  1546. if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start)
  1547. return(0);
  1548. #endif /* IKS_OPTION */
  1549. }
  1550. dohangup = 0; /* Hangup not requested yet */
  1551. #ifdef ANYX25
  1552. dox25clr = 0; /* X.25 clear not requested yet */
  1553. #endif /* ANYX25 */
  1554. if (msgflg) {
  1555. #ifdef NETCONN
  1556. if (network) {
  1557. if (ttpipe)
  1558. printf("\r\nConnecting via command \"%s\"",ttname);
  1559. else
  1560. printf("\r\nConnecting to host %s",ttname);
  1561. #ifdef ANYX25
  1562. if (nettype == NET_SX25 || nettype == NET_IX25)
  1563. printf(", Link ID %d, LCN %d",linkid,lcn);
  1564. #endif /* ANYX25 */
  1565. } else {
  1566. #endif /* NETCONN */
  1567. printf("\r\nConnecting to %s",ttname);
  1568. if (speed > -1L) printf(", speed %ld",speed);
  1569. #ifdef NETCONN
  1570. }
  1571. #endif /* NETCONN */
  1572. if (tt_escape) {
  1573. printf("\r\n");
  1574. shoesc(escape);
  1575. printf("Type the escape character followed by C to get back,\r\n");
  1576. printf("or followed by ? to see other options.\r\n");
  1577. } else {
  1578. printf(".\r\n\nESCAPE CHARACTER IS DISABLED\r\n\n");
  1579. }
  1580. if (seslog) {
  1581. extern int slogts;
  1582. char * s = "";
  1583. switch (sessft) {
  1584. case XYFT_D:
  1585. s = "debug"; break;
  1586. case XYFT_T:
  1587. s = slogts ? "timestamped-text" : "text"; break;
  1588. default:
  1589. s = "binary";
  1590. }
  1591. printf("Session Log: %s, %s\r\n",sesfil,s);
  1592. }
  1593. if (debses) printf("Debugging Display...)\r\n");
  1594. printf("----------------------------------------------------\r\n");
  1595. fflush(stdout);
  1596. }
  1597. /* Condition console terminal and communication line */
  1598. if (conbin((char)escape) < 0) {
  1599. printf("Sorry, can't condition console terminal\n");
  1600. goto conret0;
  1601. }
  1602. debug(F101,"CONNECT cmask","",cmask);
  1603. debug(F101,"CONNECT cmdmsk","",cmdmsk);
  1604. debug(F101,"CONNECT speed before ttvt","",speed);
  1605. if ((n = ttvt(speed,flow)) < 0) { /* Enter "virtual terminal" mode */
  1606. if (!network) {
  1607. debug(F101,"CONNECT ttvt","",n);
  1608. tthang(); /* Hang up and close the device. */
  1609. ttclos(0);
  1610. dologend();
  1611. if (ttopen(ttname, /* Open it again... */
  1612. &local,
  1613. network ? -nettype : mdmtyp,
  1614. 0
  1615. ) < 0) {
  1616. ckmakmsg(temp,TMPLEN,"Sorry, can't reopen ",ttname,NULL,NULL);
  1617. perror(temp);
  1618. goto conret0;
  1619. }
  1620. #ifdef IKS_OPTION
  1621. if (TELOPT_SB(TELOPT_KERMIT).kermit.u_start)
  1622. return(0);
  1623. #endif /* IKS_OPTION */
  1624. if (ttvt(speed,flow) < 0) { /* Try virtual terminal mode again. */
  1625. conres(); /* Failure this time is fatal. */
  1626. printf("Sorry, Can't condition communication line\n");
  1627. goto conret0;
  1628. }
  1629. }
  1630. }
  1631. debug(F101,"CONNECT ttvt ok, escape","",escape);
  1632. debug(F101,"CONNECT carrier-watch","",carrier);
  1633. if ((!network
  1634. #ifdef TN_COMPORT
  1635. || istncomport()
  1636. #endif /* TN_COMPORT */
  1637. ) && (carrier != CAR_OFF)) {
  1638. int x;
  1639. x = ttgmdm();
  1640. debug(F100,"CONNECT ttgmdm","",x);
  1641. if ((x > -1) && !(x & BM_DCD)) {
  1642. #ifndef NOHINTS
  1643. extern int hints;
  1644. #endif /* NOHINTS */
  1645. debug(F100,"CONNECT ttgmdm CD test fails","",x);
  1646. conres();
  1647. printf("?Carrier required but not detected.\n");
  1648. #ifndef NOHINTS
  1649. if (!hints)
  1650. return(0);
  1651. printf("***********************************\n");
  1652. printf(" Hint: To CONNECT to a serial device that\n");
  1653. printf(" is not presenting the Carrier Detect signal,\n");
  1654. printf(" first tell C-Kermit to:\n\n");
  1655. printf(" SET CARRIER-WATCH OFF\n\n");
  1656. printf("***********************************\n\n");
  1657. #endif /* NOHINTS */
  1658. goto conret0;
  1659. }
  1660. debug(F100,"CONNECT ttgmdm ok","",0);
  1661. }
  1662. #ifndef NOCSETS
  1663. /* Set up character set translations */
  1664. unicode = 0; /* Assume Unicode won't be involved */
  1665. tcs = 0; /* "Transfer" or "Other" charset */
  1666. sxo = rxo = NULL; /* Initialize byte-to-byte functions */
  1667. sxi = rxi = NULL;
  1668. if (tcsr != tcsl) { /* Remote and local sets differ... */
  1669. #ifdef UNICODE
  1670. if (tcsr == FC_UTF8 || /* Remote charset is UTF-8 */
  1671. tcsl == FC_UTF8) { /* or local one is. */
  1672. xuf = xl_ufc[tcsl]; /* Incoming Unicode to local */
  1673. if (xuf || tcsl == FC_UTF8) {
  1674. tcs = (tcsr == FC_UTF8) ? tcsl : tcsr; /* The "other" set */
  1675. xfu = xl_fcu[tcs]; /* Local byte to remote Unicode */
  1676. if (xfu)
  1677. unicode = (tcsr == FC_UTF8) ? 1 : 2;
  1678. }
  1679. tcssize = fcsinfo[tcs].size; /* Size of other character set. */
  1680. } else {
  1681. #endif /* UNICODE */
  1682. tcs = gettcs(tcsr,tcsl); /* Get intermediate set. */
  1683. sxo = xls[tcs][tcsl]; /* translation function */
  1684. rxo = xlr[tcs][tcsr]; /* pointers for output functions */
  1685. sxi = xls[tcs][tcsr]; /* and for input functions. */
  1686. rxi = xlr[tcs][tcsl];
  1687. #ifdef UNICODE
  1688. }
  1689. #endif /* UNICODE */
  1690. }
  1691. /*
  1692. This is to prevent use of zmstuff() and zdstuff() by translation functions.
  1693. They only work with disk i/o, not with communication i/o. Luckily Russian
  1694. translation functions don't do any stuffing...
  1695. */
  1696. langsv = language;
  1697. #ifndef NOCYRIL
  1698. if (language != L_RUSSIAN)
  1699. #endif /* NOCYRIL */
  1700. language = L_USASCII;
  1701. #ifdef COMMENT
  1702. #ifdef DEBUG
  1703. if (deblog) {
  1704. debug(F101,"CONNECT tcs","",tcs);
  1705. debug(F101,"CONNECT tcsl","",tcsl);
  1706. debug(F101,"CONNECT tcsr","",tcsr);
  1707. debug(F101,"CONNECT fcsinfo[tcsl].size","",fcsinfo[tcsl].size);
  1708. debug(F101,"CONNECT fcsinfo[tcsr].size","",fcsinfo[tcsr].size);
  1709. debug(F101,"CONNECT unicode","",unicode);
  1710. }
  1711. #endif /* DEBUG */
  1712. #endif /* COMMENT */
  1713. #ifdef CK_XYZ
  1714. #ifndef XYZ_INTERNAL
  1715. {
  1716. extern int binary; /* See about ZMODEM autodownloads */
  1717. char * s;
  1718. s = binary ? ptab[PROTO_Z].p_b_rcmd : ptab[PROTO_Z].p_t_rcmd;
  1719. if (!s) s = "";
  1720. zmdlok = (*s != NUL); /* OK if we have external commands */
  1721. }
  1722. #endif /* XYZ_INTERNAL */
  1723. #endif /* CK_XYZ */
  1724. #ifndef NOESCSEQ
  1725. /*
  1726. We need to activate the escape-sequence recognition feature when:
  1727. (a) translation is elected, AND
  1728. (b) the local and/or remote set is a 7-bit set other than US ASCII.
  1729. Or:
  1730. SET TERMINAL APC is not OFF (handled in the next statement).
  1731. */
  1732. escseq = (tcs != TC_TRANSP) && /* Not transparent */
  1733. (fcsinfo[tcsl].size == 128 || fcsinfo[tcsr].size == 128) && /* 7 bits */
  1734. (fcsinfo[tcsl].code != FC_USASCII); /* But not ASCII */
  1735. #endif /* NOESCSEQ */
  1736. #endif /* NOCSETS */
  1737. #ifndef NOESCSEQ
  1738. #ifdef CK_APC
  1739. escseq = escseq || (apcstatus & APC_ON);
  1740. apcactive = 0; /* An APC command is not active */
  1741. apclength = 0; /* ... */
  1742. #endif /* CK_APC */
  1743. inesc = ES_NORMAL; /* Initial state of recognizer */
  1744. debug(F101,"CONNECT escseq","",escseq);
  1745. #endif /* NOESCSEQ */
  1746. parent_id = getpid(); /* Get parent's pid for signalling */
  1747. debug(F101,"CONNECT parent pid","",parent_id);
  1748. if (xpipe[0] > -1) /* If old pipe hanging around, close */
  1749. close(xpipe[0]);
  1750. xpipe[0] = -1;
  1751. if (xpipe[1] > -1)
  1752. close(xpipe[1]);
  1753. xpipe[1] = -1;
  1754. goterr = 0; /* Error flag for pipe & fork */
  1755. if (pipe(xpipe) != 0) { /* Create new pipe to pass info */
  1756. perror("Can't make pipe"); /* between forks. */
  1757. debug(F101,"CONNECT pipe error","",errno);
  1758. goterr = 1;
  1759. } else
  1760. #ifdef BEOSORBEBOX
  1761. {
  1762. pid = spawn_thread(concld, "Lower Fork", B_NORMAL_PRIORITY, NULL);
  1763. resume_thread(pid);
  1764. }
  1765. #else
  1766. if ((pid = fork()) == (PID_T) -1) { /* Pipe OK, make port fork. */
  1767. perror("Can't make port fork");
  1768. debug(F101,"CONNECT fork error","",errno);
  1769. goterr = 1;
  1770. }
  1771. #endif /* BEOSORBEBOX */
  1772. debug(F101,"CONNECT created fork, pid","",pid);
  1773. if (goterr) { /* Failed to make pipe or fork */
  1774. conres(); /* Reset the console. */
  1775. if (msgflg) {
  1776. printf("\r\nCommunications disconnect (Back at %s)\r\n",
  1777. *myhost ?
  1778. myhost :
  1779. #ifdef UNIX
  1780. "local UNIX system"
  1781. #else
  1782. "local system"
  1783. #endif /* UNIX */
  1784. );
  1785. }
  1786. printf("\n");
  1787. what = W_NOTHING; /* So console modes are set right. */
  1788. #ifndef NOCSETS
  1789. language = langsv; /* Restore language */
  1790. #endif /* NOCSETS */
  1791. parent_id = (PID_T) 0; /* Clean up */
  1792. goto conret1;
  1793. }
  1794. debug(F101,"CONNECT fork pid","",pid);
  1795. /* Upper fork (KEYB fork) reads keystrokes and sends them out. */
  1796. if (pid) { /* pid != 0, so I am the upper fork. */
  1797. /*
  1798. Before doing anything significant, the child fork must wait for a go-ahead
  1799. character from xpipe[0]. Before starting to wait, we have enough time to
  1800. clear buffers and set up the signal handler. When done with this, we will
  1801. allow the child to continue by satisfying its pending read.
  1802. Remember the child and parent have separate address space. The child has
  1803. its own copy of input buffers, so we must clear the input buffers in the
  1804. parent. Otherwise strange effects may occur, like chunks of characters
  1805. repeatedly echoed on terminal screen. The child process is designed to
  1806. empty its input buffers by reading all available characters and either
  1807. echoing them on the terminal screen or saving them for future use in the
  1808. parent. The latter case happens during APC processing - see the code around
  1809. CEV_APC occurrences to see how the child passes its ibuf etc to parent via
  1810. xpipe, for preservation until the next entry to this module, to ensure that
  1811. no characters are lost between CONNECT sessions.
  1812. */
  1813. /*
  1814. This one needs a bit of extra explanation... In addition to the CONNECT
  1815. module's own buffers, which are communicated and synchronized via xpipe,
  1816. the low-level UNIX communication routines (ttinc, ttxin, etc) are also
  1817. buffered, statically, in the ckutio.c module. But when the two CONNECT
  1818. forks split off, the lower fork is updating this buffer's pointers and
  1819. counts, but the upper fork never finds out about it and still has the old
  1820. ones. The following UNIX-specific call to the ckutio.c module takes care
  1821. of this... Without it, we get dual echoing of incoming characters.
  1822. */
  1823. ttflux();
  1824. /*
  1825. At this point, perhaps you are wondering why we use forks at all. It is
  1826. simply because there is no other method portable among all UNIX variations.
  1827. Not threads, not select(), ... (Yes, select() is more common now; it might
  1828. actually be worth writing a variation of this module that works like BSD
  1829. Telnet, one fork, driven by select()).
  1830. */
  1831. ibp = ibuf; /* Clear ibuf[]. */
  1832. ibc = 0; /* Child now has its own copy */
  1833. signal(CK_FORK_SIG, pipeint); /* Handler for messages from child. */
  1834. write(xpipe[1], ibuf, 1); /* Allow child to proceed */
  1835. close(xpipe[1]); xpipe[1] = -1; /* Parent - prevent future writes */
  1836. what = W_CONNECT; /* Keep track of what we're doing */
  1837. active = 1;
  1838. debug(F101,"CONNECT keyboard fork duplex","",duplex);
  1839. /*
  1840. Catch communication errors or mode changes in lower fork.
  1841. Note: Some C compilers (e.g. Cray UNICOS) interpret the ANSI C standard
  1842. about setjmp() in a way that disallows constructions like:
  1843. if ((var = [sig]setjmp(env)) == 0) ...
  1844. which prevents the value returned by cklongjmp() from being used at all.
  1845. So the signal handlers set a global variable, sjval, instead.
  1846. */
  1847. if (
  1848. #ifdef CK_POSIX_SIG
  1849. sigsetjmp(con_env,1)
  1850. #else
  1851. setjmp(con_env)
  1852. #endif /* CK_POSIX_SIG */
  1853. == 0) { /* Normal entry... */
  1854. jbset = 1; /* Now we have a longjmp buffer */
  1855. sjval = CEV_NO; /* Initialize setjmp return code. */
  1856. debug(F101,"CONNECT setjmp normal entry","",sjval);
  1857. #ifdef ANYX25
  1858. if (network && (nettype == NET_SX25 || nettype == NET_IX25)) {
  1859. obufl = 0;
  1860. bzero (x25obuf,sizeof(x25obuf));
  1861. }
  1862. #endif /* ANYX25 */
  1863. /*
  1864. Here is the big loop that gets characters from the keyboard and sends them
  1865. out the communication device. There are two components to the communication
  1866. path: the connection from the keyboard to C-Kermit, and from C-Kermit to
  1867. the remote computer. The treatment of the 8th bit of keyboard characters
  1868. is governed by SET COMMAND BYTESIZE (cmdmsk). The treatment of the 8th bit
  1869. of characters sent to the remote is governed by SET TERMINAL BYTESIZE
  1870. (cmask). This distinction was introduced in edit 5A(164).
  1871. */
  1872. while (active) {
  1873. #ifndef NOSETKEY
  1874. if (kmptr) { /* Have current macro? */
  1875. debug(F100,"CONNECT kmptr non NULL","",0);
  1876. if ((c = (CHAR) *kmptr++) == NUL) { /* Get char from it */
  1877. kmptr = NULL; /* If no more chars, */
  1878. debug(F100,"CONNECT macro empty, continuing","",0);
  1879. continue; /* reset pointer and continue */
  1880. }
  1881. debug(F000,"CONNECT char from macro","",c);
  1882. } else /* No macro... */
  1883. #endif /* NOSETKEY */
  1884. c = CONGKS(); /* Read from keyboard */
  1885. #ifdef COMMENT
  1886. /* too much... */
  1887. debug(F101,"CONNECT ** KEYB","",c);
  1888. #endif /* COMMENT */
  1889. if (c == -1) { /* If read() got an error... */
  1890. debug(F101,"CONNECT keyboard read errno","",errno);
  1891. #ifdef COMMENT
  1892. /*
  1893. This seems to cause problems. If read() returns -1, the signal has already
  1894. been delivered, and nothing will wake up the pause().
  1895. */
  1896. pause(); /* Wait for transmitter to finish. */
  1897. #else
  1898. #ifdef A986
  1899. /*
  1900. On Altos machines with Xenix 3.0, pressing DEL in connect mode brings us
  1901. here (reason unknown). The console line discipline at this point has
  1902. intr = ^C. The communications tty has intr = DEL but we get here after
  1903. pressing DEL on the keyboard, even when the remote system has been set not
  1904. to echo. With A986 defined, we stay in the read loop and beep only if the
  1905. offending character is not DEL.
  1906. */
  1907. if ((c & 127) != 127) conoc(BEL);
  1908. #else
  1909. #ifdef EINTR
  1910. /*
  1911. This can be caused by the other fork signalling this one about
  1912. an echoing change during TELNET negotiations.
  1913. */
  1914. if (errno == EINTR)
  1915. continue;
  1916. #endif /* EINTR */
  1917. conoc(BEL); /* Otherwise, beep */
  1918. active = 0; /* and terminate the read loop */
  1919. continue;
  1920. #endif /* A986 */
  1921. #endif /* COMMENT */
  1922. }
  1923. c &= cmdmsk; /* Do any requested masking */
  1924. #ifndef NOSETKEY
  1925. /*
  1926. Note: kmptr is NULL if we got character c from the keyboard, and it is
  1927. not NULL if it came from a macro. In the latter case, we must avoid
  1928. expanding it again.
  1929. */
  1930. if (!kmptr && macrotab[c]) { /* Macro definition for c? */
  1931. kmptr = macrotab[c]; /* Yes, set up macro pointer */
  1932. continue; /* and restart the loop, */
  1933. } else c = keymap[c]; /* else use single-char keymap */
  1934. #endif /* NOSETKEY */
  1935. if (
  1936. #ifndef NOSETKEY
  1937. !kmptr &&
  1938. #endif /* NOSETKEY */
  1939. (tt_escape && (c & 0xff) == escape)) { /* Escape char? */
  1940. debug(F000,"CONNECT got escape","",c);
  1941. c = CONGKS() & 0177; /* Got esc, get its arg */
  1942. /* No key mapping here */
  1943. doesc((char) c); /* Now process it */
  1944. } else { /* It's not the escape character */
  1945. csave = c; /* Save it before translation */
  1946. /* for local echoing. */
  1947. #ifndef NOCSETS
  1948. if (inesc == ES_NORMAL) { /* If not inside escape seq.. */
  1949. /* Translate character sets */
  1950. #ifdef UNICODE
  1951. int x;
  1952. CHAR ch;
  1953. ch = c;
  1954. if (unicode == 1) { /* Remote is UTF-8 */
  1955. outxcount = b_to_u(ch,outxbuf,OUTXBUFSIZ,tcssize);
  1956. outxbuf[outxcount] = NUL;
  1957. } else if (unicode == 2) { /* Local is UTF-8 */
  1958. x = u_to_b(ch); /* So translate to remote byte */
  1959. if (x < 0)
  1960. continue;
  1961. outxbuf[0] = (unsigned)(x & 0xff);
  1962. outxcount = 1;
  1963. outxbuf[outxcount] = NUL;
  1964. } else {
  1965. #endif /* UNICODE */
  1966. /* Local-to-intermediate */
  1967. if (sxo) c = (*sxo)((char)c);
  1968. /* Intermediate-to-remote */
  1969. if (rxo) c = (*rxo)((char)c);
  1970. outxbuf[0] = c;
  1971. outxcount = 1;
  1972. outxbuf[outxcount] = NUL;
  1973. #ifdef UNICODE
  1974. }
  1975. #endif /* UNICODE */
  1976. }
  1977. if (escseq)
  1978. apcrc = chkaes((char)c);
  1979. #else
  1980. outxbuf[0] = c;
  1981. outxcount = 1;
  1982. outxbuf[outxcount] = NUL;
  1983. #endif /* NOCSETS */
  1984. for (i = 0; i < outxcount; i++) {
  1985. c = outxbuf[i];
  1986. /*
  1987. If Shift-In/Shift-Out is selected and we have a 7-bit connection,
  1988. handle shifting here.
  1989. */
  1990. if (sosi) { /* Shift-In/Out selected? */
  1991. if (cmask == 0177) { /* In 7-bit environment? */
  1992. if (c & 0200) { /* 8-bit character? */
  1993. if (outshift == 0) { /* If not shifted, */
  1994. ttoc(dopar(SO)); /* shift. */
  1995. outshift = 1;
  1996. }
  1997. } else {
  1998. if (outshift == 1) { /* 7-bit character */
  1999. ttoc(dopar(SI)); /* If shifted, */
  2000. outshift = 0; /* unshift. */
  2001. }
  2002. }
  2003. }
  2004. if (c == SO) outshift = 1; /* User typed SO */
  2005. if (c == SI) outshift = 0; /* User typed SI */
  2006. }
  2007. c &= cmask; /* Apply Kermit-to-host mask now. */
  2008. #ifdef SUNX25
  2009. if (network && nettype == NET_SX25) {
  2010. if (padparms[PAD_ECHO]) {
  2011. if (debses)
  2012. conol(dbchr(c)) ;
  2013. else
  2014. if ((c != padparms[PAD_CHAR_DELETE_CHAR]) &&
  2015. (c != padparms[PAD_BUFFER_DELETE_CHAR]) &&
  2016. (c != padparms[PAD_BUFFER_DISPLAY_CHAR]))
  2017. conoc(c) ;
  2018. if (seslog && !sessft)
  2019. logchar(c);
  2020. }
  2021. if (c == CR && (padparms[PAD_LF_AFTER_CR] == 4 ||
  2022. padparms[PAD_LF_AFTER_CR] == 5)) {
  2023. if (debses)
  2024. conol(dbchr(LF)) ;
  2025. else
  2026. conoc(LF) ;
  2027. if (seslog && !sessft)
  2028. logchar(LF);
  2029. }
  2030. if (c == padparms[PAD_BREAK_CHARACTER]) {
  2031. breakact();
  2032. } else if (padparms[PAD_DATA_FORWARD_TIMEOUT]) {
  2033. tosend = 1;
  2034. x25obuf [obufl++] = c;
  2035. } else if (((c == padparms[PAD_CHAR_DELETE_CHAR])||
  2036. (c == padparms[PAD_BUFFER_DELETE_CHAR]) ||
  2037. (c == padparms[PAD_BUFFER_DISPLAY_CHAR]))
  2038. && (padparms[PAD_EDITING])) {
  2039. if (c == padparms[PAD_CHAR_DELETE_CHAR]) {
  2040. if (obufl > 0) {
  2041. conol("\b \b"); obufl--;
  2042. } else {}
  2043. } else if
  2044. (c == padparms[PAD_BUFFER_DELETE_CHAR]) {
  2045. conol ("\r\nPAD Buffer Deleted\r\n");
  2046. obufl = 0;
  2047. } else if
  2048. (c==padparms[PAD_BUFFER_DISPLAY_CHAR]) {
  2049. conol("\r\n");
  2050. conol(x25obuf);
  2051. conol("\r\n");
  2052. } else {}
  2053. } else {
  2054. x25obuf [obufl++] = c;
  2055. if (obufl == MAXOX25) tosend = 1;
  2056. else if (c == CR) tosend = 1;
  2057. }
  2058. if (tosend) {
  2059. if (ttol((CHAR *)x25obuf,obufl) < 0) {
  2060. perror ("\r\nCan't send characters");
  2061. active = 0;
  2062. } else {
  2063. bzero (x25obuf,sizeof(x25obuf));
  2064. obufl = 0;
  2065. tosend = 0;
  2066. }
  2067. } else {};
  2068. } else {
  2069. #endif /* SUNX25 */
  2070. if (c == '\015') { /* Carriage Return */
  2071. int stuff = -1;
  2072. if (tnlm) { /* TERMINAL NEWLINE ON */
  2073. stuff = LF; /* Stuff LF */
  2074. #ifdef TNCODE
  2075. } else if (network && /* TELNET NEWLINE */
  2076. IS_TELNET()) {
  2077. switch (!TELOPT_ME(TELOPT_BINARY) ?
  2078. tn_nlm :
  2079. tn_b_nlm
  2080. ) {
  2081. case TNL_CRLF:
  2082. stuff = LF;
  2083. break;
  2084. case TNL_CRNUL:
  2085. stuff = NUL;
  2086. break;
  2087. }
  2088. #endif /* TNCODE */
  2089. }
  2090. if (stuff > -1) {
  2091. ttoc(dopar('\015')); /* Send CR */
  2092. if (duplex) conoc('\015'); /* Echo CR */
  2093. c = stuff; /* Char to stuff */
  2094. csave = c;
  2095. }
  2096. }
  2097. #ifdef TNCODE
  2098. /* If user types the 0xff character (TELNET IAC), it must be doubled. */
  2099. else /* Not CR */
  2100. if ((dopar((CHAR) c) == IAC) && /* IAC (0xff) */
  2101. network && IS_TELNET()) {
  2102. /* Send one copy now */
  2103. /* and the other one just below. */
  2104. ttoc((char)IAC);
  2105. }
  2106. #endif /* TNCODE */
  2107. /* Send the character */
  2108. if (ttoc((char)dopar((CHAR) c)) > -1) {
  2109. if (duplex) { /* If half duplex, must echo */
  2110. if (debses)
  2111. conol(dbchr(csave)); /* original char */
  2112. else /* not the translated one */
  2113. conoc((char)csave);
  2114. if (seslog) { /* And maybe log it too */
  2115. c2 = csave;
  2116. if (sessft == 0 && csave == '\r')
  2117. c2 = '\n';
  2118. logchar((char)c2);
  2119. }
  2120. }
  2121. } else {
  2122. perror("\r\nCan't send character");
  2123. active = 0;
  2124. }
  2125. #ifdef SUNX25
  2126. }
  2127. #endif /* SUNX25 */
  2128. } /* for... */
  2129. }
  2130. }
  2131. /* now active == 0 */
  2132. signal(CK_FORK_SIG, SIG_IGN); /* Turn off CK_FORK_SIG */
  2133. sjval = CEV_NO; /* Set to hangup */
  2134. } /* Come here on termination of child */
  2135. /* cklongjmp() executed in pipeint() (parent only!) comes here */
  2136. /*
  2137. Now the child fork is gone or is waiting for CK_FORK_SIG in ck_sndmsg().
  2138. So we can't get (in the parent) any subsequent CK_FORK_SIG signals until
  2139. we signal the child with CK_FORK_SIG.
  2140. */
  2141. debug(F100,"CONNECT signaling port fork","",0);
  2142. signal(CK_FORK_SIG, SIG_IGN); /* Turn this off */
  2143. debug(F101,"CONNECT killing port fork","",pid);
  2144. if (pid) {
  2145. int x = 0;
  2146. #ifdef BEOSORBEBOX
  2147. {
  2148. long ret_val;
  2149. x = kill(pid,SIGKILLTHR); /* Kill lower fork */
  2150. wait_for_thread(pid, &ret_val);
  2151. }
  2152. #else
  2153. #ifdef Plan9
  2154. x = kill(pid,SIGKILL); /* Kill lower fork */
  2155. #else
  2156. x = kill(pid,9);
  2157. #endif /* Plan9 */
  2158. #endif /* BEOSORBEBOX */
  2159. wait((WAIT_T *)0); /* Wait till gone. */
  2160. if (x < 0) {
  2161. printf("WARNING: Failure to kill fork, pid %d: %s, errno=%d\n",
  2162. (int) pid, ck_errstr(), errno);
  2163. debug(F111,"CONNECT error killing pid",ck_errstr(),errno);
  2164. }
  2165. debug(F101,"CONNECT killed port fork","",pid);
  2166. pid = (PID_T) 0;
  2167. }
  2168. if (sjval == CEV_HUP) { /* Read error on comm device */
  2169. dohangup = 1; /* so we want to hang up our side */
  2170. #ifdef NETCONN
  2171. if (network) { /* and/or close network connection */
  2172. ttclos(0);
  2173. dologend();
  2174. #ifdef SUNX25
  2175. if (nettype == NET_SX25) /* If X.25, restore the PAD params */
  2176. initpad();
  2177. #endif /* SUNX25 */
  2178. }
  2179. #endif /* NETCONN */
  2180. }
  2181. #ifdef CK_APC
  2182. if (sjval == CEV_APC) { /* Application Program Command rec'd */
  2183. apcactive = APC_REMOTE; /* Flag APC as active */
  2184. active = 0; /* Flag CONNECT as inactive */
  2185. }
  2186. #endif /* CK_APC */
  2187. conres(); /* Reset the console. */
  2188. if (dohangup > 0) { /* If hangup requested, do that. */
  2189. #ifndef NODIAL
  2190. if (dohangup > 1) /* User asked for it */
  2191. if (mdmhup() < 1) /* Maybe hang up via modem */
  2192. #endif /* NODIAL */
  2193. tthang(); /* And make sure we don't hang up */
  2194. dohangup = 0; /* again unless requested again. */
  2195. }
  2196. #ifdef COMMENT
  2197. #ifdef NETCONN
  2198. #ifdef SIGPIPE
  2199. if (network && sigpiph) /* Restore previous SIGPIPE handler */
  2200. (VOID) signal(SIGPIPE, sigpiph);
  2201. #endif /* SIGPIPE */
  2202. #endif /* NETCONN */
  2203. #endif /* COMMENT */
  2204. #ifdef ANYX25
  2205. if (dox25clr) { /* If X.25 Clear requested */
  2206. x25clear(); /* do that. */
  2207. #ifndef IBMX25
  2208. initpad();
  2209. #endif /* IBMX25 */
  2210. dox25clr = 0; /* But only once. */
  2211. }
  2212. #endif /* ANYX25 */
  2213. if (quitnow) doexit(GOOD_EXIT,xitsta); /* Exit now if requested. */
  2214. if (msgflg)
  2215. printf("(Back at %s)", *myhost ? myhost : "local UNIX system");
  2216. #ifdef CK_APC
  2217. if (!apcactive)
  2218. #endif /* CK_APC */
  2219. printf("\n");
  2220. what = W_NOTHING; /* So console modes set right. */
  2221. #ifndef NOCSETS
  2222. language = langsv; /* Restore language */
  2223. #endif /* NOCSETS */
  2224. parent_id = (PID_T) 0;
  2225. goto conret1;
  2226. }
  2227. #ifndef BEOSORBEBOX
  2228. else { /* *** */ /* Inferior reads, prints port input */
  2229. concld(/* (void *)&pid */);
  2230. }
  2231. #endif /* BEOSORBEBOX */
  2232. conret1:
  2233. conret = 1;
  2234. conret0:
  2235. signal(CK_FORK_SIG, SIG_IGN); /* In case this wasn't done already */
  2236. debug(F101,"CONNECT conect exit ibc","",ibc);
  2237. debug(F101,"CONNECT conect exit obc","",obc);
  2238. close(xpipe[0]); xpipe[0] = -1; /* Close the pipe */
  2239. close(xpipe[1]); xpipe[1] = -1;
  2240. if (msgflg) {
  2241. #ifdef CK_APC
  2242. if (apcactive == APC_LOCAL)
  2243. printf("\n");
  2244. #endif /* CK_APC */
  2245. printf("----------------------------------------------------\r\n");
  2246. }
  2247. fflush(stdout);
  2248. return(conret);
  2249. }
  2250. /* H C O N N E -- Give help message for connect. */
  2251. int
  2252. hconne() {
  2253. int c;
  2254. static char *hlpmsg[] = {
  2255. "\r\n ? for this message",
  2256. "\r\n 0 (zero) to send a null",
  2257. "\r\n B to send a BREAK",
  2258. #ifdef CK_LBRK
  2259. "\r\n L to send a Long BREAK",
  2260. #endif /* CK_LBRK */
  2261. #ifdef NETCONN
  2262. "\r\n I to send a network interrupt packet",
  2263. #ifdef TCPSOCKET
  2264. "\r\n A to send Are You There?",
  2265. #endif /* TCPSOCKET */
  2266. #ifdef ANYX25
  2267. "\r\n R to reset X.25 virtual circuit",
  2268. #endif /* ANYX25 */
  2269. #endif /* NETCONN */
  2270. "\r\n U to hangup and close the connection",
  2271. "\r\n Q to hangup and quit Kermit",
  2272. "\r\n S for status",
  2273. #ifdef NOPUSH
  2274. "\r\n ! to push to local shell (disabled)",
  2275. "\r\n Z to suspend (disabled)",
  2276. #else
  2277. "\r\n ! to push to local shell",
  2278. #ifdef NOJC
  2279. "\r\n Z to suspend (disabled)",
  2280. #else
  2281. "\r\n Z to suspend",
  2282. #endif /* NOJC */
  2283. #endif /* NOPUSH */
  2284. "\r\n \\ backslash code:",
  2285. "\r\n \\nnn decimal character code",
  2286. "\r\n \\Onnn octal character code",
  2287. "\r\n \\Xhh hexadecimal character code",
  2288. "\r\n terminate with carriage return.",
  2289. "\r\n Type the escape character again to send the escape character, or",
  2290. "\r\n press the space-bar to resume the CONNECT command.\r\n",
  2291. "" };
  2292. conol("\r\n----------------------------------------------------");
  2293. conol("\r\nPress C to return to ");
  2294. conol(*myhost ? myhost : "the C-Kermit prompt");
  2295. conol(", or:");
  2296. conola(hlpmsg); /* Print the help message. */
  2297. conol("Command>"); /* Prompt for command. */
  2298. c = CONGKS() & 0177; /* Get character, strip any parity. */
  2299. /* No key mapping or translation here */
  2300. if (c != CMDQ)
  2301. conoll("");
  2302. conoll("----------------------------------------------------");
  2303. return(c); /* Return it. */
  2304. }
  2305. /* D O E S C -- Process an escape character argument */
  2306. VOID
  2307. #ifdef CK_ANSIC
  2308. doesc(char c)
  2309. #else
  2310. doesc(c) char c;
  2311. #endif /* CK_ANSIC */
  2312. /* doesc */ {
  2313. CHAR d;
  2314. debug(F101,"CONNECT doesc","",c);
  2315. while (1) {
  2316. if (c == escape) { /* Send escape character */
  2317. d = dopar((CHAR) c); ttoc((char) d); return;
  2318. } else /* Or else look it up below. */
  2319. if (isupper(c)) c = tolower(c);
  2320. switch(c) {
  2321. case 'c': /* Escape back to prompt */
  2322. case '\03':
  2323. active = 0; conol("\r\n"); return;
  2324. case 'b': /* Send a BREAK signal */
  2325. case '\02':
  2326. ttsndb(); return;
  2327. #ifdef NETCONN
  2328. case 'i': /* Send Interrupt */
  2329. case '\011':
  2330. #ifdef TCPSOCKET
  2331. #ifndef IP
  2332. #define IP 244
  2333. #endif /* IP */
  2334. if (network && IS_TELNET()) { /* TELNET */
  2335. temp[0] = (CHAR) IAC; /* I Am a Command */
  2336. temp[1] = (CHAR) IP; /* Interrupt Process */
  2337. temp[2] = NUL;
  2338. ttol((CHAR *)temp,2);
  2339. } else
  2340. #endif /* TCPSOCKET */
  2341. #ifdef SUNX25
  2342. if (network && (nettype == NET_SX25)) {
  2343. (VOID) x25intr(0); /* X.25 interrupt packet */
  2344. conol("\r\n");
  2345. } else
  2346. #endif /* SUNX25 */
  2347. conoc(BEL);
  2348. return;
  2349. #ifdef TCPSOCKET
  2350. case 'a': /* "Are You There?" */
  2351. case '\01':
  2352. #ifndef AYT
  2353. #define AYT 246
  2354. #endif /* AYT */
  2355. if (network && IS_TELNET()) {
  2356. temp[0] = (CHAR) IAC; /* I Am a Command */
  2357. temp[1] = (CHAR) AYT; /* Are You There? */
  2358. temp[2] = NUL;
  2359. ttol((CHAR *)temp,2);
  2360. } else conoc(BEL);
  2361. return;
  2362. #endif /* TCPSOCKET */
  2363. #endif /* NETCONN */
  2364. #ifdef CK_LBRK
  2365. case 'l': /* Send a Long BREAK signal */
  2366. ttsndlb(); return;
  2367. #endif /* CK_LBRK */
  2368. case 'u': /* Hangup */
  2369. /* case '\010': */ /* No, too dangerous */
  2370. #ifdef ANYX25
  2371. if (network && (nettype == NET_SX25 || nettype == NET_IX25))
  2372. dox25clr = 1;
  2373. else
  2374. #endif /* ANYX25 */
  2375. dohangup = 2; active = 0; conol("\r\nHanging up "); return;
  2376. #ifdef ANYX25
  2377. case 'r': /* Reset the X.25 virtual circuit */
  2378. case '\022':
  2379. if (network && (nettype == NET_SX25 || nettype == NET_IX25))
  2380. (VOID) x25reset(0,0);
  2381. conol("\r\n");
  2382. return;
  2383. #endif /* ANYX25 */
  2384. case 'q': /* Quit */
  2385. dohangup = 2; quitnow = 1; active = 0; conol("\r\n"); return;
  2386. case 's': /* Status */
  2387. conoll("");
  2388. conoll("----------------------------------------------------");
  2389. #ifdef NETCMD
  2390. if (ttpipe)
  2391. ckmakmsg(temp,TMPLEN," Pipe: \"",ttname,"\"",NULL);
  2392. else
  2393. #endif /* NETCMD */
  2394. ckmakmsg(temp,
  2395. TMPLEN,
  2396. " ",
  2397. (network ? "Host" : "Device"),
  2398. ": ",
  2399. ttname
  2400. );
  2401. conoll(temp);
  2402. if (!network && speed >= 0L) {
  2403. sprintf(temp,"Speed %ld", speed);
  2404. conoll(temp);
  2405. }
  2406. sprintf(temp," Terminal echo: %s", duplex ? "local" : "remote");
  2407. conoll(temp);
  2408. sprintf(temp," Terminal bytesize: %d", (cmask == 0177) ? 7 : 8);
  2409. conoll(temp);
  2410. sprintf(temp," Command bytesize: %d", (cmdmsk == 0177) ? 7 : 8 );
  2411. conoll(temp);
  2412. if (hwparity)
  2413. sprintf(temp," Parity[hardware]: %s",parnam((char)hwparity));
  2414. else
  2415. sprintf(temp," Parity: %s", parnam((char)parity));
  2416. conoll(temp);
  2417. sprintf(temp," Autodownload: %s", autodl ? "on" : "off");
  2418. conoll(temp);
  2419. ckmakmsg(temp, /* (would not be safe for sprintf) */
  2420. TMPLEN,
  2421. " Session log: ",
  2422. *sesfil ? sesfil : "(none)",
  2423. NULL,
  2424. NULL
  2425. );
  2426. conoll(temp);
  2427. #ifndef NOSHOW
  2428. if (!network) shomdm();
  2429. #endif /* NOSHOW */
  2430. #ifdef CKLOGDIAL
  2431. {
  2432. long z;
  2433. z = dologshow(0);
  2434. if (z > -1L) {
  2435. sprintf(temp," Elapsed time: %s",hhmmss(z));
  2436. conoll(temp);
  2437. }
  2438. }
  2439. #endif /* CKLOGDIAL */
  2440. conoll("----------------------------------------------------");
  2441. return;
  2442. case 'h': /* Help */
  2443. case '?': /* Help */
  2444. c = hconne(); continue;
  2445. case '0': /* Send a null */
  2446. c = '\0'; d = dopar((CHAR) c); ttoc((char) d); return;
  2447. case 'z': case '\032': /* Suspend */
  2448. #ifndef NOPUSH
  2449. if (!nopush)
  2450. stptrap(0);
  2451. else
  2452. conoc(BEL);
  2453. #else
  2454. conoc(BEL);
  2455. #endif /* NOPUSH */
  2456. return;
  2457. case '@': /* Start inferior command processor */
  2458. case '!':
  2459. #ifndef NOPUSH
  2460. if (!nopush) {
  2461. conres(); /* Put console back to normal */
  2462. zshcmd(""); /* Fork a shell. */
  2463. if (conbin((char)escape) < 0) {
  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 */