$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
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.
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"
#include "common.h"
#include "bashgetopt.h"
-
#if !defined (errno)
extern int errno;
#endif
{
unsigned int execlen;
char *execstr;
+ int flags;
execlen = strlen (callback) + 10;
/* 1 for space between %s and %d,
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
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));
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? */
if (unbuffered_read == 0)
zsyncfd (fd);
+ interrupt_immediately--;
return EXECUTION_SUCCESS;
}