mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/1] filetype: add command support
@ 2012-08-13 20:03 Jean-Christophe PLAGNIOL-VILLARD
  2012-08-13 21:53 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2012-08-13 20:03 UTC (permalink / raw)
  To: barebox

this will allow to detect the filetype of a file and export it as filetype

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/Kconfig    |    5 +++++
 commands/Makefile   |    1 +
 commands/filetype.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+)
 create mode 100644 commands/filetype.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 92a8152..e9fb9f6 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -220,6 +220,11 @@ config CMD_DIRNAME
 	  Strip last component of file name and store the result in a
 	  environment variable
 
+config CMD_FILETYPE
+	tristate
+	depends on FILETYPE
+	prompt "filetype"
+
 endmenu
 
 menu "console                       "
diff --git a/commands/Makefile b/commands/Makefile
index e9157bf..083782f 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -72,3 +72,4 @@ obj-$(CONFIG_CMD_AUTOMOUNT)	+= automount.o
 obj-$(CONFIG_CMD_GLOBAL)	+= global.o
 obj-$(CONFIG_CMD_BASENAME)	+= basename.o
 obj-$(CONFIG_CMD_DIRNAME)	+= dirname.o
+obj-$(CONFIG_CMD_FILETYPE)	+= filetype.o
diff --git a/commands/filetype.c b/commands/filetype.c
new file mode 100644
index 0000000..eba16bb
--- /dev/null
+++ b/commands/filetype.c
@@ -0,0 +1,60 @@
+/*
+ * (C) Copyright 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
+ *
+ * Under GPLv2 Only
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <filetype.h>
+#include <environment.h>
+#include <magicvar.h>
+#include <getopt.h>
+
+static int do_filetype(int argc, char *argv[])
+{
+	int opt;
+	enum filetype type;
+	char* filename = NULL;
+	int verbose = 0;
+
+	while ((opt = getopt(argc, argv, "vf:")) > 0) {
+		switch (opt) {
+		case 'f':
+			filename = optarg;
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		}
+	}
+
+	if (!filename)
+		return COMMAND_ERROR_USAGE;
+
+	type = file_name_detect_type(filename);
+
+	if (verbose)
+		pr_info("%s: %s detected\n", filename,
+			file_type_to_string(type));
+
+	export_env_ull("filetype" , type);
+
+	return 0;
+}
+
+BAREBOX_CMD_HELP_START(filetype)
+BAREBOX_CMD_HELP_USAGE("filetype -f file [-v]\n")
+BAREBOX_CMD_HELP_USAGE("export filetype\n")
+BAREBOX_CMD_HELP_USAGE("-v     verbose\n")
+BAREBOX_CMD_HELP_SHORT("detect file type\n")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(filetype)
+	.cmd		= do_filetype,
+	.usage		= "detect file type",
+	BAREBOX_CMD_HELP(cmd_filetype_help)
+BAREBOX_CMD_END
+
+BAREBOX_MAGICVAR(filetype, "Detected filetype id");
-- 
1.7.10.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/1] filetype: add command support
  2012-08-13 20:03 [PATCH 1/1] filetype: add command support Jean-Christophe PLAGNIOL-VILLARD
@ 2012-08-13 21:53 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2012-08-13 21:53 UTC (permalink / raw)
  To: Jean-Christophe PLAGNIOL-VILLARD; +Cc: barebox

On Mon, Aug 13, 2012 at 10:03:03PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> this will allow to detect the filetype of a file and export it as filetype
> 
> Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
> ---
>  commands/Kconfig    |    5 +++++
>  commands/Makefile   |    1 +
>  commands/filetype.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 66 insertions(+)
>  create mode 100644 commands/filetype.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index 92a8152..e9fb9f6 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -220,6 +220,11 @@ config CMD_DIRNAME
>  	  Strip last component of file name and store the result in a
>  	  environment variable
>  
> +config CMD_FILETYPE
> +	tristate
> +	depends on FILETYPE
> +	prompt "filetype"

How about a short description of the command here?

> +
> +static int do_filetype(int argc, char *argv[])
> +{
> +	int opt;
> +	enum filetype type;
> +	char* filename = NULL;
> +	int verbose = 0;
> +
> +	while ((opt = getopt(argc, argv, "vf:")) > 0) {
> +		switch (opt) {
> +		case 'f':
> +			filename = optarg;
> +			break;
> +		case 'v':
> +			verbose = 1;
> +			break;
> +		}
> +	}
> +
> +	if (!filename)
> +		return COMMAND_ERROR_USAGE;
> +
> +	type = file_name_detect_type(filename);
> +
> +	if (verbose)
> +		pr_info("%s: %s detected\n", filename,
> +			file_type_to_string(type));
> +
> +	export_env_ull("filetype" , type);

Normally we do this by passing the variable name to the command, see
readline, basename or getopt. Lets do it the same way here.

Also, we should export strings instead of numbers. The numbers are not
documented anywhere and I think we should not expose them.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-08-13 21:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-13 20:03 [PATCH 1/1] filetype: add command support Jean-Christophe PLAGNIOL-VILLARD
2012-08-13 21:53 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox