18 "%sUsage:%s readarray [-d delim] [-n count] [-O origin] [-s count] "
19 "[-t] [-u fd] [-C callback] [-c quantum] array\n"
20 "Read records from standard input (or -u fd) into the named array.\n",
29 char *callback = NULL;
30 char *oarr_name = NULL;
32 if (OPT_ISSET(ops,
'u')) {
33 srcfd = OPT_ARG(ops,
'u') ? atoi(OPT_ARG(ops,
'u')) : 0;
35 if (OPT_ISSET(ops,
'C')) {
36 callback = OPT_ARG(ops,
'C') ?
ztrdup(OPT_ARG(ops,
'C')) : NULL;
39 zwarnnam(nam,
"%d: Name of the output array is required, aborting",
50 "%d: Extra arguments detected, only one argument is needed, see "
61 if (!OPT_ISSET(ops,
'O')) {
63 char **emptyarr = (
char **)
zalloc(
sizeof(
char *));
67 stream = fdopen(srcfd,
"r");
69 zwarnnam(nam,
"line %d: couldn't open descriptor %d: %e", __LINE__, srcfd,
86 if (OPT_ISSET(ops,
'd')) {
87 delim = OPT_ARG(ops,
'd') ? OPT_ARG(ops,
'd')[0] :
'\n';
89 if (OPT_ISSET(ops,
'n')) {
90 to_copy = OPT_ARG(ops,
'n') ? atoi(OPT_ARG(ops,
'n')) : 0;
92 if (OPT_ISSET(ops,
'O')) {
93 start_at = OPT_ARG(ops,
'O') ? atoi(OPT_ARG(ops,
'O')) : 1;
95 if (OPT_ISSET(ops,
's')) {
96 skip_first = OPT_ARG(ops,
's') ? atoi(OPT_ARG(ops,
's')) : 0;
98 if (OPT_ISSET(ops,
't')) {
101 if (OPT_ISSET(ops,
'c')) {
102 quantum = OPT_ARG(ops,
'c') ? atoi(OPT_ARG(ops,
'c')) : 5000;
107 int index = start_at;
109 while ((read_len = getline(&line, &len, stream)) != -1) {
110 if (skip_first > 0) {
114 if (remdel && read_len > 0 && line[read_len - 1] ==
'\n') {
115 line[--read_len] =
'\0';
117 if (to_copy > 0 && index - start_at >= to_copy)
119 char indexed_name[256];
120 snprintf(indexed_name,
sizeof(indexed_name),
"%s[%d]", oarr_name, index);
122 if (callback && (index - start_at + 1) % quantum == 0) {
124 snprintf(idx_str,
sizeof(idx_str),
"%d", index);
125 char *args[] = {idx_str, line, NULL};
126 execstring(callback, args, 0, 0);
132 size_t cap = 1024, sz = 0;
133 char *buf = (
char *)
zalloc(cap);
136 zwarnnam(nam,
"%d: Out of memory", __LINE__);
139 while ((c = fgetc(stream)) != EOF) {
140 if (skip_first > 0 && c == delim) {
146 size_t newcap = cap * 2;
147 char *nbuf = (
char *)
zrealloc(buf, newcap);
150 zwarnnam(nam,
"%d: Out of memory", __LINE__);
158 buf[sz++] = (char)delim;
161 if (to_copy > 0 && index - start_at >= to_copy)
163 char indexed_name[256];
164 snprintf(indexed_name,
sizeof(indexed_name),
"%s[%d]", oarr_name,
167 if (callback && (index - start_at + 1) % quantum == 0) {
169 snprintf(idx_str,
sizeof(idx_str),
"%d", index);
170 char *args[] = {idx_str, buf, NULL};
171 execstring(callback, args, 0, 0);
179 if (sz > 0 && (to_copy == 0 || index - start_at < to_copy)) {
181 char indexed_name[256];
182 snprintf(indexed_name,
sizeof(indexed_name),
"%s[%d]", oarr_name, index);
const char * zp_icon(const char *s)
Return icon string if enabled, empty string otherwise.
static void readarray_usage(void)
int bin_readarray(char *nam, char **argv, Options ops, int func)
readarray builtin entrypoint
Module declaration header (mdh) for zpmod.
Prototype stub for zpmod when building out-of-tree.
Optional terminal/locale detection for emoji support in messages.
void * zalloc(size_t size)
void unsetparam(const char *name)
void setaparam(const char *name, char **value)
void zfree(void *ptr, size_t size)
void setsparam(const char *name, char *value)
void * zrealloc(void *ptr, size_t size)
void zwarnnam(const char *, const char *,...)
char * ztrdup(const char *)
Local, non-invasive shims to suppress benign vendor header warnings.