* Here we start a thread, that will monitor the netgraph * sockets and catch any unexpected messages or data on them, * that can arrive while user edits his/her commands. * * Whenever we expect data on netgraph sockets, we send signal * to monitoring thread. The signal forces it to exit select() * system call and sleep on condvar until we wake it. While * monitoring thread sleeps, we can do
| 329 | * sockets. |
| 330 | */ |
| 331 | static int |
| 332 | DoInteractive(void) |
| 333 | { |
| 334 | #ifndef FSTACK |
| 335 | pthread_t monitor; |
| 336 | #endif |
| 337 | EditLine *el; |
| 338 | History *hist; |
| 339 | HistEvent hev = { 0, "" }; |
| 340 | |
| 341 | (*help_cmd.func)(0, NULL); |
| 342 | #ifndef FSTACK |
| 343 | pthread_create(&monitor, NULL, Monitor, NULL); |
| 344 | #endif |
| 345 | el = el_init(getprogname(), stdin, stdout, stderr); |
| 346 | if (el == NULL) |
| 347 | return (CMDRTN_ERROR); |
| 348 | el_set(el, EL_PROMPT, Prompt); |
| 349 | el_set(el, EL_SIGNAL, 1); |
| 350 | el_set(el, EL_EDITOR, "emacs"); |
| 351 | hist = history_init(); |
| 352 | if (hist == NULL) |
| 353 | return (CMDRTN_ERROR); |
| 354 | history(hist, &hev, H_SETSIZE, 100); |
| 355 | history(hist, &hev, H_SETUNIQUE, 1); |
| 356 | el_set(el, EL_HIST, history, (const char *)hist); |
| 357 | el_source(el, NULL); |
| 358 | |
| 359 | for (;;) { |
| 360 | const char *buf; |
| 361 | int count; |
| 362 | |
| 363 | if ((buf = el_gets(el, &count)) == NULL) { |
| 364 | printf("\n"); |
| 365 | break; |
| 366 | } |
| 367 | history(hist, &hev, H_ENTER, buf); |
| 368 | #ifndef FSTACK |
| 369 | pthread_kill(monitor, SIGUSR1); |
| 370 | pthread_mutex_lock(&mutex); |
| 371 | #endif |
| 372 | if (DoParseCommand(buf) == CMDRTN_QUIT) { |
| 373 | #ifndef FSTACK |
| 374 | pthread_mutex_unlock(&mutex); |
| 375 | #endif |
| 376 | break; |
| 377 | } |
| 378 | #ifndef FSTACK |
| 379 | pthread_cond_signal(&cond); |
| 380 | pthread_mutex_unlock(&mutex); |
| 381 | #endif |
| 382 | } |
| 383 | |
| 384 | history_end(hist); |
| 385 | el_end(el); |
| 386 | #ifndef FSTACK |
| 387 | pthread_cancel(monitor); |
| 388 | #endif |
no test coverage detected