int raw_send(char *net_i, uint16_t ether_type, uint8_t *ptr, int len) { int fd; int ifindex = 0; uint8_t *buf = NULL; char *device = NULL; struct sockaddr_ll sll; uint16_t n_ethtype; char dmac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; char smac[6]; raw_print("line: %d.\n", __LINE__); if (net_i) { device = net_i; if (get_macaddr(device, smac)) { memset(smac, 0x88, 6); } } else { device = "lo"; memset(smac, 0x88, 6); } if ((ifindex = get_ifindex(device)) <= 0) { return -1; } n_ethtype = htons(ether_type); if ((fd = socket(AF_PACKET, SOCK_RAW, n_ethtype)) < 0) { printf("create socket failed.\n"); return fd; } raw_print("line: %d.\n", __LINE__); bzero(&sll, sizeof(sll)); sll.sll_family = AF_PACKET; sll.sll_halen = 6; //memcpy(sll.sll_addr, dmac, 6); sll.sll_ifindex = ifindex; sll.sll_protocol = n_ethtype; bind(fd, (struct sockaddr *)&sll, sizeof(sll)); if (strlen((char *)ptr) == 0) { ptr = (uint8_t *)"xuchunxiao"; len = strlen((char *)ptr); } buf = malloc(len + 14); memcpy(buf, dmac, 6); memcpy(buf + 6, smac, 6); memcpy(buf + 12, &n_ethtype, 2); memcpy(buf + 14, ptr, len); raw_print("line: %d.\n", __LINE__); if (sendto(fd, buf, len + 14, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0) { perror("sendto error.\n"); return -1; } raw_print("line: %d.\n", __LINE__); free(buf); close(fd); return 0; }
int raw_send_all(char *net_i, uint8_t *ptr, int len) { int fd; int ifindex = 0; char *device = NULL; struct sockaddr_ll sll; raw_print("line: %d.\n", __LINE__); condition_if_false_ret(ptr != NULL, -1); if (net_i) { device = net_i; } else { device = "lo"; } if ((ifindex = get_ifindex(device)) <= 0) { return -1; } if ((fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) { printf("create socket failed.\n"); return fd; } raw_print("line: %d.\n", __LINE__); bzero(&sll, sizeof(sll)); sll.sll_family = AF_PACKET; sll.sll_halen = 6; //memcpy(sll.sll_addr, dmac, 6); sll.sll_ifindex = ifindex; sll.sll_protocol = htons(ETH_P_ALL); bind(fd, (struct sockaddr *)&sll, sizeof(sll)); if (strlen((char *)ptr) == 0) { ptr = (uint8_t *)"xuchunxiao"; len = strlen((char *)ptr); } raw_print("line: %d.\n", __LINE__); if (sendto(fd, ptr, len, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0) { perror("sendto error.\n"); return -1; } raw_print("line: %d.\n", __LINE__); close(fd); return 0; }
int osc_print(int ps1, int ps2, char* pt) { if (pt && *pt) snprintf(temp, sizeof(temp), "\033]%d;%s\007", ps1, pt); else snprintf(temp, sizeof(temp), "\033]%d;?\007", ps1); raw_print(temp); return 0; }
jboolean nhjni_run() { windowprocs = _nhjni_proxy_procs; _nhjni_proxy_init(); int fd = create_levelfile(0, (char *)0); if (fd < 0) { raw_print("Cannot create lock file"); } else { hackpid = 1; write(fd, (genericptr_t) &hackpid, sizeof(hackpid)); close(fd); } iflags.news = TRUE; int argc=1; char *argv[]={"nethack",NULL}; initoptions(); init_nhwindows(&argc,argv); dlb_init(); vision_init(); display_gamewindows(); if ((fd = restore_saved_game()) >= 0) { const char *fq_save = fqname(SAVEF, SAVEPREFIX, 1); pline("Restoring save file..."); mark_synch(); /* flush output */ if(!dorecover(fd)) goto not_recovered; check_special_room(FALSE); //wd_message(); if (discover || wizard) { if(yn("Do you want to keep the save file?") == 'n') (void) delete_savefile(); else { (void) chmod(fq_save,FCMASK); /* back to readable */ compress(fq_save); } } flags.move = 0; } else { not_recovered: player_selection(); newgame(); flags.move = 0; set_wear(); (void) pickup(1); } moveloop(); return JNI_TRUE; }
static void vraw_printf(const char *line, va_list the_args) { if (!strchr(line, '%')) raw_print(line); else { /* We can't use msgvprintf here because the game might not be running. We use xmvasprintf instead (vasprintf would be a little more appropriate but might not be available), then transfer to the stack, so that there are no untracked allocations when we make the API call. */ struct xmalloc_block *xm_temp = NULL; const char *fmtline = xmvasprintf(&xm_temp, line, the_args); char fmtline_onstack[strlen(fmtline) + 1]; strcpy(fmtline_onstack, fmtline); xmalloc_cleanup(&xm_temp); raw_print(fmtline_onstack); } }
/* Exits the window system. This should dismiss all windows, except the "window" used for raw_print(). str is printed if possible. */ void curses_exit_nhwindows(const char *str) { curses_cleanup(); curs_set(orig_cursor); endwin(); iflags.window_inited = 0; if (str != NULL) { raw_print(str); } }
void get_term_title(char *title) { int n; static char buffer[BUFSIZ]; char temp[20]; snprintf(temp,sizeof(temp),"\033[%dt",21); raw_print(temp); n = tty_read(buffer,sizeof(buffer)); if(n==0) goto read_failure; if(n==1) { int n2 = tty_read(buffer+1,sizeof(buffer)-1); if(n2==0) goto read_failure; n+=n2; } // n+= tty_read(buffer+1,sizeof(buffer)-1); while( !(buffer[n-2] == '\033' && buffer[n-1] == '\\') ) { // n += tty_read(buffer+n,sizeof(buffer)-n); int n2 = tty_read(buffer+1,sizeof(buffer)-1); if(n2==0) goto read_failure; n+=n2; } buffer[n-2]= '\0'; snprintf((char *)title,sizeof(buffer),"%s",buffer+3); return; read_failure: sprintf((char *)title,"xterm"); }
void pline VA_DECL(const char *, line) #endif /* USE_STDARG | USE_VARARG */ { /* start of vpline() or of nested block in USE_OLDARG's pline() */ char pbuf[3 * BUFSZ]; int ln; xchar msgtyp; /* Do NOT use VA_START and VA_END in here... see above */ if (!line || !*line) return; #ifdef HANGUPHANDLING if (program_state.done_hup) return; #endif if (program_state.wizkit_wishing) return; if (index(line, '%')) { Vsprintf(pbuf, line, VA_ARGS); line = pbuf; } if ((ln = (int) strlen(line)) > BUFSZ - 1) { if (line != pbuf) /* no '%' was present */ (void) strncpy(pbuf, line, BUFSZ - 1); /* caveat: unterminated */ /* truncate, preserving the final 3 characters: "___ extremely long text" -> "___ extremely l...ext" (this may be suboptimal if overflow is less than 3) */ (void) strncpy(pbuf + BUFSZ - 1 - 6, "...", 3); /* avoid strncpy; buffers could overlap if excess is small */ pbuf[BUFSZ - 1 - 3] = line[ln - 3]; pbuf[BUFSZ - 1 - 2] = line[ln - 2]; pbuf[BUFSZ - 1 - 1] = line[ln - 1]; pbuf[BUFSZ - 1] = '\0'; line = pbuf; } if (!iflags.window_inited) { raw_print(line); iflags.last_msg = PLNMSG_UNKNOWN; return; } #ifndef MAC if (no_repeat && !strcmp(line, toplines)) return; #endif /* MAC */ if (vision_full_recalc) vision_recalc(0); if (u.ux) flush_screen(1); /* %% */ msgtyp = msgtype_type(line); if (msgtyp == MSGTYP_NOSHOW) return; if (msgtyp == MSGTYP_NOREP && !strcmp(line, prevmsg)) return; putstr(WIN_MESSAGE, 0, line); /* this gets cleared after every pline message */ iflags.last_msg = PLNMSG_UNKNOWN; strncpy(prevmsg, line, BUFSZ); if (msgtyp == MSGTYP_STOP) display_nhwindow(WIN_MESSAGE, TRUE); /* --more-- */ #if !(defined(USE_STDARG) || defined(USE_VARARGS)) /* provide closing brace for the nested block which immediately follows USE_OLDARGS's VA_DECL() */ VA_END(); #endif }
int dosave() { #ifdef KEEP_SAVE /*WAC for reloading*/ register int fd; #endif clear_nhwindow(WIN_MESSAGE); if(yn("Really save?") == 'n') { clear_nhwindow(WIN_MESSAGE); if(multi > 0) nomul(0); } else { clear_nhwindow(WIN_MESSAGE); pline("Saving..."); #if defined(UNIX) || defined(VMS) || defined(__EMX__) program_state.done_hup = 0; #endif #ifdef KEEP_SAVE saverestore = FALSE; if (flags.keep_savefile) if(yn("Really quit?") == 'n') saverestore = TRUE; if(dosave0() && !saverestore) { #else if(dosave0()) { #endif program_state.something_worth_saving = 0; u.uhp = -1; /* universal game's over indicator */ /* make sure they see the Saving message */ display_nhwindow(WIN_MESSAGE, TRUE); exit_nhwindows("Be seeing you..."); terminate(EXIT_SUCCESS); } /*WAC redraw later else (void)doredraw();*/ } #ifdef KEEP_SAVE if (saverestore) { /*WAC pulled this from pcmain.c - restore game from the file just saved*/ fd = create_levelfile(0); if (fd < 0) { raw_print("Cannot create lock file"); } else { hackpid = 1; write(fd, (genericptr_t) &hackpid, sizeof(hackpid)); close(fd); } #ifdef MFLOPPY level_info[0].where = ACTIVE; #endif fd = restore_saved_game(); if (fd >= 0) dorecover(fd); check_special_room(FALSE); flags.move = 0; /*WAC correct these after restore*/ if(flags.moonphase == FULL_MOON) change_luck(1); if(flags.friday13) change_luck(-1); if(iflags.window_inited) clear_nhwindow(WIN_MESSAGE); } saverestore = FALSE; #endif (void)doredraw(); return 0; } #if defined(UNIX) || defined(VMS) || defined (__EMX__) || defined(WIN32) /*ARGSUSED*/ void hangup(sig_unused) /* called as signal() handler, so sent at least one arg */ int sig_unused; { # ifdef NOSAVEONHANGUP (void) signal(SIGINT, SIG_IGN); clearlocks(); # ifndef VMS terminate(EXIT_FAILURE); # endif # else /* SAVEONHANGUP */ if (!program_state.done_hup++) { if (program_state.something_worth_saving) (void) dosave0(); # ifdef VMS /* don't call exit when already within an exit handler; that would cancel any other pending user-mode handlers */ if (!program_state.exiting) # endif { clearlocks(); terminate(EXIT_FAILURE); } } # endif return; } #endif /* returns 1 if save successful */ int dosave0() { const char *fq_save; register int fd, ofd; xchar ltmp; d_level uz_save; char whynot[BUFSZ]; if (!SAVEF[0]) return 0; fq_save = fqname(SAVEF, SAVEPREFIX, 1); /* level files take 0 */ #if defined(UNIX) || defined(VMS) (void) signal(SIGHUP, SIG_IGN); #endif #ifndef NO_SIGNAL (void) signal(SIGINT, SIG_IGN); #endif #if defined(MICRO) && defined(MFLOPPY) if (!saveDiskPrompt(0)) return 0; #endif HUP if (iflags.window_inited) { uncompress_area(fq_save, SAVEF); fd = open_savefile(); if (fd > 0) { (void) close(fd); clear_nhwindow(WIN_MESSAGE); There("seems to be an old save file."); if (yn("Overwrite the old file?") == 'n') { compress_area(fq_save, SAVEF); #ifdef KEEP_SAVE /*WAC don't restore if you didn't save*/ saverestore = FALSE; #endif return 0; } } } HUP mark_synch(); /* flush any buffered screen output */ fd = create_savefile(); if(fd < 0) { HUP pline("Cannot open save file."); (void) delete_savefile(); /* ab@unido */ return(0); } vision_recalc(2); /* shut down vision to prevent problems in the event of an impossible() call */ /* undo date-dependent luck adjustments made at startup time */ if(flags.moonphase == FULL_MOON) /* ut-sally!fletcher */ change_luck(-1); /* and unido!ab */ if(flags.friday13) change_luck(1); if(iflags.window_inited) HUP clear_nhwindow(WIN_MESSAGE); #if defined(MICRO) && defined(TTY_GRAPHICS) if (!strncmpi("tty", windowprocs.name, 3)) { dotcnt = 0; dotrow = 2; curs(WIN_MAP, 1, 1); putstr(WIN_MAP, 0, "Saving:"); } #endif #ifdef MFLOPPY /* make sure there is enough disk space */ if (iflags.checkspace) { long fds, needed; savelev(fd, ledger_no(&u.uz), COUNT_SAVE); savegamestate(fd, COUNT_SAVE); needed = bytes_counted; for (ltmp = 1; ltmp <= maxledgerno(); ltmp++) if (ltmp != ledger_no(&u.uz) && level_info[ltmp].where) needed += level_info[ltmp].size + (sizeof ltmp); fds = freediskspace(fq_save); if (needed > fds) { HUP { There("is insufficient space on SAVE disk."); pline("Require %ld bytes but only have %ld.", needed, fds); } flushout(); (void) close(fd); (void) delete_savefile(); return 0; } co_false(); }
void raw_println(char* buf, int len) { // Print with a carriage return raw_print(buf, len); printf("\n"); }
void print(std::string const & input) { raw_print(input + "\n"); }