From patchwork Wed Jul 8 00:12:03 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 29572 Return-Path: X-Original-To: patchwork-incoming@bilbo.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ozlabs.org (ozlabs.org [203.10.76.45]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "mx.ozlabs.org", Issuer "CA Cert Signing Authority" (verified OK)) by bilbo.ozlabs.org (Postfix) with ESMTPS id 37C0BB7092 for ; Wed, 8 Jul 2009 10:25:24 +1000 (EST) Received: by ozlabs.org (Postfix) id 8CC60DF2FC; Wed, 8 Jul 2009 10:21:41 +1000 (EST) Delivered-To: patchwork-incoming@ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id 8B130DE1EB for ; Wed, 8 Jul 2009 10:21:41 +1000 (EST) X-Original-To: cbe-oss-dev@ozlabs.org Delivered-To: cbe-oss-dev@ozlabs.org Received: from hera.kernel.org (hera.kernel.org [140.211.167.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B5A43DDDB6; Wed, 8 Jul 2009 10:18:31 +1000 (EST) Received: from hera.kernel.org (IDENT:U2FsdGVkX1/RS9o9IQzBHsUUb7ws1JmWxX4Em8ysBA8@localhost [127.0.0.1]) by hera.kernel.org (8.14.2/8.14.2) with ESMTP id n680IQT5026843 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 8 Jul 2009 00:18:26 GMT Received: (from geoff@localhost) by hera.kernel.org (8.14.2/8.13.1/Submit) id n680IPLN026842; Wed, 8 Jul 2009 00:18:25 GMT Message-Id: <20090708001136.898562194@am.sony.com> User-Agent: quilt/0.47-1 Date: Tue, 07 Jul 2009 17:12:03 -0700 From: Geoff Levand To: Jeremy Kerr In-Reply-To: <20090708001134.934244536@am.sony.com> References: <20090708001134.934244536@am.sony.com> Content-Disposition: inline; filename=discover-options.diff X-Virus-Scanned: ClamAV 0.93.3/9541/Tue Jul 7 17:31:53 2009 on hera.kernel.org X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00, UNPARSEABLE_RELAY autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on hera.kernel.org X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.0 (hera.kernel.org [127.0.0.1]); Wed, 08 Jul 2009 00:18:27 +0000 (UTC) Cc: cbe-oss-dev@ozlabs.org Subject: [Cbe-oss-dev] [patch 29/31] petitboot: Add discover server options X-BeenThere: cbe-oss-dev@ozlabs.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: Discussion about Open Source Software for the Cell Broadband Engine List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Errors-To: cbe-oss-dev-bounces+patchwork-incoming=ozlabs.org@ozlabs.org Add --help, --log, and --version command line options to the discover server. Signed-off-by: Geoff Levand --- discover/pb-discover.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) --- a/discover/pb-discover.c +++ b/discover/pb-discover.c @@ -1,5 +1,10 @@ +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif + #include +#include #include #include @@ -11,6 +16,79 @@ #include "discover-server.h" #include "device-handler.h" +static void print_version(void) +{ + printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n"); +} + +static void print_usage(void) +{ + print_version(); + printf( +"Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n"); +} + +/** + * enum opt_value - Tri-state options variables. + */ + +enum opt_value {opt_undef = 0, opt_yes, opt_no}; + +/** + * struct opts - Values from command line options. + */ + +struct opts { + enum opt_value show_help; + const char *log_file; + enum opt_value show_version; +}; + +/** + * opts_parse - Parse the command line options. + */ + +static int opts_parse(struct opts *opts, int argc, char *argv[]) +{ + static const struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"log", required_argument, NULL, 'l'}, + {"version", no_argument, NULL, 'V'}, + { NULL, 0, NULL, 0}, + }; + static const char short_options[] = "hl:V"; + static const struct opts default_values = { + .log_file = "pb-discover.log", + }; + + *opts = default_values; + + while (1) { + int c = getopt_long(argc, argv, short_options, long_options, + NULL); + + if (c == EOF) + break; + + switch (c) { + case 'h': + opts->show_help = opt_yes; + break; + case 'l': + opts->log_file = optarg; + break; + case 'V': + opts->show_version = opt_yes; + break; + default: + opts->show_help = opt_yes; + return -1; + } + } + + return optind != argc; +} + static int running; static void sigint_handler(int __attribute__((unused)) signum) @@ -18,15 +96,31 @@ static void sigint_handler(int __attribu running = 0; } -int main(void) +int main(int argc, char *argv[]) { struct device_handler *handler; struct discover_server *server; + struct opts opts; struct udev *udev; struct user_event *uev; FILE *log; - log = fopen("pb-discover.log", "a"); + if (opts_parse(&opts, argc, argv)) { + print_usage(); + return EXIT_FAILURE; + } + + if (opts.show_help == opt_yes) { + print_usage(); + return EXIT_SUCCESS; + } + + if (opts.show_version == opt_yes) { + print_version(); + return EXIT_SUCCESS; + } + + log = fopen(opts.log_file, "a"); assert(log); pb_log_set_stream(log);