fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerMerge changes to config paths6d0cd3d9b

master
45.4 KiB1530 linesraw
1import binascii
2import errno
3import shlex
4import glob
5import http.server as SimpleHTTPServer
6import json
7import multiprocessing.pool
8import operator
9import os
10import platform
11import re
12import select
13import socket
14import socketserver as SocketServer
15import subprocess
16import sys
17import tempfile
18import threading
19from html import escape as escape_html
20from itertools import chain
21from typing import Optional
22from urllib.parse import parse_qs
23
24COMMON_WSL_CMD_PATHS = (
25    "/mnt/c/Windows/System32",
26    "/windir/c/Windows/System32",
27    "/c/Windows/System32",
28)
29FISH_BIN_PATH = False  # will be set later
30
31
32ENTER_BOLD_MODE = "\x1b[1m"
33EXIT_ATTRIBUTE_MODE = "\x1b[m"
34ENTER_UNDERLINE_MODE = "\x1b[4m"
35
36# Disable CLI web browsers
37term = os.environ.pop("TERM", None)
38# This import must be done with an empty $TERM, otherwise a command-line browser may be started
39# which will block the whole process - see https://docs.python.org/3/library/webbrowser.html
40import webbrowser
41
42if term is not None:
43    os.environ["TERM"] = term
44
45TERMINAL_COLOR_THEME = os.environ["__fish_terminal_color_theme"]
46
47
48def find_executable(exe, paths=()):
49    final_path = os.environ["PATH"].split(os.pathsep)
50    if paths:
51        final_path.extend(paths)
52    for p in final_path:
53        proposed_path = os.path.join(p, exe)
54        if os.access(proposed_path, os.X_OK):
55            return proposed_path
56
57
58def is_wsl():
59    """Return whether we are running under the Windows Subsystem for Linux"""
60    if "linux" in platform.system().lower() and os.access("/proc/version", os.R_OK):
61        with open("/proc/version", "r") as f:
62            # Find 'Microsoft' for wsl1 and 'microsoft' for wsl2
63            if "microsoft" in f.read().lower():
64                return True
65    return False
66
67
68def is_windows():
69    """Return whether we are running under the Windows"""
70    return sys.platform.startswith("win")
71
72
73def is_sailfish_os():
74    """Return whether we are running on Sailfish OS"""
75    if "linux" in platform.system().lower() and os.access(
76        "/etc/sailfish-release", os.R_OK
77    ):
78        with open("/etc/sailfish-release", "r") as f:
79            # Find 'ID=sailfishos'
80            if "sailfishos" in f.read():
81                return True
82    return False
83
84
85def is_termux():
86    """Return whether we are running under the Termux application for Android"""
87    return "com.termux" in os.environ["PATH"] and find_executable("termux-open-url")
88
89
90def is_chromeos_garcon():
91    """Return whether we are running in Chrome OS and the browser can't see local files"""
92    # In Crostini Chrome OS Linux, the default browser opens URLs in Chrome
93    # running outside the linux VM. This browser does not have access to the
94    # Linux filesystem. This uses Garcon, see for example
95    # https://chromium.googlesource.com/chromiumos/platform2/+/master/vm_tools/garcon/#opening-urls
96    # https://source.chromium.org/search?q=garcon-url-handler
97    try:
98        return "garcon-url-handler" in webbrowser.get().name
99    except:
100        return False
101
102
103def run_fish_cmd(text, strict=False, env=None):
104    print("$ " + text)
105    p = subprocess.Popen(
106        [FISH_BIN_PATH],
107        stdin=subprocess.PIPE,
108        stdout=subprocess.PIPE,
109        stderr=subprocess.PIPE,
110        env=env,
111    )
112    out, err = p.communicate(text.encode("utf-8"))
113    out = out.decode("utf-8", "replace")
114    err = err.decode("utf-8", "replace")
115    if strict:
116        assert err == ""
117        # TODO use check_output()
118        return out
119    return out, err
120
121
122def list_embedded_files(path, suffix):
123    return [
124        path
125        for path in run_fish_cmd(
126            "status list-files " + escape_fish_cmd(path), strict=True
127        ).splitlines()
128        if path.endswith(suffix)
129    ]
130
131
132def get_embedded_file(path):
133    return run_fish_cmd("status get-file " + escape_fish_cmd(path), strict=True)
134
135
136def escape_fish_cmd(text):
137    # Replace one backslash with two, and single quotes with backslash-quote
138    escaped = text.replace("\\", "\\\\").replace("'", "\\'")
139    return "'" + escaped + "'"
140
141
142def strip_one_layer(text, char):
143    # Strip the text from one layer of a given character
144    if text[-1] == char:
145        text = text[:-1]
146    if text[0] == char:
147        text = text[1:]
148    return text
149
150
151named_colors = {
152    "black": "000000",
153    "red": "800000",
154    "green": "008000",
155    "brown": "725000",
156    "yellow": "808000",
157    "blue": "000080",
158    "magenta": "800080",
159    "purple": "800080",
160    "cyan": "008080",
161    "grey": "e5e5e5",
162    "brgrey": "555555",
163    "white": "c0c0c0",
164    "brblack": "808080",
165    "brred": "ff0000",
166    "brgreen": "00ff00",
167    "brbrown": "ffff00",
168    "bryellow": "ffff00",
169    "brblue": "0000ff",
170    "brmagenta": "ff00ff",
171    "brpurple": "ff00ff",
172    "brcyan": "00ffff",
173    "brwhite": "ffffff",
174}
175
176bindings_blacklist = set(["self-insert", "'begin;end'"])
177
178
179def parse_one_color(comp):
180    """A basic function to parse a single color value like 'FFA000'"""
181    if comp in named_colors:
182        # Named color
183        return named_colors[comp]
184    elif (
185        re.match(r"[0-9a-fA-F]{3}", comp) is not None
186        or re.match(r"[0-9a-fA-F]{6}", comp) is not None
187    ):
188        # Hex color
189        return comp
190    else:
191        # Unknown
192        return ""
193
194
195def better_color(c1, c2):
196    """Indicate which color is "better", i.e. prefer term256 colors"""
197    if not c2:
198        return c1
199    if not c1:
200        return c2
201    if c1 == "normal":
202        return c2
203    if c2 == "normal":
204        return c1
205    if c2 in named_colors:
206        return c1
207    if c1 in named_colors:
208        return c2
209    return c1
210
211
212def parse_colors(colors_str):
213    for words in map(shlex.split, colors_str.splitlines()):
214        assert re.match(
215            r"^((?:fish_color_|fish_pager_color_)\w+)$",
216            words[0],
217        )
218    return {
219        "colors": sorted(
220            [
221                {"name": words[0]} | parse_color(words[1:])
222                for words in map(shlex.split, colors_str.splitlines())
223            ],
224            key=operator.itemgetter("name"),
225        ),
226    }
227
228
229def parse_color(comps):
230    """A basic function to parse a color string, for example, ['red', '--bold']."""
231    color = ""
232    background_color = ""
233    underline_color = ""
234    bold = False
235    underline = None
236    italics = False
237    strikethrough = False
238    dim = False
239    reverse = False
240    i = 0
241    while i < len(comps):
242        comp = comps[i]
243        if comp == "--bold" or comp == "-o":
244            bold = True
245        elif comp == "--underline" or comp == "-u":
246            underline = "single"
247        elif comp.startswith("--underline="):
248            # TODO(python>3.8): use removeprefix
249            underline = comp[len("--underline=") :]
250        elif comp.startswith(
251            "-u"
252        ):  # Multiple short options like "-rucurly" are not yet supported.
253            # TODO(python>3.8): use removeprefix
254            underline = comp[len("-u") :]
255        elif comp == "--italics" or comp == "-i":
256            italics = True
257        elif comp == "--dim" or comp == "-d":
258            dim = True
259        elif comp == "--reverse" or comp == "-r":
260            reverse = True
261        elif comp == "--strikethrough" or comp == "-s":
262            strikethrough = True
263        elif comp.startswith("--theme="):
264            pass  # Not yet supported here.
265        else:
266
267            def parse_opt(
268                current_best: str, i: int, long_opt: str, short_opt: Optional[str]
269            ) -> str:
270                if comp.startswith(long_opt):
271                    c = comp[len(long_opt) :]
272                    if c[0] == "=":
273                        # There was a = between the long option and the value.
274                        # i.e. support also --background=red, not just --background red
275                        c = c[1:]
276                    parsed_c = parse_one_color(c)
277                    # We prefer the unparsed version - if it says "brgreen", we use brgreen,
278                    # instead of 00ff00
279                    if better_color(current_best, parsed_c) == parsed_c:
280                        return True, c, i
281                elif short_opt is not None and comp.startswith(short_opt):
282                    if comp == short_opt:
283                        if i + 1 == len(comps):
284                            c = ""
285                        else:
286                            c = comps[i + 1]
287                            i += 1
288                    else:
289                        c = comp[len(short_opt) :]
290                    parsed_c = parse_one_color(c)
291                    if better_color(current_best, parsed_c) == parsed_c:
292                        return True, c, i
293                return False, current_best, i
294
295            is_bg, background_color, i = parse_opt(
296                background_color, i, "--background", "-b"
297            )
298            is_ul, underline_color, i = parse_opt(
299                underline_color, i, "--underline-color", None
300            )
301            if not (is_bg or is_ul):
302                # Regular color
303                parsed_c = parse_one_color(comp)
304                if better_color(color, parsed_c) == parsed_c:
305                    color = comp
306        i += 1
307
308    return {
309        "color": color,
310        "background": background_color,
311        "underline-color": underline_color,
312        "bold": bold,
313        "underline": underline,
314        "italics": italics,
315        "dim": dim,
316        "reverse": reverse,
317        "strikethrough": strikethrough,
318    }
319
320
321def unparse_color(col):
322    """A basic function to return the fish version of a color dict"""
323    if isinstance(col, str):
324        return col
325    ret = ""
326    if col["color"]:
327        ret += col["color"]
328    if col["bold"]:
329        ret += " --bold"
330    if col["underline"] is not None:
331        ret += " --underline=" + str(col["underline"])
332    if col["italics"]:
333        ret += " --italics"
334    if col["dim"]:
335        ret += " --dim"
336    if col["reverse"]:
337        ret += " --reverse"
338    if col["strikethrough"]:
339        ret += " --strikethrough"
340    if col["background"]:
341        ret += " --background=" + col["background"]
342    if col["underline-color"]:
343        ret += " --underline-color=" + col["underline-color"]
344    return ret
345
346
347def parse_bool(val):
348    val = val.lower()
349    if val.startswith("f") or val.startswith("0"):
350        return False
351    if val.startswith("t") or val.startswith("1"):
352        return True
353    return bool(val)
354
355
356def html_color_for_ansi_color_index(val):
357    arr = [
358        "black",
359        "#FF0000",
360        "#00FF00",
361        "#AA5500",
362        "#0000FF",
363        "#AA00AA",
364        "#00AAAA",
365        "#AAAAAA",
366        "#555555",
367        "#FF5555",
368        "#55FF55",
369        "#FFFF55",
370        "#5555FF",
371        "#FF55FF",
372        "#55FFFF",
373        "white",
374        "#000000",
375        "#00005f",
376        "#000087",
377        "#0000af",
378        "#0000d7",
379        "#0000ff",
380        "#005f00",
381        "#005f5f",
382        "#005f87",
383        "#005faf",
384        "#005fd7",
385        "#005fff",
386        "#008700",
387        "#00875f",
388        "#008787",
389        "#0087af",
390        "#0087d7",
391        "#0087ff",
392        "#00af00",
393        "#00af5f",
394        "#00af87",
395        "#00afaf",
396        "#00afd7",
397        "#00afff",
398        "#00d700",
399        "#00d75f",
400        "#00d787",
401        "#00d7af",
402        "#00d7d7",
403        "#00d7ff",
404        "#00ff00",
405        "#00ff5f",
406        "#00ff87",
407        "#00ffaf",
408        "#00ffd7",
409        "#00ffff",
410        "#5f0000",
411        "#5f005f",
412        "#5f0087",
413        "#5f00af",
414        "#5f00d7",
415        "#5f00ff",
416        "#5f5f00",
417        "#5f5f5f",
418        "#5f5f87",
419        "#5f5faf",
420        "#5f5fd7",
421        "#5f5fff",
422        "#5f8700",
423        "#5f875f",
424        "#5f8787",
425        "#5f87af",
426        "#5f87d7",
427        "#5f87ff",
428        "#5faf00",
429        "#5faf5f",
430        "#5faf87",
431        "#5fafaf",
432        "#5fafd7",
433        "#5fafff",
434        "#5fd700",
435        "#5fd75f",
436        "#5fd787",
437        "#5fd7af",
438        "#5fd7d7",
439        "#5fd7ff",
440        "#5fff00",
441        "#5fff5f",
442        "#5fff87",
443        "#5fffaf",
444        "#5fffd7",
445        "#5fffff",
446        "#870000",
447        "#87005f",
448        "#870087",
449        "#8700af",
450        "#8700d7",
451        "#8700ff",
452        "#875f00",
453        "#875f5f",
454        "#875f87",
455        "#875faf",
456        "#875fd7",
457        "#875fff",
458        "#878700",
459        "#87875f",
460        "#878787",
461        "#8787af",
462        "#8787d7",
463        "#8787ff",
464        "#87af00",
465        "#87af5f",
466        "#87af87",
467        "#87afaf",
468        "#87afd7",
469        "#87afff",
470        "#87d700",
471        "#87d75f",
472        "#87d787",
473        "#87d7af",
474        "#87d7d7",
475        "#87d7ff",
476        "#87ff00",
477        "#87ff5f",
478        "#87ff87",
479        "#87ffaf",
480        "#87ffd7",
481        "#87ffff",
482        "#af0000",
483        "#af005f",
484        "#af0087",
485        "#af00af",
486        "#af00d7",
487        "#af00ff",
488        "#af5f00",
489        "#af5f5f",
490        "#af5f87",
491        "#af5faf",
492        "#af5fd7",
493        "#af5fff",
494        "#af8700",
495        "#af875f",
496        "#af8787",
497        "#af87af",
498        "#af87d7",
499        "#af87ff",
500        "#afaf00",
501        "#afaf5f",
502        "#afaf87",
503        "#afafaf",
504        "#afafd7",
505        "#afafff",
506        "#afd700",
507        "#afd75f",
508        "#afd787",
509        "#afd7af",
510        "#afd7d7",
511        "#afd7ff",
512        "#afff00",
513        "#afff5f",
514        "#afff87",
515        "#afffaf",
516        "#afffd7",
517        "#afffff",
518        "#d70000",
519        "#d7005f",
520        "#d70087",
521        "#d700af",
522        "#d700d7",
523        "#d700ff",
524        "#d75f00",
525        "#d75f5f",
526        "#d75f87",
527        "#d75faf",
528        "#d75fd7",
529        "#d75fff",
530        "#d78700",
531        "#d7875f",
532        "#d78787",
533        "#d787af",
534        "#d787d7",
535        "#d787ff",
536        "#d7af00",
537        "#d7af5f",
538        "#d7af87",
539        "#d7afaf",
540        "#d7afd7",
541        "#d7afff",
542        "#d7d700",
543        "#d7d75f",
544        "#d7d787",
545        "#d7d7af",
546        "#d7d7d7",
547        "#d7d7ff",
548        "#d7ff00",
549        "#d7ff5f",
550        "#d7ff87",
551        "#d7ffaf",
552        "#d7ffd7",
553        "#d7ffff",
554        "#ff0000",
555        "#ff005f",
556        "#ff0087",
557        "#ff00af",
558        "#ff00d7",
559        "#ff00ff",
560        "#ff5f00",
561        "#ff5f5f",
562        "#ff5f87",
563        "#ff5faf",
564        "#ff5fd7",
565        "#ff5fff",
566        "#ff8700",
567        "#ff875f",
568        "#ff8787",
569        "#ff87af",
570        "#ff87d7",
571        "#ff87ff",
572        "#ffaf00",
573        "#ffaf5f",
574        "#ffaf87",
575        "#ffafaf",
576        "#ffafd7",
577        "#ffafff",
578        "#ffd700",
579        "#ffd75f",
580        "#ffd787",
581        "#ffd7af",
582        "#ffd7d7",
583        "#ffd7ff",
584        "#ffff00",
585        "#ffff5f",
586        "#ffff87",
587        "#ffffaf",
588        "#ffffd7",
589        "#ffffff",
590        "#080808",
591        "#121212",
592        "#1c1c1c",
593        "#262626",
594        "#303030",
595        "#3a3a3a",
596        "#444444",
597        "#4e4e4e",
598        "#585858",
599        "#626262",
600        "#6c6c6c",
601        "#767676",
602        "#808080",
603        "#8a8a8a",
604        "#949494",
605        "#9e9e9e",
606        "#a8a8a8",
607        "#b2b2b2",
608        "#bcbcbc",
609        "#c6c6c6",
610        "#d0d0d0",
611        "#dadada",
612        "#e4e4e4",
613        "#eeeeee",
614    ]
615    if val < 0 or val >= len(arr):
616        return ""
617    else:
618        return arr[val]
619
620
621# Given a known ANSI escape sequence, convert it to HTML and append to the list
622# Returns whether we have an open <span>
623
624
625def append_html_for_ansi_escape(full_val, result, span_open):
626    # Strip off the initial \x1b[ and terminating m
627    val = full_val[2:-1]
628
629    # Helper function to close a span if it's open
630    def close_span():
631        if span_open:
632            result.append("</span>")
633
634    # term24bit foreground color
635    match = re.match(r"38;2;(\d+);(\d+);(\d+)", val)
636    if match is not None:
637        close_span()
638        # Just use the rgb values directly
639        html_color = "#%02x%02x%02x" % (
640            int(match.group(1)),
641            int(match.group(2)),
642            int(match.group(3)),
643        )
644        result.append('<span style="color: ' + html_color + '">')
645        return True  # span now open
646
647    # term256 foreground color
648    match = re.match(r"38;5;(\d+)", val)
649    if match is not None:
650        close_span()
651        html_color = html_color_for_ansi_color_index(int(match.group(1)))
652        result.append('<span style="color: ' + html_color + '">')
653        return True  # span now open
654
655    # term16 foreground color
656    if val in (str(x) for x in chain(range(90, 97), range(30, 38))):
657        close_span()
658        html_color = html_color_for_ansi_color_index(
659            int(val) - (30 if int(val) < 90 else 82)
660        )
661        result.append('<span style="color: ' + html_color + '">')
662        return True  # span now open
663
664    # Try special escapes
665    if full_val == EXIT_ATTRIBUTE_MODE:
666        close_span()
667        return False
668
669    # TODO We don't handle bold, underline, italics, dim, strikethrough, or reverse yet
670
671    # Do nothing on failure
672    return span_open
673
674
675def strip_ansi(val):
676    # Make a half-assed effort to strip ANSI control sequences
677    # We assume that all such sequences start with 0x1b and end with m or ctrl-o,
678    # which catches most cases
679    return re.sub("\x1b[^m]*m\x0f?", "", val)
680
681
682def ansi_prompt_line_width(val):
683    # Given an ANSI prompt, return the length of its longest line, as in the
684    # number of characters it takes up. Start by stripping off ANSI.
685    stripped_val = strip_ansi(val)
686
687    # Now count the longest line
688    return max([len(x) for x in stripped_val.split("\n")])
689
690
691def ansi_to_html(val):
692    # Split us up by ANSI escape sequences. We want to catch not only the
693    # standard color codes, but also things like sgr0. Hence this lame check.
694    # Note that Python 2.6 doesn't have a flag param to re.split, so we have to
695    # compile it first.
696    reg = re.compile(
697        """
698        (                        # Capture
699         \x1b                    # Escape
700         [^m]*                   # Zero or more non-'m's
701         m                       # Literal m terminates the sequence
702         \x0f?                   # HACK: A ctrl-o - this is how tmux' sgr0 ends
703        )                        # End capture
704        """,
705        re.VERBOSE,
706    )
707    separated = reg.split(val)
708
709    # We have to HTML escape the text and convert ANSI escapes into HTML
710    # Collect it all into this array
711    result = []
712
713    span_open = False
714
715    # Text is at even indexes, escape sequences at odd indexes
716    for i in range(len(separated)):
717        component = separated[i]
718        if i % 2 == 0:
719            # It's text, possibly empty
720            # Clean up other ANSI junk
721            result.append(escape_html(strip_ansi(component)))
722        else:
723            # It's an escape sequence. Close the previous escape.
724            span_open = append_html_for_ansi_escape(component, result, span_open)
725
726    # Close final escape
727    if span_open:
728        result.append("</span>")
729
730    # Remove empty elements
731    result = [x for x in result if x]
732
733    # Clean up empty spans, the nasty way
734    idx = len(result) - 1
735    while idx >= 1:
736        if result[idx] == "</span>" and result[idx - 1].startswith("<span"):
737            # Empty span, delete these two
738            result[idx - 1 : idx + 1] = []
739            idx = idx - 1
740        idx = idx - 1
741
742    return "".join(result)
743
744
745class FishVar:
746    """A class that represents a variable"""
747
748    def __init__(self, name, value):
749        self.name = name
750        self.value = value
751        self.universal = False
752        self.exported = False
753
754    def get_json_obj(self):
755        # Return an array(3): name, value, flags
756        flags = []
757        if self.universal:
758            flags.append("universal")
759        if self.exported:
760            flags.append("exported")
761        return {"name": self.name, "value": self.value, "Flags": ", ".join(flags)}
762
763
764class FishBinding:
765    """A class that represents keyboard binding"""
766
767    def __init__(self, command, raw_binding, readable_binding):
768        self.command = command
769        self.bindings = []
770        self.add_binding(raw_binding, readable_binding)
771
772    def add_binding(self, raw_binding, readable_binding):
773        for i in self.bindings:
774            if i["readable_binding"] == readable_binding:
775                i["raw_bindings"].append(raw_binding)
776                break
777        else:
778            self.bindings.append(
779                {"readable_binding": readable_binding, "raw_bindings": [raw_binding]}
780            )
781
782    def get_json_obj(self):
783        return {
784            "command": self.command,
785            "bindings": self.bindings,
786        }
787
788
789class FishConfigTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
790    """TCPServer that only accepts connections from localhost (IPv4/IPv6)."""
791
792    WHITELIST = set(["::1", "::ffff:127.0.0.1", "127.0.0.1"])
793
794    address_family = socket.AF_INET6 if socket.has_ipv6 else socket.AF_INET
795
796    def verify_request(self, request, client_address):
797        return client_address[0] in FishConfigTCPServer.WHITELIST
798
799
800class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
801    def write_to_wfile(self, txt):
802        self.wfile.write(txt.encode("utf-8"))
803
804    def do_get_colors(self):
805        out, err = run_fish_cmd(
806            ""
807            + "if set -q __fish_initialized && test $__fish_initialized -lt 4300\n"
808            + "    echo >&2 missing migration?\n"
809            + "    exit 1\n"
810            + "end\n"
811            + "__fish_color_theme={} __fish_apply_theme 2>/dev/null\n".format(
812                TERMINAL_COLOR_THEME
813            )
814            + "or __fish_color_theme=unknown __fish_apply_theme\n"
815            + "__fish_theme_export_for_webconfig",
816            env=os.environ | {"__fish_force_load_default_theme": "1"},
817        )
818        assert err == ""
819
820        words = iter(shlex.split(out))
821        current = next(words)
822        themes = [
823            {
824                "theme": "Current",
825                "by_color_theme": {
826                    # Hack-ish: we don't know about the light/dark variants.
827                    "unknown": parse_colors(current)
828                    | {
829                        "preferred_background": "white"
830                        if TERMINAL_COLOR_THEME == "light"
831                        else "black",
832                    }
833                },
834                "terminal_color_theme": TERMINAL_COLOR_THEME,
835            }
836        ]
837        while True:
838            try:
839                theme_name = next(words)
840            except StopIteration:
841                break
842            theme = {
843                "theme": theme_name,
844            }
845            pretty_name = next(words)
846            if pretty_name:
847                theme["name"] = pretty_name
848            url = next(words)
849            if url:
850                theme["url"] = url
851            by_color_theme = {}
852            for _i in range(int(next(words))):
853                color_theme = next(words)
854                preferred_background = next(words)
855                data = next(words)
856                by_color_theme[color_theme] = parse_colors(data) | {
857                    "preferred_background": preferred_background
858                }
859            theme["by_color_theme"] = by_color_theme
860            themes += [theme]
861        return themes
862
863    def do_get_functions(self):
864        out, err = run_fish_cmd("functions")
865        out = out.strip()
866        return out.split("\n")
867
868    def do_get_variable_names(self, cmd):
869        "Given a command like 'set -U' return all the variable names"
870        out, err = run_fish_cmd(cmd)
871        return out.split("\n")
872
873    def do_get_variables(self):
874        out, err = run_fish_cmd("set -L")
875
876        # Put all the variables into a dictionary
877        vars = {}
878        for line in out.split("\n"):
879            comps = line.split(" ", 1)
880            if len(comps) < 2:
881                continue
882            fish_var = FishVar(comps[0], comps[1])
883            vars[fish_var.name] = fish_var
884
885        # Mark universal variables. L means don't abbreviate.
886        for name in self.do_get_variable_names("set -nUL"):
887            if name in vars:
888                vars[name].universal = True
889        # Mark exported variables. L means don't abbreviate.
890        for name in self.do_get_variable_names("set -nxL"):
891            if name in vars:
892                vars[name].exported = True
893
894        # Do not return history as a variable, it may be so large the browser hangs.
895        vars.pop("history", None)
896
897        return [
898            vars[key].get_json_obj()
899            for key in sorted(vars.keys(), key=lambda x: x.lower())
900        ]
901
902    def do_get_bindings(self):
903        """Get key bindings"""
904
905        # Running __fish_config_interactive print fish greeting and
906        # loads key bindings
907        greeting, err = run_fish_cmd(" __fish_config_interactive")
908
909        # Load the key bindings and then list them with bind
910        out, err = run_fish_cmd("__fish_config_interactive; bind")
911
912        # Remove fish greeting from output
913        out = out[len(greeting) :]
914
915        # Put all the bindings into a list
916        bindings = []
917        command_to_binding = {}
918
919        for line in out.split("\n"):
920            comps = line.split(" ", 2)
921
922            # If we don't have "bind", a sequence and a mapping,
923            # it's not a valid binding.
924            if len(comps) != 3:
925                continue
926
927            # Store the "--preset" value for later
928            if comps[1] == "--preset":
929                preset = True
930                # There's possibly a way to do this faster, but it's not important.
931                comps = line.split(" ", 3)[1:]
932            elif comps[1] == "--user":
933                preset = False
934                comps = line.split(" ", 3)[1:]
935            # Check again if we removed the level.
936            if len(comps) != 3:
937                continue
938
939            key = comps[1]
940            command = comps[2]
941
942            if command in bindings_blacklist:
943                continue
944
945            if command in command_to_binding:
946                fish_binding = command_to_binding[command]
947                fish_binding.add_binding(line, key)
948            else:
949                fish_binding = FishBinding(command, line, key)
950                bindings.append(fish_binding)
951                command_to_binding[command] = fish_binding
952
953        return [binding.get_json_obj() for binding in bindings]
954
955    def do_get_history(self):
956        # Use NUL to distinguish between history items.
957        out, err = run_fish_cmd("builtin history -z")
958        result = out.split("\0")
959        if result:
960            result.pop()  # trim off the trailing element
961        return result
962
963    def do_set_color_for_variable(self, name, color):
964        "Sets a color for a fish color name, like 'fish_color_autosuggestion'"
965        if not name:
966            raise ValueError
967        if not color and not color == "":
968            color = "normal"
969        else:
970            color = unparse_color(color)
971        name = name.strip()
972        if not re.match("^[a-zA-Z0-9_]+$", name):
973            print("Refusing to use variable name: '", name, "'")
974            return
975        color = color.strip()
976        if not re.match("^[a-zA-Z0-9_= -]*$", color):
977            print("Refusing to use color value: ", color)
978            return
979        return name + " " + color
980
981    def do_get_function(self, func_name):
982        out, err = run_fish_cmd("functions " + func_name + " | fish_indent --html")
983        return out
984
985    def do_delete_history_item(self, history_item_text):
986        # It's really lame that we always return success here
987        cmd = (
988            "builtin history delete --case-sensitive --exact -- %s; builtin history save"
989            % escape_fish_cmd(history_item_text)
990        )
991        out, err = run_fish_cmd(cmd)
992        return True
993
994    def do_set_prompt_function(self, prompt_func):
995        cmd = (
996            "functions -e fish_right_prompt; "
997            + prompt_func
998            + "\n"
999            + "funcsave fish_prompt && funcsave fish_right_prompt 2>/dev/null"
1000        )
1001        out, err = run_fish_cmd(cmd)
1002        return len(err) == 0
1003
1004    def do_get_prompt(self, prompt_function_text, extras_dict):
1005        # Return the prompt output by the given command
1006        cmd = (
1007            prompt_function_text
1008            + '\n builtin cd "'
1009            + initial_wd
1010            + '" \n false \n fish_prompt\n'
1011        )
1012        prompt_demo_ansi, err = run_fish_cmd(cmd)
1013        prompt_demo_html = ansi_to_html(prompt_demo_ansi)
1014        right_demo_ansi, err = run_fish_cmd(
1015            "functions -e fish_right_prompt; "
1016            + prompt_function_text
1017            + '\n builtin cd "'
1018            + initial_wd
1019            + '" \n false \n functions -q fish_right_prompt && fish_right_prompt\n'
1020        )
1021        right_demo_html = ansi_to_html(right_demo_ansi)
1022        prompt_demo_font_size = self.font_size_for_ansi_prompt(
1023            prompt_demo_ansi + right_demo_ansi
1024        )
1025        result = {
1026            "function": prompt_function_text,
1027            "demo": prompt_demo_html,
1028            "font_size": prompt_demo_font_size,
1029            "right": right_demo_html,
1030        }
1031        if extras_dict:
1032            result.update(extras_dict)
1033        return result
1034
1035    def do_get_current_prompt(self):
1036        # Return the current prompt. We run 'false' to demonstrate how the
1037        # prompt shows the command status (#1624).
1038        prompt_func, err = run_fish_cmd(
1039            "functions fish_prompt; functions fish_right_prompt"
1040        )
1041        result = self.do_get_prompt(
1042            prompt_func.strip(),
1043            {"name": "Current"},
1044        )
1045        return result
1046
1047    def do_get_sample_prompt(self, text, extras_dict):
1048        # Return the prompt you get from the given text. Extras_dict is a
1049        # dictionary whose values get merged in. We run 'false' to demonstrate
1050        # how the prompt shows the command status (#1624)
1051        return self.do_get_prompt(text.strip(), extras_dict)
1052
1053    def parse_one_sample_prompt_hash(self, line, result_dict):
1054        # Allow us to skip whitespace, etc.
1055        if not line:
1056            return True
1057        if line.isspace():
1058            return True
1059
1060        # Parse a comment hash like '# name: Classic'
1061        match = re.match(r"#\s*(\w+?): (.+)", line, re.IGNORECASE)
1062        if match:
1063            key = match.group(1).strip()
1064            value = match.group(2).strip()
1065            result_dict[key] = value
1066            return True
1067        # Skip other hash comments
1068        return line.startswith("#")
1069
1070    def read_one_sample_prompt(self, path):
1071        try:
1072            extras_dict = {}
1073            # Read one sample prompt from out
1074            function_lines = []
1075            parsing_hashes = True
1076            for line in get_embedded_file(path).splitlines(keepends=True):
1077                # Parse hashes until parse_one_sample_prompt_hash return
1078                # False.
1079                if parsing_hashes:
1080                    parsing_hashes = self.parse_one_sample_prompt_hash(
1081                        line, extras_dict
1082                    )
1083                # Maybe not we're not parsing hashes, or maybe we already
1084                # were not.
1085                if not parsing_hashes:
1086                    function_lines.append(line)
1087            func = "".join(function_lines).strip()
1088            result = self.do_get_sample_prompt(func, extras_dict)
1089            return result
1090        except IOError:
1091            # Ignore unreadable files, etc.
1092            return None
1093
1094    def do_get_sample_prompts_list(self):
1095        paths = list_embedded_files("prompts", ".fish")
1096        result = []
1097        try:
1098            pool = multiprocessing.pool.ThreadPool(processes=8)
1099
1100            # Kick off the "Current" meta-sample
1101            current_metasample_async = pool.apply_async(self.do_get_current_prompt)
1102
1103            # Read all of the prompts in sample_prompts
1104            sample_results = pool.map(self.read_one_sample_prompt, paths, 1)
1105            result.append(current_metasample_async.get())
1106            result.extend([r for r in sample_results if r])
1107        except ImportError:
1108            # If the platform doesn't support multiprocessing, we just do it one at a time.
1109            # This happens e.g. on Termux.
1110            print(
1111                "Platform doesn't support multiprocessing, running one at a time. This may take a while."
1112            )
1113            result.append(self.do_get_current_prompt())
1114            result.extend([self.read_one_sample_prompt(path) for path in paths])
1115        return result
1116
1117    def secure_startswith(self, haystack, needle):
1118        if len(haystack) < len(needle):
1119            return False
1120        bits = 0
1121        for x, y in zip(haystack, needle):
1122            bits |= ord(x) ^ ord(y)
1123        return bits == 0
1124
1125    def font_size_for_ansi_prompt(self, prompt_demo_ansi):
1126        width = ansi_prompt_line_width(prompt_demo_ansi)
1127        # Pick a font size
1128        if width >= 70:
1129            font_size = "8pt"
1130        if width >= 60:
1131            font_size = "10pt"
1132        elif width >= 50:
1133            font_size = "11pt"
1134        elif width >= 40:
1135            font_size = "13pt"
1136        elif width >= 30:
1137            font_size = "15pt"
1138        elif width >= 25:
1139            font_size = "16pt"
1140        elif width >= 20:
1141            font_size = "17pt"
1142        else:
1143            font_size = "18pt"
1144        return font_size
1145
1146    def do_GET(self):
1147        p = self.path
1148
1149        authpath = "/" + authkey
1150        if self.secure_startswith(p, authpath):
1151            p = p[len(authpath) :]
1152        else:
1153            return self.send_error(403)
1154        self.path = p
1155
1156        if p == "/colors/":
1157            output = self.do_get_colors()
1158        elif p == "/functions/":
1159            output = self.do_get_functions()
1160        elif p == "/variables/":
1161            output = self.do_get_variables()
1162        elif p == "/history/":
1163            # start = time.time()
1164            output = self.do_get_history()
1165            # end = time.time()
1166            # print "History: ", end - start
1167        elif p == "/sample_prompts/":
1168            output = self.do_get_sample_prompts_list()
1169        elif p == "/bindings/":
1170            output = self.do_get_bindings()
1171        else:
1172            return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
1173
1174        # Return valid output
1175        self.send_response(200)
1176        self.send_header("Content-type", "application/json")
1177        self.end_headers()
1178        self.write_to_wfile("\n")
1179
1180        # Output JSON
1181        self.write_to_wfile(json.dumps(output))
1182
1183    def do_POST(self):
1184        p = self.path
1185
1186        authpath = "/" + authkey
1187        if self.secure_startswith(p, authpath):
1188            p = p[len(authpath) :]
1189        else:
1190            return self.send_error(403)
1191        self.path = p
1192
1193        # This is cheesy, we want just the actual content-type.
1194        # In some cases it'll give us the encoding as well,
1195        # ("application/json;charset=utf-8")
1196        # but we don't currently care.
1197        content_type = self.headers["content-type"].split(";")[0]
1198
1199        if content_type == "application/x-www-form-urlencoded":
1200            length = int(self.headers["content-length"])
1201            url_str = self.rfile.read(length).decode("utf-8")
1202            postvars = parse_qs(url_str, keep_blank_values=1)
1203        elif content_type == "application/json":
1204            length = int(self.headers["content-length"])
1205            # This used to use the provided encoding, but we use utf-8
1206            # all around the place and nobody has ever complained.
1207            #
1208            # If any other encoding is received this will raise a UnicodeError,
1209            # which will throw us out of the function and should at most exit webconfig.
1210            # If that happens to anyone we expect bug reports.
1211            url_str = self.rfile.read(length).decode("utf-8")
1212            postvars = json.loads(url_str)
1213        elif content_type == "multipart/form-data":
1214            # This used to be a thing, as far as I could find there's
1215            # no use anymore, but let's keep an error around just in case.
1216            return self.send_error(500)
1217        else:
1218            postvars = {}
1219
1220        if p == "/set_color/":
1221            print("# Colorscheme: " + postvars.get("theme"))
1222            output = ""
1223            theme_contents = []
1224            for item in postvars.get("colors"):
1225                what = item.get("what")
1226                color = item.get("color")
1227                if what:
1228                    theme_contents += [self.do_set_color_for_variable(what, color)]
1229
1230            varname = "__fish_webconfig_theme_notification"
1231            out, err = run_fish_cmd(
1232                "__fish_theme_freeze 'fish_config' {}\n".format(
1233                    " ".join(escape_fish_cmd(x) for x in theme_contents)
1234                )
1235                + "begin\n"
1236                + "    set -l i 0\n"
1237                + "    if not set -q {}\n".format(varname)
1238                + "        or string match -qr -- '^set-theme-v1-#(?<i>\\d+)$' ${}\n".format(
1239                    varname
1240                )
1241                + "        set -U {} set-theme-v1-#(math $i + 1)\n".format(varname)
1242                + "    end\n"
1243                + "end\n"
1244            )
1245            assert out == ""
1246            assert err == ""
1247
1248        elif p == "/get_function/":
1249            what = postvars.get("what")
1250            output = [self.do_get_function(what[0])]
1251        elif p == "/delete_history_item/":
1252            what = postvars.get("what")
1253            if self.do_delete_history_item(what[0]):
1254                output = ["OK"]
1255            else:
1256                output = ["Unable to delete history item"]
1257        elif p == "/set_prompt/":
1258            what = postvars.get("fish_prompt")
1259            if self.do_set_prompt_function(what):
1260                output = ["OK"]
1261            else:
1262                output = ["Unable to set prompt"]
1263        else:
1264            return self.send_error(404)
1265
1266        # Return valid output
1267        self.send_response(200)
1268        self.send_header("Content-type", "application/json")
1269        self.end_headers()
1270        self.write_to_wfile("\n")
1271
1272        # Output JSON
1273        self.write_to_wfile(json.dumps(output))
1274
1275    def log_request(self, code="-", size="-"):
1276        """Disable request logging"""
1277        pass
1278
1279    def log_error(self, format, *args):
1280        if format == "code %d, message %s" and hasattr(self, "path"):
1281            # This appears to be a send_error() message
1282            # We want to include the path (if we have one)
1283            (code, msg) = args
1284            format = "code %d, message %s, path %s"
1285            args = (code, msg, self.path)
1286        SimpleHTTPServer.SimpleHTTPRequestHandler.log_error(self, format, *args)
1287
1288
1289redirect_template_html = """
1290<!DOCTYPE html>
1291<html>
1292 <head>
1293  <meta http-equiv="refresh" content="0;URL='%s'" />
1294 </head>
1295 <body>
1296  <p><a href="%s">Start the Fish Web config</a></p>
1297 </body>
1298</html>
1299"""
1300
1301# find fish
1302fish_bin_dir = os.environ.get("fish_bin_dir")
1303fish_bin_path = None
1304
1305# only need the '.exe' extension on Windows
1306fish_bin_name = "fish.exe" if is_windows() else "fish"
1307
1308if not fish_bin_dir:
1309    print("The $__fish_bin_dir environment variable is not set. Looking in $PATH...")
1310    fish_bin_path = find_executable(fish_bin_name)
1311    if not fish_bin_path:
1312        print("fish could not be found. Is fish installed correctly?")
1313        sys.exit(-1)
1314    else:
1315        print("fish found at '%s'" % fish_bin_path)
1316
1317else:
1318    fish_bin_path = os.path.join(fish_bin_dir, fish_bin_name)
1319
1320if not os.access(fish_bin_path, os.X_OK):
1321    print(
1322        "fish could not be executed at path '%s'. "
1323        "Is fish installed correctly?" % fish_bin_path
1324    )
1325    sys.exit(-1)
1326FISH_BIN_PATH = fish_bin_path
1327
1328# We want to show the demo prompts in the directory from which this was invoked,
1329# so get the current working directory
1330initial_wd = os.getcwd()
1331
1332# Make sure that the working directory is the one that contains the script
1333# server file, because the document root is the working directory.
1334where = os.path.dirname(sys.argv[0])
1335os.chdir(where)
1336
1337# Generate a 16-byte random key as a hexadecimal string
1338authkey = binascii.b2a_hex(os.urandom(16)).decode("ascii")
1339
1340# Try to find a suitable port
1341PORT = 8000
1342HOST = "::" if socket.has_ipv6 else "localhost"
1343while PORT <= 9000:
1344    try:
1345        Handler = FishConfigHTTPRequestHandler
1346        httpd = FishConfigTCPServer((HOST, PORT), Handler)
1347        # Success
1348        break
1349    except (OSError, socket.error) as e:
1350        if e.errno == errno.EAFNOSUPPORT and HOST == "::":
1351            print("Ipv6 seems to be unavailable, trying ipv4")
1352            # We can't do ipv6, let's try v4.
1353            HOST = "127.0.0.1"
1354            PORT = 8000
1355            # Override the class address_family variable (yes, this is a hack)
1356            FishConfigTCPServer.address_family = socket.AF_INET
1357            continue
1358        if e.errno != errno.EADDRINUSE:
1359            print(str(e))
1360            sys.exit(-1)
1361    PORT += 1
1362
1363if PORT > 9000:
1364    # Nobody say it
1365    print("Unable to find an open port between 8000 and 9000")
1366    sys.exit(-1)
1367
1368# Get any initial tab (functions, colors, etc)
1369# Just look at the first letter
1370initial_tab = ""
1371if len(sys.argv) > 1:
1372    for tab in [
1373        "functions",
1374        "prompt",
1375        "colors",
1376        "variables",
1377        "history",
1378        "bindings",
1379    ]:
1380        if tab.startswith(sys.argv[1]):
1381            initial_tab = "#!/" + tab
1382            break
1383
1384url = "http://localhost:%d/%s/%s" % (PORT, authkey, initial_tab)
1385
1386# Create temporary file to hold redirect to real server. This prevents exposing
1387# the URL containing the authentication key on the command line (see
1388# CVE-2014-2914 or https://github.com/fish-shell/fish-shell/issues/1438).
1389#
1390
1391# If on Windows, the file needs to be closed after writing, otherwise, the browser won't be able to open it."
1392# unfortunately this was added in python 3.12, so we don't add it on other platforms
1393# to support older python versions there.
1394kwargs = {}
1395if is_windows():
1396    kwargs["delete_on_close"] = False
1397f = tempfile.NamedTemporaryFile(
1398    prefix="web_config",
1399    suffix=".html",
1400    mode="w",
1401    delete=True,
1402    **kwargs,
1403)
1404
1405f.write(redirect_template_html % (url, url))
1406f.flush()
1407
1408if is_windows():
1409    f.close()
1410
1411# Open temporary file as URL
1412# Use open on macOS >= 10.12.5 to work around #4035.
1413fileurl = "file://" + f.name
1414
1415if is_windows():
1416    fileurl = fileurl.replace("\\", "/")
1417
1418print(
1419    "Web config started at %s%s%s"
1420    % (ENTER_UNDERLINE_MODE, fileurl, EXIT_ATTRIBUTE_MODE)
1421)
1422print(
1423    "If that doesn't work, try opening %s%s%s"
1424    % (ENTER_UNDERLINE_MODE, url, EXIT_ATTRIBUTE_MODE)
1425)
1426print("%sHit ENTER to stop.%s" % (ENTER_BOLD_MODE, EXIT_ATTRIBUTE_MODE))
1427
1428
1429def runThing():
1430    if os.environ.get("BROWSER") == "true":
1431        # Don't start a browser in this case (see issue #11926)
1432        pass
1433    elif is_wsl():
1434        cmd_path = find_executable("cmd.exe", COMMON_WSL_CMD_PATHS)
1435        if cmd_path:
1436            subprocess.call([cmd_path, "/c", "start %s" % url])
1437        else:
1438            print("Please add the directory containing cmd.exe to your $PATH")
1439            sys.exit(-1)
1440    elif is_termux():
1441        subprocess.call(["termux-open-url", url])
1442    elif is_chromeos_garcon():
1443        webbrowser.open(url)
1444    elif is_sailfish_os():
1445        subprocess.call(["xdg-open", url])
1446    else:
1447        webbrowser.open(fileurl)
1448
1449
1450# Some browsers still block webbrowser.open if they haven't been opened before,
1451# so we just spawn it in a thread.
1452thread = threading.Thread(target=runThing)
1453thread.start()
1454
1455# Safari will open sockets and not write to them, causing potential hangs
1456# on shutdown.
1457httpd.block_on_close = False
1458httpd.daemon_threads = True
1459
1460# Select on stdin and httpd
1461stdin_no = sys.stdin.fileno()
1462
1463
1464def create_socket(start_port, end_port):
1465    """Attempt to create a socket from a range of ports."""
1466    for port in range(start_port, end_port + 1):
1467        try:
1468            sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1469            sck.bind(("localhost", port))
1470            sck.listen()
1471            return (sck, port)
1472        except socket.error:
1473            pass
1474    print("Unable to find an open port between {} and {}.".format(start_port, end_port))
1475    sys.exit(-1)
1476
1477
1478def capture_enter(port):
1479    """Read keyboard events and establish a socket connection when pressing the Enter."""
1480    import msvcrt
1481
1482    while True:
1483        if msvcrt.kbhit():
1484            key = msvcrt.getch().decode()
1485            if key == "\r" or key == "\n":
1486                break
1487    sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1488    sck.connect(("localhost", port))
1489
1490
1491def get_windows_signal():
1492    """Using socket as a replacement for stdin on Windows."""
1493    # The intent is to get a free port between 8000 and 9000, like for the HTTP
1494    # server. But we already know that port 8000..PORT are not available. So
1495    # starting from `PORT+1` is more efficient.
1496    # More importantly though, Windows allows multiple sockets to bind to the
1497    # same port in some circumstances (see SO_EXCLUSIVEADDRUSE documentation),
1498    # and thus allows the signal socket to bind to the same port as HTTP.
1499    # A browser may then end up reaching the wrong socket and causing
1500    # fish_config to shutdown prematurely.
1501    (sig, sig_port) = create_socket(PORT + 1, 9000)
1502    threading.Thread(target=capture_enter, args=(sig_port,)).start()
1503    return sig
1504
1505
1506try:
1507    httpd_fileno = httpd.fileno()
1508    sig = get_windows_signal() if is_windows() else sys.stdin
1509    sig_fileno = sig.fileno()
1510    while True:
1511        ready_read = select.select([sig_fileno, httpd_fileno], [], [])
1512        if ready_read[0][0] != httpd_fileno:
1513            print("Shutting down.")
1514
1515            # On windows the newline has already been consumed by the capture_enter function.
1516            if not is_windows():
1517                # Consume the newline so it doesn't get printed by the caller
1518                sys.stdin.readline()
1519            break
1520        else:
1521            httpd.handle_request()
1522except KeyboardInterrupt:
1523    print("\nShutting down.")
1524
1525# Clean up temporary file
1526# If on Windows, the file already closed
1527if not is_windows():
1528    f.close()
1529
1530thread.join()