xref: /titanic_50/usr/src/lib/libast/common/misc/optctx.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 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  * Glenn Fowler
25  * AT&T Research
26  *
27  * _opt_infop_ context control
28  *
29  * allocate new context:
30  *	new_context = optctx(0, 0);
31  * free new context:
32  *	optctx(0, new_context);
33  * switch to new_context:
34  *	old_context = optctx(new_context, 0);
35  * switch to old_context and free new_context:
36  *	optctx(old_context, new_context);
37  */
38 
39 #include <optlib.h>
40 
41 static Opt_t*	freecontext;
42 
43 Opt_t*
optctx(Opt_t * p,Opt_t * o)44 optctx(Opt_t* p, Opt_t* o)
45 {
46 	if (o)
47 	{
48 		if (freecontext)
49 			free(o);
50 		else
51 			freecontext = o;
52 		if (!p)
53 			return 0;
54 	}
55 	if (p)
56 	{
57 		o = _opt_infop_;
58 		_opt_infop_ = p;
59 	}
60 	else
61 	{
62 		if (o = freecontext)
63 			freecontext = 0;
64 		else if (!(o = newof(0, Opt_t, 1, 0)))
65 			return 0;
66 		memset(o, 0, sizeof(Opt_t));
67 		o->state = _opt_infop_->state;
68 	}
69 	return o;
70 }
71