sfdcprefix.c revision 7c2fbfb345896881c631598ee3852ce9ce33fb07
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* Copyright (c) 1985-2008 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* http://www.opensource.org/licenses/cpl1.0.txt *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "sfdchdr.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * a discipline that prepends a prefix string to each output line
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Glenn Fowler
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * AT&T Research
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * @(#)$Id: sfdcprefix (AT&T Research) 1998-06-25 $
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintypedef struct
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Sfdisc_t disc; /* sfio discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin size_t length; /* prefix length */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin size_t empty; /* empty line prefix length */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int skip; /* this line already prefixed */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin char prefix[1]; /* prefix string */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin} Prefix_t;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * prefix write
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic ssize_t pfxwrite(Sfio_t* f, const Void_t* buf, register size_t n, Sfdisc_t* dp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic ssize_t pfxwrite(f, buf, n, dp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* f;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVoid_t* buf;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinregister size_t n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfdisc_t* dp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register Prefix_t* pfx = (Prefix_t*)dp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* b;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* s;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* e;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* t;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register ssize_t w;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int skip;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin skip = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin w = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin b = (char*)buf;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin s = b;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin e = s + n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin do
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!(t = memchr(s, '\n', e - s)))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin skip = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin t = e - 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = t - s + 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (pfx->skip)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin pfx->skip = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sfwr(f, pfx->prefix, n > 1 ? pfx->length : pfx->empty, dp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin w += sfwr(f, s, n, dp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((s = t + 1) >= e)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return w;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } while ((s = t + 1) < e);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin pfx->skip = skip;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return w;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * remove the discipline on close
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int pfxexcept(Sfio_t* f, int type, Void_t* data, Sfdisc_t* dp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int pfxexcept(f, type, data, dp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* f;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint type;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVoid_t* data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfdisc_t* dp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (type == SF_FINAL || type == SF_DPOP)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin free(dp);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * push the prefix discipline on f
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint sfdcprefix(Sfio_t* f, const char* prefix)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint sfdcprefix(f, prefix)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* f;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinchar* prefix;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register Prefix_t* pfx;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register char* s;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin size_t n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * this is a writeonly discipline
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!prefix || !(n = strlen(prefix)) || !(sfset(f, 0, 0) & SF_WRITE))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!(pfx = (Prefix_t*)malloc(sizeof(Prefix_t) + n)))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin memset(pfx, 0, sizeof(*pfx));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin pfx->disc.writef = pfxwrite;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin pfx->disc.exceptf = pfxexcept;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin pfx->length = n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin memcpy(pfx->prefix, prefix, n);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin s = (char*)prefix + n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while (--s > (char*)prefix && (*s == ' ' || *s == '\t'));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n = s - (char*)prefix;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (*s != ' ' || *s != '\t')
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n++;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin pfx->empty = n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (sfdisc(f, &pfx->disc) != &pfx->disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin free(pfx);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}