| 1 |
/* |
| 2 |
* tclUnixChan.c |
| 3 |
* |
| 4 |
* Common channel driver for Unix channels based on files, command pipes |
| 5 |
* and TCP sockets. |
| 6 |
* |
| 7 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 1998-1999 Scriptics Corporation. |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution |
| 11 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclInt.h" /* Internal definitions for Tcl. */ |
| 15 |
#include "tclFileSystem.h" |
| 16 |
#include "tclIO.h" /* To get Channel type declaration. */ |
| 17 |
|
| 18 |
#undef SUPPORTS_TTY |
| 19 |
#if defined(HAVE_TERMIOS_H) |
| 20 |
# define SUPPORTS_TTY 1 |
| 21 |
# include <termios.h> |
| 22 |
# ifdef HAVE_SYS_IOCTL_H |
| 23 |
# include <sys/ioctl.h> |
| 24 |
# endif /* HAVE_SYS_IOCTL_H */ |
| 25 |
# ifdef HAVE_SYS_MODEM_H |
| 26 |
# include <sys/modem.h> |
| 27 |
# endif /* HAVE_SYS_MODEM_H */ |
| 28 |
|
| 29 |
# ifdef FIONREAD |
| 30 |
# define GETREADQUEUE(fd, int) ioctl((fd), FIONREAD, &(int)) |
| 31 |
# elif defined(FIORDCHK) |
| 32 |
# define GETREADQUEUE(fd, int) int = ioctl((fd), FIORDCHK, NULL) |
| 33 |
# else |
| 34 |
# define GETREADQUEUE(fd, int) int = 0 |
| 35 |
# endif |
| 36 |
|
| 37 |
# ifdef TIOCOUTQ |
| 38 |
# define GETWRITEQUEUE(fd, int) ioctl((fd), TIOCOUTQ, &(int)) |
| 39 |
# else |
| 40 |
# define GETWRITEQUEUE(fd, int) int = 0 |
| 41 |
# endif |
| 42 |
|
| 43 |
# if !defined(CRTSCTS) && defined(CNEW_RTSCTS) |
| 44 |
# define CRTSCTS CNEW_RTSCTS |
| 45 |
# endif /* !CRTSCTS&CNEW_RTSCTS */ |
| 46 |
# if !defined(PAREXT) && defined(CMSPAR) |
| 47 |
# define PAREXT CMSPAR |
| 48 |
# endif /* !PAREXT&&CMSPAR */ |
| 49 |
|
| 50 |
#endif /* HAVE_TERMIOS_H */ |
| 51 |
|
| 52 |
/* |
| 53 |
* Helper macros to make parts of this file clearer. The macros do exactly |
| 54 |
* what they say on the tin. :-) They also only ever refer to their arguments |
| 55 |
* once, and so can be used without regard to side effects. |
| 56 |
*/ |
| 57 |
|
| 58 |
#define SET_BITS(var, bits) ((var) |= (bits)) |
| 59 |
#define CLEAR_BITS(var, bits) ((var) &= ~(bits)) |
| 60 |
|
| 61 |
/* |
| 62 |
* This structure describes per-instance state of a file based channel. |
| 63 |
*/ |
| 64 |
|
| 65 |
typedef struct FileState { |
| 66 |
Tcl_Channel channel; /* Channel associated with this file. */ |
| 67 |
int fd; /* File handle. */ |
| 68 |
int validMask; /* OR'ed combination of TCL_READABLE, |
| 69 |
* TCL_WRITABLE, or TCL_EXCEPTION: indicates |
| 70 |
* which operations are valid on the file. */ |
| 71 |
} FileState; |
| 72 |
|
| 73 |
#ifdef SUPPORTS_TTY |
| 74 |
|
| 75 |
/* |
| 76 |
* The following structure is used to set or get the serial port attributes in |
| 77 |
* a platform-independent manner. |
| 78 |
*/ |
| 79 |
|
| 80 |
typedef struct TtyAttrs { |
| 81 |
int baud; |
| 82 |
int parity; |
| 83 |
int data; |
| 84 |
int stop; |
| 85 |
} TtyAttrs; |
| 86 |
|
| 87 |
#endif /* SUPPORTS_TTY */ |
| 88 |
|
| 89 |
#define UNSUPPORTED_OPTION(detail) \ |
| 90 |
if (interp) { \ |
| 91 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( \ |
| 92 |
"%s not supported for this platform", (detail))); \ |
| 93 |
Tcl_SetErrorCode(interp, "TCL", "UNSUPPORTED", (char *)NULL); \ |
| 94 |
} |
| 95 |
|
| 96 |
/* |
| 97 |
* Static routines for this file: |
| 98 |
*/ |
| 99 |
|
| 100 |
static int FileBlockModeProc(ClientData instanceData, int mode); |
| 101 |
static int FileCloseProc(ClientData instanceData, |
| 102 |
Tcl_Interp *interp); |
| 103 |
static int FileClose2Proc(ClientData instanceData, |
| 104 |
Tcl_Interp *interp, int flags); |
| 105 |
static int FileGetHandleProc(ClientData instanceData, |
| 106 |
int direction, ClientData *handlePtr); |
| 107 |
static int FileInputProc(ClientData instanceData, char *buf, |
| 108 |
int toRead, int *errorCode); |
| 109 |
static int FileOutputProc(ClientData instanceData, |
| 110 |
const char *buf, int toWrite, int *errorCode); |
| 111 |
static int FileSeekProc(ClientData instanceData, long offset, |
| 112 |
int mode, int *errorCode); |
| 113 |
static int FileTruncateProc(ClientData instanceData, |
| 114 |
Tcl_WideInt length); |
| 115 |
static Tcl_WideInt FileWideSeekProc(ClientData instanceData, |
| 116 |
Tcl_WideInt offset, int mode, int *errorCode); |
| 117 |
static void FileWatchProc(ClientData instanceData, int mask); |
| 118 |
#ifdef SUPPORTS_TTY |
| 119 |
static void TtyGetAttributes(int fd, TtyAttrs *ttyPtr); |
| 120 |
static int TtyGetOptionProc(ClientData instanceData, |
| 121 |
Tcl_Interp *interp, const char *optionName, |
| 122 |
Tcl_DString *dsPtr); |
| 123 |
static int TtyGetBaud(speed_t speed); |
| 124 |
static speed_t TtyGetSpeed(int baud); |
| 125 |
static void TtyInit(int fd); |
| 126 |
static void TtyModemStatusStr(int status, Tcl_DString *dsPtr); |
| 127 |
static int TtyParseMode(Tcl_Interp *interp, const char *mode, |
| 128 |
TtyAttrs *ttyPtr); |
| 129 |
static void TtySetAttributes(int fd, TtyAttrs *ttyPtr); |
| 130 |
static int TtySetOptionProc(ClientData instanceData, |
| 131 |
Tcl_Interp *interp, const char *optionName, |
| 132 |
const char *value); |
| 133 |
#endif /* SUPPORTS_TTY */ |
| 134 |
|
| 135 |
/* |
| 136 |
* This structure describes the channel type structure for file based IO: |
| 137 |
*/ |
| 138 |
|
| 139 |
static const Tcl_ChannelType fileChannelType = { |
| 140 |
"file", /* Type name. */ |
| 141 |
TCL_CHANNEL_VERSION_5, |
| 142 |
FileCloseProc, |
| 143 |
FileInputProc, |
| 144 |
FileOutputProc, |
| 145 |
FileSeekProc, |
| 146 |
NULL, /* Set option proc. */ |
| 147 |
NULL, /* Get option proc. */ |
| 148 |
FileWatchProc, |
| 149 |
FileGetHandleProc, |
| 150 |
FileClose2Proc, |
| 151 |
FileBlockModeProc, |
| 152 |
NULL, /* Flush proc. */ |
| 153 |
NULL, /* Bubbled event handler proc. */ |
| 154 |
FileWideSeekProc, |
| 155 |
NULL, /* Thread action proc. */ |
| 156 |
FileTruncateProc |
| 157 |
}; |
| 158 |
|
| 159 |
#ifdef SUPPORTS_TTY |
| 160 |
/* |
| 161 |
* This structure describes the channel type structure for serial IO. |
| 162 |
* Note that this type is a subclass of the "file" type. |
| 163 |
*/ |
| 164 |
|
| 165 |
static const Tcl_ChannelType ttyChannelType = { |
| 166 |
"tty", |
| 167 |
TCL_CHANNEL_VERSION_5, |
| 168 |
FileCloseProc, |
| 169 |
FileInputProc, |
| 170 |
FileOutputProc, |
| 171 |
NULL, /* Seek proc. */ |
| 172 |
TtySetOptionProc, |
| 173 |
TtyGetOptionProc, |
| 174 |
FileWatchProc, |
| 175 |
FileGetHandleProc, |
| 176 |
FileClose2Proc, |
| 177 |
FileBlockModeProc, |
| 178 |
NULL, /* Flush proc. */ |
| 179 |
NULL, /* Bubbled event handler proc. */ |
| 180 |
NULL, /* Seek proc. */ |
| 181 |
NULL, /* Thread action proc. */ |
| 182 |
NULL /* Truncate proc. */ |
| 183 |
}; |
| 184 |
#endif /* SUPPORTS_TTY */ |
| 185 |
|
| 186 |
/* |
| 187 |
*---------------------------------------------------------------------- |
| 188 |
* |
| 189 |
* FileBlockModeProc -- |
| 190 |
* |
| 191 |
* Helper function to set blocking and nonblocking modes on a file based |
| 192 |
* channel. Invoked by generic IO level code. |
| 193 |
* |
| 194 |
* Results: |
| 195 |
* 0 if successful, errno when failed. |
| 196 |
* |
| 197 |
* Side effects: |
| 198 |
* Sets the device into blocking or non-blocking mode. |
| 199 |
* |
| 200 |
*---------------------------------------------------------------------- |
| 201 |
*/ |
| 202 |
|
| 203 |
static int |
| 204 |
FileBlockModeProc( |
| 205 |
ClientData instanceData, /* File state. */ |
| 206 |
int mode) /* The mode to set. Can be TCL_MODE_BLOCKING |
| 207 |
* or TCL_MODE_NONBLOCKING. */ |
| 208 |
{ |
| 209 |
FileState *fsPtr = (FileState *)instanceData; |
| 210 |
|
| 211 |
if (TclUnixSetBlockingMode(fsPtr->fd, mode) < 0) { |
| 212 |
return errno; |
| 213 |
} |
| 214 |
|
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
/* |
| 219 |
*---------------------------------------------------------------------- |
| 220 |
* |
| 221 |
* FileInputProc -- |
| 222 |
* |
| 223 |
* This function is invoked from the generic IO level to read input from |
| 224 |
* a file based channel. |
| 225 |
* |
| 226 |
* Results: |
| 227 |
* The number of bytes read is returned or -1 on error. An output |
| 228 |
* argument contains a POSIX error code if an error occurs, or zero. |
| 229 |
* |
| 230 |
* Side effects: |
| 231 |
* Reads input from the input device of the channel. |
| 232 |
* |
| 233 |
*---------------------------------------------------------------------- |
| 234 |
*/ |
| 235 |
|
| 236 |
static int |
| 237 |
FileInputProc( |
| 238 |
ClientData instanceData, /* File state. */ |
| 239 |
char *buf, /* Where to store data read. */ |
| 240 |
int toRead, /* How much space is available in the |
| 241 |
* buffer? */ |
| 242 |
int *errorCodePtr) /* Where to store error code. */ |
| 243 |
{ |
| 244 |
FileState *fsPtr = (FileState *)instanceData; |
| 245 |
int bytesRead; /* How many bytes were actually read from the |
| 246 |
* input device? */ |
| 247 |
|
| 248 |
*errorCodePtr = 0; |
| 249 |
|
| 250 |
/* |
| 251 |
* Assume there is always enough input available. This will block |
| 252 |
* appropriately, and read will unblock as soon as a short read is |
| 253 |
* possible, if the channel is in blocking mode. If the channel is |
| 254 |
* nonblocking, the read will never block. |
| 255 |
*/ |
| 256 |
|
| 257 |
do { |
| 258 |
bytesRead = read(fsPtr->fd, buf, toRead); |
| 259 |
} while ((bytesRead < 0) && (errno == EINTR)); |
| 260 |
|
| 261 |
if (bytesRead < 0) { |
| 262 |
*errorCodePtr = errno; |
| 263 |
return -1; |
| 264 |
} |
| 265 |
return bytesRead; |
| 266 |
} |
| 267 |
|
| 268 |
/* |
| 269 |
*---------------------------------------------------------------------- |
| 270 |
* |
| 271 |
* FileOutputProc-- |
| 272 |
* |
| 273 |
* This function is invoked from the generic IO level to write output to |
| 274 |
* a file channel. |
| 275 |
* |
| 276 |
* Results: |
| 277 |
* The number of bytes written is returned or -1 on error. An output |
| 278 |
* argument contains a POSIX error code if an error occurred, or zero. |
| 279 |
* |
| 280 |
* Side effects: |
| 281 |
* Writes output on the output device of the channel. |
| 282 |
* |
| 283 |
*---------------------------------------------------------------------- |
| 284 |
*/ |
| 285 |
|
| 286 |
static int |
| 287 |
FileOutputProc( |
| 288 |
ClientData instanceData, /* File state. */ |
| 289 |
const char *buf, /* The data buffer. */ |
| 290 |
int toWrite, /* How many bytes to write? */ |
| 291 |
int *errorCodePtr) /* Where to store error code. */ |
| 292 |
{ |
| 293 |
FileState *fsPtr = (FileState *)instanceData; |
| 294 |
int written; |
| 295 |
|
| 296 |
*errorCodePtr = 0; |
| 297 |
|
| 298 |
if (toWrite == 0) { |
| 299 |
/* |
| 300 |
* SF Tcl Bug 465765. Do not try to write nothing into a file. STREAM |
| 301 |
* based implementations will considers this as EOF (if there is a |
| 302 |
* pipe behind the file). |
| 303 |
*/ |
| 304 |
|
| 305 |
return 0; |
| 306 |
} |
| 307 |
written = write(fsPtr->fd, buf, toWrite); |
| 308 |
if (written >= 0) { |
| 309 |
return written; |
| 310 |
} |
| 311 |
*errorCodePtr = errno; |
| 312 |
return -1; |
| 313 |
} |
| 314 |
|
| 315 |
/* |
| 316 |
*---------------------------------------------------------------------- |
| 317 |
* |
| 318 |
* FileCloseProc -- |
| 319 |
* |
| 320 |
* This function is called from the generic IO level to perform |
| 321 |
* channel-type-specific cleanup when a file based channel is closed. |
| 322 |
* |
| 323 |
* Results: |
| 324 |
* 0 if successful, errno if failed. |
| 325 |
* |
| 326 |
* Side effects: |
| 327 |
* Closes the device of the channel. |
| 328 |
* |
| 329 |
*---------------------------------------------------------------------- |
| 330 |
*/ |
| 331 |
|
| 332 |
static int |
| 333 |
FileCloseProc( |
| 334 |
ClientData instanceData, /* File state. */ |
| 335 |
Tcl_Interp *interp) /* For error reporting - unused. */ |
| 336 |
{ |
| 337 |
FileState *fsPtr = (FileState *)instanceData; |
| 338 |
int errorCode = 0; |
| 339 |
|
| 340 |
Tcl_DeleteFileHandler(fsPtr->fd); |
| 341 |
|
| 342 |
/* |
| 343 |
* Do not close standard channels while in thread-exit. |
| 344 |
*/ |
| 345 |
|
| 346 |
if (!TclInThreadExit() |
| 347 |
|| ((fsPtr->fd != 0) && (fsPtr->fd != 1) && (fsPtr->fd != 2))) { |
| 348 |
if (close(fsPtr->fd) < 0) { |
| 349 |
errorCode = errno; |
| 350 |
} |
| 351 |
} |
| 352 |
ckfree(fsPtr); |
| 353 |
return errorCode; |
| 354 |
} |
| 355 |
static int |
| 356 |
FileClose2Proc( |
| 357 |
ClientData instanceData, /* File state. */ |
| 358 |
Tcl_Interp *interp, /* For error reporting - unused. */ |
| 359 |
int flags) |
| 360 |
{ |
| 361 |
if ((flags & (TCL_CLOSE_READ | TCL_CLOSE_WRITE)) == 0) { |
| 362 |
return FileCloseProc(instanceData, interp); |
| 363 |
} |
| 364 |
return EINVAL; |
| 365 |
} |
| 366 |
|
| 367 |
/* |
| 368 |
*---------------------------------------------------------------------- |
| 369 |
* |
| 370 |
* FileSeekProc -- |
| 371 |
* |
| 372 |
* This function is called by the generic IO level to move the access |
| 373 |
* point in a file based channel. |
| 374 |
* |
| 375 |
* Results: |
| 376 |
* -1 if failed, the new position if successful. An output argument |
| 377 |
* contains the POSIX error code if an error occurred, or zero. |
| 378 |
* |
| 379 |
* Side effects: |
| 380 |
* Moves the location at which the channel will be accessed in future |
| 381 |
* operations. |
| 382 |
* |
| 383 |
*---------------------------------------------------------------------- |
| 384 |
*/ |
| 385 |
|
| 386 |
static int |
| 387 |
FileSeekProc( |
| 388 |
ClientData instanceData, /* File state. */ |
| 389 |
long offset, /* Offset to seek to. */ |
| 390 |
int mode, /* Relative to where should we seek? Can be |
| 391 |
* one of SEEK_START, SEEK_SET or SEEK_END. */ |
| 392 |
int *errorCodePtr) /* To store error code. */ |
| 393 |
{ |
| 394 |
FileState *fsPtr = (FileState *)instanceData; |
| 395 |
Tcl_WideInt oldLoc, newLoc; |
| 396 |
|
| 397 |
/* |
| 398 |
* Save our current place in case we need to roll-back the seek. |
| 399 |
*/ |
| 400 |
|
| 401 |
oldLoc = TclOSseek(fsPtr->fd, (Tcl_SeekOffset) 0, SEEK_CUR); |
| 402 |
if (oldLoc == Tcl_LongAsWide(-1)) { |
| 403 |
/* |
| 404 |
* Bad things are happening. Error out... |
| 405 |
*/ |
| 406 |
|
| 407 |
*errorCodePtr = errno; |
| 408 |
return -1; |
| 409 |
} |
| 410 |
|
| 411 |
newLoc = TclOSseek(fsPtr->fd, (Tcl_SeekOffset) offset, mode); |
| 412 |
|
| 413 |
/* |
| 414 |
* Check for expressability in our return type, and roll-back otherwise. |
| 415 |
*/ |
| 416 |
|
| 417 |
if (newLoc > Tcl_LongAsWide(INT_MAX)) { |
| 418 |
*errorCodePtr = EOVERFLOW; |
| 419 |
TclOSseek(fsPtr->fd, (Tcl_SeekOffset) oldLoc, SEEK_SET); |
| 420 |
return -1; |
| 421 |
} else { |
| 422 |
*errorCodePtr = (newLoc == Tcl_LongAsWide(-1)) ? errno : 0; |
| 423 |
} |
| 424 |
return (int) Tcl_WideAsLong(newLoc); |
| 425 |
} |
| 426 |
|
| 427 |
/* |
| 428 |
*---------------------------------------------------------------------- |
| 429 |
* |
| 430 |
* FileWideSeekProc -- |
| 431 |
* |
| 432 |
* This function is called by the generic IO level to move the access |
| 433 |
* point in a file based channel, with offsets expressed as wide |
| 434 |
* integers. |
| 435 |
* |
| 436 |
* Results: |
| 437 |
* -1 if failed, the new position if successful. An output argument |
| 438 |
* contains the POSIX error code if an error occurred, or zero. |
| 439 |
* |
| 440 |
* Side effects: |
| 441 |
* Moves the location at which the channel will be accessed in future |
| 442 |
* operations. |
| 443 |
* |
| 444 |
*---------------------------------------------------------------------- |
| 445 |
*/ |
| 446 |
|
| 447 |
static Tcl_WideInt |
| 448 |
FileWideSeekProc( |
| 449 |
ClientData instanceData, /* File state. */ |
| 450 |
Tcl_WideInt offset, /* Offset to seek to. */ |
| 451 |
int mode, /* Relative to where should we seek? Can be |
| 452 |
* one of SEEK_START, SEEK_CUR or SEEK_END. */ |
| 453 |
int *errorCodePtr) /* To store error code. */ |
| 454 |
{ |
| 455 |
FileState *fsPtr = (FileState *)instanceData; |
| 456 |
Tcl_WideInt newLoc; |
| 457 |
|
| 458 |
newLoc = TclOSseek(fsPtr->fd, (Tcl_SeekOffset) offset, mode); |
| 459 |
|
| 460 |
*errorCodePtr = (newLoc == -1) ? errno : 0; |
| 461 |
return newLoc; |
| 462 |
} |
| 463 |
|
| 464 |
/* |
| 465 |
*---------------------------------------------------------------------- |
| 466 |
* |
| 467 |
* FileWatchProc -- |
| 468 |
* |
| 469 |
* Initialize the notifier to watch the fd from this channel. |
| 470 |
* |
| 471 |
* Results: |
| 472 |
* None. |
| 473 |
* |
| 474 |
* Side effects: |
| 475 |
* Sets up the notifier so that a future event on the channel will |
| 476 |
* be seen by Tcl. |
| 477 |
* |
| 478 |
*---------------------------------------------------------------------- |
| 479 |
*/ |
| 480 |
|
| 481 |
/* |
| 482 |
* Bug ad5a57f2f271: Tcl_NotifyChannel is not a Tcl_FileProc, |
| 483 |
* so do not pass it to directly to Tcl_CreateFileHandler. |
| 484 |
* Instead, pass a wrapper which is a Tcl_FileProc. |
| 485 |
*/ |
| 486 |
static void |
| 487 |
FileWatchNotifyChannelWrapper( |
| 488 |
ClientData clientData, |
| 489 |
int mask) |
| 490 |
{ |
| 491 |
Tcl_Channel channel = (Tcl_Channel)clientData; |
| 492 |
Tcl_NotifyChannel(channel, mask); |
| 493 |
} |
| 494 |
|
| 495 |
static void |
| 496 |
FileWatchProc( |
| 497 |
ClientData instanceData, /* The file state. */ |
| 498 |
int mask) /* Events of interest; an OR-ed combination of |
| 499 |
* TCL_READABLE, TCL_WRITABLE and |
| 500 |
* TCL_EXCEPTION. */ |
| 501 |
{ |
| 502 |
FileState *fsPtr = (FileState *)instanceData; |
| 503 |
|
| 504 |
/* |
| 505 |
* Make sure we only register for events that are valid on this file. |
| 506 |
*/ |
| 507 |
|
| 508 |
mask &= fsPtr->validMask; |
| 509 |
if (mask) { |
| 510 |
Tcl_CreateFileHandler(fsPtr->fd, mask, |
| 511 |
FileWatchNotifyChannelWrapper, fsPtr->channel); |
| 512 |
} else { |
| 513 |
Tcl_DeleteFileHandler(fsPtr->fd); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
/* |
| 518 |
*---------------------------------------------------------------------- |
| 519 |
* |
| 520 |
* FileGetHandleProc -- |
| 521 |
* |
| 522 |
* Called from Tcl_GetChannelHandle to retrieve OS handles from a file |
| 523 |
* based channel. |
| 524 |
* |
| 525 |
* Results: |
| 526 |
* Returns TCL_OK with the fd in handlePtr, or TCL_ERROR if there is no |
| 527 |
* handle for the specified direction. |
| 528 |
* |
| 529 |
* Side effects: |
| 530 |
* None. |
| 531 |
* |
| 532 |
*---------------------------------------------------------------------- |
| 533 |
*/ |
| 534 |
|
| 535 |
static int |
| 536 |
FileGetHandleProc( |
| 537 |
ClientData instanceData, /* The file state. */ |
| 538 |
int direction, /* TCL_READABLE or TCL_WRITABLE */ |
| 539 |
ClientData *handlePtr) /* Where to store the handle. */ |
| 540 |
{ |
| 541 |
FileState *fsPtr = (FileState *)instanceData; |
| 542 |
|
| 543 |
if (direction & fsPtr->validMask) { |
| 544 |
*handlePtr = INT2PTR(fsPtr->fd); |
| 545 |
return TCL_OK; |
| 546 |
} |
| 547 |
return TCL_ERROR; |
| 548 |
} |
| 549 |
|
| 550 |
#ifdef SUPPORTS_TTY |
| 551 |
/* |
| 552 |
*---------------------------------------------------------------------- |
| 553 |
* |
| 554 |
* TtyModemStatusStr -- |
| 555 |
* |
| 556 |
* Converts a RS232 modem status list of readable flags |
| 557 |
* |
| 558 |
*---------------------------------------------------------------------- |
| 559 |
*/ |
| 560 |
|
| 561 |
static void |
| 562 |
TtyModemStatusStr( |
| 563 |
int status, /* RS232 modem status */ |
| 564 |
Tcl_DString *dsPtr) /* Where to store string */ |
| 565 |
{ |
| 566 |
#ifdef TIOCM_CTS |
| 567 |
Tcl_DStringAppendElement(dsPtr, "CTS"); |
| 568 |
Tcl_DStringAppendElement(dsPtr, (status & TIOCM_CTS) ? "1" : "0"); |
| 569 |
#endif /* TIOCM_CTS */ |
| 570 |
#ifdef TIOCM_DSR |
| 571 |
Tcl_DStringAppendElement(dsPtr, "DSR"); |
| 572 |
Tcl_DStringAppendElement(dsPtr, (status & TIOCM_DSR) ? "1" : "0"); |
| 573 |
#endif /* TIOCM_DSR */ |
| 574 |
#ifdef TIOCM_RNG |
| 575 |
Tcl_DStringAppendElement(dsPtr, "RING"); |
| 576 |
Tcl_DStringAppendElement(dsPtr, (status & TIOCM_RNG) ? "1" : "0"); |
| 577 |
#endif /* TIOCM_RNG */ |
| 578 |
#ifdef TIOCM_CD |
| 579 |
Tcl_DStringAppendElement(dsPtr, "DCD"); |
| 580 |
Tcl_DStringAppendElement(dsPtr, (status & TIOCM_CD) ? "1" : "0"); |
| 581 |
#endif /* TIOCM_CD */ |
| 582 |
} |
| 583 |
|
| 584 |
/* |
| 585 |
*---------------------------------------------------------------------- |
| 586 |
* |
| 587 |
* TtySetOptionProc -- |
| 588 |
* |
| 589 |
* Sets an option on a channel. |
| 590 |
* |
| 591 |
* Results: |
| 592 |
* A standard Tcl result. Also sets the interp's result on error if |
| 593 |
* interp is not NULL. |
| 594 |
* |
| 595 |
* Side effects: |
| 596 |
* May modify an option on a device. Sets Error message if needed (by |
| 597 |
* calling Tcl_BadChannelOption). |
| 598 |
* |
| 599 |
*---------------------------------------------------------------------- |
| 600 |
*/ |
| 601 |
|
| 602 |
static int |
| 603 |
TtySetOptionProc( |
| 604 |
ClientData instanceData, /* File state. */ |
| 605 |
Tcl_Interp *interp, /* For error reporting - can be NULL. */ |
| 606 |
const char *optionName, /* Which option to set? */ |
| 607 |
const char *value) /* New value for option. */ |
| 608 |
{ |
| 609 |
FileState *fsPtr = (FileState *)instanceData; |
| 610 |
size_t len, vlen; |
| 611 |
TtyAttrs tty; |
| 612 |
int argc; |
| 613 |
const char **argv; |
| 614 |
struct termios iostate; |
| 615 |
|
| 616 |
len = strlen(optionName); |
| 617 |
vlen = strlen(value); |
| 618 |
|
| 619 |
/* |
| 620 |
* Option -mode baud,parity,databits,stopbits |
| 621 |
*/ |
| 622 |
|
| 623 |
if ((len > 2) && (strncmp(optionName, "-mode", len) == 0)) { |
| 624 |
if (TtyParseMode(interp, value, &tty) != TCL_OK) { |
| 625 |
return TCL_ERROR; |
| 626 |
} |
| 627 |
|
| 628 |
/* |
| 629 |
* system calls results should be checked there. - dl |
| 630 |
*/ |
| 631 |
|
| 632 |
TtySetAttributes(fsPtr->fd, &tty); |
| 633 |
return TCL_OK; |
| 634 |
} |
| 635 |
|
| 636 |
/* |
| 637 |
* Option -handshake none|xonxoff|rtscts|dtrdsr |
| 638 |
*/ |
| 639 |
|
| 640 |
if ((len > 1) && (strncmp(optionName, "-handshake", len) == 0)) { |
| 641 |
/* |
| 642 |
* Reset all handshake options. DTR and RTS are ON by default. |
| 643 |
*/ |
| 644 |
|
| 645 |
tcgetattr(fsPtr->fd, &iostate); |
| 646 |
CLEAR_BITS(iostate.c_iflag, IXON | IXOFF | IXANY); |
| 647 |
#ifdef CRTSCTS |
| 648 |
CLEAR_BITS(iostate.c_cflag, CRTSCTS); |
| 649 |
#endif /* CRTSCTS */ |
| 650 |
if (strncasecmp(value, "NONE", vlen) == 0) { |
| 651 |
/* |
| 652 |
* Leave all handshake options disabled. |
| 653 |
*/ |
| 654 |
} else if (strncasecmp(value, "XONXOFF", vlen) == 0) { |
| 655 |
SET_BITS(iostate.c_iflag, IXON | IXOFF | IXANY); |
| 656 |
} else if (strncasecmp(value, "RTSCTS", vlen) == 0) { |
| 657 |
#ifdef CRTSCTS |
| 658 |
SET_BITS(iostate.c_cflag, CRTSCTS); |
| 659 |
#else /* !CRTSTS */ |
| 660 |
UNSUPPORTED_OPTION("-handshake RTSCTS"); |
| 661 |
return TCL_ERROR; |
| 662 |
#endif /* CRTSCTS */ |
| 663 |
} else if (strncasecmp(value, "DTRDSR", vlen) == 0) { |
| 664 |
UNSUPPORTED_OPTION("-handshake DTRDSR"); |
| 665 |
return TCL_ERROR; |
| 666 |
} else { |
| 667 |
if (interp) { |
| 668 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 669 |
"bad value for -handshake: must be one of" |
| 670 |
" xonxoff, rtscts, dtrdsr or none", -1)); |
| 671 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "FCONFIGURE", |
| 672 |
"VALUE", (char *)NULL); |
| 673 |
} |
| 674 |
return TCL_ERROR; |
| 675 |
} |
| 676 |
tcsetattr(fsPtr->fd, TCSADRAIN, &iostate); |
| 677 |
return TCL_OK; |
| 678 |
} |
| 679 |
|
| 680 |
/* |
| 681 |
* Option -xchar {\x11 \x13} |
| 682 |
*/ |
| 683 |
|
| 684 |
if ((len > 1) && (strncmp(optionName, "-xchar", len) == 0)) { |
| 685 |
Tcl_DString ds; |
| 686 |
|
| 687 |
if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) { |
| 688 |
return TCL_ERROR; |
| 689 |
} else if (argc != 2) { |
| 690 |
if (interp) { |
| 691 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 692 |
"bad value for -xchar: should be a list of" |
| 693 |
" two elements", -1)); |
| 694 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "FCONFIGURE", |
| 695 |
"VALUE", NULL); |
| 696 |
} |
| 697 |
ckfree(argv); |
| 698 |
return TCL_ERROR; |
| 699 |
} |
| 700 |
|
| 701 |
tcgetattr(fsPtr->fd, &iostate); |
| 702 |
|
| 703 |
Tcl_UtfToExternalDString(NULL, argv[0], -1, &ds); |
| 704 |
iostate.c_cc[VSTART] = *(const cc_t *) Tcl_DStringValue(&ds); |
| 705 |
TclDStringClear(&ds); |
| 706 |
|
| 707 |
Tcl_UtfToExternalDString(NULL, argv[1], -1, &ds); |
| 708 |
iostate.c_cc[VSTOP] = *(const cc_t *) Tcl_DStringValue(&ds); |
| 709 |
Tcl_DStringFree(&ds); |
| 710 |
ckfree(argv); |
| 711 |
|
| 712 |
tcsetattr(fsPtr->fd, TCSADRAIN, &iostate); |
| 713 |
return TCL_OK; |
| 714 |
} |
| 715 |
|
| 716 |
/* |
| 717 |
* Option -timeout msec |
| 718 |
*/ |
| 719 |
|
| 720 |
if ((len > 2) && (strncmp(optionName, "-timeout", len) == 0)) { |
| 721 |
int msec; |
| 722 |
|
| 723 |
tcgetattr(fsPtr->fd, &iostate); |
| 724 |
if (Tcl_GetInt(interp, value, &msec) != TCL_OK) { |
| 725 |
return TCL_ERROR; |
| 726 |
} |
| 727 |
iostate.c_cc[VMIN] = 0; |
| 728 |
iostate.c_cc[VTIME] = (msec==0) ? 0 : (msec<100) ? 1 : (msec+50)/100; |
| 729 |
tcsetattr(fsPtr->fd, TCSADRAIN, &iostate); |
| 730 |
return TCL_OK; |
| 731 |
} |
| 732 |
|
| 733 |
/* |
| 734 |
* Option -ttycontrol {DTR 1 RTS 0 BREAK 0} |
| 735 |
*/ |
| 736 |
|
| 737 |
if ((len > 4) && (strncmp(optionName, "-ttycontrol", len) == 0)) { |
| 738 |
#if defined(TIOCMGET) && defined(TIOCMSET) |
| 739 |
int control, flag; |
| 740 |
int i; |
| 741 |
|
| 742 |
if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) { |
| 743 |
return TCL_ERROR; |
| 744 |
} |
| 745 |
if ((argc % 2) == 1) { |
| 746 |
if (interp) { |
| 747 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 748 |
"bad value for -ttycontrol: should be a list of" |
| 749 |
" signal,value pairs", -1)); |
| 750 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "FCONFIGURE", |
| 751 |
"VALUE", (char *)NULL); |
| 752 |
} |
| 753 |
ckfree(argv); |
| 754 |
return TCL_ERROR; |
| 755 |
} |
| 756 |
|
| 757 |
ioctl(fsPtr->fd, TIOCMGET, &control); |
| 758 |
for (i = 0; i < argc-1; i += 2) { |
| 759 |
if (Tcl_GetBoolean(interp, argv[i+1], &flag) == TCL_ERROR) { |
| 760 |
ckfree(argv); |
| 761 |
return TCL_ERROR; |
| 762 |
} |
| 763 |
if (strncasecmp(argv[i], "DTR", strlen(argv[i])) == 0) { |
| 764 |
if (flag) { |
| 765 |
SET_BITS(control, TIOCM_DTR); |
| 766 |
} else { |
| 767 |
CLEAR_BITS(control, TIOCM_DTR); |
| 768 |
} |
| 769 |
} else if (strncasecmp(argv[i], "RTS", strlen(argv[i])) == 0) { |
| 770 |
if (flag) { |
| 771 |
SET_BITS(control, TIOCM_RTS); |
| 772 |
} else { |
| 773 |
CLEAR_BITS(control, TIOCM_RTS); |
| 774 |
} |
| 775 |
} else if (strncasecmp(argv[i], "BREAK", strlen(argv[i])) == 0) { |
| 776 |
#if defined(TIOCSBRK) && defined(TIOCCBRK) |
| 777 |
if (flag) { |
| 778 |
ioctl(fsPtr->fd, TIOCSBRK, NULL); |
| 779 |
} else { |
| 780 |
ioctl(fsPtr->fd, TIOCCBRK, NULL); |
| 781 |
} |
| 782 |
#else /* TIOCSBRK & TIOCCBRK */ |
| 783 |
UNSUPPORTED_OPTION("-ttycontrol BREAK"); |
| 784 |
ckfree(argv); |
| 785 |
return TCL_ERROR; |
| 786 |
#endif /* TIOCSBRK & TIOCCBRK */ |
| 787 |
} else { |
| 788 |
if (interp) { |
| 789 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 790 |
"bad signal \"%s\" for -ttycontrol: must be" |
| 791 |
" DTR, RTS or BREAK", argv[i])); |
| 792 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "FCONFIGURE", |
| 793 |
"VALUE", (char *)NULL); |
| 794 |
} |
| 795 |
ckfree(argv); |
| 796 |
return TCL_ERROR; |
| 797 |
} |
| 798 |
} /* -ttycontrol options loop */ |
| 799 |
|
| 800 |
ioctl(fsPtr->fd, TIOCMSET, &control); |
| 801 |
ckfree(argv); |
| 802 |
return TCL_OK; |
| 803 |
#else /* TIOCMGET&TIOCMSET */ |
| 804 |
UNSUPPORTED_OPTION("-ttycontrol"); |
| 805 |
#endif /* TIOCMGET&TIOCMSET */ |
| 806 |
} |
| 807 |
|
| 808 |
return Tcl_BadChannelOption(interp, optionName, |
| 809 |
"mode handshake timeout ttycontrol xchar"); |
| 810 |
} |
| 811 |
|
| 812 |
/* |
| 813 |
*---------------------------------------------------------------------- |
| 814 |
* |
| 815 |
* TtyGetOptionProc -- |
| 816 |
* |
| 817 |
* Gets a mode associated with an IO channel. If the optionName arg is |
| 818 |
* non-NULL, retrieves the value of that option. If the optionName arg is |
| 819 |
* NULL, retrieves a list of alternating option names and values for the |
| 820 |
* given channel. |
| 821 |
* |
| 822 |
* Results: |
| 823 |
* A standard Tcl result. Also sets the supplied DString to the string |
| 824 |
* value of the option(s) returned. Sets error message if needed |
| 825 |
* (by calling Tcl_BadChannelOption). |
| 826 |
* |
| 827 |
*---------------------------------------------------------------------- |
| 828 |
*/ |
| 829 |
|
| 830 |
static int |
| 831 |
TtyGetOptionProc( |
| 832 |
ClientData instanceData, /* File state. */ |
| 833 |
Tcl_Interp *interp, /* For error reporting - can be NULL. */ |
| 834 |
const char *optionName, /* Option to get. */ |
| 835 |
Tcl_DString *dsPtr) /* Where to store value(s). */ |
| 836 |
{ |
| 837 |
FileState *fsPtr = (FileState *)instanceData; |
| 838 |
size_t len; |
| 839 |
char buf[3*TCL_INTEGER_SPACE + 16]; |
| 840 |
int valid = 0; /* Flag if valid option parsed. */ |
| 841 |
|
| 842 |
if (optionName == NULL) { |
| 843 |
len = 0; |
| 844 |
} else { |
| 845 |
len = strlen(optionName); |
| 846 |
} |
| 847 |
if (len == 0) { |
| 848 |
Tcl_DStringAppendElement(dsPtr, "-mode"); |
| 849 |
} |
| 850 |
if (len==0 || (len>2 && strncmp(optionName, "-mode", len)==0)) { |
| 851 |
TtyAttrs tty; |
| 852 |
|
| 853 |
valid = 1; |
| 854 |
TtyGetAttributes(fsPtr->fd, &tty); |
| 855 |
snprintf(buf, sizeof(buf), "%d,%c,%d,%d", tty.baud, tty.parity, tty.data, tty.stop); |
| 856 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 857 |
} |
| 858 |
|
| 859 |
/* |
| 860 |
* Get option -xchar |
| 861 |
*/ |
| 862 |
|
| 863 |
if (len == 0) { |
| 864 |
Tcl_DStringAppendElement(dsPtr, "-xchar"); |
| 865 |
Tcl_DStringStartSublist(dsPtr); |
| 866 |
} |
| 867 |
if (len==0 || (len>1 && strncmp(optionName, "-xchar", len)==0)) { |
| 868 |
struct termios iostate; |
| 869 |
Tcl_DString ds; |
| 870 |
|
| 871 |
valid = 1; |
| 872 |
tcgetattr(fsPtr->fd, &iostate); |
| 873 |
Tcl_DStringInit(&ds); |
| 874 |
|
| 875 |
Tcl_ExternalToUtfDString(NULL, (char *)&iostate.c_cc[VSTART], 1, &ds); |
| 876 |
Tcl_DStringAppendElement(dsPtr, Tcl_DStringValue(&ds)); |
| 877 |
TclDStringClear(&ds); |
| 878 |
|
| 879 |
Tcl_ExternalToUtfDString(NULL, (char *)&iostate.c_cc[VSTOP], 1, &ds); |
| 880 |
Tcl_DStringAppendElement(dsPtr, Tcl_DStringValue(&ds)); |
| 881 |
Tcl_DStringFree(&ds); |
| 882 |
} |
| 883 |
if (len == 0) { |
| 884 |
Tcl_DStringEndSublist(dsPtr); |
| 885 |
} |
| 886 |
|
| 887 |
/* |
| 888 |
* Get option -queue |
| 889 |
* Option is readonly and returned by [fconfigure chan -queue] but not |
| 890 |
* returned by unnamed [fconfigure chan]. |
| 891 |
*/ |
| 892 |
|
| 893 |
if ((len > 1) && (strncmp(optionName, "-queue", len) == 0)) { |
| 894 |
int inQueue=0, outQueue=0, inBuffered, outBuffered; |
| 895 |
|
| 896 |
valid = 1; |
| 897 |
GETREADQUEUE(fsPtr->fd, inQueue); |
| 898 |
GETWRITEQUEUE(fsPtr->fd, outQueue); |
| 899 |
inBuffered = Tcl_InputBuffered(fsPtr->channel); |
| 900 |
outBuffered = Tcl_OutputBuffered(fsPtr->channel); |
| 901 |
|
| 902 |
snprintf(buf, sizeof(buf), "%d", inBuffered+inQueue); |
| 903 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 904 |
snprintf(buf, sizeof(buf), "%d", outBuffered+outQueue); |
| 905 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 906 |
} |
| 907 |
|
| 908 |
#if defined(TIOCMGET) |
| 909 |
/* |
| 910 |
* Get option -ttystatus |
| 911 |
* Option is readonly and returned by [fconfigure chan -ttystatus] but not |
| 912 |
* returned by unnamed [fconfigure chan]. |
| 913 |
*/ |
| 914 |
|
| 915 |
if ((len > 4) && (strncmp(optionName, "-ttystatus", len) == 0)) { |
| 916 |
int status; |
| 917 |
|
| 918 |
valid = 1; |
| 919 |
ioctl(fsPtr->fd, TIOCMGET, &status); |
| 920 |
TtyModemStatusStr(status, dsPtr); |
| 921 |
} |
| 922 |
#endif /* TIOCMGET */ |
| 923 |
|
| 924 |
if (valid) { |
| 925 |
return TCL_OK; |
| 926 |
} |
| 927 |
return Tcl_BadChannelOption(interp, optionName, |
| 928 |
"mode queue ttystatus xchar"); |
| 929 |
} |
| 930 |
|
| 931 |
static const struct {int baud; speed_t speed;} speeds[] = { |
| 932 |
#ifdef B0 |
| 933 |
{0, B0}, |
| 934 |
#endif |
| 935 |
#ifdef B50 |
| 936 |
{50, B50}, |
| 937 |
#endif |
| 938 |
#ifdef B75 |
| 939 |
{75, B75}, |
| 940 |
#endif |
| 941 |
#ifdef B110 |
| 942 |
{110, B110}, |
| 943 |
#endif |
| 944 |
#ifdef B134 |
| 945 |
{134, B134}, |
| 946 |
#endif |
| 947 |
#ifdef B150 |
| 948 |
{150, B150}, |
| 949 |
#endif |
| 950 |
#ifdef B200 |
| 951 |
{200, B200}, |
| 952 |
#endif |
| 953 |
#ifdef B300 |
| 954 |
{300, B300}, |
| 955 |
#endif |
| 956 |
#ifdef B600 |
| 957 |
{600, B600}, |
| 958 |
#endif |
| 959 |
#ifdef B1200 |
| 960 |
{1200, B1200}, |
| 961 |
#endif |
| 962 |
#ifdef B1800 |
| 963 |
{1800, B1800}, |
| 964 |
#endif |
| 965 |
#ifdef B2400 |
| 966 |
{2400, B2400}, |
| 967 |
#endif |
| 968 |
#ifdef B4800 |
| 969 |
{4800, B4800}, |
| 970 |
#endif |
| 971 |
#ifdef B9600 |
| 972 |
{9600, B9600}, |
| 973 |
#endif |
| 974 |
#ifdef B14400 |
| 975 |
{14400, B14400}, |
| 976 |
#endif |
| 977 |
#ifdef B19200 |
| 978 |
{19200, B19200}, |
| 979 |
#endif |
| 980 |
#ifdef EXTA |
| 981 |
{19200, EXTA}, |
| 982 |
#endif |
| 983 |
#ifdef B28800 |
| 984 |
{28800, B28800}, |
| 985 |
#endif |
| 986 |
#ifdef B38400 |
| 987 |
{38400, B38400}, |
| 988 |
#endif |
| 989 |
#ifdef EXTB |
| 990 |
{38400, EXTB}, |
| 991 |
#endif |
| 992 |
#ifdef B57600 |
| 993 |
{57600, B57600}, |
| 994 |
#endif |
| 995 |
#ifdef _B57600 |
| 996 |
{57600, _B57600}, |
| 997 |
#endif |
| 998 |
#ifdef B76800 |
| 999 |
{76800, B76800}, |
| 1000 |
#endif |
| 1001 |
#ifdef B115200 |
| 1002 |
{115200, B115200}, |
| 1003 |
#endif |
| 1004 |
#ifdef _B115200 |
| 1005 |
{115200, _B115200}, |
| 1006 |
#endif |
| 1007 |
#ifdef B153600 |
| 1008 |
{153600, B153600}, |
| 1009 |
#endif |
| 1010 |
#ifdef B230400 |
| 1011 |
{230400, B230400}, |
| 1012 |
#endif |
| 1013 |
#ifdef B307200 |
| 1014 |
{307200, B307200}, |
| 1015 |
#endif |
| 1016 |
#ifdef B460800 |
| 1017 |
{460800, B460800}, |
| 1018 |
#endif |
| 1019 |
#ifdef B500000 |
| 1020 |
{500000, B500000}, |
| 1021 |
#endif |
| 1022 |
#ifdef B576000 |
| 1023 |
{576000, B576000}, |
| 1024 |
#endif |
| 1025 |
#ifdef B921600 |
| 1026 |
{921600, B921600}, |
| 1027 |
#endif |
| 1028 |
#ifdef B1000000 |
| 1029 |
{1000000, B1000000}, |
| 1030 |
#endif |
| 1031 |
#ifdef B1152000 |
| 1032 |
{1152000, B1152000}, |
| 1033 |
#endif |
| 1034 |
#ifdef B1500000 |
| 1035 |
{1500000,B1500000}, |
| 1036 |
#endif |
| 1037 |
#ifdef B2000000 |
| 1038 |
{2000000, B2000000}, |
| 1039 |
#endif |
| 1040 |
#ifdef B2500000 |
| 1041 |
{2500000,B2500000}, |
| 1042 |
#endif |
| 1043 |
#ifdef B3000000 |
| 1044 |
{3000000,B3000000}, |
| 1045 |
#endif |
| 1046 |
#ifdef B3500000 |
| 1047 |
{3500000,B3500000}, |
| 1048 |
#endif |
| 1049 |
#ifdef B4000000 |
| 1050 |
{4000000,B4000000}, |
| 1051 |
#endif |
| 1052 |
{-1, 0} |
| 1053 |
}; |
| 1054 |
|
| 1055 |
/* |
| 1056 |
*--------------------------------------------------------------------------- |
| 1057 |
* |
| 1058 |
* TtyGetSpeed -- |
| 1059 |
* |
| 1060 |
* Given an integer baud rate, get the speed_t value that should be |
| 1061 |
* used to select that baud rate. |
| 1062 |
* |
| 1063 |
* Results: |
| 1064 |
* As above. |
| 1065 |
* |
| 1066 |
*--------------------------------------------------------------------------- |
| 1067 |
*/ |
| 1068 |
|
| 1069 |
static speed_t |
| 1070 |
TtyGetSpeed( |
| 1071 |
int baud) /* The baud rate to look up. */ |
| 1072 |
{ |
| 1073 |
int bestIdx, bestDiff, i, diff; |
| 1074 |
|
| 1075 |
bestIdx = 0; |
| 1076 |
bestDiff = 1000000; |
| 1077 |
|
| 1078 |
/* |
| 1079 |
* If the baud rate does not correspond to one of the known mask values, |
| 1080 |
* choose the mask value whose baud rate is closest to the specified baud |
| 1081 |
* rate. |
| 1082 |
*/ |
| 1083 |
|
| 1084 |
for (i = 0; speeds[i].baud >= 0; i++) { |
| 1085 |
diff = speeds[i].baud - baud; |
| 1086 |
if (diff < 0) { |
| 1087 |
diff = -diff; |
| 1088 |
} |
| 1089 |
if (diff < bestDiff) { |
| 1090 |
bestIdx = i; |
| 1091 |
bestDiff = diff; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
return speeds[bestIdx].speed; |
| 1095 |
} |
| 1096 |
|
| 1097 |
/* |
| 1098 |
*--------------------------------------------------------------------------- |
| 1099 |
* |
| 1100 |
* TtyGetBaud -- |
| 1101 |
* |
| 1102 |
* Return the integer baud rate corresponding to a given speed_t value. |
| 1103 |
* |
| 1104 |
* Results: |
| 1105 |
* As above. If the mask value was not recognized, 0 is returned. |
| 1106 |
* |
| 1107 |
*--------------------------------------------------------------------------- |
| 1108 |
*/ |
| 1109 |
|
| 1110 |
static int |
| 1111 |
TtyGetBaud( |
| 1112 |
speed_t speed) /* Speed mask value to look up. */ |
| 1113 |
{ |
| 1114 |
int i; |
| 1115 |
|
| 1116 |
for (i = 0; speeds[i].baud >= 0; i++) { |
| 1117 |
if (speeds[i].speed == speed) { |
| 1118 |
return speeds[i].baud; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
return 0; |
| 1122 |
} |
| 1123 |
|
| 1124 |
/* |
| 1125 |
*--------------------------------------------------------------------------- |
| 1126 |
* |
| 1127 |
* TtyGetAttributes -- |
| 1128 |
* |
| 1129 |
* Get the current attributes of the specified serial device. |
| 1130 |
* |
| 1131 |
* Results: |
| 1132 |
* None. |
| 1133 |
* |
| 1134 |
* Side effects: |
| 1135 |
* None. |
| 1136 |
* |
| 1137 |
*--------------------------------------------------------------------------- |
| 1138 |
*/ |
| 1139 |
|
| 1140 |
static void |
| 1141 |
TtyGetAttributes( |
| 1142 |
int fd, /* Open file descriptor for serial port to be |
| 1143 |
* queried. */ |
| 1144 |
TtyAttrs *ttyPtr) /* Buffer filled with serial port |
| 1145 |
* attributes. */ |
| 1146 |
{ |
| 1147 |
struct termios iostate; |
| 1148 |
int baud, parity, data, stop; |
| 1149 |
|
| 1150 |
tcgetattr(fd, &iostate); |
| 1151 |
|
| 1152 |
baud = TtyGetBaud(cfgetospeed(&iostate)); |
| 1153 |
|
| 1154 |
parity = 'n'; |
| 1155 |
#ifdef PAREXT |
| 1156 |
switch ((int) (iostate.c_cflag & (PARENB | PARODD | PAREXT))) { |
| 1157 |
case PARENB : parity = 'e'; break; |
| 1158 |
case PARENB | PARODD : parity = 'o'; break; |
| 1159 |
case PARENB | PAREXT : parity = 's'; break; |
| 1160 |
case PARENB | PARODD | PAREXT : parity = 'm'; break; |
| 1161 |
} |
| 1162 |
#else /* !PAREXT */ |
| 1163 |
switch ((int) (iostate.c_cflag & (PARENB | PARODD))) { |
| 1164 |
case PARENB : parity = 'e'; break; |
| 1165 |
case PARENB | PARODD : parity = 'o'; break; |
| 1166 |
} |
| 1167 |
#endif /* PAREXT */ |
| 1168 |
|
| 1169 |
data = iostate.c_cflag & CSIZE; |
| 1170 |
data = (data == CS5) ? 5 : (data == CS6) ? 6 : (data == CS7) ? 7 : 8; |
| 1171 |
|
| 1172 |
stop = (iostate.c_cflag & CSTOPB) ? 2 : 1; |
| 1173 |
|
| 1174 |
ttyPtr->baud = baud; |
| 1175 |
ttyPtr->parity = parity; |
| 1176 |
ttyPtr->data = data; |
| 1177 |
ttyPtr->stop = stop; |
| 1178 |
} |
| 1179 |
|
| 1180 |
/* |
| 1181 |
*--------------------------------------------------------------------------- |
| 1182 |
* |
| 1183 |
* TtySetAttributes -- |
| 1184 |
* |
| 1185 |
* Set the current attributes of the specified serial device. |
| 1186 |
* |
| 1187 |
* Results: |
| 1188 |
* None. |
| 1189 |
* |
| 1190 |
* Side effects: |
| 1191 |
* None. |
| 1192 |
* |
| 1193 |
*--------------------------------------------------------------------------- |
| 1194 |
*/ |
| 1195 |
|
| 1196 |
static void |
| 1197 |
TtySetAttributes( |
| 1198 |
int fd, /* Open file descriptor for serial port to be |
| 1199 |
* modified. */ |
| 1200 |
TtyAttrs *ttyPtr) /* Buffer containing new attributes for serial |
| 1201 |
* port. */ |
| 1202 |
{ |
| 1203 |
struct termios iostate; |
| 1204 |
int parity, data, flag; |
| 1205 |
|
| 1206 |
tcgetattr(fd, &iostate); |
| 1207 |
cfsetospeed(&iostate, TtyGetSpeed(ttyPtr->baud)); |
| 1208 |
cfsetispeed(&iostate, TtyGetSpeed(ttyPtr->baud)); |
| 1209 |
|
| 1210 |
flag = 0; |
| 1211 |
parity = ttyPtr->parity; |
| 1212 |
if (parity != 'n') { |
| 1213 |
SET_BITS(flag, PARENB); |
| 1214 |
#ifdef PAREXT |
| 1215 |
CLEAR_BITS(iostate.c_cflag, PAREXT); |
| 1216 |
if ((parity == 'm') || (parity == 's')) { |
| 1217 |
SET_BITS(flag, PAREXT); |
| 1218 |
} |
| 1219 |
#endif /* PAREXT */ |
| 1220 |
if ((parity == 'm') || (parity == 'o')) { |
| 1221 |
SET_BITS(flag, PARODD); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
data = ttyPtr->data; |
| 1225 |
SET_BITS(flag, |
| 1226 |
(data == 5) ? CS5 : |
| 1227 |
(data == 6) ? CS6 : |
| 1228 |
(data == 7) ? CS7 : CS8); |
| 1229 |
if (ttyPtr->stop == 2) { |
| 1230 |
SET_BITS(flag, CSTOPB); |
| 1231 |
} |
| 1232 |
|
| 1233 |
CLEAR_BITS(iostate.c_cflag, PARENB | PARODD | CSIZE | CSTOPB); |
| 1234 |
SET_BITS(iostate.c_cflag, flag); |
| 1235 |
|
| 1236 |
tcsetattr(fd, TCSADRAIN, &iostate); |
| 1237 |
} |
| 1238 |
|
| 1239 |
/* |
| 1240 |
*--------------------------------------------------------------------------- |
| 1241 |
* |
| 1242 |
* TtyParseMode -- |
| 1243 |
* |
| 1244 |
* Parse the "-mode" argument to the fconfigure command. The argument is |
| 1245 |
* of the form baud,parity,data,stop. |
| 1246 |
* |
| 1247 |
* Results: |
| 1248 |
* The return value is TCL_OK if the argument was successfully parsed, |
| 1249 |
* TCL_ERROR otherwise. If TCL_ERROR is returned, an error message is |
| 1250 |
* left in the interp's result (if interp is non-NULL). |
| 1251 |
* |
| 1252 |
*--------------------------------------------------------------------------- |
| 1253 |
*/ |
| 1254 |
|
| 1255 |
static int |
| 1256 |
TtyParseMode( |
| 1257 |
Tcl_Interp *interp, /* If non-NULL, interp for error return. */ |
| 1258 |
const char *mode, /* Mode string to be parsed. */ |
| 1259 |
TtyAttrs *ttyPtr) /* Filled with data from mode string */ |
| 1260 |
{ |
| 1261 |
int i, end; |
| 1262 |
char parity; |
| 1263 |
const char *bad = "bad value for -mode"; |
| 1264 |
|
| 1265 |
i = sscanf(mode, "%d,%c,%d,%d%n", |
| 1266 |
&ttyPtr->baud, |
| 1267 |
&parity, |
| 1268 |
&ttyPtr->data, |
| 1269 |
&ttyPtr->stop, &end); |
| 1270 |
if ((i != 4) || (mode[end] != '\0')) { |
| 1271 |
if (interp != NULL) { |
| 1272 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1273 |
"%s: should be baud,parity,data,stop", bad)); |
| 1274 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "SERIALMODE", (char *)NULL); |
| 1275 |
} |
| 1276 |
return TCL_ERROR; |
| 1277 |
} |
| 1278 |
|
| 1279 |
/* |
| 1280 |
* Only allow setting mark/space parity on platforms that support it Make |
| 1281 |
* sure to allow for the case where strchr is a macro. [Bug: 5089] |
| 1282 |
* |
| 1283 |
* We cannot if/else/endif the strchr arguments, it has to be the whole |
| 1284 |
* function. On AIX this function is apparently a macro, and macros do |
| 1285 |
* not allow preprocessor directives in their arguments. |
| 1286 |
*/ |
| 1287 |
|
| 1288 |
#ifdef PAREXT |
| 1289 |
#define PARITY_CHARS "noems" |
| 1290 |
#define PARITY_MSG "n, o, e, m, or s" |
| 1291 |
#else |
| 1292 |
#define PARITY_CHARS "noe" |
| 1293 |
#define PARITY_MSG "n, o, or e" |
| 1294 |
#endif /* PAREXT */ |
| 1295 |
|
| 1296 |
if (strchr(PARITY_CHARS, parity) == NULL) { |
| 1297 |
if (interp != NULL) { |
| 1298 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1299 |
"%s parity: should be %s", bad, PARITY_MSG)); |
| 1300 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "SERIALMODE", (char *)NULL); |
| 1301 |
} |
| 1302 |
return TCL_ERROR; |
| 1303 |
} |
| 1304 |
ttyPtr->parity = parity; |
| 1305 |
if ((ttyPtr->data < 5) || (ttyPtr->data > 8)) { |
| 1306 |
if (interp != NULL) { |
| 1307 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1308 |
"%s data: should be 5, 6, 7, or 8", bad)); |
| 1309 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "SERIALMODE", (char *)NULL); |
| 1310 |
} |
| 1311 |
return TCL_ERROR; |
| 1312 |
} |
| 1313 |
if ((ttyPtr->stop < 0) || (ttyPtr->stop > 2)) { |
| 1314 |
if (interp != NULL) { |
| 1315 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1316 |
"%s stop: should be 1 or 2", bad)); |
| 1317 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "SERIALMODE", (char *)NULL); |
| 1318 |
} |
| 1319 |
return TCL_ERROR; |
| 1320 |
} |
| 1321 |
return TCL_OK; |
| 1322 |
} |
| 1323 |
|
| 1324 |
/* |
| 1325 |
*--------------------------------------------------------------------------- |
| 1326 |
* |
| 1327 |
* TtyInit -- |
| 1328 |
* |
| 1329 |
* Given file descriptor that refers to a serial port, initialize the |
| 1330 |
* serial port to a set of sane values so that Tcl can talk to a device |
| 1331 |
* located on the serial port. |
| 1332 |
* |
| 1333 |
* Side effects: |
| 1334 |
* Serial device initialized to non-blocking raw mode, similar to sockets |
| 1335 |
* All other modes can be simulated on top of this in Tcl. |
| 1336 |
* |
| 1337 |
*--------------------------------------------------------------------------- |
| 1338 |
*/ |
| 1339 |
|
| 1340 |
static void |
| 1341 |
TtyInit( |
| 1342 |
int fd) /* Open file descriptor for serial port to be |
| 1343 |
* initialized. */ |
| 1344 |
{ |
| 1345 |
struct termios iostate; |
| 1346 |
tcgetattr(fd, &iostate); |
| 1347 |
|
| 1348 |
if (iostate.c_iflag != IGNBRK |
| 1349 |
|| iostate.c_oflag != 0 |
| 1350 |
|| iostate.c_lflag != 0 |
| 1351 |
|| iostate.c_cflag & CREAD |
| 1352 |
|| iostate.c_cc[VMIN] != 1 |
| 1353 |
|| iostate.c_cc[VTIME] != 0) { |
| 1354 |
iostate.c_iflag = IGNBRK; |
| 1355 |
iostate.c_oflag = 0; |
| 1356 |
iostate.c_lflag = 0; |
| 1357 |
iostate.c_cflag |= CREAD; |
| 1358 |
iostate.c_cc[VMIN] = 1; |
| 1359 |
iostate.c_cc[VTIME] = 0; |
| 1360 |
|
| 1361 |
tcsetattr(fd, TCSADRAIN, &iostate); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
#endif /* SUPPORTS_TTY */ |
| 1365 |
|
| 1366 |
/* |
| 1367 |
*---------------------------------------------------------------------- |
| 1368 |
* |
| 1369 |
* TclpOpenFileChannel -- |
| 1370 |
* |
| 1371 |
* Open an file based channel on Unix systems. |
| 1372 |
* |
| 1373 |
* Results: |
| 1374 |
* The new channel or NULL. If NULL, the output argument errorCodePtr is |
| 1375 |
* set to a POSIX error and an error message is left in the interp's |
| 1376 |
* result if interp is not NULL. |
| 1377 |
* |
| 1378 |
* Side effects: |
| 1379 |
* May open the channel and may cause creation of a file on the file |
| 1380 |
* system. |
| 1381 |
* |
| 1382 |
*---------------------------------------------------------------------- |
| 1383 |
*/ |
| 1384 |
|
| 1385 |
Tcl_Channel |
| 1386 |
TclpOpenFileChannel( |
| 1387 |
Tcl_Interp *interp, /* Interpreter for error reporting; can be |
| 1388 |
* NULL. */ |
| 1389 |
Tcl_Obj *pathPtr, /* Name of file to open. */ |
| 1390 |
int mode, /* POSIX open mode. */ |
| 1391 |
int permissions) /* If the open involves creating a file, with |
| 1392 |
* what modes to create it? */ |
| 1393 |
{ |
| 1394 |
int fd, channelPermissions; |
| 1395 |
FileState *fsPtr; |
| 1396 |
const char *native, *translation; |
| 1397 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 1398 |
const Tcl_ChannelType *channelTypePtr; |
| 1399 |
|
| 1400 |
switch (mode & O_ACCMODE) { |
| 1401 |
case O_RDONLY: |
| 1402 |
channelPermissions = TCL_READABLE; |
| 1403 |
break; |
| 1404 |
case O_WRONLY: |
| 1405 |
channelPermissions = TCL_WRITABLE; |
| 1406 |
break; |
| 1407 |
case O_RDWR: |
| 1408 |
channelPermissions = (TCL_READABLE | TCL_WRITABLE); |
| 1409 |
break; |
| 1410 |
default: |
| 1411 |
/* |
| 1412 |
* This may occurr if modeString was "", for example. |
| 1413 |
*/ |
| 1414 |
|
| 1415 |
Tcl_Panic("TclpOpenFileChannel: invalid mode value"); |
| 1416 |
return NULL; |
| 1417 |
} |
| 1418 |
|
| 1419 |
native = (const char *)Tcl_FSGetNativePath(pathPtr); |
| 1420 |
if (native == NULL) { |
| 1421 |
if (interp != (Tcl_Interp *) NULL) { |
| 1422 |
/* |
| 1423 |
* We need this just to ensure we return the correct error messages under |
| 1424 |
* some circumstances (relative paths only), so because the normalization |
| 1425 |
* is very expensive, don't invoke it for native or absolute paths. |
| 1426 |
* Note: since paths starting with ~ are absolute, it also considers tilde expansion, |
| 1427 |
* (proper error message of tests *io-40.17 "tilde substitution in open") |
| 1428 |
*/ |
| 1429 |
if ( |
| 1430 |
( |
| 1431 |
( |
| 1432 |
!TclFSCwdIsNative() && |
| 1433 |
(Tcl_FSGetPathType(pathPtr) != TCL_PATH_ABSOLUTE) |
| 1434 |
) || |
| 1435 |
(*TclGetString(pathPtr) == '~') /* possible tilde expansion */ |
| 1436 |
) && |
| 1437 |
Tcl_FSGetNormalizedPath(interp, pathPtr) == NULL |
| 1438 |
) { |
| 1439 |
return NULL; |
| 1440 |
} |
| 1441 |
|
| 1442 |
Tcl_AppendResult(interp, "couldn't open \"", |
| 1443 |
TclGetString(pathPtr), "\": filename is invalid on this platform", |
| 1444 |
(char *)NULL); |
| 1445 |
} |
| 1446 |
return NULL; |
| 1447 |
} |
| 1448 |
|
| 1449 |
#ifdef DJGPP |
| 1450 |
SET_BITS(mode, O_BINARY); |
| 1451 |
#endif |
| 1452 |
|
| 1453 |
fd = TclOSopen(native, mode, permissions); |
| 1454 |
|
| 1455 |
if (fd < 0) { |
| 1456 |
if (interp != NULL) { |
| 1457 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1458 |
"couldn't open \"%s\": %s", |
| 1459 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1460 |
} |
| 1461 |
return NULL; |
| 1462 |
} |
| 1463 |
|
| 1464 |
/* |
| 1465 |
* Set close-on-exec flag on the fd so that child processes will not |
| 1466 |
* inherit this fd. |
| 1467 |
*/ |
| 1468 |
|
| 1469 |
fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 1470 |
|
| 1471 |
snprintf(channelName, sizeof(channelName), "file%d", fd); |
| 1472 |
|
| 1473 |
#ifdef SUPPORTS_TTY |
| 1474 |
if (strcmp(native, "/dev/tty") != 0 && isatty(fd)) { |
| 1475 |
/* |
| 1476 |
* Initialize the serial port to a set of sane parameters. Especially |
| 1477 |
* important if the remote device is set to echo and the serial port |
| 1478 |
* driver was also set to echo -- as soon as a char were sent to the |
| 1479 |
* serial port, the remote device would echo it, then the serial |
| 1480 |
* driver would echo it back to the device, etc. |
| 1481 |
* |
| 1482 |
* Note that we do not do this if we're dealing with /dev/tty itself, |
| 1483 |
* as that tends to cause Bad Things To Happen when you're working |
| 1484 |
* interactively. Strictly a better check would be to see if the FD |
| 1485 |
* being set up is a device and has the same major/minor as the |
| 1486 |
* initial std FDs (beware reopening!) but that's nearly as messy. |
| 1487 |
*/ |
| 1488 |
|
| 1489 |
translation = "auto crlf"; |
| 1490 |
channelTypePtr = &ttyChannelType; |
| 1491 |
TtyInit(fd); |
| 1492 |
} else |
| 1493 |
#endif /* SUPPORTS_TTY */ |
| 1494 |
{ |
| 1495 |
translation = NULL; |
| 1496 |
channelTypePtr = &fileChannelType; |
| 1497 |
} |
| 1498 |
|
| 1499 |
fsPtr = (FileState *)ckalloc(sizeof(FileState)); |
| 1500 |
fsPtr->validMask = channelPermissions | TCL_EXCEPTION; |
| 1501 |
fsPtr->fd = fd; |
| 1502 |
|
| 1503 |
fsPtr->channel = Tcl_CreateChannel(channelTypePtr, channelName, |
| 1504 |
fsPtr, channelPermissions); |
| 1505 |
|
| 1506 |
if (translation != NULL) { |
| 1507 |
/* |
| 1508 |
* Gotcha. Most modems need a "\r" at the end of the command sequence. |
| 1509 |
* If you just send "at\n", the modem will not respond with "OK" |
| 1510 |
* because it never got a "\r" to actually invoke the command. So, by |
| 1511 |
* default, newlines are translated to "\r\n" on output to avoid "bug" |
| 1512 |
* reports that the serial port isn't working. |
| 1513 |
*/ |
| 1514 |
|
| 1515 |
if (Tcl_SetChannelOption(interp, fsPtr->channel, |
| 1516 |
"-translation", translation) != TCL_OK) { |
| 1517 |
Tcl_Close(NULL, fsPtr->channel); |
| 1518 |
return NULL; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
|
| 1522 |
return fsPtr->channel; |
| 1523 |
} |
| 1524 |
|
| 1525 |
/* |
| 1526 |
*---------------------------------------------------------------------- |
| 1527 |
* |
| 1528 |
* Tcl_MakeFileChannel -- |
| 1529 |
* |
| 1530 |
* Makes a Tcl_Channel from an existing OS level file handle. |
| 1531 |
* |
| 1532 |
* Results: |
| 1533 |
* The Tcl_Channel created around the preexisting OS level file handle. |
| 1534 |
* |
| 1535 |
* Side effects: |
| 1536 |
* None. |
| 1537 |
* |
| 1538 |
*---------------------------------------------------------------------- |
| 1539 |
*/ |
| 1540 |
|
| 1541 |
Tcl_Channel |
| 1542 |
Tcl_MakeFileChannel( |
| 1543 |
ClientData handle, /* OS level handle. */ |
| 1544 |
int mode) /* OR'ed combination of TCL_READABLE and |
| 1545 |
* TCL_WRITABLE to indicate file mode. */ |
| 1546 |
{ |
| 1547 |
FileState *fsPtr; |
| 1548 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 1549 |
int fd = PTR2INT(handle); |
| 1550 |
const Tcl_ChannelType *channelTypePtr; |
| 1551 |
struct stat buf; |
| 1552 |
|
| 1553 |
if (mode == 0) { |
| 1554 |
return NULL; |
| 1555 |
} |
| 1556 |
|
| 1557 |
#ifdef SUPPORTS_TTY |
| 1558 |
if (isatty(fd)) { |
| 1559 |
channelTypePtr = &ttyChannelType; |
| 1560 |
snprintf(channelName, sizeof(channelName), "serial%d", fd); |
| 1561 |
goto final; |
| 1562 |
} else |
| 1563 |
#endif /* SUPPORTS_TTY */ |
| 1564 |
if (fstat(fd, &buf) == 0 && S_ISSOCK(buf.st_mode)) { |
| 1565 |
struct sockaddr sockaddr; |
| 1566 |
socklen_t sockaddrLen = sizeof(sockaddr); |
| 1567 |
|
| 1568 |
sockaddr.sa_family = AF_UNSPEC; |
| 1569 |
if ((getsockname(fd, (struct sockaddr *)&sockaddr, &sockaddrLen) == 0) |
| 1570 |
&& (sockaddrLen > 0) |
| 1571 |
&& (sockaddr.sa_family == AF_INET |
| 1572 |
|| sockaddr.sa_family == AF_INET6)) { |
| 1573 |
return (Tcl_Channel)TclpMakeTcpClientChannelMode(INT2PTR(fd), mode); |
| 1574 |
} |
| 1575 |
} |
| 1576 |
channelTypePtr = &fileChannelType; |
| 1577 |
snprintf(channelName, sizeof(channelName), "file%d", fd); |
| 1578 |
final: |
| 1579 |
fsPtr = (FileState *)ckalloc(sizeof(FileState)); |
| 1580 |
fsPtr->fd = fd; |
| 1581 |
fsPtr->validMask = mode | TCL_EXCEPTION; |
| 1582 |
fsPtr->channel = Tcl_CreateChannel(channelTypePtr, channelName, |
| 1583 |
fsPtr, mode); |
| 1584 |
|
| 1585 |
return fsPtr->channel; |
| 1586 |
} |
| 1587 |
|
| 1588 |
/* |
| 1589 |
*---------------------------------------------------------------------- |
| 1590 |
* |
| 1591 |
* TclpGetDefaultStdChannel -- |
| 1592 |
* |
| 1593 |
* Creates channels for standard input, standard output or standard error |
| 1594 |
* output if they do not already exist. |
| 1595 |
* |
| 1596 |
* Results: |
| 1597 |
* Returns the specified default standard channel, or NULL. |
| 1598 |
* |
| 1599 |
* Side effects: |
| 1600 |
* May cause the creation of a standard channel and the underlying file. |
| 1601 |
* |
| 1602 |
*---------------------------------------------------------------------- |
| 1603 |
*/ |
| 1604 |
|
| 1605 |
Tcl_Channel |
| 1606 |
TclpGetDefaultStdChannel( |
| 1607 |
int type) /* One of TCL_STDIN, TCL_STDOUT, TCL_STDERR. */ |
| 1608 |
{ |
| 1609 |
Tcl_Channel channel = NULL; |
| 1610 |
int fd = 0; /* Initializations needed to prevent */ |
| 1611 |
int mode = 0; /* compiler warning (used before set). */ |
| 1612 |
const char *bufMode = NULL; |
| 1613 |
|
| 1614 |
/* |
| 1615 |
* Some #def's to make the code a little clearer! |
| 1616 |
*/ |
| 1617 |
|
| 1618 |
#define ERROR_OFFSET ((Tcl_SeekOffset) -1) |
| 1619 |
|
| 1620 |
switch (type) { |
| 1621 |
case TCL_STDIN: |
| 1622 |
if ((TclOSseek(0, 0, SEEK_CUR) == ERROR_OFFSET) |
| 1623 |
&& (errno == EBADF)) { |
| 1624 |
return NULL; |
| 1625 |
} |
| 1626 |
fd = 0; |
| 1627 |
mode = TCL_READABLE; |
| 1628 |
bufMode = "line"; |
| 1629 |
break; |
| 1630 |
case TCL_STDOUT: |
| 1631 |
if ((TclOSseek(1, 0, SEEK_CUR) == ERROR_OFFSET) |
| 1632 |
&& (errno == EBADF)) { |
| 1633 |
return NULL; |
| 1634 |
} |
| 1635 |
fd = 1; |
| 1636 |
mode = TCL_WRITABLE; |
| 1637 |
bufMode = "line"; |
| 1638 |
break; |
| 1639 |
case TCL_STDERR: |
| 1640 |
if ((TclOSseek(2, 0, SEEK_CUR) == ERROR_OFFSET) |
| 1641 |
&& (errno == EBADF)) { |
| 1642 |
return NULL; |
| 1643 |
} |
| 1644 |
fd = 2; |
| 1645 |
mode = TCL_WRITABLE; |
| 1646 |
bufMode = "none"; |
| 1647 |
break; |
| 1648 |
default: |
| 1649 |
Tcl_Panic("TclGetDefaultStdChannel: Unexpected channel type"); |
| 1650 |
break; |
| 1651 |
} |
| 1652 |
|
| 1653 |
#undef ERROR_OFFSET |
| 1654 |
|
| 1655 |
channel = Tcl_MakeFileChannel(INT2PTR(fd), mode); |
| 1656 |
if (channel == NULL) { |
| 1657 |
return NULL; |
| 1658 |
} |
| 1659 |
|
| 1660 |
/* |
| 1661 |
* Set up the normal channel options for stdio handles. |
| 1662 |
*/ |
| 1663 |
|
| 1664 |
if (Tcl_GetChannelType(channel) == &fileChannelType) { |
| 1665 |
Tcl_SetChannelOption(NULL, channel, "-translation", "auto"); |
| 1666 |
} else { |
| 1667 |
Tcl_SetChannelOption(NULL, channel, "-translation", "auto crlf"); |
| 1668 |
} |
| 1669 |
Tcl_SetChannelOption(NULL, channel, "-buffering", bufMode); |
| 1670 |
return channel; |
| 1671 |
} |
| 1672 |
|
| 1673 |
/* |
| 1674 |
*---------------------------------------------------------------------- |
| 1675 |
* |
| 1676 |
* Tcl_GetOpenFile -- |
| 1677 |
* |
| 1678 |
* Given a name of a channel registered in the given interpreter, returns |
| 1679 |
* a FILE * for it. |
| 1680 |
* |
| 1681 |
* Results: |
| 1682 |
* A standard Tcl result. If the channel is registered in the given |
| 1683 |
* interpreter and it is managed by the "file" channel driver, and it is |
| 1684 |
* open for the requested mode, then the output parameter filePtr is set |
| 1685 |
* to a FILE * for the underlying file. On error, the filePtr is not set, |
| 1686 |
* TCL_ERROR is returned and an error message is left in the interp's |
| 1687 |
* result. |
| 1688 |
* |
| 1689 |
* Side effects: |
| 1690 |
* May invoke fdopen to create the FILE * for the requested file. |
| 1691 |
* |
| 1692 |
*---------------------------------------------------------------------- |
| 1693 |
*/ |
| 1694 |
|
| 1695 |
int |
| 1696 |
Tcl_GetOpenFile( |
| 1697 |
Tcl_Interp *interp, /* Interpreter in which to find file. */ |
| 1698 |
const char *chanID, /* String that identifies file. */ |
| 1699 |
int forWriting, /* 1 means the file is going to be used for |
| 1700 |
* writing, 0 means for reading. */ |
| 1701 |
int checkUsage, /* 1 means verify that the file was opened in |
| 1702 |
* a mode that allows the access specified by |
| 1703 |
* "forWriting". Ignored, we always check that |
| 1704 |
* the channel is open for the requested |
| 1705 |
* mode. */ |
| 1706 |
ClientData *filePtr) /* Store pointer to FILE structure here. */ |
| 1707 |
{ |
| 1708 |
Tcl_Channel chan; |
| 1709 |
int chanMode, fd; |
| 1710 |
const Tcl_ChannelType *chanTypePtr; |
| 1711 |
ClientData data; |
| 1712 |
FILE *f; |
| 1713 |
|
| 1714 |
chan = Tcl_GetChannel(interp, chanID, &chanMode); |
| 1715 |
if (chan == NULL) { |
| 1716 |
return TCL_ERROR; |
| 1717 |
} |
| 1718 |
if (forWriting && !(chanMode & TCL_WRITABLE)) { |
| 1719 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1720 |
"\"%s\" wasn't opened for writing", chanID)); |
| 1721 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "CHANNEL", "NOT_WRITABLE", |
| 1722 |
(char *)NULL); |
| 1723 |
return TCL_ERROR; |
| 1724 |
} else if (!forWriting && !(chanMode & TCL_READABLE)) { |
| 1725 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1726 |
"\"%s\" wasn't opened for reading", chanID)); |
| 1727 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "CHANNEL", "NOT_READABLE", |
| 1728 |
(char *)NULL); |
| 1729 |
return TCL_ERROR; |
| 1730 |
} |
| 1731 |
|
| 1732 |
/* |
| 1733 |
* We allow creating a FILE * out of file based, pipe based and socket |
| 1734 |
* based channels. We currently do not allow any other channel types, |
| 1735 |
* because it is likely that stdio will not know what to do with them. |
| 1736 |
*/ |
| 1737 |
|
| 1738 |
chanTypePtr = Tcl_GetChannelType(chan); |
| 1739 |
if ((chanTypePtr == &fileChannelType) |
| 1740 |
#ifdef SUPPORTS_TTY |
| 1741 |
|| (chanTypePtr == &ttyChannelType) |
| 1742 |
#endif /* SUPPORTS_TTY */ |
| 1743 |
|| (strcmp(chanTypePtr->typeName, "tcp") == 0) |
| 1744 |
|| (strcmp(chanTypePtr->typeName, "pipe") == 0)) { |
| 1745 |
if (Tcl_GetChannelHandle(chan, |
| 1746 |
(forWriting ? TCL_WRITABLE : TCL_READABLE), &data) == TCL_OK) { |
| 1747 |
fd = PTR2INT(data); |
| 1748 |
|
| 1749 |
/* |
| 1750 |
* The call to fdopen below is probably dangerous, since it will |
| 1751 |
* truncate an existing file if the file is being opened for |
| 1752 |
* writing.... |
| 1753 |
*/ |
| 1754 |
|
| 1755 |
f = fdopen(fd, (forWriting ? "w" : "r")); |
| 1756 |
if (f == NULL) { |
| 1757 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1758 |
"cannot get a FILE * for \"%s\"", chanID)); |
| 1759 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "CHANNEL", |
| 1760 |
"FILE_FAILURE", (char *)NULL); |
| 1761 |
return TCL_ERROR; |
| 1762 |
} |
| 1763 |
*filePtr = f; |
| 1764 |
return TCL_OK; |
| 1765 |
} |
| 1766 |
} |
| 1767 |
|
| 1768 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1769 |
"\"%s\" cannot be used to get a FILE *", chanID)); |
| 1770 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "CHANNEL", "NO_DESCRIPTOR", |
| 1771 |
(char *)NULL); |
| 1772 |
return TCL_ERROR; |
| 1773 |
} |
| 1774 |
|
| 1775 |
#ifndef HAVE_COREFOUNDATION /* Darwin/Mac OS X CoreFoundation notifier is |
| 1776 |
* in tclMacOSXNotify.c */ |
| 1777 |
/* |
| 1778 |
*---------------------------------------------------------------------- |
| 1779 |
* |
| 1780 |
* TclUnixWaitForFile -- |
| 1781 |
* |
| 1782 |
* This function waits synchronously for a file to become readable or |
| 1783 |
* writable, with an optional timeout. |
| 1784 |
* |
| 1785 |
* Results: |
| 1786 |
* The return value is an OR'ed combination of TCL_READABLE, |
| 1787 |
* TCL_WRITABLE, and TCL_EXCEPTION, indicating the conditions that are |
| 1788 |
* present on file at the time of the return. This function will not |
| 1789 |
* return until either "timeout" milliseconds have elapsed or at least |
| 1790 |
* one of the conditions given by mask has occurred for file (a return |
| 1791 |
* value of 0 means that a timeout occurred). No normal events will be |
| 1792 |
* serviced during the execution of this function. |
| 1793 |
* |
| 1794 |
* Side effects: |
| 1795 |
* Time passes. |
| 1796 |
* |
| 1797 |
*---------------------------------------------------------------------- |
| 1798 |
*/ |
| 1799 |
|
| 1800 |
int |
| 1801 |
TclUnixWaitForFile( |
| 1802 |
int fd, /* Handle for file on which to wait. */ |
| 1803 |
int mask, /* What to wait for: OR'ed combination of |
| 1804 |
* TCL_READABLE, TCL_WRITABLE, and |
| 1805 |
* TCL_EXCEPTION. */ |
| 1806 |
int timeout) /* Maximum amount of time to wait for one of |
| 1807 |
* the conditions in mask to occur, in |
| 1808 |
* milliseconds. A value of 0 means don't wait |
| 1809 |
* at all, and a value of -1 means wait |
| 1810 |
* forever. */ |
| 1811 |
{ |
| 1812 |
Tcl_Time abortTime = {0, 0}, now; /* silence gcc 4 warning */ |
| 1813 |
struct timeval blockTime, *timeoutPtr; |
| 1814 |
int numFound, result = 0; |
| 1815 |
fd_set readableMask; |
| 1816 |
fd_set writableMask; |
| 1817 |
fd_set exceptionMask; |
| 1818 |
|
| 1819 |
#ifndef _DARWIN_C_SOURCE |
| 1820 |
/* |
| 1821 |
* Sanity check fd. |
| 1822 |
*/ |
| 1823 |
|
| 1824 |
if (fd >= FD_SETSIZE) { |
| 1825 |
Tcl_Panic("TclUnixWaitForFile can't handle file id %d", fd); |
| 1826 |
/* must never get here, or select masks overrun will occur below */ |
| 1827 |
} |
| 1828 |
#endif |
| 1829 |
|
| 1830 |
/* |
| 1831 |
* If there is a non-zero finite timeout, compute the time when we give |
| 1832 |
* up. |
| 1833 |
*/ |
| 1834 |
|
| 1835 |
if (timeout > 0) { |
| 1836 |
Tcl_GetTime(&now); |
| 1837 |
abortTime.sec = now.sec + timeout/1000; |
| 1838 |
abortTime.usec = now.usec + (timeout%1000)*1000; |
| 1839 |
if (abortTime.usec >= 1000000) { |
| 1840 |
abortTime.usec -= 1000000; |
| 1841 |
abortTime.sec += 1; |
| 1842 |
} |
| 1843 |
timeoutPtr = &blockTime; |
| 1844 |
} else if (timeout == 0) { |
| 1845 |
timeoutPtr = &blockTime; |
| 1846 |
blockTime.tv_sec = 0; |
| 1847 |
blockTime.tv_usec = 0; |
| 1848 |
} else { |
| 1849 |
timeoutPtr = NULL; |
| 1850 |
} |
| 1851 |
|
| 1852 |
/* |
| 1853 |
* Initialize the select masks. |
| 1854 |
*/ |
| 1855 |
|
| 1856 |
FD_ZERO(&readableMask); |
| 1857 |
FD_ZERO(&writableMask); |
| 1858 |
FD_ZERO(&exceptionMask); |
| 1859 |
|
| 1860 |
/* |
| 1861 |
* Loop in a mini-event loop of our own, waiting for either the file to |
| 1862 |
* become ready or a timeout to occur. |
| 1863 |
*/ |
| 1864 |
|
| 1865 |
while (1) { |
| 1866 |
if (timeout > 0) { |
| 1867 |
blockTime.tv_sec = abortTime.sec - now.sec; |
| 1868 |
blockTime.tv_usec = abortTime.usec - now.usec; |
| 1869 |
if (blockTime.tv_usec < 0) { |
| 1870 |
blockTime.tv_sec -= 1; |
| 1871 |
blockTime.tv_usec += 1000000; |
| 1872 |
} |
| 1873 |
if (blockTime.tv_sec < 0) { |
| 1874 |
blockTime.tv_sec = 0; |
| 1875 |
blockTime.tv_usec = 0; |
| 1876 |
} |
| 1877 |
} |
| 1878 |
|
| 1879 |
/* |
| 1880 |
* Setup the select masks for the fd. |
| 1881 |
*/ |
| 1882 |
|
| 1883 |
if (mask & TCL_READABLE) { |
| 1884 |
FD_SET(fd, &readableMask); |
| 1885 |
} |
| 1886 |
if (mask & TCL_WRITABLE) { |
| 1887 |
FD_SET(fd, &writableMask); |
| 1888 |
} |
| 1889 |
if (mask & TCL_EXCEPTION) { |
| 1890 |
FD_SET(fd, &exceptionMask); |
| 1891 |
} |
| 1892 |
|
| 1893 |
/* |
| 1894 |
* Wait for the event or a timeout. |
| 1895 |
*/ |
| 1896 |
|
| 1897 |
numFound = select(fd + 1, &readableMask, &writableMask, |
| 1898 |
&exceptionMask, timeoutPtr); |
| 1899 |
if (numFound == 1) { |
| 1900 |
if (FD_ISSET(fd, &readableMask)) { |
| 1901 |
SET_BITS(result, TCL_READABLE); |
| 1902 |
} |
| 1903 |
if (FD_ISSET(fd, &writableMask)) { |
| 1904 |
SET_BITS(result, TCL_WRITABLE); |
| 1905 |
} |
| 1906 |
if (FD_ISSET(fd, &exceptionMask)) { |
| 1907 |
SET_BITS(result, TCL_EXCEPTION); |
| 1908 |
} |
| 1909 |
result &= mask; |
| 1910 |
if (result) { |
| 1911 |
break; |
| 1912 |
} |
| 1913 |
} |
| 1914 |
if (timeout == 0) { |
| 1915 |
break; |
| 1916 |
} |
| 1917 |
if (timeout < 0) { |
| 1918 |
continue; |
| 1919 |
} |
| 1920 |
|
| 1921 |
/* |
| 1922 |
* The select returned early, so we need to recompute the timeout. |
| 1923 |
*/ |
| 1924 |
|
| 1925 |
Tcl_GetTime(&now); |
| 1926 |
if ((abortTime.sec < now.sec) |
| 1927 |
|| (abortTime.sec==now.sec && abortTime.usec<=now.usec)) { |
| 1928 |
break; |
| 1929 |
} |
| 1930 |
} |
| 1931 |
return result; |
| 1932 |
} |
| 1933 |
#endif /* HAVE_COREFOUNDATION */ |
| 1934 |
|
| 1935 |
/* |
| 1936 |
*---------------------------------------------------------------------- |
| 1937 |
* |
| 1938 |
* FileTruncateProc -- |
| 1939 |
* |
| 1940 |
* Truncates a file to a given length. |
| 1941 |
* |
| 1942 |
* Results: |
| 1943 |
* 0 if the operation succeeded, and -1 if it failed (in which case |
| 1944 |
* *errorCodePtr will be set to errno). |
| 1945 |
* |
| 1946 |
* Side effects: |
| 1947 |
* The underlying file is potentially truncated. This can have a wide |
| 1948 |
* variety of side effects, including moving file pointers that point at |
| 1949 |
* places later in the file than the truncate point. |
| 1950 |
* |
| 1951 |
*---------------------------------------------------------------------- |
| 1952 |
*/ |
| 1953 |
|
| 1954 |
static int |
| 1955 |
FileTruncateProc( |
| 1956 |
ClientData instanceData, |
| 1957 |
Tcl_WideInt length) |
| 1958 |
{ |
| 1959 |
FileState *fsPtr = (FileState *)instanceData; |
| 1960 |
int result; |
| 1961 |
|
| 1962 |
#ifdef HAVE_TYPE_OFF64_T |
| 1963 |
/* |
| 1964 |
* We assume this goes with the type for now... |
| 1965 |
*/ |
| 1966 |
|
| 1967 |
result = ftruncate64(fsPtr->fd, (off64_t) length); |
| 1968 |
#else |
| 1969 |
result = ftruncate(fsPtr->fd, (off_t) length); |
| 1970 |
#endif |
| 1971 |
if (result) { |
| 1972 |
return errno; |
| 1973 |
} |
| 1974 |
return 0; |
| 1975 |
} |
| 1976 |
|
| 1977 |
/* |
| 1978 |
* Local Variables: |
| 1979 |
* mode: c |
| 1980 |
* c-basic-offset: 4 |
| 1981 |
* fill-column: 78 |
| 1982 |
* End: |
| 1983 |
*/ |