static void handle_sigcont(int signum) { if (!child_controls_terminal && editor_status != EDITOR_INITIALIZING) { term_raw(); resize(); } }
void relay(int s) { char buf[8192]; int n; fd_set readfds; struct ttys *ttyp; /* Don't need our data anymore */ /* XXX This makes SunOS barf */ /* brk(0); */ signal(SIGQUIT, slirp_exit); signal(SIGHUP, slirp_exit); signal(SIGINT, slirp_exit); signal(SIGTERM, slirp_exit); /* Fudge to get term_raw and term_restore to work */ if (NULL == (ttyp = tty_attach (0, slirp_tty))) { lprint ("Error: tty_attach failed in misc.c:relay()\r\n"); slirp_exit (1); } ttyp->fd = 0; ttyp->flags |= TTY_CTTY; term_raw(ttyp); while (1) { FD_ZERO(&readfds); FD_SET(0, &readfds); FD_SET(s, &readfds); n = select(s+1, &readfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0); if (n <= 0) slirp_exit(0); if (FD_ISSET(0, &readfds)) { n = read(0, buf, 8192); if (n <= 0) slirp_exit(0); n = writen(s, buf, n); if (n <= 0) slirp_exit(0); } if (FD_ISSET(s, &readfds)) { n = read(s, buf, 8192); if (n <= 0) slirp_exit(0); n = writen(0, buf, n); if (n <= 0) slirp_exit(0); } } /* Just in case.... */ exit(1); }
int PMAPI PM_getch(void) { int c; if (term_raw() == -1) return (0); c = getc(stdin); #if defined(__QNX__) && !defined(__QNXNTO__) if (c == 0xA) c = 0x0D; else if (c == 0x7F) c = 0x08; #endif term_restore(); return c; }
/* currently handle wdgts without cursor */ if(w->crsr == -1 && w->vx < w->pxsize - w->x - w->xsize) w->vx++; return 0; } static int kbd_left(struct wdgt *w) { if(w->crsr == -1 && w->vx) w->vx--; return 0; } int scr_keyh(struct wdgt *w, int key) { int ret = KEY_HANDLED; DBG("calling key %c for %s", key, w->name); switch(key) { case KBD_DOWN: kbd_down(w); break; case KBD_UP: kbd_up(w); break; case KBD_PAGE_DOWN: kbd_page_down(w); break; case KBD_PAGE_UP: kbd_page_up(w); break; case KBD_LEFT: kbd_left(w); break; case KBD_RIGHT: kbd_right(w); break; case KBD_HOME: ret = kbd_home(w); break; case KBD_END: ret = kbd_end(w); break; default: ret = KEY_SKIPPED; } return ret; } struct termios tio; void term_raw() { struct termios t; tcgetattr(0, &tio); t = tio; t.c_lflag &= ~(ICANON|ECHO); tcsetattr(0, TCSANOW, &t); fcntl(0, F_SETFL, O_NONBLOCK); } void term_rest() { tcsetattr(0, TCSANOW, &tio); } int old_curs_vis; /* this is the cursor mode, set to normal as default */ void curses_init(void) { initscr(); /* disable cursor */ old_curs_vis = curs_set(0); start_color(); scr_color_init(); cbreak(); /* nodelay(stdscr,TRUE); keypad(stdscr, FALSE); meta(stdscr, FALSE); */ // scrollok(main_win, TRUE); noecho(); term_raw(); }
int PMAPI PM_kbhit(void) { int blocking, c; if (term_raw() == -1) return 0; /* Go into non blocking mode */ blocking = fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK; fcntl(STDIN_FILENO, F_SETFL, blocking); c = getc(stdin); /* restore blocking mode */ fcntl(STDIN_FILENO, F_SETFL, blocking & ~O_NONBLOCK); term_restore(); if (c != EOF) { ungetc(c, stdin); return c; } clearerr(stdin); return 0; }
struct ttys *tty_attach(int unit, char *device) { char buff[256], *bptr; struct ttys *ttyp, *ttyp_tmp, *ttyp_last = 0; struct stat stat; DEBUG_CALL("tty_attach"); DEBUG_ARG("unit = %d", unit); DEBUG_ARG("device = %lx", (long)device); if ((ttyp = (struct ttys *)malloc(sizeof(struct ttys))) == NULL) return 0; memset(ttyp, 0, sizeof(struct ttys)); ttyp->next = 0; ttyp->fd = 0; /* Default changed from -1 -RedWolf */ /* Only open the device if there is one */ if (device) { if ((ttyp->fd = open(device, O_RDWR)) < 0) { free(ttyp); return 0; /* XXXXX */ } lprint ("Opening device %s...\r\n\r\n", device); } /* Link it to the *tail* of the list XXXXX */ if (!ttys) { ttys = ttyp; } else { for (ttyp_tmp = ttys; ttyp_tmp; ttyp_tmp = ttyp_tmp->next) ttyp_last = ttyp_tmp; /* XXX More checks? */ ttyp_last->next = ttyp; } #ifdef FULL_BOLT fd_nonblock(ttyp->fd); #endif if (ttyp->fd >= 0 && isatty(ttyp->fd) && fstat(ttyp->fd, &stat) == 0) { /* Save the current permissions */ ttyp->mode = stat.st_mode; #ifdef HAVE_FCHMOD fchmod(ttyp->fd, S_IRUSR|S_IWUSR); #else chmod(ttyname(ttyp->fd), S_IRUSR|S_IWUSR); #endif } ttyp->unit = unit; #ifndef FULL_BOLT ttyp->towrite = towrite_max; #endif #ifndef FULL_BOLT ttyp->baud = DEFAULT_BAUD; ttyp->bytesps = ttyp->baud/10; #endif ttyp->lastime = curtime; ttyp->sc_xc_state = 0; ttyp->sc_rc_state = 0; /* Default is SLIP */ ttyp->proto = PROTO_SLIP; ttyp->up = 1; /* SLIP is always up */ ttyp->if_input = sl_input; ttyp->if_encap = sl_encap; ttys_unit[unit] = ttyp; /* Rawify the terminal, if applicable */ if (ttyp->fd >= 0) term_raw(ttyp); /* Config the new tty */ if ((bptr = (char *)getenv("HOME"))) sprintf(buff, "%s/.slirprc-%d", bptr, unit); else sprintf(buff, ".slirprc-%d", unit); config(buff, ttyp->unit); return ttyp; }
int main(int argc, char *argv[]) { int ch, idx, plen, nready, interactive = 0, listonly = 0; const char *id, *user = NULL, *pattern = NULL, *tty = NULL, *decimal = "."; char path[PATH_MAX], buf[LINE_MAX], *cp, *ep; double seconds, to_wait, speed = 1.0, max_wait = 0; FILE *lfile; fd_set *fdsw; sigaction_t sa; size_t len, nbytes, nread, off; ssize_t nwritten; #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME) setprogname(argc > 0 ? argv[0] : "sudoreplay"); #endif #ifdef HAVE_SETLOCALE setlocale(LC_ALL, ""); decimal = localeconv()->decimal_point; #endif while ((ch = getopt(argc, argv, "d:f:hlm:s:V")) != -1) { switch(ch) { case 'd': session_dir = optarg; break; case 'f': /* Set the replay filter. */ replay_filter = 0; for (cp = strtok(optarg, ","); cp; cp = strtok(NULL, ",")) { if (strcmp(cp, "stdout") == 0) SET(replay_filter, 1 << IOFD_STDOUT); else if (strcmp(cp, "stderr") == 0) SET(replay_filter, 1 << IOFD_STDERR); else if (strcmp(cp, "ttyout") == 0) SET(replay_filter, 1 << IOFD_TTYOUT); else errorx(1, "invalid filter option: %s", optarg); } break; case 'h': help(); /* NOTREACHED */ case 'l': listonly = 1; break; case 'm': errno = 0; max_wait = strtod(optarg, &ep); if (*ep != '\0' || errno != 0) errorx(1, "invalid max wait: %s", optarg); break; case 's': errno = 0; speed = strtod(optarg, &ep); if (*ep != '\0' || errno != 0) errorx(1, "invalid speed factor: %s", optarg); break; case 'V': (void) printf("%s version %s\n", getprogname(), PACKAGE_VERSION); exit(0); default: usage(1); /* NOTREACHED */ } } argc -= optind; argv += optind; if (listonly) exit(list_sessions(argc, argv, pattern, user, tty)); if (argc != 1) usage(1); /* 6 digit ID in base 36, e.g. 01G712AB */ id = argv[0]; if (!VALID_ID(id)) errorx(1, "invalid ID %s", id); plen = snprintf(path, sizeof(path), "%s/%.2s/%.2s/%.2s/timing", session_dir, id, &id[2], &id[4]); if (plen <= 0 || plen >= sizeof(path)) errorx(1, "%s/%.2s/%.2s/%.2s/%.2s/timing: %s", session_dir, id, &id[2], &id[4], strerror(ENAMETOOLONG)); plen -= 7; /* Open files for replay, applying replay filter for the -f flag. */ for (idx = 0; idx < IOFD_MAX; idx++) { if (ISSET(replay_filter, 1 << idx) || idx == IOFD_TIMING) { io_fds[idx].v = open_io_fd(path, plen, io_fnames[idx]); if (io_fds[idx].v == NULL) error(1, "unable to open %s", path); } } /* Read log file. */ path[plen] = '\0'; strlcat(path, "/log", sizeof(path)); lfile = fopen(path, "r"); if (lfile == NULL) error(1, "unable to open %s", path); cp = NULL; len = 0; /* Pull out command (third line). */ if (getline(&cp, &len, lfile) == -1 || getline(&cp, &len, lfile) == -1 || getline(&cp, &len, lfile) == -1) { errorx(1, "invalid log file %s", path); } printf("Replaying sudo session: %s", cp); free(cp); fclose(lfile); fflush(stdout); zero_bytes(&sa, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESETHAND; sa.sa_handler = cleanup; (void) sigaction(SIGINT, &sa, NULL); (void) sigaction(SIGKILL, &sa, NULL); (void) sigaction(SIGTERM, &sa, NULL); (void) sigaction(SIGHUP, &sa, NULL); sa.sa_flags = SA_RESTART; sa.sa_handler = SIG_IGN; (void) sigaction(SIGTSTP, &sa, NULL); (void) sigaction(SIGQUIT, &sa, NULL); /* XXX - read user input from /dev/tty and set STDOUT to raw if not a pipe */ /* Set stdin to raw mode if it is a tty */ interactive = isatty(STDIN_FILENO); if (interactive) { ch = fcntl(STDIN_FILENO, F_GETFL, 0); if (ch != -1) (void) fcntl(STDIN_FILENO, F_SETFL, ch | O_NONBLOCK); if (!term_raw(STDIN_FILENO, 1)) error(1, "cannot set tty to raw mode"); } fdsw = (fd_set *)emalloc2(howmany(STDOUT_FILENO + 1, NFDBITS), sizeof(fd_mask)); /* * Timing file consists of line of the format: "%f %d\n" */ #ifdef HAVE_ZLIB_H while (gzgets(io_fds[IOFD_TIMING].g, buf, sizeof(buf)) != NULL) { #else while (fgets(buf, sizeof(buf), io_fds[IOFD_TIMING].f) != NULL) { #endif if (!parse_timing(buf, decimal, &idx, &seconds, &nbytes)) errorx(1, "invalid timing file line: %s", buf); if (interactive) check_input(STDIN_FILENO, &speed); /* Adjust delay using speed factor and clamp to max_wait */ to_wait = seconds / speed; if (max_wait && to_wait > max_wait) to_wait = max_wait; delay(to_wait); /* Even if we are not relaying, we still have to delay. */ if (io_fds[idx].v == NULL) continue; /* All output is sent to stdout. */ while (nbytes != 0) { if (nbytes > sizeof(buf)) len = sizeof(buf); else len = nbytes; #ifdef HAVE_ZLIB_H nread = gzread(io_fds[idx].g, buf, len); #else nread = fread(buf, 1, len, io_fds[idx].f); #endif nbytes -= nread; off = 0; do { /* no stdio, must be unbuffered */ nwritten = write(STDOUT_FILENO, buf + off, nread - off); if (nwritten == -1) { if (errno == EINTR) continue; if (errno == EAGAIN) { FD_SET(STDOUT_FILENO, fdsw); do { nready = select(STDOUT_FILENO + 1, NULL, fdsw, NULL, NULL); } while (nready == -1 && errno == EINTR); if (nready == 1) continue; } error(1, "writing to standard output"); } off += nwritten; } while (nread > off); } } term_restore(STDIN_FILENO, 1); exit(0); } static void delay(double secs) { struct timespec ts, rts; int rval; /* * Typical max resolution is 1/HZ but we can't portably check that. * If the interval is small enough, just ignore it. */ if (secs < 0.0001) return; rts.tv_sec = secs; rts.tv_nsec = (secs - (double) rts.tv_sec) * 1000000000.0; do { memcpy(&ts, &rts, sizeof(ts)); rval = nanosleep(&ts, &rts); } while (rval == -1 && errno == EINTR); if (rval == -1) error(1, "nanosleep: tv_sec %ld, tv_nsec %ld", ts.tv_sec, ts.tv_nsec); } static void * open_io_fd(char *path, int len, const char *suffix) { path[len] = '\0'; strlcat(path, suffix, PATH_MAX); #ifdef HAVE_ZLIB_H return gzopen(path, "r"); #else return fopen(path, "r"); #endif }
InputStream openFTPStream(ParsedURL *pu, URLFile *uf) { Str tmp; int status; char *user = NULL; char *pass = NULL; Str uname = NULL; Str pwd = NULL; int add_auth_cookie_flag = FALSE; char *realpathname = NULL; if (!pu->host) return NULL; if (pu->user == NULL && pu->pass == NULL) { if (find_auth_user_passwd(pu, NULL, &uname, &pwd, 0)) { if (uname) user = uname->ptr; if (pwd) pass = pwd->ptr; } } if (user) /* do nothing */ ; else if (pu->user) user = pu->user; else user = "******"; if (current_ftp.host) { if (!strcmp(current_ftp.host, pu->host) && current_ftp.port == pu->port && !strcmp(current_ftp.user, user)) { ftp_command(¤t_ftp, "NOOP", NULL, &status); if (status != 200) ftp_close(¤t_ftp); else goto ftp_read; } else ftp_quit(¤t_ftp); } if (pass) /* do nothing */ ; else if (pu->pass) pass = pu->pass; else if (pu->user) { pwd = NULL; find_auth_user_passwd(pu, NULL, &uname, &pwd, 0); if (pwd == NULL) { if (fmInitialized) { term_raw(); pwd = Strnew_charp(inputLine("Password: "******"Password: "******"Password: "******"anonymous"); #else tmp = Strnew_charp("anonymous"); #endif /* __MINGW32_VERSION */ Strcat_char(tmp, '@'); pass = tmp->ptr; } if (!current_ftp.host) { current_ftp.host = allocStr(pu->host, -1); current_ftp.port = pu->port; current_ftp.user = allocStr(user, -1); current_ftp.pass = allocStr(pass, -1); if (!ftp_login(¤t_ftp)) return NULL; } if (add_auth_cookie_flag) add_auth_user_passwd(pu, NULL, uname, pwd, 0); ftp_read: ftp_command(¤t_ftp, "TYPE", "I", &status); if (ftp_pasv(¤t_ftp) < 0) { ftp_quit(¤t_ftp); return NULL; } if (pu->file == NULL || *pu->file == '\0' || pu->file[strlen(pu->file) - 1] == '/') goto ftp_dir; realpathname = file_unquote(pu->file); if (*realpathname == '/' && *(realpathname + 1) == '~') realpathname++; /* Get file */ uf->modtime = ftp_modtime(¤t_ftp, realpathname); ftp_command(¤t_ftp, "RETR", realpathname, &status); if (status == 125 || status == 150) return newFileStream(current_ftp.data, (void (*)())closeFTPdata); ftp_dir: pu->scheme = SCM_FTPDIR; return NULL; }
int main(int argc, char *argv[]) { const char *term = getenv("TERM"); const char *home = getenv("HOME"); const char *tag = NULL; const char *rc = NULL; const char *command = NULL; char *command_history_filename; char *search_history_filename; char *editor_dir; bool read_rc = true; int i; if (!home) home = ""; home_dir = xstrdup(home); for (i = 1; i < argc; i++) { const char *opt = argv[i]; if (opt[0] != '-' || !opt[1]) break; if (!opt[2]) { switch (opt[1]) { case 'R': read_rc = false; continue; case 't': tag = opt_arg(opt, argv[++i]); continue; case 'r': rc = opt_arg(opt, argv[++i]); continue; case 'c': command = opt_arg(opt, argv[++i]); continue; case 'V': printf("%s %s\nWritten by Timo Hirvonen\n", program, version); return 0; } if (opt[1] == '-') { i++; break; } } printf("Usage: %s [-R] [-V] [-c command] [-t tag] [-r rcfile] [file]...\n", argv[0]); return 1; } if (!isatty(1)) { fprintf(stderr, "stdout doesn't refer to a terminal\n"); return 1; } if (term == NULL || term[0] == 0) { fprintf(stderr, "TERM not set\n"); return 1; } switch (term_init(term)) { case -1: fprintf(stderr, "terminal is hardcopy\n"); return 1; case -2: fprintf(stderr, "terminal could not be found\n"); return 1; case -3: fprintf(stderr, "terminfo database could not be found\n"); return 1; } // create this early. needed if lock-files is true editor_dir = editor_file(""); mkdir(editor_dir, 0755); free(editor_dir); setlocale(LC_CTYPE, ""); charset = nl_langinfo(CODESET); if (streq(charset, "UTF-8")) term_utf8 = true; exec_builtin_rc(builtin_rc); fill_builtin_colors(); // NOTE: syntax_changed() uses window. should possibly create window after reading rc window = new_window(); root_frame = new_root_frame(window); if (read_rc) { if (rc) { read_config(commands, rc, true); } else { char *filename = editor_file("rc"); if (read_config(commands, filename, false)) { free(filename); filename = xsprintf("%s/rc", pkgdatadir); read_config(commands, filename, true); } free(filename); } } update_all_syntax_colors(); sort_aliases(); /* Terminal does not generate signals for control keys. */ set_signal_handler(SIGINT, SIG_IGN); set_signal_handler(SIGQUIT, SIG_IGN); set_signal_handler(SIGPIPE, SIG_IGN); /* Terminal does not generate signal for ^Z but someone can send * us SIGTSTP nevertheless. SIGSTOP can't be caught. */ set_signal_handler(SIGTSTP, handle_sigtstp); set_signal_handler(SIGCONT, handle_sigcont); set_signal_handler(SIGWINCH, handle_sigwinch); load_file_history(); command_history_filename = editor_file("command-history"); search_history_filename = editor_file("search-history"); history_load(&command_history, command_history_filename, command_history_size); history_load(&search_history, search_history_filename, search_history_size); if (search_history.count) search_set_regexp(search_history.ptrs[search_history.count - 1]); /* Initialize terminal but don't update screen yet. Also display * "Press any key to continue" prompt if there were any errors * during reading configuration files. */ term_raw(); if (nr_errors) { any_key(); clear_error(); } editor_status = EDITOR_RUNNING; for (; i < argc; i++) window_open_buffer(window, argv[i], false, NULL); if (window->views.count == 0) window_open_empty_buffer(window); set_view(window->views.ptrs[0]); if (command || tag) resize(); if (command) handle_command(commands, command); if (tag) { PTR_ARRAY(array); ptr_array_add(&array, xstrdup("tag")); ptr_array_add(&array, xstrdup(tag)); ptr_array_add(&array, NULL); run_commands(commands, &array); ptr_array_free(&array); } resize(); main_loop(); ui_end(); // unlock files and add files to file history remove_frame(root_frame); history_save(&command_history, command_history_filename); history_save(&search_history, search_history_filename); free(command_history_filename); free(search_history_filename); save_file_history(); return 0; }