aboutsummaryrefslogtreecommitdiffstats
path: root/builtins/mapfile.c
blob: 8b79c3741c2361823e893e1ab3ed8826f37ccd20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* mapfile.c, created from mapfile.def. */
#line 23 "./mapfile.def"

#line 56 "./mapfile.def"

#line 64 "./mapfile.def"

#include <config.h>

#include "builtins.h"
#include "posixstat.h"

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "bashansi.h"
#include "bashintl.h"

#include <stdio.h>
#include <errno.h>

#include "../bashintl.h"
#include "../shell.h"
#include "common.h"
#include "bashgetopt.h"

#if !defined (errno)
extern int errno;
#endif

#if defined (ARRAY_VARS)

#define DEFAULT_ARRAY_NAME	"MAPFILE"

/* The value specifying how frequently `mapfile'  calls the callback. */
#define DEFAULT_QUANTUM 5000

/* Values for FLAGS */
#define MAPF_CLEARARRAY	0x01
#define MAPF_CHOP	0x02

static int
run_callback(callback, current_index)
     const char *callback;
     unsigned int current_index;
{
  unsigned int execlen;
  char  *execstr;
  int flags;

  execlen = strlen (callback) + 10;
  /* 1 for space between %s and %d,
     another 1 for the last nul char for C string. */
  execlen += 2;
  execstr = xmalloc (execlen);

  flags = SEVAL_NOHIST;
#if 0
  if (interactive)
    flags |= SEVAL_INTERACT;
#endif
  snprintf (execstr, execlen, "%s %d", callback, current_index);
  return parse_and_execute(execstr, NULL, flags);
}

static void
do_chop(line)
     char * line;
{
  int length;

  length = strlen (line);
  if (length && line[length-1] == '\n') 
    line[length-1] = '\0';
}

static int
mapfile (fd, line_count_goal, origin, nskip, callback_quantum, callback, array_name, flags)
     int fd;
     long line_count_goal, origin, nskip, callback_quantum;
     char *callback, *array_name;
     int flags;
{
  char *line;
  size_t line_length;
  unsigned int array_index, line_count;
  SHELL_VAR *entry;
  int unbuffered_read;
  
  line = NULL;
  line_length = 0;
  unbuffered_read = 0;

  /* The following check should be done before reading any lines.  Doing it
     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 || readonly_p (entry) || noassign_p (entry))
    {
      if (entry && readonly_p (entry))
	err_readonly (array_name);
	
      return (EXECUTION_FAILURE);
    }
  else if (array_p (entry) == 0)
    {
      builtin_error (_("%s: not an indexed array"), array_name);
      return (EXECUTION_FAILURE);
    }
      
  if (flags & MAPF_CLEARARRAY)
    array_flush (array_cell (entry));

#ifndef __CYGWIN__
  unbuffered_read = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
#else
  unbuffered_read = 1;
#endif

  zreset ();

  /* Skip any lines at beginning of file? */
  for (line_count = 0; line_count < nskip; line_count++)
    if (zgetline (fd, &line, &line_length, unbuffered_read) < 0)
      break;

  line = 0;
  line_length = 0;    

  /* Reset the buffer for bash own stream */
  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) 
	break;

      /* Remove trailing newlines? */
      if (flags & MAPF_CHOP)
	do_chop (line);
	  
      /* Has a callback been registered and if so is it time to call it? */
      if (callback && line_count && (line_count % callback_quantum) == 0) 
	{
	  run_callback (callback, array_index);

	  /* Reset the buffer for bash own stream. */
	  if (unbuffered_read == 0)
	    zsyncfd (fd);
	}

      bind_array_element (entry, array_index, line, 0);
    }

  xfree (line);

  if (unbuffered_read == 0)
    zsyncfd (fd);

  interrupt_immediately--;
  return EXECUTION_SUCCESS;
}

int
mapfile_builtin (list)
     WORD_LIST *list;
{
  int opt, code, fd, clear_array, flags;
  intmax_t intval;
  long lines, origin, nskip, callback_quantum;
  char *array_name, *callback;

  clear_array = 1;
  fd = 0;
  lines = origin = nskip = 0;
  flags = MAPF_CLEARARRAY;
  callback_quantum = DEFAULT_QUANTUM;
  callback = 0;

  reset_internal_getopt ();
  while ((opt = internal_getopt (list, "u:n:O:tC:c:s:")) != -1)
    {
      switch (opt)
	{
	case 'u':
	  code = legal_number (list_optarg, &intval);
	  if (code == 0 || intval < 0 || intval != (int)intval)
	    {
	      builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
	      return (EXECUTION_FAILURE);
	    }
	  else
	    fd = intval;

	  if (sh_validfd (fd) == 0)
	    {
	      builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
	      return (EXECUTION_FAILURE);
	    }
	  break;	  

	case 'n':
	  code = legal_number (list_optarg, &intval);
	  if (code == 0 || intval < 0 || intval != (unsigned)intval)
	    {
	      builtin_error (_("%s: invalid line count"), list_optarg);
	      return (EXECUTION_FAILURE);
	    }
	  else
	    lines = intval;
	  break;

	case 'O':
	  code = legal_number (list_optarg, &intval);
	  if (code == 0 || intval < 0 || intval != (unsigned)intval)
	    {
	      builtin_error (_("%s: invalid array origin"), list_optarg);
	      return (EXECUTION_FAILURE);
	    }
	  else
	    origin = intval;
	  flags &= ~MAPF_CLEARARRAY;
	  break;
	case 't':
	  flags |= MAPF_CHOP;
	  break;
	case 'C':
	  callback = list_optarg;
	  break;
	case 'c':
	  code = legal_number (list_optarg, &intval);
	  if (code == 0 || intval <= 0 || intval != (unsigned)intval)
	    {
	      builtin_error (_("%s: invalid callback quantum"), list_optarg);
	      return (EXECUTION_FAILURE);
	    }
	  else
	    callback_quantum = intval;
	  break;
	case 's':
	  code = legal_number (list_optarg, &intval);
	  if (code == 0 || intval < 0 || intval != (unsigned)intval)
	    {
	      builtin_error (_("%s: invalid line count"), list_optarg);
	      return (EXECUTION_FAILURE);
	    }
	  else
	    nskip = intval;
	  break;
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }
  list = loptend;

  if (list == 0) 
    array_name = DEFAULT_ARRAY_NAME;
  else if (list->word == 0 || list->word->word == 0)
    {
      builtin_error ("internal error: getting variable name");
      return (EXECUTION_FAILURE);
    }
  else if (list->word->word[0] == '\0')
    {
      builtin_error (_("empty array variable name"));
      return (EX_USAGE);
    } 
  else
    array_name = list->word->word;
  
  if (legal_identifier (array_name) == 0 && valid_array_reference (array_name) == 0)
    {
      sh_invalidid (array_name);
      return (EXECUTION_FAILURE);
    }

  return mapfile (fd, lines, origin, nskip, callback_quantum, callback, array_name, flags);
}

#else

int
mapfile_builtin (list)
     WORD_LIST *list;
{
  builtin_error (_("array variable support required"));
  return (EXECUTION_FAILURE);
}

#endif  /* ARRAY_VARS */