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