static void pselecttst(void) { char c; int rc, opt=1; fprintf(stderr, "%s: START: testing std unix pselect functionality " "should echo keystrokes until 'q' key is pressed\n", __func__); tty_cbreak(STDIN_FILENO); ioctl(STDIN_FILENO, FIONBIO, &opt); while(1) { rc=waitkey(); if (rc==1) { c=fgetc(stdin); rc = write(STDOUT_FILENO, &c, 1); assert(rc==1); rc = write(STDOUT_FILENO, "\n", 1); assert(rc==1); if (c=='q') break; } else { fprintf(stderr,"THAT's ODD waitkey returned %d", rc); break; } } tty_reset(STDIN_FILENO); fprintf(stderr, "%s: END\n", __func__); }
static void tek_graph(PLStream *pls) { if (pls->termin && (pls->graphx == TEXT_MODE)) { tty_cbreak(); pls->graphx = GRAPHICS_MODE; switch (pls->dev_minor) { case xterm: case mskermit: case vlt: printf("\033[?38h"); /* switch to tek screen */ break; case versaterm: printf("\033%%!0"); /* switch to tek4107 screen */ break; case tek4107: printf("\033%%!0"); /* set tek mode */ printf(CLEAR_VIEW); /* clear screen */ printf("\033LV0"); /* set dialog invisible */ break; } } }
static void tty_init(int fd) { if ( signal( SIGTERM, sig_catch ) == SIG_ERR ) { printf("signal(SIGTERM) error\n"); exit(1); } if ( signal( SIGQUIT, sig_catch ) == SIG_ERR ) { printf("signal(SIGQUIT) error\n"); exit(1); } atexit(tty_atexit); tty_cbreak(fd); }
int main() { atexit(tty_atexit); tty_cbreak(STDIN_FILENO); char c; //while (c != '\n') { //} while (c != '\n') { c = getchar(); if(c == 27){ getchar(); c = getchar(); if(c == 65){ putchar('\b'); putchar('\b'); putchar(' '); putchar('\b'); putchar('\b'); putchar(' '); putchar('\b'); putchar('\b'); putchar(' '); putchar('\b'); putchar('\b'); putchar(' '); putchar('\b'); putchar('\b'); putchar(' '); putchar('\b'); printf("up"); } else if(c == 66){ printf("\rdown"); } } else { printf("%d ", c); } } return 0; }
/** * Runs a command in the shell. The shell may be interactive, and CGDB * will be paused for the duration of the shell. Any leading stuff, like * 'shell ' or '!' should be removed prior to calling this function. * * \param command The command to run at the shell. Empty string or null * means to invoke an interactive shell. * * \return The exit status of the system() call. */ int run_shell_command(const char *command) { int rv; /* Cleanly scroll the screen up for a prompt */ scrl(1); move(LINES - 1, 0); printf("\n"); /* Put the terminal in cooked mode and turn on echo */ endwin(); tty_set_attributes(STDIN_FILENO, &term_attributes); /* NULL or empty string means invoke user's shell */ if (command == NULL || strlen(command) == 0) { /* Check for SHELL environment variable */ char *shell = getenv("SHELL"); if (shell == NULL) { /* Run /bin/sh instead */ rv = system("/bin/sh"); } else { rv = system(shell); } } else { /* Execute the command passed in via system() */ rv = system(command); } /* Press any key to continue... */ fprintf(stderr, "Hit ENTER to continue..."); while (fgetc(stdin) != '\n') { } /* Turn off echo and put the terminal back into raw mode */ tty_cbreak(STDIN_FILENO, &term_attributes); if_draw(); return rv; }
int main(void) { int i; char c; if (signal(SIGINT, sig_catch) == SIG_ERR) /* catch signals */ err_sys("signal(SIGINT) error"); if (signal(SIGQUIT, sig_catch) == SIG_ERR) err_sys("signal(SIGQUIT) error"); if (signal(SIGTERM, sig_catch) == SIG_ERR) err_sys("signal(SIGTERM) error"); if (tty_raw(STDIN_FILENO) < 0) err_sys("tty_raw error"); printf("Enter raw mode characters, terminate with DELETE\n"); while ((i = read(STDIN_FILENO, &c, 1)) == 1) { if ((c &= 255) == 0177) /* 0177 = ASCII DELETE */ break; printf("%o\n", c); } if (tty_reset(STDIN_FILENO) < 0) err_sys("tty_reset error"); if (i <= 0) err_sys("read error"); if (tty_cbreak(STDIN_FILENO) < 0) err_sys("tty_cbreak error"); printf("\nEnter cbreak mode characters, terminate with SIGINT\n"); while ((i = read(STDIN_FILENO, &c, 1)) == 1) { c &= 255; printf("%o\n", c); } if (tty_reset(STDIN_FILENO) < 0) err_sys("tty_reset error"); if (i <= 0) err_sys("read error"); exit(0); }
int main(int argc, char *argv[]) { atexit(tty_atexit); int i; char c; Signal(SIGINT, sig_catch); Signal(SIGQUIT, sig_catch); Signal(SIGTERM, sig_catch); if(tty_raw(STDIN_FILENO) < 0) err_sys("tty_raw error"); printf("Enter raw mode characters, terminate with DELETE\n"); while ((i = read(STDIN_FILENO, &c, 1)) == 1) { if((c &= 255) == 0177) /* 0177 = ASCII DELETE*/ break; printf("%o\n", c ); } if(tty_reset(STDIN_FILENO) < 0) err_sys("tty_reset error"); if(i <= 0) err_sys("read error"); if(tty_cbreak(STDIN_FILENO) < 0) err_sys("tty_raw error"); printf("\n Enter cbreak mode characters, terminate with SIGINT\n"); while ( (i = read(STDIN_FILENO, &c, 1)) == 1) { c &= 255; printf("%o\n", c); } tty_reset(STDIN_FILENO); if( i < 0) err_sys("read error");; return 0; }
void main(int argc, char *argv[]) { int sock,child_pid; struct hostent *host; struct sockaddr_in serv_addr; int port; char addr[50]; char hostname[50]; char buf[100]; if(argc != 2) { fprintf(stderr,"usage: %s<address>\n",argv[0]); exit(1); } strcpy(addr,argv[1]); port = ECHO_PORT; bzero((char *)&serv_addr, sizeof(serv_addr)); serv_addr.sin_family=AF_INET; serv_addr.sin_port = htons(port); /*숫자와 점으로 이루어진 주소인지 체크한다.*/ if ((serv_addr.sin_addr.s_addr=inet_addr(addr))!=INADDR_NONE) { strcpy(hostname,addr); } else {/*문자로 이루어진 주소값 이므로, 데이터베이스에서 주소를 찾는다.*/ if ((host=gethostbyname(addr))==NULL) { herror("host name error"); exit(1); } bcopy(host->h_addr,(char *)&serv_addr.sin_addr, host->h_length); strcpy(hostname, host->h_name); } /*소켓 생성*/ if((sock=socket(AF_INET,SOCK_STREAM,0))<0) { perror("socket"); exit(1); } printf("Trying %s...\n",inet_ntoa(serv_addr.sin_addr)); /*서버로 연결을 시도한다. */ if(connect(sock,(struct sockaddr*) &serv_addr, sizeof(serv_addr)) <0) { close(sock); perror("connect"); exit(1); } printf("Connected to %s.\", hostname); if((child_pid = fork()) ==0) {/*this is the child process*/ int n; while((n = read(sock, buf, 100)) !=0) write(1,buf,n); if(kill(getppid(), SIGKILL)<0) perror("kill"); } else { /*this is the parent process*/ int n; /* cbreak-mode로 만든다.*/ tty_cbreak(fileno(stdin),1); while((n = read(fileno(stdin),buf,1)) == 1) { if((n = write(sock, buf, 1)) <0) break; } if(n<0) fprintf(stderr, "error!\n"); /*terminal mode 복귀시킨다.*/ tty_cbreak(fileno(stdin),0); kill(child_pid, SIGKILL); } close(sock); printf("Connection closed.\n"); }
int main(int argc, char *argv[]) { /* Uncomment to debug and attach */ #if 0 int c; read(0, &c, 1); #endif parse_long_options(&argc, &argv); current_line = ibuf_init(); cgdbrc_init(); if (create_and_init_pair() == -1) { fprintf(stderr, "%s:%d Unable to create PTY pair", __FILE__, __LINE__); exit(-1); } /* First create tgdb, because it has the error log */ if (start_gdb(argc, argv) == -1) { fprintf(stderr, "%s:%d Unable to invoke GDB", __FILE__, __LINE__); exit(-1); } /* From here on, the logger is initialized */ /* Create the home directory */ if (init_home_dir() == -1) { logger_write_pos(logger, __FILE__, __LINE__, "Unable to create home dir ~/.cgdb"); cleanup(); exit(-1); } if (init_readline() == -1) { logger_write_pos(logger, __FILE__, __LINE__, "Unable to init readline"); cleanup(); exit(-1); } if (tty_cbreak(STDIN_FILENO, &term_attributes) == -1) { logger_write_pos(logger, __FILE__, __LINE__, "tty_cbreak error"); cleanup(); exit(-1); } if (init_kui() == -1) { logger_write_pos(logger, __FILE__, __LINE__, "init_kui error"); cleanup(); exit(-1); } /* Initialize the display */ switch (if_init()) { case 1: logger_write_pos(logger, __FILE__, __LINE__, "Unable to initialize the curses library"); cleanup(); exit(-1); case 2: logger_write_pos(logger, __FILE__, __LINE__, "Unable to handle signal: SIGWINCH"); cleanup(); exit(-1); case 3: logger_write_pos(logger, __FILE__, __LINE__, "Unable to setup highlighting groups"); cleanup(); exit(-1); case 4: logger_write_pos(logger, __FILE__, __LINE__, "New GDB window failed -- out of memory?"); cleanup(); exit(-1); } /* Initialize the pipe that is used for resize */ if (init_resize_pipe() == -1) { logger_write_pos(logger, __FILE__, __LINE__, "init_resize_pipe error"); cleanup(); exit(-1); } { char config_file[FSUTIL_PATH_MAX]; FILE *config; fs_util_get_path(cgdb_home_dir, "cgdbrc", config_file); config = fopen(config_file, "r"); if (config) { command_parse_file(config); fclose(config); } } /* Enter main loop */ main_loop(); /* Shut down curses and exit */ cleanup(); return 0; }
void loop(int sockfd) { //add temp value int bindport; int broadcast; int cbreak; int chunkwrite; int client; int connectudp; int crlf; int debug; int dofork; int dontroute; char foreignip[128]; int foreignport; int halfclose; int ignorewerr; int iptos; int ipttl; char joinip[128]; int keepalive; long linger; int listenq; char localip[128]; int maxseg; int mcastttl; int msgpeek; int nodelay; int nbuf; int onesbcast; int pauseclose; int pauseinit; int pauselisten; int pauserw; int reuseaddr; int reuseport; int readlen; int writelen; int recvdstaddr; int rcvbuflen; int sndbuflen; long rcvtimeo; long sndtimeo; char *rbuf; char *wbuf; int server; int sigio; int sourcesink; int sroute_cnt; int udp; int urgwrite; int verbose; int usewritev; struct sockaddr_in servaddr; int maxfdp1, nread, ntowrite, stdineof, clilen; fd_set rset; struct sockaddr_in cliaddr; /* for UDP server */ #ifdef MSG_TRUNC /* 4.3BSD Reno and later */ struct iovec iov[1]; struct msghdr msg; #ifdef IP_RECVDSTADDR /* 4.3BSD Reno and later */ static struct cmsghdr *cmptr = NULL; /* malloc'ed */ struct in_addr dstinaddr; /* for UDP server */ #define CONTROLLEN (sizeof(struct cmsghdr) + sizeof(struct in_addr)) #endif /* IP_RECVDSTADDR */ #endif /* MSG_TRUNC */ #ifdef notdef /* following doesn't appear to work */ /* * This is an attempt to set stdin to cbreak, so that input characters * are delivered one at a time, to see Nagle algorithm in effect * (or disabled). */ if (cbreak && isatty(STDIN_FILENO)) { if (tty_cbreak(STDIN_FILENO) < 0) err_sys("tty_cbreak error"); if (atexit(tty_atexit) < 0) err_sys("tty_atexit error"); if (signal(SIGINT, sig_catch) == SIG_ERR) err_sys("signal error"); if (signal(SIGQUIT, sig_catch) == SIG_ERR) err_sys("signal error"); if (signal(SIGTERM, sig_catch) == SIG_ERR) err_sys("signal error"); } #endif if (pauseinit) sleep(pauseinit); /* intended for server */ stdineof = 0; FD_ZERO(&rset); maxfdp1 = sockfd + 1; /* check descriptors [0..sockfd] */ /* UDP client issues connect(), so read() and write() are used. Server is harder since cannot issue connect(). We use recvfrom() or recvmsg(), depending on OS. */ for ( ; ; ) { if (stdineof == 0) FD_SET(STDIN_FILENO, &rset); FD_SET(sockfd, &rset); if (select(maxfdp1, &rset, NULL, NULL, NULL) < 0) err_sys("select error"); if (FD_ISSET(STDIN_FILENO, &rset)) { /* data to read on stdin */ if ( (nread = read(STDIN_FILENO, rbuf, readlen)) < 0) err_sys("read error from stdin"); else if (nread == 0) { /* EOF on stdin */ if (halfclose) { if (shutdown(sockfd, 1) < 0) err_sys("shutdown() error"); FD_CLR(STDIN_FILENO, &rset); stdineof = 1; /* don't read stdin anymore */ continue; /* back to select() */ } break; /* default: stdin EOF -> done */ } if (crlf) { ntowrite = crlf_add(wbuf, writelen, rbuf, nread); if (write(sockfd, wbuf, ntowrite) != ntowrite) err_sys("write error"); } else { if (write(sockfd, rbuf, nread) != nread) err_sys("write error"); } } if (FD_ISSET(sockfd, &rset)) { /* data to read from socket */ if (udp && server) { clilen = sizeof(cliaddr); #ifndef MSG_TRUNC /* vanilla BSD sockets */ nread = recvfrom(sockfd, rbuf, readlen, 0, (struct sockaddr *) &cliaddr, &clilen); #else /* 4.3BSD Reno and later; use recvmsg() to get at MSG_TRUNC flag */ /* Also lets us get at control information (destination address) */ iov[0].iov_base = rbuf; iov[0].iov_len = readlen; msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = (caddr_t) &cliaddr; msg.msg_namelen = clilen; #ifdef IP_RECVDSTADDR if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL) err_sys("malloc error for control buffer"); msg.msg_control = (caddr_t) cmptr; /* for dest address */ msg.msg_controllen = CONTROLLEN; #else msg.msg_control = (caddr_t) 0; /* no ancillary data */ msg.msg_controllen = 0; #endif /* IP_RECVDSTADDR */ msg.msg_flags = 0; /* flags returned here */ nread = recvmsg(sockfd, &msg, 0); #endif /* MSG_TRUNC */ if (nread < 0) err_sys("datagram receive error"); if (verbose) { printf("from %s", INET_NTOA(cliaddr.sin_addr)); #ifdef MSG_TRUNC #ifdef IP_RECVDSTADDR if (recvdstaddr) { if (cmptr->cmsg_level != IPPROTO_IP) err_quit("control level != IPPROTO_IP"); if (cmptr->cmsg_type != IP_RECVDSTADDR) err_quit("control type != IP_RECVDSTADDR"); if (cmptr->cmsg_len != CONTROLLEN) err_quit("control length (%d) != %d", cmptr->cmsg_len, CONTROLLEN); memcpy((char *) &dstinaddr, (char *) CMSG_DATA(cmptr), sizeof(struct in_addr)); printf(", to %s", INET_NTOA(dstinaddr)); } #endif /* IP_RECVDSTADDR */ #endif /* MSG_TRUNC */ printf(": "); fflush(stdout); } #ifdef MSG_TRUNC if (msg.msg_flags & MSG_TRUNC) printf("(datagram truncated)\n"); #endif } else { if ( (nread = read(sockfd, rbuf, readlen)) < 0) err_sys("read error"); else if (nread == 0) { if (verbose) fprintf(stderr, "connection closed by peer\n"); break; /* EOF, terminate */ } } if (crlf) { ntowrite = crlf_strip(wbuf, writelen, rbuf, nread); if (writen(STDOUT_FILENO, wbuf, ntowrite) != ntowrite) err_sys("writen error to stdout"); } else { if (writen(STDOUT_FILENO, rbuf, nread) != nread) err_sys("writen error to stdout"); } } } if (pauseclose) { if (verbose) fprintf(stderr, "pausing before close\n"); sleep(pauseclose); } if (close(sockfd) < 0) err_sys("close error"); /* since SO_LINGER may be set */ }
int main(int argc, char **argv) { int gdb_fd, child_fd, slavefd, masterfd; #if 0 int c; read(0, &c, 1); #endif if (tty_cbreak(STDIN_FILENO, &term_attributes) == -1) logger_write_pos(logger, __FILE__, __LINE__, "tty_cbreak error"); pty_pair = pty_pair_create(); if (!pty_pair) { fprintf(stderr, "%s:%d Unable to create PTY pair", __FILE__, __LINE__); exit(-1); } slavefd = pty_pair_get_slavefd(pty_pair); if (slavefd == -1) { fprintf(stderr, "%s:%d Unable to get slavefd", __FILE__, __LINE__); exit(-1); } masterfd = pty_pair_get_masterfd(pty_pair); if (masterfd == -1) { fprintf(stderr, "%s:%d Unable to get masterfd", __FILE__, __LINE__); exit(-1); } if (tty_off_xon_xoff(masterfd) == -1) exit(-1); rline = rline_initialize(slavefd, rlctx_send_user_command, tab_completion, getenv("TERM")); if ((tgdb = tgdb_initialize(NULL, argc - 1, argv + 1, &gdb_fd, &child_fd)) == NULL) { logger_write_pos(logger, __FILE__, __LINE__, "tgdb_start error"); goto driver_end; } if (tgdb_set_verbose_error_handling(tgdb, 1) != 1) { logger_write_pos(logger, __FILE__, __LINE__, "driver error"); goto driver_end; } /* Ask TGDB to print error messages */ if (tgdb_set_verbose_gui_command_output(tgdb, 1) != 1) { logger_write_pos(logger, __FILE__, __LINE__, "driver error"); goto driver_end; } set_up_signal(); main_loop(gdb_fd, child_fd); if (tgdb_shutdown(tgdb) == -1) logger_write_pos(logger, __FILE__, __LINE__, "could not shutdown"); driver_end: if (tty_set_attributes(STDIN_FILENO, &term_attributes) == -1) logger_write_pos(logger, __FILE__, __LINE__, "tty_reset error"); return 0; }
int main() { int i; int c; struct sigaction sigact; sigact.sa_handler = sig_catch; sigact.sa_flags = 0; sigemptyset(&sigact.sa_mask); if (-1 == sigaction(SIGINT, &sigact, NULL)) { perror("sigaction"); return -1; } if (-1 == sigaction(SIGQUIT, &sigact, NULL)) { perror("sigaction"); return -1; } if (-1 == sigaction(SIGTERM, &sigact, NULL)) { perror("sigaction"); return -1; } if (0 > tty_raw(STDIN_FILENO)) { perror("tty_raw"); return -1; } printf("Enter raw mode characters, terminate with DELETE\n"); while (1 == (i = read(STDIN_FILENO, &c, 1))) { if ((c &= 255) == 0177) { break; } printf("%o\n", c); } if (0 > tty_reset(STDIN_FILENO)) { perror("tty_reset"); return -1; } if (i <= 0) { fprintf(stderr, "read error\n"); return -1; } if (0 > tty_cbreak(STDIN_FILENO)) { perror("tty_cbreak"); return -1; } printf("\nEnter cbreak mode characters, terminate with SIGINT\n"); while (1 == (i = read(STDIN_FILENO, &c, 1))) { c &= 255; printf("%o\n", c); } if (0 > tty_reset(STDIN_FILENO)) { perror("tty_reset"); return -1; } if (0 >= i) { fprintf(stderr, "read error\n"); } exit(0); }