static void init_tty() { if (gtty(1, &tty_state) < 0) { fprintf(stderr, "gtty failed\n"); exit(-1); } memcpy(&tty_new, &tty_state, sizeof(tty_new)); //#ifdef LINUX tty_new.c_lflag &= ~(ICANON | ECHO | ISIG); tcsetattr(1, TCSANOW, &tty_new); restore_tty(); //#else // tty_new.sg_flags |= RAW; //#ifdef HP_UX // tty_new.sg_flags &= ~(O_HUPCL | O_XTABS | LCASE | ECHO | CRMOD); //#else // tty_new.sg_flags &= ~(TANDEM | CBREAK | LCASE | ECHO | CRMOD); //#endif stty(1, &tty_new); //#endif }
static void tty_time_out(void) { int exit_code; signal(SIGALRM, SIG_IGN); if(tty && need_tty_reset) restore_tty (&in_orig); #if future if (fail_on_timeout) exit_code=SHFAIL; else { if (default_choice && mode_defined) { if (yes_no) { if ('Y' == default_choice) exit_code=0; else exit_code=1; } else exit_code=default_choice-minc+1; } else exit_code=DONE; } #else exit_code = DONE; #endif exit(exit_code); }
do_more( FILE *fp ) { int key, c, lines_shown = 0; char line[BUFSIZ]; set_crmode_noecho(); while ( fgets( line, BUFSIZ, fp ) ){ if ( lines_shown == SCREENLENGTH ){ /* screenful */ printf("[ -- MORE -- ] "); fflush(stdout); /* force message*/ key = getakey( "q \n"); /* get input */ printf("\r \r"); /* clear message */ if ( key == 'q' ) break; /* quit */ if ( key == ' ' ) /* new page */ lines_shown = 0 ; else if ( key == '\n' ) lines_shown--; } fputs( line, stdout ); /* output line */ lines_shown++; /* count lines */ } restore_tty(); }
static void cleanup_tty(void) { if(tty && need_tty_reset) { restore_tty (&in_orig); setup_signal(); } }
int clip___RUN(ClipMachine * ClipMachineMemory) { char *com = _clip_parc(ClipMachineMemory, 1); int old_cursor = 0; if (com == NULL) { _clip_trap_printf(ClipMachineMemory, __FILE__, __LINE__, "invalid argument"); _clip_retc(ClipMachineMemory, ""); return 1; } if (ClipMachineMemory->fullscreen) { old_cursor = ClipMachineMemory->screen->cursor; restore_tty(ClipMachineMemory->screen_base); /* restore start mode */ } system(com); if (ClipMachineMemory->fullscreen) restart_tty(ClipMachineMemory->screen_base); /* set work mode */ if (ClipMachineMemory->fullscreen) { redraw_Screen(ClipMachineMemory->screen, ClipMachineMemory->flags1 & UTF8TERM_FLAG); ClipMachineMemory->screen->cursor = !ClipMachineMemory->screen->cursor; sync_Screen(ClipMachineMemory->screen, ClipMachineMemory->flags1 & UTF8TERM_FLAG); ClipMachineMemory->screen->cursor = old_cursor; sync_Screen(ClipMachineMemory->screen, ClipMachineMemory->flags1 & UTF8TERM_FLAG); } return 0; }
/* restore tty and exit */ void rexit( int code ) { if( has_setup_tty ) { restore_tty(); } exit( code ); }
/* PROTO */ void input_quit(char *arg) { struct ConnPtr *trav, *temp; restore_tty(); if (conn->otr) { printf("\n"); otr_cleanup(); } log_buddies_offline(); imcomm_delete_handle_now(conn->conn); if (conn->clist->conn == conn->conn) { temp = conn->clist; conn->clist = conn->clist->next; if (temp->username) free(temp->username); free(temp); } else { for (trav = conn->clist; trav->next;) { if (trav->next->conn == conn->conn) { temp = trav->next; trav->next = trav->next->next; if (temp->username) free(temp->username); free(temp); } } } delete_buddylist(buddylist); if (conn->username) free(conn->username); if (conn->password) free(conn->password); if (conn->awaymsg) free(conn->awaymsg); if (conn->lastsn) free(conn->lastsn); free(conn); printf("\n"); exit(0); }
void twdone(void) { if (!TROFF && t.twrest) { obufp = obuf; oputs(t.twrest); flusho(); if (pipeflg) { pclose(ptid); } restore_tty(); } }
static #ifdef USG void #endif cleanup() { restore_tty(); signal(SIGINT, old_ctrlc); #ifdef SIGTSTP signal(SIGTSTP, old_ctrlz); #endif error("\n\nYou may need to reset the 80960 and/or reload your program.\n"); }
/* * close_tty - restore the terminal device and close it. */ static void close_tty(void) { /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); close(real_ttyfd); real_ttyfd = -1; }
int main(int argc, char** argv) { int ret; t_arg arg; if (isatty(STDIN_FILENO) == 0) return fatal(2, strerror(errno), 1); ret = 0; if (get_arg(argc, argv, &arg)) return 1; if (arg.name_process != 0) { if (init_tty()) return 1; ret = inspect_proccess(&arg); restore_tty(); } return ret; }
/* * close_tty - restore the terminal device and close it. */ static void close_tty(void) { disestablish_ppp(ttyfd); /* drop dtr to hang up */ if (modem) { setdtr(ttyfd, FALSE); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(ttyfd); if (tty_mode != (mode_t) -1) fchmod(ttyfd, tty_mode); close(ttyfd); ttyfd = -1; }
int _clip_restore_tty(ClipMachine * ClipMachineMemory) { return restore_tty(ClipMachineMemory->screen->base); }
int main(int argc, char **argv){ int n, i; char c[4], bp[1024], area[2048], print=FALSE; struct termio line; if (argc < 2) { my_str("Usage: ./select <file(s)>\n"); return 1; } init_tty(); fill_elems(argc, argv); signal(SIGWINCH, show_params); term_vi(); term_init(bp, area); ioctl(0, TCGETA, &line); gl_env.line = line; line.c_lflag &= ~(ECHO | ISIG | ICANON); line.c_cc[VMIN] = 3; line.c_cc[VTIME] = VTI; ioctl(0, TCSETA, &line); show_params(); while(1){ n = read(0, c, 3); c[n] = '\0'; /*my_str("\n"); for(i=0;i<n;i++){ my_int(c[i]); my_char('\n'); } my_str("\n");*/ if (my_strcmp(c, gl_env.esc) == 0) { // esc break; } else if (my_strcmp(c, "\n") == 0) { // enter print = TRUE; break; } else if (my_strcmp(c, " ") == 0) { // space gl_env.elems[gl_env.current].mode = 1 - gl_env.elems[gl_env.current].mode; if (gl_env.elems[gl_env.current].mode) { gl_env.current = gl_env.current + 1; if (gl_env.current >= gl_env.nbelems) gl_env.current = 0; } show_params(); } else if (my_strcmp(c, KU) == 0) { // up gl_env.current -= 1; if (gl_env.current < 0) gl_env.current = gl_env.nbelems - 1; show_params(); } else if (my_strcmp(c, KD) == 0) { // down gl_env.current += 1; if (gl_env.current >= gl_env.nbelems) gl_env.current = 0; show_params(); } else if (my_strcmp(c, KL) == 0) { // left gl_env.current -= gl_env.win.ws_row; if (gl_env.current < 0) gl_env.current = 0; show_params(); } else if (my_strcmp(c, KR) == 0) { //right gl_env.current += gl_env.win.ws_row; if (gl_env.current >= gl_env.nbelems) gl_env.current = gl_env.nbelems-1; show_params(); } } term_clear(); ioctl(0, TCSETA, &(gl_env.line)); term_ve(); restore_tty(); if (print) { //term_clear(); //term_pos(0, 0); for (i=0;i<gl_env.nbelems;i++){ if (gl_env.elems[i].mode) { my_str(gl_env.elems[i].elem); my_char(' '); } } } return 0; }
int do_exec(char* com, char* wd) { char path[MAXPATHLEN]; char pcom[MAXCOMSZ]; char *arglist[MAXARGS]; char *tz; register int i, len; register int argptr; int status, pid, w; int pmode; void (*isig) (), (*qsig) (); strncpy(path, BINDIR, MAXPATHLEN); strncpy(pcom, com, MAXCOMSZ); len = Min(strlen(com) + 1, MAXCOMSZ); pmode = LOOKFIRST; for (i = 0, argptr = 0; i < len; i++) { if (pcom[i] == '\0') break; if (pmode == QUOTEMODE) { if (pcom[i] == '\001') { pmode = LOOKFIRST; pcom[i] = '\0'; continue; } continue; } if (pcom[i] == '\001') { pmode = QUOTEMODE; arglist[argptr++] = &pcom[i + 1]; if (argptr + 1 == MAXARGS) break; continue; } if (pmode == LOOKFIRST) if (pcom[i] != ' ') { arglist[argptr++] = &pcom[i]; if (argptr + 1 == MAXARGS) break; pmode = LOOKLAST; } else continue; if (pcom[i] == ' ') { pmode = LOOKFIRST; pcom[i] = '\0'; } } arglist[argptr] = NULL; if (argptr == 0) return -1; if (*arglist[0] == '/') strncpy(path, arglist[0], MAXPATHLEN); else strncat(path, arglist[0], MAXPATHLEN); reset_tty(); alarm(0); #ifdef IRIX if ((pid = fork()) == 0) { #else if ((pid = vfork()) == 0) { #endif #ifdef BBSD waitpid(pid, &status, 0); #endif if (wd) if (chdir(wd)) { sprintf(genbuf, "Unable to chdir to '%s'\n", wd); report(genbuf); exit(-1); } bbssetenv("PATH", "/bin:."); bbssetenv("TERM", currentuser.termtype); bbssetenv("USER", currentuser.userid); bbssetenv("USERNAME", currentuser.username); if ((tz = getenv("TZ")) != NULL) bbssetenv("TZ", tz); if (numbbsenvs == 0) bbsenv[0] = NULL; //dup2(0,1); //dup2(0,2); execve(path, arglist, bbsenv); sprintf(genbuf, "EXECV FAILED... path = '%s'\n", path); report(genbuf); exit(-1); } isig = signal(SIGINT, SIG_IGN); qsig = signal(SIGQUIT, SIG_IGN); #ifndef BBSD while ((w = wait(&status)) != pid && w != 1) /* NULL STATEMENT */ ; #endif signal(SIGINT, isig); signal(SIGQUIT, qsig); restore_tty(); #ifdef DOTIMEOUT alarm(IDLE_TIMEOUT); #endif return ((w == -1) ? w : status); } char* horoscope(int month, int day) { char *name[12] = { "ĦôÉ", "ˮƿ", "Ë«Óã", "ĵÑò", "½ðÅ£", "Ë«×Ó", "¾Þз", "ʨ×Ó", "´¦Å®", "Ìì³Ó", "ÌìЫ", "ÉäÊÖ" }; switch (month) { case 1: if (day < 21) return (name[0]); else return (name[1]); case 2: if (day < 19) return (name[1]); else return (name[2]); case 3: if (day < 21) return (name[2]); else return (name[3]); case 4: if (day < 21) return (name[3]); else return (name[4]); case 5: if (day < 21) return (name[4]); else return (name[5]); case 6: if (day < 22) return (name[5]); else return (name[6]); case 7: if (day < 23) return (name[6]); else return (name[7]); case 8: if (day < 23) return (name[7]); else return (name[8]); case 9: if (day < 23) return (name[8]); else return (name[9]); case 10: if (day < 24) return (name[9]); else return (name[10]); case 11: if (day < 23) return (name[10]); else return (name[11]); case 12: if (day < 22) return (name[11]); else return (name[0]); } return ("²»Ïê"); }
int main (int argc, char *argv[]) { int kfd, tfd; int nfd; fd_set fixed_mask, select_mask; int cr; char c; int i; int echo; int mapda; int ctsrts; if (argc < 2) { fprintf (stderr, "Usage %s <tty_spec> [ echo ] [ crlf ] [ noctsrts ]\n", argv[0]); init_tty("", 0, 0, 0); exit (1); } echo = 0; mapda = 0; ctsrts = 1; for (i = 2; i < argc; i++) { if (strcmp(argv[i], "echo") == 0) { echo = 1; } else if (strcmp(argv[i], "crlf") == 0) { mapda = 1; } else if (strcmp(argv[i], "noctsrts") == 0) { ctsrts = 0; } else { fprintf (stderr, "Usage %s <tty_spec> [ echo ] [ crlf ] [ noctsrts ]\n", argv[0]); init_tty("", 0, 0, 0); exit (1); } } init_tty(argv[1], ctsrts, echo, mapda); tfd = get_tty_fd(); printf ("Ready. Exit with Ctrl X.\n"); kfd = fileno(stdin); init_kbd(kfd); nfd = tfd; if (nfd < kfd) nfd = kfd; FD_ZERO (&fixed_mask); FD_SET (tfd, &fixed_mask); FD_SET (kfd, &fixed_mask); for (;;) { memcpy (&select_mask, &fixed_mask, sizeof(fd_set)); cr = select (nfd+1, &select_mask, (fd_set*)NULL, (fd_set*)NULL, (struct timeval*)NULL); if (cr == -1) { if (errno != EINTR) { perror ("select"); } } else if (cr == 0) { fprintf (stderr, "select returned 0\n"); } else { if (FD_ISSET(kfd, &select_mask) ) { #ifdef DEBUG fprintf (stderr, "kbd event selected\n"); #endif errno = 0; cr = read (kfd, &c, 1); if (cr != 1) { perror ("read kbd"); } #ifdef DEBUG fprintf (stderr, "kbd char read: %c 0x%x\n", c, (int)c); #endif if (c == 0x18) { /* Exit */ restore_kbd (kfd); restore_tty (); (void) putchar ((int)'\n'); exit (0); } send_tty (c); #ifdef DEBUG fprintf (stderr, "sent to tty.\n"); #endif } else if (FD_ISSET(tfd, &select_mask) ) { #ifdef DEBUG fprintf (stderr, "tty event selected\n"); #endif read_tty (&c); #ifdef DEBUG fprintf (stderr, "tty char read: %c %x\n", c, (int)c); #endif (void) putchar ((int)c); #ifdef DEBUG fprintf (stderr, "sent to display.\n"); #endif (void) fflush (stdout); } else { fprintf (stderr, "select returned but fd not set\n"); } } } }
void handle_sigint(int sig __attribute__((unused))) { restore_tty(); exit(EX_SIGINT); }
const struct choice * selected_choice(void) { char buf[6]; int key, selection = 0, visible_choices_count; int word_position; size_t cursor_position, length, query_length, scroll; cursor_position = query_length = strlen(query); filter_choices(); init_tty(); if (cursor_position >= (size_t)columns) scroll = cursor_position - columns + 1; else scroll = 0; visible_choices_count = print_choices(selection); print_query(query, query_length, cursor_position, scroll); tty_putp(cursor_normal); for (;;) { fflush(tty_out); memset(buf, 0, sizeof(buf)); key = get_key(buf, sizeof(buf), &length); switch (key) { case ENTER: if (visible_choices_count > 0) { restore_tty(); if (selection >= 0 && selection < (ssize_t)choices.length) return &choices.v[selection]; else return NULL; } break; case ALT_ENTER: restore_tty(); choices.v[choices.length].string = query; choices.v[choices.length].description = ""; return &choices.v[choices.length]; case DEL: if (cursor_position > 0) { for (length = 1; isu8cont(query[cursor_position - length]); length++); delete_between( query, query_length, cursor_position - length, cursor_position); cursor_position -= length; query_length -= length; filter_choices(); selection = 0; } break; case CTRL_D: if (cursor_position < query_length) { for (length = 1; isu8cont(query[cursor_position + length]); length++); delete_between( query, query_length, cursor_position, cursor_position + length); query_length -= length; filter_choices(); selection = 0; } break; case CTRL_U: delete_between( query, query_length, 0, cursor_position); query_length -= cursor_position; cursor_position = 0; filter_choices(); selection = 0; break; case CTRL_K: delete_between( query, query_length, cursor_position, query_length); query_length = cursor_position; filter_choices(); selection = 0; break; case CTRL_W: if (cursor_position == 0) break; for (word_position = cursor_position;;) { while (isu8cont(query[--word_position])); if (word_position < 1) break; if (query[word_position] != ' ' && query[word_position - 1] == ' ') break; } delete_between( query, query_length, word_position, cursor_position); query_length -= cursor_position - word_position; cursor_position = word_position; filter_choices(); selection = 0; break; case CTRL_A: cursor_position = 0; break; case CTRL_E: cursor_position = query_length; break; case DOWN: if (selection < visible_choices_count - 1) ++selection; break; case UP: if (selection > 0) --selection; break; case LEFT: while (cursor_position > 0 && isu8cont(query[--cursor_position])); break; case RIGHT: while (cursor_position < query_length && isu8cont(query[++cursor_position])); break; default: if (!isu8start(buf[0]) && !isprint(buf[0])) continue; if (query_size < query_length + length) { query_size = 2*query_length + length; if ((query = reallocarray(query, query_size, sizeof(char))) == NULL) err(1, NULL); } if (cursor_position < query_length) memmove(query + cursor_position + length, query + cursor_position, query_length - cursor_position); memcpy(query + cursor_position, buf, length); cursor_position += length; query_length += length; query[query_length] = '\0'; filter_choices(); selection = 0; break; } tty_putp(cursor_invisible); visible_choices_count = print_choices(selection); if (cursor_position >= scroll + columns) scroll = cursor_position - columns + 1; if (cursor_position < scroll) scroll = cursor_position; print_query(query, query_length, cursor_position, scroll); tty_putp(cursor_normal); } }