Skip to content

Commit 2d875dd

Browse files
committed
Show extra help with multiple -h,--help. Show ctags command-line
1 parent 0f3b14d commit 2d875dd

File tree

4 files changed

+95
-58
lines changed

4 files changed

+95
-58
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/Ctags.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.nio.charset.StandardCharsets;
3535
import java.time.Duration;
3636
import java.util.ArrayList;
37+
import java.util.Collections;
3738
import java.util.List;
3839
import java.util.concurrent.ExecutionException;
3940
import java.util.concurrent.ExecutorService;
@@ -61,6 +62,7 @@ public class Ctags implements Resettable {
6162
private volatile boolean closing;
6263
private final LangTreeMap defaultLangMap = new LangTreeMap();
6364
private LangMap langMap;
65+
private List<String> command;
6466
private Process ctags;
6567
private OutputStreamWriter ctagsIn;
6668
private BufferedReader ctagsOut;
@@ -136,9 +138,17 @@ public void close() {
136138
}
137139
}
138140

139-
private void initialize() throws IOException {
140-
ProcessBuilder processBuilder;
141-
List<String> command = new ArrayList<>();
141+
/**
142+
* Gets the command-line arguments used to run ctags.
143+
* @return a defined (immutable) list
144+
*/
145+
public List<String> getArgv() {
146+
initialize();
147+
return Collections.unmodifiableList(command);
148+
}
149+
150+
private void initialize() {
151+
command = new ArrayList<>();
142152

143153
command.add(binary);
144154
command.add("--c-kinds=+l");
@@ -201,18 +211,20 @@ private void initialize() throws IOException {
201211

202212
/* Add extra command line options for ctags. */
203213
if (CTagsExtraOptionsFile != null) {
204-
LOGGER.log(Level.INFO, "Adding extra options to ctags");
214+
LOGGER.log(Level.FINER, "Adding extra options to ctags");
205215
command.add("--options=" + CTagsExtraOptionsFile);
206216
}
217+
}
207218

219+
private void run() throws IOException {
208220
StringBuilder sb = new StringBuilder();
209221
for (String s : command) {
210222
sb.append(s).append(" ");
211223
}
212224
String commandStr = sb.toString();
213225
LOGGER.log(Level.FINE, "Executing ctags command [{0}]", commandStr);
214226

215-
processBuilder = new ProcessBuilder(command);
227+
ProcessBuilder processBuilder = new ProcessBuilder(command);
216228

217229
ctags = processBuilder.start();
218230
ctagsIn = new OutputStreamWriter(
@@ -408,6 +420,7 @@ public Definitions doCtags(String file) throws IOException,
408420
}
409421
} else {
410422
initialize();
423+
run();
411424
}
412425

413426
CtagsReader rdr = new CtagsReader();

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.opengrok.indexer.Info;
5353
import org.opengrok.indexer.analysis.AnalyzerGuru;
5454
import org.opengrok.indexer.analysis.AnalyzerGuruHelp;
55+
import org.opengrok.indexer.analysis.Ctags;
5556
import org.opengrok.indexer.configuration.Configuration;
5657
import org.opengrok.indexer.configuration.ConfigurationHelp;
5758
import org.opengrok.indexer.configuration.LuceneLockName;
@@ -64,6 +65,7 @@
6465
import org.opengrok.indexer.index.IndexVersion.IndexVersionException;
6566
import org.opengrok.indexer.logger.LoggerFactory;
6667
import org.opengrok.indexer.logger.LoggerUtil;
68+
import org.opengrok.indexer.util.CtagsUtil;
6769
import org.opengrok.indexer.util.Executor;
6870
import org.opengrok.indexer.util.OptionParser;
6971
import org.opengrok.indexer.util.Statistics;
@@ -107,9 +109,8 @@ public final class Indexer {
107109
private static boolean bareConfig = false;
108110
private static boolean awaitProfiler;
109111

110-
private static boolean help;
112+
private static int help;
111113
private static String helpUsage;
112-
private static boolean helpDetailed;
113114

114115
private static String configFilename = null;
115116
private static int status = 0;
@@ -146,13 +147,16 @@ public static void main(String[] argv) {
146147

147148
try {
148149
argv = parseOptions(argv);
149-
if (help) {
150+
if (help > 0) {
150151
PrintStream helpStream = status != 0 ? System.err : System.out;
151152
helpStream.println(helpUsage);
152-
if (helpDetailed) {
153+
if (help > 1) {
153154
helpStream.println(AnalyzerGuruHelp.getUsage());
154155
helpStream.println(ConfigurationHelp.getSamples());
155156
}
157+
if (help > 2) {
158+
helpStream.println(getCtagsCommand());
159+
}
156160
System.exit(status);
157161
}
158162

@@ -425,13 +429,13 @@ public static String[] parseOptions(String[] argv) throws ParseException {
425429
* Pre-match any of the --help options so that some possible exception-
426430
* generating args handlers (e.g. -R) can be short-circuited.
427431
*/
428-
help = Arrays.stream(argv).anyMatch(s -> HELP_OPT_1.equals(s) ||
432+
boolean preHelp = Arrays.stream(argv).anyMatch(s -> HELP_OPT_1.equals(s) ||
429433
HELP_OPT_2.equals(s) || HELP_OPT_3.equals(s));
430434

431435
OptionParser configure = OptionParser.scan(parser -> {
432436
parser.on("-R configPath").Do(cfgFile -> {
433437
try {
434-
if (!help) {
438+
if (!preHelp) {
435439
cfg = Configuration.read(new File((String) cfgFile));
436440
}
437441
} catch (IOException e) {
@@ -448,14 +452,11 @@ public static String[] parseOptions(String[] argv) throws ParseException {
448452
String.format("\nUsage: java -jar %s [options] [subDir1 [...]]\n", program));
449453

450454
parser.on(HELP_OPT_3, Indexer.HELP_OPT_2, HELP_OPT_1,
451-
"Display this usage summary.").Do(v -> {
452-
help = true;
453-
helpUsage = parser.getUsage();
455+
"Display this usage summary. Repeat for more detailed help.").Do(v -> {
456+
++help;
457+
helpUsage = parser.getUsage();
454458
});
455459

456-
parser.on("--detailed",
457-
"Display additional help with -h,--help.").Do(v -> helpDetailed = true);
458-
459460
parser.on(
460461
"-A (.ext|prefix.):(-|analyzer)", "--analyzer", "/(\\.\\w+|\\w+\\.):(-|[a-zA-Z_0-9.]+)/",
461462
"Files with the named prefix/extension should be analyzed",
@@ -1116,6 +1117,36 @@ private static void pauseToAwaitProfiler() {
11161117
}
11171118
}
11181119

1120+
private static String getCtagsCommand() {
1121+
StringBuilder result = new StringBuilder();
1122+
Ctags ctags = CtagsUtil.newInstance(env);
1123+
List<String> argv = ctags.getArgv();
1124+
for (int i = 0; i < argv.size(); ++i) {
1125+
if (i > 0) {
1126+
result.append("\t");
1127+
}
1128+
String arg = argv.get(i);
1129+
if (arg == null) {
1130+
result.append("UNDEFINED");
1131+
} else {
1132+
result.append(maybeEscapeForSh(arg));
1133+
}
1134+
if (i + 1 < argv.size()) {
1135+
result.append(" \\");
1136+
}
1137+
result.append("\n");
1138+
}
1139+
return result.toString();
1140+
}
1141+
1142+
private static String maybeEscapeForSh(String value) {
1143+
if (!value.matches(".*[^-:.+=a-zA-Z0-9_].*")) {
1144+
return value;
1145+
}
1146+
return "$'" + value.replace("\\", "\\\\").replace("'", "\\'").replace("\n", "\\n").
1147+
replace("\r", "\\r").replace("\t", "\\t") + "'";
1148+
}
1149+
11191150
private Indexer() {
11201151
}
11211152
}

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexerParallelizer.java

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@
2727
import java.util.concurrent.Executors;
2828
import java.util.concurrent.ForkJoinPool;
2929
import java.util.concurrent.ScheduledThreadPoolExecutor;
30-
import java.util.logging.Logger;
3130

32-
import org.opengrok.indexer.analysis.AnalyzerGuru;
3331
import org.opengrok.indexer.analysis.Ctags;
3432
import org.opengrok.indexer.analysis.CtagsValidator;
3533
import org.opengrok.indexer.configuration.RuntimeEnvironment;
36-
import org.opengrok.indexer.logger.LoggerFactory;
3734
import org.opengrok.indexer.util.BoundedBlockingObjectPool;
35+
import org.opengrok.indexer.util.CtagsUtil;
3836
import org.opengrok.indexer.util.LazilyInstantiate;
3937
import org.opengrok.indexer.util.ObjectFactory;
4038
import org.opengrok.indexer.util.ObjectPool;
@@ -50,9 +48,6 @@
5048
*/
5149
public class IndexerParallelizer implements AutoCloseable {
5250

53-
private static final Logger LOGGER =
54-
LoggerFactory.getLogger(IndexerParallelizer.class);
55-
5651
private final RuntimeEnvironment env;
5752
private final int indexingParallelism;
5853

@@ -228,7 +223,7 @@ private void createLazyForkJoinPool() {
228223
private void createLazyCtagsPool() {
229224
lzCtagsPool = LazilyInstantiate.using(() ->
230225
new BoundedBlockingObjectPool<>(indexingParallelism,
231-
new CtagsValidator(), new CtagsObjectFactory(env)));
226+
new CtagsValidator(), new CtagsObjectFactory()));
232227
}
233228

234229
private void createLazyCtagsWatcherExecutor() {
@@ -255,41 +250,10 @@ private void createLazyHistoryRenamedExecutor() {
255250
Executors.newFixedThreadPool(env.getHistoryRenamedParallelism()));
256251
}
257252

258-
/**
259-
* Creates a new instance, and attempts to configure it from the specified
260-
* environment instance.
261-
* @return a defined instance, possibly with a {@code null} ctags binary
262-
* setting if a value was not available from {@link RuntimeEnvironment}.
263-
*/
264-
private static Ctags getNewCtags(RuntimeEnvironment env) {
265-
Ctags ctags = new Ctags();
266-
267-
String ctagsBinary = env.getCtags();
268-
if (ctagsBinary == null) {
269-
LOGGER.severe("Unable to run ctags!" +
270-
" searching definitions will not work!");
271-
} else {
272-
ctags.setBinary(ctagsBinary);
273-
ctags.setLangMap(AnalyzerGuru.getLangMap());
274-
275-
String filename = env.getCTagsExtraOptionsFile();
276-
if (filename != null) {
277-
ctags.setCTagsExtraOptionsFile(filename);
278-
}
279-
}
280-
return ctags;
281-
}
282-
283-
private class CtagsObjectFactory implements ObjectFactory<Ctags> {
284-
285-
private final RuntimeEnvironment env;
286-
287-
CtagsObjectFactory(RuntimeEnvironment env) {
288-
this.env = env;
289-
}
253+
private static class CtagsObjectFactory implements ObjectFactory<Ctags> {
290254

291255
public Ctags createNew() {
292-
return getNewCtags(env);
256+
return CtagsUtil.newInstance(RuntimeEnvironment.getInstance());
293257
}
294258
}
295259
}

opengrok-indexer/src/main/java/org/opengrok/indexer/util/CtagsUtil.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919

2020
/*
2121
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
2223
*/
2324

2425
package org.opengrok.indexer.util;
2526

27+
import org.opengrok.indexer.analysis.AnalyzerGuru;
28+
import org.opengrok.indexer.analysis.Ctags;
29+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
2630
import org.opengrok.indexer.logger.LoggerFactory;
2731

2832
import java.util.logging.Level;
@@ -54,6 +58,31 @@ public static boolean validate(String ctagsBinary) {
5458
return true;
5559
}
5660

57-
private CtagsUtil() {
61+
/**
62+
* Creates a new instance, and attempts to configure it from the
63+
* environment.
64+
* @return a defined instance, possibly with a {@code null} ctags binary
65+
* setting if a value was not available from {@link RuntimeEnvironment}.
66+
*/
67+
public static Ctags newInstance(RuntimeEnvironment env) {
68+
Ctags ctags = new Ctags();
69+
70+
String ctagsBinary = env.getCtags();
71+
if (ctagsBinary == null) {
72+
LOGGER.severe("Unable to run ctags! Searching definitions will not work!");
73+
} else {
74+
ctags.setBinary(ctagsBinary);
75+
ctags.setLangMap(AnalyzerGuru.getLangMap());
76+
77+
String filename = env.getCTagsExtraOptionsFile();
78+
if (filename != null) {
79+
ctags.setCTagsExtraOptionsFile(filename);
80+
}
5881
}
82+
return ctags;
83+
}
84+
85+
/** Private to enforce static. */
86+
private CtagsUtil() {
87+
}
5988
}

0 commit comments

Comments
 (0)