2
0

fix-multiline-comments.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #! /bin/sh
  2. #
  3. # Fix multiline comments to match CODING_STYLE
  4. #
  5. # Copyright (C) 2018 Red Hat, Inc.
  6. #
  7. # Author: Paolo Bonzini
  8. #
  9. # Usage: scripts/fix-multiline-comments.sh [-i] FILE...
  10. #
  11. # -i edits the file in place (requires gawk 4.1.0).
  12. #
  13. # Set the AWK environment variable to choose the awk interpreter to use
  14. # (default 'awk')
  15. if test "$1" = -i; then
  16. # gawk extension
  17. inplace="-i inplace"
  18. shift
  19. fi
  20. ${AWK-awk} $inplace 'BEGIN { indent = -1 }
  21. {
  22. line = $0
  23. # apply a star to the indent on lines after the first
  24. if (indent != -1) {
  25. if (line == "") {
  26. line = sp " *"
  27. } else if (substr(line, 1, indent + 2) == sp " ") {
  28. line = sp " *" substr(line, indent + 3)
  29. }
  30. }
  31. is_lead = (line ~ /^[ \t]*\/\*/)
  32. is_trail = (line ~ /\*\//)
  33. if (is_lead && !is_trail) {
  34. # grab the indent at the start of a comment, but not for
  35. # single-line comments
  36. match(line, /^[ \t]*\/\*/)
  37. indent = RLENGTH - 2
  38. sp = substr(line, 1, indent)
  39. }
  40. # the regular expression filters out lone /*, /**, or */
  41. if (indent != -1 && !(line ~ /^[ \t]*(\/\*+|\*\/)[ \t]*$/)) {
  42. if (is_lead) {
  43. # split the leading /* or /** on a separate line
  44. match(line, /^[ \t]*\/\*+/)
  45. lead = substr(line, 1, RLENGTH)
  46. match(line, /^[ \t]*\/\*+[ \t]*/)
  47. line = lead "\n" sp " *" substr(line, RLENGTH)
  48. }
  49. if (is_trail) {
  50. # split the trailing */ on a separate line
  51. match(line, /[ \t]*\*\//)
  52. line = substr(line, 1, RSTART - 1) "\n" sp " */"
  53. }
  54. }
  55. if (is_trail) {
  56. indent = -1
  57. }
  58. print line
  59. }' "$@"