Annotation of embedaddon/sudo/common/term.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (c) 2011 Todd C. Miller <Todd.Miller@courtesan.com>
! 3: *
! 4: * Permission to use, copy, modify, and distribute this software for any
! 5: * purpose with or without fee is hereby granted, provided that the above
! 6: * copyright notice and this permission notice appear in all copies.
! 7: *
! 8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
! 13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
! 14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 15: */
! 16:
! 17: #include <config.h>
! 18:
! 19: #include <sys/types.h>
! 20: #include <sys/param.h>
! 21: #include <stdio.h>
! 22: #ifdef STDC_HEADERS
! 23: # include <stdlib.h>
! 24: # include <stddef.h>
! 25: #else
! 26: # ifdef HAVE_STDLIB_H
! 27: # include <stdlib.h>
! 28: # endif
! 29: #endif /* STDC_HEADERS */
! 30: #ifdef HAVE_STRING_H
! 31: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
! 32: # include <memory.h>
! 33: # endif
! 34: # include <string.h>
! 35: #endif /* HAVE_STRING_H */
! 36: #ifdef HAVE_STRINGS_H
! 37: # include <strings.h>
! 38: #endif /* HAVE_STRINGS_H */
! 39: #include <termios.h>
! 40:
! 41: #include "missing.h"
! 42:
! 43: #ifndef TCSASOFT
! 44: # define TCSASOFT 0
! 45: #endif
! 46: #ifndef ECHONL
! 47: # define ECHONL 0
! 48: #endif
! 49: #ifndef IEXTEN
! 50: # define IEXTEN 0
! 51: #endif
! 52: #ifndef IUCLC
! 53: # define IUCLC 0
! 54: #endif
! 55:
! 56: #ifndef _POSIX_VDISABLE
! 57: # ifdef VDISABLE
! 58: # define _POSIX_VDISABLE VDISABLE
! 59: # else
! 60: # define _POSIX_VDISABLE 0
! 61: # endif
! 62: #endif
! 63:
! 64: static struct termios term, oterm;
! 65: static int changed;
! 66: int term_erase;
! 67: int term_kill;
! 68:
! 69: int
! 70: term_restore(int fd, int flush)
! 71: {
! 72: if (changed) {
! 73: int flags = TCSASOFT;
! 74: flags |= flush ? TCSAFLUSH : TCSADRAIN;
! 75: if (tcsetattr(fd, flags, &oterm) != 0)
! 76: return 0;
! 77: changed = 0;
! 78: }
! 79: return 1;
! 80: }
! 81:
! 82: int
! 83: term_noecho(int fd)
! 84: {
! 85: if (!changed && tcgetattr(fd, &oterm) != 0)
! 86: return 0;
! 87: (void) memcpy(&term, &oterm, sizeof(term));
! 88: CLR(term.c_lflag, ECHO|ECHONL);
! 89: #ifdef VSTATUS
! 90: term.c_cc[VSTATUS] = _POSIX_VDISABLE;
! 91: #endif
! 92: if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
! 93: changed = 1;
! 94: return 1;
! 95: }
! 96: return 0;
! 97: }
! 98:
! 99: int
! 100: term_raw(int fd, int isig)
! 101: {
! 102: struct termios term;
! 103:
! 104: if (!changed && tcgetattr(fd, &oterm) != 0)
! 105: return 0;
! 106: (void) memcpy(&term, &oterm, sizeof(term));
! 107: /* Set terminal to raw mode */
! 108: term.c_cc[VMIN] = 1;
! 109: term.c_cc[VTIME] = 0;
! 110: CLR(term.c_iflag, ICRNL | IGNCR | INLCR | IUCLC | IXON);
! 111: CLR(term.c_oflag, OPOST);
! 112: CLR(term.c_lflag, ECHO | ICANON | ISIG | IEXTEN);
! 113: if (isig)
! 114: SET(term.c_lflag, ISIG);
! 115: if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
! 116: changed = 1;
! 117: return 1;
! 118: }
! 119: return 0;
! 120: }
! 121:
! 122: int
! 123: term_cbreak(int fd)
! 124: {
! 125: if (!changed && tcgetattr(fd, &oterm) != 0)
! 126: return 0;
! 127: (void) memcpy(&term, &oterm, sizeof(term));
! 128: /* Set terminal to half-cooked mode */
! 129: term.c_cc[VMIN] = 1;
! 130: term.c_cc[VTIME] = 0;
! 131: CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
! 132: SET(term.c_lflag, ISIG);
! 133: #ifdef VSTATUS
! 134: term.c_cc[VSTATUS] = _POSIX_VDISABLE;
! 135: #endif
! 136: if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
! 137: term_erase = term.c_cc[VERASE];
! 138: term_kill = term.c_cc[VKILL];
! 139: changed = 1;
! 140: return 1;
! 141: }
! 142: return 0;
! 143: }
! 144:
! 145: int
! 146: term_copy(int src, int dst)
! 147: {
! 148: struct termios tt;
! 149:
! 150: if (tcgetattr(src, &tt) != 0)
! 151: return 0;
! 152: if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)
! 153: return 0;
! 154: return 1;
! 155: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>