From 32ec2af9c8855d9a298bdae22fe026ce2e1f7ec5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Sat, 26 Oct 2002 23:27:29 +0200
Subject: [PATCH] (sexp_put_length): Bugfix, don't output any leading zero.
 (main): Implemented -s option.

Rev: src/nettle/examples/sexp-conv.c:1.8
---
 examples/sexp-conv.c | 53 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 4 deletions(-)

diff --git a/examples/sexp-conv.c b/examples/sexp-conv.c
index 21d120c2..2acde89c 100644
--- a/examples/sexp-conv.c
+++ b/examples/sexp-conv.c
@@ -13,6 +13,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+/* For getopt */
+#include <unistd.h>
+
 void
 die(const char *format, ...)
 #if __GNUC___
@@ -160,6 +163,9 @@ sexp_get_char(struct sexp_input *input, uint8_t *out)
     case SEXP_HEX:
       /* Not yet implemented */
       abort();
+
+    default:
+      abort();
     }
 }
 
@@ -556,8 +562,13 @@ sexp_put_length(struct sexp_output *output, unsigned indent,
 {
   unsigned digit = 1;
 
-  while (digit < length)
-    digit *= 10;
+  for (;;)
+    {
+      unsigned next = digit * 10;
+      if (next > length)
+	break;
+      digit = next;
+    }
 
   for (; digit; length %= digit, digit /= 10)
     sexp_put_char(output, indent, '0' + length / digit);
@@ -857,14 +868,48 @@ sexp_convert_item(struct sexp_input *input, enum sexp_mode mode,
   return 1;
 }
 
+static int
+match_argument(const char *given, const char *name)
+{
+}
+
 int
 main(int argc, char **argv)
-{
+{  
   struct sexp_input input;
   struct sexp_output output;
+  enum sexp_mode mode = SEXP_ADVANCED;
+  unsigned width;
+  
+  int c;
+  while ( (c = getopt(argc, argv, "s:w:")) != -1)
+    switch (c)
+      {
+      case 's':
+	/* FIXME: Allow abbreviations */
+	if (!strcmp(optarg, "advanced"))
+	  mode = SEXP_ADVANCED;
+	else if (!strcmp(optarg, "transport"))
+	  mode = SEXP_TRANSPORT;
+	else if (!strcmp(optarg, "canonical"))
+	  mode = SEXP_CANONICAL;
+	else
+	  die("Available syntax variants: advanced, transport, canonical\n");
+	break;
+
+      case 'w':
+	die("Option -w not yet implemented.\n");
+	
+      case '?':
+	printf("Usage: sexp-conv [-m syntax]\n"
+	       "Available syntax variants: advanced, transport, canonical\n");
+	return EXIT_FAILURE;
 
+      default: abort();
+      }
+  
   sexp_input_init(&input, stdin);
-  sexp_output_init(&output, stdout, SEXP_ADVANCED);
+  sexp_output_init(&output, stdout, mode);
 
   sexp_convert_file(&input, &output);
 
-- 
GitLab