fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
6d0cd3d9b
master
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 : 43os .environ ["TERM" ]= term 44 45TERMINAL_COLOR_THEME = os .environ ["__fish_terminal_color_theme" ] 46 47 48def find_executable (exe ,paths = ()): 49final_path = os .environ ["PATH" ].split (os .pathsep ) 50if paths : 51final_path .extend (paths ) 52for p in final_path : 53proposed_path = os .path .join (p ,exe ) 54if os .access (proposed_path ,os .X_OK ): 55return proposed_path 56 57 58def is_wsl (): 59"""Return whether we are running under the Windows Subsystem for Linux""" 60if "linux" in platform .system ().lower ()and os .access ("/proc/version" ,os .R_OK ): 61with open ("/proc/version" ,"r" )as f : 62# Find 'Microsoft' for wsl1 and 'microsoft' for wsl2 63if "microsoft" in f .read ().lower (): 64return True 65return False 66 67 68def is_windows (): 69"""Return whether we are running under the Windows""" 70return sys .platform .startswith ("win" ) 71 72 73def is_sailfish_os (): 74"""Return whether we are running on Sailfish OS""" 75if "linux" in platform .system ().lower ()and os .access ( 76"/etc/sailfish-release" ,os .R_OK 77 ): 78with open ("/etc/sailfish-release" ,"r" )as f : 79# Find 'ID=sailfishos' 80if "sailfishos" in f .read (): 81return True 82return False 83 84 85def is_termux (): 86"""Return whether we are running under the Termux application for Android""" 87return "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 97try : 98return "garcon-url-handler" in webbrowser .get ().name 99except : 100return False 101 102 103def run_fish_cmd (text ,strict = False ,env = None ): 104"$ " + text ) 105p = subprocess .Popen ( 106 [FISH_BIN_PATH ], 107stdin = subprocess .PIPE , 108stdout = subprocess .PIPE , 109stderr = subprocess .PIPE , 110env = env , 111 ) 112out ,err = p .communicate (text .encode ("utf-8" )) 113out = out .decode ("utf-8" ,"replace" ) 114err = err .decode ("utf-8" ,"replace" ) 115if strict : 116assert err == "" 117# TODO use check_output() 118return out 119return out ,err 120 121 122def list_embedded_files (path ,suffix ): 123return [ 124path 125for path in run_fish_cmd ( 126"status list-files " + escape_fish_cmd (path ),strict = True 127 ).splitlines () 128if path .endswith (suffix ) 129 ] 130 131 132def get_embedded_file (path ): 133return 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 138escaped = text .replace ("\\" ,"\\\\" ).replace ("'" ,"\\'" ) 139return "'" + escaped + "'" 140 141 142def strip_one_layer (text ,char ): 143# Strip the text from one layer of a given character 144if text [- 1 ]== char : 145text = text [:- 1 ] 146if text [0 ]== char : 147text = text [1 :] 148return 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'""" 181if comp in named_colors : 182# Named color 183return named_colors [comp ] 184elif ( 185re .match (r"[0-9a-fA-F]{3}" ,comp )is not None 186or re .match (r"[0-9a-fA-F]{6}" ,comp )is not None 187 ): 188# Hex color 189return comp 190else : 191# Unknown 192return "" 193 194 195def better_color (c1 ,c2 ): 196"""Indicate which color is "better", i.e. prefer term256 colors""" 197if not c2 : 198return c1 199if not c1 : 200return c2 201if c1 == "normal" : 202return c2 203if c2 == "normal" : 204return c1 205if c2 in named_colors : 206return c1 207if c1 in named_colors : 208return c2 209return c1 210 211 212def parse_colors (colors_str ): 213for words in map (shlex .split ,colors_str .splitlines ()): 214assert re .match ( 215r"^((?:fish_color_|fish_pager_color_)\w+)$" , 216words [0 ], 217 ) 218return { 219"colors" :sorted ( 220 [ 221 {"name" :words [0 ]}| parse_color (words [1 :]) 222for words in map (shlex .split ,colors_str .splitlines ()) 223 ], 224key = operator .itemgetter ("name" ), 225 ), 226 } 227 228 229def parse_color (comps ): 230"""A basic function to parse a color string, for example, ['red', '--bold'].""" 231color = "" 232background_color = "" 233underline_color = "" 234bold = False 235underline = None 236italics = False 237strikethrough = False 238dim = False 239reverse = False 240i = 0 241while i < len (comps ): 242comp = comps [i ] 243if comp == "--bold" or comp == "-o" : 244bold = True 245elif comp == "--underline" or comp == "-u" : 246underline = "single" 247elif comp .startswith ("--underline=" ): 248# TODO(python>3.8): use removeprefix 249underline = comp [len ("--underline=" ) :] 250elif comp .startswith ( 251"-u" 252 ):# Multiple short options like "-rucurly" are not yet supported. 253# TODO(python>3.8): use removeprefix 254underline = comp [len ("-u" ) :] 255elif comp == "--italics" or comp == "-i" : 256italics = True 257elif comp == "--dim" or comp == "-d" : 258dim = True 259elif comp == "--reverse" or comp == "-r" : 260reverse = True 261elif comp == "--strikethrough" or comp == "-s" : 262strikethrough = True 263elif comp .startswith ("--theme=" ): 264pass # Not yet supported here. 265else : 266 267def parse_opt ( 268current_best :str ,i :int ,long_opt :str ,short_opt :Optional [str ] 269 )-> str : 270if comp .startswith (long_opt ): 271c = comp [len (long_opt ) :] 272if c [0 ]== "=" : 273# There was a = between the long option and the value. 274# i.e. support also --background=red, not just --background red 275c = c [1 :] 276parsed_c = parse_one_color (c ) 277# We prefer the unparsed version - if it says "brgreen", we use brgreen, 278# instead of 00ff00 279if better_color (current_best ,parsed_c )== parsed_c : 280return True ,c ,i 281elif short_opt is not None and comp .startswith (short_opt ): 282if comp == short_opt : 283if i + 1 == len (comps ): 284c = "" 285else : 286c = comps [i + 1 ] 287i += 1 288else : 289c = comp [len (short_opt ) :] 290parsed_c = parse_one_color (c ) 291if better_color (current_best ,parsed_c )== parsed_c : 292return True ,c ,i 293return False ,current_best ,i 294 295is_bg ,background_color ,i = parse_opt ( 296background_color ,i ,"--background" ,"-b" 297 ) 298is_ul ,underline_color ,i = parse_opt ( 299underline_color ,i ,"--underline-color" ,None 300 ) 301if not (is_bg or is_ul ): 302# Regular color 303parsed_c = parse_one_color (comp ) 304if better_color (color ,parsed_c )== parsed_c : 305color = comp 306i += 1 307 308return { 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""" 323if isinstance (col ,str ): 324return col 325ret = "" 326if col ["color" ]: 327ret += col ["color" ] 328if col ["bold" ]: 329ret += " --bold" 330if col ["underline" ]is not None : 331ret += " --underline=" + str (col ["underline" ]) 332if col ["italics" ]: 333ret += " --italics" 334if col ["dim" ]: 335ret += " --dim" 336if col ["reverse" ]: 337ret += " --reverse" 338if col ["strikethrough" ]: 339ret += " --strikethrough" 340if col ["background" ]: 341ret += " --background=" + col ["background" ] 342if col ["underline-color" ]: 343ret += " --underline-color=" + col ["underline-color" ] 344return ret 345 346 347def parse_bool (val ): 348val = val .lower () 349if val .startswith ("f" )or val .startswith ("0" ): 350return False 351if val .startswith ("t" )or val .startswith ("1" ): 352return True 353return bool (val ) 354 355 356def html_color_for_ansi_color_index (val ): 357arr = [ 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 ] 615if val < 0 or val >= len (arr ): 616return "" 617else : 618return 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 627val = full_val [2 :- 1 ] 628 629# Helper function to close a span if it's open 630def close_span (): 631if span_open : 632result .append ("</span>" ) 633 634# term24bit foreground color 635match = re .match (r"38;2;(\d+);(\d+);(\d+)" ,val ) 636if match is not None : 637close_span () 638# Just use the rgb values directly 639html_color = "#%02x%02x%02x" % ( 640int (match .group (1 )), 641int (match .group (2 )), 642int (match .group (3 )), 643 ) 644result .append ('<span style="color: ' + html_color + '">' ) 645return True # span now open 646 647# term256 foreground color 648match = re .match (r"38;5;(\d+)" ,val ) 649if match is not None : 650close_span () 651html_color = html_color_for_ansi_color_index (int (match .group (1 ))) 652result .append ('<span style="color: ' + html_color + '">' ) 653return True # span now open 654 655# term16 foreground color 656if val in (str (x )for x in chain (range (90 ,97 ),range (30 ,38 ))): 657close_span () 658html_color = html_color_for_ansi_color_index ( 659int (val )- (30 if int (val )< 90 else 82 ) 660 ) 661result .append ('<span style="color: ' + html_color + '">' ) 662return True # span now open 663 664# Try special escapes 665if full_val == EXIT_ATTRIBUTE_MODE : 666close_span () 667return False 668 669# TODO We don't handle bold, underline, italics, dim, strikethrough, or reverse yet 670 671# Do nothing on failure 672return 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 679return 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. 685stripped_val = strip_ansi (val ) 686 687# Now count the longest line 688return 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. 696reg = re .compile ( 697""" 698( # Capture 699\x1b # Escape 700[^m]* # Zero or more non-'m's 701m # Literal m terminates the sequence 702\x0f? # HACK: A ctrl-o - this is how tmux' sgr0 ends 703) # End capture 704""" , 705re .VERBOSE , 706 ) 707separated = 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 711result = [] 712 713span_open = False 714 715# Text is at even indexes, escape sequences at odd indexes 716for i in range (len (separated )): 717component = separated [i ] 718if i % 2 == 0 : 719# It's text, possibly empty 720# Clean up other ANSI junk 721result .append (escape_html (strip_ansi (component ))) 722else : 723# It's an escape sequence. Close the previous escape. 724span_open = append_html_for_ansi_escape (component ,result ,span_open ) 725 726# Close final escape 727if span_open : 728result .append ("</span>" ) 729 730# Remove empty elements 731result = [x for x in result if x ] 732 733# Clean up empty spans, the nasty way 734idx = len (result )- 1 735while idx >= 1 : 736if result [idx ]== "</span>" and result [idx - 1 ].startswith ("<span" ): 737# Empty span, delete these two 738result [idx - 1 :idx + 1 ]= [] 739idx = idx - 1 740idx = idx - 1 741 742return "" .join (result ) 743 744 745class FishVar : 746"""A class that represents a variable""" 747 748def __init__ (self ,name ,value ): 749self .name = name 750self .value = value 751self .universal = False 752self .exported = False 753 754def get_json_obj (self ): 755# Return an array(3): name, value, flags 756flags = [] 757if self .universal : 758flags .append ("universal" ) 759if self .exported : 760flags .append ("exported" ) 761return {"name" :self .name ,"value" :self .value ,"Flags" :", " .join (flags )} 762 763 764class FishBinding : 765"""A class that represents keyboard binding""" 766 767def __init__ (self ,command ,raw_binding ,readable_binding ): 768self .command = command 769self .bindings = [] 770self .add_binding (raw_binding ,readable_binding ) 771 772def add_binding (self ,raw_binding ,readable_binding ): 773for i in self .bindings : 774if i ["readable_binding" ]== readable_binding : 775i ["raw_bindings" ].append (raw_binding ) 776break 777else : 778self .bindings .append ( 779 {"readable_binding" :readable_binding ,"raw_bindings" : [raw_binding ]} 780 ) 781 782def get_json_obj (self ): 783return { 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 792WHITELIST = set (["::1" ,"::ffff:127.0.0.1" ,"127.0.0.1" ]) 793 794address_family = socket .AF_INET6 if socket .has_ipv6 else socket .AF_INET 795 796def verify_request (self ,request ,client_address ): 797return client_address [0 ]in FishConfigTCPServer .WHITELIST 798 799 800class FishConfigHTTPRequestHandler (SimpleHTTPServer .SimpleHTTPRequestHandler ): 801def write_to_wfile (self ,txt ): 802self .wfile .write (txt .encode ("utf-8" )) 803 804def do_get_colors (self ): 805out ,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 ( 812TERMINAL_COLOR_THEME 813 ) 814+ "or __fish_color_theme=unknown __fish_apply_theme\n" 815+ "__fish_theme_export_for_webconfig" , 816env = os .environ | {"__fish_force_load_default_theme" :"1" }, 817 ) 818assert err == "" 819 820words = iter (shlex .split (out )) 821current = next (words ) 822themes = [ 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" 830if TERMINAL_COLOR_THEME == "light" 831else "black" , 832 } 833 }, 834"terminal_color_theme" :TERMINAL_COLOR_THEME , 835 } 836 ] 837while True : 838try : 839theme_name = next (words ) 840except StopIteration : 841break 842theme = { 843"theme" :theme_name , 844 } 845pretty_name = next (words ) 846if pretty_name : 847theme ["name" ]= pretty_name 848url = next (words ) 849if url : 850theme ["url" ]= url 851by_color_theme = {} 852for _i in range (int (next (words ))): 853color_theme = next (words ) 854preferred_background = next (words ) 855data = next (words ) 856by_color_theme [color_theme ]= parse_colors (data )| { 857"preferred_background" :preferred_background 858 } 859theme ["by_color_theme" ]= by_color_theme 860themes += [theme ] 861return themes 862 863def do_get_functions (self ): 864out ,err = run_fish_cmd ("functions" ) 865out = out .strip () 866return out .split ("\n" ) 867 868def do_get_variable_names (self ,cmd ): 869"Given a command like 'set -U' return all the variable names" 870out ,err = run_fish_cmd (cmd ) 871return out .split ("\n" ) 872 873def do_get_variables (self ): 874out ,err = run_fish_cmd ("set -L" ) 875 876# Put all the variables into a dictionary 877vars = {} 878for line in out .split ("\n" ): 879comps = line .split (" " ,1 ) 880if len (comps )< 2 : 881continue 882fish_var = FishVar (comps [0 ],comps [1 ]) 883vars [fish_var .name ]= fish_var 884 885# Mark universal variables. L means don't abbreviate. 886for name in self .do_get_variable_names ("set -nUL" ): 887if name in vars : 888vars [name ].universal = True 889# Mark exported variables. L means don't abbreviate. 890for name in self .do_get_variable_names ("set -nxL" ): 891if name in vars : 892vars [name ].exported = True 893 894# Do not return history as a variable, it may be so large the browser hangs. 895vars .pop ("history" ,None ) 896 897return [ 898vars [key ].get_json_obj () 899for key in sorted (vars .keys (),key = lambda x :x .lower ()) 900 ] 901 902def do_get_bindings (self ): 903"""Get key bindings""" 904 905# Running __fish_config_interactive print fish greeting and 906# loads key bindings 907greeting ,err = run_fish_cmd (" __fish_config_interactive" ) 908 909# Load the key bindings and then list them with bind 910out ,err = run_fish_cmd ("__fish_config_interactive; bind" ) 911 912# Remove fish greeting from output 913out = out [len (greeting ) :] 914 915# Put all the bindings into a list 916bindings = [] 917command_to_binding = {} 918 919for line in out .split ("\n" ): 920comps = line .split (" " ,2 ) 921 922# If we don't have "bind", a sequence and a mapping, 923# it's not a valid binding. 924if len (comps )!= 3 : 925continue 926 927# Store the "--preset" value for later 928if comps [1 ]== "--preset" : 929preset = True 930# There's possibly a way to do this faster, but it's not important. 931comps = line .split (" " ,3 )[1 :] 932elif comps [1 ]== "--user" : 933preset = False 934comps = line .split (" " ,3 )[1 :] 935# Check again if we removed the level. 936if len (comps )!= 3 : 937continue 938 939key = comps [1 ] 940command = comps [2 ] 941 942if command in bindings_blacklist : 943continue 944 945if command in command_to_binding : 946fish_binding = command_to_binding [command ] 947fish_binding .add_binding (line ,key ) 948else : 949fish_binding = FishBinding (command ,line ,key ) 950bindings .append (fish_binding ) 951command_to_binding [command ]= fish_binding 952 953return [binding .get_json_obj ()for binding in bindings ] 954 955def do_get_history (self ): 956# Use NUL to distinguish between history items. 957out ,err = run_fish_cmd ("builtin history -z" ) 958result = out .split ("\0" ) 959if result : 960result .pop ()# trim off the trailing element 961return result 962 963def do_set_color_for_variable (self ,name ,color ): 964"Sets a color for a fish color name, like 'fish_color_autosuggestion'" 965if not name : 966raise ValueError 967if not color and not color == "" : 968color = "normal" 969else : 970color = unparse_color (color ) 971name = name .strip () 972if not re .match ("^[a-zA-Z0-9_]+$" ,name ): 973"Refusing to use variable name: '" ,name ,"'" ) 974return 975color = color .strip () 976if not re .match ("^[a-zA-Z0-9_= -]*$" ,color ): 977"Refusing to use color value: " ,color ) 978return 979return name + " " + color 980 981def do_get_function (self ,func_name ): 982out ,err = run_fish_cmd ("functions " + func_name + " | fish_indent --html" ) 983return out 984 985def do_delete_history_item (self ,history_item_text ): 986# It's really lame that we always return success here 987cmd = ( 988"builtin history delete --case-sensitive --exact -- %s; builtin history save" 989% escape_fish_cmd (history_item_text ) 990 ) 991out ,err = run_fish_cmd (cmd ) 992return True 993 994def do_set_prompt_function (self ,prompt_func ): 995cmd = ( 996"functions -e fish_right_prompt; " 997+ prompt_func 998+ "\n" 999+ "funcsave fish_prompt && funcsave fish_right_prompt 2>/dev/null" 1000 ) 1001out ,err = run_fish_cmd (cmd ) 1002return len (err )== 0 1003 1004def do_get_prompt (self ,prompt_function_text ,extras_dict ): 1005# Return the prompt output by the given command 1006cmd = ( 1007prompt_function_text 1008+ '\n builtin cd "' 1009+ initial_wd 1010+ '" \n false \n fish_prompt\n' 1011 ) 1012prompt_demo_ansi ,err = run_fish_cmd (cmd ) 1013prompt_demo_html = ansi_to_html (prompt_demo_ansi ) 1014right_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 ) 1021right_demo_html = ansi_to_html (right_demo_ansi ) 1022prompt_demo_font_size = self .font_size_for_ansi_prompt ( 1023prompt_demo_ansi + right_demo_ansi 1024 ) 1025result = { 1026"function" :prompt_function_text , 1027"demo" :prompt_demo_html , 1028"font_size" :prompt_demo_font_size , 1029"right" :right_demo_html , 1030 } 1031if extras_dict : 1032result .update (extras_dict ) 1033return result 1034 1035def do_get_current_prompt (self ): 1036# Return the current prompt. We run 'false' to demonstrate how the 1037# prompt shows the command status (#1624). 1038prompt_func ,err = run_fish_cmd ( 1039"functions fish_prompt; functions fish_right_prompt" 1040 ) 1041result = self .do_get_prompt ( 1042prompt_func .strip (), 1043 {"name" :"Current" }, 1044 ) 1045return result 1046 1047def 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) 1051return self .do_get_prompt (text .strip (),extras_dict ) 1052 1053def parse_one_sample_prompt_hash (self ,line ,result_dict ): 1054# Allow us to skip whitespace, etc. 1055if not line : 1056return True 1057if line .isspace (): 1058return True 1059 1060# Parse a comment hash like '# name: Classic' 1061match = re .match (r"#\s*(\w+?): (.+)" ,line ,re .IGNORECASE ) 1062if match : 1063key = match .group (1 ).strip () 1064value = match .group (2 ).strip () 1065result_dict [key ]= value 1066return True 1067# Skip other hash comments 1068return line .startswith ("#" ) 1069 1070def read_one_sample_prompt (self ,path ): 1071try : 1072extras_dict = {} 1073# Read one sample prompt from out 1074function_lines = [] 1075parsing_hashes = True 1076for line in get_embedded_file (path ).splitlines (keepends = True ): 1077# Parse hashes until parse_one_sample_prompt_hash return 1078# False. 1079if parsing_hashes : 1080parsing_hashes = self .parse_one_sample_prompt_hash ( 1081line ,extras_dict 1082 ) 1083# Maybe not we're not parsing hashes, or maybe we already 1084# were not. 1085if not parsing_hashes : 1086function_lines .append (line ) 1087func = "" .join (function_lines ).strip () 1088result = self .do_get_sample_prompt (func ,extras_dict ) 1089return result 1090except IOError : 1091# Ignore unreadable files, etc. 1092return None 1093 1094def do_get_sample_prompts_list (self ): 1095paths = list_embedded_files ("prompts" ,".fish" ) 1096result = [] 1097try : 1098pool = multiprocessing .pool .ThreadPool (processes = 8 ) 1099 1100# Kick off the "Current" meta-sample 1101current_metasample_async = pool .apply_async (self .do_get_current_prompt ) 1102 1103# Read all of the prompts in sample_prompts 1104sample_results = pool .map (self .read_one_sample_prompt ,paths ,1 ) 1105result .append (current_metasample_async .get ()) 1106result .extend ([r for r in sample_results if r ]) 1107except 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"Platform doesn't support multiprocessing, running one at a time. This may take a while." 1112 ) 1113result .append (self .do_get_current_prompt ()) 1114result .extend ([self .read_one_sample_prompt (path )for path in paths ]) 1115return result 1116 1117def secure_startswith (self ,haystack ,needle ): 1118if len (haystack )< len (needle ): 1119return False 1120bits = 0 1121for x ,y in zip (haystack ,needle ): 1122bits |= ord (x )^ ord (y ) 1123return bits == 0 1124 1125def font_size_for_ansi_prompt (self ,prompt_demo_ansi ): 1126width = ansi_prompt_line_width (prompt_demo_ansi ) 1127# Pick a font size 1128if width >= 70 : 1129font_size = "8pt" 1130if width >= 60 : 1131font_size = "10pt" 1132elif width >= 50 : 1133font_size = "11pt" 1134elif width >= 40 : 1135font_size = "13pt" 1136elif width >= 30 : 1137font_size = "15pt" 1138elif width >= 25 : 1139font_size = "16pt" 1140elif width >= 20 : 1141font_size = "17pt" 1142else : 1143font_size = "18pt" 1144return font_size 1145 1146def do_GET (self ): 1147p = self .path 1148 1149authpath = "/" + authkey 1150if self .secure_startswith (p ,authpath ): 1151p = p [len (authpath ) :] 1152else : 1153return self .send_error (403 ) 1154self .path = p 1155 1156if p == "/colors/" : 1157output = self .do_get_colors () 1158elif p == "/functions/" : 1159output = self .do_get_functions () 1160elif p == "/variables/" : 1161output = self .do_get_variables () 1162elif p == "/history/" : 1163# start = time.time() 1164output = self .do_get_history () 1165# end = time.time() 1166# print "History: ", end - start 1167elif p == "/sample_prompts/" : 1168output = self .do_get_sample_prompts_list () 1169elif p == "/bindings/" : 1170output = self .do_get_bindings () 1171else : 1172return SimpleHTTPServer .SimpleHTTPRequestHandler .do_GET (self ) 1173 1174# Return valid output 1175self .send_response (200 ) 1176self .send_header ("Content-type" ,"application/json" ) 1177self .end_headers () 1178self .write_to_wfile ("\n" ) 1179 1180# Output JSON 1181self .write_to_wfile (json .dumps (output )) 1182 1183def do_POST (self ): 1184p = self .path 1185 1186authpath = "/" + authkey 1187if self .secure_startswith (p ,authpath ): 1188p = p [len (authpath ) :] 1189else : 1190return self .send_error (403 ) 1191self .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. 1197content_type = self .headers ["content-type" ].split (";" )[0 ] 1198 1199if content_type == "application/x-www-form-urlencoded" : 1200length = int (self .headers ["content-length" ]) 1201url_str = self .rfile .read (length ).decode ("utf-8" ) 1202postvars = parse_qs (url_str ,keep_blank_values = 1 ) 1203elif content_type == "application/json" : 1204length = 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. 1211url_str = self .rfile .read (length ).decode ("utf-8" ) 1212postvars = json .loads (url_str ) 1213elif 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. 1216return self .send_error (500 ) 1217else : 1218postvars = {} 1219 1220if p == "/set_color/" : 1221"# Colorscheme: " + postvars .get ("theme" )) 1222output = "" 1223theme_contents = [] 1224for item in postvars .get ("colors" ): 1225what = item .get ("what" ) 1226color = item .get ("color" ) 1227if what : 1228theme_contents += [self .do_set_color_for_variable (what ,color )] 1229 1230varname = "__fish_webconfig_theme_notification" 1231out ,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 ( 1239varname 1240 ) 1241+ " set -U {} set-theme-v1-#(math $i + 1)\n" .format (varname ) 1242+ " end\n" 1243+ "end\n" 1244 ) 1245assert out == "" 1246assert err == "" 1247 1248elif p == "/get_function/" : 1249what = postvars .get ("what" ) 1250output = [self .do_get_function (what [0 ])] 1251elif p == "/delete_history_item/" : 1252what = postvars .get ("what" ) 1253if self .do_delete_history_item (what [0 ]): 1254output = ["OK" ] 1255else : 1256output = ["Unable to delete history item" ] 1257elif p == "/set_prompt/" : 1258what = postvars .get ("fish_prompt" ) 1259if self .do_set_prompt_function (what ): 1260output = ["OK" ] 1261else : 1262output = ["Unable to set prompt" ] 1263else : 1264return self .send_error (404 ) 1265 1266# Return valid output 1267self .send_response (200 ) 1268self .send_header ("Content-type" ,"application/json" ) 1269self .end_headers () 1270self .write_to_wfile ("\n" ) 1271 1272# Output JSON 1273self .write_to_wfile (json .dumps (output )) 1274 1275def log_request (self ,code = "-" ,size = "-" ): 1276"""Disable request logging""" 1277pass 1278 1279def log_error (self ,format ,* args ): 1280if 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 1284format = "code %d, message %s, path %s" 1285args = (code ,msg ,self .path ) 1286SimpleHTTPServer .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"The $__fish_bin_dir environment variable is not set. Looking in $PATH..." ) 1310fish_bin_path = find_executable (fish_bin_name ) 1311if not fish_bin_path : 1312"fish could not be found. Is fish installed correctly?" ) 1313sys .exit (- 1 ) 1314else : 1315"fish found at '%s'" % fish_bin_path ) 1316 1317else : 1318fish_bin_path = os .path .join (fish_bin_dir ,fish_bin_name ) 1319 1320if not os .access (fish_bin_path ,os .X_OK ): 1321"fish could not be executed at path '%s'. " 1323"Is fish installed correctly?" % fish_bin_path 1324 ) 1325sys .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 : 1344try : 1345Handler = FishConfigHTTPRequestHandler 1346httpd = FishConfigTCPServer ((HOST ,PORT ),Handler ) 1347# Success 1348break 1349except (OSError ,socket .error )as e : 1350if e .errno == errno .EAFNOSUPPORT and HOST == "::" : 1351"Ipv6 seems to be unavailable, trying ipv4" ) 1352# We can't do ipv6, let's try v4. 1353HOST = "127.0.0.1" 1354PORT = 8000 1355# Override the class address_family variable (yes, this is a hack) 1356FishConfigTCPServer .address_family = socket .AF_INET 1357continue 1358if e .errno != errno .EADDRINUSE : 1359str (e )) 1360sys .exit (- 1 ) 1361PORT += 1 1362 1363if PORT > 9000 : 1364# Nobody say it 1365"Unable to find an open port between 8000 and 9000" ) 1366sys .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 : 1372for tab in [ 1373"functions" , 1374"prompt" , 1375"colors" , 1376"variables" , 1377"history" , 1378"bindings" , 1379 ]: 1380if tab .startswith (sys .argv [1 ]): 1381initial_tab = "#!/" + tab 1382break 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 (): 1396kwargs ["delete_on_close" ]= False 1397f = tempfile .NamedTemporaryFile ( 1398prefix = "web_config" , 1399suffix = ".html" , 1400mode = "w" , 1401delete = True , 1402** kwargs , 1403) 1404 1405f .write (redirect_template_html % (url ,url )) 1406f .flush () 1407 1408if is_windows (): 1409f .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 (): 1416fileurl = fileurl .replace ("\\" ,"/" ) 1417 1418"Web config started at %s%s%s" 1420% (ENTER_UNDERLINE_MODE ,fileurl ,EXIT_ATTRIBUTE_MODE ) 1421) 1422"If that doesn't work, try opening %s%s%s" 1424% (ENTER_UNDERLINE_MODE ,url ,EXIT_ATTRIBUTE_MODE ) 1425) 1426"%sHit ENTER to stop.%s" % (ENTER_BOLD_MODE ,EXIT_ATTRIBUTE_MODE )) 1427 1428 1429def runThing (): 1430if os .environ .get ("BROWSER" )== "true" : 1431# Don't start a browser in this case (see issue #11926) 1432pass 1433elif is_wsl (): 1434cmd_path = find_executable ("cmd.exe" ,COMMON_WSL_CMD_PATHS ) 1435if cmd_path : 1436subprocess .call ([cmd_path ,"/c" ,"start %s" % url ]) 1437else : 1438"Please add the directory containing cmd.exe to your $PATH" ) 1439sys .exit (- 1 ) 1440elif is_termux (): 1441subprocess .call (["termux-open-url" ,url ]) 1442elif is_chromeos_garcon (): 1443webbrowser .open (url ) 1444elif is_sailfish_os (): 1445subprocess .call (["xdg-open" ,url ]) 1446else : 1447webbrowser .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.""" 1466for port in range (start_port ,end_port + 1 ): 1467try : 1468sck = socket .socket (socket .AF_INET ,socket .SOCK_STREAM ) 1469sck .bind (("localhost" ,port )) 1470sck .listen () 1471return (sck ,port ) 1472except socket .error : 1473pass 1474"Unable to find an open port between {} and {}." .format (start_port ,end_port )) 1475sys .exit (- 1 ) 1476 1477 1478def capture_enter (port ): 1479"""Read keyboard events and establish a socket connection when pressing the Enter.""" 1480import msvcrt 1481 1482while True : 1483if msvcrt .kbhit (): 1484key = msvcrt .getch ().decode () 1485if key == "\r" or key == "\n" : 1486break 1487sck = socket .socket (socket .AF_INET ,socket .SOCK_STREAM ) 1488sck .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 ) 1502threading .Thread (target = capture_enter ,args = (sig_port ,)).start () 1503return sig 1504 1505 1506try : 1507httpd_fileno = httpd .fileno () 1508sig = get_windows_signal ()if is_windows ()else sys .stdin 1509sig_fileno = sig .fileno () 1510while True : 1511ready_read = select .select ([sig_fileno ,httpd_fileno ], [], []) 1512if ready_read [0 ][0 ]!= httpd_fileno : 1513"Shutting down." ) 1514 1515# On windows the newline has already been consumed by the capture_enter function. 1516if not is_windows (): 1517# Consume the newline so it doesn't get printed by the caller 1518sys .stdin .readline () 1519break 1520else : 1521httpd .handle_request () 1522except KeyboardInterrupt : 1523"\nShutting down." ) 1524 1525# Clean up temporary file 1526# If on Windows, the file already closed 1527if not is_windows (): 1528f .close () 1529 1530thread .join ()