devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] dtc: simplify command line invocation
@ 2015-06-30 23:31 Andre Przywara
       [not found] ` <1435707088-12800-1-git-send-email-osp-s5aTT1PPrfyzQB+pC5nmwQ@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Andre Przywara @ 2015-06-30 23:31 UTC (permalink / raw)
  To: David Gibson, Jon Loeliger; +Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA

Hi,

as promised the revised version of my mini-series.
Compared to the first drop I do not bother anymore to do file name
based guessing in case of an error opening or accessing the input
file. Also the .dts heuristic (first character is a '/') is now gone.
I omitted the third patch ("allow a second parameter to be the output
file name") entirely.

Hope that fits better, if not, let me know!

Cheers,
Andre.

-------------

Most of time I find myself converting .dts to .dtb files and vice
versa, but having to explicitly specify -I and -O every time seems to
be unnecessary. The file names usually have proper extensions, also
a DTB file is easily recognized by its magic.
This series aims to make life easier, by trying to auto-detect the
file types if no explicit -I or -O parameters have been given.
The behaviour with explicit file types specified via these parameters
is not changed.
For details see the respective commit messages.

Andre Przywara (2):
  guess input file format based on file content or file name
  guess output file format

 dtc.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)

-- 
1.8.4

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

* [PATCH v2 1/2] guess input file format based on file content or file name
       [not found] ` <1435707088-12800-1-git-send-email-osp-s5aTT1PPrfyzQB+pC5nmwQ@public.gmane.org>
@ 2015-06-30 23:31   ` Andre Przywara
  2015-06-30 23:31   ` [PATCH v2 2/2] guess output file format Andre Przywara
  2015-07-01  3:35   ` [PATCH v2 0/2] dtc: simplify command line invocation David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: Andre Przywara @ 2015-06-30 23:31 UTC (permalink / raw)
  To: David Gibson, Jon Loeliger; +Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA

Always needing to specify the input file format can be quite
annoying, especially since a dtb is easily detected by its magic.
Looking at the file name extension sounds useful as a hint, too.

Add heuristic file type guessing of the input file format in case
none has been specified on the command line.
The heuristics are as follows (in that order):
- Any issues with opening the file drop back to the current default
behaviour.
- A directory will be treated as the /proc/device-tree type.
- If the first 4 bytes are the DTB magic, assume "dtb".
- If no other test succeeded so far, use a file name based
guessing method: if the filename ends with .dts or .DTS, device tree
source text is assumed, .dtb or .DTB hint at a device tree blob.

For the majority of practical use cases this gets rid of the tedious
-I specification on the command line and simplifies actual typing of
dtc command lines.
Any explicit specification of the input type by using -I still avoids
any guessing, which resembles the current behaviour.

Signed-off-by: Andre Przywara <osp-s5aTT1PPrfyzQB+pC5nmwQ@public.gmane.org>
---
 dtc.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/dtc.c b/dtc.c
index 8c4add6..53793bf 100644
--- a/dtc.c
+++ b/dtc.c
@@ -18,6 +18,8 @@
  *                                                                   USA
  */
 
+#include <sys/stat.h>
+
 #include "dtc.h"
 #include "srcpos.h"
 
@@ -104,10 +106,55 @@ static const char * const usage_opts_help[] = {
 	NULL,
 };
 
+static const char *guess_type_by_name(const char *fname, const char *fallback)
+{
+	const char *s;
+
+	s = strrchr(fname, '.');
+	if (s == NULL)
+		return fallback;
+	if (!strcasecmp(s, ".dts"))
+		return "dts";
+	if (!strcasecmp(s, ".dtb"))
+		return "dtb";
+	return fallback;
+}
+
+static const char *guess_input_format(const char *fname, const char *fallback)
+{
+	struct stat statbuf;
+	uint32_t magic;
+	FILE *f;
+
+	if (stat(fname, &statbuf) != 0)
+		return fallback;
+
+	if (S_ISDIR(statbuf.st_mode))
+		return "fs";
+
+	if (!S_ISREG(statbuf.st_mode))
+		return fallback;
+
+	f = fopen(fname, "r");
+	if (f == NULL)
+		return fallback;
+	if (fread(&magic, 4, 1, f) != 1) {
+		fclose(f);
+		return fallback;
+	}
+	fclose(f);
+
+	magic = fdt32_to_cpu(magic);
+	if (magic == FDT_MAGIC)
+		return "dtb";
+
+	return guess_type_by_name(fname, fallback);
+}
+
 int main(int argc, char *argv[])
 {
 	struct boot_info *bi;
-	const char *inform = "dts";
+	const char *inform = NULL;
 	const char *outform = "dts";
 	const char *outname = "-";
 	const char *depname = NULL;
@@ -213,6 +260,8 @@ int main(int argc, char *argv[])
 		fprintf(depfile, "%s:", outname);
 	}
 
+	if (inform == NULL)
+		inform = guess_input_format(arg, "dts");
 	if (streq(inform, "dts"))
 		bi = dt_from_source(arg);
 	else if (streq(inform, "fs"))
-- 
1.8.4

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

* [PATCH v2 2/2] guess output file format
       [not found] ` <1435707088-12800-1-git-send-email-osp-s5aTT1PPrfyzQB+pC5nmwQ@public.gmane.org>
  2015-06-30 23:31   ` [PATCH v2 1/2] guess input file format based on file content or file name Andre Przywara
@ 2015-06-30 23:31   ` Andre Przywara
  2015-07-01  3:35   ` [PATCH v2 0/2] dtc: simplify command line invocation David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: Andre Przywara @ 2015-06-30 23:31 UTC (permalink / raw)
  To: David Gibson, Jon Loeliger; +Cc: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA

If no output file type is specified via the -O parameter, guess the
desired file type by looking at the file name extension.
If that provides no useful hints, assume "dtb" as long as the input
type is "dts". Any other input type will lead to "dts" being used as
the guessed output type.
Any explicit specification of the output type will skip this guessing.

Signed-off-by: Andre Przywara <osp-s5aTT1PPrfyzQB+pC5nmwQ@public.gmane.org>
---
 dtc.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/dtc.c b/dtc.c
index 53793bf..5fa23c4 100644
--- a/dtc.c
+++ b/dtc.c
@@ -155,7 +155,7 @@ int main(int argc, char *argv[])
 {
 	struct boot_info *bi;
 	const char *inform = NULL;
-	const char *outform = "dts";
+	const char *outform = NULL;
 	const char *outname = "-";
 	const char *depname = NULL;
 	bool force = false, sort = false;
@@ -262,6 +262,15 @@ int main(int argc, char *argv[])
 
 	if (inform == NULL)
 		inform = guess_input_format(arg, "dts");
+	if (outform == NULL) {
+		outform = guess_type_by_name(outname, NULL);
+		if (outform == NULL) {
+			if (streq(inform, "dts"))
+				outform = "dtb";
+			else
+				outform = "dts";
+		}
+	}
 	if (streq(inform, "dts"))
 		bi = dt_from_source(arg);
 	else if (streq(inform, "fs"))
-- 
1.8.4

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

* Re: [PATCH v2 0/2] dtc: simplify command line invocation
       [not found] ` <1435707088-12800-1-git-send-email-osp-s5aTT1PPrfyzQB+pC5nmwQ@public.gmane.org>
  2015-06-30 23:31   ` [PATCH v2 1/2] guess input file format based on file content or file name Andre Przywara
  2015-06-30 23:31   ` [PATCH v2 2/2] guess output file format Andre Przywara
