int main (int argc, char **argv) { GError *error = NULL; GOptionContext *context = g_option_context_new ("ROLL_FORMAT_STRING\n"); g_option_context_set_summary (context, "Rolls some dice or shows the odds to get a successful roll.\n" "ROLL_FORMAT_STRING may be for example 1d10+2 or 3d6-1."); g_option_context_add_main_entries (context, entries, NULL); if (! g_option_context_parse (context, &argc, &argv, &error)) { g_print ("option parsing failed: %s\n", error->message); exit (1); } if (remaining_args == NULL) { char *help_str = g_option_context_get_help (context, TRUE, NULL); g_print ("%s", help_str); g_free (help_str); exit (2); } char *n_sides_str = NULL; char *modifier_str = NULL; char *roll_format_str = remaining_args[0]; char *n_dice_str = roll_format_str; char *ptr = roll_format_str; do { if (*ptr == 'd' || *ptr == 'D') { *ptr = '\0'; n_sides_str = ++ptr; } else if (*ptr == '+' || *ptr == '-') { modifier_str = ptr; } } while (*ptr++); int n_dice = atoi (n_dice_str); int n_sides = (n_sides_str) ? atoi (n_sides_str) : 0; int modifier = (modifier_str) ? atoi (modifier_str) : 0; int score = 0; // roll result, including modifiers int i; if (is_probability_mode) { /* For a roll result (which is 1-based) to be used as frequency array * index (which is 0-based), the 0 value needs to be taken into * account. It may in fact happen when using a negative modifier */ int max_score = n_dice * n_sides + modifier; int n_freq = max_score + 1; unsigned int *freq = calloc (n_freq, sizeof (unsigned int)); /* Enumerating all the possible rolls is like counting in the * "n_sides" base with "n_dice" digits. */ int n_rolls = pow (n_sides, n_dice); for (i = 0; i < n_rolls; i++) { score = 0; int roll = i; int die_index = n_dice; // for each die while (die_index-- > 0) { // get rightmost die value score += (roll % n_sides) + 1; // right-shift roll /= n_sides; } score += modifier; freq[score]++; } print_freq_table (freq, n_freq, n_rolls); free (freq); } else { /* Roll the dice */ Die *die = die_new (n_sides); for (i = 0; i < n_dice; i++) { score += die_roll (die); } score = MAX (0, score + modifier); printf ("%d\n", score); die_free (die); } return 0; }
int sfi_early_init(sfi_info_t *sfi_info) { int i; size_t syst_num; phys_addr_t low_frame; phys_addr_t high_frame; phys_addr_t syst_phys; memset(sfi_info, 0, sizeof(*sfi_info)); syst_phys = sfi_syst_phys(); if(syst_phys == -1) { return 0; } low_frame = high_frame = syst_phys; sfi_info->system_table = (sfi_system_table_t*) (((char*)map_virtual_page((syst_phys & 0xFFFFF000) | 3)) + (syst_phys & 0xFFF)); syst_num = SFI_NUM_SYST_ENTRIES(sfi_info->system_table); for(i = 0; i < syst_num; i++) { if(sfi_info->system_table->entries[i] > 0xFFFFFFFF) { panic("SFI table above 4G region"); } if(sfi_info->system_table->entries[i] < low_frame) { low_frame = sfi_info->system_table->entries[i]; } if(sfi_info->system_table->entries[i] > high_frame) { high_frame = sfi_info->system_table->entries[i]; } } sfi_info->low_frame = low_frame = low_frame & 0xFFFFF000; high_frame &= 0xFFFFF000; unmap_virtual_page(sfi_info->system_table); sfi_info->num_frames = ((high_frame - low_frame) / 0x1000) + 1; sfi_info->start_addr = map_contiguous_virtual_pages(low_frame | 3, sfi_info->num_frames); if(sfi_info->start_addr == NULL) { panic("Failed to map STI"); } sfi_info->system_table = (sfi_system_table_t*) SFI_PHYS_TO_VIRT(sfi_info, syst_phys); print_syst_table(sfi_info); sfi_info->cpus_table = get_sfi_table(sfi_info, SFI_SIG_CPUS); print_cpus_table(sfi_info); sfi_info->apic_table = get_sfi_table(sfi_info, SFI_SIG_APIC); print_apic_table(sfi_info); sfi_info->mmap_table = get_sfi_table(sfi_info, SFI_SIG_MMAP); print_mmap_table(sfi_info); sfi_info->freq_table = get_sfi_table(sfi_info, SFI_SIG_FREQ); print_freq_table(sfi_info); sfi_info->mtmr_table = get_sfi_table(sfi_info, SFI_SIG_MTMR); print_mtmr_table(sfi_info); sfi_info->mrtc_table = get_sfi_table(sfi_info, SFI_SIG_MRTC); print_mrtc_table(sfi_info); sfi_info->wake_table = get_sfi_table(sfi_info, SFI_SIG_WAKE); print_wake_table(sfi_info); sfi_info->devs_table = get_sfi_table(sfi_info, SFI_SIG_DEVS); print_devs_table(sfi_info); sfi_info->gpio_table = get_sfi_table(sfi_info, SFI_SIG_GPIO); print_gpio_table(sfi_info); sfi_info->xsdt_table = get_sfi_table(sfi_info, SFI_SIG_XSDT); print_xsdt_table(sfi_info); return 0; }