aboutsummaryrefslogtreecommitdiff
path: root/toys/pending
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-12-19 21:49:14 -0600
committerRob Landley <rob@landley.net>2015-12-19 21:49:14 -0600
commit146d219e117b56cc6f7e77c1083e91a961b906b5 (patch)
tree6087f0f0ebc8f2db71f38f468baa04cf97542f0a /toys/pending
parentb64abbc1c637f8b79a4a5f1382ae7d6f4bcc0d4d (diff)
downloadtoybox-146d219e117b56cc6f7e77c1083e91a961b906b5.tar.gz
Redo linestack to handle embedded NULs.
Diffstat (limited to 'toys/pending')
-rw-r--r--toys/pending/vi.c58
1 files changed, 42 insertions, 16 deletions
diff --git a/toys/pending/vi.c b/toys/pending/vi.c
index cd72f03e..20d765cf 100644
--- a/toys/pending/vi.c
+++ b/toys/pending/vi.c
@@ -26,7 +26,7 @@ GLOBALS(
struct linestack {
long len, max;
- char *line[];
+ struct ptr_len idx[];
};
// Insert one stack into another before position in old stack.
@@ -43,14 +43,14 @@ void linestack_addstack(struct linestack **lls, struct linestack *throw,
if (catch->len+throw->len >= catch->max) {
// New size rounded up to next multiple of 64, allocate and copy start.
catch->max = ((catch->len+throw->len)|63)+1;
- *lls = xmalloc(sizeof(struct linestack)+catch->max*sizeof(char *));
- memcpy(*lls, catch, sizeof(struct linestack)+pos*sizeof(char *));
+ *lls = xmalloc(sizeof(struct linestack)+catch->max*sizeof(struct ptr_len));
+ memcpy(*lls, catch, sizeof(struct linestack)+pos*sizeof(struct ptr_len));
}
// Copy end (into new allocation if necessary)
if (pos != catch->len)
- memmove((*lls)->line+pos+throw->len, catch->line+pos,
- (catch->len-pos)*sizeof(char *));
+ memmove((*lls)->idx+pos+throw->len, catch->idx+pos,
+ (catch->len-pos)*sizeof(struct ptr_len));
// Cleanup if we had to realloc.
if (catch != *lls) {
@@ -58,34 +58,60 @@ void linestack_addstack(struct linestack **lls, struct linestack *throw,
catch = *lls;
}
- memcpy(catch->line+pos, throw->line, throw->len*sizeof(char *));
+ memcpy(catch->idx+pos, throw->idx, throw->len*sizeof(struct ptr_len));
catch->len += throw->len;
}
-void linestack_insert(struct linestack **lls, long pos, char *line)
+void linestack_insert(struct linestack **lls, long pos, char *line, long len)
{
// alloca() was in 32V and Turbo C for DOS, but isn't in posix or c99.
// I'm not thrashing the heap for this, but this should work even if
// a broken compiler adds gratuitous padding.
struct {
struct linestack ls;
- char *line;
+ struct ptr_len pl;
} ls;
ls.ls.len = ls.ls.max = 1;
- *ls.ls.line = line;
+ ls.ls.idx[0].ptr = line;
+ ls.ls.idx[0].len = len;
linestack_addstack(lls, &ls.ls, pos);
}
-void vi_main(void)
+void linestack_append(struct linestack **lls, char *line)
{
- int i;
+ linestack_insert(lls, (*lls)->len, line, strlen(line));
+}
+
+struct linestack *linestack_load(char *name)
+{
+ FILE *fp = fopen(name, "r");
+ struct linestack *ls;
+
+ if (!fp) return 0;
+
+ ls = xzalloc(sizeof(struct linestack));
- TT.ls = xzalloc(sizeof(struct linestack));
+ for (;;) {
+ char *line = 0;
+ ssize_t len;
- linestack_insert(&TT.ls, 0, "one");
- linestack_insert(&TT.ls, 1, "two");
- linestack_insert(&TT.ls, 2, "three");
+ if ((len = getline(&line, (void *)&len, fp))<1) break;
+ if (line[len-1]=='\n') len--;
+ linestack_insert(&ls, ls->len, line, len);
+ }
+ fclose(fp);
+
+ return ls;
+}
+
+void vi_main(void)
+{
+ int i;
- for (i=0; i<TT.ls->len; i++) printf("%s\n", TT.ls->line[i]);
+ if (!(TT.ls = linestack_load(*toys.optargs)))
+ TT.ls = xzalloc(sizeof(struct linestack));
+
+ for (i=0; i<TT.ls->len; i++)
+ printf("%.*s\n", (int)TT.ls->idx[i].len, (char *)TT.ls->idx[i].ptr);
}