1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "sfdchdr.h"
23da2e3ebdSchin
24da2e3ebdSchin /*
25da2e3ebdSchin * a discipline that prepends a prefix string to each output line
26da2e3ebdSchin *
27da2e3ebdSchin * Glenn Fowler
28da2e3ebdSchin * AT&T Research
29da2e3ebdSchin *
30da2e3ebdSchin * @(#)$Id: sfdcprefix (AT&T Research) 1998-06-25 $
31da2e3ebdSchin */
32da2e3ebdSchin
33da2e3ebdSchin typedef struct
34da2e3ebdSchin {
35da2e3ebdSchin Sfdisc_t disc; /* sfio discipline */
36da2e3ebdSchin size_t length; /* prefix length */
37da2e3ebdSchin size_t empty; /* empty line prefix length */
38da2e3ebdSchin int skip; /* this line already prefixed */
39da2e3ebdSchin char prefix[1]; /* prefix string */
40da2e3ebdSchin } Prefix_t;
41da2e3ebdSchin
42da2e3ebdSchin /*
43da2e3ebdSchin * prefix write
44da2e3ebdSchin */
45da2e3ebdSchin
46da2e3ebdSchin #if __STD_C
pfxwrite(Sfio_t * f,const Void_t * buf,register size_t n,Sfdisc_t * dp)47da2e3ebdSchin static ssize_t pfxwrite(Sfio_t* f, const Void_t* buf, register size_t n, Sfdisc_t* dp)
48da2e3ebdSchin #else
49da2e3ebdSchin static ssize_t pfxwrite(f, buf, n, dp)
50da2e3ebdSchin Sfio_t* f;
51da2e3ebdSchin Void_t* buf;
52da2e3ebdSchin register size_t n;
53da2e3ebdSchin Sfdisc_t* dp;
54da2e3ebdSchin #endif
55da2e3ebdSchin {
56da2e3ebdSchin register Prefix_t* pfx = (Prefix_t*)dp;
57da2e3ebdSchin register char* b;
58da2e3ebdSchin register char* s;
59da2e3ebdSchin register char* e;
60da2e3ebdSchin register char* t;
61da2e3ebdSchin register ssize_t w;
62da2e3ebdSchin int skip;
63da2e3ebdSchin
64da2e3ebdSchin skip = 0;
65da2e3ebdSchin w = 0;
66da2e3ebdSchin b = (char*)buf;
67da2e3ebdSchin s = b;
68da2e3ebdSchin e = s + n;
69da2e3ebdSchin do
70da2e3ebdSchin {
71da2e3ebdSchin if (!(t = memchr(s, '\n', e - s)))
72da2e3ebdSchin {
73da2e3ebdSchin skip = 1;
74da2e3ebdSchin t = e - 1;
75da2e3ebdSchin }
76da2e3ebdSchin n = t - s + 1;
77da2e3ebdSchin if (pfx->skip)
78da2e3ebdSchin pfx->skip = 0;
79da2e3ebdSchin else
80da2e3ebdSchin sfwr(f, pfx->prefix, n > 1 ? pfx->length : pfx->empty, dp);
81da2e3ebdSchin w += sfwr(f, s, n, dp);
82da2e3ebdSchin if ((s = t + 1) >= e)
83da2e3ebdSchin return w;
84da2e3ebdSchin } while ((s = t + 1) < e);
85da2e3ebdSchin pfx->skip = skip;
86da2e3ebdSchin return w;
87da2e3ebdSchin
88da2e3ebdSchin }
89da2e3ebdSchin
90da2e3ebdSchin /*
91da2e3ebdSchin * remove the discipline on close
92da2e3ebdSchin */
93da2e3ebdSchin
94da2e3ebdSchin #if __STD_C
pfxexcept(Sfio_t * f,int type,Void_t * data,Sfdisc_t * dp)95da2e3ebdSchin static int pfxexcept(Sfio_t* f, int type, Void_t* data, Sfdisc_t* dp)
96da2e3ebdSchin #else
97da2e3ebdSchin static int pfxexcept(f, type, data, dp)
98da2e3ebdSchin Sfio_t* f;
99da2e3ebdSchin int type;
100da2e3ebdSchin Void_t* data;
101da2e3ebdSchin Sfdisc_t* dp;
102da2e3ebdSchin #endif
103da2e3ebdSchin {
104da2e3ebdSchin if (type == SF_FINAL || type == SF_DPOP)
105da2e3ebdSchin free(dp);
106da2e3ebdSchin return 0;
107da2e3ebdSchin }
108da2e3ebdSchin
109da2e3ebdSchin /*
110da2e3ebdSchin * push the prefix discipline on f
111da2e3ebdSchin */
112da2e3ebdSchin
113da2e3ebdSchin #if __STD_C
sfdcprefix(Sfio_t * f,const char * prefix)114da2e3ebdSchin int sfdcprefix(Sfio_t* f, const char* prefix)
115da2e3ebdSchin #else
116da2e3ebdSchin int sfdcprefix(f, prefix)
117da2e3ebdSchin Sfio_t* f;
118da2e3ebdSchin char* prefix;
119da2e3ebdSchin #endif
120da2e3ebdSchin {
121da2e3ebdSchin register Prefix_t* pfx;
122da2e3ebdSchin register char* s;
123da2e3ebdSchin size_t n;
124da2e3ebdSchin
125da2e3ebdSchin /*
126da2e3ebdSchin * this is a writeonly discipline
127da2e3ebdSchin */
128da2e3ebdSchin
129da2e3ebdSchin if (!prefix || !(n = strlen(prefix)) || !(sfset(f, 0, 0) & SF_WRITE))
130da2e3ebdSchin return -1;
131da2e3ebdSchin if (!(pfx = (Prefix_t*)malloc(sizeof(Prefix_t) + n)))
132da2e3ebdSchin return -1;
133da2e3ebdSchin memset(pfx, 0, sizeof(*pfx));
134da2e3ebdSchin
135da2e3ebdSchin pfx->disc.writef = pfxwrite;
136da2e3ebdSchin pfx->disc.exceptf = pfxexcept;
137da2e3ebdSchin pfx->length = n;
138da2e3ebdSchin memcpy(pfx->prefix, prefix, n);
139da2e3ebdSchin s = (char*)prefix + n;
140da2e3ebdSchin while (--s > (char*)prefix && (*s == ' ' || *s == '\t'));
141da2e3ebdSchin n = s - (char*)prefix;
142da2e3ebdSchin if (*s != ' ' || *s != '\t')
143da2e3ebdSchin n++;
144da2e3ebdSchin pfx->empty = n;
145da2e3ebdSchin
146da2e3ebdSchin if (sfdisc(f, &pfx->disc) != &pfx->disc)
147da2e3ebdSchin {
148da2e3ebdSchin free(pfx);
149da2e3ebdSchin return -1;
150da2e3ebdSchin }
151da2e3ebdSchin
152da2e3ebdSchin return 0;
153da2e3ebdSchin }
154