From: Antony Pavlov <antonynpavlov@gmail.com>
To: Christoph Fritz <chf.fritz@googlemail.com>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH v2] commands: add 'findstr' to search file for string
Date: Wed, 21 May 2014 13:22:28 +0400 [thread overview]
Message-ID: <20140521132228.aeeab4590eb6731f0654c785@gmail.com> (raw)
In-Reply-To: <1400659362.4772.13.camel@mars>
On Wed, 21 May 2014 10:02:42 +0200
Christoph Fritz <chf.fritz@googlemail.com> wrote:
> Command 'findstr' can be for example used to find string
We already have some commands with string.h style names:
memcmp, memcpy, memset
Can we use 'strstr' name for your new command?
Another proposition:
can we add <offset from file start> and <no more N characters to search> parameters?
Using these parameters you can speed up your search and use your command with /dev/mem.
> "MAC=1C:BA:8C:F3:82:BB" in file /dev/eeprom0 to set an
> appropriate environment variable:
>
> $ findstr -o 16 -l 17 -t eth0.ethaddr pcm051_eth0_MAC= /dev/eeprom0
>
> Usage: findstr [OPTIONS] <STRING> <FILE>
> Find ASCII string in file and print it
> -o <offset> set offset of string which gets printed
> -l <length> set length of string which gets printed
> -t <var> print into variable instead of stdio
>
> Signed-off-by: Christoph Fritz <chf.fritz@googlemail.com>
> ---
> Changes in v2:
> - fix Kconfig: 'default n' is default
> - add default branch to switch
> - fix search string read-buffer overlap
> ---
> commands/Kconfig | 6 +++
> commands/Makefile | 1 +
> commands/findstr.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 147 insertions(+)
> create mode 100644 commands/findstr.c
>
> diff --git a/commands/Kconfig b/commands/Kconfig
> index 3ef8860..a795da9 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -149,6 +149,12 @@ config CMD_GLOBAL
> help
> The global command allows to create global variables
>
> +config CMD_FINDSTR
> + tristate
> + prompt "findstr"
> + help
> + Find ASCII string in file.
> +
> endmenu
>
> menu "file commands"
> diff --git a/commands/Makefile b/commands/Makefile
> index f927d21..580b137 100644
> --- a/commands/Makefile
> +++ b/commands/Makefile
> @@ -97,3 +97,4 @@ obj-$(CONFIG_CMD_READF) += readf.o
> obj-$(CONFIG_CMD_MENUTREE) += menutree.o
> obj-$(CONFIG_CMD_2048) += 2048.o
> obj-$(CONFIG_CMD_REGULATOR) += regulator.o
> +obj-$(CONFIG_CMD_FINDSTR) += findstr.o
> diff --git a/commands/findstr.c b/commands/findstr.c
> new file mode 100644
> index 0000000..934c300
> --- /dev/null
> +++ b/commands/findstr.c
> @@ -0,0 +1,140 @@
> +/*
> + * Copyright (c) 2014 Christoph Fritz <chf.fritz@googlemail.com>
> + *
> + * See file CREDITS for list of people who contributed to this
> + * project.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2
> + * as published by the Free Software Foundation.
> + *
> + * 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.
> + *
> + */
> +
> +/**
> + * @file
> + * @brief Find string in file
> + */
> +
> +#include <common.h>
> +#include <command.h>
> +#include <fs.h>
> +#include <fcntl.h>
> +#include <errno.h>
> +#include <getopt.h>
> +#include <environment.h>
> +
> +#define FINDSTR_BUF_SIZE RW_BUF_SIZE
> +
> +extern char *mem_rw_buf;
> +
> +/**
> + * @param[in] argc Argument count from command line
> + * @param[in] argv List of input arguments
> + */
> +static int do_findstr(int argc, char *argv[])
> +{
> + int opt, fd, r, ret = 1;
> + loff_t len = 0, offset = 0, idx = 0;
> + u_char *s, *t = NULL, *v = (u_char *)mem_rw_buf;
> + unsigned int v_idx, s_idx = 0;
> +
> + if (argc < 2)
> + return COMMAND_ERROR_USAGE;
> +
> + while ((opt = getopt(argc, argv, "o:l:t:")) > 0) {
> + switch (opt) {
> + case 'o':
> + offset = simple_strtoul(optarg, NULL, 0);
> + break;
> + case 'l':
> + len = simple_strtoul(optarg, NULL, 0);
> + break;
> + case 't':
> + t = optarg;
> + break;
> + default:
> + return COMMAND_ERROR_USAGE;
> + }
> + }
> +
> + if (argc - optind != 2)
> + return COMMAND_ERROR_USAGE;
> +
> + s = argv[optind];
> +
> + fd = open(argv[optind+1], O_RDONLY);
> + if (fd < 0)
> + return COMMAND_ERROR_USAGE;
> +
> + while (1) {
> + r = read(fd, mem_rw_buf, FINDSTR_BUF_SIZE);
> + if (r < 0) {
> + ret = -EIO;
> + goto out;
> + }
> + if (!r)
> + break;
> +
> + v_idx = 0;
> +
> + while (r) {
> + if (v[v_idx] == s[s_idx])
> + s_idx++;
> + else
> + s_idx = 0;
> +
> + idx++;
> + v_idx++;
> +
> + if (s_idx == strlen(s)) { /* found */
> + loff_t hit = idx - strlen(s);
> +
> + if (lseek(fd, hit + offset, SEEK_SET) < 0) {
> + ret = -EIO;
> + goto out;
> + }
> +
> + if (!len)
> + len = strlen(s);
> + r = read(fd, mem_rw_buf, len);
> + if (r != len) {
> + ret = -EIO;
> + goto out;
> + }
> +
> + v[r] = '\0';
> +
> + if (t)
> + setenv(t, v);
> + else
> + printf("%s\n", v);
> +
> + ret = 0;
> + goto out;
> + }
> + r--;
> + }
> + }
> +out:
> + close(fd);
> + return ret;
> +}
> +
> +BAREBOX_CMD_HELP_START(findstr)
> +BAREBOX_CMD_HELP_USAGE("findstr [OPTIONS] <STRING> <FILE>\n")
> +BAREBOX_CMD_HELP_SHORT("Find ASCII string in file and print it\n")
> +BAREBOX_CMD_HELP_OPT("-o <offset>", "set offset of string which gets printed\n")
> +BAREBOX_CMD_HELP_OPT("-l <length>", "set length of string which gets printed\n")
> +BAREBOX_CMD_HELP_OPT("-t <var>", "print into variable instead of stdio\n")
> +BAREBOX_CMD_HELP_END
> +
> +BAREBOX_CMD_START(findstr)
> + .cmd = do_findstr,
> + .usage = "findstr [OPTIONS] <STRING> <FILE>",
> + BAREBOX_CMD_HELP(cmd_findstr_help)
> +BAREBOX_CMD_END
> --
> 1.7.10.4
>
>
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
--
--
Best regards,
Antony Pavlov
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2014-05-21 9:12 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-20 17:27 [PATCH] commands: add 'findstr' to get string from file Christoph Fritz
2014-05-20 18:08 ` Alexander Aring
2014-05-20 20:29 ` Christoph Fritz
2014-05-21 3:44 ` Alexander Aring
2014-05-21 8:02 ` [PATCH v2] commands: add 'findstr' to search file for string Christoph Fritz
2014-05-21 9:22 ` Antony Pavlov [this message]
2014-05-21 9:49 ` Christoph Fritz
2014-05-21 13:51 ` [PATCH] commands: add 'findstr' to get string from file Sascha Hauer
2014-05-22 9:20 ` Christoph Fritz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140521132228.aeeab4590eb6731f0654c785@gmail.com \
--to=antonynpavlov@gmail.com \
--cc=barebox@lists.infradead.org \
--cc=chf.fritz@googlemail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox