From patchwork Mon Apr 13 01:11:50 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 25883 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id 5D9F1DE933 for ; Mon, 13 Apr 2009 11:17:52 +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 C3625DE1C8; Mon, 13 Apr 2009 11:15:55 +1000 (EST) Received: from hera.kernel.org (IDENT:U2FsdGVkX18DB2tWWAzs37s2Cj4zSqUL8YIZ2xxIUp8@localhost [127.0.0.1]) by hera.kernel.org (8.14.2/8.14.2) with ESMTP id n3D1Fmai007179 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 13 Apr 2009 01:15:48 GMT Received: (from geoff@localhost) by hera.kernel.org (8.14.2/8.13.1/Submit) id n3D1Fl0W007178; Mon, 13 Apr 2009 01:15:47 GMT Message-Id: <20090413011137.581132307@am.sony.com> References: <20090413011136.475152916@am.sony.com> In-Reply-To: <20090413011136.475152916@am.sony.com> User-Agent: quilt/0.46-1 Date: Sun, 12 Apr 2009 18:11:50 -0700 From: Geoff Levand To: Jeremy Kerr Content-Disposition: inline; filename=ui-url.diff X-Virus-Scanned: ClamAV 0.93.3/9226/Sun Apr 12 20:02:45 2009 on hera.kernel.org X-Virus-Status: Clean X-Spam-Status: No, score=-2.2 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]); Mon, 13 Apr 2009 01:15:50 +0000 (UTC) Cc: cbe-oss-dev@ozlabs.org Subject: [Cbe-oss-dev] [patch 14/24] petitboot: Add URL parsing routines 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 Create the new files url.h and url.c for parsing URL strings. The new structure struct pb_url holds the results of the parse operation. Signed-off-by: Geoff Levand --- rules.mk | 2 ui/common/url.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ui/common/url.h | 63 +++++++++++++++++++ 3 files changed, 249 insertions(+), 1 deletion(-) --- a/rules.mk +++ b/rules.mk @@ -44,7 +44,7 @@ discover_objs = discover/udev.o discove discover/device-handler.o discover/paths.o discover/parser-utils.o # client objs -ui_common_objs = ui/common/discover-client.o +ui_common_objs = ui/common/discover-client.o ui/common/url.o ncurses_objs = twin_objs = ui/twin/pb-twin.o --- /dev/null +++ b/ui/common/url.c @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2009 Sony Computer Entertainment Inc. + * Copyright 2009 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#if defined(HAVE_CONFIG_H) +#include "config.h" +#endif + +#define _GNU_SOURCE +#include +#include + +#include "log/log.h" +#include "talloc/talloc.h" +#include "url.h" + +/** + * pb_scheme_info - Helper for parsing URLs. + */ + +struct pb_scheme_info { + enum pb_url_scheme scheme; + const char *str; + unsigned int str_len; +}; + +/** + * pb_url_find_scheme - Find the pb_scheme_info for a URL string. + */ + +static const struct pb_scheme_info *pb_url_find_scheme(const char *url_str) +{ + static const struct pb_scheme_info a[] = { + { + .scheme = pb_url_file, + .str = "file://", + .str_len = sizeof("file://") - 1, + }, + { + .scheme = pb_url_ftp, + .str = "ftp://", + .str_len = sizeof("ftp://") - 1, + }, + { + .scheme = pb_url_http, + .str = "http://", + .str_len = sizeof("http://") - 1, + }, + { + .scheme = pb_url_https, + .str = "https://", + .str_len = sizeof("https://") - 1, + }, + { + .scheme = pb_url_nfs, + .str = "nfs://", + .str_len = sizeof("nfs://") - 1, + }, + { + .scheme = pb_url_sftp, + .str = "sftp://", + .str_len = sizeof("sftp://") - 1, + }, + { + .scheme = pb_url_tftp, + .str = "tftp://", + .str_len = sizeof("tftp://") - 1, + }, + }; + static const struct pb_scheme_info file_scheme = { + .str = "", + .scheme = pb_url_file, + }; + unsigned int i; + + for (i = 0; i < sizeof(a) / sizeof(a[0]); i++) + if (!strncasecmp(url_str, a[i].str, a[i].str_len)) + return &a[i]; + + /* Assume this is a non-url local file. */ + + return &file_scheme; +} + +/** + * pb_url_parse - Parse a remote file URL. + * @ctx: The talloc context to associate with the returned string. + * + * Returns a talloc'ed struct pb_url instance on success, or NULL on error. + */ + +struct pb_url *pb_url_parse(void *ctx, const char *url_str) +{ + const struct pb_scheme_info *si; + struct pb_url *url; + const char *p; + + pb_log("%s: '%s'\n", __func__, url_str); + + if (!url_str || !*url_str) { + assert(0 && "bad url"); + return NULL; + } + + url = talloc_zero(ctx, struct pb_url); + + if (!url) + return NULL; + + si = pb_url_find_scheme(url_str); + + url->scheme = si->scheme; + p = url_str + si->str_len; + + url->full = talloc_strdup(url, url_str); + + if (url->scheme == pb_url_file) { + url->port = NULL; + url->host = NULL; + url->path = talloc_strdup(url, p); + } else { + int len; + const char *col; + const char *path; + + path = strchr(p, '/'); + + if (!path) { + pb_log("%s: parse path failed '%s'\n", p); + goto fail; + } + + col = strchr(p, ':'); + + if (col) { + len = path - col - 1; + url->port = len ? talloc_strndup(url, col + 1, len) + : NULL; + len = col - p; + url->host = len ? talloc_strndup(url, p, len) : NULL; + } else { + url->port = NULL; + url->host = talloc_strndup(url, p, path - p); + } + url->path = talloc_strdup(url, path); + } + + p = strrchr(url->path, '/'); + + if (p) { + p++; + url->dir = talloc_strndup(url, url->path, p - url->path); + url->file = talloc_strdup(url, p); + } else { + url->dir = NULL; + url->file = talloc_strdup(url, url->path); + } + + pb_log(" scheme %d\n", url->scheme); + pb_log(" host '%s'\n", url->host); + pb_log(" port '%s'\n", url->port); + pb_log(" path '%s'\n", url->path); + pb_log(" dir '%s'\n", url->dir); + pb_log(" file '%s'\n", url->file); + + return url; + +fail: + talloc_free(url); + return NULL; +} --- /dev/null +++ b/ui/common/url.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2009 Sony Computer Entertainment Inc. + * Copyright 2009 Sony Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#if !defined(_PB_URL_PARSER_H) +#define _PB_URL_PARSER_H + +/** + * enum pb_url_scheme - Enumeration of the URL schemes we can handle. + */ + +enum pb_url_scheme { + pb_url_unknown = 0, + pb_url_file, + pb_url_ftp, + pb_url_http, + pb_url_https, + pb_url_nfs, + pb_url_sftp, + pb_url_tftp, +}; + +/** + * struct pb_url - Parsed URL info. + * @scheme: The enum pb_url_scheme for this URL. + * @full: The full URL = scheme://host:port/path. + * @host: The host part of URL. + * @port: The port part of URL. + * @path: The path part of URL. + * @dir: The dir part of path. + * @file: The file part of path. + * + * For info on URL syntax see: + * http://www.ietf.org/rfc/rfc3986.txt + */ + +struct pb_url { + enum pb_url_scheme scheme; + char *full; + char *host; + char *port; + char *path; + char *dir; + char *file; +}; + +struct pb_url *pb_url_parse(void *ctx, const char *url_str); + +#endif