From mboxrd@z Thu Jan 1 00:00:00 1970 List-Id: Return-Path: Received: from localhost (localhost [127.0.0.1]) by mail.ignore.pl (Postfix) with ESMTP id 255D8477A8; Fri, 15 May 2026 12:45:58 +0000 (UTC) X-Virus-Scanned: Debian amavis at ignore.pl Received: from mail.ignore.pl ([127.0.0.1]) by localhost (geidontei.ignore.pl [127.0.0.1]) (amavis, port 10024) with ESMTP id stqvfKr2mVQa; Fri, 15 May 2026 12:45:54 +0000 (UTC) Authentication-Results: mail.ignore.pl; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=fNcyfmMg; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) by mail.ignore.pl (Postfix) with ESMTPS id 0C818477C9; Fri, 15 May 2026 12:45:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1778849153; x=1810385153; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=1ghoVzKtmyF2IW1IeYYmoLgg5RhWuEh96gUM29vSEpc=; b=fNcyfmMgO/oh+5Roos+X1uU3lV+EuHmdXTDVRaYdZwGltgQKDhwMrAUK cCX12E6QxJlAJdz91O22HoEAzbVFd1//eYWgW6axjSASBYxLfjpEnYDyo Bl92Zv20pu35Wpj5GTKzcJQdBMqASU7Fb0vnZpkCq3Hscej6jjqX+Skcd dujeD4By9zpryqxoGeyPHkRZO6rbTMTx/EZS3mZenQ92/gGy4M0AiTVZj EEmbRc7qBmQyrBBMjwKBAwpdYdHHOoiv/W8/lIT1X+4d9tJZd7ttgU9ls MzUBzmXwzCcIxZQ2xd9VHaK2jssFAbXBzH2DJ1DfO7EUhc0ca0pHSF1wM Q==; X-CSE-ConnectionGUID: 5sa9ghBwTu21SgXcYlm1aw== X-CSE-MsgGUID: 4x7caUlzSFKM0A448cjhTA== X-IronPort-AV: E=McAfee;i="6800,10657,11786"; a="83416912" X-IronPort-AV: E=Sophos;i="6.23,236,1770624000"; d="scan'208";a="83416912" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 May 2026 05:45:49 -0700 X-CSE-ConnectionGUID: uMF6lczqRU+kPpGfplhYcQ== X-CSE-MsgGUID: X6IWdWimSLyvo+LO1HF3xQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,236,1770624000"; d="scan'208";a="238566217" Received: from hpe-dl385gen10.igk.intel.com ([10.91.240.117]) by orviesa008.jf.intel.com with ESMTP; 15 May 2026 05:45:48 -0700 From: Jakub Slepecki To: patches@ignore.pl Cc: please@ignore.pl, Jakub Slepecki Subject: [PATCH] what: use pl.lapp for what command Date: Fri, 15 May 2026 14:45:39 +0200 Message-ID: <20260515124539.477794-1-jakub.slepecki@intel.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Organization: Intel Technology Poland sp. z o.o. - ul. Slowackiego 173, 80-298 Gdansk - KRS 101882 - NIP 957-07-52-316 Content-Transfer-Encoding: 8bit Penlight's pl.lapp has a very nice interface for users that want to do `which what` being somewhat self-documenting. It has some minor problems, it misses some *nix or GNU traditions (e.g., lack of - with file type that's addressed here) and some quirks like useless newlines. These sound solvable upstream, but for now we need to wrap them. No functional changes are done to the command interface. All calls that were correct are intended to continue being correct. Remove io.stdin check before closing the input stream. Lua has an internal check that fails the io.close() returning error message. We can just ignore it. Signed-off-by: Jakub Slepecki --- scripts/install | 6 +++--- what.lua | 17 ++++++++--------- what/lapp.lua | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 what/lapp.lua diff --git a/scripts/install b/scripts/install index 545a82a22d37..4b5d73951926 100755 --- a/scripts/install +++ b/scripts/install @@ -26,17 +26,17 @@ fi case $ID in arch) [ -n "$buildtime" ] && buildtime=busted - [ -n "$runtime" ] && runtime= + [ -n "$runtime" ] && runtime=lua-penlight echo pacman -S $buildtime $runtime ;; debian) [ -n "$buildtime" ] && buildtime=lua-busted - [ -n "$runtime" ] && runtime= + [ -n "$runtime" ] && runtime=lua-penlight echo apt install $buildtime $runtime ;; ubuntu) [ -n "$buildtime" ] && buildtime=lua-busted - [ -n "$runtime" ] && runtime= + [ -n "$runtime" ] && runtime=lua-penlight echo apt install $buildtime $runtime ;; *) echo "ID=$ID" >&2; exit 1;; diff --git a/what.lua b/what.lua index 400a908f65ab..cb01fb034170 100755 --- a/what.lua +++ b/what.lua @@ -1,12 +1,11 @@ #!/usr/bin/env lua +local lapp = require "what.lapp" +local args = lapp[[ +Usage: what [] + + (input default -) Pathname or explicit - +]] local config = require "what.config" local translate = require "what.translate" -local input = io.stdin -local pathname = arg[1] -if pathname and pathname ~= "-" then - input = io.open(pathname) or error(string.format("%s: could not open: %s", arg[0], pathname)) -end -translate(config, input) -if input ~= io.stdin then - input:close() -end +translate(config, args.input) +io.close(args.input) diff --git a/what/lapp.lua b/what/lapp.lua new file mode 100644 index 000000000000..81bbd27a0294 --- /dev/null +++ b/what/lapp.lua @@ -0,0 +1,27 @@ +--- Extension module for Penlight's pl.lapp. +local lapp = require "pl.lapp" + + +--- Built-in file type from pl.lapp has minor limitations. For one, it does not +--- support "-" as filename as an alias for standard input. We do that here. +--- +--- User must io.close() the file themselves. +lapp.add_type( + "input", + function (name) + if not name or name == "-" then + return io.stdin + end + return io.open(name, "r") or name + end, + function (handle) + if io.type(handle) ~= "file" then + io.stderr:write( + string.format("what: could not open: %s\n", handle)) + os.exit(1) + -- lapp.error() prints useless newline + end + end) + + +return lapp -- 2.43.0