1/*++
2/* NAME
3/*	tok822_node 3
4/* SUMMARY
5/*	token memory management
6/* SYNOPSIS
7/*	#include <tok822.h>
8/*
9/*	TOK822	*tok822_alloc(type, strval)
10/*	int	type;
11/*	const char *strval;
12/*
13/*	TOK822	*tok822_free(tp)
14/*	TOK822	*tp;
15/* DESCRIPTION
16/*	This module implements memory management for token
17/*	structures. A distinction is made between single-character
18/*	tokens that have no string value, and string-valued tokens.
19/*
20/*	tok822_alloc() allocates memory for a token structure of
21/*	the named type, and initializes it properly. In case of
22/*	a single-character token, no string memory is allocated.
23/*	Otherwise, \fIstrval\fR is a null pointer or provides
24/*	string data to initialize the token with.
25/*
26/*	tok822_free() releases the memory used for the specified token
27/*	and conveniently returns a null pointer value.
28/* LICENSE
29/* .ad
30/* .fi
31/*	The Secure Mailer license must be distributed with this software.
32/* AUTHOR(S)
33/*	Wietse Venema
34/*	IBM T.J. Watson Research
35/*	P.O. Box 704
36/*	Yorktown Heights, NY 10598, USA
37/*--*/
38
39/* System library. */
40
41#include <sys_defs.h>
42#include <string.h>
43
44/* Utility library. */
45
46#include <mymalloc.h>
47#include <vstring.h>
48
49/* Global library. */
50
51#include "tok822.h"
52
53/* tok822_alloc - allocate and initialize token */
54
55TOK822 *tok822_alloc(int type, const char *strval)
56{
57    TOK822 *tp;
58
59#define CONTAINER_TOKEN(x) \
60	((x) == TOK822_ADDR || (x) == TOK822_STARTGRP)
61
62    tp = (TOK822 *) mymalloc(sizeof(*tp));
63    tp->type = type;
64    tp->next = tp->prev = tp->head = tp->tail = tp->owner = 0;
65    tp->vstr = (type < TOK822_MINTOK || CONTAINER_TOKEN(type) ? 0 :
66		strval == 0 ? vstring_alloc(10) :
67		vstring_strcpy(vstring_alloc(strlen(strval) + 1), strval));
68    return (tp);
69}
70
71/* tok822_free - destroy token */
72
73TOK822 *tok822_free(TOK822 *tp)
74{
75    if (tp->vstr)
76	vstring_free(tp->vstr);
77    myfree((char *) tp);
78    return (0);
79}
80