/* Copyright status: this file is in the public domain. */ #include #include #include #include "stdio-util.h" typedef struct indent_priv INDENT_PRIV; struct indent_priv { FILE *inner; int atnl; } ; static int w_indent(void *pv, const char *data, int len) { INDENT_PRIV *p; int atnl; FILE *f; int o; int o0; p = pv; atnl = p->atnl; f = p->inner; o0 = -1; for (o=0;o= 0) abort(); putc('\t',f); atnl = 0; } if (data[o] == '\n') { if (o0 >= 0) { fwrite(data+o0,1,(o+1)-o0,f); o0 = -1; } atnl = 1; } else { if (o0 < 0) o0 = o; atnl = 0; } } if (o0 >= 0) fwrite(data+o0,1,o-o0,f); p->atnl = atnl; return(len); } static int c_indent(void *pv) { free(pv); return(0); } FILE *fwrap_indent(FILE *inner) { INDENT_PRIV *p; FILE *f; p = malloc(sizeof(INDENT_PRIV)); if (! p) return(0); f = funopen(p,0,&w_indent,0,&c_indent); if (! f) { free(p); return(0); } p->inner = inner; p->atnl = 1; return(f); }