xref: /titanic_41/usr/src/lib/libast/common/string/fmtident.c (revision 25ccf31477c2a409aed8d0cf2027cd1073b0fe00)
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 #include <ast.h>
25 #include <ctype.h>
26 
27 #define IDENT	01
28 #define USAGE	02
29 
30 /*
31  * format what(1) and/or ident(1) string a
32  */
33 
34 char*
35 fmtident(const char* a)
36 {
37 	register char*	s = (char*)a;
38 	register char*	t;
39 	char*		buf;
40 	int		i;
41 
42 	i = 0;
43 	for (;;)
44 	{
45 		while (isspace(*s))
46 			s++;
47 		if (s[0] == '[')
48 		{
49 			while (*++s && *s != '\n');
50 			i |= USAGE;
51 		}
52 		else if (s[0] == '@' && s[1] == '(' && s[2] == '#' && s[3] == ')')
53 			s += 4;
54 		else if (s[0] == '$' && s[1] == 'I' && s[2] == 'd' && s[3] == ':' && isspace(s[4]))
55 		{
56 			s += 5;
57 			i |= IDENT;
58 		}
59 		else
60 			break;
61 	}
62 	if (i)
63 	{
64 		i &= IDENT;
65 		for (t = s; isprint(*t) && *t != '\n'; t++)
66 			if (i && t[0] == ' ' && t[1] == '$')
67 				break;
68 		while (t > s && isspace(t[-1]))
69 			t--;
70 		i = t - s;
71 		buf = fmtbuf(i + 1);
72 		memcpy(buf, s, i);
73 		s = buf;
74 		s[i] = 0;
75 	}
76 	return s;
77 }
78