-
Notifications
You must be signed in to change notification settings - Fork 0
/
etags-c
executable file
·340 lines (311 loc) · 12.1 KB
/
etags-c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/sh
# SH FILE: etags-c
#
# Purpose : Build Emacs Tags file for C/C++ source code files.
# Created : Monday, November 9 2020.
# Author : Pierre Rouleau <prouleau001@gmail.com>
# Time-stamp: <2024-07-22 15:07:35 EDT, updated by Pierre Rouleau>
# ----------------------------------------------------------------------------
# Module Description
# ------------------
#
# Build an Emacs Tags file for all C and C++ source code files in the
# sub-directory tree. This includes all files with the following extensions:
#
# - C files : .c, .h, .c.in, .h.in,
# - C++ files: .c++, .h++, .c++.in, .h++.in
# .cpp, .hpp, .cpp.in, .hpp.in
# .C, .H, .C.in, .H.in
# .hh, .hh.in
# .inl, .M, .pdb
#
# Usage:
# - cd where you want to store the TAGS file
# - etags-c [--list=FILENAME] | [DIRPATH [DIRPATH ...]]
#
# ----------------------------------------------------------------------------
# Dependencies
# ------------
#
# - Uses fd (also named fdfind in some Linux distros like Mint) if it is
# available. See URL: https://github.com/sharkdp/fd .
# - if fd/fdfind is not available, use find instead.
#
# Using fd/fdfind is preferred because it supports ignoring files identified
# by .gitignore, .ignore, .fdignore files. The fd utility can also execute
# faster than find in several scenarios.
#
# - To force use of find, set the ETAGS_USE_FIND environment variable to 1.
# - Uses etags to process files unless ETAGS_USE_UCTAGS environment variable
# is set to 1, in which case the ctags utility is found, assuming that it
# is the Universal Ctags utility, not any other ctag command.
# You can also set ETAGS_USE_UCTAGS to the absolute path of the
# Universal Ctags executable file.
# ----------------------------------------------------------------------------
# TODO: eliminate code duplication while keeping capability to stay
# compatible with various shells (sh, bash, etc...)
# ----------------------------------------------------------------------------
# Code
# ----
print_usage()
{
printf -- "
etags-c: build a etags-compliant TAGS file in the current directory
for all C and C++ source code files in the directory trees
specified on the command line.
Usage: etags-c [-h|--help]
• Print this help information.
Usage: etags-c [--list=FILENAME] | [DIRPATH [DIRPATH ...]]
• [Re]build an etags-compliant TAGS file in current directory
for the C/C++ code & header files found:
- in the directory tree(s) identified on the command line, or
- in the list provided in the FILENAME specified by the
--list option, or
- specified by the ETAGS_FILE_LIST environment variable.
• Overwrites any TAGS file in the current directory.
• Stores the names of inspected files in the file named
.etags-c-files.txt in the current directory.
• Identifies the C/C++ files using one of the following methods:
• If ETAGS_FILE_LIST environment variable exists, it uses it as
the name of a file that holds a list of C/C++ files to parse.
The name of this file can be anything except .etags-c-files.txt
and it must exists. It it does not etags-c exists with an error.
Each line of the file must identify a C/C++ file to parse.
• Searches the list of C/C++ source code files.
• It uses 'fdfind' or 'fd' if available, 'find' otherwise.
- The fd utility is from: https://github.com/sharkdp/fd
- In some Linux distros, 'fd' is renamed 'fdfind'.
• To force the use of 'find', set ETAGS_USE_FIND environment
variable to 1.
• ☝️ When using 'fdfind' or 'fd', the files identified
by the .gitignore, .ignore or .fdignore files are ignored.
• Stores the names of inspected files in the file named
.etags-c-files.txt stored in the same directory as TAGS.
• Uses etags to build the TAGS file by default.
• Set ETAGS_USE_UCTAGS to 1 or to the absolute path of Universal
Ctags executable file to force using Universal Ctags instead.
When setting it to 1, the ctags found in PATH is expected
to be Universal Ctags.
• Prints the path of the \"find\" program used and when
repository ignore files are scanned.\n"
if [ "$ETAGS_USE_FIND" = "1" ]; then
printf -- "
⚠️ ETAGS_USE_FIND is set to 1, forcing the use of 'find'!
• To prevent this, set it to 0 or remove that
environment variable.
• With USRHOME, you can use setenv and clrenv.
See: https://github.com/pierre-rouleau/usrhome
"
fi
}
# --
# run__etags_c -- run etags or ctags on a set of files
# ------------
#
# Use etags to parse the source code files unless ETAGS_USE_UCTAGS
# environment variable is set to 1. In which case it uses ctags.
# Ideally that would be the latest version of Universal ctags
# from https://github.com/universal-ctags/ctags#readme as opposed to the
# old and currently un-maintained Exeburant ctags.
# When using Emacs, Emacs provides its own ctags that is essentially etags.
# Etags generates a tag file format a little different and better suited for Emacs.
run__etags_c()
{
# Expects no argument, but expects a list of files to parse on stdin.
if [ -n "$ETAGS_USE_UCTAGS" ]; then
if [ "$ETAGS_USE_UCTAGS" = "1" ]; then
xargs ctags --output-format=etags
printf "Note: ETAGS_USE_UCTAGS forced using %s to process files.\n" "$(which ctags)"
elif [ -x "$ETAGS_USE_UCTAGS" ]; then
xargs "$ETAGS_USE_UCTAGS" --output-format=etags
printf "Note: ETAGS_USE_UCTAGS forced using %s to process files.\n" "$ETAGS_USE_UCTAGS"
else
printf -- "*** etags-c ERROR: invalid ETAGS_USE_UCTAGS value\n"
printf -- "*** ETAGS_USE_UCTAGS : %s\n" "$ETAGS_USE_UCTAGS"
printf -- "*** This is not 1 nor a valid executable file name.\n"
exit 4
fi
else
# Qualify tag names with their class name in C++.
etags --class-qualify -
fi
}
# --
# Check validity of arguments
if [ -z "$1" ]; then
print_usage
exit 1
elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
print_usage
if [ "$#" = "1" ]; then
exit 0
else
exit 1
fi
fi
case "$1" in
--list=*)
# extract the file name from the --list= option
fname="$(echo "$1" | sed 's/--list=//g')"
if [ ! -f "${fname}" ]; then
printf -- "\
*** etags-c ERROR: specified list file does not exist: %s
" "${fname}"
exit 1
else
ETAGS_FILE_LIST="${fname}"
shift
fi
;;
esac
# --
if [ -n "$ETAGS_FILE_LIST" ]; then
if [ "$ETAGS_FILE_LIST" = ".etags-c-files.txt" ]; then
echo "Error! Do NOT set ETAGS_FILE_LIST to .etags-c-files.txt"
exit 1
fi
echo "Using the file list identified by ETAGS_FILE_LIST: $ETAGS_FILE_LIST"
cp "$ETAGS_FILE_LIST" .etags-c-files.txt
run__etags_c < "$ETAGS_FILE_LIST"
else
# Use the directory names provided on command line.
# Check their validity.
# Ensure that each directory specified on the command line
# exists. Also ensure that a directory name is not specified more
# than once. Use the realpath of that directory name when comparing.
dpaths=
for dname in "$@"; do
if [ ! -d "${dname}" ]; then
printf -- "Specified directory does not exists: %s\n" "${dname}"
exit 2
fi
dpath="$(realpath "${dname}")"
for dp in ${dpaths}; do
if [ "$dp" = "$dpath" ]; then
printf -- "Multiple entry of directory: %s\n" "${dname}"
exit 3
fi
# check if new entry is a sub-directory of an exiting entry
case "${dpath}" in
"${dp}"*)
printf -- "%s is a sub-dir of already specified %s\n" "${dname}" "${dp}"
exit 3
;;
esac
# check if new entry is a parent directory of an existing entry
case "${dp}" in
"${dpath}"*)
printf -- "%s is a parent dir of already specified %s\n" "${dname}" "${dp}"
exit 3
;;
esac
done
dpaths="${dpaths} ${dpath}"
done
echo "No list of files identified by ETAGS_FILE_LIST."
echo "- Building the list of source files."
# Find the files, pass their names to etags which creates the TAGS file.
# To find the files, try to use 'fd' or 'fdfind' if they exist,
# otherwise use 'find'. Echo the actual program used to find the files.
# Search for all possible C and C++ file extensions in the directory tree.
# - Identify how to search files
if [ "$ETAGS_USE_FIND" = "1" ]; then
search_with='find'
else
if [ -x "$(command -v fdfind)" ]; then
search_with='fd'
fd_pgm=fdfind
elif [ -x "$(command -v fd)" ]; then
search_with='fd'
fd_pgm=fd
else
search_with='find'
fi
fi
case $(uname) in
Darwin)
find_path_option='-f'
;;
Linux)
find_path_option=
;;
*)
printf -- "etags-c ** Error Operating System, %s, is not supported!\n" "$(uname)"
exit 1
;;
esac
case "$search_with" in
find)
printf -- "etags-c -- ETAGS_USE_FIND set to 1: forces the use of find!\n"
printf -- " -> Using %s\n" "$(which find)"
find -L \
${find_path_option} "$@" \
\( -name "*.[ch]" \
-or -name "*.[ch].in" \
-or -name "*.[ch]++" \
-or -name "*.[ch]++.in" \
-or -name "*.[ch]pp" \
-or -name "*.[ch]pp.in" \
-or -name "*.[ch]xx" \
-or -name "*.[ch]xx.in" \
-or -name "*.[CH]" \
-or -name "*.[CH].in" \
-or -name "*.cc" \
-or -name "*.cc.in" \
-or -name "*.hh" \
-or -name "*.hh.in" \
-or -name "*.inl" \
-or -name "*.M" \
-or -name "*.pdb" \
\) -print | tee .etags-c-files.txt | run__etags_c
;;
fd)
printf -- "etags-c -- Using sharkdp/fd fast find.\n"
printf -- " -> Using %s\n" "$(which "${fd_pgm}")"
printf -- " -> ☝️ Ignoring files identified in .gitignore, .ignore or .fdignore\n"
printf -- " -> ☝️ You may want to run a search with find once to detect broken symlinks\n"
${fd_pgm} --absolute-path \
--follow \
--type f \
--type l \
-e .c \
-e .c.in \
-e .h \
-e .h.in \
-e .c++ \
-e .c++.in \
-e .h++ \
-e .h++.in \
-e .cpp \
-e .cpp.in \
-e .hpp \
-e .hpp.in \
-e .cxx \
-e .cxx.in \
-e .hxx \
-e .hxx.in \
-e .C \
-e .C.in \
-e .H \
-e .H.in \
-e .cc \
-e .cc.in \
-e .hh \
-e .hh.in \
-e .inl \
-e .M \
-e .pdb \
. "$@" | tee .etags-c-files.txt | run__etags_c
;;
*)
printf -- "etags-c ** Error: cannot identify find program!\n"
exit 1
;;
esac
fi
file_count="$(wc -l .etags-c-files.txt | awk '{print $1}')"
printf -- "%s files found and processed. List stored in: .etags-c-files.txt\n" "${file_count=}"
# ----------------------------------------------------------------------------
# Local Variables:
# sh-shell: sh
# End: