From af107a198dce152da74590f2eec5a320002c0f82 Mon Sep 17 00:00:00 2001
From: Marcus Brinkmann <marcus@gnu.org>
Date: Sun, 10 Feb 2002 17:57:49 +0000
Subject: 2002-02-10  Marcus Brinkmann  <marcus@gnu.org>

	* main.c: Include `argp.h' and `version.h'.
	(argp_program_version): New global variable.
	(tty_name, tty_type, tty_arg): Likewise.
	(parse_opt): New function.
	(term_argp): New global variable.
	(main): Call argp_parse, use new global variables to parse the
	options.  Remove TYPE variable.  Get the bootstrap port after
	checking the argument line.
	* term.h (pterm_name): Remove variable.
	(tty_arg): Declare variable.
	* devio.c (initial_open): Use tty_arg instead pterm_name.
	(devio_assert_dtr): Likewise.
---
 term/ChangeLog |  15 +++++++++
 term/devio.c   |   4 +--
 term/main.c    | 105 ++++++++++++++++++++++++++++++++++++++++++---------------
 term/term.h    |   5 ++-
 4 files changed, 97 insertions(+), 32 deletions(-)

(limited to 'term')

diff --git a/term/ChangeLog b/term/ChangeLog
index 368feefc..84431c6a 100644
--- a/term/ChangeLog
+++ b/term/ChangeLog
@@ -1,3 +1,18 @@
+2002-02-10  Marcus Brinkmann  <marcus@gnu.org>
+
+	* main.c: Include `argp.h' and `version.h'.
+	(argp_program_version): New global variable.
+	(tty_name, tty_type, tty_arg): Likewise.
+	(parse_opt): New function.
+	(term_argp): New global variable.
+	(main): Call argp_parse, use new global variables to parse the
+	options.  Remove TYPE variable.  Get the bootstrap port after
+	checking the argument line.
+	* term.h (pterm_name): Remove variable.
+	(tty_arg): Declare variable.
+	* devio.c (initial_open): Use tty_arg instead pterm_name.
+	(devio_assert_dtr): Likewise.
+
 2002-02-10  Marcus Brinkmann  <marcus@gnu.org>
 
 	* term.h: Include `hurd/hurd_types.h'.
diff --git a/term/devio.c b/term/devio.c
index 55b26336..576be5b3 100644
--- a/term/devio.c
+++ b/term/devio.c
@@ -452,7 +452,7 @@ initial_open ()
   phys_reply = ports_get_send_right (phys_reply_pi);
 
   err = device_open_request (device_master, phys_reply,
-			     D_READ|D_WRITE, pterm_name);
+			     D_READ|D_WRITE, tty_arg);
   if (err)
     {
       mach_port_deallocate (mach_task_self (), phys_reply);
@@ -496,7 +496,7 @@ devio_assert_dtr ()
     return 0;
 
   err = device_open_request (device_master, phys_reply,
-			     D_READ|D_WRITE, pterm_name);
+			     D_READ|D_WRITE, tty_arg);
 
   if (err)
     return err;
diff --git a/term/main.c b/term/main.c
index c49168c7..5e90e28e 100644
--- a/term/main.c
+++ b/term/main.c
@@ -23,17 +23,27 @@
 #include <fcntl.h>
 #include <hurd/trivfs.h>
 #include <stdio.h>
+#include <argp.h>
 #include <hurd/fsys.h>
 #include <string.h>
 
+#include <version.h>
+
+const char *argp_program_version = STANDARD_HURD_VERSION (term);
+
 int trivfs_fstype = FSTYPE_TERM;
-int trivfs_fsid = 0;		/* pid?? */
+int trivfs_fsid = 0;
 int trivfs_support_read = 1;
 int trivfs_support_write = 1;
 int trivfs_support_exec = 0;
 
 int trivfs_allow_open = O_READ|O_WRITE;
 
+/* The argument line options.  */
+char *tty_name;
+enum { T_NONE = 0, T_DEVICE, T_PTYMASTER, T_PTYSLAVE } tty_type;
+char *tty_arg;
+
 int
 demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp)
 {
@@ -47,6 +57,55 @@ demuxer (mach_msg_header_t *inp, mach_msg_header_t *outp)
 	  || device_reply_server (inp, outp));
 }
 
+
+static error_t
+parse_opt (int opt, char *arg, struct argp_state *state)
+{
+  switch (opt)
+    {
+    default:
+      return ARGP_ERR_UNKNOWN;
+    case ARGP_KEY_INIT:
+    case ARGP_KEY_SUCCESS:
+    case ARGP_KEY_ERROR:
+      break;
+    case ARGP_KEY_ARG:
+      if (!tty_name)
+	tty_name = arg;
+      else if (!tty_type)
+	{
+	  if (!strcmp (arg, "device"))
+	    tty_type = T_DEVICE;
+	  else if (!strcmp (arg, "pty-master"))
+	    tty_type = T_PTYMASTER;
+	  else if (!strcmp (arg, "pty-slave"))
+	    tty_type = T_PTYSLAVE;
+	  else
+	    argp_error (state, "Invalid terminal type");
+	}
+      else if (!tty_arg)
+	tty_arg = arg;
+      else
+	argp_error (state, "Too many arguments");
+      break;
+    case ARGP_KEY_END:
+      if (!tty_name || !tty_type || !tty_arg)
+	argp_error (state, "Too few arguments");
+      break;
+    }
+  return 0;
+}
+
+static struct argp term_argp =
+  { 0, parse_opt, "NAME TYPE ARG", "A translator that emulates a terminal.\v"\
+    "Possible values for TYPE:\n"\
+    "  device      Use Mach device ARG as bottom handler.\n"\
+    "  pty-master  Master for slave at ARG.\n"\
+    "  pty-slave   Slave for master at ARG.\n"\
+    "\n"\
+    "The filename of the node that the translator is attached to should be\n"\
+    "supplied in NAME.\n" };
+
 int
 main (int argc, char **argv)
 {
@@ -54,7 +113,6 @@ main (int argc, char **argv)
   struct port_class *peerclass, *peercntlclass;
   struct trivfs_control **ourcntl, **peercntl;
   mach_port_t bootstrap, right;
-  enum {T_DEVICE, T_PTYMASTER, T_PTYSLAVE} type;
   struct stat st;
 
   term_bucket = ports_create_bucket ();
@@ -68,17 +126,11 @@ main (int argc, char **argv)
 
   init_users ();
 
-  task_get_bootstrap_port (mach_task_self (), &bootstrap);
-  
-  if (argc != 4)
-    {
-      fprintf (stderr, "Usage: term ttyname type arg\n");
-      exit (1);
-    }
+  argp_parse (&term_argp, argc, argv, 0, 0, 0);
 
-  if (!strcmp (argv[2], "device"))
+  switch (tty_type)
     {
-      type = T_DEVICE;
+    case T_DEVICE:
       bottom = &devio_bottom;
       ourclass = tty_class;
       ourcntlclass = tty_cntl_class;
@@ -86,11 +138,9 @@ main (int argc, char **argv)
       peerclass = 0;
       peercntlclass = 0;
       peercntl = 0;
-      pterm_name = argv[3];
-    }
-  else if (!strcmp (argv[2], "pty-master"))
-    {
-      type = T_PTYMASTER;
+      break;
+
+    case T_PTYMASTER:
       bottom = &ptyio_bottom;
       ourclass = pty_class;
       ourcntlclass = pty_cntl_class;
@@ -98,10 +148,9 @@ main (int argc, char **argv)
       peerclass = tty_class;
       peercntlclass = tty_cntl_class;
       peercntl = &termctl;
-    }
-  else if (!strcmp (argv[2], "pty-slave"))
-    {
-      type = T_PTYSLAVE;
+      break;
+
+    case T_PTYSLAVE:
       bottom = &ptyio_bottom;
       ourclass = tty_class;
       ourcntlclass = tty_cntl_class;
@@ -109,14 +158,16 @@ main (int argc, char **argv)
       peerclass = pty_class;
       peercntlclass = pty_cntl_class;
       peercntl = &ptyctl;
-    }
-  else
-    {
-      fprintf (stderr, 
-	       "Allowable types are device, pty-master, and pty-slave.\n");
+      break;
+
+    default:
+      /* Should not happen.  */
+      fprintf (stderr, "Unknown terminal type is unknown.\n");
       exit (1);
     }
   
+  task_get_bootstrap_port (mach_task_self (), &bootstrap);
+  
   if (bootstrap == MACH_PORT_NULL)
     {
       fprintf (stderr, "Must be started as a translator\n");
@@ -135,12 +186,12 @@ main (int argc, char **argv)
 
   /* For ptys, the nodename depends on which half is used.  For now just use
      the hook to store the nodename.  */
-  (*ourcntl)->hook = argv[1];
+  (*ourcntl)->hook = tty_name;
 
   /* Set peer */
   if (peerclass)
     {
-      char *peer_name = argv[3];
+      char *peer_name = tty_arg;
       file_t file = file_name_lookup (peer_name, O_CREAT|O_NOTRANS, 0666);
 
       if (file != MACH_PORT_NULL)
diff --git a/term/term.h b/term/term.h
index aafc844a..694c598e 100644
--- a/term/term.h
+++ b/term/term.h
@@ -116,9 +116,6 @@ struct trivfs_control *termctl;
 /* Trivfs control structure for the pty */
 struct trivfs_control *ptyctl;
 
-/* Mach device name for this terminal */
-char *pterm_name;
-
 /* The queues we use */
 struct queue *inputq, *rawq, *outputq;
 
@@ -325,6 +322,8 @@ void rescan_inputq (void);
 void write_character (int);
 void init_users (void);
 
+extern char *tty_arg;
+
 /* kludge--these are pty versions of trivfs_S_io_* functions called by
    the real functions in users.c to do work for ptys.  */
 error_t pty_io_write (struct trivfs_protid *, char *,
-- 
cgit v1.2.3