From nobody Thu Sep 19 01:06:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=cloud.com ARC-Seal: i=1; a=rsa-sha256; t=1721896093; cv=none; d=zohomail.com; s=zohoarc; b=iaEBUV+vBhaSXkUgcZ10ukiiRWGrCUFt9vkhfS2VJKkq4gVe8At0P8mZ89aHxc6l09J1JpajgehejZFFg4zDljUC4RI5ZBX1NeZJQsprczgZvroLywcvU16C8kJoTc8DqoMtJ6dT915C+J3aX5Vw33Q9BBarEbzw7PWvV/pqu9A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721896093; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=OBaRyDnAGRnUAzYqwQI4QueAjcDEPAmUfs0Um5xu1Fw=; b=JyA+ZqzhCUPg278pLBsa9zTzG845MB96iowdb6ivihH7Dl5JInuoUtC00xG7CF2ys6g3pGUIRzlVzUNDBbrq8keL2R20ktnwnecNO47ThTR8ZPlAIu5W4MEu8Y4CRPIyuHlmZCZgu8q/RuNc6IO7pXkJKvcUPmym04Oa5Qlsdn8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 17218960939171012.5495628000259; Thu, 25 Jul 2024 01:28:13 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.764818.1175393 (Exim 4.92) (envelope-from ) id 1sWtpM-0007OJ-Cy; Thu, 25 Jul 2024 08:27:56 +0000 Received: by outflank-mailman (output) from mailman id 764818.1175393; Thu, 25 Jul 2024 08:27:56 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sWtpM-0007O8-A6; Thu, 25 Jul 2024 08:27:56 +0000 Received: by outflank-mailman (input) for mailman id 764818; Thu, 25 Jul 2024 08:27:54 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sWtpK-00070a-Pg for xen-devel@lists.xenproject.org; Thu, 25 Jul 2024 08:27:54 +0000 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [2a00:1450:4864:20::52f]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id c8da7fd4-4a5f-11ef-8776-851b0ebba9a2; Thu, 25 Jul 2024 10:27:52 +0200 (CEST) Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-5a1c49632deso784624a12.2 for ; Thu, 25 Jul 2024 01:27:52 -0700 (PDT) Received: from fhilly.. ([185.25.67.249]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590e7esm558590a12.36.2024.07.25.01.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jul 2024 01:27:51 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: c8da7fd4-4a5f-11ef-8776-851b0ebba9a2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloud.com; s=cloud; t=1721896072; x=1722500872; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OBaRyDnAGRnUAzYqwQI4QueAjcDEPAmUfs0Um5xu1Fw=; b=VA/+1NZG2Ye8x+cMODrXVvy6Unbm++2iWufSv0SZOqAQC96FnaR+YLSwub7qLFFS7k x/8cQtKO4uVEUAlyWEhVskccGzKSTRpRSD9zeSjfUT04EmJLqGhs3mxLzwSbjhZHXy7t a6ElbEJksFbsktC0YVVeRestsirLlLGsot6xA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721896072; x=1722500872; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OBaRyDnAGRnUAzYqwQI4QueAjcDEPAmUfs0Um5xu1Fw=; b=BI1RACxYHeaoM3vE6CmJFkDA+MWZOezYp+lW9Q0ffqUfW18g6wqOBALVRxyh1AHfDW LGxX+lmtE+7oIcMDfz5zMzMGq8bHnwYZuAthkrjQGXwdebTq5P3gHYZ/4Ndhs4ba+3a4 yujxffFIQFEUr5Rs1tLm+T1aU5OxuLL5phRVZMdsbyYSP7ZuEplZOh08G/Q3VThLqyr4 43vFDXbTywJfL/tQ23HwxWF7eTlVyTBDTaOMGV3gWdan4UMaXJCBj+6ubGyXcKnqB6pE 5hoh3fQi6fXRipcQIbSjQTbMm6tZtFl7fPgEmp31UXgl+txvlWHF0SqEvVllY+eOAPMg 2rBQ== X-Gm-Message-State: AOJu0YzoFQ6EZcA0dzjrCHx/GoTzA1WTzgW3nO8VEtwVe6X2uwLssLoo kJFvYAuQZtA79cDOJW9KbEA4DGwRJzHS4VP09mMQoTpDggT0EMEKiBnogG7V8aAJFcHxBQ5wAlG N X-Google-Smtp-Source: AGHT+IH7u/9vSqUG18/Wai/gBMKMoz1kLZADxXkS2EPtQ2jADup/zKwH3xjvDtCWFr24WntaeJTVng== X-Received: by 2002:a05:6402:2681:b0:586:e6e3:ea18 with SMTP id 4fb4d7f45d1cf-5acaad3c2b4mr672296a12.23.1721896071456; Thu, 25 Jul 2024 01:27:51 -0700 (PDT) From: Fouad Hilly To: xen-devel@lists.xenproject.org Cc: Fouad Hilly , Anthony PERARD Subject: [PATCH v6 2/4] x86/ucode: refactor xen-ucode to utilize getopt Date: Thu, 25 Jul 2024 09:27:23 +0100 Message-ID: <20240725082725.2685481-3-fouad.hilly@cloud.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240725082725.2685481-1-fouad.hilly@cloud.com> References: <20240725082725.2685481-1-fouad.hilly@cloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @cloud.com) X-ZM-MESSAGEID: 1721896095208116600 Content-Type: text/plain; charset="utf-8" Use getopt_long() to handle command line arguments. Introduce ext_err for common exit with errors. Introducing usage() to handle usage\help messages in a common block. show_curr_cpu is printed to stdout only. Signed-off-by: Fouad Hilly --- [v6] 1- Update usage() printed message format: [microcode file] [options] -> [mi= crocode file | options] 2- Add missing blanks in switch ( opt ) [v5] 1- Update message description. 2- re-arrange static and automatic variables. 3- Fix indentations. 4- reverted the deletion of show-cpu-info for backwards compatibility. [v4] 1- Merge three patches into one. 2- usage() to print messages to the correct stream. 3- Update commit message and description. --- tools/misc/xen-ucode.c | 52 +++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/tools/misc/xen-ucode.c b/tools/misc/xen-ucode.c index 390969db3d1c..2c9f337b86cb 100644 --- a/tools/misc/xen-ucode.c +++ b/tools/misc/xen-ucode.c @@ -11,6 +11,7 @@ #include #include #include +#include =20 static xc_interface *xch; =20 @@ -71,12 +72,29 @@ static void show_curr_cpu(FILE *f) } } =20 +static void usage(FILE *stream, const char *name) +{ + fprintf(stream, + "%s: Xen microcode updating tool\n" + "options:\n" + " -h, --help display this help\n" + " -s, --show-cpu-info show CPU information\n" + "Usage: %s [microcode file | options]\n", name, name); + show_curr_cpu(stream); +} + int main(int argc, char *argv[]) { + static const struct option options[] =3D { + {"help", no_argument, NULL, 'h'}, + {"show-cpu-info", no_argument, NULL, 's'}, + {NULL, no_argument, NULL, 0} + }; int fd, ret; char *filename, *buf; size_t len; struct stat st; + int opt; =20 xch =3D xc_interface_open(NULL, NULL, 0); if ( xch =3D=3D NULL ) @@ -86,22 +104,34 @@ int main(int argc, char *argv[]) exit(1); } =20 - if ( argc < 2 ) + while ( (opt =3D getopt_long(argc, argv, "hs", options, NULL)) !=3D -1= ) { - fprintf(stderr, - "xen-ucode: Xen microcode updating tool\n" - "Usage: %s [ | show-cpu-info]\n", argv[0]); - show_curr_cpu(stderr); - exit(2); + switch ( opt ) + { + case 'h': + usage(stdout, argv[0]); + exit(EXIT_SUCCESS); + + case 's': + show_curr_cpu(stdout); + exit(EXIT_SUCCESS); + + default: + goto ext_err; + } } =20 - if ( !strcmp(argv[1], "show-cpu-info") ) + if ( optind =3D=3D argc ) + goto ext_err; + + /* For backwards compatibility to the pre-getopt() cmdline handling */ + if ( !strcmp(argv[optind], "show-cpu-info") ) { show_curr_cpu(stdout); return 0; } =20 - filename =3D argv[1]; + filename =3D argv[optind]; fd =3D open(filename, O_RDONLY); if ( fd < 0 ) { @@ -146,4 +176,10 @@ int main(int argc, char *argv[]) close(fd); =20 return 0; + + ext_err: + fprintf(stderr, + "%s: unable to process command line arguments\n", argv[0]); + usage(stderr, argv[0]); + exit(EXIT_FAILURE); } --=20 2.42.0