void *listening(void *) { char buffer[1]; box(chat, '*', '*'); wrefresh(chat); while(true) { bzero(buffer, 1); std::string str; str.clear(); while(true) { int n = read(sockMain, buffer, 1); if(buffer[0] == '\0') break; if(n <= 0) break; str += buffer[0]; } if(!str.empty()) { if(str[0] == '&') { wclear(users); mvwaddstr(users, 1, 1, "Users:\n"); str.erase(str.begin()); mvwaddstr(users, 2, 1, str.c_str()); box(input, '*', '*'); wrefresh(users); wrefresh(chat); } else { if(str.size() > 50) { str.insert(49, "\n*"); } mvwaddstr(chat, car, 1, str.c_str()); wrefresh(chat); if(str.size() > 50 && car + 1 != 16) { car++; } if(car + 1 == 16) { scrollok(chat, true); wsetscrreg(chat, 2, 16); idlok(chat, true); wscrl(chat, 1); box(chat, '*', '*'); box(users, '*', '*'); wrefresh(users); wrefresh(chat); } else if(car + 1 == 16 && str.size() > 50) { scrollok(chat, true); wsetscrreg(chat, 2, 16); idlok(chat, true); wscrl(chat, 3); box(chat, '*', '*'); wrefresh(chat); } else car++; } } else if(str.empty()) { break; } str.clear(); bzero(buffer, 1); } close(sockMain); pthread_exit(NULL); }
int main (int argc, char** argv) { const char* default_port = "23"; struct sigaction sa; int i; /* process command line args */ for (i = 1; i < argc; ++i) { /* help */ if (strcmp(argv[i], "-h") == 0) { printf( "CLC %s by Sean Middleditch <*****@*****.**>\n" "This program has been released into the PUBLIC DOMAIN.\n\n" "Usage:\n" " clc [-h] <host> [<port>]\n\n" "Options:\n" " -h display help\n", CLC_VERSION ); return 0; } /* other unknown option */ if (argv[i][0] == '-') { fprintf(stderr, "Unknown option %s.\nUse -h to see available options.\n", argv[i]); exit(1); } /* if host is unset, this is the host */ if (host == NULL) { host = argv[i]; /* otherwise, it's a port */ } else { port = argv[i]; } } /* ensure we have a host */ if (host == NULL) { fprintf(stderr, "No host was given.\nUse -h to see command format.\n"); exit(1); } /* set default port if none was given */ if (port == NULL) port = default_port; /* cleanup on any failure */ atexit(cleanup); /* set terminal defaults */ memset(&terminal, 0, sizeof(struct TERMINAL)); terminal.state = TERM_ASCII; terminal.flags = TERM_FLAGS_DEFAULT; terminal.color = TERM_COLOR_DEFAULT; /* initial telnet handler */ telnet = telnet_init(telnet_telopts, telnet_event, 0, 0); /* connect to server */ sock = do_connect(host, port); if (sock == -1) { fprintf(stderr, "Failed to connect to %s:%s\n", host, port); exit(1); } printf("Connected to %s:%s\n", host, port); /* set initial banner */ snprintf(banner, sizeof(banner), "CLC - %s:%s (connected)", host, port); /* configure curses */ initscr(); start_color(); nonl(); cbreak(); noecho(); win_main = newwin(LINES-2, COLS, 0, 0); win_banner = newwin(1, COLS, LINES-2, 0); win_input = newwin(1, COLS, LINES-1, 0); idlok(win_main, TRUE); scrollok(win_main, TRUE); nodelay(win_input, FALSE); keypad(win_input, TRUE); use_default_colors(); init_pair(COLOR_RED, COLOR_RED, -1); init_pair(COLOR_BLUE, COLOR_BLUE, -1); init_pair(COLOR_GREEN, COLOR_GREEN, -1); init_pair(COLOR_CYAN, COLOR_CYAN, -1); init_pair(COLOR_MAGENTA, COLOR_MAGENTA, -1); init_pair(COLOR_YELLOW, COLOR_YELLOW, -1); init_pair(COLOR_WHITE, COLOR_WHITE, -1); init_pair(TERM_COLOR_DEFAULT, -1, -1); wbkgd(win_main, COLOR_PAIR(TERM_COLOR_DEFAULT)); wclear(win_main); init_pair(10, COLOR_WHITE, COLOR_BLUE); wbkgd(win_banner, COLOR_PAIR(10)); wclear(win_banner); init_pair(11, -1, -1); wbkgd(win_input, COLOR_PAIR(11)); wclear(win_input); redraw_display(); /* set signal handlers */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = handle_signal; sigaction(SIGINT, &sa, NULL); sigaction(SIGWINCH, &sa, NULL); /* initial edit buffer */ memset(&editbuf, 0, sizeof(struct EDITBUF)); /* setup poll info */ struct pollfd fds[2]; fds[0].fd = 1; fds[0].events = POLLIN; fds[1].fd = sock; fds[1].events = POLLIN; /* main loop */ while (running) { /* poll sockets */ if (poll(fds, 2, -1) == -1) { if (errno != EAGAIN && errno != EINTR) { endwin(); fprintf(stderr, "poll() failed: %s\n", strerror(errno)); return 1; } } /* resize event? */ if (have_sigwinch) { have_sigwinch = 0; redraw_display(); } /* escape? */ if (have_sigint) { exit(0); } /* input? */ if (fds[0].revents & POLLIN) { int key = wgetch(win_input); if (key != ERR) on_key(key); } /* process input data */ if (fds[1].revents & POLLIN) { char buffer[2048]; int ret = recv(sock, buffer, sizeof(buffer), 0); if (ret == -1) { if (errno != EAGAIN && errno != EINTR) { endwin(); fprintf(stderr, "recv() failed: %s\n", strerror(errno)); return 1; } } else if (ret == 0) { running = 0; } else { recv_bytes += ret; telnet_recv(telnet, buffer, ret); } } /* flush output */ paint_banner(); wnoutrefresh(win_main); wnoutrefresh(win_banner); wnoutrefresh(win_input); doupdate(); } /* final display, pause */ sock = -1; autobanner = 1; paint_banner(); wnoutrefresh(win_banner); doupdate(); wgetch(win_input); /* clean up */ endwin(); printf("Disconnected.\n"); /* free memory (so Valgrind leak detection is useful) */ telnet_free(telnet); return 0; }
int main(int argc, char *argv[]) { int MAXLINES = 1000; FILE *fp; char buf[BUFSIZ]; int i; char **olptr; int done = FALSE; int length = 0; #if CAN_RESIZE bool use_resize = TRUE; #endif while ((i = getopt(argc, argv, "n:rtT:")) != EOF) { switch (i) { case 'n': if ((MAXLINES = atoi(optarg)) < 1) usage(); break; #if CAN_RESIZE case 'r': use_resize = FALSE; break; #endif #ifdef TRACE case 'T': trace(atoi(optarg)); break; case 't': trace(TRACE_CALLS); break; #endif default: usage(); } } if (optind + 1 != argc) usage(); if ((lines = (char **)calloc(MAXLINES+2, sizeof(*lines))) == 0) usage(); fname = argv[optind]; if ((fp = fopen(fname, "r")) == 0) { perror(fname); return EXIT_FAILURE; } (void) signal(SIGINT, finish); /* arrange interrupts to terminate */ #if CAN_RESIZE if (use_resize) (void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */ #endif /* slurp the file */ for (lptr = &lines[0]; (lptr - lines) < MAXLINES; lptr++) { char temp[BUFSIZ], *s, *d; int col; if (fgets(buf, sizeof(buf), fp) == 0) break; /* convert tabs so that shift will work properly */ for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) { if (*d == '\n') { *d = '\0'; break; } else if (*d == '\t') { col = (col | 7) + 1; while ((d-temp) != col) *d++ = ' '; } else if (isprint(*d)) { col++; d++; } else { sprintf(d, "\\%03o", *s & 0xff); d += strlen(d); col = (d - temp); } } *lptr = strdup(temp); } (void) fclose(fp); length = lptr - lines; (void) initscr(); /* initialize the curses library */ keypad(stdscr, TRUE); /* enable keyboard mapping */ (void) nonl(); /* tell curses not to do NL->CR/NL on output */ (void) cbreak(); /* take input chars one at a time, no wait for \n */ (void) noecho(); /* don't echo input */ idlok(stdscr, TRUE); /* allow use of insert/delete line */ lptr = lines; while (!done) { int n, c; bool got_number; show_all(); got_number = FALSE; n = 0; for (;;) { #if CAN_RESIZE if (interrupted) adjust(0); #endif waiting = TRUE; c = getch(); waiting = FALSE; if ((c < 127) && isdigit(c)) { if (!got_number) { mvprintw(0,0, "Count: "); clrtoeol(); } addch(c); n = 10 * n + (c - '0'); got_number = TRUE; } else break; } if (!got_number && n == 0) n = 1; switch(c) { case KEY_DOWN: case 'n': olptr = lptr; for (i = 0; i < n; i++) if ((lptr - lines) < (length - LINES + 1)) lptr++; else break; wscrl(stdscr, lptr - olptr); break; case KEY_UP: case 'p': olptr = lptr; for (i = 0; i < n; i++) if (lptr > lines) lptr--; else break; wscrl(stdscr, lptr - olptr); break; case 'h': case KEY_HOME: lptr = lines; break; case 'e': case KEY_END: if (length > LINES) lptr = lines + length - LINES + 1; else lptr = lines; break; case 'r': case KEY_RIGHT: shift++; break; case 'l': case KEY_LEFT: if (shift) shift--; else beep(); break; case 'q': done = TRUE; break; #ifdef KEY_RESIZE case KEY_RESIZE: /* ignore this; ncurses will repaint */ break; #endif #if CAN_RESIZE case ERR: break; #endif default: beep(); } } finish(0); /* we're done */ }
/* * cl_vi_init -- * Initialize the curses vi screen. */ static int cl_vi_init(SCR *sp) { CL_PRIVATE *clp; char *o_cols, *o_lines, *o_term; const char *ttype; clp = CLP(sp); /* If already initialized, just set the terminal modes. */ if (F_ISSET(clp, CL_SCR_VI_INIT)) goto fast; /* Curses vi always reads from (and writes to) a terminal. */ if (!F_ISSET(clp, CL_STDIN_TTY) || !isatty(STDOUT_FILENO)) { msgq(sp, M_ERR, "016|Vi's standard input and output must be a terminal"); return (1); } /* We'll need a terminal type. */ if (opts_empty(sp, O_TERM, 0)) return (1); ttype = O_STR(sp, O_TERM); /* * XXX * Changing the row/column and terminal values is done by putting them * into the environment, which is then read by curses. What this loses * in ugliness, it makes up for in stupidity. We can't simply put the * values into the environment ourselves, because in the presence of a * kernel mechanism for returning the window size, entering values into * the environment will screw up future screen resizing events, e.g. if * the user enters a :shell command and then resizes their window. So, * if they weren't already in the environment, we make sure to delete * them immediately after setting them. * * XXX * Putting the TERM variable into the environment is necessary, even * though we're using newterm() here. We may be using initscr() as * the underlying function. */ o_term = getenv("TERM"); cl_putenv(sp, "TERM", ttype, 0); o_lines = getenv("LINES"); cl_putenv(sp, "LINES", NULL, (u_long)O_VAL(sp, O_LINES)); o_cols = getenv("COLUMNS"); cl_putenv(sp, "COLUMNS", NULL, (u_long)O_VAL(sp, O_COLUMNS)); /* Delete cur_term if exists. */ if (F_ISSET(clp, CL_SETUPTERM)) { if (del_curterm(cur_term)) return (1); F_CLR(clp, CL_SETUPTERM); } /* * XXX * The SunOS initscr() can't be called twice. Don't even think about * using it. It fails in subtle ways (e.g. select(2) on fileno(stdin) * stops working). (The SVID notes that applications should only call * initscr() once.) * * XXX * The HP/UX newterm doesn't support the NULL first argument, so we * have to specify the terminal type. */ errno = 0; if ((clp->screen = newterm(__UNCONST(ttype), stdout, stdin)) == NULL) { if (errno) msgq(sp, M_SYSERR, "%s", ttype); else msgq(sp, M_ERR, "%s: unknown terminal type", ttype); return (1); } if (o_term == NULL) cl_unsetenv(sp, "TERM"); if (o_lines == NULL) cl_unsetenv(sp, "LINES"); if (o_cols == NULL) cl_unsetenv(sp, "COLUMNS"); /* * XXX * Someone got let out alone without adult supervision -- the SunOS * newterm resets the signal handlers. There's a race, but it's not * worth closing. */ (void)sig_init(sp->gp, sp); /* * We use raw mode. What we want is 8-bit clean, however, signals * and flow control should continue to work. Admittedly, it sounds * like cbreak, but it isn't. Using cbreak() can get you additional * things like IEXTEN, which turns on flags like DISCARD and LNEXT. * * !!! * If raw isn't turning off echo and newlines, something's wrong. * However, it shouldn't hurt. */ noecho(); /* No character echo. */ nonl(); /* No CR/NL translation. */ raw(); /* 8-bit clean. */ idlok(stdscr, 1); /* Use hardware insert/delete line. */ /* Put the cursor keys into application mode. */ (void)keypad(stdscr, TRUE); /* * XXX * The screen TI sequence just got sent. See the comment in * cl_funcs.c:cl_attr(). */ clp->ti_te = TI_SENT; /* * XXX * Historic implementations of curses handled SIGTSTP signals * in one of three ways. They either: * * 1: Set their own handler, regardless. * 2: Did not set a handler if a handler was already installed. * 3: Set their own handler, but then called any previously set * handler after completing their own cleanup. * * We don't try and figure out which behavior is in place, we force * it to SIG_DFL after initializing the curses interface, which means * that curses isn't going to take the signal. Since curses isn't * reentrant (i.e., the whole curses SIGTSTP interface is a fantasy), * we're doing The Right Thing. */ (void)signal(SIGTSTP, SIG_DFL); /* * If flow control was on, turn it back on. Turn signals on. ISIG * turns on VINTR, VQUIT, VDSUSP and VSUSP. The main curses code * already installed a handler for VINTR. We're going to disable the * other three. * * XXX * We want to use ^Y as a vi scrolling command. If the user has the * DSUSP character set to ^Y (common practice) clean it up. As it's * equally possible that the user has VDSUSP set to 'a', we disable * it regardless. It doesn't make much sense to suspend vi at read, * so I don't think anyone will care. Alternatively, we could look * it up in the table of legal command characters and turn it off if * it matches one. VDSUSP wasn't in POSIX 1003.1-1990, so we test for * it. * * XXX * We don't check to see if the user had signals enabled originally. * If they didn't, it's unclear what we're supposed to do here, but * it's also pretty unlikely. */ if (tcgetattr(STDIN_FILENO, &clp->vi_enter)) { msgq(sp, M_SYSERR, "tcgetattr"); goto err; } if (clp->orig.c_iflag & IXON) clp->vi_enter.c_iflag |= IXON; if (clp->orig.c_iflag & IXOFF) clp->vi_enter.c_iflag |= IXOFF; clp->vi_enter.c_lflag |= ISIG; #ifdef VDSUSP clp->vi_enter.c_cc[VDSUSP] = _POSIX_VDISABLE; #endif clp->vi_enter.c_cc[VQUIT] = _POSIX_VDISABLE; clp->vi_enter.c_cc[VSUSP] = _POSIX_VDISABLE; /* * XXX * OSF/1 doesn't turn off the <discard>, <literal-next> or <status> * characters when curses switches into raw mode. It should be OK * to do it explicitly for everyone. */ #ifdef VDISCARD clp->vi_enter.c_cc[VDISCARD] = _POSIX_VDISABLE; #endif #ifdef VLNEXT clp->vi_enter.c_cc[VLNEXT] = _POSIX_VDISABLE; #endif #ifdef VSTATUS clp->vi_enter.c_cc[VSTATUS] = _POSIX_VDISABLE; #endif /* Initialize terminal based information. */ if (cl_term_init(sp)) goto err; fast: /* Set the terminal modes. */ if (tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &clp->vi_enter)) { msgq(sp, M_SYSERR, "tcsetattr"); err: (void)cl_vi_end(sp->gp); return (1); } return (0); }
static void create_curses_window(MAIN_WINDOW_REC *window) { window->curses_win = newwin(window->lines, COLS, window->first_line, 0); idlok(window->curses_win, 1); }
void initialise_curses(void) { #ifdef BUILDWIDE setlocale(LC_CTYPE, ""); #endif /* maybe this will fix the resize problem */ unsetenv("LINES"); unsetenv("COLUMNS"); initscr(); #ifdef LEAVE_SCROLLBACK #define isprivate(s) ((s) != 0 && strstr(s, "\033[?") != 0) if (isprivate(enter_ca_mode) && isprivate(exit_ca_mode)) { (void) putp(exit_ca_mode); (void) putp(clear_screen); enter_ca_mode = 0; exit_ca_mode = 0; } #endif if(has_colors() == TRUE) { if(start_color() != ERR) { use_default_colors(); } else { debug(DEBUGLEVEL_WARN, "CmdStartup start_color failed\n"); } } else { debug(DEBUGLEVEL_WARN, "CmdStartup has_colors false\n"); } noraw(); cbreak(); noecho(); scrollok(stdscr, TRUE); idlok(stdscr, TRUE); keypad(stdscr, TRUE); intrflush(stdscr, FALSE); // nodelay(stdscr, TRUE); // halfdelay(10); timeout(50); def_prog_mode(); // debug("CmdStartup color (%ld %ld)\n", stdscr->_attrs, stdscr->_bkgd); /* for(iColourNum = 0; iColourNum <= 9; iColourNum++) { iF = -1; iB = -1; pair_content(iColourNum, &iF, &iB); debug("CmdStartup colour %d: %d %d\n", iColourNum, iF, iB); } */ // pair_content(1, &iForeground, &iBackground); CmdBackground(0); }
cell pp_curs_idlok(cell x) { if (!Running) return UNSPECIFIC; idlok(stdscr, car(x) == S9_TRUE? TRUE: FALSE); return UNSPECIFIC; }
void startdebugger() { static int firsttime = 1; curblank = 0x40; debuggeron = 1; #ifdef __MSDOS__ __dpmi_regs regs; regs.x.ax = 0x0003; __dpmi_int(0x10, ®s); #endif // __MSDOS__ if (firsttime) { initscr(); cbreak(); noecho(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE); /* set up colors */ start_color(); init_pair(cp_white, COLOR_WHITE, COLOR_BLACK); init_pair(cp_magenta, COLOR_MAGENTA, COLOR_BLACK); init_pair(cp_red, COLOR_RED, COLOR_BLACK); init_pair(cp_cyan, COLOR_CYAN, COLOR_BLACK); init_pair(cp_green, COLOR_GREEN, COLOR_BLACK); init_pair(cp_yellow, COLOR_YELLOW, COLOR_BLACK); init_pair(cp_white_on_blue, COLOR_WHITE, COLOR_BLUE); } execut = 0; if (firsttime) { startdisplay(); debugwin = newwin(20, 77, 2, 1); wbkgd(debugwin, CP(cp_white_on_blue) | ' '); // wattrset(debugwin, CP(cp_white_on_blue)); scrollok(debugwin, TRUE); idlok(debugwin, TRUE); firsttime = 0; } else { touchwin(stdscr); touchwin(debugwin); refresh(); wrefresh(debugwin); } debugloop(); cleardisplay(); // "pushad / call LastLog / ... / popad" elided SaveOAMRamLog(); if (execut == 1) { start65816(); return; } endprog(); return; }
/* * main: * The main program, of course */ int main(int argc, char **argv) { char *env; time_t lowtime; md_init(); #ifdef MASTER /* * Check to see if he is a wizard */ if (argc >= 2 && argv[1][0] == '\0') if (strcmp(PASSWD, md_crypt(md_getpass("wizard's password: "******"mT")) == 0) { wizard = TRUE; player.t_flags |= SEEMONST; argv++; argc--; } #endif /* * get home and options from environment */ strcpy(home, md_gethomedir()); if (strlen(home) > MAXSTR - strlen("rogue.save") - 1) *home = 0; strcpy(file_name, home); strcat(file_name, "rogue.save"); if ((env = getenv("ROGUEOPTS")) != NULL) parse_opts(env); if (env == NULL || whoami[0] == '\0') strucpy(whoami, md_getusername(), strlen(md_getusername())); lowtime = time(NULL); if (getenv("SEED") != NULL) { dnum = atoi(getenv("SEED")); noscore = 1; } else dnum = (unsigned int) lowtime + md_getpid(); seed = dnum; open_score(); /* * Drop setuid/setgid after opening the scoreboard file. */ md_normaluser(); /* * check for print-score option */ md_normaluser(); /* we drop any setgid/setuid priveldges here */ if (argc == 2) { if (strcmp(argv[1], "-s") == 0) { noscore = TRUE; score(0, -1, 0); exit(0); } else if (strcmp(argv[1], "-d") == 0) { dnum = rnd(100); /* throw away some rnd()s to break patterns */ while (--dnum) rnd(100); purse = rnd(100) + 1; level = rnd(100) + 1; initscr(); getltchars(); death(death_monst()); exit(0); } } init_check(); /* check for legal startup */ if (argc == 2) if (!restore(argv[1])) /* Note: restore will never return */ my_exit(1); #ifdef MASTER if (wizard) printf("Hello %s, welcome to dungeon #%d", whoami, dnum); else #endif printf("Hello %s, just a moment while I dig the dungeon...", whoami); fflush(stdout); initscr(); /* Start up cursor package */ init_probs(); /* Set up prob tables for objects */ init_player(); /* Set up initial player stats */ init_names(); /* Set up names of scrolls */ init_colors(); /* Set up colors of potions */ init_stones(); /* Set up stone settings of rings */ init_materials(); /* Set up materials of wands */ setup(); /* * The screen must be at least NUMLINES x NUMCOLS */ if (LINES < NUMLINES || COLS < NUMCOLS) { printf("\nSorry, the screen must be at least %dx%d\n", NUMLINES, NUMCOLS); endwin(); my_exit(1); } /* * Set up windows */ hw = newwin(LINES, COLS, 0, 0); idlok(stdscr, TRUE); idlok(hw, TRUE); #ifdef MASTER noscore = wizard; #endif new_level(); /* Draw current level */ /* * Start up daemons and fuses */ start_daemon(runners, 0, AFTER); start_daemon(doctor, 0, AFTER); fuse(swander, 0, WANDERTIME, AFTER); start_daemon(stomach, 0, AFTER); playit(); return(0); }
int main(int ac, char *av[]) { bool restore; /* Revoke setgid privileges */ setgid(getgid()); if (strcmp(av[0], "a.out") == 0) { outf = fopen("q", "w"); setbuf(outf, NULL); Debug = TRUE; } restore = FALSE; switch (ac) { case 2: rest_f(av[1]); restore = TRUE; case 1: break; default: usage(); /* NOTREACHED */ } Play = PLAYER; initscr(); delwin(stdscr); stdscr = Board = newwin(BOARD_Y, BOARD_X, 0, 0); Score = newwin(SCORE_Y, SCORE_X, 0, 40); Miles = newwin(MILES_Y, MILES_X, 17, 0); #ifdef attron idlok(Board, TRUE); idlok(Score, TRUE); idlok(Miles, TRUE); #endif leaveok(Score, TRUE); leaveok(Miles, TRUE); clearok(curscr, TRUE); srandomdev(); cbreak(); noecho(); signal(SIGINT, rub); for (;;) { if (!restore || (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)) { if (Player[COMP].total < Player[PLAYER].total) Player[PLAYER].games++; else if (Player[COMP].total > Player[PLAYER].total) Player[COMP].games++; Player[COMP].total = 0; Player[PLAYER].total = 0; } do { if (!restore) Handstart = Play = other(Handstart); if (!restore || On_exit) { shuffle(); init(); } newboard(); if (restore) mvwaddstr(Score, ERR_Y, ERR_X, Initstr); prboard(); do { domove(); if (Finished) newscore(); prboard(); } while (!Finished); check_more(); restore = On_exit = FALSE; } while (Player[COMP].total < 5000 && Player[PLAYER].total < 5000); } }
/* ================== CON_Init Initialize the console in curses mode, fall back to tty mode on failure ================== */ void CON_Init(void) { int col; #ifndef _WIN32 // If the process is backgrounded (running non interactively) // then SIGTTIN or SIGTOU is emitted, if not caught, turns into a SIGSTP signal(SIGTTIN, SIG_IGN); signal(SIGTTOU, SIG_IGN); #endif // Make sure we're on a tty if (isatty(STDIN_FILENO) != 1 || isatty(STDOUT_FILENO) != 1 || isatty(STDERR_FILENO) != 1) { CON_Init_tty(); return; } // Initialize curses and set up the root window if (!curses_on) { SCREEN *test = newterm(NULL, stdout, stdin); if (!test) { CON_Init_tty(); CON_Print_tty("Couldn't initialize curses, falling back to tty\n"); return; } endwin(); delscreen(test); initscr(); cbreak(); noecho(); nonl(); intrflush(stdscr, FALSE); nodelay(stdscr, TRUE); keypad(stdscr, TRUE); wnoutrefresh(stdscr); // Set up colors if (has_colors()) { use_default_colors(); start_color(); init_pair(1, COLOR_BLACK, -1); init_pair(2, COLOR_RED, -1); init_pair(3, COLOR_GREEN, -1); init_pair(4, COLOR_YELLOW, -1); init_pair(5, COLOR_BLUE, -1); init_pair(6, COLOR_CYAN, -1); init_pair(7, COLOR_MAGENTA, -1); init_pair(8, -1, -1); } } // Create the border borderwin = newwin(LOG_LINES + 2, LOG_COLS + 2, 1, 0); CON_SetColor(borderwin, 2); box(borderwin, 0, 0); wnoutrefresh(borderwin); // Create the log window logwin = newpad(MAX_LOG_LINES, LOG_COLS); scrollok(logwin, TRUE); idlok(logwin, TRUE); if (curses_on) CON_ColorPrint(logwin, logbuf, qtrue); getyx(logwin, lastline, col); if (col) lastline++; scrollline = lastline - LOG_LINES; if (scrollline < 0) scrollline = 0; pnoutrefresh(logwin, scrollline, 0, 2, 1, LOG_LINES + 1, LOG_COLS + 1); // Create the scroll bar scrollwin = newwin(LOG_LINES, 1, 2, COLS - 1); CON_DrawScrollBar(); CON_SetColor(stdscr, 3); mvaddch(1, COLS - 1, SCRLBAR_UP); mvaddch(LINES - 2, COLS - 1, SCRLBAR_DOWN); // Create the input field inputwin = newwin(1, COLS - Q_PrintStrlen(PROMPT) - 8, LINES - 1, Q_PrintStrlen(PROMPT) + 8); input_field.widthInChars = COLS - Q_PrintStrlen(PROMPT) - 9; if (curses_on) { if (input_field.cursor < input_field.scroll) input_field.scroll = input_field.cursor; else if (input_field.cursor >= input_field.scroll + input_field.widthInChars) input_field.scroll = input_field.cursor - input_field.widthInChars + 1; CON_ColorPrint(inputwin, input_field.buffer + input_field.scroll, qfalse); } CON_UpdateCursor(); wnoutrefresh(inputwin); // Create the clock clockwin = newwin(1, 8, LINES - 1, 0); CON_UpdateClock(); // Display the title and input prompt move(0, (COLS - Q_PrintStrlen(TITLE)) / 2); CON_ColorPrint(stdscr, TITLE, qtrue); move(LINES - 1, 8); CON_ColorPrint(stdscr, PROMPT, qtrue); wnoutrefresh(stdscr); doupdate(); #ifndef _WIN32 // Catch window resizes signal(SIGWINCH, (void *)CON_Resize); #endif curses_on = qtrue; }
void initializecurses() /* This function draws the windows of the monitoring tool using ncurses */ { int i; initscr(); cbreak(); noecho(); nonl(); X1 = 0; X2 = COLS-1; assocY1 = 0; assocY2 = (int)((LINES-14)/2)-1; pathY1 = (int)((LINES-14)/2); pathY2 = LINES-14; statusY1 = pathY2+1; statusY2 = statusY1+5; textY1 = statusY2+1; textY2 = textY1+2; helpY1 = textY2+1; helpY2 = helpY1+3; assocWin = newwin((assocY2-assocY1),X2,assocY1+1,0); pathWin = newwin((pathY2-pathY1-1),X2,pathY1+2,0); statusWin = newwin(5,X2,statusY1+1,0); textWin = newwin(2,X2,textY1+1,0); receivedWin = newwin(3,X2,helpY1+1,0); assocWinHeader = newwin(1,X2,0,0); pathWinHeader = newwin(2,X2,pathY1,0); statusWinHeader = newwin(1,X2,statusY1,0); textWinHeader = newwin(1,X2,textY1,0); receivedWinHeader = newwin(1,X2,helpY1,0); waddstr(assocWinHeader,"Association Status"); for (i=18; i<COLS-1; i++) mvwaddch(assocWinHeader,0,i,'*'); waddstr(pathWinHeader,"Path Status"); for (i=11; i<COLS-1; i++) mvwaddch(pathWinHeader,0,i,'*'); mvwaddstr(pathWinHeader,1,0,"[Select numbers 0-9 to display path details for the path with the corresponding Path ID]"); waddstr(statusWinHeader,"SCTP Events Status"); for (i=18; i<COLS-1; i++) mvwaddch(statusWinHeader,0,i,'*'); waddstr(textWinHeader,"Enter text here"); for (i=15; i<COLS-1; i++) mvwaddch(textWinHeader,0,i,'*'); waddstr(receivedWinHeader,"Data(Text) received"); for (i=19; i<COLS-1; i++) mvwaddch(receivedWinHeader,0,i,'*'); waddstr(statusWin, statusInfo); wnoutrefresh(assocWinHeader); wnoutrefresh(pathWinHeader); wnoutrefresh(statusWinHeader); wnoutrefresh(textWinHeader); wnoutrefresh(receivedWinHeader); wnoutrefresh(assocWin); wnoutrefresh(pathWin); wnoutrefresh(statusWin); wnoutrefresh(textWin); wnoutrefresh(receivedWin); keypad(textWin,TRUE); (void) idlok(assocWin,TRUE); (void) idlok(pathWin,TRUE); (void) idlok(textWin,TRUE); (void) scrollok(assocWin,TRUE); (void) scrollok(pathWin,TRUE); (void) scrollok(statusWin,TRUE); (void) scrollok(textWin,TRUE); (void) scrollok(receivedWin,TRUE); doupdate(); }
int main() { struct tm *tm; time_t t; char m[4][7] = {0}; char p[CHAR_BIT+1]; char s[512]; int i,j,k; initscr(); cbreak(); clear(); scrollok(stdscr, TRUE); idlok(stdscr, TRUE); echo(); attrset(A_BOLD); box(stdscr, 0, 0); start_color(); init_pair(2, COLOR_YELLOW, COLOR_BLACK); attrset(COLOR_PAIR(2) | A_BOLD); mvprintw(2, 17, " 10000 100000 100000 10 10000 1000"); mvprintw(3, 17, " 00 00 00 00 00 00 0 00 00"); mvprintw(4, 17, " 00 00 00 00 00 00 000 0000"); mvprintw(5, 17, " 00 00 00 00 00 00 0 00 00"); mvprintw(6, 17, " 00001 000001 000001 0001 00001 00 001 "); mvprintw(7, 17, " ...:::Binary:Clock:::... "); init_pair(1, COLOR_BLUE, COLOR_GREEN); curs_set(0); m[0][4] = ' '; m[0][2] = ' '; m[1][0] = ' '; m[0][0] = ' '; while(1) { time(&t); tm = localtime(&t); if(!tm) exit(EXIT_FAILURE); strftime(s, sizeof s, "%H%M%S", tm); for(i = 0; i < 6; s[i++] -= '0'); loop(3, 1, 4); loop(3, 0, 2); loop(3, 3, 4); loop(3, 2, 3); loop(3, 5, 4); loop(3, 4, 3); for (i = 0; i < 4; i++) { move(11+i,31); sprintf(s, "%s",m[i]); addstr(s); }/* move(11,31); sprintf(s, "%s",m[0]); addstr(s); move(12,31); sprintf(s, "%s",m[1]); addstr(s); move(13,31); sprintf(s, "%s",m[2]); addstr(s); move(14,31); sprintf(s, "%s",m[3]); addstr(s); */ refresh(); sleep(1); } endwin(); return 0; }
int main(int argc, char** argv){ int port,sd,row,col,crow,ccol,ctcol; char buffer[BUFFSIZE],tmp[BUFFSIZE]; char username[MAXUSERSIZE],*user;//samething temp holder variable if(argc!=4){ fprintf(stderr,"Usage: %s takes 3 arguments a server, a portname, and a username", argv[0]); exit(EXIT_FAILURE); } port= atoi(argv[2]); bzero(username,MAXUSERSIZE); memcpy(username,argv[3],strlen(argv[3])); user=username; // their_addr.sin_family=AF_INET; // their_addr.sin_port = htons(port); //if(!inet_aton(argv[1], &their_addr.sin_addr.s_addr)){ // fprintf(stderr,"%s requires a valid server address", argv[0]); // exit(EXIT_FAILURE); //} struct addrinfo* ai,*ac; struct addrinfo hints; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = 0; hints.ai_protocol = port; hints.ai_canonname = NULL; hints.ai_addr = NULL; hints.ai_next = NULL; if(getaddrinfo(argv[1],argv[2],NULL,&ai)<0){ close(sd); fprintf(stderr,"please input a vaild server!\n",argv[1]); exit(EXIT_FAILURE); } for (ac = ai; ac != NULL; ac = ac->ai_next) { sd = socket(AF_INET, SOCK_DGRAM,0); fcntl(sd, F_SETFL, O_NONBLOCK); if (sd == -1) continue; if (connect(sd, ac->ai_addr, ac->ai_addrlen) != -1) break; /* Success */ close(sd); } // their_addr.sin_addr.s_addr=ai.ai_addr; // memset(&(their_addr.sin_zero),'\0',8); //char host[NI_MAXHOST],service[NI_MAXSERV]; WINDOW *chat_win,*text_win,*user_win; // if(connect(sd,ai->ai_addr,ai->ai_addrlen)){ // close(sd); // fprintf(stderr,"Failed to connect to %s!\n",argv[1]); // exit(EXIT_FAILURE); // }else{ initscr(); crow=1; ccol=2; ctcol=2; getmaxyx(stdscr,row,col); chat_win=create_newwin(row-3,col-30,0,0); user_win=create_newwin(row-2,col,0,col-30); text_win=create_newwin(3,col,row-3,0); scrollok(chat_win, TRUE); idlok(chat_win,TRUE); scrollok(text_win, TRUE); noecho(); if(sd!=-1){ mvwprintw(chat_win,crow++,ccol,"connected to server %s",argv[1]); wrefresh(chat_win); }else{ mvwprintw(chat_win,crow++,ccol,"couldn't connect to server %s ... exiting",argv[1]); wrefresh(chat_win); sleep(1); delwin(text_win); delwin(chat_win); delwin(user_win); endwin(); return 0; } // } int bsize=BUFFSIZE; int nread; //send user name to server bzero(buffer,BUFFSIZE); bzero(tmp,BUFFSIZE); memcpy(buffer,username,strlen(username)); send(sd,buffer,strlen(username),0); int i=0; int j; char c; int getusers=1; wmove(text_win,1,ctcol); wrefresh(text_win); while(1){ bzero(buffer,BUFFSIZE); if(kbhit()){ c=getchar(); if(c=='\r') continue; tmp[i]=c; i++; mvwaddch(text_win,1,ctcol,c); wrefresh(text_win); ctcol++; while(1){ if(kbhit()){ c=getchar(); if(c=='\r') break; if(c==(char)127||c==(char)8){ if(i>0){ ctcol--; wmove(text_win,1,ctcol); wdelch(text_win); wrefresh(text_win); tmp[i]='\0'; i--; } continue; } if(i>BUFFSIZE) continue; tmp[i]=c; i++; mvwaddch(text_win,1,ctcol,c); wrefresh(text_win); ctcol++; } if((nread=recvp(chat_win,&crow,ccol,col-28,user_win,user,sd,buffer,BUFFSIZE,0))!=-1){ wmove(text_win,1,ctcol); wrefresh(text_win); } } } if(i>0){ if(memcmp(tmp,"clear\0",6)==0){ wmove(chat_win,1,ccol); wclrtobot(chat_win); box(chat_win, 0 , 0); wrefresh(chat_win); box(text_win,0,0); wrefresh(text_win); crow=1; ccol=1; }else{ send(sd,tmp,i,0); } wmove(text_win,1,0); wclrtoeol(text_win); wrefresh(text_win); ctcol=1; i=0; bzero(tmp,BUFFSIZE); } if((nread=recvp(chat_win,&crow,ccol,col-28,user_win,user,sd,buffer,BUFFSIZE,0))!=-1){ wmove(text_win,1,ctcol); wrefresh(text_win); if(memcmp(buffer,"close\0",6)==0){ sleep(1); break; } //onetime thing if(getusers){ getusers=0; memcpy(tmp,":users:",7); send(sd,tmp,7,0); bzero(tmp,BUFFSIZE); } } } close(sd); delwin(text_win); delwin(chat_win); delwin(user_win); endwin(); return 0; }