Added some comments to sun_map_parse_read().
authorDaniel Ottavio <ottavio@fsl.cs.sunysb.edu>
Mon, 15 Aug 2005 01:37:33 +0000 (01:37 +0000)
committerDaniel Ottavio <ottavio@fsl.cs.sunysb.edu>
Mon, 15 Aug 2005 01:37:33 +0000 (01:37 +0000)
* amd/amd.h: Added an extern from info_file.c called
file_read_line().  Previously this function was static but is now
used in sun2amd.c.

* amd/info_file.c: Changed the static function read_line() to a
public function called file_read_line().  This function is now
used in sun2amd.c

* amd/sun2amd.8: Filled in the contents of this man page.  This is
a conversion utility that converts Sun maps to Amd maps.

* amd/sun2amd.c: Implemented a working version of this utility.

ChangeLog
amd/amd.h
amd/info_file.c
amd/sun2amd.8
amd/sun2amd.c
amd/sun_map_parse.y

index c5f5a131c2042721c3b9800a3a0744d26e176eea..a9dbd3982fb76f1d7cb20680d0e4e837b91c73d0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
        of the list of options for each entry.  The previous way was not
        working properly so now we just look for the 'fstype' keyword in
        each option.  If it exists we copy its key value.
+       Added some comments to sun_map_parse_read().
+       
+       * amd/amd.h: Added an extern from info_file.c called
+       file_read_line().  Previously this function was static but is now
+       used in sun2amd.c.
+
+       * amd/info_file.c: Changed the static function read_line() to a
+       public function called file_read_line().  This function is now
+       used in sun2amd.c
+
+       * amd/sun2amd.8: Filled in the contents of this man page.  This is
+       a conversion utility that converts Sun maps to Amd maps.
+
+       * amd/sun2amd.c: Implemented a working version of this utility.
 
 2005-08-11  Erez Zadok  <ezk@cs.sunysb.edu>
 
index 6c41f9ca002c0a86083ba4899fa77f48ccfe8dd0..790321f57e45d818ac81d5573bff58cf2c2110b0 100644 (file)
--- a/amd/amd.h
+++ b/amd/amd.h
@@ -572,6 +572,7 @@ extern int  background(void);
 extern void deslashify(char *);
 extern void do_task_notify(void);
 extern int  eval_fs_opts(am_opts *, char *, char *, char *, char *, char *);
+extern int  file_read_line(char *, int, FILE *);
 extern void forcibly_timeout_mp(am_node *);
 extern void free_map(am_node *);
 extern void free_opts(am_opts *);
index af0a5392a2703b0ba27224fdcee5da53b8e8ca37..654edf76a8026b60115857784bacfa24589361ba 100644 (file)
@@ -59,8 +59,8 @@ int file_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *));
 int file_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
 
 
