My dotfiles. Period.

update_scopedb.sh 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/sh
  2. set -e
  3. PROG_NAME=$0
  4. CSCOPE_EXE=cscope
  5. CSCOPE_ARGS=
  6. DB_FILE=cscope.out
  7. PROJECT_ROOT=
  8. FILE_LIST_CMD=
  9. BUILD_INVERTED_INDEX=0
  10. ShowUsage() {
  11. echo "Usage:"
  12. echo " $PROG_NAME <options>"
  13. echo ""
  14. echo " -e [exe=cscope]: The cscope executable to run"
  15. echo " -f [file=cscope.out]: The path to the ctags file to update"
  16. echo " -p [dir=]: The path to the project root"
  17. echo " -L [cmd=]: The file list command to run"
  18. echo " -I: Builds an inverted index"
  19. echo ""
  20. }
  21. while getopts "h?e:f:p:L:I" opt; do
  22. case $opt in
  23. h|\?)
  24. ShowUsage
  25. exit 0
  26. ;;
  27. e)
  28. CSCOPE_EXE=$OPTARG
  29. ;;
  30. f)
  31. DB_FILE=$OPTARG
  32. ;;
  33. p)
  34. PROJECT_ROOT=$OPTARG
  35. ;;
  36. L)
  37. FILE_LIST_CMD=$OPTARG
  38. ;;
  39. I)
  40. BUILD_INVERTED_INDEX=1
  41. ;;
  42. esac
  43. done
  44. shift $((OPTIND - 1))
  45. if [ "$1" != "" ]; then
  46. echo "Invalid Argument: $1"
  47. exit 1
  48. fi
  49. echo "Locking cscope DB file..."
  50. echo $$ > "$DB_FILE.lock"
  51. # Remove lock and temp file if script is stopped unexpectedly.
  52. CleanUp() {
  53. rm -f "$DB_FILE.lock" "$DB_FILE.files" "$DB_FILE.temp"
  54. if [ "$BUILD_INVERTED_INDEX" -eq 1 ]; then
  55. rm -f "$DB_FILE.temp.in" "$DB_FILE.temp.po"
  56. fi
  57. }
  58. trap CleanUp INT QUIT TERM EXIT
  59. PREVIOUS_DIR=$(pwd)
  60. if [ -d "$PROJECT_ROOT" ]; then
  61. cd "$PROJECT_ROOT"
  62. fi
  63. if [ -n "${FILE_LIST_CMD}" ]; then
  64. if [ "${PROJECT_ROOT}" = "." ]; then
  65. eval "$FILE_LIST_CMD" | while read -r l; do
  66. echo "\"${l}\""
  67. done > "${DB_FILE}.files"
  68. else
  69. # If using a tags cache directory, use absolute paths
  70. eval "$FILE_LIST_CMD" | while read -r l; do
  71. echo "\"${PROJECT_ROOT%/}/${l}\""
  72. done > "${DB_FILE}.files"
  73. fi
  74. else
  75. find . -type f ! -name ${DB_FILE} | while read -r l; do
  76. echo "\"${l}\""
  77. done > "${DB_FILE}.files"
  78. fi
  79. if [ ! -s "${DB_FILE}.files" ]; then
  80. echo "There is no files to generate cscope DB"
  81. exit
  82. fi
  83. CSCOPE_ARGS="${CSCOPE_ARGS} -i ${DB_FILE}.files"
  84. if [ "$BUILD_INVERTED_INDEX" -eq 1 ]; then
  85. CSCOPE_ARGS="$CSCOPE_ARGS -q"
  86. fi
  87. echo "Running cscope"
  88. echo "$CSCOPE_EXE $CSCOPE_ARGS -b -k -f \"$DB_FILE.temp\""
  89. "$CSCOPE_EXE" $CSCOPE_ARGS -v -b -k -f "$DB_FILE.temp"
  90. if [ -d "$PROJECT_ROOT" ]; then
  91. cd "$PREVIOUS_DIR"
  92. fi
  93. echo "Replacing cscope DB file"
  94. if [ "$BUILD_INVERTED_INDEX" -eq 1 ]; then
  95. echo "mv -f \"$DB_FILE.temp.in\" \"$DB_FILE.in\""
  96. mv -f "$DB_FILE.temp.in" "$DB_FILE.in"
  97. echo "mv -f \"$DB_FILE.temp.po\" \"$DB_FILE.po\""
  98. mv -f "$DB_FILE.temp.po" "$DB_FILE.po"
  99. fi
  100. echo "mv -f \"$DB_FILE.temp\" \"$DB_FILE\""
  101. mv -f "$DB_FILE.temp" "$DB_FILE"
  102. echo "Unlocking cscope DB file..."
  103. rm -f "$DB_FILE.lock"
  104. echo "Done."