xref: /titanic_50/usr/src/lib/libast/common/misc/fmtrec.c (revision 7c2fbfb345896881c631598ee3852ce9ce33fb07)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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 
31 char*
32 fmtrec(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