hercules-team / augeas

A configuration editing tool and API
http://augeas.net/
GNU Lesser General Public License v2.1
486 stars 199 forks source link

Shellvars.lns can't insert comment if the first line a newline #202

Closed telnoratti closed 9 years ago

telnoratti commented 9 years ago

I've tested this on CentOS 6 and CentOS 7, but I pulled the most recent version of Shellvars.lns and the issue still persists. I also haven't tested with anything other than comments, so it could be more general.

Steps to reproduce

Create a file that is valid shell syntax with a newline at the beginning called test. https://linx.li/b59d7e0.txt is what I used.

augtool -A -L
set /augeas/load/Shellvars/lens "Shellvars.lns"
set /augeas/load/Shellvars/incl "/test"
load
ins #comment before /files/test/*[1]
set /files/test/#comment[1] "test comment"
save

I get the following error when printed

/augeas/files/test/error = "put_failed"
/augeas/files/test/error/path = "/files/test"
/augeas/files/test/error/lens = "/usr/share/augeas/lenses/dist/shellvars.aug:191.12-.56:"
/augeas/files/test/error/message = "Failed to match \n    { }*({ /#comment/ = /[^\\001-\\004\\t\\n\\r ][^\\001-\\004\\n]*[^\\001-\\004\\t\\n\\r ]|[^\\001-\\004\\t\\n\\r ]/ } | { /\\\\.source/ = /[^\\001-\\004\\t\\n #;=]+/ } | { /export(([0-9A-Z_a-z][0-9A-Z_a-z]*\\\\[|\\\\[)[0-9][0-9][0-9]*\\\\]|([0-9A-Z_a-z][0-9A-Z_a-z]*\\\\[|\\\\[)[0-9]\\\\]|[0-9A-Z_a-z][0-9A-Z_a-z]*)|expor(([0-9A-Z_a-su-z][0-9A-Z_a-z]*\\\\[|\\\\[)[0-9][0-9][0-9]*\\\\]|([0-9A-Z_a-su-z][0-9A-Z_a-z]*\\\\[|\\\\[)[0-9]\\\\]|[0-9A-Z_a-su-z][0-9A-Z_a-z]*|)|expo(([0-9A-Z_a-qs-z][0-9A-Z_a-z]*\\\\[|\\\\[)[0-9][0-9][0-9]*\\\\]|([0-9A-Z_a-qs-z][0-9A-Z_a-z]*\\\\[|\\\\[)[0-9]\\\\]|[0-9A-Z_a-qs-z][0-9A-Z_a-z]*|)|exp(([0-9A-Z_a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A-Z_a-z]*|)|un([0-9A-Z_a-rt-z][0-9A-Z_a-z]*|)|(u[0-9A-Z_a-mo-z]|e[0-9A-Z_a-wyz]|[0-9A-Z_a-df-tv-z][0-9A-Z_a-z])[0-9A-Z_a-z]*|u|e|[0-9A-Z_a-df-tv-z]/ = /((([^\\001-\\004\\t\\n \"'-);`]|\\\\\\\\\")*|(\"([^\\001-\\004\"\\\\]|\\\\\\\\[^\\001-\\004\\n]|[\\t ]*\\\\\\\\\\n[\\t ]*)*\"|'[^\\001-\\004']*')+|`[^\\001-\\004\\n`]*`|``[^\\001-\\004\\t\\n ;`]+``|\\\\$\\\\([^\\001-\\004\\n#\\\\]*\\\\)|\\\\$\\\\(\\\\([^\\001-\\004\\n#)\\\\]*\\\\)\\\\)|\\\\([\\t ]*\\\\)))/ } | { /@unset/ } | { /@export/ } | { /@builtin/ = /ulimit|shift|exit/ } | { /@return/ = /(([0-9]+))?/ } | <<rec:{ /@if/ = /([^\\001-\\004\\t\\n \"#';]|[^\\001-\\004\\t\\n \"#';]([^\\001-\\004\\n\"#';]|(\"([^\\001-\\004\"\\\\]|\\\\\\\\[^\\001-\\004\\n]|[\\t ]*\\\\\\\\\\n[\\t ]*)*\"|'[^\\001-\\004']*')+|`[^\\001-\\004\\n`]*`|``[^\\001-\\004\\t\\n ;`]+``|\\\\$\\\\([^\\001-\\004\\n#\\\\]*\\\\)|\\\\$\\\\(\\\\([^\\001-\\004\\n#)\\\\]*\\\\)\\\\))+[^\\001-\\004\\t\\n \"#';])/ } | { /@for/ = /([^\\001-\\004\\t\\n \"#';]|[^\\001-\\004\\t\\n \"#';]([^\\001-\\004\\n\"#';]|(\"([^\\001-\\004\"\\\\]|\\\\\\\\[^\\001-\\004\\n]|[\\t ]*\\\\\\\\\\n[\\t ]*)*\"|'[^\\001-\\004']*')+|`[^\\001-\\004\\n`]*`|``[^\\001-\\004\\t\\n ;`]+``|\\\\$\\\\([^\\001-\\004\\n#\\\\]*\\\\)|\\\\$\\\\(\\\\([^\\001-\\004\\n#)\\\\]*\\\\)\\\\))+[^\\001-\\004\\t\\n \"#';])/ } | { /@select/ = /([^\\001-\\004\\t\\n \"#';]|[^\\001-\\004\\t\\n \"#';]([^\\001-\\004\\n\"#';]|(\"([^\\001-\\004\"\\\\]|\\\\\\\\[^\\001-\\004\\n]|[\\t ]*\\\\\\\\\\n[\\t ]*)*\"|'[^\\001-\\004']*')+|`[^\\001-\\004\\n`]*`|``[^\\001-\\004\\t\\n ;`]+``|\\\\$\\\\([^\\001-\\004\\n#\\\\]*\\\\)|\\\\$\\\\(\\\\([^\\001-\\004\\n#)\\\\]*\\\\)\\\\))+[^\\001-\\004\\t\\n \"#';])/ } | { /@while/ = /([^\\001-\\004\\t\\n \"#';]|[^\\001-\\004\\t\\n \"#';]([^\\001-\\004\\n\"#';]|(\"([^\\001-\\004\"\\\\]|\\\\\\\\[^\\001-\\004\\n]|[\\t ]*\\\\\\\\\\n[\\t ]*)*\"|'[^\\001-\\004']*')+|`[^\\001-\\004\\n`]*`|``[^\\001-\\004\\t\\n ;`]+``|\\\\$\\\\([^\\001-\\004\\n#\\\\]*\\\\)|\\\\$\\\\(\\\\([^\\001-\\004\\n#)\\\\]*\\\\)\\\\))+[^\\001-\\004\\t\\n \"#';])/ } | { /@until/ = /([^\\001-\\004\\t\\n \"#';]|[^\\001-\\004\\t\\n \"#';]([^\\001-\\004\\n\"#';]|(\"([^\\001-\\004\"\\\\]|\\\\\\\\[^\\001-\\004\\n]|[\\t ]*\\\\\\\\\\n[\\t ]*)*\"|'[^\\001-\\004']*')+|`[^\\001-\\004\\n`]*`|``[^\\001-\\004\\t\\n ;`]+``|\\\\$\\\\([^\\001-\\004\\n#\\\\]*\\\\)|\\\\$\\\\(\\\\([^\\001-\\004\\n#)\\\\]*\\\\)\\\\))+[^\\001-\\004\\t\\n \"#';])/ } | { /@case/ = /(([^\\001-\\004\\t\\n \"'-);`]|\\\\\\\\\")+|\"([^\\001-\\004\\t\\n \"'-);`]|\\\\\\\\\")+\")/ } | { /@function/ = /[.0-9A-Z_a-z-]+/ }>>)*\n  with tree\n    { \"#comment\" = \"test comment\" } {  } { \"VAR\" = \"\"test\"\" }"

If I remove the newline at the beginning of the file it works fine. It appears that at least one Shellvars file (/etc/sysconfig/selinux) on CentOS contains a new line at the beginning.

lutter commented 9 years ago

Because of the way the lens handles blank lines at the beginning of a file, the insertion results in an invalid tree, one that can't be turned back into a file with that lens. The fix will be to modify the lens to handle this situation.