fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
6d0cd3d9b
master
1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Run me like this: ./create_manpage_completions.py /usr/share/man/man{1,8}/* > man_completions.fish 5 6""" 7<OWNER> = Siteshwar Vashisht 8<YEAR> = 2012 9 10Copyright (c) 2012, Siteshwar Vashisht 11All rights reserved. 12 13Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 14 15Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 16Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 18""" 19 20import argparse 21import bz2 22import errno 23import gzip 24import os 25import re 26import string 27import subprocess 28import sys 29import traceback 30from subprocess import DEVNULL 31 32lzma_available = True 33try : 34import lzma 35except ImportError : 36lzma_available = False 37 38 39class Deroffer : 40g_specs_specletter = { 41# Output composed latin1 letters 42"-D" :"\320" , 43"Sd" :"\360" , 44"Tp" :"\376" , 45"TP" :"\336" , 46"AE" :"\306" , 47"ae" :"\346" , 48"OE" :"OE" , 49"oe" :"oe" , 50":a" :"\344" , 51":A" :"\304" , 52":e" :"\353" , 53":E" :"\313" , 54":i" :"\357" , 55":I" :"\317" , 56":o" :"\366" , 57":O" :"\326" , 58":u" :"\374" , 59":U" :"\334" , 60":y" :"\377" , 61"ss" :"\337" , 62"'A" :"\301" , 63"'E" :"\311" , 64"'I" :"\315" , 65"'O" :"\323" , 66"'U" :"\332" , 67"'Y" :"\335" , 68"'a" :"\341" , 69"'e" :"\351" , 70"'i" :"\355" , 71"'o" :"\363" , 72"'u" :"\372" , 73"'y" :"\375" , 74"^A" :"\302" , 75"^E" :"\312" , 76"^I" :"\316" , 77"^O" :"\324" , 78"^U" :"\333" , 79"^a" :"\342" , 80"^e" :"\352" , 81"^i" :"\356" , 82"^o" :"\364" , 83"^u" :"\373" , 84"`A" :"\300" , 85"`E" :"\310" , 86"`I" :"\314" , 87"`O" :"\322" , 88"`U" :"\331" , 89"`a" :"\340" , 90"`e" :"\350" , 91"`i" :"\354" , 92"`o" :"\362" , 93"`u" :"\371" , 94"~A" :"\303" , 95"~N" :"\321" , 96"~O" :"\325" , 97"~a" :"\343" , 98"~n" :"\361" , 99"~o" :"\365" , 100",C" :"\307" , 101",c" :"\347" , 102"/l" :"/l" , 103"/L" :"/L" , 104"/o" :"\370" , 105"/O" :"\330" , 106"oA" :"\305" , 107"oa" :"\345" , 108# Ligatures 109"fi" :"fi" , 110"ff" :"ff" , 111"fl" :"fl" , 112"Fi" :"ffi" , 113"Ff" :"fff" , 114"Fl" :"ffl" , 115 } 116 117g_specs = { 118"mi" :"-" , 119"en" :"-" , 120"hy" :"-" , 121"em" :"--" , 122"lq" :"“" , 123"rq" :"”" , 124"Bq" :",," , 125"oq" :"`" , 126"cq" :"'" , 127"aq" :"'" , 128"dq" :'"' , 129"or" :"|" , 130"at" :"@" , 131"sh" :"#" , 132"Eu" :"\244" , 133"eu" :"\244" , 134"Do" :"$" , 135"ct" :"\242" , 136"Fo" :"\253" , 137"Fc" :"\273" , 138"fo" :"<" , 139"fc" :">" , 140"r!" :"\241" , 141"r?" :"\277" , 142"Of" :"\252" , 143"Om" :"\272" , 144"pc" :"\267" , 145"S1" :"\271" , 146"S2" :"\262" , 147"S3" :"\263" , 148"<-" :"<-" , 149"->" :"->" , 150"<>" :"<->" , 151"ua" :"^" , 152"da" :"v" , 153"lA" :"<=" , 154"rA" :"=>" , 155"hA" :"<=>" , 156"uA" :"^^" , 157"dA" :"vv" , 158"ba" :"|" , 159"bb" :"|" , 160"br" :"|" , 161"bv" :"|" , 162"ru" :"_" , 163"ul" :"_" , 164"ci" :"O" , 165"bu" :"o" , 166"co" :"\251" , 167"rg" :"\256" , 168"tm" :"(TM)" , 169"dd" :"||" , 170"dg" :"|" , 171"ps" :"\266" , 172"sc" :"\247" , 173"de" :"\260" , 174"%0" :"0/00" , 175"14" :"\274" , 176"12" :"\275" , 177"34" :"\276" , 178"f/" :"/" , 179"sl" :"/" , 180"rs" :"\\" , 181"sq" :"[]" , 182"fm" :"'" , 183"ha" :"^" , 184"ti" :"~" , 185"lB" :"[" , 186"rB" :"]" , 187"lC" :"{" , 188"rC" :"}" , 189"la" :"<" , 190"ra" :">" , 191"lh" :"<=" , 192"rh" :"=>" , 193"tf" :"therefore" , 194"~~" :"~~" , 195"~=" :"~=" , 196"!=" :"!=" , 197"**" :"*" , 198"+-" :"\261" , 199"<=" :"<=" , 200"==" :"==" , 201"=~" :"=~" , 202">=" :">=" , 203"AN" :"\\/" , 204"OR" :"/\\" , 205"no" :"\254" , 206"te" :"there exists" , 207"fa" :"for all" , 208"Ah" :"aleph" , 209"Im" :"imaginary" , 210"Re" :"real" , 211"if" :"infinity" , 212"md" :"\267" , 213"mo" :"member of" , 214"mu" :"\327" , 215"nm" :"not member of" , 216"pl" :"+" , 217"eq" :"=" , 218"pt" :"oc" , 219"pp" :"perpendicular" , 220"sb" :"(=" , 221"sp" :"=)" , 222"ib" :"(-" , 223"ip" :"-)" , 224"ap" :"~" , 225"is" :"I" , 226"sr" :"root" , 227"pd" :"d" , 228"c*" :"(x)" , 229"c+" :"(+)" , 230"ca" :"cap" , 231"cu" :"U" , 232"di" :"\367" , 233"gr" :"V" , 234"es" :"{}" , 235"CR" :"_|" , 236"st" :"such that" , 237"/_" :"/_" , 238"lz" :"<>" , 239"an" :"-" , 240# Output Greek 241"*A" :"Alpha" , 242"*B" :"Beta" , 243"*C" :"Xi" , 244"*D" :"Delta" , 245"*E" :"Epsilon" , 246"*F" :"Phi" , 247"*G" :"Gamma" , 248"*H" :"Theta" , 249"*I" :"Iota" , 250"*K" :"Kappa" , 251"*L" :"Lambda" , 252"*M" :"Mu" , 253"*N" :"Nu" , 254"*O" :"Omicron" , 255"*P" :"Pi" , 256"*Q" :"Psi" , 257"*R" :"Rho" , 258"*S" :"Sigma" , 259"*T" :"Tau" , 260"*U" :"Upsilon" , 261"*W" :"Omega" , 262"*X" :"Chi" , 263"*Y" :"Eta" , 264"*Z" :"Zeta" , 265"*a" :"alpha" , 266"*b" :"beta" , 267"*c" :"xi" , 268"*d" :"delta" , 269"*e" :"epsilon" , 270"*f" :"phi" , 271"+f" :"phi" , 272"*g" :"gamma" , 273"*h" :"theta" , 274"+h" :"theta" , 275"*i" :"iota" , 276"*k" :"kappa" , 277"*l" :"lambda" , 278"*m" :"\265" , 279"*n" :"nu" , 280"*o" :"omicron" , 281"*p" :"pi" , 282"+p" :"omega" , 283"*q" :"psi" , 284"*r" :"rho" , 285"*s" :"sigma" , 286"*t" :"tau" , 287"*u" :"upsilon" , 288"*w" :"omega" , 289"*x" :"chi" , 290"*y" :"eta" , 291"*z" :"zeta" , 292"ts" :"sigma" , 293 } 294 295g_re_word = re .compile (r"[a-zA-Z_]+" )# equivalent to the word() method 296g_re_number = re .compile (r"[+-]?\d+" )# equivalent to the number() method 297g_re_esc_char = re .compile ( 298# novermin 299r"""([a-zA-Z_]) | # Word 300([+-]?\d) | # Number 301\\ # Backslash (for escape seq) 302""" , 303re .VERBOSE , 304 ) 305 306g_re_not_backslash_or_whitespace = re .compile ( 307r"[^ \t\n\r\f\v\\]+" 308 )# Match a sequence of not backslash or whitespace 309 310g_re_newline_collapse = re .compile (r"\n{3,}" ) 311 312g_re_font = re .compile ( 313# novermin 314r"""\\f( # Starts with backslash f 315(\(\S{2}) | # Open paren, then two printable chars 316(\[\S*?\]) | # Open bracket, zero or more printable characters, then close bracket 317\S) # Any printable character 318""" , 319re .VERBOSE , 320 ) 321 322# This gets filled in in __init__ below 323g_macro_dict = False 324 325def __init__ (self ): 326self .reg_table = {} 327self .tr_from = "" 328self .tr_to = "" 329self .tr = "" 330self .nls = 2 331self .specletter = False 332self .refer = False 333self .macro = 0 334self .nobody = False 335self .inlist = False 336self .inheader = False 337self .pic = False 338self .tbl = False 339self .tblstate = 0 340self .tblTab = "" 341self .eqn = False 342self .skipheaders = False 343self .skiplists = False 344self .ignore_sonx = False 345self .output = [] 346self .name = "" 347 348self .OPTIONS = 0 349self .FORMAT = 1 350self .DATA = 2 351 352# words is uninteresting and should be treated as false 353 354if not Deroffer .g_macro_dict : 355Deroffer .g_macro_dict = { 356"SH" :Deroffer .macro_sh , 357"SS" :Deroffer .macro_ss_ip , 358"IP" :Deroffer .macro_ss_ip , 359"H " :Deroffer .macro_ss_ip , 360"I " :Deroffer .macro_i_ir , 361"IR" :Deroffer .macro_i_ir , 362"IB" :Deroffer .macro_i_ir , 363"B " :Deroffer .macro_i_ir , 364"BR" :Deroffer .macro_i_ir , 365"BI" :Deroffer .macro_i_ir , 366"R " :Deroffer .macro_i_ir , 367"RB" :Deroffer .macro_i_ir , 368"RI" :Deroffer .macro_i_ir , 369"AB" :Deroffer .macro_i_ir , 370"Nm" :Deroffer .macro_Nm , 371"] " :Deroffer .macro_close_bracket , 372"PS" :Deroffer .macro_ps , 373"PE" :Deroffer .macro_pe , 374"TS" :Deroffer .macro_ts , 375"T&" :Deroffer .macro_t_and , 376"TE" :Deroffer .macro_te , 377"EQ" :Deroffer .macro_eq , 378"EN" :Deroffer .macro_en , 379"R1" :Deroffer .macro_r1 , 380"R2" :Deroffer .macro_r2 , 381"de" :Deroffer .macro_de , 382"BL" :Deroffer .macro_bl_vl , 383"VL" :Deroffer .macro_bl_vl , 384"AL" :Deroffer .macro_bl_vl , 385"LB" :Deroffer .macro_bl_vl , 386"RL" :Deroffer .macro_bl_vl , 387"ML" :Deroffer .macro_bl_vl , 388"DL" :Deroffer .macro_bl_vl , 389"BV" :Deroffer .macro_bv , 390"LE" :Deroffer .macro_le , 391"LP" :Deroffer .macro_lp_pp , 392"PP" :Deroffer .macro_lp_pp , 393"P\n" :Deroffer .macro_lp_pp , 394"ds" :Deroffer .macro_ds , 395"so" :Deroffer .macro_so_nx , 396"nx" :Deroffer .macro_so_nx , 397"tr" :Deroffer .macro_tr , 398"sp" :Deroffer .macro_sp , 399 } 400 401def flush_output (self ,where ): 402if where : 403where .write (self .get_output ()) 404self .output [:]= [] 405 406def get_output (self ): 407res = "" .join (self .output ) 408clean_res = Deroffer .g_re_newline_collapse .sub ("\n" ,res ) 409return clean_res 410 411def putchar (self ,c ): 412self .output .append (c ) 413return c 414 415# This gets swapped in in place of condputs the first time tr gets modified 416def condputs_tr (self ,str ): 417special = ( 418self .pic 419or self .eqn 420or self .refer 421or self .macro 422or (self .skiplists and self .inlist ) 423or (self .skipheaders and self .inheader ) 424 ) 425if not special : 426self .output .append (str .translate (self .tr )) 427 428def condputs (self ,str ): 429special = ( 430self .pic 431or self .eqn 432or self .refer 433or self .macro 434or (self .skiplists and self .inlist ) 435or (self .skipheaders and self .inheader ) 436 ) 437if not special : 438self .output .append (str ) 439 440def str_at (self ,idx ): 441return self .s [idx :idx + 1 ] 442 443def skip_char (self ,amt = 1 ): 444self .s = self .s [amt :] 445 446def skip_leading_whitespace (self ): 447self .s = self .s .lstrip () 448 449def is_white (self ,idx ): 450# Note this returns false for empty strings (idx >= len(self.s)) 451return self .s [idx :idx + 1 ].isspace () 452 453def str_eq (self ,offset ,other ,len ): 454return self .s [offset :offset + len ]== other [:len ] 455 456def prch (self ,idx ): 457# Note that this return False for the empty string (idx >= len(self.s)) 458ch = self .s [idx :idx + 1 ] 459return ch not in " \t\n" 460 461def font (self ): 462match = Deroffer .g_re_font .match (self .s ) 463if not match : 464return False 465self .skip_char (match .end ()) 466return True 467 468def font2 (self ): 469if self .s [0 :2 ]== "\\f" : 470c = self .str_at (2 ) 471if c == "(" and self .prch (3 )and self .prch (4 ): 472self .skip_char (5 ) 473return True 474elif c == "[" : 475self .skip_char (2 ) 476while self .prch (0 )and self .str_at (0 )!= "]" : 477self .skip_char () 478if self .str_at (0 )== "]" : 479self .skip_char () 480elif self .prch (2 ): 481self .skip_char (3 ) 482return True 483return False 484 485def comment (self ): 486# Here we require that the string start with \" 487while self .str_at (0 )and self .str_at (0 )!= "\n" : 488self .skip_char () 489return True 490 491def numreq (self ): 492# We require that the string starts with backslash 493if self .str_at (1 )in "hvwud" and self .str_at (2 )== "'" : 494self .macro += 1 495self .skip_char (3 ) 496while self .str_at (0 )!= "'" and self .esc_char (): 497pass # Weird 498if self .str_at (0 )== "'" : 499self .skip_char () 500self .macro -= 1 501return True 502return False 503 504def device_control (self ): 505# groff \X'...' device control escape (and \Z'...' zero-width). 506# help2man 1.50+ uses \X'tty: link URL' for hyperlinks. 507# We just skip the entire escape. 508if self .str_at (1 )in "XZ" and self .str_at (2 )== "'" : 509self .skip_char (3 ) 510while self .str_at (0 )and self .str_at (0 )!= "'" : 511self .skip_char () 512if self .str_at (0 )== "'" : 513self .skip_char () 514return True 515return False 516 517def var (self ): 518reg = "" 519s0s1 = self .s [0 :2 ] 520if s0s1 == "\\n" : 521if self .s [3 :5 ]== "dy" : 522self .skip_char (5 ) 523return True 524elif self .str_at (2 )== "(" and self .prch (3 )and self .prch (4 ): 525self .skip_char (5 ) 526return True 527elif self .str_at (2 )== "[" and self .prch (3 ): 528self .skip_char (3 ) 529while self .str_at (0 )and self .str_at (0 )!= "]" : 530self .skip_char () 531return True 532elif self .prch (2 ): 533self .skip_char (3 ) 534return True 535elif s0s1 == "\\*" : 536if self .str_at (2 )== "(" and self .prch (3 )and self .prch (4 ): 537reg = self .s [3 :5 ] 538self .skip_char (5 ) 539elif self .str_at (2 )== "[" and self .prch (3 ): 540self .skip_char (3 ) 541while self .str_at (0 )and self .str_at (0 )!= "]" : 542reg = reg + self .str_at (0 ) 543self .skip_char () 544if self .s [0 :1 ]== "]" : 545self .skip_char () 546else : 547return False 548elif self .prch (2 ): 549reg = self .str_at (2 ) 550self .skip_char (3 ) 551else : 552return False 553 554if reg in self .reg_table : 555old_s = self .s 556self .s = self .reg_table [reg ] 557self .text_arg () 558return True 559return False 560 561def size (self ): 562# We require that the string starts with \s 563if self .digit (2 )or (self .str_at (2 )in "-+" and self .digit (3 )): 564self .skip_char (3 ) 565while self .digit (0 ): 566self .skip_char () 567return True 568return False 569 570def spec (self ): 571self .specletter = False 572if self .s [0 :2 ]== "\\(" and self .prch (2 )and self .prch (3 ): 573key = self .s [2 :4 ] 574if key in Deroffer .g_specs_specletter : 575self .condputs (Deroffer .g_specs_specletter [key ]) 576self .specletter = True 577elif key in Deroffer .g_specs : 578self .condputs (Deroffer .g_specs [key ]) 579self .skip_char (4 ) 580return True 581elif self .s .startswith ("\\%" ): 582self .specletter = True 583self .skip_char (2 ) 584return True 585else : 586return False 587 588def esc (self ): 589# We require that the string start with backslash 590c = self .s [1 :2 ] 591if not c : 592return False 593if c in "eE" : 594self .condputs ("\\" ) 595elif c in "t" : 596self .condputs ("\t" ) 597elif c in "0~" : 598self .condputs (" " ) 599elif c in "|^&:" : 600pass 601else : 602self .condputs (c ) 603self .skip_char (2 ) 604return True 605 606def word (self ): 607got_something = False 608while True : 609match = Deroffer .g_re_word .match (self .s ) 610if not match : 611break 612got_something = True 613self .condputs (match .group (0 )) 614self .skip_char (match .end (0 )) 615 616# Consume all specials 617while self .spec (): 618if not self .specletter : 619break 620 621return got_something 622 623def text (self ): 624while True : 625idx = self .s .find ("\\" ) 626if idx == - 1 : 627self .condputs (self .s ) 628self .s = "" 629break 630else : 631self .condputs (self .s [:idx ]) 632self .skip_char (idx ) 633if not self .esc_char_backslash (): 634self .condputs (self .str_at (0 )) 635self .skip_char () 636return True 637 638def letter (self ,idx ): 639ch = self .str_at (idx ) 640return ch .isalpha ()or ch == "_" # underscore is used in C identifiers 641 642def digit (self ,idx ): 643ch = self .str_at (idx ) 644return ch .isdigit () 645 646def number (self ): 647match = Deroffer .g_re_number .match (self .s ) 648if not match : 649return False 650else : 651self .condputs (match .group (0 )) 652self .skip_char (match .end ()) 653return True 654 655def esc_char_backslash (self ): 656# Like esc_char, but we know the string starts with a backslash 657c = self .s [1 :2 ] 658if c == '"' : 659return self .comment () 660elif c == "f" : 661return self .font () 662elif c == "s" : 663return self .size () 664elif c in "hvwud" : 665return self .numreq () 666elif c in "XZ" : 667return self .device_control () 668elif c in "n*" : 669return self .var () 670elif c == "(" : 671return self .spec () 672else : 673return self .esc () 674 675def esc_char (self ): 676if self .s [0 :1 ]== "\\" : 677return self .esc_char_backslash () 678return self .word ()or self .number () 679 680def quoted_arg (self ): 681if self .str_at (0 )== '"' : 682self .skip_char () 683while self .s and self .str_at (0 )!= '"' : 684if not self .esc_char (): 685if self .s : 686self .condputs (self .str_at (0 )) 687self .skip_char () 688return True 689else : 690return False 691 692def text_arg (self ): 693# PCA: The deroff.c textArg() disallowed quotes at the start of an argument 694# I'm not sure if this was a bug or not 695got_something = False 696while True : 697match = Deroffer .g_re_not_backslash_or_whitespace .match (self .s ) 698if match : 699# Output the characters in the match 700self .condputs (match .group (0 )) 701self .skip_char (match .end (0 )) 702got_something = True 703 704# Next is either an escape, or whitespace, or the end 705# If it's the whitespace or the end, we're done 706if not self .s or self .is_white (0 ): 707return got_something 708 709# Try an escape 710if not self .esc_char (): 711# Some busted escape? Just output it 712self .condputs (self .str_at (0 )) 713self .skip_char () 714got_something = True 715 716def text_arg2 (self ): 717if not self .esc_char (): 718if self .s and not self .is_white (0 ): 719self .condputs (self .str_at (0 )) 720self .skip_char () 721else : 722return False 723while True : 724if not self .esc_char (): 725if self .s and not self .is_white (0 ): 726self .condputs (self .str_at (0 )) 727self .skip_char () 728else : 729return True 730 731# Macro functions 732def macro_sh (self ): 733for header_str in [" SYNOPSIS" ,' "SYNOPSIS' ," ‹BERSICHT" ,' "‹BERSICHT' ]: 734if self .s [2 :].startswith (header_str ): 735self .inheader = True 736break 737else : 738# Did not find a header string 739self .inheader = False 740self .nobody = True 741 742def macro_ss_ip (self ): 743self .nobody = True 744return False 745 746def macro_i_ir (self ): 747return False 748 749def macro_Nm (self ): 750if self .s == "Nm\n" : 751self .condputs (self .name ) 752else : 753self .name = self .s [3 :].strip ()+ " " 754return True 755 756def macro_close_bracket (self ): 757self .refer = False 758return False 759 760def macro_ps (self ): 761if self .is_white (2 ): 762self .pic = True 763self .condputs ("\n" ) 764return True 765 766def macro_pe (self ): 767if self .is_white (2 ): 768self .pic = False 769self .condputs ("\n" ) 770return True 771 772def macro_ts (self ): 773if self .is_white (2 ): 774self .tbl ,self .tblstate = True ,self .OPTIONS 775self .condputs ("\n" ) 776return True 777 778def macro_t_and (self ): 779if self .is_white (2 ): 780self .tbl ,self .tblstate = True ,self .FORMAT 781self .condputs ("\n" ) 782return True 783 784def macro_te (self ): 785if self .is_white (2 ): 786self .tbl = False 787self .condputs ("\n" ) 788return True 789 790def macro_eq (self ): 791if self .is_white (2 ): 792self .eqn = True 793self .condputs ("\n" ) 794return True 795 796def macro_en (self ): 797if self .is_white (2 ): 798self .eqn = False 799self .condputs ("\n" ) 800return True 801 802def macro_r1 (self ): 803if self .is_white (2 ): 804self .refer2 = True 805self .condputs ("\n" ) 806return True 807 808def macro_r2 (self ): 809if self .is_white (2 ): 810self .refer2 = False 811self .condputs ("\n" ) 812return True 813 814def macro_de (self ): 815macro = True 816self .condputs ("\n" ) 817return True 818 819def macro_bl_vl (self ): 820if self .is_white (2 ): 821self .inlist = True 822self .condputs ("\n" ) 823return True 824 825def macro_bv (self ): 826if self .str_at (2 )== "L" and self .white (self .str_at (3 )): 827self .inlist = True 828self .condputs ("\n" ) 829return True 830 831def macro_le (self ): 832if self .is_white (2 ): 833self .inlist = False 834self .condputs ("\n" ) 835return True 836 837def macro_lp_pp (self ): 838self .condputs ("\n" ) 839return True 840 841def macro_ds (self ): 842self .skip_char (2 ) 843self .skip_leading_whitespace () 844if self .str_at (0 ): 845# Split at whitespace 846comps = self .s .split (None ,2 ) 847if len (comps )== 2 : 848name ,value = comps 849value = value .rstrip () 850self .reg_table [name ]= value 851self .condputs ("\n" ) 852return True 853 854def macro_so_nx (self ): 855# We always ignore include directives 856# deroff.c for some reason allowed this to fall through to the 'tr' case 857# I think that was just a bug so I won't replicate it 858return True 859 860def macro_tr (self ): 861self .skip_char (2 ) 862self .skip_leading_whitespace () 863while self .s and self .str_at (0 )!= "\n" : 864c = self .str_at (0 ) 865ns = self .str_at (1 ) 866self .skip_char (2 ) 867if not ns or ns == "\n" : 868ns = " " 869self .tr_from += c 870self .tr_to += ns 871 872# Update our table, then swap in the slower tr-savvy condputs 873self .tr = str .maketrans (self .tr_from ,self .tr_to ) 874self .condputs = self .condputs_tr 875return True 876 877def macro_sp (self ): 878self .condputs ("\n" ) 879return True 880 881def macro_other (self ): 882self .condputs ("\n" ) 883return True 884 885def request_or_macro (self ): 886# s[0] is period or open single quote 887self .skip_char () 888s0 = self .s [1 :2 ] 889if s0 == "\\" : 890if self .str_at (1 )== '"' : 891self .condputs ("\n" ) 892return True 893else : 894pass 895elif s0 == "[" : 896self .refer = True 897self .condputs ("\n" ) 898return True 899elif s0 == "]" : 900self .refer = False 901self .skip_char () 902return self .text () 903elif s0 == "." : 904self .macro = False 905self .condputs ("\n" ) 906return True 907 908self .nobody = False 909s0s1 = self .s [0 :2 ] 910 911macro_func = Deroffer .g_macro_dict .get (s0s1 ,Deroffer .macro_other ) 912if macro_func (self ): 913return True 914 915if self .skipheaders and self .nobody : 916return True 917 918self .skip_leading_whitespace () 919while self .s and not self .is_white (0 ): 920self .skip_char () 921self .skip_leading_whitespace () 922while True : 923if not self .quoted_arg ()and not self .text_arg (): 924if self .s : 925self .condputs (self .str_at (0 )) 926self .skip_char () 927else : 928return True 929 930def request_or_macro2 (self ): 931self .skip_char () 932s0 = self .s [0 :1 ] 933if s0 == "\\" : 934if self .str_at (1 )== '"' : 935self .condputs ("\n" ) 936return True 937else : 938pass 939elif s0 == "[" : 940self .refer = True 941self .condputs ("\n" ) 942return True 943elif s0 == "]" : 944self .refer = False 945self .skip_char () 946return self .text () 947elif s0 == "." : 948self .macro = False 949self .condputs ("\n" ) 950return True 951 952self .nobody = False 953s0s1 = self .s [0 :2 ] 954if s0s1 == "SH" : 955for header_str in [" SYNOPSIS" ,' "SYNOPSIS' ," ‹BERSICHT" ,' "‹BERSICHT' ]: 956if self .s [2 :].startswith (header_str ): 957self .inheader = True 958break 959else : 960# Did not find a header string 961self .inheader = False 962self .nobody = True 963elif s0s1 in ["SS" ,"IP" ,"H " ]: 964self .nobody = True 965elif s0s1 in ["I " ,"IR" ,"IB" ,"B " ,"BR" ,"BI" ,"R " ,"RB" ,"RI" ,"AB" ]: 966pass 967elif s0s1 in ["] " ]: 968self .refer = False 969elif s0s1 in ["PS" ]: 970if self .is_white (2 ): 971self .pic = True 972self .condputs ("\n" ) 973return True 974elif s0s1 in ["PE" ]: 975if self .is_white (2 ): 976self .pic = False 977self .condputs ("\n" ) 978return True 979elif s0s1 in ["TS" ]: 980if self .is_white (2 ): 981self .tbl ,self .tblstate = True ,self .OPTIONS 982self .condputs ("\n" ) 983return True 984elif s0s1 in ["T&" ]: 985if self .is_white (2 ): 986self .tbl ,self .tblstate = True ,self .FORMAT 987self .condputs ("\n" ) 988return True 989elif s0s1 in ["TE" ]: 990if self .is_white (2 ): 991self .tbl = False 992self .condputs ("\n" ) 993return True 994elif s0s1 in ["EQ" ]: 995if self .is_white (2 ): 996self .eqn = True 997self .condputs ("\n" ) 998return True 999elif s0s1 in ["EN" ]: 1000if self .is_white (2 ): 1001self .eqn = False 1002self .condputs ("\n" ) 1003return True 1004elif s0s1 in ["R1" ]: 1005if self .is_white (2 ): 1006self .refer2 = True 1007self .condputs ("\n" ) 1008return True 1009elif s0s1 in ["R2" ]: 1010if self .is_white (2 ): 1011self .refer2 = False 1012self .condputs ("\n" ) 1013return True 1014elif s0s1 in ["de" ]: 1015macro = True 1016self .condputs ("\n" ) 1017return True 1018elif s0s1 in ["BL" ,"VL" ,"AL" ,"LB" ,"RL" ,"ML" ,"DL" ]: 1019if self .is_white (2 ): 1020self .inlist = True 1021self .condputs ("\n" ) 1022return True 1023elif s0s1 in ["BV" ]: 1024if self .str_at (2 )== "L" and self .white (self .str_at (3 )): 1025self .inlist = True 1026self .condputs ("\n" ) 1027return True 1028elif s0s1 in ["LE" ]: 1029if self .is_white (2 ): 1030self .inlist = False 1031self .condputs ("\n" ) 1032return True 1033elif s0s1 in ["LP" ,"PP" ,"P\n" ]: 1034self .condputs ("\n" ) 1035return True 1036elif s0s1 in ["ds" ]: 1037self .skip_char (2 ) 1038self .skip_leading_whitespace () 1039if self .str_at (0 ): 1040# Split at whitespace 1041comps = self .s .split (None ,2 ) 1042if len (comps )== 2 : 1043name ,value = comps 1044value = value .rstrip () 1045self .reg_table [name ]= value 1046self .condputs ("\n" ) 1047return True 1048elif s0s1 in ["so" ,"nx" ]: 1049# We always ignore include directives 1050# deroff.c for some reason allowed this to fall through to the 'tr' case 1051# I think that was just a bug so I won't replicate it 1052return True 1053elif s0s1 in ["tr" ]: 1054self .skip_char (2 ) 1055self .skip_leading_whitespace () 1056while self .s and self .str_at (0 )!= "\n" : 1057c = self .str_at (0 ) 1058ns = self .str_at (1 ) 1059self .skip_char (2 ) 1060if not ns or ns == "\n" : 1061ns = " " 1062self .tr_from += c 1063self .tr_to += ns 1064 1065# Update our table, then swap in the slower tr-savvy condputs 1066self .tr = str .maketrans (self .tr_from ,self .tr_to ) 1067self .condputs = self .condputs_tr 1068 1069return True 1070elif s0s1 in ["sp" ]: 1071self .condputs ("\n" ) 1072return True 1073else : 1074self .condputs ("\n" ) 1075return True 1076 1077if self .skipheaders and self .nobody : 1078return True 1079 1080self .skip_leading_whitespace () 1081while self .s and not self .is_white (0 ): 1082self .skip_char () 1083self .skip_leading_whitespace () 1084while True : 1085if not self .quoted_arg ()and not self .text_arg (): 1086if self .s : 1087self .condputs (self .str_at (0 )) 1088self .skip_char () 1089else : 1090return True 1091 1092def do_tbl (self ): 1093if self .tblstate == self .OPTIONS : 1094while self .s and self .str_at (0 )!= ";" and self .str_at (0 )!= "\n" : 1095self .skip_leading_whitespace () 1096if not self .str_at (0 ).isalpha (): 1097# deroff.c has a bug where it can loop forever here...we try to work around it 1098self .skip_char () 1099else :# Parse option 1100option = self .s 1101arg = "" 1102 1103idx = 0 1104while option [idx :idx + 1 ].isalpha (): 1105idx += 1 1106 1107if option [idx :idx + 1 ]== "(" : 1108option = option [:idx ] 1109self .s = self .s [idx + 1 :] 1110arg = self .s 1111else : 1112self .s = "" 1113 1114if arg : 1115idx = arg .find (")" ) 1116if idx != - 1 : 1117arg = arg [:idx ] 1118self .s = self .s [idx + 1 :] 1119else : 1120# self.skip_char() 1121pass 1122 1123if option .lower ()== "tab" : 1124self .tblTab = arg [0 :1 ] 1125 1126self .tblstate = self .FORMAT 1127self .condputs ("\n" ) 1128 1129elif self .tblstate == self .FORMAT : 1130while self .s and self .str_at (0 )!= "." and self .str_at (0 )!= "\n" : 1131self .skip_leading_whitespace () 1132if self .str_at (0 ): 1133self .skip_char () 1134 1135if self .str_at (0 )== "." : 1136self .tblstate = self .DATA 1137self .condputs ("\n" ) 1138elif self .tblstate == self .DATA : 1139if self .tblTab : 1140self .s = self .s .replace (self .tblTab ,"\t" ) 1141self .text () 1142return True 1143 1144def do_line (self ): 1145if self .s [0 :1 ]in ".'" : 1146if not self .request_or_macro (): 1147return False 1148elif self .tbl : 1149self .do_tbl () 1150else : 1151self .text () 1152return True 1153 1154def deroff (self ,str ): 1155lines = str .split ("\n" ) 1156for line in lines : 1157self .s = line + "\n" 1158if not self .do_line (): 1159break 1160# self.putchar('\n') 1161 1162 1163# This gets set to the name of the command that we are currently executing 1164CMDNAME = "" 1165 1166# Information used to track which of our parsers were successful 1167PARSER_INFO = {} 1168 1169# built_command writes into this global variable, yuck 1170built_command_output = [] 1171 1172# Diagnostic output 1173diagnostic_output = [] 1174diagnostic_indent = 0 1175 1176# Three diagnostic verbosity levels 1177VERY_VERBOSE ,BRIEF_VERBOSE ,NOT_VERBOSE = 2 ,1 ,0 1178 1179# Pick some reasonable default values for settings 1180VERBOSITY ,WRITE_TO_STDOUT ,DEROFF_ONLY ,KEEP_FILES = NOT_VERBOSE ,False ,False ,False 1181 1182 1183def add_diagnostic (dgn ,msg_verbosity = VERY_VERBOSE ): 1184# Add a diagnostic message, if msg_verbosity <= VERBOSITY 1185if msg_verbosity <= VERBOSITY : 1186diagnostic_output .append (" " * diagnostic_indent + dgn ) 1187 1188 1189def flush_diagnostics (where ): 1190if diagnostic_output : 1191output_str = "\n" .join (diagnostic_output ) 1192output_str ,file = where ) 1193diagnostic_output [:]= [] 1194 1195 1196# Make sure we don't output the same completion multiple times, which can happen 1197# For example, xsubpp.1.gz and xsubpp5.10.1.gz 1198# This maps commands to lists of completions 1199already_output_completions = {} 1200 1201 1202def compile_and_search (regex ,input ): 1203options_section_regex = re .compile (regex ,re .DOTALL ) 1204options_section_matched = re .search (options_section_regex ,input ) 1205return options_section_matched 1206 1207 1208def unquote_double_quotes (data ): 1209if len (data )< 2 : 1210return data 1211if data [0 ]== '"' and data [len (data )- 1 ]== '"' : 1212data = data [1 :len (data )- 1 ] 1213return data 1214 1215 1216def unquote_single_quotes (data ): 1217if len (data )< 2 : 1218return data 1219if data [0 ]== "`" and data [len (data )- 1 ]== "'" : 1220data = data [1 :len (data )- 1 ] 1221return data 1222 1223 1224# Make a string of characters that are deemed safe in fish without needing to be escaped 1225# Note that space is not included 1226g_fish_safe_chars = frozenset (string .ascii_letters + string .digits + "_+-/:=@" ) 1227 1228 1229def fish_escape_single_quote (str ): 1230# Escape a string if necessary so that it can be put in single quotes 1231# If it has no non-safe chars, there's nothing to do 1232if g_fish_safe_chars .issuperset (str ): 1233return str 1234 1235str = str .replace ("\\" ,"\\\\" )# Replace one backslash with two 1236str = str .replace ( 1237"'" ,"\\'" 1238 )# Replace one single quote with a backslash-single-quote 1239return "'" + str + "'" 1240 1241 1242def output_complete_command (cmdname ,args ,description ,output_list ): 1243comps = ["complete -c" ,cmdname ] 1244comps .extend (args ) 1245if description : 1246comps .append ("-d" ) 1247comps .append (description ) 1248output_list .append (" " .join (comps )) 1249 1250 1251def built_command (options ,description ): 1252# print "Options are: ", options 1253man_optionlist = re .split (' |,|"|=|[|]' ,options ) 1254fish_options = [] 1255for optionstr in man_optionlist : 1256option = re .sub (r"(\[.*\])" ,"" ,optionstr ) 1257option = option .strip (" \t\r\n[](){}.,:!" ) 1258 1259# Skip some problematic cases 1260if option in ["-" ,"--" ]: 1261continue 1262if any (c in "{}()" for c in option ): 1263continue 1264 1265if option .startswith ("--" ): 1266# New style long option (--recursive) 1267fish_options .append ("-l " + fish_escape_single_quote (option [2 :])) 1268elif option .startswith ("-" )and len (option )== 2 : 1269# New style short option (-r) 1270fish_options .append ("-s " + fish_escape_single_quote (option [1 :])) 1271elif option .startswith ("-" )and len (option )> 2 : 1272# Old style long option (-recursive) 1273fish_options .append ("-o " + fish_escape_single_quote (option [1 :])) 1274 1275# Determine which options are new (not already in existing_options) 1276# Then add those to the existing options 1277existing_options = already_output_completions .setdefault (CMDNAME ,set ()) 1278fish_options = [opt for opt in fish_options if opt not in existing_options ] 1279existing_options .update (fish_options ) 1280 1281# Maybe it's all for naught 1282if not fish_options : 1283return 1284 1285# Here's what we'll use to truncate if necessary 1286max_description_width = 78 1287truncation_suffix = "…" 1288 1289# Try to include as many whole sentences as will fit 1290# Clean up some probably bogus escapes in the process 1291clean_desc = description .replace ("\\'" ,"'" ).replace ("\\." ,"." ) 1292sentences = clean_desc .split ("." ) 1293 1294# Clean up "sentences" that are just whitespace 1295# But don't let it be empty 1296sentences = [x for x in sentences if x .strip ()] 1297if not sentences : 1298sentences = ["" ] 1299 1300udot = "." 1301uspace = " " 1302 1303truncated_description = sentences [0 ]+ udot 1304for line in sentences [1 :]: 1305if not line : 1306continue 1307proposed_description = truncated_description + uspace + line + udot 1308if len (proposed_description )<= max_description_width : 1309# It fits 1310truncated_description = proposed_description 1311else : 1312# No fit 1313break 1314# Strip trailing dots 1315truncated_description = truncated_description .strip (udot ) 1316 1317# If the first sentence does not fit, truncate if necessary 1318if len (truncated_description )> max_description_width : 1319prefix_len = max_description_width - len (truncation_suffix ) 1320truncated_description = truncated_description [:prefix_len ]+ truncation_suffix 1321 1322# Escape some more things 1323truncated_description = fish_escape_single_quote (truncated_description ) 1324escaped_cmd = fish_escape_single_quote (CMDNAME ) 1325 1326output_complete_command ( 1327escaped_cmd ,fish_options ,truncated_description ,built_command_output 1328 ) 1329 1330 1331def remove_groff_formatting (data ): 1332# Strip groff \X'...' device control escapes (help2man 1.50+ hyperlinks) 1333# and \Z'...' zero-width escapes. 1334data = re .sub (r"\\[XZ]'[^']*'" ,"" ,data ) 1335data = data .replace ("\\fI" ,"" ) 1336data = data .replace ("\\fP" ,"" ) 1337data = data .replace ("\\f1" ,"" ) 1338data = data .replace ("\\fB" ,"" ) 1339data = data .replace ("\\fR" ,"" ) 1340data = data .replace ("\\e" ,"" ) 1341data = re .sub (r".PD( \d+)" ,"" ,data ) 1342data = data .replace (".BI" ,"" ) 1343data = data .replace (".BR" ,"" ) 1344data = data .replace ("0.5i" ,"" ) 1345data = data .replace (".rb" ,"" ) 1346data = data .replace ("\\^" ,"" ) 1347data = data .replace ("{ " ,"" ) 1348data = data .replace (" }" ,"" ) 1349data = data .replace (r"\ " ,"" ) 1350data = data .replace (r"\-" ,"-" ) 1351data = data .replace (r"\&" ,"" ) 1352data = data .replace (".B" ,"" ) 1353data = data .replace (r"\-" ,"-" ) 1354data = data .replace (".I" ,"" ) 1355data = data .replace ("\f" ,"" ) 1356data = data .replace (r"\(oq" ,"'" ) 1357data = data .replace (r"\(cq" ,"'" ) 1358data = data .replace (r"\(aq" ,"'" ) 1359data = data .replace (r"\(dq" ,'"' ) 1360data = data .replace (r"\(lq" ,'"' ) 1361data = data .replace (r"\(rq" ,'"' ) 1362return data 1363 1364 1365class ManParser (object ): 1366def is_my_type (self ,manpage ): 1367return False 1368 1369def parse_man_page (self ,manpage ): 1370return False 1371 1372 1373class Type1ManParser (ManParser ): 1374def is_my_type (self ,manpage ): 1375return compile_and_search (r'\.SH "OPTIONS"(.*?)' ,manpage )is not None 1376 1377def parse_man_page (self ,manpage ): 1378options_section_regex = re .compile (r'\.SH "OPTIONS"(.*?)(\.SH|\Z)' ,re .DOTALL ) 1379options_section = re .search (options_section_regex ,manpage ).group (1 ) 1380 1381options_parts_regex = re .compile (r"\.PP(.*?)\.RE" ,re .DOTALL ) 1382options_matched = re .search (options_parts_regex ,options_section ) 1383add_diagnostic ("Command is %r" % CMDNAME ) 1384 1385if options_matched is None : 1386add_diagnostic ("Unable to find options" ) 1387if self .fallback (options_section ): 1388return True 1389elif self .fallback2 (options_section ): 1390return True 1391return False 1392 1393while options_matched is not None : 1394data = options_matched .group (1 ) 1395last_dotpp_index = data .rfind (".PP" ) 1396if last_dotpp_index != - 1 : 1397data = data [last_dotpp_index + 3 :] 1398 1399data = remove_groff_formatting (data ) 1400data = data .split (".RS 4" ) 1401if len (data )> 1 :# and len(data[1]) <= 300): 1402optionName = data [0 ].strip () 1403 1404if optionName .find ("-" )== - 1 : 1405add_diagnostic ("%r doesn't contain '-' " % optionName ) 1406else : 1407optionName = unquote_double_quotes (optionName ) 1408optionName = unquote_single_quotes (optionName ) 1409optionDescription = data [1 ].strip ().replace ("\n" ," " ) 1410built_command (optionName ,optionDescription ) 1411 1412else : 1413add_diagnostic ("Unable to split option from description" ) 1414return False 1415 1416options_section = options_section [options_matched .end ()- 3 :] 1417options_matched = re .search (options_parts_regex ,options_section ) 1418 1419def fallback (self ,options_section ): 1420add_diagnostic ("Trying fallback" ) 1421options_parts_regex = re .compile (r"\.TP( \d+)?(.*?)\.TP" ,re .DOTALL ) 1422options_matched = re .search (options_parts_regex ,options_section ) 1423if options_matched is None : 1424add_diagnostic ("Still not found" ) 1425return False 1426while options_matched is not None : 1427data = options_matched .group (2 ) 1428data = remove_groff_formatting (data ) 1429data = data .strip () 1430data = data .split ("\n" ,1 ) 1431if len (data )> 1 and len (data [1 ].strip ())> 0 :# and len(data[1])<400): 1432optionName = data [0 ].strip () 1433if optionName .find ("-" )== - 1 : 1434add_diagnostic ("%r doesn't contain '-'" % optionName ) 1435else : 1436optionName = unquote_double_quotes (optionName ) 1437optionName = unquote_single_quotes (optionName ) 1438optionDescription = data [1 ].strip ().replace ("\n" ," " ) 1439built_command (optionName ,optionDescription ) 1440else : 1441add_diagnostic ("Unable to split option from description" ) 1442return False 1443 1444options_section = options_section [options_matched .end ()- 3 :] 1445options_matched = re .search (options_parts_regex ,options_section ) 1446return True 1447 1448def fallback2 (self ,options_section ): 1449add_diagnostic ("Trying last chance fallback" ) 1450ix_remover_regex = re .compile (r"\.IX.*" ) 1451trailing_num_regex = re .compile ("\\d+$" ) 1452options_parts_regex = re .compile (r"\.IP (.*?)\.IP" ,re .DOTALL ) 1453 1454options_section = re .sub (ix_remover_regex ,"" ,options_section ) 1455options_matched = re .search (options_parts_regex ,options_section ) 1456if options_matched is None : 1457add_diagnostic ("Still (still!) not found" ) 1458return False 1459while options_matched is not None : 1460data = options_matched .group (1 ) 1461 1462data = remove_groff_formatting (data ) 1463data = data .strip () 1464data = data .split ("\n" ,1 ) 1465if len (data )> 1 and len (data [1 ].strip ())> 0 :# and len(data[1])<400): 1466optionName = re .sub (trailing_num_regex ,"" ,data [0 ].strip ()) 1467 1468if "-" not in optionName : 1469add_diagnostic ("%r doesn't contain '-'" % optionName ) 1470else : 1471optionName = optionName .strip () 1472optionName = unquote_double_quotes (optionName ) 1473optionName = unquote_single_quotes (optionName ) 1474optionDescription = data [1 ].strip ().replace ("\n" ," " ) 1475built_command (optionName ,optionDescription ) 1476else : 1477add_diagnostic ("Unable to split option from description" ) 1478return False 1479 1480options_section = options_section [options_matched .end ()- 3 :] 1481options_matched = re .search (options_parts_regex ,options_section ) 1482return True 1483 1484 1485class Type2ManParser (ManParser ): 1486def is_my_type (self ,manpage ): 1487return compile_and_search (r"\.SH OPTIONS(.*?)" ,manpage )is not None 1488 1489def parse_man_page (self ,manpage ): 1490options_section_regex = re .compile (r"\.SH OPTIONS(.*?)(\.SH|\Z)" ,re .DOTALL ) 1491options_section = re .search (options_section_regex ,manpage ).group (1 ) 1492 1493options_parts_regex = re .compile ( 1494r"\.[IT]P( \d+(\.\d)?i?)?(.*?)\.([IT]P|UNINDENT|UN|SH)" ,re .DOTALL 1495 ) 1496options_matched = re .search (options_parts_regex ,options_section ) 1497add_diagnostic ("Command is %r" % CMDNAME ) 1498 1499if options_matched is None : 1500add_diagnostic ("%r: Unable to find options" % self ) 1501return False 1502 1503while options_matched is not None : 1504data = options_matched .group (3 ) 1505data = remove_groff_formatting (data ) 1506data = data .strip () 1507data = data .split ("\n" ,1 ) 1508 1509if len (data )> 1 and len (data [1 ].strip ())> 0 :# and len(data[1])<400): 1510optionName = data [0 ].strip () 1511if "-" not in optionName : 1512add_diagnostic ("%r doesn't contain '-'" % optionName ) 1513else : 1514optionName = unquote_double_quotes (optionName ) 1515optionName = unquote_single_quotes (optionName ) 1516optionDescription = data [1 ].strip ().replace ("\n" ," " ) 1517built_command (optionName ,optionDescription ) 1518else : 1519add_diagnostic ("Unable to split option from description" ) 1520 1521options_section = options_section [options_matched .end ()- 3 :] 1522options_matched = re .search (options_parts_regex ,options_section ) 1523 1524 1525class Type3ManParser (ManParser ): 1526def is_my_type (self ,manpage ): 1527return compile_and_search (r"\.SH DESCRIPTION(.*?)" ,manpage )is not None 1528 1529def parse_man_page (self ,manpage ): 1530options_section_regex = re .compile (r"\.SH DESCRIPTION(.*?)(\.SH|\Z)" ,re .DOTALL ) 1531options_section = re .search (options_section_regex ,manpage ).group (1 ) 1532 1533options_parts_regex = re .compile (r"\.TP(.*?)\.TP" ,re .DOTALL ) 1534options_matched = re .search (options_parts_regex ,options_section ) 1535add_diagnostic ("Command is %r" % CMDNAME ) 1536 1537if options_matched is None : 1538add_diagnostic ("Unable to find options section" ) 1539return False 1540 1541while options_matched is not None : 1542data = options_matched .group (1 ) 1543 1544data = remove_groff_formatting (data ) 1545data = data .strip () 1546data = data .split ("\n" ,1 ) 1547 1548if len (data )> 1 :# and len(data[1])<400): 1549optionName = data [0 ].strip () 1550if optionName .find ("-" )== - 1 : 1551add_diagnostic ("%r doesn't contain '-'" % optionName ) 1552else : 1553optionName = unquote_double_quotes (optionName ) 1554optionName = unquote_single_quotes (optionName ) 1555optionDescription = data [1 ].strip ().replace ("\n" ," " ) 1556built_command (optionName ,optionDescription ) 1557 1558else : 1559add_diagnostic ("Unable to split option from description" ) 1560return False 1561 1562options_section = options_section [options_matched .end ()- 3 :] 1563options_matched = re .search (options_parts_regex ,options_section ) 1564 1565 1566class Type4ManParser (ManParser ): 1567def is_my_type (self ,manpage ): 1568return compile_and_search (r"\.SH FUNCTION LETTERS(.*?)" ,manpage )is not None 1569 1570def parse_man_page (self ,manpage ): 1571options_section_regex = re .compile ( 1572r"\.SH FUNCTION LETTERS(.*?)(\.SH|\Z)" ,re .DOTALL 1573 ) 1574options_section = re .search (options_section_regex ,manpage ).group (1 ) 1575 1576options_parts_regex = re .compile (r"\.TP(.*?)\.TP" ,re .DOTALL ) 1577options_matched = re .search (options_parts_regex ,options_section ) 1578add_diagnostic ("Command is %r" % CMDNAME ) 1579 1580if options_matched is None : 1581"Unable to find options section" ,file = sys .stderr ) 1582return False 1583 1584while options_matched is not None : 1585data = options_matched .group (1 ) 1586 1587data = remove_groff_formatting (data ) 1588data = data .strip () 1589data = data .split ("\n" ,1 ) 1590 1591if len (data )> 1 :# and len(data[1])<400): 1592optionName = data [0 ].strip () 1593if optionName .find ("-" )== - 1 : 1594add_diagnostic ("%r doesn't contain '-' " % optionName ) 1595else : 1596optionName = unquote_double_quotes (optionName ) 1597optionName = unquote_single_quotes (optionName ) 1598optionDescription = data [1 ].strip ().replace ("\n" ," " ) 1599built_command (optionName ,optionDescription ) 1600 1601else : 1602add_diagnostic ("Unable to split option from description" ) 1603return False 1604 1605options_section = options_section [options_matched .end ()- 3 :] 1606options_matched = re .search (options_parts_regex ,options_section ) 1607 1608 1609class TypeScdocManParser (ManParser ): 1610def is_my_type (self ,manpage ): 1611return ( 1612compile_and_search ( 1613r"\.(\\)(\") Generated by scdoc(.*?)\.SH OPTIONS(.*?)" ,manpage 1614 ) 1615is not None 1616 ) 1617 1618def parse_man_page (self ,manpage ): 1619options_section_regex = re .compile (r"\.SH OPTIONS(.*?)\.SH" ,re .DOTALL ) 1620options_section_matched = re .search (options_section_regex ,manpage ) 1621if options_section_matched is None : 1622return False 1623options_section = options_section_matched .group (1 ) 1624 1625options_parts_regex = re .compile (r"(.*?)\.RE" ,re .DOTALL ) 1626options_matched = re .match (options_parts_regex ,options_section ) 1627add_diagnostic ("Command is %r" % CMDNAME ) 1628 1629if options_matched is None : 1630add_diagnostic ("%r: Unable to find options" % self ) 1631return False 1632 1633while options_matched is not None : 1634# Get first option and move options_section 1635option = options_matched .group (1 ) 1636options_section = options_section [options_matched .end () : :] 1637options_matched = re .match (options_parts_regex ,options_section ) 1638 1639option = remove_groff_formatting (option ) 1640option = option .split ("\n" ) 1641 1642# Crean option list 1643option_clean = [] 1644for line in option : 1645if line not in ["" ,".P" ,".RS 4" ]: 1646option_clean .append (line ) 1647 1648# Shold be at least two lines 1649if len (option_clean )< 2 : 1650add_diagnostic ("Unable to split option from description" ) 1651continue 1652 1653# Name and description, others lines are ignored 1654option_name = option_clean [0 ] 1655option_description = option_clean [1 ] 1656 1657if "-" not in option_name : 1658add_diagnostic ("%r doesn't contain '-'" % option_name ) 1659 1660option_name = unquote_double_quotes (option_name ) 1661option_name = unquote_single_quotes (option_name ) 1662built_command (option_name ,option_description ) 1663 1664return True 1665 1666 1667class TypeDarwinManParser (ManParser ): 1668def is_my_type (self ,manpage ): 1669return compile_and_search (r"\.S[hH] DESCRIPTION" ,manpage )is not None 1670 1671def trim_groff (self ,line ): 1672# Remove initial period 1673if line .startswith ("." ): 1674line = line [1 :] 1675# Skip leading groff crud 1676while re .match (r"[A-Z][a-z]\s" ,line ): 1677line = line [3 :] 1678 1679# If the line ends with a space and then a period or comma, then erase the space 1680# This hack handles lines of the form '.Ar projectname .' 1681if line .endswith (" ," )or line .endswith (" ." ): 1682line = line [:- 2 ]+ line [- 1 ] 1683return line 1684 1685def count_argument_dashes (self ,line ): 1686# Determine how many dashes the line has using the following regex hack 1687# Look for the start of a line, followed by a dot, then a sequence of 1688# one or more dashes ('Fl') 1689result = 0 1690if line .startswith ("." ): 1691line = line [4 :] 1692while line .startswith ("Fl " ): 1693result = result + 1 1694line = line [3 :] 1695return result 1696 1697# Replace some groff escapes. There's a lot we don't bother to handle. 1698def groff_replace_escapes (self ,line ): 1699line = line .replace (".Nm" ,CMDNAME ) 1700line = line .replace ("\\ " ," " ) 1701line = line .replace (r"\& " ,"" ) 1702line = line .replace (".Pp" ,"" ) 1703return line 1704 1705def is_option (self ,line ): 1706return line .startswith (".It Fl" ) 1707 1708def parse_man_page (self ,manpage ): 1709got_something = False 1710lines = manpage .splitlines () 1711# Discard lines until we get to ".sh Description" 1712while lines and not ( 1713lines [0 ].startswith (".Sh DESCRIPTION" ) 1714or lines [0 ].startswith (".SH DESCRIPTION" ) 1715 ): 1716lines .pop (0 ) 1717 1718while lines : 1719# Pop until we get to the next option 1720while lines and not self .is_option (lines [0 ]): 1721lines .pop (0 ) 1722 1723if not lines : 1724continue 1725 1726# Get the line and clean it up 1727line = lines .pop (0 ) 1728 1729# Try to guess how many dashes this argument has 1730dash_count = self .count_argument_dashes (line ) 1731 1732line = self .groff_replace_escapes (line ) 1733line = self .trim_groff (line ) 1734line = line .strip () 1735if not line : 1736continue 1737 1738# Extract the name 1739name = line .split (None ,2 )[0 ] 1740 1741# Extract the description 1742desc_lines = [] 1743while lines and not self .is_option (lines [0 ]): 1744line = lines .pop (0 ).strip () 1745# Ignore comments 1746if line .startswith (r".\"" ): 1747continue 1748if line .startswith ("." ): 1749line = self .groff_replace_escapes (line ) 1750line = self .trim_groff (line ).strip () 1751if line : 1752desc_lines .append (line ) 1753desc = " " .join (desc_lines ) 1754 1755if name == "-" : 1756# Skip double -- arguments 1757continue 1758elif len (name )> 1 : 1759# Output the command 1760built_command (("-" * dash_count )+ name ,desc ) 1761got_something = True 1762elif len (name )== 1 : 1763built_command ("-" + name ,desc ) 1764got_something = True 1765 1766return got_something 1767 1768 1769class TypeDeroffManParser (ManParser ): 1770def is_my_type (self ,manpage ): 1771return True # We're optimists 1772 1773def is_option (self ,line ): 1774return line .startswith ("-" ) 1775 1776def could_be_description (self ,line ): 1777return len (line )> 0 and not line .startswith ("-" ) 1778 1779def parse_man_page (self ,manpage ): 1780d = Deroffer () 1781d .deroff (manpage ) 1782output = d .get_output () 1783lines = output .split ("\n" ) 1784 1785got_something = False 1786 1787# Discard lines until we get to DESCRIPTION or OPTIONS 1788while lines and not ( 1789lines [0 ].startswith ("DESCRIPTION" ) 1790or lines [0 ].startswith ("OPTIONS" ) 1791or lines [0 ].startswith ("COMMAND OPTIONS" ) 1792 ): 1793lines .pop (0 ) 1794 1795# Look for BUGS and stop there 1796for idx in range (len (lines )): 1797line = lines [idx ] 1798if line .startswith ("BUGS" ): 1799# Drop remaining elements 1800lines [idx :]= [] 1801break 1802 1803while lines : 1804# Pop until we get to the next option 1805while lines and not self .is_option (lines [0 ]): 1806line = lines .pop (0 ) 1807 1808if not lines : 1809continue 1810 1811options = lines .pop (0 ) 1812 1813# Pop until we get to either an empty line or a line starting with - 1814description = "" 1815while lines and self .could_be_description (lines [0 ]): 1816if description : 1817description += " " 1818description += lines .pop (0 ) 1819 1820built_command (options ,description ) 1821got_something = True 1822 1823return got_something 1824 1825 1826# Return whether the file at the given path is overwritable 1827# Raises IOError if it cannot be opened 1828def file_is_overwritable (path ): 1829result = False 1830file = open (path ,"r" ,encoding = "utf-8" ) 1831for line in file : 1832# Skip leading empty lines 1833line = line .strip () 1834if not line : 1835continue 1836 1837# We look in the initial run of lines that start with # 1838if not line .startswith ("#" ): 1839break 1840 1841# See if this contains the magic word 1842if "Autogenerated" in line : 1843result = True 1844break 1845 1846file .close () 1847return result 1848 1849 1850# Remove any and all autogenerated completions in the given directory 1851def cleanup_autogenerated_completions_in_directory (dir ): 1852try : 1853for filename in os .listdir (dir ): 1854# Skip non .fish files 1855if not filename .endswith (".fish" ): 1856continue 1857path = os .path .join (dir ,filename ) 1858cleanup_autogenerated_file (path ) 1859except OSError : 1860return False 1861 1862 1863# Delete the file if it is autogenerated 1864def cleanup_autogenerated_file (path ): 1865try : 1866if file_is_overwritable (path ): 1867os .remove (path ) 1868except (OSError ,IOError ): 1869pass 1870 1871 1872def parse_manpage_at_path (manpage_path ,output_directory ): 1873# Return if CMDNAME is in 'ignoredcommands' 1874ignoredcommands = [ 1875"cc" , 1876"g++" , 1877"gcc" , 1878"c++" , 1879"cpp" , 1880"emacs" , 1881"gprof" , 1882"wget" , 1883"ld" , 1884"awk" , 1885 ] 1886if CMDNAME in ignoredcommands : 1887return 1888 1889# Ignore some commands' gazillion man pages 1890# for subcommands - especially things we already have 1891ignored_prefixes = [ 1892"bundle-" , 1893"cargo-" , 1894"ffmpeg-" , 1895"flatpak-" , 1896"gcloud_" , 1897"git-" , 1898"jj-" , 1899"npm-" , 1900"openssl-" , 1901"ostree-" , 1902"perf-" , 1903"perl" , 1904"pip-" , 1905"zsh" , 1906 ] 1907for prefix in ignored_prefixes : 1908if CMDNAME .startswith (prefix ): 1909return 1910 1911# Clear diagnostics 1912global diagnostic_indent 1913diagnostic_output [:]= [] 1914diagnostic_indent = 0 1915 1916# Set up some diagnostics 1917add_diagnostic ("Considering " + manpage_path ) 1918diagnostic_indent += 1 1919 1920if manpage_path .endswith (".gz" ): 1921fd = gzip .open (manpage_path ,"r" ) 1922manpage = fd .read () 1923manpage = manpage .decode ("latin-1" ) 1924elif manpage_path .endswith (".bz2" ): 1925fd = bz2 .BZ2File (manpage_path ,"r" ) 1926manpage = fd .read () 1927manpage = manpage .decode ("latin-1" ) 1928elif manpage_path .endswith (".xz" )or manpage_path .endswith (".lzma" ): 1929if not lzma_available : 1930return 1931fd = lzma .LZMAFile (str (manpage_path ),"r" ) 1932manpage = fd .read () 1933manpage = manpage .decode ("latin-1" ) 1934elif manpage_path .endswith ((".1" ,".2" ,".3" ,".4" ,".5" ,".6" ,".7" ,".8" ,".9" )): 1935fd = open (manpage_path ,"r" ,encoding = "latin-1" ) 1936manpage = fd .read () 1937else : 1938return 1939fd .close () 1940 1941manpage = str (manpage ) 1942 1943# Ignore the millions of links to BUILTIN(1) 1944if "BUILTIN 1" in manpage or "builtin.1" in manpage : 1945return 1946 1947# Clear the output list 1948built_command_output [:]= [] 1949global already_output_completions 1950already_output_completions = {} 1951 1952if DEROFF_ONLY : 1953parsers = [TypeDeroffManParser ()] 1954else : 1955parsers = [ 1956TypeScdocManParser (), 1957Type1ManParser (), 1958Type2ManParser (), 1959Type4ManParser (), 1960Type3ManParser (), 1961TypeDarwinManParser (), 1962TypeDeroffManParser (), 1963 ] 1964parsersToTry = [p for p in parsers if p .is_my_type (manpage )] 1965 1966success = False 1967for parser in parsersToTry : 1968add_diagnostic ("Trying %s" % parser .__class__ .__name__ ) 1969diagnostic_indent += 1 1970success = parser .parse_man_page (manpage ) 1971diagnostic_indent -= 1 1972# Make sure empty files aren't reported as success 1973if not built_command_output : 1974success = False 1975if success : 1976PARSER_INFO .setdefault (parser .__class__ .__name__ , []).append (CMDNAME ) 1977break 1978 1979if success : 1980if WRITE_TO_STDOUT : 1981output_file = sys .stdout 1982else : 1983fullpath = os .path .join (output_directory ,CMDNAME + ".fish" ) 1984try : 1985output_file = open (fullpath ,"w" ,encoding = "utf-8" ) 1986except IOError as err : 1987add_diagnostic ( 1988"Unable to open file '%s': error(%d): %s" 1989% (fullpath ,err .errno ,err .strerror ) 1990 ) 1991return False 1992 1993# Output the magic word Autogenerated so we can tell if we can overwrite this 1994built_command_output .insert ( 19950 ,"# " + CMDNAME + "\n# Autogenerated from man page " + manpage_path 1996 ) 1997# built_command_output.insert(2, "# using " + parser.__class__.__name__) # XXX MISATTRIBUTES THE CULPABLE PARSER! Was really using Type2 but reporting TypeDeroffManParser 1998 1999for line in built_command_output : 2000output_file .write (line ) 2001output_file .write ("\n" ) 2002output_file .write ("\n" ) 2003add_diagnostic (manpage_path + " parsed successfully" ) 2004if output_file != sys .stdout : 2005output_file .close () 2006else : 2007parser_names = ", " .join (p .__class__ .__name__ for p in parsersToTry ) 2008# add_diagnostic('%s contains no options or is unparsable' % manpage_path, BRIEF_VERBOSE) 2009add_diagnostic ( 2010"%s contains no options or is unparsable (tried parser %s)" 2011% (manpage_path ,parser_names ), 2012BRIEF_VERBOSE , 2013 ) 2014 2015return success 2016 2017 2018def parse_and_output_man_pages (paths ,output_directory ,show_progress ): 2019global diagnostic_indent ,CMDNAME 2020paths .sort () 2021total_count = len (paths ) 2022successful_count ,index = 0 ,0 2023padding_len = len (str (total_count )) 2024last_progress_string_length = 0 2025if show_progress and not WRITE_TO_STDOUT : 2026"Parsing man pages and writing completions to {0}" .format (output_directory ) 2028 ) 2029 2030man_page_suffixes = set ([os .path .splitext (m )[1 ][1 :]for m in paths ]) 2031lzma_xz_occurs = "xz" in man_page_suffixes or "lzma" in man_page_suffixes 2032if lzma_xz_occurs and not lzma_available : 2033add_diagnostic ( 2034'At least one man page is compressed with lzma or xz, but the "lzma" module is not available.' 2035" Any man page compressed with either will be skipped." , 2036NOT_VERBOSE , 2037 ) 2038flush_diagnostics (sys .stderr ) 2039 2040for manpage_path in paths : 2041index += 1 2042 2043# Get the "base" command, e.g. mkfs.xfs.8.gz -> mkfs.xfs 2044man_file_name = os .path .basename (manpage_path ) 2045# 1. strip the optional compressor suffix 2046CMDNAME ,extension = os .path .splitext (man_file_name ) 2047# 2. strip mandatory section nu:ber 2048if CMDNAME .endswith ((".1" ,".2" ,".3" ,".4" ,".5" ,".6" ,".7" ,".8" ,".9" )): 2049CMDNAME ,extension = os .path .splitext (CMDNAME ) 2050# 3. XXX strip optional version(?) number? 2051# see comment above `already_output_completions = {}` line 2052 2053# Show progress if we're doing that 2054if show_progress : 2055progress_str = " {0} / {1} : {2}" .format ( 2056 (str (index ).rjust (padding_len )),total_count ,man_file_name 2057 ) 2058# Pad on the right with spaces so we overwrite whatever we wrote last time 2059padded_progress_str = progress_str .ljust (last_progress_string_length ) 2060last_progress_string_length = len (progress_str ) 2061sys .stdout .write ("\r{0}\r" .format (padded_progress_str )) 2062sys .stdout .flush () 2063 2064try : 2065if parse_manpage_at_path (manpage_path ,output_directory ): 2066successful_count += 1 2067except IOError : 2068diagnostic_indent = 0 2069add_diagnostic ("Cannot open " + manpage_path ) 2070except (KeyboardInterrupt ,SystemExit ): 2071raise 2072except : 2073add_diagnostic ( 2074"Error parsing %s: %s" % (manpage_path ,sys .exc_info ()[0 ]), 2075BRIEF_VERBOSE , 2076 ) 2077flush_diagnostics (sys .stderr ) 2078traceback .print_exc (file = sys .stderr ) 2079flush_diagnostics (sys .stderr ) 2080"" )# Newline after loop 2081add_diagnostic ( 2082"Successfully parsed %d / %d pages" % (successful_count ,total_count ), 2083BRIEF_VERBOSE , 2084 ) 2085flush_diagnostics (sys .stderr ) 2086 2087 2088def get_paths_from_man_locations (): 2089# Return all the paths to man(1) and man(8) files in the manpath 2090 2091parent_paths = [] 2092 2093# Most (GNU, macOS, Haiku) modern implementations of man support being called with `--path`. 2094# Traditional implementations require a second `manpath` program: examples include FreeBSD and Solaris. 2095# Prefer an external program first because these programs return a superset of the $MANPATH variable. 2096for prog in [["man" ,"--path" ], ["manpath" ]]: 2097try : 2098output = subprocess .check_output (prog ,stderr = DEVNULL ) 2099output = output .decode ("latin-1" ) 2100parent_paths = output .strip ().split (":" ) 2101break 2102except (OSError ,subprocess .CalledProcessError ): 2103continue 2104# If we can't have the OS interpret $MANPATH, just use it as-is (gulp). 2105if not parent_paths and os .getenv ("MANPATH" ): 2106parent_paths = os .getenv ("MANPATH" ).strip ().split (":" ) 2107# Fallback: With mandoc (OpenBSD, embedded Linux) and NetBSD man, the only way to get the default manpath is by reading /etc. 2108if not parent_paths : 2109try : 2110with open ("/etc/man.conf" ,"r" )as file : 2111data = file .read () 2112for key in ["MANPATH" ,"_default" ]: 2113# novermin 2114for match in re .findall (r"^%s\s+(.*)$" % key ,data ,re .I | re .M ): 2115parent_paths .append (match ) 2116except FileNotFoundError : 2117pass 2118# Fallback: hard-code some common paths. These should be likely for FHS Linux distros, BSDs, and macOS. 2119if not parent_paths : 2120parent_paths = ["/usr/share/man" ,"/usr/local/man" ,"/usr/local/share/man" ] 2121"Unable to get the manpath, falling back to %s." % ":" .join (parent_paths ), 2123"Explicitly set $MANPATH to fix this error." , 2124file = sys .stderr , 2125 ) 2126 2127result = [] 2128for parent_path in parent_paths : 2129for section in ["man1" ,"man6" ,"man8" ]: 2130directory_path = os .path .join (parent_path ,section ) 2131try : 2132names = os .listdir (directory_path ) 2133except OSError : 2134names = [] 2135names .sort () 2136for name in names : 2137result .append (os .path .join (directory_path ,name )) 2138return result 2139 2140 2141if __name__ == "__main__" : 2142script_name = sys .argv [0 ] 2143parser = argparse .ArgumentParser ( 2144description = "create_manpage_completions: Generate fish-shell completions from manpages" 2145 ) 2146parser .add_argument ( 2147"-c" , 2148"--cleanup-in" , 2149type = str , 2150help = "Directories to clean up in" , 2151action = "append" , 2152 ) 2153parser .add_argument ( 2154"-d" , 2155"--directory" , 2156type = str , 2157help = "The directory to save the completions in" , 2158 ) 2159parser .add_argument ( 2160"-k" , 2161"--keep" , 2162help = "Whether to keep files in the target directory" , 2163action = "store_true" , 2164 ) 2165parser .add_argument ( 2166"-m" , 2167"--manpath" , 2168help = "Whether to use manpath" , 2169action = "store_true" , 2170 ) 2171parser .add_argument ( 2172"-p" , 2173"--progress" , 2174help = "Whether to show progress" , 2175action = "store_true" , 2176 ) 2177parser .add_argument ( 2178"-s" , 2179"--stdout" , 2180help = "Write the completions to stdout" , 2181action = "store_true" , 2182 ) 2183parser .add_argument ( 2184"-v" , 2185"--verbose" , 2186type = int , 2187choices = [0 ,1 ,2 ], 2188help = "The level of debug output to show" , 2189 ) 2190parser .add_argument ( 2191"-z" , 2192"--deroff-only" , 2193help = "Whether to just deroff" , 2194action = "store_true" , 2195 ) 2196parser .add_argument ("file_paths" ,type = str ,nargs = "*" ) 2197 2198args = parser .parse_args () 2199 2200if args .verbose : 2201VERBOSITY = args .verbose 2202if args .stdout : 2203WRITE_TO_STDOUT = True 2204if args .deroff_only : 2205DEROFF_ONLY = True 2206if args .keep : 2207KEEP_FILES = True 2208if args .manpath : 2209# Fetch all man1 and man8 files from the manpath or man.conf 2210args .file_paths .extend (get_paths_from_man_locations ()) 2211 2212# Directories within which we will clean up autogenerated completions 2213# This script originally wrote completions into ~/.config/fish/completions 2214# Now it writes them into a separate directory 2215if args .cleanup_in : 2216for cleanup_dir in args .cleanup_in : 2217cleanup_autogenerated_completions_in_directory (cleanup_dir ) 2218 2219if not args .file_paths : 2220if args .manpath : 2221"No paths specified and I can't make sense of your MANPATH" ) 2222"Please either give paths or set $MANPATH and try again" ) 2223else : 2224"No paths specified" ) 2225sys .exit (0 ) 2226 2227if not args .stdout and not args .directory : 2228# Default to ~/.cache/fish/generated_completions 2229# Create it if it doesn't exist 2230xdg_cache_home = os .getenv ("XDG_CACHE_HOME" ,"~/.cache" ) 2231args .directory = os .path .expanduser ( 2232xdg_cache_home + "/fish/generated_completions/" 2233 ) 2234if not args .stdout : 2235try : 2236os .makedirs (args .directory ) 2237except OSError as e : 2238if e .errno != errno .EEXIST : 2239raise 2240 2241if not args .stdout and not args .keep : 2242# Remove old generated files 2243cleanup_autogenerated_completions_in_directory (args .directory ) 2244 2245parse_and_output_man_pages (args .file_paths ,args .directory ,args .progress )