]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - builtins/mapfile.def
Imported from ../bash-4.0.tar.gz.
[thirdparty/bash.git] / builtins / mapfile.def
index 32742ee1f31383fe782ae7072d32116d37716e29..e37cd22701d18bc3790487d2d82ccf560ce0943a 100644 (file)
@@ -24,7 +24,7 @@ $PRODUCES mapfile.c
 $BUILTIN mapfile
 $FUNCTION mapfile_builtin
 $SHORT_DOC mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
-Read lines from a file into an array variable.
+Read lines from the standard input into an array variable.
 
 Read lines from the standard input into the array variable ARRAY, or from
 file descriptor FD if the -u option is supplied.  The variable MAPFILE is
@@ -42,7 +42,9 @@ Options:
 Arguments:
   ARRAY                Array variable name to use for file data.
 
-If -C is supplied without -c, the default quantum is 5000.
+If -C is supplied without -c, the default quantum is 5000.  When
+CALLBACK is evaluated, it is supplied the index of the next array
+element to be assigned as an additional argument.
 
 If not supplied with an explicit origin, mapfile will clear ARRAY before
 assigning to it.
@@ -51,6 +53,14 @@ Exit Status:
 Returns success unless an invalid option is given or ARRAY is readonly.
 $END
 
+$BUILTIN readarray
+$FUNCTION mapfile_builtin
+$SHORT_DOC readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
+Read lines from a file into an array variable.
+
+A synonym for `mapfile'.
+$END
+
 #include <config.h>
 
 #include "builtins.h"
@@ -70,7 +80,6 @@ $END
 #include "common.h"
 #include "bashgetopt.h"
 
-
 #if !defined (errno)
 extern int errno;
 #endif
@@ -93,6 +102,7 @@ run_callback(callback, current_index)
 {
   unsigned int execlen;
   char  *execstr;
+  int flags;
 
   execlen = strlen (callback) + 10;
   /* 1 for space between %s and %d,
@@ -100,8 +110,13 @@ run_callback(callback, current_index)
   execlen += 2;
   execstr = xmalloc (execlen);
 
+  flags = 0;
+#if 0
+  if (interactive)
+    flags |= SEVAL_NOHIST|SEVAL_INTERACT;
+#endif
   snprintf (execstr, execlen, "%s %d", callback, current_index);
-  return parse_and_execute(execstr, NULL, 0);
+  return parse_and_execute(execstr, NULL, flags);
 }
 
 static void
@@ -136,8 +151,13 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n
      here allows us to call bind_array_element instead of bind_array_variable
      and skip the variable lookup on every call. */
   entry = find_or_make_array_variable (array_name, 1);
-  if (entry == 0)
-    return (EXECUTION_FAILURE);
+  if (entry == 0 || readonly_p (entry) || noassign_p (entry))
+    {
+      if (readonly_p (entry))
+       err_readonly (array_name);
+       
+      return (EXECUTION_FAILURE);
+    }
   if (flags & MAPF_CLEARARRAY)
     array_flush (array_cell (entry));
 
@@ -147,19 +167,24 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n
   unbuffered_read = 1;
 #endif
 
+  zreset ();
+
   /* Skip any lines at beginning of file? */
   for (line_count = 0; line_count < nskip; line_count++)
-    zgetline(fd, &line, &line_length, unbuffered_read);
+    if (zgetline (fd, &line, &line_length, unbuffered_read) < 0)
+      break;
+
   line = 0;
   line_length = 0;    
 
   /* Reset the buffer for bash own stream */
-  for (array_index = origin, line_count = 0; 
-       zgetline(fd, &line, &line_length, unbuffered_read) != -1;
+  interrupt_immediately++;
+  for (array_index = origin, line_count = 1; 
+       zgetline (fd, &line, &line_length, unbuffered_read) != -1;
        array_index++, line_count++) 
     {
       /* Have we exceeded # of lines to store? */
-      if (line_count_goal != 0 && line_count >= line_count_goal) 
+      if (line_count_goal != 0 && line_count > line_count_goal) 
        break;
 
       /* Remove trailing newlines? */
@@ -184,6 +209,7 @@ mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_n
   if (unbuffered_read == 0)
     zsyncfd (fd);
 
+  interrupt_immediately--;
   return EXECUTION_SUCCESS;
 }