@ 2015-07-01  3:35   ` David Gibson
  2 siblings, 0 replies; 4+ messages in thread
From: David Gibson @ 2015-07-01  3:35 UTC (permalink / raw)
  To: Andre Przywara; +Cc: Jon Loeliger, devicetree-compiler-u79uwXL29TY76Z2rM5mHXA

[-- Attachment #1: Type: text/plain, Size: 707 bytes --]

On Wed, Jul 01, 2015 at 12:31:26AM +0100, Andre Przywara wrote:
> Hi,
> 
> as promised the revised version of my mini-series.
> Compared to the first drop I do not bother anymore to do file name
> based guessing in case of an error opening or accessing the input
> file. Also the .dts heuristic (first character is a '/') is now gone.
> I omitted the third patch ("allow a second parameter to be the output
> file name") entirely.
> 
> Hope that fits better, if not, let me know!

Applied, thanks.

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

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

end of thread, other threads:[~2015-07-01  3:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-30 23:31 [PATCH v2 0/2] dtc: simplify command line invocation Andre Przywara
     [not found] ` <1435707088-12800-1-git-send-email-osp-s5aTT1PPrfyzQB+pC5nmwQ@public.gmane.org>
2015-06-30 23:31   ` [PATCH v2 1/2] guess input file format based on file content or file name Andre Przywara
2015-06-30 23:31   ` [PATCH v2 2/2] guess output file format Andre Przywara
2015-07-01  3:35   ` [PATCH v2 0/2] dtc: simplify command line invocation David Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).