short choose_game_type() { char line[MAX_LINE]; /* Asking for a choice */ do { printf("\nChose what type of game you wanna play:\n"); printf("\n1 - Human vs. Human\n"); printf("2 - Human vs Computer - ?\n"); printf("3 - Computer vs Computer - ? : "); /* Retrieving the name in the name string */ fgets(line, MAX_LINE, stdin); /* Ungetting the last character readed if the string entered is too long or for ungetting the new line character */ ungetc(line[strlen(line)-1], stdin); /* Emptying the buffer (so that extra characters won't be used for the next name) */ empty_buffer(); line[1] = '\0'; } while(line[ZERO] != '1' && line[ZERO] != '2' && line[ZERO] != '3'); /* Returning 1 for the first choice, 2 for the second */ return atoi(line); }
/* * putbits: * -------- * write N bits into the bit stream. * bs = bit stream structure * val = value to write into the buffer * N = number of bits of val */ void putbits(bitstream_t *bs, unsigned long int val, unsigned int N) { static int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff}; register int j = N; register int k, tmp; #ifdef DEBUG if (N > MAX_LENGTH) printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); #endif bs->totbit += N; while (j > 0) { k = MIN(j, bs->buf_bit_idx); tmp = val >> (j-k); bs->buf[bs->buf_byte_idx] |= (tmp&putmask[k]) << (bs->buf_bit_idx-k); bs->buf_bit_idx -= k; if (!bs->buf_bit_idx) { bs->buf_bit_idx = 8; bs->buf_byte_idx--; if (bs->buf_byte_idx < 0) empty_buffer(bs, MINIMUM); bs->buf[bs->buf_byte_idx] = 0; } j -= k; } }
INLINE #endif /* not __clang__ */ void putbits (Bit_stream_struc * bs, unsigned int val, int N) { register int j = N; register int k, tmp; /* if (N > MAX_LENGTH) fprintf(stderr, "Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); ignore check!! MFC Apr 00 */ bs->totbit += N; while (j > 0) { k = MIN (j, bs->buf_bit_idx); tmp = val >> (j - k); bs->buf[bs->buf_byte_idx] |= (tmp & putmask[k]) << (bs->buf_bit_idx - k); bs->buf_bit_idx -= k; if (!bs->buf_bit_idx) { bs->buf_bit_idx = 8; bs->buf_byte_idx--; if (bs->buf_byte_idx < 0) empty_buffer (bs, minimum); bs->buf[bs->buf_byte_idx] = 0; } j -= k; } }
/*close the device containing the bit stream after a write process*/ void close_bit_stream_w (Bit_stream_struc * bs) { putbits (bs, 0, 7); empty_buffer (bs, bs->buf_byte_idx + 1); fclose (bs->pt); desalloc_buffer (bs); }
short choose_player_color() { char line[MAX_LINE]; do { /* Asking for a choice */ printf("\nChose your player's color: \n"); printf("\n1 - BLACK Player\n"); printf("2 - WHITE Player - ? : "); /* Retrieving the name in the name string */ fgets(line, MAX_LINE, stdin); /* Ungetting the last character readed if the string entered is too long or for ungetting the new line character */ ungetc(line[strlen(line)-1], stdin); /* Emptying the buffer (so that extra characters won't be used for the next name */ empty_buffer(); line[1] = '\0'; } while(line[ZERO] != '1' && line[ZERO] != '2'); /* Returning 1 for the first choice, 2 for the second */ return (atoi(line)-1); }
char* ask_load_game() { /* Allocating the string for the return of the file name */ char* filename = malloc(MAX_FILE_NAME * sizeof(char)); printf("\nEnter the loading file's name: "); /* Getting the file name from the user */ fgets(filename, MAX_FILE_NAME, stdin); /* Ungetting the last character readed if the string entered is too long */ ungetc(filename[strlen(filename)-1], stdin); /* Emptying the buffer (so that extra characters won't be used for the next answer */ empty_buffer(); /* If the last character of the name is a new line (that fgets can put into a string), replacing it with a string terminating character */ if(filename[strlen(filename)-1] == NL) filename[strlen(filename)-1] = '\0'; /* Verifying that the file exixts */ if(exists_file(filename, R_OK) == NO) { /* If not, printing an error, freeing the string and returning NULL */ printf("\nThis file does not exist or is not valid\n\n"); free(filename); return NULL; } return filename; }
int get_next_line(int fd, char **line) { static char buff[BUFF_SIZE]; int first_passage; int nb; int count; if (BUFF_SIZE <= 0 || !line || (fd < 2 && fd != 0)) return (-1); *line = (char *)ft_memalloc(sizeof(*line) * (BUFF_SIZE + 1)); first_passage = 1; if ((nb = empty_buffer(buff, line, (-1), 0)) == 1) return (1); else first_passage = 0; while ((count = read(fd, buff, BUFF_SIZE)) > 0) { if ((check_cond(buff, line, count, &first_passage)) == 1) return (1); end_line(buff, line, (-1), (-1)); } if (count == -1) return (-1); if (nb == 2 || **line) return (1); return (0); }
void enter_one_name(Game* game, short color) { char name[MAX_NAME]; char line[MAX_LINE]; /* While the user is not satisfied with the name */ do { /* Label for asking the black player */ ask_name: /* Asking for the name of the black player */ printf("\nEnter the name of the %s player (maximumof %d characters - letters and digits only): ", (color==BLACK?"BLACK":"WHITE"), MAX_NAME-1); /* Retrieving the name in the name string */ fgets(name, MAX_NAME, stdin); /* Ungetting the last character readed if the string entered is too long */ ungetc(name[strlen(name)-1], stdin); /* Emptying the buffer (so that extra characters won't be used for the next name */ empty_buffer(); /* If the last character of the name is a new line (that fgets can put into a string), replacing it with a string terminating character */ if(name[strlen(name)-1] == NL) name[strlen(name)-1] = '\0'; /* If the name conatins other characters than letters and numbers, asking again */ if(!contains_only_letters(name)) goto ask_name; /* While the answer is not Yes of No, asking if the name suits the player */ do { printf("\n%s, is that the name you want? (Y/N) : ", name); fgets(line, MAX_LINE, stdin); str2upper(line); } while(line[ZERO] != 'Y' && line[ZERO] != 'N'); } while(line[ZERO] == 'N'); /* If the name entered for the white player is the same as the one for te black player, asking again */ if(color == WHITE && game->p1.player_type == HUMAN && strcmp(name, game->p1.player_name) == 0) { printf("\nThis name is already taken"); goto ask_name; } /* Copying the string into the reserved space for it and updating the type of the player (here HUMAN) */ if(color == BLACK) { strcpy(game->p1.player_name, name); game->p1.player_type = HUMAN; } else { strcpy(game->p2.player_name, name); game->p2.player_type = HUMAN; } printf("\n"); }
int ask_save_game(Game* game) { /* Testing whether the game is saved of not. If not, asking the players if they want to save it */ if(game->saved == NO) { /* If the answer is yes, retrieving the name of the file */ if(ask_YN("Do you wish to save the game?") == YES) { char filename[MAX_FILE_NAME]; printf("\nType the saving file's name (maximum of %d characters) : ", MAX_FILE_NAME); fgets(filename, MAX_FILE_NAME, stdin); /* Ungetting the last character readed if the string entered is too long */ ungetc(filename[strlen(filename)-1], stdin); /* Emptying the buffer (so that extra characters won't be used for the next answer */ empty_buffer(); /* If the last character of the name is a new line (that fgets can put into a string), replacing it with a string terminating character */ if(filename[strlen(filename)-1] == NL) filename[strlen(filename)-1] = '\0'; /* If the file already exists and can be opened for writing, asking if the user wants to continue */ if(exists_file(filename, W_OK) == YES) { printf("\nThis file already exists, saving the game will erase all its contents"); /* If he doesn't want to, returning NO for the saving */ if(ask_YN("\nDo you wish to continue?") == NO) return NO; } /* Saving the game, and if it has been saved, printing a message */ if(game_saving(game, filename)) printf("\nYour game has been saved in the file '%s'\n", filename); /* If it has not been saved, printing an error */ else printf("\nSomething went wrong, the game has not been saved - don't get mad at me..."); } /* If player don't want to save the game, returning NO for the saving */ else return NO; } /* If the game was already saved, printing a message */ else { printf("\nThe current game is already saved"); /* Returning no for the saving */ return NO; } /* Returning YES if it has been saved */ return YES; }
int copy_buffer(char *buffer,int size,Bit_stream_struc *bs) { int i,j=0; if (size!=0 && (bs->buf_size-1 - bs->buf_byte_idx) > size ) return -1; for (i=bs->buf_size-1 ; i > bs->buf_byte_idx ; (i-- )) buffer[j++]=bs->buf[i]; assert(j == (bs->buf_size-1 - bs->buf_byte_idx)); empty_buffer(bs); /* empty buffer, (changes bs->buf_size) */ return j; }
void swallow_whitespace(FILE *stream) { int c; do { while((c = getc_unlocked(stream)) == ' ' || c == '\t' || c == '\n'); if(c == '#') empty_buffer(stream); } while(c == '#'); ungetc(c, stream); }
/*write 1 bit from the bit stream */ void put1bit (Bit_stream_struc * bs, int bit) { bs->totbit++; bs->buf[bs->buf_byte_idx] |= (bit & 0x1) << (bs->buf_bit_idx - 1); bs->buf_bit_idx--; if (!bs->buf_bit_idx) { bs->buf_bit_idx = 8; bs->buf_byte_idx--; if (bs->buf_byte_idx < 0) empty_buffer (bs, minimum); bs->buf[bs->buf_byte_idx] = 0; } }
int minishell(\ void) { int ret; char cmd_buffer[FT_SH_CMD_BUFFER_SIZE + 1]; ft_bzero((void *)cmd_buffer, sizeof(char) * (FT_SH_CMD_BUFFER_SIZE + 1)); ft_printf((IS(O_COLOR, OPT) ? FMT_COL_PROMPT : FMT_STD_PROMPT)); ret = read(0, cmd_buffer, FT_SH_CMD_BUFFER_SIZE); if (ret < 0) { error(Read, NULL, EXIT_FAILURE); return (1); } else if (!ret) return (0); else if (empty_buffer(cmd_buffer)) return (1); if (cmd_buffer[ret - 1] == '\n') cmd_buffer[ret - 1] = 0; return (handle_cmd(cmd_buffer)); }
int sync() { return empty_buffer(); }
static int st_joining(struct state *st) { struct pkt pkt; int ret; // Actually attempt to join the data if (st->buffer_offset >= 0) { struct pkt pkts[REQUIRED_MATCHES]; struct pkt old_pkts[REQUIRED_MATCHES]; int synced = 0; int disk_offset = 0; int buffer_start = st->buffer_offset; for (disk_offset=disk_offset; (disk_offset+REQUIRED_MATCHES) < SKIP_AMOUNT && !synced; disk_offset++) { fill_buffer(st, pkts, REQUIRED_MATCHES, packet_get_next_raw); for (st->search_limit = 0; (st->search_limit + REQUIRED_MATCHES) < SKIP_AMOUNT && !synced; st->search_limit++) { int i; int matches_found = 0; fill_buffer(st, old_pkts, REQUIRED_MATCHES, buffer_get_packet); // Check to see if our run matches up for (i=0; i<REQUIRED_MATCHES; i++) { int dat = pkts[i].data.nand_cycle.data; int old_dat = old_pkts[i].data.nand_cycle.data; int ctrl = pkts[i].data.nand_cycle.control; int old_ctrl = old_pkts[i].data.nand_cycle.control; if (dat == old_dat && ctrl == old_ctrl) matches_found++; } // If enough packets match, we're synced if (matches_found >= REQUIRED_MATCHES-1) { st->last_sec_dif = st->sec_dif; st->last_nsec_dif = st->nsec_dif; st->sec_dif = -(pkts[REQUIRED_MATCHES/2].header.sec-old_pkts[REQUIRED_MATCHES/2].header.sec); st->nsec_dif = -(pkts[REQUIRED_MATCHES/2].header.nsec-old_pkts[REQUIRED_MATCHES/2].header.nsec); if (st->nsec_dif > 1000000000L) { st->nsec_dif -= 1000000000L; st->sec_dif++; } else if (st->nsec_dif < 0) { st->nsec_dif += 1000000000L; st->sec_dif--; } synced=1; buffer_unget_packet(st, old_pkts); } else { empty_buffer(st, old_pkts, REQUIRED_MATCHES, buffer_unget_packet); buffer_get_packet(st, old_pkts); } } if (!synced) { empty_buffer(st, pkts, REQUIRED_MATCHES, packet_unget); empty_buffer(st, old_pkts, REQUIRED_MATCHES, buffer_unget_packet); packet_get_next_raw(st, pkts); } } if (!synced) printf("Couldn't join\n"); // Now we're synced, just ignore the rest of the matched-buffer // packets. This is because if they're in the buffer, they've // already been written out. int tries = 0; while ((st->buffer_offset+st->search_limit)%SKIP_AMOUNT != buffer_start-1) { struct pkt old_pkt; int dat, old_dat, ctrl, old_ctrl; buffer_get_packet(st, &old_pkt); packet_get_next_raw(st, &pkt); dat = pkt.data.nand_cycle.data; old_dat = old_pkt.data.nand_cycle.data; ctrl = pkt.data.nand_cycle.control; old_ctrl = old_pkt.data.nand_cycle.control; tries++; if ((dat != old_dat) || (ctrl != old_ctrl)) { printf("Join anomaly after %d tries: %d/%d and %d/%d\n", tries, old_dat, dat, old_ctrl, ctrl); } } st->buffer_offset = -1; st->search_limit = 0; } // Done now, copy data while ((ret = packet_get_next_raw(st, &pkt)) == 0) { if (!is_nand(st, &pkt)) { packet_unget(st, &pkt); jstate_set(st, ST_SEARCHING); break; } if (st->nsec_dif > 0) { pkt.header.nsec += st->nsec_dif; if (pkt.header.nsec > 1000000000L) { pkt.header.nsec -= 1000000000L; pkt.header.sec++; } pkt.header.sec += st->sec_dif; } else { pkt.header.nsec -= st->nsec_dif; if (pkt.header.nsec <= 0) { pkt.header.nsec += 1000000000L; pkt.header.sec--; } pkt.header.sec -= st->sec_dif; } packet_write(st, &pkt); buffer_put_packet(st, &pkt); } return ret; }
int main (int argc, char * argv[]){ printf ("\n--------------X ROUTING CONFIG--------------\n\n"); printf ("COMMAND LIST: command_list\n"); printf ("MANUAL: man <command>\n"); printf ("EXIT: exit\n"); printf ("\n"); char user_input [USER_INPUT], *result;; char user_command [40]; char arg1 [30]; char arg2 [30]; char arg3 [30]; char arg4 [30]; char arg5 [30]; char aux_buffer[200]; int p = 0; while (p == 0){ printf ("> "); result = fgets (user_input, USER_INPUT, stdin); int j; for (j=0; j<200; j++){ if (user_input [j] == ' '){ aux_buffer[j] = '|'; ++j;} else{ if(user_input [j] == '\n'){aux_buffer[j] ='\0'; break;}} aux_buffer[j] = user_input [j]; } int i; int stream_index; int end_of_stream = 0; int arg_num = 0; for (i=0; i<40; i++){ if (aux_buffer[i] == '|' || aux_buffer[i] == '\0'){ if (aux_buffer[i] == '|'){ stream_index = i+1; arg_num++; user_command [i] = '\0';} else {aux_buffer[i] = '\0'; end_of_stream = 1; arg_num++; user_command [i] = '\0';} break; } user_command [i] = aux_buffer[i]; } int command_number = 0; if (strcmp (user_command, "") == 0) command_number = 69; /*if (command_number != 69){ printf ("\n> USER COMMAND: >%s<\n", user_command);}*/ if (strcmp (user_command, "route_create") == 0) command_number = 1; if (strcmp (user_command, "route_lookup") == 0) command_number = 2; if (strcmp (user_command, "route_print") == 0) command_number = 3; if (strcmp (user_command, "route_table_remove") == 0) command_number = 4; if (strcmp (user_command, "route_table_find") == 0) command_number = 5; if (strcmp (user_command, "route_table_lookup") == 0) command_number = 6; if (strcmp (user_command, "route_table_read") == 0) command_number = 7; if (strcmp (user_command, "route_table_write") == 0) command_number = 8; if (strcmp (user_command, "route_table_print") == 0) command_number = 9; if (strcmp (user_command, "man") == 0) command_number = 10; if (strcmp (user_command, "command_list") == 0) command_number = 11; if (strcmp (user_command, "exit") == 0) command_number = 12; if (strcmp (user_command, "clear") == 0) command_number = 13; if (command_number != 69)//{ if (command_number == 0) printf ("> COMMAND NOT FOUND\n"); //else printf ("> COMMAND NUMBER: >%d<\n\n", command_number);} int end_loop = 0; int k = 0; if (end_of_stream == 0){ arg_num = 2; for (i=stream_index; i<170; i++){ switch (arg_num){ case 2: arg1 [k] = aux_buffer[i]; ++k; if (aux_buffer[i+1] == '|'){++arg_num; ++i; arg1 [k] ='\0'; k=0;} else{if (aux_buffer[i+1] == '\0'){end_loop = 1; arg1 [k] ='\0';}} break; case 3: arg2 [k] = aux_buffer[i]; ++k; if (aux_buffer[i+1] == '|'){++arg_num; ++i; arg2 [k] ='\0'; k=0;} else{if (aux_buffer[i+1] == '\0'){end_loop = 1; arg2 [k] ='\0';}} break; case 4: arg3 [k] = aux_buffer[i]; ++k; if (aux_buffer[i+1] == '|'){++arg_num; ++i; arg3 [k] ='\0'; k=0;} else{if (aux_buffer[i+1] == '\0'){end_loop = 1; arg3 [k] ='\0';}} break; case 5: arg4 [k] = aux_buffer[i]; ++k; if (aux_buffer[i+1] == '|'){++arg_num; ++i; arg4 [k] ='\0'; k=0;} else{if (aux_buffer[i+1] == '\0'){end_loop = 1; arg4 [k] ='\0';}} break; case 6: arg5 [k] = aux_buffer[i]; ++k; if (aux_buffer[i+1] == '|'){++arg_num; ++i; arg5 [k] ='\0'; k=0;} else{if (aux_buffer[i+1] == '\0'){end_loop = 1; arg5 [k] ='\0';}} break; } if (end_loop == 1) break; } } int arg_num_required; int exit_flag = 0; ipv4_addr_t ip_addr; ipv4_addr_t ip_mask; ipv4_addr_t ip_gw; int index; unsigned int adist; if (ipv4_route_table_aux == NULL) ipv4_route_table_aux = ipv4_route_table_create (); switch (command_number){ case 1: arg_num_required = 6; if (arg_num != arg_num_required){ printf ("> usage: route_create <ip_addr> <ip_mask> <iface> <gw> <adist>\n"); break;} ipv4_str_addr (arg1, ip_addr); ipv4_str_addr (arg2, ip_mask); ipv4_str_addr (arg4, ip_gw); adist = (unsigned int) atoi (arg5); ipv4_route_t * ipv4_route_new; ipv4_route_new = ipv4_route_create (ip_addr, ip_mask, arg3, ip_gw, adist); ipv4_route_table_add (ipv4_route_table_aux, ipv4_route_new); break; case 2: break; case 3: arg_num_required = 3; if (arg_num != arg_num_required){ printf ("> usage: route_print <ip_addr> <ip_mask>\n"); break;} ipv4_str_addr (arg1, ip_addr); ipv4_str_addr (arg2, ip_mask); index = ipv4_route_table_find (ipv4_route_table_aux, ip_addr, ip_mask); ipv4_route_aux = ipv4_route_table_get (ipv4_route_table_aux, index); ipv4_route_print (ipv4_route_aux); printf ("\n\n"); break; case 4: arg_num_required = 3; if (arg_num != arg_num_required){ printf ("> usage: route_table_remove <ip_addr> <ip_mask>\n"); break;} index = ipv4_route_table_find (ipv4_route_table_aux, ip_addr, ip_mask); ipv4_route_aux = ipv4_route_table_remove (ipv4_route_table_aux, index); ipv4_route_free (ipv4_route_aux); break; case 5: arg_num_required = 3; if (arg_num != arg_num_required){ printf ("> usage: route_table_find <ip_addr> <ip_mask>\n"); break;} ipv4_str_addr (arg1, ip_addr); ipv4_str_addr (arg2, ip_mask); index = ipv4_route_table_find (ipv4_route_table_aux, ip_addr, ip_mask); printf ("INDEX FOR SUBNET %s WITH MASK %s: %d\n\n", arg1, arg2, index); break; case 6: ipv4_route_aux = NULL; arg_num_required = 2; if (arg_num != arg_num_required){ printf ("> usage: route_table_lookup <ip_addr>\n"); break;} ipv4_str_addr (arg1, ip_addr); ipv4_route_aux = ipv4_route_table_lookup (ipv4_route_table_aux, ip_addr); if (ipv4_route_aux != NULL){ ipv4_route_print (ipv4_route_aux); printf ("\n");} else printf ("No route available\n"); break; case 7: arg_num_required = 2; if (arg_num != arg_num_required){ printf ("> usage: route_table_read <filename>\n"); break;} ipv4_route_table_read (arg1, ipv4_route_table_aux); break; case 8: arg_num_required = 2; if (arg_num != arg_num_required){ printf ("> usage: route_table_write <filename>\n"); break;} ipv4_route_table_write (ipv4_route_table_aux, arg1); break; case 9: arg_num_required = 1; if (arg_num != arg_num_required){ printf ("> usage: route_table_print\n"); break;} printf ("\n"); ipv4_route_table_print (ipv4_route_table_aux); printf ("\n"); break; case 10: arg_num_required = 2; if (arg_num != arg_num_required){ printf ("> usage: man <command>\n"); break;} int man_number; if (strcmp (arg1, "route_create") == 0) man_number = 1; if (strcmp (arg1, "route_lookup") == 0) man_number = 2; if (strcmp (arg1, "route_print") == 0) man_number = 3; if (strcmp (arg1, "route_table_remove") == 0) man_number = 4; if (strcmp (arg1, "route_table_find") == 0) man_number = 5; if (strcmp (arg1, "route_table_lookup") == 0) man_number = 6; if (strcmp (arg1, "route_table_read") == 0) man_number = 7; if (strcmp (arg1, "route_table_write") == 0) man_number = 8; if (strcmp (arg1, "route_table_print") == 0) man_number = 9; if (strcmp (arg1, "man") == 0) man_number = 10; if (strcmp (arg1, "command_list") == 0) man_number = 11; if (strcmp (arg1, "exit") == 0) man_number = 12; if (strcmp (arg1, "clear") == 0) command_number = 13; printf ("\n"); switch (man_number){ case 1: printf ("route_create <ip_addr> <ip_mask> <iface> <gw> <adist>\n\n"); printf ("This command creates an IPv4 route\n\n"); printf ("<ip_addr>\t\tIPv4 subnet destination address\n"); printf ("<ip_mask>\t\tDestination subnet mask\n"); printf ("<iface>\t\t\tInterface name\n"); printf ("<gw>\t\t\tGateway\n"); printf ("<adist>\t\t\tAdministrative distance\n\n"); break; case 2: printf ("route_lookup <> <> \n\n"); printf ("This command indicates if the specified IPv4 address belongs to the indicated subnet\n\n"); printf ("<> \n"); printf ("<> \n\n"); break; case 3: printf ("route_print <ip_addr> <ip_mask>\n\n"); printf ("This command prints the specified route\n\n"); printf ("<ip_addr> IPv4 subnet destination address\n"); printf ("<ip_mask> Destination subnet mask\n\n"); break; case 4: printf ("route_table_remove <ip_addr> <ip_mask>\n\n"); printf ("This command removes one of the routes from the routing table\n\n"); printf ("<ip_addr> IPv4 subnet destination address\n"); printf ("<ip_mask> Destination subnet mask\n\n"); break; case 5: printf ("route_table_find <ip_addr> <ip_mask>\n\n"); printf ("This command prints the index assigned to the corresponding subnet IPv4 address\n\n"); printf ("<ip_addr> IPv4 subnet destination address\n"); printf ("<ip_mask> Destination subnet mask\n\n"); break; case 6: printf ("route_table_lookup <ip_addr>\n\n"); printf ("This command prints the best route to reach the specified destination subnet IPv4 address\n\n"); printf ("<ip_addr> IPv4 subnet destination address\n\n"); break; case 7: printf ("route_table_read <filename>\n\n"); printf ("This command reads the specified file and adds the content to the actual table\n\n"); printf ("<filename> Name of the file\n\n"); break; case 8: printf ("route_table_read <write>\n\n"); printf ("This command writes the contents of the actual table in a file\n\n"); printf ("<filename> Name of the file\n\n"); break; case 9: printf ("route_table_print\n\n"); printf ("This command prints the contents of the actual table\n\n"); break; case 10: break; case 11: break; case 12: break; default: printf ("No manual entry for %s\n\n", arg1); } break; case 11: printf ("route_create\n"); printf ("route_lookup\n"); printf ("route_print\n"); printf ("route_table_remove\n"); printf ("route_table_find\n"); printf ("route_table_lookup\n"); printf ("route_table_read\n"); printf ("route_table_write\n"); printf ("route_table_print\n"); printf ("man\n"); printf ("command_list\n"); printf ("exit\n"); printf ("clear\n"); printf ("\n"); break; case 12: exit_flag = 1; break; case 13: system ("clear"); break; } empty_buffer (arg1, sizeof (arg1)); empty_buffer (arg2, sizeof (arg2)); empty_buffer (arg3, sizeof (arg3)); empty_buffer (arg4, sizeof (arg4)); empty_buffer (arg5, sizeof (arg5)); empty_buffer (user_command, sizeof (user_command)); empty_buffer (user_input, sizeof (user_input)); empty_buffer (aux_buffer, sizeof (aux_buffer)); if (exit_flag == 1) break; } ipv4_route_table_free (ipv4_route_table_aux); return 1; }
/* Přeposílání jednotlivých zpráv na ethernet * * Funkce filtruje zprávy, které začínaji znakem #. * Každá přijatá zpráva ukončená znakem 0x0A se odesílá * jednotlivě se zpožděním kvůli synchronizaci přenosu. * Před každým odesláním packetu prečte data z paměti. * */ void ethernet_transmit(){ switch (state) { case WAIT_FOR_DATA: { while(!empty_buffer(&buf_uart)){ if(buf_uart.buffer[buf_uart.consumer] == FIRST_CHAR){ BUF_CONSUMER_PLUS1; state = WRONG_MESSAGE; break; } if(buf_uart.buffer[buf_uart.consumer] == LAST_CHAR){ BUF_CONSUMER_PLUS1; break; } state = RIGHT_MESSAGE; break; } break; } case WRONG_MESSAGE: { while(!empty_buffer(&buf_uart)){ if(buf_uart.buffer[buf_uart.consumer] != LAST_CHAR){ BUF_CONSUMER_PLUS1; } else{ BUF_CONSUMER_PLUS1; state = WAIT_FOR_DATA; break; } } break; } case RIGHT_MESSAGE: { while(!empty_buffer(&buf_uart)){ if(buf_uart.buffer[buf_uart.consumer] == FIRST_CHAR){ state = WRONG_MESSAGE; break; } if(buf_uart.buffer[buf_uart.consumer] != LAST_CHAR){ buffer[message_lenght] = buf_uart.buffer[buf_uart.consumer]; BUF_CONSUMER_PLUS1; message_lenght++; } else{ buffer[message_lenght] = buf_uart.buffer[buf_uart.consumer]; BUF_CONSUMER_PLUS1; message_lenght++; state = ETHERNET_TRANSMIT; break; } } break; } case ETHERNET_TRANSMIT: { /* Read all data from EEPROM page*/ Chip_EEPROM_Read(LPC_EEPROM, 0, PAGE_ADDR, eeprom_read, EEPROM_RWSIZE_8BITS, EEPROM_PAGE_SIZE); /* Destination address */ memcpy(destination_addr,(char*)eeprom_read + 2, sizeof(destination_addr)); /* Source address */ memcpy(source_addr,(char*)eeprom_read + 9, sizeof(source_addr)); /* Source ip */ memcpy(source_ip,(char*)eeprom_read + 16, sizeof(source_ip)); /* Destination ip */ memcpy(destination_ip,(char*)eeprom_read + 21, sizeof(destination_ip)); delayMs(3); UDP_packet_send((char*) buffer, message_lenght,destination_addr, source_addr,source_ip,destination_ip); memset(buffer,0,message_lenght); // Vynulování přeposlaných dat v bufferu message_lenght = 0; state = WAIT_FOR_DATA; break; } } }