static void do_setenv(t_env *env, const char *name, const char *value) { char buffer[ENV_TABLE_CONTENT_SIZE]; size_t size_left; size_t len_name; size_t len_value; const char *ptr; env->flag = 0; len_name = word_len(name); size_left = sizeof(buffer); if (size_left >= len_name) { memcpy(buffer, name, len_name); size_left -= len_name; if (size_left > 1) { buffer[len_name] = '='; len_value = word_len(value); if (--size_left >= len_value) { memcpy(buffer + len_name + 1, value, len_value); buffer[len_name + 1 + len_value] = '\0'; env->flag = 1; } } } (env->flag) ? do_setenv_insert(env, buffer) : do_setenv_too_long(); }
/* If it fits, copy word with null termination into out and return 0. * Else return 1. */ static int word_get(char *s, char *out, int out_size) { int len = word_len(s); int i; if (len >= out_size) return 1; for (i = 0; i < len; i++) out[i] = s[i]; out[i] = 0; return 0; }
int get_word(char *line, int *i, t_tok *tok) { int len; len = word_len(line, (*i), tok); if (line == NULL || i == NULL || tok == NULL) return (false); if ((tok->word = my_strndup(&line[(*i)], len - 1)) == NULL) return (false); (*i) += (len > 0 ? (len - 1) : len); return (true); }
void csort(char *src, char *dest) { int i, j = 0, c = 0; char *words[256]; int count[256]; for (i = 0; i < 256; count[i] = 0, words[i++] = 0); c = wcount(src, words); for (j = 0; j < c - 1; j++) for (i = j + 1; i < c; i++) if (word_len(words[i]) < word_len(words[j])) count[j]++; else count[i]++; char *p = dest; for (i = 0; i < c; i++) for (j = 0; j < c; j++) if (count[j] == i) { copy_word(words[j], p); p += word_len(words[j]); *p++ = ' '; } *p = '\0'; }
/* Translate a direction in the bot format into a direction in Nethack format. * Return -1 if direction is invalid. */ static int botdir2nhdir(char * botdir) { int wlen = word_len(botdir); if (strncmp(botdir, "NORTH" ,wlen) == 0) { return 'k'; } else if (strncmp(botdir, "NORTH_WEST",wlen) == 0) { return 'y'; } else if (strncmp(botdir, "NORTH_EAST",wlen) == 0) { return 'u'; } else if (strncmp(botdir, "SOUTH" ,wlen) == 0) { return 'j'; } else if (strncmp(botdir, "SOUTH_WEST",wlen) == 0) { return 'b'; } else if (strncmp(botdir, "SOUTH_EAST",wlen) == 0) { return 'n'; } else if (strncmp(botdir, "WEST" ,wlen) == 0) { return 'h'; } else if (strncmp(botdir, "EAST" ,wlen) == 0) { return 'l'; } else if (strncmp(botdir, "DOWN" ,wlen) == 0) { return '>'; } else if (strncmp(botdir, "UP" ,wlen) == 0) { return '<'; } fprintf(stderr,"Direction inconnue : %s\n",botdir); exit(1); }
static void open_socket (int port) { struct sockaddr_in my_addr; int sock = socket(AF_INET, SOCK_STREAM, 0); CHK(sock); my_addr.sin_addr.s_addr = htonl(INADDR_ANY); my_addr.sin_port = htons(port); my_addr.sin_family = AF_INET; int optval = 1; CHK(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval)); CHK(bind(sock,(struct sockaddr *)&my_addr,sizeof(struct sockaddr_in))); CHK(listen(sock,1)); sockfd = accept(sock,&bot_addr, &bot_addrlen); CHK(sockfd); close(sock); char msg[MAX_MSG_SIZE] ; recv(sockfd,msg,MAX_MSG_SIZE,0); int wlen = 4; if (strncmp(msg, "NAME", wlen) == 0) { mm_botname(msg + wlen + 1,word_len(msg+wlen+1)); } }
static void parse_botcmd(char * botcmd) { int alen = word_len(botcmd); if (strncmp(botcmd, "SEARCH", alen) == 0) { mm_keypress('s'); return; } if (strncmp(botcmd, "OPEN", alen) == 0) { mm_keypress('o'); } else if (strncmp(botcmd, "FORCE", alen) == 0) { mm_keypress('\4'); } else if (strncmp(botcmd, "MOVE", alen) == 0) { } else{ fprintf(stderr,"Commande inconnue : %s\n",botcmd); exit(1); } botcmd += alen + 1; mm_keypress(botdir2nhdir(botcmd)); }
/* - it takes the cursor position into account for <hline> and <center> tags - when max_lines > -1, can display a limited number of lines (used in --More-- prompt reading) - returns position in the string where it stopped - if dev is NULL, it produces no output, but does run the function and update the cursor pos - force_auto_color_off is used for hline tags, that really don't like auto-coloring - is_symbol says if it's a symbol that needs auto-coloring - dont_auto_color is for controlling exceptions to the rule of auto-coloring */ int Out_text(StringIO *dev, User *usr, char *str, int *cpos, int *lines, int max_lines, int force_auto_color_off) { char buf[MAX_COLORBUF], c; int pos, n, dont_auto_color, color, is_symbol; if (usr == NULL || usr->display == NULL || str == NULL || cpos == NULL || lines == NULL) return 0; if (max_lines > -1 && *lines >= max_lines) return 0; dont_auto_color = force_auto_color_off; pos = 0; while(*str) { is_symbol = 0; pos++; c = *str; if ((usr->flags & USR_HACKERZ) && HACK_CHANCE) c = hackerz_mode(c); /* user-defined auto-coloring symbols */ if ((usr->flags & (USR_ANSI|USR_DONT_AUTO_COLOR)) == USR_ANSI) { char *symbol_str; symbol_str = (usr->symbols == NULL) ? Default_Symbols : usr->symbols; if (cstrchr(symbol_str, c) != NULL) { is_symbol = 1; /* auto-coloring a single dot is ugly, but multiple ones is fun */ if (c == '.' && str[1] != '.' && (pos > 0 && str[-1] != '.')) dont_auto_color = AUTO_COLOR_FORCED; } } /* word-wrap in display function Charset1 contains characters that break the line */ if (cstrchr(Wrap_Charset1, c) != NULL) { if (*cpos + word_len(str+1) >= usr->display->term_width) { if (c != ' ') { if (is_symbol && !dont_auto_color) { auto_color(usr, buf, MAX_COLORBUF); put_StringIO(dev, buf); } write_StringIO(dev, str, 1); if (is_symbol && !dont_auto_color) { restore_colorbuf(usr, usr->color, buf, MAX_COLORBUF); put_StringIO(dev, buf); } } if (str[1] == ' ') { str++; pos++; } put_StringIO(dev, "\r\n"); *cpos = 0; (*lines)++; if (max_lines > -1 && *lines >= max_lines) return pos; dont_auto_color = force_auto_color_off; if (*str) str++; continue; } } else { /* pretty word-wrap: Charset2 contains characters that wrap along with the line mind that the < character is also used for long color codes */ if (c != '<' && cstrchr(Wrap_Charset2, c) != NULL) { if (*cpos + word_len(str+1) >= usr->display->term_width) { write_StringIO(dev, "\r\n", 2); *cpos = 0; (*lines)++; if (max_lines > -1 && *lines >= max_lines) return pos-1; dont_auto_color = force_auto_color_off; } } } switch(c) { case '\b': write_StringIO(dev, "\b", 1); if (*cpos) (*cpos)--; dont_auto_color = force_auto_color_off; break; case '\n': write_StringIO(dev, "\r\n", 2); *cpos = 0; (*lines)++; if (max_lines > -1 && *lines >= max_lines) return pos; dont_auto_color = force_auto_color_off; break; case KEY_CTRL('Q'): /* don't auto-color this string */ dont_auto_color = AUTO_COLOR_FORCED; break; case KEY_CTRL('X'): write_StringIO(dev, "\r", 1); *cpos = 0; dont_auto_color = force_auto_color_off; break; case KEY_CTRL('A'): if (usr->flags & USR_BEEP) write_StringIO(dev, "\a", 1); dont_auto_color = force_auto_color_off; break; case KEY_CTRL('L'): /* clear screen */ if (usr->flags & (USR_ANSI|USR_BOLD)) write_StringIO(dev, "\x1b[1;1H\x1b[2J", 10); break; case KEY_CTRL('Z'): case KEY_CTRL('R'): case KEY_CTRL('G'): case KEY_CTRL('Y'): case KEY_CTRL('B'): case KEY_CTRL('M'): case KEY_CTRL('C'): case KEY_CTRL('W'): /* case KEY_CTRL('F'): */ if (usr->flags & USR_ANSI) { usr->color = c; color = Ansi_Color(usr, c); if (usr->flags & USR_BOLD) bufprintf(buf, sizeof(buf), "\x1b[1;%dm", color); else bufprintf(buf, sizeof(buf), "\x1b[%dm", color); put_StringIO(dev, buf); dont_auto_color = AUTO_COLOR_FORCED; } break; case KEY_CTRL('K'): str++; if (!*str) break; print_hotkey(usr, *str, buf, sizeof(buf), cpos); put_StringIO(dev, buf); dont_auto_color = force_auto_color_off; break; case KEY_CTRL('N'): if (usr->flags & USR_ANSI) { bufprintf(buf, sizeof(buf), "\x1b[0;%dm", color_table[usr->colors[BACKGROUND]].value+10); put_StringIO(dev, buf); } else if (usr->flags & USR_BOLD) put_StringIO(dev, "\x1b[0m"); if (usr->flags & USR_BOLD) put_StringIO(dev, "\x1b[1m"); dont_auto_color = force_auto_color_off; break; case KEY_CTRL('D'): if (usr->flags & (USR_ANSI | USR_BOLD)) put_StringIO(dev, "\x1b[0m"); dont_auto_color = force_auto_color_off; break; /* long codes are specified as '<yellow>', '<beep>', etc. */ case '<': n = long_color_code(dev, usr, str, cpos, lines, max_lines, dont_auto_color); if (n > 0) dont_auto_color = AUTO_COLOR_FORCED; str += n; pos += n; break; default: if (is_symbol && !dont_auto_color) { auto_color(usr, buf, MAX_COLORBUF); put_StringIO(dev, buf); } write_StringIO(dev, &c, 1); (*cpos)++; if (is_symbol && !dont_auto_color) { restore_colorbuf(usr, usr->color, buf, MAX_COLORBUF); put_StringIO(dev, buf); } if (!is_symbol) dont_auto_color = force_auto_color_off; } if (*str) str++; } /* Flush(usr); the buffering code and mainloop() will flush for us */ return pos; }