Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Read file with escaped newlines into array



On Thu, Nov 19, 2015 at 6:34 PM, Sebastian Gniazdowski
<sgniazdowski@xxxxxxxxx> wrote:
> Hello
> fc -W stores history to a given file. It escapes newlines with \. How
> to read such file into array and have "\^M" lines put back together
> into single array entries?

Also note that the history file is stored metafied, so even if you
take care of escaped newlines, you still would need to unmetafy it.

-- 
Mikael Magnusson

unmetafy.c

#define Meta ((char) 0x83)

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

/* from zsh utils.c */
char *unmetafy(char *s, int *len)
{
  char *p, *t;

  for (p = s; *p && *p != Meta; p++);
  for (t = p; (*t = *p++);)
    if (*t++ == Meta)
      t[-1] = *p++ ^ 32;
  if (len)
    *len = t - s;
  return s;
}

int main(int argc, char *argv[]) {
  char *line = NULL;
  size_t size;

  while (getline(&line, &size, stdin) != -1) {
    unmetafy(line, NULL);
    printf("%s", line);
  }

  if (line) free(line);
  return EXIT_SUCCESS;
}



Messages sorted by: Reverse Date, Date, Thread, Author