xref: /Universal-ctags/docs/interactive-mode.rst (revision 45e335ab6e42c701cb0966c081b8e1b20878ce01)
1053c1b24SAman Gupta.. _interactive-mode:
2053c1b24SAman Gupta
3053c1b24SAman Gupta======================================================================
402d99bdfSHiroo HAYASHIInteractive mode
5053c1b24SAman Gupta======================================================================
6053c1b24SAman Gupta
7dccba5efSHiroo HAYASHIUniversal Ctags can be run with ``--_interactive``, which enters a REPL that
84711ae33SJakub Wilkcan be used programmatically to control ctags generation. In this mode, json
9053c1b24SAman Guptacommands are received over stdin, and corresponding responses are emitted over
10053c1b24SAman Guptastdout.
11053c1b24SAman Gupta
129a407b99SVincent RischmannThis feature needs ctags to be built with json support and this requires libjansson to be installed
139a407b99SVincent Rischmannat build-time. If it's supported it will be listed in the output of ``--list-features``:
149a407b99SVincent Rischmann
159a407b99SVincent Rischmann.. code-block:: console
169a407b99SVincent Rischmann
17*45e335abSHiroo HAYASHI	$ ctags --list-features | grep json
189a407b99SVincent Rischmann	json
199a407b99SVincent Rischmann
20dccba5efSHiroo HAYASHICommunication with Universal Ctags over stdio uses the `json lines`_ format, where each
21053c1b24SAman Guptajson object appears on a single line and is terminated with a newline.
22053c1b24SAman Gupta
23053c1b24SAman GuptaWhen ``ctags --_interactive`` is invoked, it will emit a single json object to stdout announcing
24053c1b24SAman Guptaits name and version. This signals the start of the interactive loop, and the user can begin sending
25053c1b24SAman Guptacommands over stdin.
26053c1b24SAman Gupta
27053c1b24SAman Gupta.. code-block:: console
28053c1b24SAman Gupta
29053c1b24SAman Gupta    $ ctags --_interactive
30053c1b24SAman Gupta    {"_type": "program", "name": "Universal Ctags", "version": "0.0.0"}
31053c1b24SAman Gupta
32053c1b24SAman GuptaThe following commands are currently supported in interactive mode:
33053c1b24SAman Gupta
34053c1b24SAman Gupta- generate-tags_
35053c1b24SAman Gupta
36053c1b24SAman Guptagenerate-tags
37053c1b24SAman Gupta-------------
38053c1b24SAman Gupta
39053c1b24SAman GuptaThe ``generate-tags`` command takes two arguments:
40053c1b24SAman Gupta
41053c1b24SAman Gupta- ``filename``: name of the file to generate tags for (required)
42053c1b24SAman Gupta- ``size``: size in bytes of the file, if the contents will be received over stdin (optional)
43053c1b24SAman Gupta
44c59f2a21SMasatake YAMATOThe simplest way to generate tags for a file is by passing its path on filesystem(``file request``). The response will include
45053c1b24SAman Guptaone json object per line representing each tag, followed by a single json object with the ``completed``
46053c1b24SAman Guptafield emitted once the file has been fully processed.
47053c1b24SAman Gupta
48053c1b24SAman Gupta.. code-block:: console
49053c1b24SAman Gupta
50053c1b24SAman Gupta    $ echo '{"command":"generate-tags", "filename":"test.rb"}' | ctags --_interactive
51053c1b24SAman Gupta    {"_type": "program", "name": "Universal Ctags", "version": "0.0.0"}
52053c1b24SAman Gupta    {"_type": "tag", "name": "foobar", "path": "test.rb", "pattern": "/^  def foobar$/", "kind": "method", "scope": "Test", "scopeKind": "class"}
53053c1b24SAman Gupta    {"_type":"completed", "command": "generate-tags"}
54053c1b24SAman Gupta
55c59f2a21SMasatake YAMATOThe ``generate-tags`` command can also be used to generate tags for code which is not present on filesystem(``inline request``). For example,
56053c1b24SAman Guptaan IDE might want to generate ctags for an unsaved buffer while the user is editing code. When ``size`` is specified,
57053c1b24SAman Guptathe corresponding number of bytes are read over stdin after the json object and newline.
58053c1b24SAman Gupta
59053c1b24SAman Gupta.. code-block:: console
60053c1b24SAman Gupta
61053c1b24SAman Gupta    $ (
62053c1b24SAman Gupta      echo '{"command":"generate-tags", "filename":"test.rb", "size": 17}'
63053c1b24SAman Gupta      echo 'def foobaz() end'
64053c1b24SAman Gupta    ) | ctags --_interactive
65053c1b24SAman Gupta    {"_type": "program", "name": "Universal Ctags", "version": "0.0.0"}
66053c1b24SAman Gupta    {"_type": "tag", "name": "foobaz", "path": "test.rb", "pattern": "/^def foobaz() end$/", "kind": "method"}
67053c1b24SAman Gupta    {"_type": "completed", "command": "generate-tags"}
68053c1b24SAman Gupta
69053c1b24SAman Gupta.. _json lines: http://jsonlines.org/
70c59f2a21SMasatake YAMATO
71c59f2a21SMasatake YAMATO.. _sandbox-submode:
72c59f2a21SMasatake YAMATO
73c59f2a21SMasatake YAMATOsandbox submode
74c59f2a21SMasatake YAMATO--------------------------
75c59f2a21SMasatake YAMATO
76c59f2a21SMasatake YAMATO``sandbox`` submode can be used with ``--_interactive=sandbox``.  This
77c59f2a21SMasatake YAMATOsubmode will activate a sandbox, to this limits the damage that the
78dccba5efSHiroo HAYASHIcan be achieved when exploiting a buffer overflow in Universal Ctags.
79c59f2a21SMasatake YAMATO
80c59f2a21SMasatake YAMATOIn the sandbox submode ctags can generate tags only for inline
81c59f2a21SMasatake YAMATOrequests because ctags has to use open system call to handle file
82c59f2a21SMasatake YAMATOrequests. The open system call is not allowed in the sandbox.
83c59f2a21SMasatake YAMATO
8404cce070SHiroo HAYASHIThis feature uses `Seccomp BPF (SECure COMPuting with filters)
8504cce070SHiroo HAYASHI<https://www.kernel.org/doc/html/latest/userspace-api/seccomp_filter.html>`_,
8604cce070SHiroo HAYASHIand is only supported on Linux. To use the sandbox submode `libseccomp
8704cce070SHiroo HAYASHI<https://github.com/seccomp/libseccomp>`_ is needed at build-time. If ctags was
88c59f2a21SMasatake YAMATObuilt with seccomp support, ``sandbox`` is listed in the output of
89c59f2a21SMasatake YAMATO``--list-features`` option.
90c59f2a21SMasatake YAMATO
91c59f2a21SMasatake YAMATO.. code-block:: console
92c59f2a21SMasatake YAMATO
93*45e335abSHiroo HAYASHI	$ ctags --list-features | grep sandbox
94c59f2a21SMasatake YAMATO	sandbox
95c59f2a21SMasatake YAMATO
96c59f2a21SMasatake YAMATO.. code-block:: console
97c59f2a21SMasatake YAMATO
98c59f2a21SMasatake YAMATO    $ (
99c59f2a21SMasatake YAMATO      echo '{"command":"generate-tags", "filename":"test.rb", "size": 17}'
100c59f2a21SMasatake YAMATO      echo 'def foobaz() end'
101c59f2a21SMasatake YAMATO    ) | ctags --_interactive=sandbox
102c59f2a21SMasatake YAMATO    {"_type": "program", "name": "Universal Ctags", "version": "0.0.0"}
103c59f2a21SMasatake YAMATO    {"_type": "tag", "name": "foobaz", "path": "test.rb", "pattern": "/^def foobaz() end$/", "kind": "method"}
104c59f2a21SMasatake YAMATO    {"_type": "completed", "command": "generate-tags"}
105