| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <string.h> |
|---|
| 15 | #include <stdio.h> |
|---|
| 16 | #include <errno.h> |
|---|
| 17 | |
|---|
| 18 | #include "NewtCore.h" |
|---|
| 19 | #include "NewtIO.h" |
|---|
| 20 | |
|---|
| 21 | #if defined(HAVE_TERMIOS_H) |
|---|
| 22 | #include <termios.h> |
|---|
| 23 | #include <unistd.h> |
|---|
| 24 | #include <sys/select.h> |
|---|
| 25 | #elif defined(__WIN32__) |
|---|
| 26 | #include <conio.h> |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | #if defined(HAVE_TERMIOS_H) |
|---|
| 32 | #define newt_getch() tcgetch(0) |
|---|
| 33 | #elif defined(__WIN32__) |
|---|
| 34 | #define newt_getch() getch() |
|---|
| 35 | #else |
|---|
| 36 | #define newt_getch() (0) |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | static int cbreak_and_noecho(int fd, int vmin, struct termios *tiosp); |
|---|
| 42 | static int tcgetch(int fd); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | void NIOSetFile(newtStream_t * stream, FILE * f) |
|---|
| 55 | { |
|---|
| 56 | stream->file = f; |
|---|
| 57 | |
|---|
| 58 | if (f == stdout) |
|---|
| 59 | stream->obj = NcGetGlobalVar(NSSYM0(_STDOUT_)); |
|---|
| 60 | else if (f == stderr) |
|---|
| 61 | stream->obj = NcGetGlobalVar(NSSYM0(_STDERR_)); |
|---|
| 62 | else |
|---|
| 63 | stream->obj = kNewtRefUnbind; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | int NIOFprintf(newtStream_t * stream, const char * format, ...) |
|---|
| 80 | { |
|---|
| 81 | va_list args; |
|---|
| 82 | int result; |
|---|
| 83 | |
|---|
| 84 | va_start(args, format); |
|---|
| 85 | result = NIOVfprintf(stream, format, args); |
|---|
| 86 | va_end(args); |
|---|
| 87 | |
|---|
| 88 | return result; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | int NIOVfprintf(newtStream_t * stream, const char * format, va_list ap) |
|---|
| 106 | { |
|---|
| 107 | int result = 0; |
|---|
| 108 | |
|---|
| 109 | if (NewtRefIsString(stream->obj)) |
|---|
| 110 | { |
|---|
| 111 | char wk[NEWT_SNPRINTF_BUFFSIZE]; |
|---|
| 112 | |
|---|
| 113 | result = vsnprintf(wk, sizeof(wk), format, ap); |
|---|
| 114 | |
|---|
| 115 | if (0 < result) |
|---|
| 116 | { |
|---|
| 117 | if (sizeof(wk) < result) |
|---|
| 118 | wk[sizeof(wk) - 1] = '\0'; |
|---|
| 119 | |
|---|
| 120 | NewtStrCat(stream->obj, wk); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | else |
|---|
| 124 | { |
|---|
| 125 | result = vfprintf(stream->file, format, ap); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | return result; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | int NIOFputc(int c, newtStream_t * stream) |
|---|
| 144 | { |
|---|
| 145 | int result = 0; |
|---|
| 146 | |
|---|
| 147 | if (NewtRefIsString(stream->obj)) |
|---|
| 148 | { |
|---|
| 149 | char wk[4]; |
|---|
| 150 | |
|---|
| 151 | sprintf(wk, "%c", c); |
|---|
| 152 | NewtStrCat(stream->obj, wk); |
|---|
| 153 | } |
|---|
| 154 | else |
|---|
| 155 | { |
|---|
| 156 | result = fputc(c, stream->file); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | return result; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | int NIOFputs(const char *str, newtStream_t * stream) |
|---|
| 175 | { |
|---|
| 176 | int result = 0; |
|---|
| 177 | |
|---|
| 178 | if (NewtRefIsString(stream->obj)) |
|---|
| 179 | { |
|---|
| 180 | NewtStrCat(stream->obj, (char *)str); |
|---|
| 181 | } |
|---|
| 182 | else |
|---|
| 183 | { |
|---|
| 184 | result = fputs(str, stream->file); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | return result; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | #if 0 |
|---|
| 192 | #pragma mark - |
|---|
| 193 | #endif |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | int NewtFprintf(FILE * f, const char * format, ...) |
|---|
| 205 | { |
|---|
| 206 | newtStream_t stream; |
|---|
| 207 | va_list args; |
|---|
| 208 | int result; |
|---|
| 209 | |
|---|
| 210 | NIOSetFile(&stream, f); |
|---|
| 211 | |
|---|
| 212 | va_start(args, format); |
|---|
| 213 | result = NIOVfprintf(&stream, format, args); |
|---|
| 214 | va_end(args); |
|---|
| 215 | |
|---|
| 216 | return result; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | int NewtFputc(int c, FILE * f) |
|---|
| 230 | { |
|---|
| 231 | newtStream_t stream; |
|---|
| 232 | |
|---|
| 233 | NIOSetFile(&stream, f); |
|---|
| 234 | return NIOFputc(c, &stream); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | int NewtFputs(const char *str, FILE * f) |
|---|
| 248 | { |
|---|
| 249 | newtStream_t stream; |
|---|
| 250 | |
|---|
| 251 | NIOSetFile(&stream, f); |
|---|
| 252 | return NIOFputs(str, &stream); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | #if 0 |
|---|
| 257 | #pragma mark - |
|---|
| 258 | #endif |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | int NewtDebugMsg(const char * title, const char * format, ...) |
|---|
| 270 | { |
|---|
| 271 | newtStream_t stream; |
|---|
| 272 | va_list args; |
|---|
| 273 | int result; |
|---|
| 274 | |
|---|
| 275 | NIOSetFile(&stream, stderr); |
|---|
| 276 | |
|---|
| 277 | if (title != NULL) |
|---|
| 278 | { |
|---|
| 279 | NIOFputs("[", &stream); |
|---|
| 280 | NIOFputs(title, &stream); |
|---|
| 281 | NIOFputs("] ", &stream); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | va_start(args, format); |
|---|
| 285 | result = NIOVfprintf(&stream, format, args); |
|---|
| 286 | va_end(args); |
|---|
| 287 | |
|---|
| 288 | return result; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | #if 0 |
|---|
| 293 | #pragma mark - |
|---|
| 294 | #endif |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | newtRef NewtFgets(FILE * stream) |
|---|
| 307 | { |
|---|
| 308 | newtRefVar result = kNewtRefNIL; |
|---|
| 309 | char buff[NEWT_FGETS_BUFFSIZE]; |
|---|
| 310 | char * str; |
|---|
| 311 | char c; |
|---|
| 312 | int maxsize; |
|---|
| 313 | int oldlen; |
|---|
| 314 | int len; |
|---|
| 315 | |
|---|
| 316 | maxsize = sizeof(buff) - 1; |
|---|
| 317 | |
|---|
| 318 | while (str = fgets(buff, sizeof(buff), stream)) |
|---|
| 319 | { |
|---|
| 320 | len = strlen(str); |
|---|
| 321 | |
|---|
| 322 | if (result == kNewtRefNIL) |
|---|
| 323 | { |
|---|
| 324 | result = NewtMakeString2(str, len, false); |
|---|
| 325 | |
|---|
| 326 | if (NewtRefIsNIL(result)) |
|---|
| 327 | { |
|---|
| 328 | return NewtThrow0(kNErrOutOfObjectMemory); |
|---|
| 329 | } |
|---|
| 330 | } |
|---|
| 331 | else |
|---|
| 332 | { |
|---|
| 333 | oldlen = NewtStringLength(result); |
|---|
| 334 | result = NewtStrCat2(result, str, len); |
|---|
| 335 | |
|---|
| 336 | if (NewtStringLength(result) < oldlen + len) |
|---|
| 337 | { |
|---|
| 338 | return NewtThrow0(kNErrOutOfObjectMemory); |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | if (len < maxsize) |
|---|
| 343 | break; |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | c = buff[maxsize - 1]; |
|---|
| 347 | |
|---|
| 348 | if (c == '\n') |
|---|
| 349 | break; |
|---|
| 350 | |
|---|
| 351 | if (c == '\r') |
|---|
| 352 | { |
|---|
| 353 | |
|---|
| 354 | c = fgetc(stream); |
|---|
| 355 | |
|---|
| 356 | if (c != '\n') |
|---|
| 357 | { |
|---|
| 358 | |
|---|
| 359 | ungetc(c, stream); |
|---|
| 360 | break; |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | return result; |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | newtRef NsGets(newtRefArg rcvr) |
|---|
| 379 | { |
|---|
| 380 | return NewtFgets(stdin); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | newtRef NewtFgetc(FILE * stream) |
|---|
| 394 | { |
|---|
| 395 | int c; |
|---|
| 396 | |
|---|
| 397 | c = fgetc(stream); |
|---|
| 398 | |
|---|
| 399 | if (c == EOF) |
|---|
| 400 | return kNewtRefNIL; |
|---|
| 401 | else |
|---|
| 402 | return NewtMakeCharacter(c); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | newtRef NsGetc(newtRefArg rcvr) |
|---|
| 416 | { |
|---|
| 417 | return NewtFgetc(stdin); |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | #ifdef HAVE_TERMIOS_H |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | int cbreak_and_noecho(int fd, int vmin, struct termios *tiosp) |
|---|
| 435 | { |
|---|
| 436 | struct termios tios; |
|---|
| 437 | int err; |
|---|
| 438 | |
|---|
| 439 | err = tcgetattr(fd, &tios); |
|---|
| 440 | if (err) return err; |
|---|
| 441 | |
|---|
| 442 | if (tiosp) *tiosp = tios; |
|---|
| 443 | |
|---|
| 444 | tios.c_lflag &= ~ (ICANON | ECHO); |
|---|
| 445 | tios.c_cc[VTIME] = 0; |
|---|
| 446 | tios.c_cc[VMIN] = vmin; |
|---|
| 447 | err = tcsetattr(fd, TCSANOW, &tios); |
|---|
| 448 | |
|---|
| 449 | return err; |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | int tcgetch(int fd) |
|---|
| 463 | { |
|---|
| 464 | struct termios tios; |
|---|
| 465 | char buf[1]; |
|---|
| 466 | int c = 0; |
|---|
| 467 | int err; |
|---|
| 468 | |
|---|
| 469 | err = cbreak_and_noecho(fd, 1, &tios); |
|---|
| 470 | if (err) return -1; |
|---|
| 471 | |
|---|
| 472 | if (0 < read(fd, buf, sizeof(buf))) |
|---|
| 473 | c = buf[0]; |
|---|
| 474 | |
|---|
| 475 | tcsetattr(fd, TCSANOW, &tios); |
|---|
| 476 | |
|---|
| 477 | return c; |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | #endif |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | newtRef NsGetch(newtRefArg rcvr) |
|---|
| 494 | { |
|---|
| 495 | int c; |
|---|
| 496 | |
|---|
| 497 | c = newt_getch(); |
|---|
| 498 | |
|---|
| 499 | if (c) |
|---|
| 500 | return NewtMakeCharacter(c); |
|---|
| 501 | else |
|---|
| 502 | return kNewtRefNIL; |
|---|
| 503 | } |
|---|