From 9da30781ed95f1bd736fb0b8b3ba3a73ee76bb3d Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 31 Mar 2012 11:45:35 -0500 Subject: [PATCH] Update: add support for repeated parameters Thus, something like my_function -s 0 -s 1 would be able to process both -s 0 and -s 1. Previously, only the last -s option would have been processed. --- level_0/f_console/c/console.h | 4 +- level_1/fl_console/c/console.c | 81 +++++++++++++++++++++- level_3/firewall/c/firewall.c | 16 +++-- .../fss_basic_list_read/c/fss_basic_list_read.c | 20 ++++-- level_3/fss_basic_read/c/fss_basic_read.c | 16 +++-- level_3/fss_extended_read/c/fss_extended_read.c | 18 +++-- level_3/fss_return_code/c/fss_return_code.c | 8 ++- level_3/return_code/c/return_code.c | 8 ++- 8 files changed, 141 insertions(+), 30 deletions(-) diff --git a/level_0/f_console/c/console.h b/level_0/f_console/c/console.h index 96f33e6..b12210f 100644 --- a/level_0/f_console/c/console.h +++ b/level_0/f_console/c/console.h @@ -99,12 +99,12 @@ extern "C"{ const f_bool has_additional; const f_u_int type; f_u_int result; - f_string_length additional; + f_string_lengths additional; f_string_length length; } f_console_parameter; #define f_console_parameter_initialize(symbol_short, symbol_long, symbol_extra, symbol_other, has_additional, type_value, length) \ - { symbol_short, symbol_long, symbol_extra, symbol_other, has_additional, type_value, f_console_result_none, 0, length } + { symbol_short, symbol_long, symbol_extra, symbol_other, has_additional, type_value, f_console_result_none, f_string_lengths_initialize, length } #endif // _di_f_console_parameter_ #ifndef _di_f_console_identify_ diff --git a/level_1/fl_console/c/console.c b/level_1/fl_console/c/console.c index e89fc49..69fdf30 100644 --- a/level_1/fl_console/c/console.c +++ b/level_1/fl_console/c/console.c @@ -65,6 +65,13 @@ extern "C"{ f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); if (f_macro_test_for_allocation_errors(allocation_status)) { + f_string_length i = 0; + + while (i < total_parameters) { + f_delete_string_lengths(status, parameters[i].additional); + i++; + } // while + f_delete_string_lengths(status, extra_initiator); return allocation_status; } @@ -86,6 +93,13 @@ extern "C"{ f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); if (f_macro_test_for_allocation_errors(allocation_status)) { + f_string_length i = 0; + + while (i < total_parameters) { + f_delete_string_lengths(status, parameters[i].additional); + i++; + } // while + f_delete_string_lengths(status, extra_initiator); return allocation_status; } @@ -116,7 +130,15 @@ extern "C"{ f_status allocation_status = f_status_initialize; f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); + if (f_macro_test_for_allocation_errors(allocation_status)) { + f_string_length i = 0; + + while (i < total_parameters) { + f_delete_string_lengths(status, parameters[i].additional); + i++; + } // while + f_delete_string_lengths(status, extra_initiator); return allocation_status; } @@ -137,7 +159,15 @@ extern "C"{ f_status allocation_status = f_status_initialize; f_resize_string_lengths(allocation_status, extra_initiator, extra_initiator.size + f_console_default_allocation_step); + if (f_macro_test_for_allocation_errors(allocation_status)) { + f_string_length i = 0; + + while (i < total_parameters) { + f_delete_string_lengths(status, parameters[i].additional); + i++; + } // while + f_delete_string_lengths(status, extra_initiator); return allocation_status; } @@ -161,10 +191,29 @@ extern "C"{ if (parameters[parameter_counter].type == f_console_type_other) { if (parameters[parameter_counter].length > 0 && parameters[parameter_counter].symbol_other != 0) { if (strncmp(argv[location], parameters[parameter_counter].symbol_other, parameters[parameter_counter].length + 1) == 0) { + f_status allocation_status = f_status_initialize; + + if (parameters[parameter_counter].additional.used >= parameters[parameter_counter].additional.size) { + f_resize_string_lengths(allocation_status, parameters[parameter_counter].additional, parameters[parameter_counter].additional.size + f_console_default_allocation_step); + } + + if (f_macro_test_for_allocation_errors(allocation_status)) { + f_string_length i = 0; + + while (i < total_parameters) { + f_delete_string_lengths(status, parameters[i].additional); + i++; + } // while + + f_delete_string_lengths(status, extra_initiator); + return allocation_status; + } + parameters[parameter_counter].result = f_console_result_found; // when "other" is supplied, the extra will be recycled to represent the location of the "other" such that ordering can be determined by the caller - parameters[parameter_counter].additional = location; + parameters[parameter_counter].additional.array[parameters[parameter_counter].additional.used] = location; + parameters[parameter_counter].additional.used++; found = f_true; break; @@ -175,8 +224,27 @@ extern "C"{ if (!found) { if (extra_initiator.used > 0) { - parameters[extra_initiator.array[0]].result = f_console_result_additional; - parameters[extra_initiator.array[0]].additional = location; + f_status allocation_status = f_status_initialize; + + if (parameters[extra_initiator.array[0]].additional.used >= parameters[extra_initiator.array[0]].additional.size) { + f_resize_string_lengths(allocation_status, parameters[extra_initiator.array[0]].additional, parameters[extra_initiator.array[0]].additional.size + f_console_default_allocation_step); + } + + if (f_macro_test_for_allocation_errors(allocation_status)) { + f_string_length i = 0; + + while (i < total_parameters) { + f_delete_string_lengths(status, parameters[i].additional); + i++; + } // while + + f_delete_string_lengths(status, extra_initiator); + return allocation_status; + } + + parameters[extra_initiator.array[0]].result = f_console_result_additional; + parameters[extra_initiator.array[0]].additional.array[parameters[extra_initiator.array[0]].additional.used] = location; + parameters[extra_initiator.array[0]].additional.used++; extra_initiator.used--; @@ -192,6 +260,13 @@ extern "C"{ f_resize_string_lengths(allocation_status, (*remaining), remaining->size + f_console_default_allocation_step); if (f_macro_test_for_allocation_errors(allocation_status)) { + f_string_length i = 0; + + while (i < total_parameters) { + f_delete_string_lengths(status, parameters[i].additional); + i++; + } // while + f_delete_string_lengths(status, extra_initiator); return allocation_status; } diff --git a/level_3/firewall/c/firewall.c b/level_3/firewall/c/firewall.c index 1e1708e..b871792 100644 --- a/level_3/firewall/c/firewall.c +++ b/level_3/firewall/c/firewall.c @@ -162,7 +162,7 @@ extern "C"{ if (data->parameters[firewall_parameter_command_stop].result == f_console_result_found) { if (found_command) { - if (data->parameters[command].additional > data->parameters[firewall_parameter_command_stop].additional) { + if (data->parameters[command].additional.array[0] > data->parameters[firewall_parameter_command_stop].additional.array[0]) { command = firewall_parameter_command_stop; } } else { @@ -173,7 +173,7 @@ extern "C"{ if (data->parameters[firewall_parameter_command_restart].result == f_console_result_found) { if (found_command) { - if (data->parameters[command].additional > data->parameters[firewall_parameter_command_restart].additional) { + if (data->parameters[command].additional.array[0] > data->parameters[firewall_parameter_command_restart].additional.array[0]) { command = firewall_parameter_command_restart; } } else { @@ -184,7 +184,7 @@ extern "C"{ if (data->parameters[firewall_parameter_command_lock].result == f_console_result_found) { if (found_command) { - if (data->parameters[command].additional > data->parameters[firewall_parameter_command_lock].additional) { + if (data->parameters[command].additional.array[0] > data->parameters[firewall_parameter_command_lock].additional.array[0]) { command = firewall_parameter_command_lock; } } else { @@ -195,7 +195,7 @@ extern "C"{ if (data->parameters[firewall_parameter_command_show].result == f_console_result_found) { if (found_command) { - if (data->parameters[command].additional > data->parameters[firewall_parameter_command_show].additional) { + if (data->parameters[command].additional.array[0] > data->parameters[firewall_parameter_command_show].additional.array[0]) { command = firewall_parameter_command_show; } } else { @@ -1629,7 +1629,13 @@ extern "C"{ #ifndef _di_firewall_delete_data_ f_return_status firewall_delete_data(firewall_data *data) { - f_status status = f_status_initialize; + f_status status = f_status_initialize; + f_string_length i = 0; + + while (i < firewall_total_parameters) { + f_delete_string_lengths(status, data->parameters[i].additional); + i++; + } // while f_delete_fss_contents(status, data->contents); f_delete_fss_objects(status, data->objects); diff --git a/level_3/fss_basic_list_read/c/fss_basic_list_read.c b/level_3/fss_basic_list_read/c/fss_basic_list_read.c index 9954a4a..f3b0766 100644 --- a/level_3/fss_basic_list_read/c/fss_basic_list_read.c +++ b/level_3/fss_basic_list_read/c/fss_basic_list_read.c @@ -179,7 +179,7 @@ extern "C"{ f_string_length original_size = data->file_position.total_elements; if (data->parameters[fss_basic_list_read_parameter_count].result == f_console_result_additional) { - target = (f_string_length) atoll(argv[data->parameters[fss_basic_list_read_parameter_count].additional]); + target = (f_string_length) atoll(argv[data->parameters[fss_basic_list_read_parameter_count].additional.array[0]]); } if (data->process_pipe) { @@ -374,7 +374,7 @@ extern "C"{ if (data->contents.array[current].used > 0) { f_string_length counter = data->contents.array[current].array[0].start; f_string_length position = 0; - f_string_length target = (f_string_length) atoll(argv[data->parameters[fss_basic_list_read_parameter_line].additional]); + f_string_length target = (f_string_length) atoll(argv[data->parameters[fss_basic_list_read_parameter_line].additional.array[0]]); f_string_location range = f_string_location_initialize; // use an invalid range to communicate range not found @@ -445,14 +445,14 @@ extern "C"{ f_string_length argv_length = f_string_length_initialize; if (data->parameters[fss_basic_list_read_parameter_name].result == f_console_result_additional) { - argv_length = strlen(argv[data->parameters[fss_basic_list_read_parameter_name].additional]); + argv_length = strlen(argv[data->parameters[fss_basic_list_read_parameter_name].additional.array[0]]); if (data->parameters[fss_basic_list_read_parameter_object].result == f_console_result_none) { for (; current < data->objects.used; current++) { name_length = data->objects.array[current].stop - data->objects.array[current].start + 1; if (name_length == argv_length) { - if (fl_compare_strings(data->buffer.string + data->objects.array[current].start, argv[data->parameters[fss_basic_list_read_parameter_name].additional], name_length, argv_length) == f_equal_to) { + if (fl_compare_strings(data->buffer.string + data->objects.array[current].start, argv[data->parameters[fss_basic_list_read_parameter_name].additional.array[0]], name_length, argv_length) == f_equal_to) { if (data->parameters[fss_basic_list_read_parameter_size].result == f_console_result_found) { if (data->contents.array[current].used > 0) { @@ -474,7 +474,7 @@ extern "C"{ if (data->contents.array[current].used > 0) { f_string_length counter = data->contents.array[current].array[0].start; f_string_length position = 0; - f_string_length target = (f_string_length) atoll(argv[data->parameters[fss_basic_list_read_parameter_line].additional]); + f_string_length target = (f_string_length) atoll(argv[data->parameters[fss_basic_list_read_parameter_line].additional.array[0]]); f_string_location range = f_string_location_initialize; // use an invalid range to communicate range not found @@ -540,7 +540,7 @@ extern "C"{ name_length = data->contents.array[current].array[0].stop - data->contents.array[current].array[0].start + 1; if (name_length == argv_length) { - if (fl_compare_strings(data->buffer.string + data->contents.array[current].array[0].start, argv[data->parameters[fss_basic_list_read_parameter_name].additional], name_length, argv_length) == f_equal_to) { + if (fl_compare_strings(data->buffer.string + data->contents.array[current].array[0].start, argv[data->parameters[fss_basic_list_read_parameter_name].additional.array[0]], name_length, argv_length) == f_equal_to) { if (data->parameters[fss_basic_list_read_parameter_count].result == f_console_result_none || (data->parameters[fss_basic_list_read_parameter_count].result == f_console_result_additional && found == target)) { f_print_partial_dynamic_string(f_standard_output, data->buffer, data->objects.array[current]); fprintf(f_standard_output, "\n"); @@ -568,7 +568,13 @@ extern "C"{ #ifndef _di_fss_basic_list_read_delete_data_ f_return_status fss_basic_list_read_delete_data(fss_basic_list_read_data *data) { - f_status status = f_status_initialize; + f_status status = f_status_initialize; + f_string_length i = 0; + + while (i < fss_basic_list_read_total_parameters) { + f_delete_string_lengths(status, data->parameters[i].additional); + i++; + } // while f_delete_fss_contents(status, data->contents); f_delete_fss_objects(status, data->objects); diff --git a/level_3/fss_basic_read/c/fss_basic_read.c b/level_3/fss_basic_read/c/fss_basic_read.c index 73c069c..1b2e07a 100644 --- a/level_3/fss_basic_read/c/fss_basic_read.c +++ b/level_3/fss_basic_read/c/fss_basic_read.c @@ -165,7 +165,7 @@ extern "C"{ f_string_length original_size = data->file_position.total_elements; if (data->parameters[fss_basic_read_parameter_count].result == f_console_result_additional) { - target = (f_string_length) atoll(argv[data->parameters[fss_basic_read_parameter_count].additional]); + target = (f_string_length) atoll(argv[data->parameters[fss_basic_read_parameter_count].additional.array[0]]); } if (data->process_pipe) { @@ -386,14 +386,14 @@ extern "C"{ f_string_length argv_length = f_string_length_initialize; if (data->parameters[fss_basic_read_parameter_name].result == f_console_result_additional) { - argv_length = strlen(argv[data->parameters[fss_basic_read_parameter_name].additional]); + argv_length = strlen(argv[data->parameters[fss_basic_read_parameter_name].additional.array[0]]); if (data->parameters[fss_basic_read_parameter_object].result == f_console_result_none) { for (; current < data->objects.used; current++) { name_length = data->objects.array[current].stop - data->objects.array[current].start + 1; if (name_length == argv_length) { - if (fl_compare_strings(data->buffer.string + data->objects.array[current].start, argv[data->parameters[fss_basic_read_parameter_name].additional], name_length, argv_length) == f_equal_to) { + if (fl_compare_strings(data->buffer.string + data->objects.array[current].start, argv[data->parameters[fss_basic_read_parameter_name].additional.array[0]], name_length, argv_length) == f_equal_to) { if (data->parameters[fss_basic_read_parameter_count].result == f_console_result_none || (data->parameters[fss_basic_read_parameter_count].result == f_console_result_additional && found == target)) { if (data->parameters[fss_basic_read_parameter_total].result == f_console_result_found) { @@ -431,7 +431,7 @@ extern "C"{ name_length = data->contents.array[current].array[0].stop - data->contents.array[current].array[0].start + 1; if (name_length == argv_length) { - if (fl_compare_strings(data->buffer.string + data->contents.array[current].array[0].start, argv[data->parameters[fss_basic_read_parameter_name].additional], name_length, argv_length) == f_equal_to) { + if (fl_compare_strings(data->buffer.string + data->contents.array[current].array[0].start, argv[data->parameters[fss_basic_read_parameter_name].additional.array[0]], name_length, argv_length) == f_equal_to) { if (data->parameters[fss_basic_read_parameter_count].result == f_console_result_none || (data->parameters[fss_basic_read_parameter_count].result == f_console_result_additional && found == target)) { f_print_partial_dynamic_string(f_standard_output, data->buffer, data->objects.array[current]); fprintf(f_standard_output, "\n"); @@ -459,7 +459,13 @@ extern "C"{ #ifndef _di_fss_basic_read_delete_data_ f_return_status fss_basic_read_delete_data(fss_basic_read_data *data) { - f_status status = f_status_initialize; + f_status status = f_status_initialize; + f_string_length i = 0; + + while (i < fss_basic_read_total_parameters) { + f_delete_string_lengths(status, data->parameters[i].additional); + i++; + } // while f_delete_fss_contents(status, data->contents); f_delete_fss_objects(status, data->objects); diff --git a/level_3/fss_extended_read/c/fss_extended_read.c b/level_3/fss_extended_read/c/fss_extended_read.c index 7d4fbf4..af2782d 100644 --- a/level_3/fss_extended_read/c/fss_extended_read.c +++ b/level_3/fss_extended_read/c/fss_extended_read.c @@ -173,11 +173,11 @@ extern "C"{ f_string_length original_size = data->file_position.total_elements; if (data->parameters[fss_extended_read_parameter_count].result == f_console_result_additional) { - target = (f_string_length) atoll(argv[data->parameters[fss_extended_read_parameter_count].additional]); + target = (f_string_length) atoll(argv[data->parameters[fss_extended_read_parameter_count].additional.array[0]]); } if (data->parameters[fss_extended_read_parameter_select].result == f_console_result_additional) { - select = (f_string_length) atoll(argv[data->parameters[fss_extended_read_parameter_select].additional]); + select = (f_string_length) atoll(argv[data->parameters[fss_extended_read_parameter_select].additional.array[0]]); } if (data->process_pipe) { @@ -393,14 +393,14 @@ extern "C"{ f_string_length argv_length = f_string_length_initialize; if (data->parameters[fss_extended_read_parameter_name].result == f_console_result_additional) { - argv_length = strlen(argv[data->parameters[fss_extended_read_parameter_name].additional]); + argv_length = strlen(argv[data->parameters[fss_extended_read_parameter_name].additional.array[0]]); if (data->parameters[fss_extended_read_parameter_object].result == f_console_result_none) { for (; current < data->objects.used; current++) { name_length = data->objects.array[current].stop - data->objects.array[current].start + 1; if (name_length == argv_length) { - if (fl_compare_strings(data->buffer.string + data->objects.array[current].start, argv[data->parameters[fss_extended_read_parameter_name].additional], name_length, argv_length) == f_equal_to) { + if (fl_compare_strings(data->buffer.string + data->objects.array[current].start, argv[data->parameters[fss_extended_read_parameter_name].additional.array[0]], name_length, argv_length) == f_equal_to) { if (data->parameters[fss_extended_read_parameter_count].result == f_console_result_none || (data->parameters[fss_extended_read_parameter_count].result == f_console_result_additional && found == target)) { if (data->parameters[fss_extended_read_parameter_total].result == f_console_result_found) { @@ -438,7 +438,7 @@ extern "C"{ name_length = data->contents.array[current].array[select].stop - data->contents.array[current].array[select].start + 1; if (name_length == argv_length) { - if (fl_compare_strings(data->buffer.string + data->contents.array[current].array[select].start, argv[data->parameters[fss_extended_read_parameter_name].additional], name_length, argv_length) == f_equal_to) { + if (fl_compare_strings(data->buffer.string + data->contents.array[current].array[select].start, argv[data->parameters[fss_extended_read_parameter_name].additional.array[0]], name_length, argv_length) == f_equal_to) { if (data->parameters[fss_extended_read_parameter_count].result == f_console_result_none || (data->parameters[fss_extended_read_parameter_count].result == f_console_result_additional && found == target)) { f_print_partial_dynamic_string(f_standard_output, data->buffer, data->objects.array[current]); fprintf(f_standard_output, "\n"); @@ -466,7 +466,13 @@ extern "C"{ #ifndef _di_fss_extended_read_delete_data_ f_return_status fss_extended_read_delete_data(fss_extended_read_data *data) { - f_status status = f_status_initialize; + f_status status = f_status_initialize; + f_string_length i = 0; + + while (i < fss_extended_read_total_parameters) { + f_delete_string_lengths(status, data->parameters[i].additional); + i++; + } // while f_delete_fss_contents(status, data->contents); f_delete_fss_objects(status, data->objects); diff --git a/level_3/fss_return_code/c/fss_return_code.c b/level_3/fss_return_code/c/fss_return_code.c index b1ce725..9992191 100644 --- a/level_3/fss_return_code/c/fss_return_code.c +++ b/level_3/fss_return_code/c/fss_return_code.c @@ -228,7 +228,13 @@ extern "C"{ #ifndef _di_fss_return_code_delete_data_ f_return_status fss_return_code_delete_data(fss_return_code_data *data) { - f_status status = f_status_initialize; + f_status status = f_status_initialize; + f_string_length i = 0; + + while (i < fss_return_code_total_parameters) { + f_delete_string_lengths(status, data->parameters[i].additional); + i++; + } // while f_delete_string_lengths(status, data->remaining); fll_delete_color_context(status, data->context); diff --git a/level_3/return_code/c/return_code.c b/level_3/return_code/c/return_code.c index 0a7ee86..8ed9ca9 100644 --- a/level_3/return_code/c/return_code.c +++ b/level_3/return_code/c/return_code.c @@ -228,7 +228,13 @@ extern "C"{ #ifndef _di_return_code_delete_data_ f_return_status return_code_delete_data(return_code_data *data) { - f_status status = f_status_initialize; + f_status status = f_status_initialize; + f_string_length i = 0; + + while (i < return_code_total_parameters) { + f_delete_string_lengths(status, data->parameters[i].additional); + i++; + } // while f_delete_string_lengths(status, data->remaining); fll_delete_color_context(status, data->context); -- 1.8.3.1