int EiC_load_history(char * fname, int prompt) { #define BufSz 512 int i; char buff[BufSz]; char *line; FILE *fp = fopen(fname,"r"); if(prompt) set_termio(); if(fp) { while(fgets(buff,BufSz-2,fp)) { for(i=0;buff[i] && buff[i] != '\n';++i) ; if(!buff[i]) buff[i++] = '\\'; buff[i] = 0; if(prompt) { printf("Re-enter [%s] (Y/N/E)?",buff); switch(special_getc()) { case 'y': case 'Y': user_puts(" Y\n"); break; case 'e': case 'E': user_puts(" E\n"); copy_line(buff); line = editLine("edit: "); if(*line) EiC_add_history(line); free(line); set_termio(); continue; default: user_puts(" N\n"); continue; } } EiC_add_history(buff); } fclose(fp); i = 1; } else i = 0; if(prompt) reset_termio(); printf("added %d lines\n",HistLineNo); return i; #undef BufSz }
unsigned char * EiC_readline(char *prompt) { /* start with a string of MAXBUF chars */ char * editLine(char *); if(line_len!=0) { free(cur_line); line_len=0; } cur_line=alloc((unsigned long)MAXBUF, "readline"); line_len=MAXBUF; /* set the termio so we can do our own input processing */ set_termio(); /* print the prompt */ user_puts(prompt); cur_line[0] = '\0'; cur_pos = 0; max_pos = 0; cur_entry = NULL; return editLine(prompt); }
int tty_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned int arg) { struct tty_struct * tty; struct tty_struct * other_tty; int pgrp; int dev; if (MAJOR(file->f_rdev) != 4) { printk("tty_ioctl: tty pseudo-major != 4\n"); return -EINVAL; } dev = MINOR(file->f_rdev); tty = TTY_TABLE(dev); if (!tty) return -EINVAL; if (IS_A_PTY(dev)) other_tty = tty_table[PTY_OTHER(dev)]; else other_tty = NULL; switch (cmd) { case TCGETS: return get_termios(tty,(struct termios *) arg); case TCSETSF: flush_input(tty); /* fallthrough */ case TCSETSW: wait_until_sent(tty); /* fallthrough */ case TCSETS: return set_termios(tty,(struct termios *) arg, dev); case TCGETA: return get_termio(tty,(struct termio *) arg); case TCSETAF: flush_input(tty); /* fallthrough */ case TCSETAW: wait_until_sent(tty); /* fallthrough */ case TCSETA: return set_termio(tty,(struct termio *) arg, dev); case TCXONC: switch (arg) { case TCOOFF: tty->stopped = 1; TTY_WRITE_FLUSH(tty); return 0; case TCOON: tty->stopped = 0; TTY_WRITE_FLUSH(tty); return 0; case TCIOFF: if (STOP_CHAR(tty)) put_tty_queue(STOP_CHAR(tty), &tty->write_q); return 0; case TCION: if (START_CHAR(tty)) put_tty_queue(START_CHAR(tty), &tty->write_q); return 0; } return -EINVAL; /* not implemented */ case TCFLSH: if (arg==0) flush_input(tty); else if (arg==1) flush_output(tty); else if (arg==2) { flush_input(tty); flush_output(tty); } else return -EINVAL; return 0; case TIOCEXCL: return -EINVAL; /* not implemented */ case TIOCNXCL: return -EINVAL; /* not implemented */ case TIOCSCTTY: return -EINVAL; /* set controlling term NI */ case TIOCGPGRP: verify_area((void *) arg,4); put_fs_long(tty->pgrp,(unsigned long *) arg); return 0; case TIOCSPGRP: if ((current->tty < 0) || (current->tty != dev) || (tty->session != current->session)) return -ENOTTY; pgrp=get_fs_long((unsigned long *) arg); if (pgrp < 0) return -EINVAL; if (session_of_pgrp(pgrp) != current->session) return -EPERM; tty->pgrp = pgrp; return 0; case TIOCOUTQ: verify_area((void *) arg,4); put_fs_long(CHARS(&tty->write_q), (unsigned long *) arg); return 0; case TIOCINQ: verify_area((void *) arg,4); if (L_CANON(tty) && !tty->secondary.data) put_fs_long(0, (unsigned long *) arg); else put_fs_long(CHARS(&tty->secondary), (unsigned long *) arg); return 0; case TIOCSTI: return -EINVAL; /* not implemented */ case TIOCGWINSZ: return get_window_size(tty,(struct winsize *) arg); case TIOCSWINSZ: if (IS_A_PTY_MASTER(dev)) set_window_size(other_tty,(struct winsize *) arg); return set_window_size(tty,(struct winsize *) arg); case TIOCGSOFTCAR: return -EINVAL; /* not implemented */ case TIOCSSOFTCAR: return -EINVAL; /* not implemented */ case TIOCLINUX: switch (get_fs_byte((char *)arg)) { case 0: return do_screendump(arg); case 1: return do_get_ps_info(arg); default: return -EINVAL; } case TIOCCONS: if (!IS_A_PTY(dev)) return -EINVAL; if (redirect) return -EBUSY; if (!suser()) return -EPERM; if (IS_A_PTY_MASTER(dev)) redirect = other_tty; else redirect = tty; return 0; case FIONBIO: if (arg) file->f_flags |= O_NONBLOCK; else file->f_flags &= ~O_NONBLOCK; return 0; case TIOCNOTTY: if (MINOR(file->f_rdev) != current->tty) return -EINVAL; current->tty = -1; if (current->leader) { if (tty->pgrp > 0) kill_pg(tty->pgrp, SIGHUP, 0); tty->pgrp = -1; tty->session = 0; } return 0; case TIOCPKT: { int on; if (!IS_A_PTY_MASTER(dev)) return (-EINVAL); verify_area ((unsigned long *)arg, sizeof (int)); on=get_fs_long ((unsigned long *)arg); if (on ) tty->packet = 1; else tty->packet = 0; return (0); } default: if (tty->ioctl) return (tty->ioctl)(tty, file, cmd, arg); else return -EINVAL; } }
int tty_ioctl(int dev, int cmd, int arg) { struct tty_struct * tty; if (MAJOR(dev) == 5) { dev=current->tty; if (dev<0) panic("tty_ioctl: dev<0"); } else dev=MINOR(dev); tty = dev + tty_table; switch (cmd) { case TCGETS: return get_termios(tty,(struct termios *) arg); case TCSETSF: flush(&tty->read_q); /* fallthrough */ case TCSETSW: wait_until_sent(tty); /* fallthrough */ case TCSETS: return set_termios(tty,(struct termios *) arg); case TCGETA: return get_termio(tty,(struct termio *) arg); case TCSETAF: flush(&tty->read_q); /* fallthrough */ case TCSETAW: wait_until_sent(tty); /* fallthrough */ case TCSETA: return set_termio(tty,(struct termio *) arg); case TCSBRK: if (!arg) { wait_until_sent(tty); send_break(tty); } return 0; case TCXONC: return -EINVAL; /* not implemented */ case TCFLSH: if (arg==0) flush(&tty->read_q); else if (arg==1) flush(&tty->write_q); else if (arg==2) { flush(&tty->read_q); flush(&tty->write_q); } else return -EINVAL; return 0; case TIOCEXCL: return -EINVAL; /* not implemented */ case TIOCNXCL: return -EINVAL; /* not implemented */ case TIOCSCTTY: return -EINVAL; /* set controlling term NI */ case TIOCGPGRP: verify_area((void *) arg,4); put_fs_long(tty->pgrp,(unsigned long *) arg); return 0; case TIOCSPGRP: tty->pgrp=get_fs_long((unsigned long *) arg); return 0; case TIOCOUTQ: verify_area((void *) arg,4); put_fs_long(CHARS(tty->write_q),(unsigned long *) arg); return 0; case TIOCSTI: return -EINVAL; /* not implemented */ case TIOCGWINSZ: return -EINVAL; /* not implemented */ case TIOCSWINSZ: return -EINVAL; /* not implemented */ case TIOCMGET: return -EINVAL; /* not implemented */ case TIOCMBIS: return -EINVAL; /* not implemented */ case TIOCMBIC: return -EINVAL; /* not implemented */ case TIOCMSET: return -EINVAL; /* not implemented */ case TIOCGSOFTCAR: return -EINVAL; /* not implemented */ case TIOCSSOFTCAR: return -EINVAL; /* not implemented */ default: return -EINVAL; } }
int tty_ioctl(struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg) { struct tty_struct * tty; struct tty_struct * other_tty; struct tty_struct * termios_tty; pid_t pgrp; int dev; int termios_dev; int retval; if (MAJOR(file->f_rdev) != TTY_MAJOR) { printk("tty_ioctl: tty pseudo-major != TTY_MAJOR\n"); return -EINVAL; } dev = MINOR(file->f_rdev); tty = TTY_TABLE(dev); if (!tty) return -EINVAL; if (IS_A_PTY(dev)) other_tty = tty_table[PTY_OTHER(dev)]; else other_tty = NULL; if (IS_A_PTY_MASTER(dev)) { termios_tty = other_tty; termios_dev = PTY_OTHER(dev); } else { termios_tty = tty; termios_dev = dev; } switch (cmd) { case TCGETS: retval = verify_area(VERIFY_WRITE, (void *) arg, sizeof (struct termios)); if (retval) return retval; memcpy_tofs((struct termios *) arg, termios_tty->termios, sizeof (struct termios)); return 0; case TCSETSF: case TCSETSW: case TCSETS: retval = check_change(termios_tty, termios_dev); if (retval) return retval; if (cmd == TCSETSF || cmd == TCSETSW) { if (cmd == TCSETSF) flush_input(termios_tty); wait_until_sent(termios_tty, 0); } return set_termios(termios_tty, (struct termios *) arg, termios_dev); case TCGETA: return get_termio(termios_tty,(struct termio *) arg); case TCSETAF: case TCSETAW: case TCSETA: retval = check_change(termios_tty, termios_dev); if (retval) return retval; if (cmd == TCSETAF || cmd == TCSETAW) { if (cmd == TCSETAF) flush_input(termios_tty); wait_until_sent(termios_tty, 0); } return set_termio(termios_tty, (struct termio *) arg, termios_dev); case TCXONC: retval = check_change(tty, dev); if (retval) return retval; switch (arg) { case TCOOFF: stop_tty(tty); break; case TCOON: start_tty(tty); break; case TCIOFF: if (STOP_CHAR(tty) != __DISABLED_CHAR) put_tty_queue(STOP_CHAR(tty), &tty->write_q); break; case TCION: if (START_CHAR(tty) != __DISABLED_CHAR) put_tty_queue(START_CHAR(tty), &tty->write_q); break; default: return -EINVAL; } return 0; case TCFLSH: retval = check_change(tty, dev); if (retval) return retval; switch (arg) { case TCIFLUSH: flush_input(tty); break; case TCIOFLUSH: flush_input(tty); /* fall through */ case TCOFLUSH: flush_output(tty); break; default: return -EINVAL; } return 0; case TIOCEXCL: set_bit(TTY_EXCLUSIVE, &tty->flags); return 0; case TIOCNXCL: clear_bit(TTY_EXCLUSIVE, &tty->flags); return 0; case TIOCSCTTY: if (current->leader && (current->session == tty->session)) return 0; /* * The process must be a session leader and * not have a controlling tty already. */ if (!current->leader || (current->tty >= 0)) return -EPERM; if (tty->session > 0) { /* * This tty is already the controlling * tty for another session group! */ if ((arg == 1) && suser()) { /* * Steal it away */ struct task_struct *p; for_each_task(p) if (p->tty == dev) p->tty = -1; } else return -EPERM; } current->tty = dev; tty->session = current->session; tty->pgrp = current->pgrp; return 0; case TIOCGPGRP: retval = verify_area(VERIFY_WRITE, (void *) arg, sizeof (pid_t)); if (retval) return retval; if (current->tty != termios_dev) return -ENOTTY; put_fs_long(termios_tty->pgrp, (pid_t *) arg); return 0; case TIOCSPGRP: retval = check_change(termios_tty, termios_dev); if (retval) return retval; if ((current->tty < 0) || (current->tty != termios_dev) || (termios_tty->session != current->session)) return -ENOTTY; pgrp = get_fs_long((pid_t *) arg); if (pgrp < 0) return -EINVAL; if (session_of_pgrp(pgrp) != current->session) return -EPERM; termios_tty->pgrp = pgrp; return 0; case TIOCOUTQ: retval = verify_area(VERIFY_WRITE, (void *) arg, sizeof (unsigned long)); if (retval) return retval; put_fs_long(CHARS(&tty->write_q), (unsigned long *) arg); return 0; case TIOCINQ: retval = verify_area(VERIFY_WRITE, (void *) arg, sizeof (unsigned long)); if (retval) return retval; if (L_ICANON(tty)) put_fs_long(inq_canon(tty), (unsigned long *) arg); else put_fs_long(CHARS(&tty->secondary), (unsigned long *) arg); return 0; case TIOCSTI: if ((current->tty != dev) && !suser()) return -EPERM; retval = verify_area(VERIFY_READ, (void *) arg, 1); if (retval) return retval; put_tty_queue(get_fs_byte((char *) arg), &tty->read_q); TTY_READ_FLUSH(tty); return 0; case TIOCGWINSZ: retval = verify_area(VERIFY_WRITE, (void *) arg, sizeof (struct winsize)); if (retval) return retval; memcpy_tofs((struct winsize *) arg, &tty->winsize, sizeof (struct winsize)); return 0; case TIOCSWINSZ: if (IS_A_PTY_MASTER(dev)) set_window_size(other_tty,(struct winsize *) arg); return set_window_size(tty,(struct winsize *) arg); case TIOCLINUX: switch (get_fs_byte((char *)arg)) { case 0: return do_screendump(arg); case 1: return do_get_ps_info(arg); #ifdef CONFIG_SELECTION case 2: return set_selection(arg); case 3: return paste_selection(tty); case 4: unblank_screen(); return 0; #endif /* CONFIG_SELECTION */ default: return -EINVAL; } case TIOCCONS: if (IS_A_CONSOLE(dev)) { if (!suser()) return -EPERM; redirect = NULL; return 0; } if (redirect) return -EBUSY; if (!suser()) return -EPERM; if (IS_A_PTY_MASTER(dev)) redirect = other_tty; else if (IS_A_PTY_SLAVE(dev)) redirect = tty; else return -ENOTTY; return 0; case FIONBIO: arg = get_fs_long((unsigned long *) arg); if (arg) file->f_flags |= O_NONBLOCK; else file->f_flags &= ~O_NONBLOCK; return 0; case TIOCNOTTY: if (current->tty != dev) return -ENOTTY; if (current->leader) disassociate_ctty(0); current->tty = -1; return 0; case TIOCGETD: retval = verify_area(VERIFY_WRITE, (void *) arg, sizeof (unsigned long)); if (retval) return retval; put_fs_long(tty->disc, (unsigned long *) arg); return 0; case TIOCSETD: retval = check_change(tty, dev); if (retval) return retval; arg = get_fs_long((unsigned long *) arg); return tty_set_ldisc(tty, arg); case TIOCGLCKTRMIOS: arg = get_fs_long((unsigned long *) arg); retval = verify_area(VERIFY_WRITE, (void *) arg, sizeof (struct termios)); if (retval) return retval; memcpy_tofs((struct termios *) arg, &termios_locked[termios_dev], sizeof (struct termios)); return 0; case TIOCSLCKTRMIOS: if (!suser()) return -EPERM; arg = get_fs_long((unsigned long *) arg); memcpy_fromfs(&termios_locked[termios_dev], (struct termios *) arg, sizeof (struct termios)); return 0; case TIOCPKT: if (!IS_A_PTY_MASTER(dev)) return -ENOTTY; retval = verify_area(VERIFY_READ, (void *) arg, sizeof (unsigned long)); if (retval) return retval; if (get_fs_long(arg)) { if (!tty->packet) { tty->packet = 1; tty->link->ctrl_status = 0; } } else tty->packet = 0; return 0; case TCSBRK: case TCSBRKP: retval = check_change(tty, dev); if (retval) return retval; wait_until_sent(tty, 0); if (!tty->ioctl) return 0; tty->ioctl(tty, file, cmd, arg); return 0; default: if (tty->ioctl) { retval = (tty->ioctl)(tty, file, cmd, arg); if (retval != -EINVAL) return retval; } if (ldiscs[tty->disc].ioctl) { retval = (ldiscs[tty->disc].ioctl) (tty, file, cmd, arg); return retval; } return -EINVAL; }
char * readline(const char *prompt) { int cur_char; char *new_line; /* start with a string of MAXBUF chars */ if (line_len != 0) { free(cur_line); line_len = 0; } cur_line = gp_alloc(MAXBUF, "readline"); line_len = MAXBUF; /* set the termio so we can do our own input processing */ set_termio(); /* print the prompt */ fputs(prompt, stderr); cur_line[0] = '\0'; cur_pos = 0; max_pos = 0; cur_entry = NULL; /* get characters */ for (;;) { cur_char = special_getc(); /* Accumulate ascii (7bit) printable characters * and all leading 8bit characters. */ if ((isprint(cur_char) || (((cur_char & 0x80) != 0) && (cur_char != EOF))) && (cur_char != 0x09) /* TAB is a printable character in some locales */ ) { size_t i; if (max_pos + 1 >= line_len) { extend_cur_line(); } for (i = max_pos; i > cur_pos; i--) { cur_line[i] = cur_line[i - 1]; } user_putc(cur_char); cur_line[cur_pos] = cur_char; cur_pos += 1; max_pos += 1; cur_line[max_pos] = '\0'; if (cur_pos < max_pos) { switch (encoding) { case S_ENC_UTF8: if ((cur_char & 0xc0) == 0) fix_line(); /* Normal ascii character */ else if ((cur_char & 0xc0) == 0xc0) ; /* start of a multibyte sequence. */ else if (((cur_char & 0xc0) == 0x80) && ((unsigned char)(cur_line[cur_pos-2]) >= 0xe0)) ; /* second byte of a >2 byte sequence */ else { /* Last char of multi-byte sequence */ fix_line(); } break; default: fix_line(); break; } } /* else interpret unix terminal driver characters */ #ifdef VERASE } else if (cur_char == term_chars[VERASE]) { /* ^H */ delete_backward(); #endif /* VERASE */ #ifdef VEOF } else if (cur_char == term_chars[VEOF]) { /* ^D? */ if (max_pos == 0) { reset_termio(); return ((char *) NULL); } delete_forward(); #endif /* VEOF */ #ifdef VKILL } else if (cur_char == term_chars[VKILL]) { /* ^U? */ clear_line(prompt); #endif /* VKILL */ #ifdef VWERASE } else if (cur_char == term_chars[VWERASE]) { /* ^W? */ delete_previous_word(); #endif /* VWERASE */ #ifdef VREPRINT } else if (cur_char == term_chars[VREPRINT]) { /* ^R? */ putc(NEWLINE, stderr); /* go to a fresh line */ redraw_line(prompt); #endif /* VREPRINT */ #ifdef VSUSP } else if (cur_char == term_chars[VSUSP]) { reset_termio(); kill(0, SIGTSTP); /* process stops here */ set_termio(); /* print the prompt */ redraw_line(prompt); #endif /* VSUSP */ } else { /* do normal editing commands */ /* some of these are also done above */ switch (cur_char) { case EOF: reset_termio(); return ((char *) NULL); case 001: /* ^A */ while (cur_pos > 0) backspace(); break; case 002: /* ^B */ if (cur_pos > 0) backspace(); break; case 005: /* ^E */ while (cur_pos < max_pos) { user_putc(cur_line[cur_pos]); cur_pos += 1; } break; case 006: /* ^F */ if (cur_pos < max_pos) { step_forward(); } break; #if defined(HAVE_DIRENT_H) || defined(WIN32) case 011: /* ^I / TAB */ tab_completion(TRUE); /* next tab completion */ break; case 034: /* remapped by wtext.c or ansi_getc from Shift-Tab */ tab_completion(FALSE); /* previous tab completion */ break; #endif case 013: /* ^K */ clear_eoline(prompt); max_pos = cur_pos; break; case 020: /* ^P */ if (history != NULL) { if (cur_entry == NULL) { cur_entry = history; clear_line(prompt); copy_line(cur_entry->line); } else if (cur_entry->prev != NULL) { cur_entry = cur_entry->prev; clear_line(prompt); copy_line(cur_entry->line); } } break; case 016: /* ^N */ if (cur_entry != NULL) { cur_entry = cur_entry->next; clear_line(prompt); if (cur_entry != NULL) copy_line(cur_entry->line); else cur_pos = max_pos = 0; } break; case 014: /* ^L */ case 022: /* ^R */ putc(NEWLINE, stderr); /* go to a fresh line */ redraw_line(prompt); break; #ifndef DEL_ERASES_CURRENT_CHAR case 0177: /* DEL */ case 023: /* Re-mapped from CSI~3 in ansi_getc() */ #endif case 010: /* ^H */ delete_backward(); break; case 004: /* ^D */ if (max_pos == 0) { reset_termio(); return ((char *) NULL); } /* intentionally omitting break */ #ifdef DEL_ERASES_CURRENT_CHAR case 0177: /* DEL */ case 023: /* Re-mapped from CSI~3 in ansi_getc() */ #endif delete_forward(); break; case 025: /* ^U */ clear_line(prompt); break; case 027: /* ^W */ delete_previous_word(); break; case '\n': /* ^J */ case '\r': /* ^M */ cur_line[max_pos + 1] = '\0'; #ifdef OS2 while (cur_pos < max_pos) { user_putc(cur_line[cur_pos]); cur_pos += 1; } #endif putc(NEWLINE, stderr); /* Shrink the block down to fit the string ? * if the alloc fails, we still own block at cur_line, * but this shouldn't really fail. */ new_line = (char *) gp_realloc(cur_line, strlen(cur_line) + 1, "line resize"); if (new_line) cur_line = new_line; /* else we just hang on to what we had - it's not a problem */ line_len = 0; FPRINTF((stderr, "Resizing input line to %d chars\n", strlen(cur_line))); reset_termio(); return (cur_line); default: break; } } } }
char * editLine(char *prompt) { /* The line to be edited is stored in cur_line.*/ /* get characters */ int cur_char; for(;;) { cur_char = special_getc(); if(isprint(cur_char) || (((unsigned char)cur_char > 0x7f) && cur_char != EOF) || cur_char == '\t') { int i,inc = 1; if(cur_char == '\t') { inc = TABSTOPS; cur_char = ' '; } if(max_pos+inc>=line_len) extend_cur_line(); for(i=max_pos+inc-1; i-inc>=cur_pos; i--) { cur_line[i] = cur_line[i-inc]; } max_pos += inc; while(inc--) { user_putc(cur_char); cur_line[cur_pos++] = cur_char; } if (cur_pos < max_pos) fix_line(); cur_line[max_pos] = '\0'; #if MATCHPAREN switch(cur_char) { case ')':backupTo('(',')');break; case ']':backupTo('[',']');break; } #endif #if defined(VERASE) } else if(cur_char == term_chars[VERASE] ){ /* DEL? */ if(cur_pos > 0) { int i; cur_pos -= 1; backspace(); for(i=cur_pos; i<max_pos; i++) cur_line[i] = cur_line[i+1]; max_pos -= 1; fix_line(); } } else if(cur_char == term_chars[VEOF] ){ /* ^D? */ if(max_pos == 0) { copy_line("to exit EiC, enter :exit\n"); user_putc(BELL); reset_termio(); return((char*)NULL); } if((cur_pos < max_pos)&&(cur_char == 004)) { /* ^D */ int i; for(i=cur_pos; i<max_pos; i++) cur_line[i] = cur_line[i+1]; max_pos -= 1; fix_line(); } } else if(cur_char == term_chars[VKILL] ){ /* ^U? */ clear_line(prompt); } else if(cur_char == term_chars[VWERASE] ){ /* ^W? */ while((cur_pos > 0) && (cur_line[cur_pos-1] == SPACE)) { cur_pos -= 1; backspace(); } while((cur_pos > 0) && (cur_line[cur_pos-1] != SPACE)) { cur_pos -= 1; backspace(); } clear_eoline(); max_pos = cur_pos; } else if(cur_char == term_chars[VREPRINT] ){ /* ^R? */ user_putc(NEWLINE); /* go to a fresh line */ redraw_line(prompt); } else if(cur_char == term_chars[VSUSP]) { reset_termio(); kill(0, SIGTSTP); /* process stops here */ set_termio(); /* print the prompt */ redraw_line(prompt); #endif } else { /* do normal editing commands */ /* some of these are also done above */ int i; switch(cur_char) { case EOF: reset_termio(); return((char *)NULL); case 001: /* ^A */ while(cur_pos > 0) { cur_pos -= 1; backspace(); } break; case 002: /* ^B */ if(cur_pos > 0) { cur_pos -= 1; backspace(); } break; case 005: /* ^E */ while(cur_pos < max_pos) { user_putc(cur_line[cur_pos]); cur_pos += 1; } break; case 006: /* ^F */ if(cur_pos < max_pos) { user_putc(cur_line[cur_pos]); cur_pos += 1; } break; case 013: /* ^K */ clear_eoline(); max_pos = cur_pos; break; case 020: /* ^P */ if(history != NULL) { if(cur_entry == NULL) { cur_entry = history; clear_line(prompt); copy_line(cur_entry->line); } else if(cur_entry->prev != NULL) { cur_entry = cur_entry->prev; clear_line(prompt); copy_line(cur_entry->line); }else user_putc(BELL); }else user_putc(BELL); break; case 016: /* ^N */ if(cur_entry != NULL) { cur_entry = cur_entry->next; clear_line(prompt); if(cur_entry != NULL) copy_line(cur_entry->line); else cur_pos = max_pos = 0; }else user_putc(BELL); break; case 014: /* ^L */ case 022: /* ^R */ user_putc(NEWLINE); /* go to a fresh line */ redraw_line(prompt); break; case 0177: /* DEL */ case 010: /* ^H */ if(cur_pos > 0) { cur_pos -= 1; backspace(); for(i=cur_pos; i<max_pos; i++) cur_line[i] = cur_line[i+1]; max_pos -= 1; fix_line(); } break; case 004: /* ^D */ if(max_pos == 0) { reset_termio(); return((char *)NULL); } if(cur_pos < max_pos) { for(i=cur_pos; i<max_pos; i++) cur_line[i] = cur_line[i+1]; max_pos -= 1; fix_line(); } break; case 025: /* ^U */ clear_line(prompt); break; case 027: /* ^W */ while((cur_pos > 0) && (cur_line[cur_pos-1] == SPACE)) { cur_pos -= 1; backspace(); } while((cur_pos > 0) && (cur_line[cur_pos-1] != SPACE)) { cur_pos -= 1; backspace(); } clear_eoline(); max_pos = cur_pos; break; case '\n': /* ^J */ case '\r': /* ^M */ user_putc(NEWLINE); cur_line[max_pos+1] = '\0'; cur_line = (char *)ralloc(cur_line, (unsigned long)(strlen(cur_line)+2), "line resize"); line_len=0; reset_termio(); return cur_line; default: break; } } } }
int tty_ioctl(int dev, int cmd, int arg) { struct tty_struct * tty; struct tty_struct * other_tty; int pgrp; if (MAJOR(dev) == 5) { dev = current->tty; if (dev<0) return -EINVAL; } else dev=MINOR(dev); tty = tty_table + (dev ? ((dev < 64)? dev-1:dev) : fg_console); if (IS_A_PTY(dev)) other_tty = tty_table + PTY_OTHER(dev); else other_tty = NULL; if (!(tty->write_q && tty->read_q && tty->secondary && tty->write)) return -EINVAL; switch (cmd) { case TCGETS: return get_termios(tty,(struct termios *) arg); case TCSETSF: flush(tty->read_q); flush(tty->secondary); if (other_tty) flush(other_tty->write_q); /* fallthrough */ case TCSETSW: wait_until_sent(tty); /* fallthrough */ case TCSETS: return set_termios(tty,(struct termios *) arg, dev); case TCGETA: return get_termio(tty,(struct termio *) arg); case TCSETAF: flush(tty->read_q); flush(tty->secondary); if (other_tty) flush(other_tty->write_q); /* fallthrough */ case TCSETAW: wait_until_sent(tty); /* fallthrough */ case TCSETA: return set_termio(tty,(struct termio *) arg, dev); case TCSBRK: if (!arg) { wait_until_sent(tty); send_break(tty); } return 0; case TCXONC: switch (arg) { case TCOOFF: tty->stopped = 1; tty->write(tty); return 0; case TCOON: tty->stopped = 0; tty->write(tty); return 0; case TCIOFF: if (STOP_CHAR(tty)) PUTCH(STOP_CHAR(tty),tty->write_q); return 0; case TCION: if (START_CHAR(tty)) PUTCH(START_CHAR(tty),tty->write_q); return 0; } return -EINVAL; /* not implemented */ case TCFLSH: if (arg==0) { flush(tty->read_q); flush(tty->secondary); if (other_tty) flush(other_tty->write_q); } else if (arg==1) flush(tty->write_q); else if (arg==2) { flush(tty->read_q); flush(tty->secondary); flush(tty->write_q); if (other_tty) flush(other_tty->write_q); } else return -EINVAL; return 0; case TIOCEXCL: return -EINVAL; /* not implemented */ case TIOCNXCL: return -EINVAL; /* not implemented */ case TIOCSCTTY: return -EINVAL; /* set controlling term NI */ case TIOCGPGRP: verify_area((void *) arg,4); put_fs_long(tty->pgrp,(unsigned long *) arg); return 0; case TIOCSPGRP: if ((current->tty < 0) || (current->tty != dev) || (tty->session != current->session)) return -ENOTTY; pgrp=get_fs_long((unsigned long *) arg); if (pgrp < 0) return -EINVAL; if (session_of_pgrp(pgrp) != current->session) return -EPERM; tty->pgrp = pgrp; return 0; case TIOCOUTQ: verify_area((void *) arg,4); put_fs_long(CHARS(tty->write_q),(unsigned long *) arg); return 0; case TIOCINQ: verify_area((void *) arg,4); put_fs_long(CHARS(tty->secondary), (unsigned long *) arg); return 0; case TIOCSTI: return -EINVAL; /* not implemented */ case TIOCGWINSZ: return get_window_size(tty,(struct winsize *) arg); case TIOCSWINSZ: if (other_tty) set_window_size(other_tty,(struct winsize *) arg); return set_window_size(tty,(struct winsize *) arg); case TIOCMGET: return -EINVAL; /* not implemented */ case TIOCMBIS: return -EINVAL; /* not implemented */ case TIOCMBIC: return -EINVAL; /* not implemented */ case TIOCMSET: return -EINVAL; /* not implemented */ case TIOCGSOFTCAR: return -EINVAL; /* not implemented */ case TIOCSSOFTCAR: return -EINVAL; /* not implemented */ case TIOCLINUX: switch (get_fs_byte((char *)arg)) { case 0: return do_screendump(arg); case 1: return do_get_ps_info(arg); default: return -EINVAL; } default: return -EINVAL; } }