-static int
-read_line(char *buf, int size, FILE *fp)
+int
+file_read_line(char *buf, int size, FILE *fp)
 {
   int done = 0;
 
@@ -106,7 +106,7 @@ file_search_or_reload(mnt_map *m,
   int chuck = 0;
   int line_no = 0;
 
-  while (read_line(key_val, sizeof(key_val), fp)) {
+  while (file_read_line(key_val, sizeof(key_val), fp)) {
     char *kp;
     char *cp;
     char *hash;
index 635258907733486ba6c3dd34485ddc26c3215618..da25f27f6aa31bfe3604f6b1dde401af3c2c864a 100644 (file)
@@ -1,5 +1,6 @@
 .\"
 .\" Copyright (c) 1997-2005 Erez Zadok
+.\" Copyright (c) 2005 Daniel P. Ottavio
 .\" Copyright (c) 1990 Jan-Simon Pendry
 .\" Copyright (c) 1990 Imperial College of Science, Technology & Medicine
 .\" Copyright (c) 1990 The Regents of the University of California.
 .\"
 .\" File: am-utils/amd/sun2amd.8
 .\"
-.TH SUN2AMD 8L "8 August 2005"
+.TH SUN2AMD 8L "14 August 2005"
+
 .SH NAME
 sun2amd \- converts Sun automount maps to Amd maps
+
+.SH SYNOPSIS
+.B sun2amd
+[-hH] [-i infile] [-o outfile]
+
+.SH DESCRIPTION
+.B sun2amd
+is used to convert Sun style automount maps to Amd style automount
+maps.  By default 
+.B sun2amd
+reads from stdin and writes to stdout.
+
+.SH OPTIONS
+.TP
+.B -h
+Help
+.TP
+.B -i
+Read Sun map information from specified file.
+.TP
+.B -o
+Write Amd map information to specified file.
+
+.SH EXAMPLE
+To convert a Sun automount file called auto_foo to an Amd file called
+auto.amd type:
+
+.B sun2amd
+-i auto_foo -o auto.amd
+
+.SH BUGS 
+* Can not convert master maps yet.
+
+* NFS is the only automount type currently supported.
+
+total_bug_count = number_found + 1;
+
+.SH "SEE ALSO"
+.BR automount(8),
+.BR amd(8)
+
+.I "Linux NFS and Automounter Administration"
+by Erez Zadok, ISBN 0-7821-2739-8, (Sybex, 2001).
+.LP
+.I http://www.am-utils.org
+.LP
+
+.SH AUTHOR
+Daniel P. Ottavio
+.I <dottavio@ic.sunysb.edu>
index 647fa9bb19c4dfe8e8b03cb93ea495f5191bd613..5fcd3b00648af47392fb51a41c46e1a775684ffb 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 1997-2005 Erez Zadok
+ * Copyright (c) 2005 Daniel P. Ottavio
  * Copyright (c) 1989 Jan-Simon Pendry
  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
  * Copyright (c) 1989 The Regents of the University of California.
 #endif /* HAVE_CONFIG_H */
 #include <am_defs.h>
 #include <amd.h>
-
-/* XXX: empty placeholder.  Fill in */
+#include <sun_map.h>
 
 
 /* dummies to make the program compile and link */
-struct amu_global_options gopt; /* may not be needed for sun2amd */
+struct amu_global_options gopt;
+
+
+/*
+ * Parse the stream sun_in, convert the map information to amd, write
+ * the results to amd_out.
+ */
+static int
+sun2amd_convert(FILE *sun_in, FILE *amd_out) 
+{  
+  char line_buff[INFO_MAX_LINE_LEN], *tmp, *key, *entry;
+  int pos, line = 0, retval = 1;
+   
+  /* just to be safe */
+  memset(line_buff, 0, sizeof(line_buff));
+  
+  /* Read the input line by line and do the conversion. */
+  while ((pos = file_read_line(line_buff, sizeof(line_buff), sun_in))) {
+    line++;
+    
+    /* Make sure we have the whole line. */
+    if (line_buff[pos - 1] != '\n') {
+      plog(XLOG_ERROR, "map line %d is too long", line);
+      goto err;
+    }
+    
+    line_buff[pos - 1] = '\0';
+    
+    /* remove comments */
+    if ((tmp = strchr(line_buff, '#')) != NULL) {
+      *tmp = '\0';
+    }
+
+    /* find start of key */
+    key = line_buff;
+    while (*key != '\0' && isspace((int)*key)) {
+      key++;
+    }
+    
+    /* ignore blank lines */
+    if (*key == '\0') {
+      continue;
+    }
+    
+    /* find the end of the key and NULL terminate */
+    tmp = key;
+    while (*tmp != '\0' && isspace((int)*tmp) == 0) {
+      tmp++;
+    }
+    if (*tmp == '\0') {
+      plog(XLOG_ERROR, "map line %d has no entry", line);
+      goto err;
+    }
+    *tmp++ = '\0';
+    if(*tmp == '\0') {
+      plog(XLOG_ERROR, "map line %d has no entry", line);
+      goto err;
+    }
+    entry = tmp;
+    
+    /* convert the sun entry to an amd entry */
+    if ((tmp = sun_entry2amd(key, entry)) == NULL) {
+      goto err;
+    }
+    
+    if (fputs(tmp, amd_out) == EOF) {
+      plog(XLOG_ERROR, "can't write amd entry on line %d: fputs: %s", line, strerror(errno));
+      goto err;
+    }
+    
+    /* just to be safe */
+    memset(line_buff, 0, sizeof(line_buff));
+  }
+
+  /* success */
+  retval = 0;
+
+ err:
+  return retval;
+} 
+
+
+/* 
+ * wrapper open function 
+ */
+static FILE *
+sun2amd_open(const char *path, const char *mode) 
+{
+  FILE *retval = NULL;
+  
+  if ((retval = fopen(path,mode)) == NULL) {
+    plog(XLOG_ERROR,"could not open file %s",path);
+  }
+  
+  return retval;
+}
+
+
+/* 
+ * echo the usage and exit 
+ */
+static void
+sun2amd_usage(void) 
+{
+  fprintf(stderr,
+         "usage : sun2amd [-hH] [-i infile] [-o outfile]\n"
+         "-h\thelp\n"
+         "-i\tspecify an infile (defaults to stdin)\n"
+         "-o\tspecify an outfile (defaults to stdout)\n"); 
+}
 
 
 int
-main()
+main(int argc, char **argv)
 {
-  exit(1);
+  /* default in/out to stdin/stdout */
+  FILE *sun_in = stdin, *amd_out = stdout;
+  int opt, retval = 1;
+  
+  while ((opt = getopt(argc, argv , "i:o:hH")) != -1) {
+    switch (opt) {
+      
+    case 'i':
+      if ((sun_in = sun2amd_open(optarg,"r")) == NULL) {
+       goto err;
+      }
+      break;
+      
+    case 'o':
+      if ((amd_out = sun2amd_open(optarg,"w")) == NULL) {
+       goto err;
+      }
+      break;
+      
+    case 'h':
+    case 'H':
+      sun2amd_usage();
+      goto err;
+    } 
+  }
+  
+  retval = sun2amd_convert(sun_in,amd_out);
+
+ err:
+  exit(retval);
 }
index 51631adacc442a4fb129b5623a8221298098a845..e2996fc9c3726d7cc7db3dbea1b93aa739e872b8 100644 (file)
@@ -411,6 +411,22 @@ option : WORD {
 
 %%
 
+/*
+ * Parse 'map_data' which is assumed to be a Sun-syle map.  If
+ * successful a sun_entry is returned.
+ *
+ * The parser is designed to parse map entries with out the keys.  For
+ * example the entry:
+ *
+ * usr -ro pluto:/usr/local 
+ *
+ * should be passed to the parser as:
+ *
+ * -ro pluto:/usr/local
+ *
+ * The reason for this is that the Amd info services already strip off
+ * the key when they read map info.
+ */
 struct sun_entry *
 sun_map_parse_read(const char *map_data)
 {