From 0f5f00c1d8b15abe41acc15cefb11dcb900c2a62 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 28 Jan 2022 19:15:59 -0600 Subject: [PATCH] Remove the linestack plumbing, an old unfinished todo item that got interrupted long enough ago it would be easier to restart from scratch. It was never used and hasn't been touched in years. --- lib/lib.h | 10 ------ lib/linestack.c | 85 ------------------------------------------------- 2 files changed, 95 deletions(-) diff --git a/lib/lib.h b/lib/lib.h index 16255da8..5e3905d5 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -304,16 +304,6 @@ void reset_env(struct passwd *p, int clear); // linestack.c -struct linestack { - long len, max; - struct ptr_len idx[]; -}; - -void linestack_addstack(struct linestack **lls, struct linestack *throw, - long pos); -void linestack_insert(struct linestack **lls, long pos, char *line, long len); -void linestack_append(struct linestack **lls, char *line); -struct linestack *linestack_load(char *name); int crunch_escape(FILE *out, int cols, int wc); int crunch_rev_escape(FILE *out, int cols, int wc); int crunch_str(char **str, int width, FILE *out, char *escmore, diff --git a/lib/linestack.c b/lib/linestack.c index 47eb2af9..1608c2f8 100644 --- a/lib/linestack.c +++ b/lib/linestack.c @@ -1,90 +1,5 @@ #include "toys.h" -// The design idea here is indexing a big blob of (potentially mmaped) data -// instead of copying the data into a zillion seperate malloc()s. - -// A linestack is an array of struct ptr_len, with a currently used len -// and max tracking the memory allocation. This indexes existing string data, -// the lifetime of which is tracked externally. - -// Insert one stack into another before position in old stack. -// (Does not copy contents of strings, just shuffles index array contents.) -void linestack_addstack(struct linestack **lls, struct linestack *throw, - long pos) -{ - struct linestack *catch = *lls; - - if (CFG_TOYBOX_DEBUG) - if (pos > catch->len) error_exit("linestack_addstack past end."); - - // Make a hole, allocating more space if necessary. - 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(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)->idx+pos+throw->len, catch->idx+pos, - (catch->len-pos)*sizeof(struct ptr_len)); - - // Cleanup if we had to realloc. - if (catch != *lls) { - free(catch); - catch = *lls; - } - - // Copy new chunk we made space for - memcpy(catch->idx+pos, throw->idx, throw->len*sizeof(struct ptr_len)); - catch->len += throw->len; -} - -// Insert one line/len into a linestack at pos -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. - // This allocates enough memory for the linestack to have one ptr_len. - // (Even if a compiler adds gratuitous padidng that just makes it bigger.) - struct { - struct ptr_len pl; - struct linestack ls; - } ls; - - ls.ls.len = ls.ls.max = 1; - ls.ls.idx[0].ptr = line; - ls.ls.idx[0].len = len; - linestack_addstack(lls, &ls.ls, pos); -} - -void linestack_append(struct linestack **lls, char *line) -{ - 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)); - - for (;;) { - char *line = 0; - ssize_t len; - - 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; -} - // Show width many columns, negative means from right edge, out=0 just measure // if escout, send it unprintable chars, otherwise pass through raw data. // Returns width in columns, moves *str to end of data consumed. -- 2.39.2