diff --git a/cdist/conf/type/__ini_value/files/base.awk b/cdist/conf/type/__ini_value/files/base.awk index 51c3ba99..57535964 100644 --- a/cdist/conf/type/__ini_value/files/base.awk +++ b/cdist/conf/type/__ini_value/files/base.awk @@ -1,7 +1,7 @@ BEGIN { bufindex = -1 buflen = 0 - maxbuflen = 2 + maxbuflen = 10 # no section means the start to the first section if(section == "") { @@ -15,9 +15,13 @@ function flush_buffer() { while(buflen > 0) _pop_line() } +function flush_lines(n) { + while(buflen > 0 && n-- > 0) + _pop_line() +} function push_line() { linebuf[++bufindex] = $0 - buflen++; + buflen++ while(buflen > maxbuflen) _pop_line() } function revert_line() { @@ -41,8 +45,8 @@ function _pop_line() { function is_comment(line) { # get character and check line_sign = substr(line, 1, 1) - for(i in comment_signs) - if(line_sign == comment_signs[i]) + for(c in comment_signs) + if(line_sign == comment_signs[c]) return 1 # nothing found @@ -85,6 +89,10 @@ found {print; next} found=1 # %codeblock_insert% + + # print line as it would else only be populated below + print + next } is_curr_section = 0 } @@ -101,7 +109,7 @@ found {print; next} # check for a delimiter and a key (must be at first position due to trimming) if((idel = index(line, delimiter)) && (ikey = index(line, key)) == 1) { - # check there are only spaces between the delimiter + # check there are only spaces between the key and delimiter if(check_spaces(substr(line, ikey + length(key), idel - (length(key) + 1)))) { found = 1 diff --git a/cdist/conf/type/__ini_value/files/parts/present/insert.awk b/cdist/conf/type/__ini_value/files/parts/present/insert.awk index c48186d8..eeaa7eb8 100644 --- a/cdist/conf/type/__ini_value/files/parts/present/insert.awk +++ b/cdist/conf/type/__ini_value/files/parts/present/insert.awk @@ -1,9 +1,56 @@ -was_com_there = was_comment(lastline(), comment) +# check if there is a comment block before the section +firstline_index = bufindex - (buflen - 1) +insertpoint = -1 # the insertpoint marks the point before the insert +lastfreespace = -1 +for(i = bufindex; i >= firstline_index; i--) { + _line = trim(linebuf[i]) + if(_line == "") { + lastfreespace = i + continue + } + if(comment && was_comment(_line, comment)) { + insertpoint = i + 1 + no_insert_comment = 1 + if(lastfreespace != insertpoint) + insert_line_after = 1 + break + } + if(!is_comment(_line) || index(_line, delimiter) > 0) { + insertpoint = i + 1 + + # only insert a line before if we do not have a space around + if(lastfreespace == insertpoint) + insertpoint++ + else + insert_line_before = 1 + # check for empty line after the insert point + # use absolute boundary cause the insertpoint can be changed + if(trim(linebuf[i + 2]) != "") + insert_line_after = 1 + break + } +} + +# insert into the last free space +if(insertpoint == -1) { + if(lastfreespace != -1) { + insertpoint = lastfreespace + insert_line_before = 1 + } + else { + insertpoint = firstline_index + insert_line_after = 1 + } +} + +# print lines before +flush_lines(insertpoint - firstline_index) + # print before and comment -flush_buffer() -if(comment && !was_com_there) c_print() - -# print value + seperator line at the end +if(insert_line_before) print "" +if(comment && !no_insert_comment) c_print() v_print() -print "" +if(insert_line_after) print "" + +flush_buffer()