shini.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # shini - compatible INI library for sh
  2. #
  3. # This code is released freely under the MIT license - see the shipped LICENSE document.
  4. # For the latest version etc, please see https://github.com/wallyhall/shini
  5. #
  6. # Solely for the purpose of portable performance, this script breaks good practice to
  7. # avoid forking subshells. One such good practice is avoiding global variables.
  8. # This variable is used to carry non-numeric results from functions to the caller.
  9. # Alternatively an echo and "$(...)" approach could be used, but is significantly slower.
  10. shini_setup()
  11. {
  12. if [ -n "$ZSH_VERSION" ]; then
  13. RESTORE_OPTS=$(set +o)
  14. # Enable BASH_REMATCH for zsh
  15. setopt KSH_ARRAYS BASH_REMATCH
  16. fi
  17. }
  18. shini_teardown()
  19. {
  20. [ -n "$ZSH_VERSION" ] && eval "$RESTORE_OPTS"
  21. }
  22. shini_function_exists()
  23. {
  24. type "$1" > /dev/null 2>&1
  25. return $?
  26. }
  27. shini_regex_match()
  28. {
  29. # $KSH_VERSION (I'm told) only exists on ksh 93 and above, which supports regex matching.
  30. if [ -n "$BASH_VERSINFO" ] && [ "$BASH_VERSINFO" -ge 3 ] || \
  31. [ -n "$ZSH_VERSION" ] || \
  32. [ -n "$KSH_VERSION" ]; then
  33. [[ "$1" =~ $2 ]] && return 0 || return 1
  34. fi
  35. printf '%s' "$1" | grep -qe "$2"
  36. return $?
  37. }
  38. shini_regex_replace()
  39. {
  40. if [ -n "$BASH_VERSINFO" ] && [ "${BASH_VERSINFO}" -ge 3 ] || \
  41. [ -n "$ZSH_VERSION" ]; then
  42. [[ "$1" =~ $2 ]] && shini_retval=${BASH_REMATCH[1]} || shini_retval="$1"
  43. return 0
  44. fi
  45. shini_retval="$(printf '%s' "$1" | sed -E "s/$2/\1/")" # If you have isses on older systems,
  46. # it may be the non-newer POSIX compliant sed.
  47. # -E should be enabling extended regex mode portably.
  48. }
  49. # @param inifile Filename of INI file to parse
  50. # @param postfix Function postfix for callbacks (optional)
  51. # @param extra Extra argument for callbacks (optional)
  52. shini_parse()
  53. {
  54. shini_parse_section "$1" '' "$2" "$3" "$4" "$5"
  55. }
  56. # @param inifile Filename of INI file to parse
  57. # @param section Section to parse (or empty string for entire file)
  58. # @param postfix Function postfix for callbacks (optional)
  59. # @param extra Extra argument for callbacks (optional)
  60. shini_parse_section()
  61. {
  62. shini_setup
  63. # ********
  64. RX_KEY='[a-zA-Z0-9_\-\.]'
  65. RX_VALUE="[^;\"]"
  66. RX_SECTION='[a-zA-Z0-9_\-]'
  67. RX_WS='[ ]'
  68. RX_QUOTE='"'
  69. RX_HEX='[0-9A-F]'
  70. POSTFIX=''
  71. SKIP_TO_SECTION=''
  72. EXTRA1=''
  73. EXTRA2=''
  74. EXTRA3=''
  75. SECTION_FOUND=-1
  76. if [ $# -ge 2 ] && [ ! -z "$2" ]; then
  77. SKIP_TO_SECTION="$2"
  78. fi
  79. if [ $# -ge 3 ] && [ ! -z "$3" ]; then
  80. POSTFIX="_$3"
  81. fi
  82. if [ $# -ge 4 ] && ! [ -z "$4" ]; then
  83. EXTRA1="$4"
  84. fi
  85. if [ $# -ge 5 ] && [ ! -z "$5" ]; then
  86. EXTRA2="$5"
  87. fi
  88. if [ $# -ge 6 ] && [ ! -z "$6" ]; then
  89. EXTRA3="$6"
  90. fi
  91. if ! shini_function_exists "__shini_parsed${POSTFIX}"; then
  92. printf 'shini: __shini_parsed%s function not declared.\n' "${POSTFIX}" 1>&2
  93. exit 255
  94. fi
  95. if [ $# -lt 1 ]; then
  96. if shini_function_exists "__shini_no_file_passed{$POSTFIX}"; then
  97. "__shini_no_file_passed${POSTFIX}" "$EXTRA1" "$EXTRA2" "$EXTRA3"
  98. else
  99. printf 'shini: Argument 1 needs to specify the INI file to parse.\n' 1>&2
  100. exit 254
  101. fi
  102. fi
  103. INI_FILE="$1"
  104. if [ ! -r "$INI_FILE" ]; then
  105. if shini_function_exists "__shini_file_unreadable${POSTFIX}"; then
  106. "__shini_file_unreadable${POSTFIX}" "$INI_FILE" "$EXTRA1" "$EXTRA2" "$EXTRA3"
  107. else
  108. printf 'shini: Unable to read INI file:\n `%s`\n' "$INI_FILE" 1>&2
  109. exit 253
  110. fi
  111. fi
  112. # Iterate INI file line by line
  113. LINE_NUM=0
  114. SECTION=''
  115. while read LINE || [ -n "$LINE" ]; do # -n $LINE catches final line if not empty
  116. # Check for new sections
  117. if shini_regex_match "$LINE" "^${RX_WS}*\[${RX_SECTION}${RX_SECTION}*\]${RX_WS}*$"; then
  118. shini_regex_replace "$LINE" "^${RX_WS}*\[(${RX_SECTION}${RX_SECTION}*)\]${RX_WS}*$" "\1"
  119. SECTION=$shini_retval
  120. if [ "$SKIP_TO_SECTION" != '' ]; then
  121. # stop once specific section is finished
  122. [ $SECTION_FOUND -eq 0 ] && break;
  123. # mark the specified section as found
  124. [ "$SKIP_TO_SECTION" = "$SECTION" ] && SECTION_FOUND=0;
  125. fi
  126. if shini_function_exists "__shini_parsed_section${POSTFIX}"; then
  127. "__shini_parsed_section${POSTFIX}" "$SECTION" "$EXTRA1" "$EXTRA2" "$EXTRA3"
  128. fi
  129. LINE_NUM=$((LINE_NUM+1))
  130. continue
  131. fi
  132. # Skip over sections we don't care about, if a specific section was specified
  133. [ "$SKIP_TO_SECTION" != '' ] && [ $SECTION_FOUND -ne 0 ] && LINE_NUM=$((LINE_NUM+1)) && continue;
  134. # Check for new values
  135. if shini_regex_match "$LINE" "^${RX_WS}*${RX_KEY}${RX_KEY}*${RX_WS}*="; then
  136. shini_regex_replace "$LINE" "^${RX_WS}*(${RX_KEY}${RX_KEY}*)${RX_WS}*=.*$"
  137. KEY=$shini_retval
  138. shini_regex_replace "$LINE" "^${RX_WS}*${RX_KEY}${RX_KEY}*${RX_WS}*=${RX_WS}*${RX_QUOTE}{0,1}(${RX_VALUE}*)${RX_QUOTE}{0,1}(${RX_WS}*\;.*)*$"
  139. VALUE=$shini_retval
  140. if shini_regex_match "$LINE" "^0x${RX_HEX}${RX_HEX}*$"; then
  141. VALUE=$(printf '%d' "$VALUE")
  142. fi
  143. "__shini_parsed${POSTFIX}" "$SECTION" "$KEY" "$VALUE" "$EXTRA1" "$EXTRA2" "$EXTRA3"
  144. if shini_function_exists "__shini_parsed_comment${POSTFIX}"; then
  145. if shini_regex_match "$LINE" ";"; then
  146. shini_regex_replace "$LINE" "^.*\;(.*)$"
  147. COMMENT=$shini_retval
  148. "__shini_parsed_comment${POSTFIX}" "$COMMENT" "$EXTRA1" "$EXTRA2" "$EXTRA3"
  149. fi
  150. fi
  151. LINE_NUM=$((LINE_NUM+1))
  152. continue
  153. fi
  154. # Announce parse errors
  155. if [ "$LINE" != '' ] &&
  156. ! shini_regex_match "$LINE" "^${RX_WS}*;.*$" &&
  157. ! shini_regex_match "$LINE" "^${RX_WS}*$"; then
  158. if shini_function_exists "__shini_parse_error${POSTFIX}"; then
  159. "__shini_parse_error${POSTFIX}" $LINE_NUM "$LINE" "$EXTRA1" "$EXTRA2" "$EXTRA3"
  160. else
  161. printf 'shini: Unable to parse line %d:\n `%s`\n' $LINE_NUM "$LINE"
  162. fi
  163. fi
  164. LINE_NUM=$((LINE_NUM+1))
  165. done < "$INI_FILE"
  166. # ********
  167. shini_teardown
  168. }
  169. # @param inifile Filename of INI file to write to
  170. # @param section Section of INI file to write to
  171. # @param variable Variable name to add/update/delete
  172. # @param value Value to add/update, do not specify to delete
  173. shini_write()
  174. {
  175. shini_setup
  176. # ********
  177. # This is not yet optimised (early write support only) -
  178. # We actually re-parse the entire file, looking for the location in which to
  179. # write the new value, writing out everything we parse as-is meanwhile.
  180. # Declare the following if you want particular behaviour (like skipping
  181. # broken INI file content or handling an unreadable file etc).
  182. # __shini_no_file_passed__writer()
  183. # __shini_file_unreadable__writer()
  184. # __shini_parse_error__writer()
  185. # Writer callbacks, used for writing the INI file content
  186. __shini_parsed_section__writer()
  187. {
  188. # Validate the last section wasn't the target section
  189. if [ "$LAST_SECTION" = "$WRITE_SECTION" ]; then
  190. # If it was, and the value wasn't written already, write it
  191. if [ $VALUE_WRITTEN -eq 0 ]; then
  192. printf "\n%s=%s" "$WRITE_KEY" "$WRITE_VALUE" >> "$INI_FILE_TEMP"
  193. VALUE_WRITTEN=1
  194. fi
  195. fi
  196. printf "\n[%s]" "$1" >> "$INI_FILE_TEMP"
  197. LAST_SECTION="$1"
  198. }
  199. __shini_parsed_comment__writer()
  200. {
  201. printf ";%s" "$1" >> "$INI_FILE_TEMP"
  202. }
  203. __shini_parsed__writer()
  204. {
  205. if [ "$1" = "$WRITE_SECTION" ]; then
  206. if [ "$2" = "$WRITE_KEY" ]; then
  207. if [ ! -z "$WRITE_VALUE" ]; then
  208. printf "\n%s=%s" "$WRITE_KEY" "$WRITE_VALUE" >> "$INI_FILE_TEMP"
  209. fi
  210. VALUE_WRITTEN=1
  211. return
  212. fi
  213. fi
  214. printf "\n%s=%s" "$2" "$3" >> "$INI_FILE_TEMP"
  215. }
  216. if [ $# -lt 3 ]; then
  217. if shini_function_exists "__shini_no_file_passed"; then
  218. __shini_no_file_passed
  219. else
  220. printf 'shini: Argument 1 needs to specify the INI file to write.\n' 1>&2
  221. exit 254
  222. fi
  223. fi
  224. INI_FILE="$1"
  225. INI_FILE_TEMP="$(mktemp -t shini_XXXXXX)"
  226. WRITE_SECTION="$2"
  227. WRITE_KEY="$3"
  228. WRITE_VALUE="$4"
  229. LAST_SECTION=""
  230. VALUE_WRITTEN=0
  231. shini_parse "$1" "_writer" "$2" "$3" "$4"
  232. # Still not written out yet
  233. if [ $VALUE_WRITTEN -eq 0 ]; then
  234. # Check if final existing section was target one, add it if not
  235. if [ "$LAST_SECTION" != "$WRITE_SECTION" ]; then
  236. printf "\n[%s]" "$WRITE_SECTION" >> "$INI_FILE_TEMP"
  237. fi
  238. # Write value at end of file
  239. printf "\n%s=%s" "$WRITE_KEY" "$WRITE_VALUE" >> "$INI_FILE_TEMP"
  240. fi
  241. mv "$INI_FILE_TEMP" "$INI_FILE"
  242. # ********
  243. shini_teardown
  244. }
  245. # default usage
  246. __shini_parsed()
  247. {
  248. if [[ $2 != *\$* ]] && [[ $3 != *\$* ]]; then
  249. eval $2=$3
  250. fi
  251. }
  252. __shini_parse_error()
  253. {
  254. error_line=$1
  255. }
  256. __shini_parse_error__writer()
  257. {
  258. error_line=$1
  259. }