1/***********************************************************************
2*                                                                      *
3*               This software is part of the ast package               *
4*          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5*                      and is licensed under the                       *
6*                 Eclipse Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*          http://www.eclipse.org/org/documents/epl-v10.html           *
11*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12*                                                                      *
13*              Information and Software Systems Research               *
14*                            AT&T Research                             *
15*                           Florham Park NJ                            *
16*                                                                      *
17*                 Glenn Fowler <gsf@research.att.com>                  *
18*                  David Korn <dgk@research.att.com>                   *
19*                   Phong Vo <kpv@research.att.com>                    *
20*                                                                      *
21***********************************************************************/
22#pragma prototyped
23
24/*
25 * return the record format string given a format descriptor
26 */
27
28#include <recfmt.h>
29#include <ctype.h>
30
31char*
32fmtrec(Recfmt_t f, int fs)
33{
34	char*	b;
35	char*	e;
36	char*	s;
37	long	n;
38	char	del[2];
39
40	b = s = fmtbuf(n = 32);
41	e = b + n;
42	switch (RECTYPE(f))
43	{
44	case REC_delimited:
45		*s++ = 'd';
46		if ((del[0] = REC_D_DELIMITER(f)) != '\n')
47		{
48			del[1] = 0;
49			if (fs)
50				sfsprintf(s, e - s, "0x%02x", *(unsigned char*)del);
51			else
52				sfsprintf(s, e - s, "%s", fmtquote(del, NiL, NiL, 1, 0));
53		}
54		else
55			*s = 0;
56		break;
57	case REC_fixed:
58		if (!fs)
59			*s++ = 'f';
60		sfsprintf(s, e - s, "%lu", REC_F_SIZE(f));
61		break;
62	case REC_variable:
63		*s++ = 'v';
64		if (n = REC_V_SIZE(f))
65			s += sfsprintf(s, e - s, "%lu", n);
66		if (REC_V_HEADER(f) != 4)
67			s += sfsprintf(s, e - s, "h%u", REC_V_HEADER(f));
68		if (REC_V_OFFSET(f) != 0)
69			s += sfsprintf(s, e - s, "o%u", REC_V_OFFSET(f));
70		if (REC_V_LENGTH(f) != 2)
71			s += sfsprintf(s, e - s, "z%u", REC_V_LENGTH(f));
72		if (REC_V_LITTLE(f) != 0)
73			*s++ = 'l';
74		if (REC_V_INCLUSIVE(f) == 0)
75			*s++ = 'n';
76		*s = 0;
77		break;
78	case REC_method:
79		*s++ = 'm';
80		switch (n = REC_M_INDEX(f))
81		{
82		case REC_M_data:
83			sfsprintf(s, e - s, "data");
84			break;
85		case REC_M_path:
86			sfsprintf(s, e - s, "path");
87			break;
88		default:
89			sfsprintf(s, e - s, "%lu", n);
90			break;
91		}
92		break;
93	case REC_none:
94		*s++ = 'n';
95		*s = 0;
96		break;
97	default:
98		sfsprintf(s, e - s, "u%u.0x%07x", RECTYPE(f), REC_U_ATTRIBUTES(f));
99		break;
100	}
101	return b;
102}
103