xref: /titanic_44/usr/src/lib/libast/common/misc/optctx.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
17c2fbfb3SApril Chin /***********************************************************************
27c2fbfb3SApril Chin *                                                                      *
37c2fbfb3SApril Chin *               This software is part of the ast package               *
4*3e14f97fSRoger A. Faulkner *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
57c2fbfb3SApril Chin *                      and is licensed under the                       *
67c2fbfb3SApril Chin *                  Common Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
87c2fbfb3SApril Chin *                                                                      *
97c2fbfb3SApril Chin *                A copy of the License is available at                 *
107c2fbfb3SApril Chin *            http://www.opensource.org/licenses/cpl1.0.txt             *
117c2fbfb3SApril Chin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
127c2fbfb3SApril Chin *                                                                      *
137c2fbfb3SApril Chin *              Information and Software Systems Research               *
147c2fbfb3SApril Chin *                            AT&T Research                             *
157c2fbfb3SApril Chin *                           Florham Park NJ                            *
167c2fbfb3SApril Chin *                                                                      *
177c2fbfb3SApril Chin *                 Glenn Fowler <gsf@research.att.com>                  *
187c2fbfb3SApril Chin *                  David Korn <dgk@research.att.com>                   *
197c2fbfb3SApril Chin *                   Phong Vo <kpv@research.att.com>                    *
207c2fbfb3SApril Chin *                                                                      *
217c2fbfb3SApril Chin ***********************************************************************/
227c2fbfb3SApril Chin #pragma prototyped
237c2fbfb3SApril Chin /*
247c2fbfb3SApril Chin  * Glenn Fowler
257c2fbfb3SApril Chin  * AT&T Research
267c2fbfb3SApril Chin  *
277c2fbfb3SApril Chin  * _opt_infop_ context control
287c2fbfb3SApril Chin  *
297c2fbfb3SApril Chin  * allocate new context:
307c2fbfb3SApril Chin  *	new_context = optctx(0, 0);
317c2fbfb3SApril Chin  * free new context:
327c2fbfb3SApril Chin  *	optctx(0, new_context);
337c2fbfb3SApril Chin  * switch to new_context:
347c2fbfb3SApril Chin  *	old_context = optctx(new_context, 0);
357c2fbfb3SApril Chin  * switch to old_context and free new_context:
367c2fbfb3SApril Chin  *	optctx(old_context, new_context);
377c2fbfb3SApril Chin  */
387c2fbfb3SApril Chin 
397c2fbfb3SApril Chin #include <optlib.h>
407c2fbfb3SApril Chin 
417c2fbfb3SApril Chin static Opt_t*	freecontext;
427c2fbfb3SApril Chin 
437c2fbfb3SApril Chin Opt_t*
optctx(Opt_t * p,Opt_t * o)447c2fbfb3SApril Chin optctx(Opt_t* p, Opt_t* o)
457c2fbfb3SApril Chin {
467c2fbfb3SApril Chin 	if (o)
477c2fbfb3SApril Chin 	{
487c2fbfb3SApril Chin 		if (freecontext)
497c2fbfb3SApril Chin 			free(o);
507c2fbfb3SApril Chin 		else
517c2fbfb3SApril Chin 			freecontext = o;
527c2fbfb3SApril Chin 		if (!p)
537c2fbfb3SApril Chin 			return 0;
547c2fbfb3SApril Chin 	}
557c2fbfb3SApril Chin 	if (p)
567c2fbfb3SApril Chin 	{
577c2fbfb3SApril Chin 		o = _opt_infop_;
587c2fbfb3SApril Chin 		_opt_infop_ = p;
597c2fbfb3SApril Chin 	}
607c2fbfb3SApril Chin 	else
617c2fbfb3SApril Chin 	{
627c2fbfb3SApril Chin 		if (o = freecontext)
637c2fbfb3SApril Chin 			freecontext = 0;
647c2fbfb3SApril Chin 		else if (!(o = newof(0, Opt_t, 1, 0)))
657c2fbfb3SApril Chin 			return 0;
667c2fbfb3SApril Chin 		memset(o, 0, sizeof(Opt_t));
677c2fbfb3SApril Chin 		o->state = _opt_infop_->state;
687c2fbfb3SApril Chin 	}
697c2fbfb3SApril Chin 	return o;
707c2fbfb3SApril Chin }
71