void ask_dev(char *dbuf, const char *msg) { char buf[DEV_NAME_LEN]; int len; clear(); unset_tty(); if (msg) printf("%s\n", msg); if (dbuf) printf("Enter device name [%s]:", dbuf); else printf("Enter device name:"); if (fgets(buf, DEV_NAME_LEN - 1, stdin)) { len = strlen(buf); if (buf[len - 1] == '\n') buf[len - 1] = '\0'; if (buf[0] != '\0' && buf[0] != ' ') strcpy(dbuf, buf); } set_tty(); }
Implementation(Memory &mem, LC3::CPU &cpu) : mem(mem), mcr(ccr, cpu), ifd(-1), ofd(-1) { mem.register_dma(KBSR::ADDRESS, &kbsr); mem.register_dma(KBDR::ADDRESS, &kbdr); mem.register_dma(DSR::ADDRESS, &dsr); mem.register_dma(DDR::ADDRESS, &ddr); mem.register_dma(MCR::ADDRESS, &mcr); mem.register_dma(CCR::ADDRESS, &ccr); #ifdef USE_LC3_IO_EMU uint16_t addr; addr = SW_S; mem.register_dma(addr, new GenericIO(addr)); addr = SW_D; mem.register_dma(addr, new GenericIO(addr)); addr = BTN_S; mem.register_dma(addr, new GenericIO(addr)); addr = BTN_D; mem.register_dma(addr, new GenericIO(addr)); addr = SSEG_S; mem.register_dma(addr, new GenericIO(addr)); addr = SSEG_D; mem.register_dma(addr, new GenericIO(addr)); addr = LED_S; mem.register_dma(addr, new GenericIO(addr)); addr = LED_D; mem.register_dma(addr, new GenericIO(addr)); addr = PS2KBD_S; mem.register_dma(addr, new GenericIO(addr)); addr = PS2KBD_D; mem.register_dma(addr, new GenericIO(addr)); #endif // Set the terminal to non-echo mode set_tty(fileno(stdin), fileno(stdout)); }
/* Create a pseudo terminal for other process to use (as this program is using up the actual TTY) */ int create_pseudo_tty() { int amaster, aslave; int flags; if (openpty(&amaster, &aslave, NULL, NULL, NULL) == -1) { errlog("Error: Openpty failed - %m\n"); return -1; } /* Set to non blocking mode */ flags = fcntl(amaster, F_GETFL); flags |= O_NONBLOCK; fcntl(amaster, F_SETFL, flags); FILE *pseudo_save_file = fopen(pseudo_tty_save_file, "w+"); if (!pseudo_save_file) { errlog("Error: Unable to open the pseudo info file - %m\n"); return -1; } /* Save the name of the created pseudo tty in a text file for other processes to use */ if (fprintf(pseudo_save_file, "%s\n", ttyname(aslave)) == -1) { errlog("Error writing to the pseudo info file\n"); fclose(pseudo_save_file); return -1; } fclose(pseudo_save_file); if (set_tty(aslave) == -1) { errlog("Error: Slave TTY not set properly\n"); return -1; } return amaster; }
/* opens a rs232 window, returns handle to give to functions below. */ int rs232dev_open(int device) { int i, fd; for (i = 0; i < RS232_NUM_DEVICES; i++) { if (!fds[i].inuse) { break; } } if (i >= RS232_NUM_DEVICES) { log_error(rs232dev_log, "No more devices available."); return -1; } #ifdef DEBUG log_message(rs232dev_log, "rs232dev_open(device=%d).", device); #endif if (rs232_devfile[device][0] == '|') { #if defined(OPENSTEP_COMPILE) || defined(RHAPSODY_COMPILE) \ || defined(NEXTSTEP_COMPILE) log_error(rs232dev_log, "Forking not supported on this platform."); return -1; #else if (fork_coproc(&fds[i].fd_w, &fds[i].fd_r, rs232_devfile[device] + 1) < 0) { log_error(rs232dev_log, "Cannot fork process."); return -1; } #endif fds[i].type = T_PROC; fds[i].inuse = 1; fds[i].file = rs232_devfile[device]; } else { #if !defined(OPENSTEP_COMPILE) && !defined(NEXTSTEP_COMPILE) fd = open(rs232_devfile[device], O_RDWR | O_NOCTTY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd < 0) { log_error(rs232dev_log, "Cannot open file \"%s\": %s", rs232_devfile[device], strerror(errno)); return -1; } fds[i].fd_r = fds[i].fd_w = fd; fds[i].file = rs232_devfile[device]; if (isatty(fd)) { fds[i].type = T_TTY; set_tty(i, devbaud[device]); } else { fds[i].type = T_FILE; } fds[i].inuse = 1; #endif } return i; }
static void setup_scr(void) { char *cbuf = gbuf, *term; if (!opt_interactive) return; if ((term = getenv("TERM"))) if (tgetent(tbuf, term) == 1) if (tgetstr("cl", &cbuf)) clear_ok = 1; set_tty(); clear(); }
void execute(char *filename, char *cmd, char *av0, char *av1, char *av2) { int id; int n; char *argp[4]; argp[0] = av0; argp[1] = av1; argp[2] = av2; argp[3] = NULL; fflush(stdout); reset_tty(); for (n = 10; (id = fork()) < 0 && n > 0; n--) sleep(5); if (id == 0) { execvp(cmd, argp); write(STDERR_FILENO, "exec failed\n", 12); exit(1); } if (id > 0) { sa.sa_flags = SA_RESTART; sa.sa_handler = SIG_IGN; (void)sigaction(SIGINT, &sa, NULL); (void)sigaction(SIGQUIT, &sa, NULL); if (catch_susp) { sa.sa_handler = SIG_DFL; (void)sigaction(SIGTSTP, &sa, NULL); (void)sigaction(SIGTTIN, &sa, NULL); (void)sigaction(SIGTTOU, &sa, NULL); } while (wait(NULL) > 0) continue; sa.sa_flags = 0; sa.sa_handler = onsignal; (void)sigaction(SIGINT, &sa, NULL); (void)sigaction(SIGQUIT, &sa, NULL); if (catch_susp) { (void)sigaction(SIGTSTP, &sa, NULL); (void)sigaction(SIGTTIN, &sa, NULL); (void)sigaction(SIGTTOU, &sa, NULL); } } else write(STDERR_FILENO, "can't fork\n", 11); set_tty(); if (!altscr) fputs("------------------------\n", stdout); prompt(filename); }
int create_cmdline_interface (char *prmt) { /* init the tty promt properties*/ init_tty_prompt (); /*signal handlers for SIGINT, SIGSEGV, SIGUSR1, SIGUSR2*/ init_signals(); /*XXX:set the tty prompt - we take control from shell don't forget to call reset_tty () once your job is done*/ set_tty(); set_prompt(prmt, strlen(prmt)); create_sync_lock (&cmd_root.lock); /*finally kick-start the shell thread*/ spawn_cli_thread (); return 0; }
static uint32_t TPM_OpenClientCharDev(int *sock_fd) { char *tty_str; uint32_t rc; tty_str = getenv("TPM_DEVICE"); if (tty_str == NULL || !strcmp("unixio",tty_str)) { rc = TPM_OpenClientSocket_UnixIO(sock_fd); if (rc == 0) { return 0; } } if (tty_str == NULL) { tty_str = DEFAULT_TPM_DEVICE; } #ifndef USE_SERIAL_PORT if ((*sock_fd = open(tty_str,O_RDWR)) < 0) { printf("TPM_OpenClientCharDev: Could not open char device %s: %s\n", tty_str, strerror(errno)); return ERR_IO; } #else if ((*sock_fd = open(tty_str,O_RDWR | O_NOCTTY | O_NDELAY)) < 0) { printf("TPM_OpenClientCharDev: Could not open char device %s: %s\n", tty_str, strerror(errno)); return ERR_IO; } fcntl(*sock_fd, F_SETFL, 0); if ((rc = set_tty(*sock_fd)) > 0) { close(*sock_fd); return rc; } #endif return 0; }
void flusho(void) { if (obufp == obuf) return; if (no_out == 0) { if (!toolate) { toolate++; #ifdef NROFF set_tty(); { char *p = t.twinit; while (*p++) ; if (p - t.twinit > 1) write(ptid, t.twinit, p - t.twinit - 1); } #endif } toolate += write(ptid, obuf, obufp - obuf); } obufp = obuf; }
int main(int argc, char *argv[]) { int result; extern char *optarg; extern int optind; int maxfd = -1; char devbuf[512]; int devbytes; int remoteaddrlen; int c; int waitlogged = 0; int maxConnects = 1; int writeonly = 0; register int i; while ( (c=getopt(argc,argv,"dl:m:p:r:s:wx:")) != EOF ) switch (c) { case 'd': isdaemon = 1; break; case 'l': linkname = optarg; break; case 'x': debug = atoi(optarg); break; case 'm': maxConnects = atoi(optarg); break; case 'p': server_port = atoi(optarg); break; case 'r': machinename = optarg; break; case 's': sttyparms = optarg; break; case 'w': writeonly = 1; break; case '?': usage(argv[0]); exit(1); } sdevname = argv[optind]; remotefd = (int *) malloc (maxConnects * sizeof(int)); // struct group *getgrgid(gid_t gid); printf("sdevname=%s,server_port=%d,stty=%s\n",sdevname,server_port,sttyparms); openlog("remserial", LOG_PID, LOG_USER); if (writeonly) devfd = open(sdevname,O_WRONLY); else devfd = open(sdevname,O_RDWR); if ( devfd == -1 ) { syslog(LOG_ERR, "Open of %s failed: %m",sdevname); printf("Open of %s failed: %m",sdevname); exit(1); } if (linkname) link_slave(devfd); if ( sttyparms ) { set_tty(devfd,sttyparms); } signal(SIGINT,sighandler); signal(SIGHUP,sighandler); signal(SIGTERM,sighandler); if ( machinename ) { //----------------------------------------------------------------- // We are the client, Find the IP address for the remote machine //----------------------------------------------------------------- remotehost = gethostbyname(machinename); if ( !remotehost ) { syslog(LOG_ERR, "Couldn't determine address of %s", machinename ); exit(1); } /* Copy it into the addr structure */ addr.sin_family = AF_INET; addr.sin_addr.s_addr = 0; //memcpy(&(addr.sin_addr),remotehost->h_addr_list[0], sizeof(struct in_addr)); //addr.sin_port = htons(server_port); addr.sin_port = htons(0); remotefd[curConnects] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if ( remotefd[curConnects] == -1 ) { syslog(LOG_ERR, "Can't open socket: %m"); exit(1); } /* Set up to listen on the given port */ if( bind( remotefd[curConnects], (struct sockaddr*)(&addr), sizeof(struct sockaddr_in)) < 0 ) { syslog(LOG_ERR, "Couldn't bind port %d, aborting: %m",client_port ); exit(1); } addr.sin_family = AF_INET; memcpy(&(addr.sin_addr),remotehost->h_addr_list[0], sizeof(struct in_addr)); addr.sin_port = htons(server_port); if (connect(remotefd[curConnects], (struct sockaddr*)(&addr), sizeof(struct sockaddr_in)) < 0 ) { syslog(LOG_ERR, "Couldn't connect client socket to server socket, aborting: %m"); exit(1); } if ( debug>1 ) syslog(LOG_NOTICE,"Connected to remote UDP socket"); curConnects += 1; } else { //------------------------------- // We are the server //------------------------------- /* Open the initial socket for communications */ sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if ( sockfd == -1 ) { syslog(LOG_ERR, "Can't open socket: %m"); exit(1); } addr.sin_family = AF_INET; addr.sin_addr.s_addr = 0; addr.sin_port = htons(server_port); /* Set up to listen on the given port */ if( bind( sockfd, (struct sockaddr*)(&addr), sizeof(struct sockaddr_in)) < 0 ) { syslog(LOG_ERR, "Couldn't bind port %d, aborting: %m",server_port ); exit(1); } if ( debug>1 ) syslog(LOG_NOTICE,"Bound port"); } if ( isdaemon ) { setsid(); close(0); close(1); close(2); } /* Set up the files/sockets for the select() call */ if ( sockfd != -1 ) { FD_SET(sockfd,&fdsread); if ( sockfd >= maxfd ) maxfd = sockfd + 1; } for (i=0 ; i<curConnects ; i++) { FD_SET(remotefd[i],&fdsread); if ( remotefd[i] >= maxfd ) maxfd = remotefd[i] + 1; } if (!writeonly) { FD_SET(devfd,&fdsread); if ( devfd >= maxfd ) maxfd = devfd + 1; } while (1) { /* Wait for data from the listening socket, the device or the remote connection */ fdsreaduse = fdsread; if ( select(maxfd,&fdsreaduse,NULL,NULL,NULL) == -1 ) break; /* Activity on the controlling socket, only on server */ if ( !machinename && FD_ISSET(sockfd,&fdsreaduse) ) { remoteaddrlen = sizeof(struct sockaddr_in); devbytes = recvfrom(sockfd, devbuf, 512, 0, (struct sockaddr *) &remoteaddr, &remoteaddrlen); //if ( debug>1 && devbytes>0 ) if (debug>1) { syslog(LOG_INFO,"Remote: %d bytes",devbytes); } printf("Socket Remote read: %d bytes\n",devbytes); if ( devbytes == 0 ) { } else if ( devfd != -1 ) { if (connect(sockfd, (struct sockaddr*)(&remoteaddr), remoteaddrlen) < 0 ) { syslog(LOG_ERR, "Couldn't connect server socket to client socket, aborting: %m"); exit(1); } // socket read/write //remotefd[curConnects] = sockfd; /* Write the data to the device */ result = write(devfd,devbuf,devbytes); if (result < 0) { printf("write(%d,devbuf,%d):%s", devfd, devbytes,strerror(result)); } else { printf("Write to device returned %d", result); } fflush(NULL); } } /* Data to read from the device */ if ( FD_ISSET(devfd,&fdsreaduse) ) { devbytes = read(devfd,devbuf,512); //if ( debug>1 && devbytes>0 ) if (debug>1) { syslog(LOG_INFO,"Device: %d bytes",devbytes); } printf("Device: %d bytes", devbytes); if ( devbytes <= 0 ) { if ( debug>0 ) { syslog(LOG_INFO,"%s closed",sdevname); printf("%s closed",sdevname); } close(devfd); FD_CLR(devfd,&fdsread); while (1) { devfd = open(sdevname,O_RDWR); if ( devfd != -1 ) break; syslog(LOG_ERR, "Open of %s failed: %m", sdevname); if ( errno != EIO ) exit(1); sleep(1); } if ( debug>0 ) syslog(LOG_INFO,"%s re-opened",sdevname); if ( sttyparms ) set_tty(devfd,sttyparms); if (linkname) link_slave(devfd); FD_SET(devfd,&fdsread); if ( devfd >= maxfd ) maxfd = devfd + 1; } else { for (i=0 ; i<curConnects ; i++) { result = write(remotefd[i],devbuf,devbytes); if (result < 0) { printf("write(remotefd[%d]=%d,devbuf,%d):%s", i, remotefd[i], devbytes, strerror(result)); } else { printf("Write to remotefd[%d]=%d returned %d", i, remotefd[i], result); } fflush(NULL); } if ( !machinename) { result = write(sockfd, devbuf,devbytes); if (result < 0) { printf("write(sockfd=%d,devbuf,%d):%s", sockfd, devbytes, strerror(result)); } else { printf("Write to sockfd=%d returned %d", sockfd, result); } fflush(NULL); } } } /* Data to read from the remote system */ for (i=0 ; i<curConnects ; i++) if (FD_ISSET(remotefd[i],&fdsreaduse) ) { devbytes = read(remotefd[i],devbuf,512); //if ( debug>1 && devbytes>0 ) if (debug>1) { syslog(LOG_INFO,"Remote: %d bytes",devbytes); } printf("Remote: %d bytes",devbytes); if ( devbytes == 0 ) { /*register int j; syslog(LOG_NOTICE,"Connection closed"); printf("Connection closed"); close(remotefd[i]); FD_CLR(remotefd[i],&fdsread); curConnects--; for (j=i ; j<curConnects ; j++) remotefd[j] = remotefd[j+1]; if ( machinename ) { // Wait for the server again remotefd[curConnects++] = connect_to(&addr); FD_SET(remotefd[curConnects-1],&fdsread); if ( remotefd[curConnects-1] >= maxfd ) maxfd = remotefd[curConnects-1] + 1; }*/ } else if ( devfd != -1 ) { /* Write the data to the device */ result = write(devfd,devbuf,devbytes); if (result < 0) { printf("write(devfd=%d,devbuf,%d):%s", devfd, devbytes, strerror(result)); } else { printf("Write to devfd=%d returned %d", devfd, result); } fflush(NULL); } } } close(sockfd); for (i=0 ; i<curConnects ; i++) close(remotefd[i]); printf("End of program, normal exit\n"); }
/* EFUN: "Hit Breakpoint" */ f_bkpt() { clean_exit(); bpt(); set_tty(); }
void set_tty(int fd) { set_tty(fd, fd); }
int main(int argc, char **argv) { char read_tty[TTY_LEN] = { 0 }; int ip_version; int socket_domain = AF_UNSPEC; char cmd[COMMAND_LEN] = { 0 }; /* Open the error log file */ error_file = fopen(error_log_file, "a+"); if (!error_file) { printf("Error: Unable to open log file - %m\n"); return 1; } /* Register actions upon interrupts */ register_kill(); /* Parse the user input */ if (!parse_user_input(argc, argv, read_tty, sizeof(read_tty), &ip_version)) { return 2; } snprintf(cmd, sizeof(cmd), "%s %s", uS_console, "connect"); if (system(cmd) == -1) { errlog("Error: Unable to connect to the micro-server\n"); return 3; } /* Create a socket to communicate with the netcons server */ socket_domain = (ip_version == IPV4) ? AF_INET : AF_INET6; fd_soc = socket(socket_domain, SOCK_DGRAM, 0); if (fd_soc == -1) { errlog("Error: Socket creation failed - %m\n"); return 4; } if (ip_version == IPV4) { /* IPv4 */ struct sockaddr_in tgt_addr; if (!prepare_sock(&tgt_addr)) { close(fd_soc); errlog("Error: Socket not valid\n"); return 5; } if (connect(fd_soc, (struct sockaddr *)&tgt_addr, sizeof(tgt_addr)) == -1) { close(fd_soc); errlog("Error: Socket connection failed - %m\n"); return 6; } } else { /* IPv6 */ struct sockaddr_in6 tgt_addr6; if (!prepare_sock6(&tgt_addr6)) { close(fd_soc); errlog("Error: Socket not valid\n"); return 5; } if (connect(fd_soc, (struct sockaddr *)&tgt_addr6, sizeof(tgt_addr6)) == -1) { close(fd_soc); errlog("Error: Socket connection failed - %m\n"); return 6; } } /* TTY Operations */ if ((fd_tty = open(read_tty, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1) { close(fd_soc); errlog("Error: Serial Port %s open failed - %m\n", read_tty); return 7; } if (!set_tty(fd_tty)) { errlog("Error: tty not set properly\n"); cleanup(); return 8; } /* Read, prepare and send the logs */ if (!read_send(fd_tty, fd_soc)) { errlog("Error: Sending logs failed\n"); cleanup(); return 9; } cleanup(); return 0; }
/*{{{ main*/ int main(int argc, char *argv[]) { /*{{{ variables*/ int x, login_y, password_y; char loginstr[9], passwordstr[9], ret; char ttystr[_POSIX_PATH_MAX]; char *background = (char *)0; char *fontname = (char *)0; /*}}} */ /*{{{ parse arguments*/ { int c; while ((c = getopt(argc, argv, "b:f:")) != EOF) switch (c) { case 'b': background = optarg; break; case 'f': fontname = optarg; break; } /*{{{ parse tty*/ { int tty; if (optind + 1 > argc) { fprintf(stderr, "Usage: %s tty\n", argv[0]); exit(1); } else { strcpy(ttystr, "/dev/"); strcat(ttystr, argv[optind++]); } close(0); close(1); close(2); setsid(); if ((tty = open(ttystr, O_RDWR)) != 0) { fprintf(stderr, "%s: Can't open controlling terminal on fd 0.\n", argv[0]); exit(1); } fchmod(tty, 0600); fchown(tty, getuid(), getgid()); open(argv[optind], O_RDWR); open(argv[optind], O_RDWR); } /*}}} */ } /*}}} */ /*{{{ get into grafics mode*/ signal(SIGTERM, quit); signal(SIGHUP, quit); set_tty(0); if ((screen = bit_open(SCREEN_DEV)) == (BITMAP *)0) { reset_tty(0); exit(EX_NOPERM); } bit_grafscreen(); /*}}} */ /*{{{ load font*/ if (fontname) { char fontpath[_POSIX_PATH_MAX]; if (*fontname == '/' || *fontname == '.') strcpy(fontpath, fontname); else { strcpy(fontpath, ICONDIR); strcat(fontpath, "/"); strcat(fontpath, fontname); } if ((font = open_font(fontname)) == (struct font *)0) font = open_font((char *)0); } else font = open_font((char *)0); /*}}} */ /*{{{ draw background*/ bit_blit(screen, 0, 0, screen->wide, screen->high, BIT_CLR, (BITMAP *)0, 0, 0); if (background) { BITMAP *bp; FILE *fp; char backgroundpath[_POSIX_PATH_MAX]; if (*background == '/' || *background == '.') strcpy(backgroundpath, background); else { strcpy(backgroundpath, ICONDIR); strcat(backgroundpath, "/"); strcat(backgroundpath, background); } if ((fp = fopen(backgroundpath, "r")) != (FILE *)0 && (bp = bitmapread(fp)) != (BITMAP *)0) { int x, y; for (x = 0; x < screen->wide; x += bp->wide) bit_blit( screen, x, 0, screen->wide - x < bp->wide ? screen->wide - x : bp->wide, bp->high, BIT_SRC, bp, 0, 0); for (y = 0; y < screen->high; y += bp->high) bit_blit( screen, 0, y, screen->wide, screen->high - y < bp->high ? screen->high - y : bp->high, BIT_SRC, screen, 0, 0); } } /*}}} */ /*{{{ draw hostname*/ { int bx, bw, by, bh; char hostname[_POSIX_PATH_MAX]; struct hostent *h; gethostname(hostname, sizeof(hostname)); if ((h = gethostbyname(hostname)) != (struct hostent *)0) strcpy(hostname, h->h_name); bw = font->head.wide * (strlen(hostname) + 2); bh = 2 * font->head.high; bx = (screen->wide - bw) / 2; by = screen->high / 6 - bh / 2; cutebox(bx, by, bw, bh); printstr(bx + font->head.wide, by + bh - font->head.high / 2, hostname); } /*}}} */ /*{{{ draw login box*/ { int bx, bw, by, bh; bx = (screen->wide - font->head.wide * 40) / 2; by = (screen->high - font->head.high * 8) / 2; bw = font->head.wide * 40; bh = font->head.high * 8; cutebox(bx, by, bw, bh); } /*}}} */ /*{{{ draw login box contents*/ x = (screen->wide - font->head.wide * 18) / 2; login_y = screen->high / 2 - font->head.wide / 6; password_y = screen->high / 2 + font->head.high / 6 + font->head.high; printstr(x, password_y, "Password: "******"Press ESC for terminal login"); /*}}} */ while (1) { /*{{{ get login and password or escape*/ printstr(x, login_y, "Login: "******"mgr", (char *)0 }; int i; sprintf(env_user, "USER=%s", pw->pw_name); sprintf(env_logname, "LOGNAME=%s", pw->pw_name); sprintf(env_home, "HOME=%s", pw->pw_dir); sprintf(env_shell, "SHELL=%s", pw->pw_shell == (char *)0 || pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell); sprintf(env_path, "PATH=%s", PATH); sprintf(env_mail, "MAIL=%s/%s", MAILDIR, pw->pw_name); if (chdir(pw->pw_dir) != 0) chdir("/"); if (ttyname(0)) { chown(ttyname(0), pw->pw_uid, pw->pw_gid); chmod(ttyname(0), 0600); } for (i = 1; i <= _NSIG; i++) signal(i, SIG_DFL); bit_destroy(screen); reset_tty(0); initgroups(pw->pw_name, pw->pw_gid); setgid(pw->pw_gid); setuid(pw->pw_uid); sprintf(mgrlogin, "%s/.mgrlogin", pw->pw_dir); execve(mgrlogin, login_argv, login_env); execve(MGR_BINARY, login_argv, login_env); exit(EX_OSFILE); } /*}}} */ else /*{{{ incorrect login*/ { printstr((screen->wide - font->head.wide * 16) / 2, login_y + 3 * font->head.high, "Login incorrect"); } /*}}} */ } /*}}} */ } }
int handle_signal(void) { int sig, ch = -1; for (sig = 0; sig < _NSIG; sig++) { if (signo[sig] == 0) continue; signo[sig] = 0; switch (sig) { case SIGQUIT: if (!inwait) { putchar('\n'); if (startup) Pause++; } else if (!dum_opt && notell) { write(STDERR_FILENO, QUIT_IT, sizeof(QUIT_IT) - 1); promptlen += sizeof(QUIT_IT) - 1; notell = 0; } break; case SIGTSTP: case SIGTTIN: case SIGTTOU: /* XXX - should use saved values instead of SIG_DFL */ sa.sa_handler = SIG_DFL; sa.sa_flags = SA_RESTART; (void)sigaction(SIGTSTP, &sa, NULL); (void)sigaction(SIGTTIN, &sa, NULL); (void)sigaction(SIGTTOU, &sa, NULL); reset_tty(); kill(getpid(), sig); sa.sa_handler = onsignal; sa.sa_flags = 0; (void)sigaction(SIGTSTP, &sa, NULL); (void)sigaction(SIGTTIN, &sa, NULL); (void)sigaction(SIGTTOU, &sa, NULL); set_tty(); if (!no_intty) ch = '\f'; /* force redraw */ break; case SIGINT: end_it(); break; case SIGWINCH: { struct winsize win; if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) != 0) break; if (win.ws_row != 0) { Lpp = win.ws_row; nscroll = Lpp/2 - 1; if (nscroll <= 0) nscroll = 1; dlines = Lpp - 1; } if (win.ws_col != 0) Mcol = win.ws_col; if (!no_intty) ch = '\f'; /* force redraw */ break; } default: /* NOTREACHED */ break; } } return (ch); }
int main(int argc, char **argv) { FILE * volatile f; char *s; volatile int left; volatile off_t initline; volatile int prnames = 0; volatile int initopt = 0; volatile int srchopt = 0; int clearit = 0; int ch; char initbuf[80]; if (pledge("stdio rpath tty", NULL) == -1) { perror("pledge"); exit(1); } setlocale(LC_ALL, ""); /* all signals just use a stub handler and interrupt syscalls */ sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = onsignal; nfiles = argc; fnames = argv; initterm(); nscroll = Lpp/2 - 1; if (nscroll <= 0) nscroll = 1; if ((s = getenv("MORE")) != NULL && *s != '\0') argscan(s); while (--nfiles > 0) { if ((ch = (*++fnames)[0]) == '-') argscan(*fnames + 1); else if (ch == '+') { s = *fnames; if (*++s == '/') { srchopt++; (void)strlcpy(initbuf, ++s, sizeof(initbuf)); } else { initopt++; for (initline = 0; *s != '\0'; s++) { if (isdigit((unsigned char)*s)) initline = initline * 10 + *s - '0'; } --initline; } } else break; } /* * Allow clreol only if Home and eraseln and EodClr strings are * defined, and in that case, make sure we are in noscroll mode. */ if (clreol) { if (Home == NULL || *Home == '\0' || eraseln == NULL || *eraseln == '\0' || EodClr == NULL || *EodClr == '\0') clreol = 0; else noscroll = 1; } if (dlines == 0) dlines = Lpp - 1; left = dlines; if (nfiles > 1) prnames++; if (!no_intty && nfiles == 0) usage(); else f = stdin; if (!no_tty) { struct sigaction osa; (void)sigaction(SIGQUIT, &sa, NULL); (void)sigaction(SIGINT, &sa, NULL); (void)sigaction(SIGWINCH, &sa, NULL); if (sigaction(SIGTSTP, &osa, NULL) == 0 && osa.sa_handler == SIG_DFL) { (void)sigaction(SIGTSTP, &sa, NULL); (void)sigaction(SIGTTIN, &sa, NULL); (void)sigaction(SIGTTOU, &sa, NULL); catch_susp++; } set_tty(); } if (no_intty) { if (no_tty) copy_file(stdin); else { if ((ch = Getc(f)) == '\f') doclear(); else { Ungetc(ch, f); if (noscroll && ch != EOF) { if (clreol) home(); else doclear(); } } if (srchopt) { if (search(initbuf, stdin, 1) == 0 && noscroll) left--; } else if (initopt) skiplns(initline, stdin); screen(stdin, left); } no_intty = 0; dup2(STDERR_FILENO, STDIN_FILENO); /* stderr is a tty */ prnames++; firstf = 0; } while (fnum < nfiles) { if ((f = checkf(fnames[fnum], &clearit)) != NULL) { context.line = context.chrctr = 0; Currline = 0; restart: if (firstf) { firstf = 0; if (srchopt) { if (search(initbuf, f, 1) < 0) goto restart; if (noscroll) left--; } else if (initopt) skiplns(initline, f); } else if (fnum < nfiles && !no_tty) left = command(fnames[fnum], f); if (left != 0) { if ((noscroll || clearit) && (file_size != LONG_MAX)) { if (clreol) home(); else doclear(); } if (prnames) { if (bad_so) erasep(0); if (clreol) cleareol(); fputs("::::::::::::::", stdout); if (promptlen > 14) erasep(14); putchar('\n'); if (clreol) cleareol(); printf("%s\n", fnames[fnum]); if (clreol) cleareol(); fputs("::::::::::::::\n", stdout); if (left > Lpp - 4) left = Lpp - 4; } if (no_tty) copy_file(f); else { within++; screen(f, left); within = 0; } } fflush(stdout); fclose(f); screen_start.line = screen_start.chrctr = 0L; context.line = context.chrctr = 0L; } fnum++; firstf = 0; } reset_tty(); exit(0); }
LOCAL void reset_tty() { #ifdef USE_V7_TTY if (ioctl(STDIN_FILENO, TIOCSETN, &savetty) == -1) { #else #ifdef TCSANOW if (tcsetattr(STDIN_FILENO, TCSANOW, &savetty) == -1) { #else if (ioctl(STDIN_FILENO, TCSETAF, &savetty) == -1) { #endif #endif printf("Cannot put tty into normal mode\n"); exit(1); } } LOCAL void set_tty() { #ifdef USE_V7_TTY if (ioctl(STDIN_FILENO, TIOCSETN, &newtty) == -1) { #else #ifdef TCSANOW if (tcsetattr(STDIN_FILENO, TCSANOW, &newtty) == -1) { #else if (ioctl(STDIN_FILENO, TCSETAF, &newtty) == -1) { #endif #endif printf("Cannot put tty into raw mode\n"); exit(1); } } /* * Come here when we get a suspend signal from the terminal */ LOCAL void onsusp(sig) int sig; { #ifdef SIGTTOU /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */ signal(SIGTTOU, SIG_IGN); #endif reset_tty(); fflush(stdout); #ifdef SIGTTOU signal(SIGTTOU, SIG_DFL); /* Send the TSTP signal to suspend our process group */ signal(SIGTSTP, SIG_DFL); /* sigsetmask(0);*/ kill(0, SIGTSTP); /* Pause for station break */ /* We're back */ signal(SIGTSTP, onsusp); #endif set_tty(); } LOCAL void crsr2(row, col) int row; int col; { printf("\033[%d;%dH", row, col); } LOCAL void readblock() { off_t dpos = file_addr - sec_addr; if (sec_addr < 0 || dpos < 0 || (dpos + sizeof (buffer)) > sizeof (sector)) { sec_addr = file_addr & ~2047; #ifdef USE_SCG readsecs(sec_addr/2048, sector, ISO_BLOCKS(sizeof (sector))); #else lseek(fileno(infile), sec_addr, SEEK_SET); read(fileno(infile), sector, sizeof (sector)); #endif dpos = file_addr - sec_addr; } movebytes(§or[dpos], buffer, sizeof (buffer)); } LOCAL void showblock(flag) int flag; { unsigned int k; int i; int j; readblock(); if (flag) { for (i = 0; i < 16; i++) { crsr2(i+3, 1); if (sizeof (file_addr) > sizeof (long)) { printf("%16.16llx ", (Llong)file_addr+(i<<4)); } else { printf("%8.8lx ", (long)file_addr+(i<<4)); } for (j = 15; j >= 0; j--) { printf("%2.2x", buffer[(i<<4)+j]); if (!(j & 0x3)) printf(" "); } for (j = 0; j < 16; j++) { k = buffer[(i << 4) + j]; if (k >= ' ' && k < 0x80) printf("%c", k); else printf("."); } } } crsr2(20, 1); if (sizeof (file_addr) > sizeof (long)) { printf(" Zone, zone offset: %14llx %12.12llx ", (Llong)file_addr>>11, (Llong)file_addr & 0x7ff); } else { printf(" Zone, zone offset: %6lx %4.4lx ", (long)(file_addr>>11), (long)(file_addr & 0x7ff)); } fflush(stdout); } LOCAL int getbyte() { char c1; c1 = buffer[file_addr & (PAGE-1)]; file_addr++; if ((file_addr & (PAGE-1)) == 0) showblock(0); return (c1); }