Fixing bash completion for mfgtool-git’s AUR package


After a recent system update I started seeing this error when opening new shells:

    bash: /etc/bash_completion.d/uuu-complete.bash: line 3: syntax error near unexpected token `('

    bash: /etc/bash_completion.d/uuu-complete.bash: line 3: ` COMPREPLY=(uuu (Universal Update Utility) for nxp imx chips -- libuuu_1.4.243-7-gc0541c8'

    bash: /etc/bash_completion.d/uuu-complete.bash: line 5: syntax error near unexpected token `newline'

    bash: /etc/bash_completion.d/uuu-complete.bash: line 5: `uuu [-d -m -v -V] <bootloader|cmdlists|cmd>'

After reporting it on the package’s AUR page, I got an email informing me that I was now a co-maintainer. Which I took as a sign of “pull requests welcome” and had another look at the issue.

Luckily, it’s clear the issue is emanating from /etc/bash_completion.d/uuu-complete.bash. The original file is below. I’m not sure what’s supposed to be in bash_completion.d files, but I started by just trying to resolve the hanging ‘(‘ and newline errors.

_uuu_autocomplete()
{
     COMPREPLY=(uuu (Universal Update Utility) for nxp imx chips -- libuuu_1.5.4-2-gf7aaaae

uuu [-d -m -v -V] <bootloader|cmdlists|cmd>

    bootloader  download bootloader to board by usb
    cmdlist     run all commands in cmdlist file
                If it is path, search uuu.auto in dir
                If it is zip, search uuu.auto in zip
    cmd         Run one command, use -H see detail
                example: SDPS: boot -f flash.bin
    -d          Daemon mode, wait for forever.
    -v -V       verbose mode, -V enable libusb error\warning info
    -dry        Dry run mode, check if script or cmd correct 
    -m          USBPATH Only monitor these paths.
                    -m 1:2 -m 1:3

    -t          Timeout second for wait known usb device appeared
    -T          Timeout second for wait next known usb device appeared at stage switch
    -e          set environment variable key=value
    -pp         usb polling period in milliseconds
    -dm         disable small memory
uuu -s          Enter shell mode. uuu.inputlog record all input commands
                you can use "uuu uuu.inputlog" next time to run all commands

uuu -udev       linux: show udev rule to avoid sudo each time 
uuu -lsusb      List connected know devices
uuu -IgSerNum   Set windows registry to ignore USB serial number for known uuu devices
uuu -h          show general help
uuu -H          show general help and detailed help for commands

uuu [-d -m -v] -b[run] <emmc|emmc_all|fat_write|nand|qspi|sd|sd_all|spl> arg...
	Run Built-in scripts
	emmc	burn boot loader to eMMC boot partition
		arg0: _flash.bin  bootloader
		arg1: _image[Optional]  image burn to emmc, default is the same as bootloader
	emmc_all	burn whole image to eMMC
		arg0: _flash.bin  bootloader, which can extract from wic image
		arg1: _image[Optional]  wic image burn to emmc.
	fat_write	update one file in fat partition, require uboot fastboot running in board
		arg0: _image  image, which cp to fat partition
		arg1: _device  storage device, mmc\sata
		arg2: _partition  fat partition number, like 1:1
		arg3: _filename[Optional]  file name in target fat partition, only support rootdir now
	nand	burn boot loader to NAND flash
		arg0: _flash.bin  bootloader
		arg1: _image[Optional]  image burn to nand, default is the same as bootloader
	qspi	burn boot loader to qspi nor flash
		arg0: _flexspi.bin  bootloader
		arg1: _image[Optional]  image burn to flexspi, default is the same as bootloader
	sd	burn boot loader to sd card
		arg0: _flash.bin  bootloader
		arg1: _image[Optional]  image burn to emmc, default is the same as bootloader
	sd_all	burn whole image to sd card
		arg0: _flash.bin  bootloader, which can extract from wic image
		arg1: _image[Optional]  wic image burn to emmc.
	spl	boot spl and uboot
		arg0: _flash.bin 

uuu -bshow <emmc|emmc_all|fat_write|nand|qspi|sd|sd_all|spl>
	Show built-in script

Enjoy auto [tab] command complete by put below script into /etc/bash_completion.d/uuu
  _uuu_autocomplete()
  {
       COMPREPLY=($(/usr/bin/uuu $1 $2 $3))
  }
  complete -o nospace -F _uuu_autocomplete  uuu

[?25h)
}
complete -o nospace -F _uuu_autocomplete  uuu

When looking through the PKGBUILD file, I noticed that it was trying to only insert a small snippet of text into the uuu-complete.bash. From the package() function:

    install -Dm0644 /dev/stdin "${pkgdir}/etc/bash_completion.d/uuu-complete.bash" << EOF
_uuu_autocomplete()
{
     COMPREPLY=($(/usr/bin/uuu $1 $2 $3))
}
complete -o nospace -F _uuu_autocomplete  uuu
EOF

I’m not entirely sure what’s happening here, but it seems pretty clear that the text snippet being loaded into uuu-complete.bash is actually being executed instead, and that output is winding up in the file instead.

So the simple answer seems to be just to load that text snippet into a real file, and then use install to place it where it needs to be.

In the AUR package, a new file – uuu-complete.bash:

_uuu_autocomplete()
{
     COMPREPLY=($(/usr/bin/uuu $1 $2 $3))
}
complete -o nospace -F _uuu_autocomplete  uuu

And the new install line:

install -Dm0644 "${srcdir}/uuu-complete.bash" "${pkgdir}/etc/bash_completion.d/uuu-complete.bash"

That’s a wrap, the updated package builds, and no more errors!