From 28d61873962f0cefc192f32942e891ec4826df84 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 10:52:51 -0600 Subject: [PATCH 01/16] Added test that set_variable_values_ini does not error when keys are not present in promised section Ticket: CFE-3866 Changelog: None --- ..._error_when_keys_in_section_not_present.cf | 35 ++++++++++ ...or_when_keys_in_section_not_present.cf.sub | 70 +++++++++++++++++++ ..._keys_in_section_not_present.cf.sub.finish | 5 ++ ...n_keys_in_section_not_present.cf.sub.start | 4 ++ 4 files changed, 114 insertions(+) create mode 100644 tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf create mode 100644 tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub create mode 100644 tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub.finish create mode 100644 tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub.start diff --git a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf new file mode 100644 index 0000000000..2d0eb27c51 --- /dev/null +++ b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf @@ -0,0 +1,35 @@ +####################################################### +# +# Test bundle set_config_values_ini +# +####################################################### + +body common control +{ + inputs => { '../../default.cf.sub' }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} + +####################################################### + + +bundle agent test +{ + meta: + "description" -> { "CFE-3866" } + string => "Test that set_variable_values_ini does not error when promised key is not present"; + +} + +####################################################### + +bundle agent check +{ + + methods: + + "Pass/FAIL" + usebundle => dcs_passif_output(".*", ".*error.*", "$(sys.cf_agent) -Kf $(this.promise_filename).sub --define AUTO", $(this.promise_filename)); + +} diff --git a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub new file mode 100644 index 0000000000..0c291b0b7b --- /dev/null +++ b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub @@ -0,0 +1,70 @@ +####################################################### +# +# Test bundle set_config_values_ini +# +####################################################### + +body common control +{ + inputs => { '../../default.cf.sub' }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} + +####################################################### + +bundle agent init +{ + files: + + "$(G.testfile).actual" + copy_from => local_cp("$(this.promise_filename).start"); + + + "$(G.testfile).expected" + copy_from => local_cp("$(this.promise_filename).finish"); + +} + +####################################################### + +bundle agent test +{ + meta: + "description" -> { "CFE-3866" } + string => "Test that set_variable_values_ini does not error when promised key is not present"; + + vars: + "config[section 1][present_at_start]" string => "1"; + "config[section 1][absent_at_start]" string => "1"; + "config[section 2][present_at_start]" string => "two"; + "sections" slist => getindices( config ); + + files: + + "$(G.testfile).actual" + edit_line => set_variable_values_ini("test.config", "$(sections)"); + +} + +####################################################### + +bundle agent check +{ + + methods: + + "check" + usebundle => dcs_if_diff( "$(G.testfile).actual", "$(G.testfile).expected", + "pass", "_fail"); + + # Fail the test if any of the files fail. + "fail" + usebundle => dcs_fail( $(this.promise_filename) ), + if => "_fail"; + + pass:: + "pass" + usebundle => dcs_pass( $(this.promise_filename) ); + +} diff --git a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub.finish b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub.finish new file mode 100644 index 0000000000..49c619c8ff --- /dev/null +++ b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub.finish @@ -0,0 +1,5 @@ +[section 1] +present_at_start=1 +absent_at_start=1 +[section 2] +present_at_start=two diff --git a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub.start b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub.start new file mode 100644 index 0000000000..f2fd73a17c --- /dev/null +++ b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub.start @@ -0,0 +1,4 @@ +[section 1] +present_at_start=1 +[section 2] +present_at_start=two From af36b6b9db2287d9204f3f7096f6e6ec910c48dc Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 13:34:53 -0600 Subject: [PATCH 02/16] Avoided trying to set field before resolving section presence Previously set_variable_values_ini would try to resolve field_edits before first ensuring that the section of the ini being promised was resolved. This results in errors when there are no fields to edit. This change uses bundle scoped classes to guard field_edits from actuating until after the promise ensuring the section itself exists has been reached, effectively preventing the errors from being emitted. Ticket: CFE-3867 Changelog: Commit --- lib/files.cf | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index c61af73ce8..d64efd828a 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -446,23 +446,24 @@ bundle edit_line set_variable_values_ini(tab, sectionName) "#+\s*$(index)\s*=.*" select_region => INI_section(escape("$(sectionName)")), edit_field => col("\s*=\s*","1","$(index)","set"), - if => "edit_$(cindex[$(index)])"; + if => and("_section_$(section_name)_present_reached", + "edit_$(cindex[$(index)])"); # match a line starting like the key something "\s*$(index)\s*=.*" edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"), select_region => INI_section(escape("$(sectionName)")), - classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"), - if => "edit_$(cindex[$(index)])"; - + if => and("_section_$(section_name)_present_reached", + "edit_$(cindex[$(index)])"); insert_lines: "[$(sectionName)]" location => start, - comment => "Insert lines"; + comment => "Insert lines", + classes => results( "bundle", "_section_$(section_name)_present" ); "$(index)=$($(tab)[$(sectionName)][$(index)])" select_region => INI_section(escape("$(sectionName)")), - if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])"; + if => "edit_$(cindex[$(index)])"; } From 08bc5944a2227c05a847c0eedc84dd01b11e91ef Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 13:44:57 -0600 Subject: [PATCH 03/16] Tweak --- ...able_values_ini_no_error_when_keys_in_section_not_present.cf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf index 2d0eb27c51..cccc1e9af4 100644 --- a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf +++ b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf @@ -30,6 +30,6 @@ bundle agent check methods: "Pass/FAIL" - usebundle => dcs_passif_output(".*", ".*error.*", "$(sys.cf_agent) -Kf $(this.promise_filename).sub --define AUTO", $(this.promise_filename)); + usebundle => dcs_passif_output(".*$(this.promise_filename).sub Pass", ".*error.*", "$(sys.cf_agent) -Kf $(this.promise_filename).sub --define AUTO,DEBUG,EXTRA", $(this.promise_filename)); } From 17053817017015412ba40030d54654b85881cd1c Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 13:56:56 -0600 Subject: [PATCH 04/16] canonify when checking if section reached section can include characters that are not valid classes are canonified automatically on definition, but we gotta do that for checking --- lib/files.cf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index d64efd828a..b333baa57c 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -446,14 +446,14 @@ bundle edit_line set_variable_values_ini(tab, sectionName) "#+\s*$(index)\s*=.*" select_region => INI_section(escape("$(sectionName)")), edit_field => col("\s*=\s*","1","$(index)","set"), - if => and("_section_$(section_name)_present_reached", + if => and(canonify("_section_$(section_name)_present_reached"), "edit_$(cindex[$(index)])"); # match a line starting like the key something "\s*$(index)\s*=.*" edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"), select_region => INI_section(escape("$(sectionName)")), - if => and("_section_$(section_name)_present_reached", + if => and(canonify("_section_$(section_name)_present_reached"), "edit_$(cindex[$(index)])"); insert_lines: "[$(sectionName)]" From 7264abac4f68baabc85e0f56d768eb2c815fe331 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 14:41:58 -0600 Subject: [PATCH 05/16] Avoid promising 2 sections in the test --- ...e_values_ini_no_error_when_keys_in_section_not_present.cf.sub | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub index 0c291b0b7b..dfa9235ad5 100644 --- a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub +++ b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf.sub @@ -37,7 +37,6 @@ bundle agent test vars: "config[section 1][present_at_start]" string => "1"; "config[section 1][absent_at_start]" string => "1"; - "config[section 2][present_at_start]" string => "two"; "sections" slist => getindices( config ); files: From 6b862f571a26932126b1a1a03419e12a0d1880f8 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 15:05:47 -0600 Subject: [PATCH 06/16] tweak test --- ...able_values_ini_no_error_when_keys_in_section_not_present.cf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf index cccc1e9af4..ad5261a6da 100644 --- a/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf +++ b/tests/acceptance/lib/files/set_variable_values_ini_no_error_when_keys_in_section_not_present.cf @@ -30,6 +30,6 @@ bundle agent check methods: "Pass/FAIL" - usebundle => dcs_passif_output(".*$(this.promise_filename).sub Pass", ".*error.*", "$(sys.cf_agent) -Kf $(this.promise_filename).sub --define AUTO,DEBUG,EXTRA", $(this.promise_filename)); + usebundle => dcs_passif_output(".*$(this.promise_filename).sub Pass.*", ".*error.*", "$(sys.cf_agent) -Kf $(this.promise_filename).sub --define AUTO,DEBUG,EXTRA", $(this.promise_filename)); } From f5dc8bacb3db9e6c12bf0cf8810f65e05699f9a9 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 15:17:06 -0600 Subject: [PATCH 07/16] restore classes already in use --- lib/files.cf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/files.cf b/lib/files.cf index b333baa57c..c24fd50e38 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -453,6 +453,7 @@ bundle edit_line set_variable_values_ini(tab, sectionName) "\s*$(index)\s*=.*" edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"), select_region => INI_section(escape("$(sectionName)")), + classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"), if => and(canonify("_section_$(section_name)_present_reached"), "edit_$(cindex[$(index)])"); insert_lines: @@ -463,7 +464,7 @@ bundle edit_line set_variable_values_ini(tab, sectionName) "$(index)=$($(tab)[$(sectionName)][$(index)])" select_region => INI_section(escape("$(sectionName)")), - if => "edit_$(cindex[$(index)])"; + if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])"; } From f25bfb00057231e4bc312f81604b3728bdd9cd71 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 15:18:38 -0600 Subject: [PATCH 08/16] restore previous indentation for promise attr --- lib/files.cf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/files.cf b/lib/files.cf index c24fd50e38..98f55f8509 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -464,7 +464,7 @@ bundle edit_line set_variable_values_ini(tab, sectionName) "$(index)=$($(tab)[$(sectionName)][$(index)])" select_region => INI_section(escape("$(sectionName)")), - if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])"; + if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])"; } From 0271fb47f133f3b952e957b5215d9bcdde321741 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 15:19:50 -0600 Subject: [PATCH 09/16] use the right variable for section name! --- lib/files.cf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index 98f55f8509..e0f717e517 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -446,7 +446,7 @@ bundle edit_line set_variable_values_ini(tab, sectionName) "#+\s*$(index)\s*=.*" select_region => INI_section(escape("$(sectionName)")), edit_field => col("\s*=\s*","1","$(index)","set"), - if => and(canonify("_section_$(section_name)_present_reached"), + if => and(canonify("_section_$(sectionName)_present_reached"), "edit_$(cindex[$(index)])"); # match a line starting like the key something @@ -454,13 +454,13 @@ bundle edit_line set_variable_values_ini(tab, sectionName) edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"), select_region => INI_section(escape("$(sectionName)")), classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"), - if => and(canonify("_section_$(section_name)_present_reached"), + if => and(canonify("_section_$(sectionName)_present_reached"), "edit_$(cindex[$(index)])"); insert_lines: "[$(sectionName)]" location => start, comment => "Insert lines", - classes => results( "bundle", "_section_$(section_name)_present" ); + classes => results( "bundle", "_section_$(sectionName)_present" ); "$(index)=$($(tab)[$(sectionName)][$(index)])" select_region => INI_section(escape("$(sectionName)")), From 7aa0d74db98d4defb39e205f49d1d1fda5ce7cc8 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Thu, 11 Nov 2021 15:45:32 -0600 Subject: [PATCH 10/16] disabled uncomment support, removed unused class conditions --- lib/files.cf | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index e0f717e517..b7e6013d71 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -442,18 +442,23 @@ bundle edit_line set_variable_values_ini(tab, sectionName) field_edits: - # If the line is there, but commented out, first uncomment it - "#+\s*$(index)\s*=.*" - select_region => INI_section(escape("$(sectionName)")), - edit_field => col("\s*=\s*","1","$(index)","set"), - if => and(canonify("_section_$(sectionName)_present_reached"), - "edit_$(cindex[$(index)])"); + # This is emitting an /error/ when no commented out managed key line is + # found within the selected region. The missing keys is completely + # expected in the case of promising a new file or section. How can this be + # guarded against executing? The only thought I have is ... + # if => regcmp( "some regex I dunno how to craft", readfile( $(edit.filename) ) + + # # If the line is there, but commented out, first uncomment it + # "#+\s*$(index)\s*=.*" + # select_region => INI_section(escape("$(sectionName)")), + # edit_field => col("\s*=\s*","1","$(index)","set"), + # if => and(canonify("_section_$(sectionName)_present_reached"), + # "edit_$(cindex[$(index)])"); # match a line starting like the key something "\s*$(index)\s*=.*" edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"), select_region => INI_section(escape("$(sectionName)")), - classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"), if => and(canonify("_section_$(sectionName)_present_reached"), "edit_$(cindex[$(index)])"); insert_lines: @@ -464,7 +469,7 @@ bundle edit_line set_variable_values_ini(tab, sectionName) "$(index)=$($(tab)[$(sectionName)][$(index)])" select_region => INI_section(escape("$(sectionName)")), - if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])"; + if => "edit_$(cindex[$(index)])"; } From 7c8413ab6f6da1c05cc06f108e9f7872dff1ea87 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Fri, 12 Nov 2021 11:38:14 -0600 Subject: [PATCH 11/16] ? --- lib/files.cf | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index b7e6013d71..66c6e64e16 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -442,18 +442,15 @@ bundle edit_line set_variable_values_ini(tab, sectionName) field_edits: - # This is emitting an /error/ when no commented out managed key line is - # found within the selected region. The missing keys is completely - # expected in the case of promising a new file or section. How can this be - # guarded against executing? The only thought I have is ... - # if => regcmp( "some regex I dunno how to craft", readfile( $(edit.filename) ) - # # If the line is there, but commented out, first uncomment it - # "#+\s*$(index)\s*=.*" - # select_region => INI_section(escape("$(sectionName)")), - # edit_field => col("\s*=\s*","1","$(index)","set"), - # if => and(canonify("_section_$(sectionName)_present_reached"), - # "edit_$(cindex[$(index)])"); + "#+\s*$(index)\s*=.*" + select_region => INI_section(escape("$(sectionName)")), + edit_field => col("\s*=\s*","1","$(index)","set"), + if => and( canonify("_section_$(sectionName)_present_reached"), + "edit_$(cindex[$(index)])", + regcmp( "\[$(sectionName)\](?s:.)#\s+$(index)\s+=\s+[^\[]*", + readfile( $(edit.filename) ))); + # match a line starting like the key something "\s*$(index)\s*=.*" From a75ccb7ab9d7628b73670f9b15140ecc8191345b Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Fri, 12 Nov 2021 11:39:47 -0600 Subject: [PATCH 12/16] ? --- lib/files.cf | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index 66c6e64e16..882a5e6794 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -442,15 +442,27 @@ bundle edit_line set_variable_values_ini(tab, sectionName) field_edits: + # This is emitting an /error/ when no commented out managed key line is + # found within the selected region. The missing keys is completely + # expected in the case of promising a new file or section. How can this be + # guarded against executing? The only thought I have is ... + # if => regcmp( "some regex I dunno how to craft", readfile( $(edit.filename) ) + # # If the line is there, but commented out, first uncomment it + # "#+\s*$(index)\s*=.*" + # select_region => INI_section(escape("$(sectionName)")), + # edit_field => col("\s*=\s*","1","$(index)","set"), + # if => and(canonify("_section_$(sectionName)_present_reached"), + # "edit_$(cindex[$(index)])"); + # If the line is there, but commented out, first uncomment it "#+\s*$(index)\s*=.*" - select_region => INI_section(escape("$(sectionName)")), - edit_field => col("\s*=\s*","1","$(index)","set"), - if => and( canonify("_section_$(sectionName)_present_reached"), - "edit_$(cindex[$(index)])", - regcmp( "\[$(sectionName)\](?s:.)#\s+$(index)\s+=\s+[^\[]*", - readfile( $(edit.filename) ))); - + select_region => INI_section(escape("$(sectionName)")), + edit_field => col("\s*=\s*","1","$(index)","set"), + if => and( canonify("_section_$(sectionName)_present_reached"), + "edit_$(cindex[$(index)])", + regcmp( "\[$(sectionName)\](?s:.)#\s+$(index)\s+=\s+[^\[]*", + readfile( $(edit.filename) ))); + # match a line starting like the key something "\s*$(index)\s*=.*" @@ -794,6 +806,7 @@ bundle edit_line set_line_based(v, sep, bp, kp, cp) # If the line is there with the wrong value, replace with # the correct value + # "^\s*(ucredit\s*=\s*(?!1$).*|ucredit)$" "^\s*($(i)$(bp)(?!$(ev[$(i)])$).*|$(i))$" comment => "Correct the value '$(i)'", replace_with => value("$(i)$(sep)$($(v)[$(i)])"), From 3204dbc89ead91c78a87248ef6f6981769e07db4 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Fri, 12 Nov 2021 13:41:01 -0600 Subject: [PATCH 13/16] moms spagetti --- lib/files.cf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index 882a5e6794..16a57ed40c 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -460,9 +460,9 @@ bundle edit_line set_variable_values_ini(tab, sectionName) edit_field => col("\s*=\s*","1","$(index)","set"), if => and( canonify("_section_$(sectionName)_present_reached"), "edit_$(cindex[$(index)])", - regcmp( "\[$(sectionName)\](?s:.)#\s+$(index)\s+=\s+[^\[]*", + regcmp( concat("\[", escape($(sectionName)), "\](?s:.)*^\s*#+\s*$(index)\s*=.*"), readfile( $(edit.filename) ))); - + # match a line starting like the key something "\s*$(index)\s*=.*" From d1a2cd704c36d92a917022390ada2aa24666e0b4 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Fri, 12 Nov 2021 14:58:09 -0600 Subject: [PATCH 14/16] ml generated regex ftw? --- lib/files.cf | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index 16a57ed40c..c2efcca6a6 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -460,10 +460,9 @@ bundle edit_line set_variable_values_ini(tab, sectionName) edit_field => col("\s*=\s*","1","$(index)","set"), if => and( canonify("_section_$(sectionName)_present_reached"), "edit_$(cindex[$(index)])", - regcmp( concat("\[", escape($(sectionName)), "\](?s:.)*^\s*#+\s*$(index)\s*=.*"), + regcmp( concat("\[", escape($(sectionName)), "\](?:[^#]#?+)+$(index)\s*=[^\[]++"), readfile( $(edit.filename) ))); - # match a line starting like the key something "\s*$(index)\s*=.*" edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"), From e80af2834afcfb407954cd308a906f900409cf7f Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Fri, 12 Nov 2021 16:29:45 -0600 Subject: [PATCH 15/16] craigs spagetti --- lib/files.cf | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index c2efcca6a6..7e56a4f6d1 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -447,7 +447,7 @@ bundle edit_line set_variable_values_ini(tab, sectionName) # expected in the case of promising a new file or section. How can this be # guarded against executing? The only thought I have is ... # if => regcmp( "some regex I dunno how to craft", readfile( $(edit.filename) ) - + # # If the line is there, but commented out, first uncomment it # "#+\s*$(index)\s*=.*" # select_region => INI_section(escape("$(sectionName)")), @@ -456,12 +456,15 @@ bundle edit_line set_variable_values_ini(tab, sectionName) # "edit_$(cindex[$(index)])"); # If the line is there, but commented out, first uncomment it "#+\s*$(index)\s*=.*" + if => and( + #canonify("_section_$(sectionName)_present_reached"), + #"edit_$(cindex[$(index)])", + # regcmp matches if the promised file contains [$(sectionName)] with a commented out $(index) + regcmp( concat("\[", escape($(sectionName)), "\]", + "[^#\[]*#\s*", escape($(index)), "\s*=.*"), + readfile( $(edit.filename) ))), select_region => INI_section(escape("$(sectionName)")), - edit_field => col("\s*=\s*","1","$(index)","set"), - if => and( canonify("_section_$(sectionName)_present_reached"), - "edit_$(cindex[$(index)])", - regcmp( concat("\[", escape($(sectionName)), "\](?:[^#]#?+)+$(index)\s*=[^\[]++"), - readfile( $(edit.filename) ))); + edit_field => col("\s*=\s*","1","$(index)","set"); # match a line starting like the key something "\s*$(index)\s*=.*" From 4c86690d5461a1d65671e330c738d0a48b5e1c49 Mon Sep 17 00:00:00 2001 From: Nick Anderson Date: Fri, 12 Nov 2021 16:48:09 -0600 Subject: [PATCH 16/16] pre-escape --- lib/files.cf | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/files.cf b/lib/files.cf index 7e56a4f6d1..6fb6cdff47 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -435,6 +435,8 @@ bundle edit_line set_variable_values_ini(tab, sectionName) # Be careful if the index string contains funny chars "cindex[$(index)]" string => canonify("$(index)"); + "_escaped_key[$(index)]" string => escape("$(index)"); + "_escaped_section[$(sectionName)]" string => escape("$(sectionName)"); classes: "edit_$(cindex[$(index)])" not => strcmp("$($(tab)[$(sectionName)][$(index)])","dontchange"), @@ -455,15 +457,15 @@ bundle edit_line set_variable_values_ini(tab, sectionName) # if => and(canonify("_section_$(sectionName)_present_reached"), # "edit_$(cindex[$(index)])"); # If the line is there, but commented out, first uncomment it - "#+\s*$(index)\s*=.*" + "#+\s*$(_escaped_key[$(index)])\s*=.*" if => and( - #canonify("_section_$(sectionName)_present_reached"), - #"edit_$(cindex[$(index)])", + #canonify("_section_$(sectionName)_present_reached"), # I don't think this helps because the class is based on the promise running on a buffer of the file, we are only checking the file as it exists before the promise finished + #"edit_$(cindex[$(index)])", # is it useful? why would you do not change? this bundle only asserts keys provided in dataset, providing a key with value donotchange is tantemount to not providing the key # regcmp matches if the promised file contains [$(sectionName)] with a commented out $(index) - regcmp( concat("\[", escape($(sectionName)), "\]", - "[^#\[]*#\s*", escape($(index)), "\s*=.*"), + regcmp( concat("\[$(_escaped_section[$(sectionName)])\]", + "[^#\[]*#\s*$(_escaped_key[$(index)])\s*=.*"), readfile( $(edit.filename) ))), - select_region => INI_section(escape("$(sectionName)")), + select_region => INI_section( "$(_escaped_section[$(sectionName)])" ), edit_field => col("\s*=\s*","1","$(index)","set"); # match a line starting like the key something