| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> |
| 3 |
<title>libSPF v1.0: macro.c Source File</title> |
| 4 |
<link href="doxygen.css" rel="stylesheet" type="text/css"> |
| 5 |
</head><body> |
| 6 |
<!-- Generated by Doxygen 1.3.8 --> |
| 7 |
<div class="qindex"> <form class="search" action="search.php" method="get"> |
| 8 |
<a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <span class="search"><u>S</u>earch for <input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div> |
| 9 |
<h1>macro.c</h1><a href="macro_8c.html">Go to the documentation of this file.</a><pre class="fragment"><div>00001 <span class="comment">/* libspf - Sender Policy Framework library</span> |
| 10 |
00002 <span class="comment">*</span> |
| 11 |
00003 <span class="comment">* ANSI C implementation of spf-draft-200405.txt</span> |
| 12 |
00004 <span class="comment">*</span> |
| 13 |
00005 <span class="comment">* Author: James Couzens <jcouzens@codeshare.ca></span> |
| 14 |
00006 <span class="comment">* Author: Sean Comeau <scomeau@obscurity.org></span> |
| 15 |
00007 <span class="comment">*</span> |
| 16 |
00008 <span class="comment">* File: util.c</span> |
| 17 |
00009 <span class="comment">* Desc: Utility functions</span> |
| 18 |
00010 <span class="comment">*</span> |
| 19 |
00011 <span class="comment">* License:</span> |
| 20 |
00012 <span class="comment">*</span> |
| 21 |
00013 <span class="comment">* The libspf Software License, Version 1.0</span> |
| 22 |
00014 <span class="comment">*</span> |
| 23 |
00015 <span class="comment">* Copyright (c) 2004 James Couzens & Sean Comeau All rights</span> |
| 24 |
00016 <span class="comment">* reserved.</span> |
| 25 |
00017 <span class="comment">*</span> |
| 26 |
00018 <span class="comment">* Redistribution and use in source and binary forms, with or without</span> |
| 27 |
00019 <span class="comment">* modification, are permitted provided that the following conditions</span> |
| 28 |
00020 <span class="comment">* are met:</span> |
| 29 |
00021 <span class="comment">*</span> |
| 30 |
00022 <span class="comment">* 1. Redistributions of source code must retain the above copyright</span> |
| 31 |
00023 <span class="comment">* notice, this list of conditions and the following disclaimer.</span> |
| 32 |
00024 <span class="comment">*</span> |
| 33 |
00025 <span class="comment">* 2. Redistributions in binary form must reproduce the above copyright</span> |
| 34 |
00026 <span class="comment">* notice, this list of conditions and the following disclaimer in</span> |
| 35 |
00027 <span class="comment">* the documentation and/or other materials provided with the</span> |
| 36 |
00028 <span class="comment">* distribution.</span> |
| 37 |
00029 <span class="comment">*</span> |
| 38 |
00030 <span class="comment">* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED</span> |
| 39 |
00031 <span class="comment">* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES</span> |
| 40 |
00032 <span class="comment">* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE</span> |
| 41 |
00033 <span class="comment">* DISCLAIMED. IN NO EVENT SHALL THE AUTHORS MAKING USE OF THIS LICESEN</span> |
| 42 |
00034 <span class="comment">* OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span> |
| 43 |
00035 <span class="comment">* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span> |
| 44 |
00036 <span class="comment">* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF</span> |
| 45 |
00037 <span class="comment">* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND</span> |
| 46 |
00038 <span class="comment">* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,</span> |
| 47 |
00039 <span class="comment">* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT</span> |
| 48 |
00040 <span class="comment">* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span> |
| 49 |
00041 <span class="comment">* SUCH DAMAGE.</span> |
| 50 |
00042 <span class="comment">*</span> |
| 51 |
00043 <span class="comment">*/</span> |
| 52 |
00044 |
| 53 |
00045 <span class="preprocessor">#include "<a class="code" href="spf_8h.html">spf.h</a>"</span> |
| 54 |
00046 <span class="preprocessor">#include "<a class="code" href="dns_8h.html">dns.h</a>"</span> |
| 55 |
00047 <span class="preprocessor">#include "<a class="code" href="macro_8h.html">macro.h</a>"</span> |
| 56 |
00048 <span class="preprocessor">#include "<a class="code" href="util_8h.html">util.h</a>"</span> |
| 57 |
00049 |
| 58 |
00050 |
| 59 |
00051 <span class="comment">/* MACRO_expand</span> |
| 60 |
00052 <span class="comment">*</span> |
| 61 |
00053 <span class="comment">* Author: Sean Comeau <scomeau@obscurity.org></span> |
| 62 |
00054 <span class="comment">* Author: James Couzens <jcouzens@codeshare.ca></span> |
| 63 |
00055 <span class="comment">*</span> |
| 64 |
00056 <span class="comment">* Date: 01/18/04</span> |
| 65 |
00057 <span class="comment">* Updated: 06/29/04 Roger Moser <Roger.Moser@pamho.net></span> |
| 66 |
00058 <span class="comment">*</span> |
| 67 |
00059 <span class="comment">* Desc:</span> |
| 68 |
00060 <span class="comment">* Walks a string of macros tokenizing and expanding them along the</span> |
| 69 |
00061 <span class="comment">* way, each token being inserted as a node into a linked list. Once the</span> |
| 70 |
00062 <span class="comment">* walk is complete the list is walked and the nodes are copied back out</span> |
| 71 |
00063 <span class="comment">* in the order they were added as expanded macros into a string which is</span> |
| 72 |
00064 <span class="comment">* then passed back to the calling function.</span> |
| 73 |
00065 <span class="comment">*</span> |
| 74 |
00066 <span class="comment">* MACRO_freebuf is removed and I moved the code inside of this function since</span> |
| 75 |
00067 <span class="comment">* before calling it, it was being walked anyway to copy out each string into</span> |
| 76 |
00068 <span class="comment">* the return buffer, so now it just free's directly after and then destructs.</span> |
| 77 |
00069 <span class="comment">*</span> |
| 78 |
00070 <span class="comment">*/</span> |
| 79 |
<a name="l00071"></a><a class="code" href="macro_8h.html#a1">00071</a> <span class="keywordtype">char</span> *<a class="code" href="macro_8h.html#a1">MACRO_expand</a>(<a class="code" href="structpeer__info__s.html">peer_info_t</a> *p, <span class="keyword">const</span> <span class="keywordtype">char</span> *s) |
| 80 |
00072 { |
| 81 |
00073 <span class="keyword">const</span> <span class="keywordtype">char</span> * <span class="keyword">const</span> macro_p = <span class="stringliteral">"%"</span>; <span class="comment">/* % (literal) */</span> |
| 82 |
00074 <span class="keyword">const</span> <span class="keywordtype">char</span> * <span class="keyword">const</span> macro_d = <span class="stringliteral">"%20"</span>; <span class="comment">/* "URL" space */</span> |
| 83 |
00075 <span class="keyword">const</span> <span class="keywordtype">char</span> * <span class="keyword">const</span> macro_u = <span class="stringliteral">" "</span>; <span class="comment">/* whitespace */</span> |
| 84 |
00076 |
| 85 |
00077 <span class="keywordtype">char</span> *buf; <span class="comment">/* return buffer */</span> |
| 86 |
00078 <span class="keywordtype">char</span> *ptr; <span class="comment">/* working pointer */</span> |
| 87 |
00079 <span class="keywordtype">char</span> *cp; <span class="comment">/* working pointer */</span> |
| 88 |
00080 <span class="keywordtype">char</span> *macro; |
| 89 |
00081 <span class="keywordtype">char</span> *s_macro; <span class="comment">/* single char macro */</span> |
| 90 |
00082 |
| 91 |
00083 <a class="code" href="structstrbuf__s.html">strbuf_t</a> *master; <span class="comment">/* list pointers */</span> |
| 92 |
00084 <a class="code" href="structstrbuf__node__s.html">strbuf_node_t</a> *c_node; <span class="comment">/* c_node node */</span> |
| 93 |
00085 <a class="code" href="structstrbuf__node__s.html">strbuf_node_t</a> *kill_node; <span class="comment">/* temp node used in destruct */</span> |
| 94 |
00086 |
| 95 |
00087 size_t len; <span class="comment">/* leng of passed string */</span> |
| 96 |
00088 size_t i = 0; <span class="comment">/* index on a str */</span> |
| 97 |
00089 size_t length = 0; <span class="comment">/* overall len of expanded str */</span> |
| 98 |
00090 |
| 99 |
00091 |
| 100 |
00092 <span class="keywordflow">if</span> (s == NULL) |
| 101 |
00093 { |
| 102 |
00094 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Passed a NULL string. Abort!\n"</span>); |
| 103 |
00095 <span class="keywordflow">return</span>(NULL); |
| 104 |
00096 } |
| 105 |
00097 |
| 106 |
00098 len = strlen(s); |
| 107 |
00099 ptr = cp = <a class="code" href="util_8h.html#a8">xstrndup</a>(s, (len + 1)); |
| 108 |
00100 |
| 109 |
00101 master = <a class="code" href="util_8h.html#a4">xmalloc</a>(<a class="code" href="util_8h.html#a3">SIZEOF</a>(<a class="code" href="structstrbuf__s.html">strbuf_t</a>)); |
| 110 |
00102 master-><a class="code" href="structstrbuf__s.html#o0">head</a> = NULL; |
| 111 |
00103 master-><a class="code" href="structstrbuf__s.html#o1">elements</a> = 0; |
| 112 |
00104 length = 0; |
| 113 |
00105 |
| 114 |
00106 <span class="keywordflow">while</span> (*ptr) |
| 115 |
00107 { |
| 116 |
00108 <span class="comment">/*</span> |
| 117 |
00109 <span class="comment"> * This works by moving through the string and replacing non-essential</span> |
| 118 |
00110 <span class="comment"> * elements with NULL chars until the character designating an expansion</span> |
| 119 |
00111 <span class="comment"> * mechanism is found. The character is then sent off to MACRO_process</span> |
| 120 |
00112 <span class="comment"> * for expansion</span> |
| 121 |
00113 <span class="comment"> */</span> |
| 122 |
00114 <span class="keywordflow">if</span> (*ptr == <span class="charliteral">'%'</span>) <span class="comment">/* start of macro */</span> |
| 123 |
00115 { |
| 124 |
00116 <span class="keywordflow">switch</span> (*(ptr + 1)) |
| 125 |
00117 { |
| 126 |
00118 <span class="keywordflow">case</span> <span class="charliteral">'%'</span>: |
| 127 |
00119 |
| 128 |
00120 <span class="comment">/* convert %% into % */</span> |
| 129 |
00121 <span class="keywordflow">if</span> (<a class="code" href="macro_8h.html#a4">MACRO_addbuf</a>(master, (<span class="keywordtype">char</span> *)macro_p, 1) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>) |
| 130 |
00122 { |
| 131 |
00123 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Unable to allocate list node with (%c)!\n"</span>, macro_p); |
| 132 |
00124 |
| 133 |
00125 <span class="keywordflow">return</span>(NULL); |
| 134 |
00126 } |
| 135 |
00127 |
| 136 |
00128 ptr += 2; |
| 137 |
00129 length++; |
| 138 |
00130 <span class="keywordflow">break</span>; |
| 139 |
00131 |
| 140 |
00132 <span class="keywordflow">case</span> <span class="charliteral">'_'</span>: |
| 141 |
00133 |
| 142 |
00134 <span class="comment">/* convert %_ to a white space */</span> |
| 143 |
00135 <span class="keywordflow">if</span> (<a class="code" href="macro_8h.html#a4">MACRO_addbuf</a>(master, (<span class="keywordtype">char</span> *)macro_u, 1) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>) |
| 144 |
00136 { |
| 145 |
00137 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Unable to allocate list node with (%c)!\n"</span>, macro_u); |
| 146 |
00138 |
| 147 |
00139 <span class="keywordflow">return</span>(NULL); |
| 148 |
00140 } |
| 149 |
00141 |
| 150 |
00142 ptr += 2; |
| 151 |
00143 length++; |
| 152 |
00144 <span class="keywordflow">break</span>; |
| 153 |
00145 |
| 154 |
00146 <span class="keywordflow">case</span> <span class="charliteral">'-'</span>: |
| 155 |
00147 |
| 156 |
00148 <span class="comment">/* convert %- into URL encoded '%20' */</span> |
| 157 |
00149 <span class="keywordflow">if</span> (<a class="code" href="macro_8h.html#a4">MACRO_addbuf</a>(master, (<span class="keywordtype">char</span> *)macro_d, 3) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>) |
| 158 |
00150 { |
| 159 |
00151 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Unable to allocate list node with (%s)!\n"</span>, macro_d); |
| 160 |
00152 |
| 161 |
00153 <span class="keywordflow">return</span>(NULL); |
| 162 |
00154 } |
| 163 |
00155 |
| 164 |
00156 ptr += 2; |
| 165 |
00157 length += 3; |
| 166 |
00158 <span class="keywordflow">break</span>; |
| 167 |
00159 |
| 168 |
00160 <span class="keywordflow">case</span> <span class="charliteral">'{'</span>: |
| 169 |
00161 |
| 170 |
00162 *ptr++ = <span class="charliteral">'\0'</span>; <span class="comment">/* % */</span> |
| 171 |
00163 *ptr++ = <span class="charliteral">'\0'</span>; <span class="comment">/* { */</span> |
| 172 |
00164 |
| 173 |
00165 <span class="keywordflow">if</span> ((i = <a class="code" href="util_8h.html#a35">UTIL_index</a>(ptr, <span class="charliteral">'}'</span>)) == 0) |
| 174 |
00166 { |
| 175 |
00167 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"'}' Invalid Macro (%c)\n"</span>, *(s + 1)); |
| 176 |
00168 |
| 177 |
00169 <span class="keywordflow">return</span>(NULL); <span class="comment">/* not closed, invalid macro */</span> |
| 178 |
00170 } |
| 179 |
00171 |
| 180 |
00172 *(ptr + i) = <span class="charliteral">'\0'</span>; <span class="comment">/* } */</span> |
| 181 |
00173 |
| 182 |
00174 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Actual macro (%s)\n"</span>, ptr); |
| 183 |
00175 <span class="keywordflow">if</span> ((macro = <a class="code" href="macro_8h.html#a2">MACRO_process</a>(p, ptr, (i + 1))) == NULL) |
| 184 |
00176 { |
| 185 |
00177 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"macro process returned null!\n"</span>); |
| 186 |
00178 } |
| 187 |
00179 <span class="keywordflow">else</span> |
| 188 |
00180 { |
| 189 |
00181 length += strlen(macro); |
| 190 |
00182 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Macro expanded to: (%s) %i bytes\n"</span>, macro, |
| 191 |
00183 strlen(macro)); |
| 192 |
00184 |
| 193 |
00185 <span class="keywordflow">if</span> (<a class="code" href="macro_8h.html#a4">MACRO_addbuf</a>(master, macro, strlen(macro)) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>) |
| 194 |
00186 { |
| 195 |
00187 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Unable to allocate list node with (%s)!\n"</span>, macro); |
| 196 |
00188 <a class="code" href="util_8h.html#a6">xfree</a>(macro); |
| 197 |
00189 |
| 198 |
00190 <span class="keywordflow">return</span>(NULL); <span class="comment">/* not closed, invalid macro */</span> |
| 199 |
00191 } |
| 200 |
00192 <a class="code" href="util_8h.html#a6">xfree</a>(macro); |
| 201 |
00193 } |
| 202 |
00194 ptr += i; |
| 203 |
00195 <span class="keywordflow">break</span>; |
| 204 |
00196 |
| 205 |
00197 <span class="keywordflow">default</span>: |
| 206 |
00198 |
| 207 |
00199 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"ERROR: Invalid macro. (%s) Abort!\n"</span>, *(ptr + 1)); |
| 208 |
00200 |
| 209 |
00201 <span class="comment">/* need cleanup function call perhaps */</span> |
| 210 |
00202 <span class="keywordflow">return</span>(NULL); |
| 211 |
00203 <span class="keywordflow">break</span>; |
| 212 |
00204 |
| 213 |
00205 } <span class="comment">/* switch */</span> |
| 214 |
00206 } <span class="comment">/* if */</span> |
| 215 |
00207 <span class="keywordflow">else</span> |
| 216 |
00208 { |
| 217 |
00209 <span class="keywordflow">if</span> ((i = <a class="code" href="util_8h.html#a35">UTIL_index</a>(ptr, <span class="charliteral">'%'</span>)) == 0) |
| 218 |
00210 { |
| 219 |
00211 <span class="keywordflow">while</span> (*(ptr + i)) |
| 220 |
00212 { |
| 221 |
00213 i++; |
| 222 |
00214 } |
| 223 |
00215 s_macro = <a class="code" href="util_8h.html#a4">xmalloc</a>(i + 1); |
| 224 |
00216 memcpy(s_macro, ptr, (i + 1)); |
| 225 |
00217 } |
| 226 |
00218 <span class="keywordflow">else</span> |
| 227 |
00219 { |
| 228 |
00220 s_macro = <a class="code" href="util_8h.html#a4">xmalloc</a>(i + 1); |
| 229 |
00221 memcpy(s_macro, ptr, i); |
| 230 |
00222 } |
| 231 |
00223 |
| 232 |
00224 length += i; |
| 233 |
00225 |
| 234 |
00226 <span class="keywordflow">if</span> (<a class="code" href="macro_8h.html#a4">MACRO_addbuf</a>(master, s_macro, (i + 1)) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>) |
| 235 |
00227 { |
| 236 |
00228 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Unable to allocate list node with (%s)!\n"</span>, s_macro); |
| 237 |
00229 |
| 238 |
00230 <span class="keywordflow">return</span>(NULL); |
| 239 |
00231 } |
| 240 |
00232 |
| 241 |
00233 ptr += (i - 1); |
| 242 |
00234 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Freeing s_macro temp buf (%s)\n"</span>, s_macro); |
| 243 |
00235 <a class="code" href="util_8h.html#a6">xfree</a>(s_macro); |
| 244 |
00236 } |
| 245 |
00237 ptr++; |
| 246 |
00238 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Remaining buffer (%s)\n"</span>, ptr); |
| 247 |
00239 } <span class="comment">/* while */</span> |
| 248 |
00240 |
| 249 |
00241 <a class="code" href="util_8h.html#a9">xprintf</a>(<span class="stringliteral">"Allocated %i bytes for return buf\n"</span>, length); |
| 250 |
00242 buf = <a class="code" href="util_8h.html#a4">xmalloc</a>(length + 1); |
| 251 |
00243 |
| 252 |
00244 c_node = master-><a class="code" href="structstrbuf__s.html#o0">head</a>; |
| 253 |
00245 <span class="keywordflow">while</span> (c_node != NULL) |
| 254 |
00246 { |
| 255 |
00247 kill_node = c_node; |
| 256 |
00248 |
| 257 |
00249 <span class="keywordflow">if</span> (kill_node-><a class="code" href="structstrbuf__node__s.html#o0">len</a> > 1) |
| 258 |
00250 { |
| 259 |
00251 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"NODE: (%s) LEN: %i\n"</span>, kill_node-><a class="code" href="structstrbuf__node__s.html#o1">s</a>, kill_node-><a class="code" href="structstrbuf__node__s.html#o0">len</a>); |
| 260 |
00252 } |
| 261 |
00253 <span class="keywordflow">else</span> |
| 262 |
00254 { |
| 263 |
00255 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"NODE: (%c) LEN: %i\n"</span>, kill_node-><a class="code" href="structstrbuf__node__s.html#o1">s</a>, kill_node-><a class="code" href="structstrbuf__node__s.html#o0">len</a>); |
| 264 |
00256 } |
| 265 |
00257 |
| 266 |
00258 strncat(buf, kill_node-><a class="code" href="structstrbuf__node__s.html#o1">s</a>, kill_node-><a class="code" href="structstrbuf__node__s.html#o0">len</a>); |
| 267 |
00259 <a class="code" href="util_8h.html#a6">xfree</a>(kill_node-><a class="code" href="structstrbuf__node__s.html#o1">s</a>); |
| 268 |
00260 c_node = c_node->next; |
| 269 |
00261 <a class="code" href="util_8h.html#a6">xfree</a>(kill_node); |
| 270 |
00262 } |
| 271 |
00263 |
| 272 |
00264 <a class="code" href="util_8h.html#a6">xfree</a>(cp); |
| 273 |
00265 <a class="code" href="util_8h.html#a6">xfree</a>(master); |
| 274 |
00266 |
| 275 |
00267 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Returning expanded macro: (%s)\n"</span>, buf); |
| 276 |
00268 |
| 277 |
00269 <span class="keywordflow">return</span>(buf); |
| 278 |
00270 } |
| 279 |
00271 |
| 280 |
00272 |
| 281 |
00273 <span class="comment">/* MACRO_process</span> |
| 282 |
00274 <span class="comment">*</span> |
| 283 |
00275 <span class="comment">* Author: James Couzens <jcouzens@codeshare.ca></span> |
| 284 |
00276 <span class="comment">*</span> |
| 285 |
00277 <span class="comment">* Date: 01/21/04</span> |
| 286 |
00278 <span class="comment">*</span> |
| 287 |
00279 <span class="comment">* Desc:</span> |
| 288 |
00280 <span class="comment">* This function takes a NULL terminated string containing only one</span> |
| 289 |
00281 <span class="comment">* macro. It returns a new string containing whatever that macro expanded to.</span> |
| 290 |
00282 <span class="comment">* Returns NULL if the macro can not be expanded.</span> |
| 291 |
00283 <span class="comment">*</span> |
| 292 |
00284 <span class="comment">* exists:%{ir}.%{l1r+-}._spf.%{d}</span> |
| 293 |
00285 <span class="comment">*</span> |
| 294 |
00286 <span class="comment">* Date: 08/??/04 - Travis Anderson <tanderson@codeshare.ca></span> |
| 295 |
00287 <span class="comment">*</span> |
| 296 |
00288 <span class="comment">* Desc:</span> |
| 297 |
00289 <span class="comment">* Fixed problematic 'p' macro with the addition of new DNS function</span> |
| 298 |
00290 <span class="comment">* DNS_check_client_reverse.</span> |
| 299 |
00291 <span class="comment">*</span> |
| 300 |
00292 <span class="comment">* Date: 09/11/04 - James Couzens <jcouzens@codeshare.ca></span> |
| 301 |
00293 <span class="comment">*</span> |
| 302 |
00294 <span class="comment">* Desc:</span> |
| 303 |
00295 <span class="comment">* Removed strlen calls originally intended for replacement</span> |
| 304 |
00296 <span class="comment">* with either a generic limit (SPF_MAX_STR), or a more stringent limit safely</span> |
| 305 |
00297 <span class="comment">* imposed upon the malloc.</span> |
| 306 |
00298 <span class="comment">* </span> |
| 307 |
00299 <span class="comment">*</span> |
| 308 |
00300 <span class="comment">*/</span> |
| 309 |
<a name="l00301"></a><a class="code" href="macro_8h.html#a2">00301</a> <span class="keywordtype">char</span> *<a class="code" href="macro_8h.html#a2">MACRO_process</a>(<a class="code" href="structpeer__info__s.html">peer_info_t</a> *p, <span class="keywordtype">char</span> *macro, <span class="keyword">const</span> size_t size) |
| 310 |
00302 { |
| 311 |
00303 <span class="keywordtype">int</span> c; <span class="comment">/* stores a lower case version of the macro if necessary */</span> |
| 312 |
00304 |
| 313 |
00305 size_t i; <span class="comment">/* utility for string lengths */</span> |
| 314 |
00306 |
| 315 |
00307 <span class="keywordtype">char</span> *rev_addr; <span class="comment">/* used by p macro */</span> |
| 316 |
00308 |
| 317 |
00309 |
| 318 |
00310 <span class="keywordflow">if</span> (macro == NULL) |
| 319 |
00311 { |
| 320 |
00312 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Passed a NULL string. Abort!\n"</span>); |
| 321 |
00313 |
| 322 |
00314 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>); |
| 323 |
00315 } |
| 324 |
00316 |
| 325 |
00317 <a class="code" href="util_8h.html#a9">xprintf</a>(<span class="stringliteral">"called with (%s) and len: %i\n"</span>,macro, size); |
| 326 |
00318 |
| 327 |
00319 rev_addr = NULL; |
| 328 |
00320 i = 0; |
| 329 |
00321 |
| 330 |
00322 <span class="keywordflow">if</span> (isupper(*macro)) |
| 331 |
00323 { |
| 332 |
00324 c = tolower(*macro); |
| 333 |
00325 } |
| 334 |
00326 <span class="keywordflow">else</span> |
| 335 |
00327 { |
| 336 |
00328 c = *macro; |
| 337 |
00329 } |
| 338 |
00330 |
| 339 |
00331 <span class="keywordflow">switch</span> (c) |
| 340 |
00332 { |
| 341 |
00333 <span class="comment">/* */</span> |
| 342 |
00334 <span class="keywordflow">case</span> <span class="charliteral">'d'</span>: |
| 343 |
00335 <span class="keywordflow">if</span> (*(macro + 1)) |
| 344 |
00336 { |
| 345 |
00337 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>)); |
| 346 |
00338 } |
| 347 |
00339 <span class="keywordflow">else</span> |
| 348 |
00340 { |
| 349 |
00341 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 'd' expands to: (%s)\n"</span>, |
| 350 |
00342 p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>); |
| 351 |
00343 |
| 352 |
00344 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>, <a class="code" href="spf_8h.html#a5">SPF_MAX_STR</a>)); |
| 353 |
00345 } |
| 354 |
00346 <span class="comment">/* */</span> |
| 355 |
00347 <span class="keywordflow">case</span> <span class="charliteral">'h'</span>: |
| 356 |
00348 <span class="keywordflow">if</span> (*(macro + 1)) |
| 357 |
00349 { |
| 358 |
00350 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o8">helo</a>)); |
| 359 |
00351 } |
| 360 |
00352 <span class="keywordflow">else</span> |
| 361 |
00353 { |
| 362 |
00354 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 'h' expands to: (%s)\n"</span>, p-><a class="code" href="structpeer__info__s.html#o8">helo</a>); |
| 363 |
00355 |
| 364 |
00356 <span class="keywordflow">if</span> (p-><a class="code" href="structpeer__info__s.html#o8">helo</a> != NULL) |
| 365 |
00357 { |
| 366 |
00358 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o8">helo</a>, <a class="code" href="spf_8h.html#a13">SPF_MAX_ENV_HELO</a>)); |
| 367 |
00359 } |
| 368 |
00360 <span class="keywordflow">else</span> |
| 369 |
00361 { |
| 370 |
00362 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o9">ehlo</a>, <a class="code" href="spf_8h.html#a13">SPF_MAX_ENV_HELO</a>)); |
| 371 |
00363 } |
| 372 |
00364 } |
| 373 |
00365 <span class="comment">/* */</span> |
| 374 |
00366 <span class="keywordflow">case</span> <span class="charliteral">'i'</span>: |
| 375 |
00367 <span class="keywordflow">if</span> (*(macro + 1)) |
| 376 |
00368 { |
| 377 |
00369 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o17">r_ip</a>)); |
| 378 |
00370 } |
| 379 |
00371 <span class="keywordflow">else</span> |
| 380 |
00372 { |
| 381 |
00373 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 'i' expands to: (%s)\n"</span>, p-><a class="code" href="structpeer__info__s.html#o17">r_ip</a>); |
| 382 |
00374 |
| 383 |
00375 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o17">r_ip</a>, <a class="code" href="spf_8h.html#a11">SPF_MAX_IP_ADDR</a>)); |
| 384 |
00376 } |
| 385 |
00377 <span class="comment">/* */</span> |
| 386 |
00378 <span class="keywordflow">case</span> <span class="charliteral">'l'</span>: |
| 387 |
00379 <span class="keywordflow">if</span> (*(macro + 1)) |
| 388 |
00380 { |
| 389 |
00381 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o21">local_part</a>)); |
| 390 |
00382 } |
| 391 |
00383 <span class="keywordflow">else</span> |
| 392 |
00384 { |
| 393 |
00385 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 'l' expands to: (%s)\n"</span>, p-><a class="code" href="structpeer__info__s.html#o21">local_part</a>); |
| 394 |
00386 |
| 395 |
00387 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o21">local_part</a>, <a class="code" href="spf_8h.html#a7">SPF_MAX_LOCAL_PART</a>)); |
| 396 |
00388 } |
| 397 |
00389 <span class="comment">/* */</span> |
| 398 |
00390 <span class="keywordflow">case</span> <span class="charliteral">'o'</span>: |
| 399 |
00391 <span class="keywordflow">if</span> (*(macro + 1)) |
| 400 |
00392 { |
| 401 |
00393 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>)); |
| 402 |
00394 } |
| 403 |
00395 <span class="keywordflow">else</span> |
| 404 |
00396 { |
| 405 |
00397 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 'o' expands to: (%s)\n"</span>, |
| 406 |
00398 p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>); |
| 407 |
00399 |
| 408 |
00400 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>, <a class="code" href="spf_8h.html#a5">SPF_MAX_STR</a>)); |
| 409 |
00401 } |
| 410 |
00402 <span class="comment">/* */</span> |
| 411 |
00403 <span class="keywordflow">case</span> <span class="charliteral">'p'</span>: |
| 412 |
00404 <span class="keywordflow">if</span> (<a class="code" href="dns_8h.html#a13">DNS_check_client_reverse</a>(p) == <a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>) |
| 413 |
00405 { |
| 414 |
00406 p-><a class="code" href="structpeer__info__s.html#o18">r_vhname</a> = <a class="code" href="util_8h.html#a4">xmalloc</a>(8); |
| 415 |
00407 snprintf(p-><a class="code" href="structpeer__info__s.html#o18">r_vhname</a>, 8, <span class="stringliteral">"unknown"</span>); |
| 416 |
00408 } |
| 417 |
00409 |
| 418 |
00410 <span class="keywordflow">if</span> (*(macro + 1)) |
| 419 |
00411 { |
| 420 |
00412 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 'p' expands to: (%s)\n"</span>, p-><a class="code" href="structpeer__info__s.html#o18">r_vhname</a>); |
| 421 |
00413 |
| 422 |
00414 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o18">r_vhname</a>)); |
| 423 |
00415 } |
| 424 |
00416 <span class="keywordflow">else</span> |
| 425 |
00417 { |
| 426 |
00418 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 'p' expands to: (%s)\n"</span>, p-><a class="code" href="structpeer__info__s.html#o18">r_vhname</a>); |
| 427 |
00419 |
| 428 |
00420 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o18">r_vhname</a>, <a class="code" href="spf_8h.html#a5">SPF_MAX_STR</a>)); |
| 429 |
00421 } |
| 430 |
00422 <span class="comment">/* */</span> |
| 431 |
00423 <span class="keywordflow">case</span> <span class="charliteral">'s'</span>: |
| 432 |
00424 |
| 433 |
00425 <span class="keywordflow">if</span> ((p-><a class="code" href="structpeer__info__s.html#o19">cur_eaddr</a> != NULL) || p-><a class="code" href="structpeer__info__s.html#o19">cur_eaddr</a>) |
| 434 |
00426 { |
| 435 |
00427 <a class="code" href="util_8h.html#a6">xfree</a>(p-><a class="code" href="structpeer__info__s.html#o19">cur_eaddr</a>); |
| 436 |
00428 } |
| 437 |
00429 |
| 438 |
00430 <a class="code" href="util_8h.html#a9">xprintf</a>(<span class="stringliteral">"local-part: (%s); current domain: (%s)\n"</span>, |
| 439 |
00431 p-><a class="code" href="structpeer__info__s.html#o21">local_part</a>, p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>); |
| 440 |
00432 i = ((strlen(p-><a class="code" href="structpeer__info__s.html#o21">local_part</a>) + strlen(p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>) + 2)); |
| 441 |
00433 p-><a class="code" href="structpeer__info__s.html#o19">cur_eaddr</a> = <a class="code" href="util_8h.html#a4">xmalloc</a>(i); |
| 442 |
00434 snprintf(p-><a class="code" href="structpeer__info__s.html#o19">cur_eaddr</a>, i, <span class="stringliteral">"%s@%s"</span>, |
| 443 |
00435 p-><a class="code" href="structpeer__info__s.html#o21">local_part</a>, p-><a class="code" href="structpeer__info__s.html#o15">current_domain</a>); |
| 444 |
00436 |
| 445 |
00437 <span class="keywordflow">if</span> (*(macro + 1)) |
| 446 |
00438 { |
| 447 |
00439 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o19">cur_eaddr</a>)); |
| 448 |
00440 } |
| 449 |
00441 <span class="keywordflow">else</span> |
| 450 |
00442 { |
| 451 |
00443 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 's' expands to: (%s)\n"</span>, p-><a class="code" href="structpeer__info__s.html#o19">cur_eaddr</a>); |
| 452 |
00444 |
| 453 |
00445 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o19">cur_eaddr</a>, <a class="code" href="spf_8h.html#a5">SPF_MAX_STR</a>)); |
| 454 |
00446 } |
| 455 |
00447 <span class="comment">/* */</span> |
| 456 |
00448 <span class="keywordflow">case</span> <span class="charliteral">'t'</span>: |
| 457 |
00449 <span class="keywordflow">if</span> (*(macro + 1)) |
| 458 |
00450 { |
| 459 |
00451 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o22">utc_time</a>)); |
| 460 |
00452 } |
| 461 |
00453 <span class="keywordflow">else</span> |
| 462 |
00454 { |
| 463 |
00455 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 't' expands to: (%s)\n"</span>, p-><a class="code" href="structpeer__info__s.html#o22">utc_time</a>); |
| 464 |
00456 |
| 465 |
00457 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o22">utc_time</a>, <a class="code" href="spf_8h.html#a10">SPF_MAX_UTC_TIME</a>)); |
| 466 |
00458 } |
| 467 |
00459 <span class="comment">/* */</span> |
| 468 |
00460 <span class="keywordflow">case</span> <span class="charliteral">'v'</span>: |
| 469 |
00461 <span class="keywordflow">if</span> (*(macro + 1)) |
| 470 |
00462 { |
| 471 |
00463 <span class="keywordflow">return</span>(<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(macro, p-><a class="code" href="structpeer__info__s.html#o20">ip_ver</a>)); |
| 472 |
00464 } |
| 473 |
00465 <span class="keywordflow">else</span> |
| 474 |
00466 { |
| 475 |
00467 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"macro 'v' expands to: (%s)\n"</span>, p-><a class="code" href="structpeer__info__s.html#o20">ip_ver</a>); |
| 476 |
00468 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o20">ip_ver</a>, <a class="code" href="spf_8h.html#a11">SPF_MAX_IP_ADDR</a>)); |
| 477 |
00469 } |
| 478 |
00470 <span class="comment">/* */</span> |
| 479 |
00471 <span class="keywordflow">case</span> <span class="charliteral">'x'</span>: |
| 480 |
00472 <span class="keywordflow">if</span> (size > 1) |
| 481 |
00473 { |
| 482 |
00474 <span class="keywordflow">if</span> (*(macro + 1) == <span class="charliteral">'R'</span> || *(macro + 1) == <span class="charliteral">'r'</span>) |
| 483 |
00475 { |
| 484 |
00476 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(p-><a class="code" href="structpeer__info__s.html#o16">mta_hname</a>, <a class="code" href="spf_8h.html#a14">SPF_MAX_HNAME</a>)); |
| 485 |
00477 } |
| 486 |
00478 } |
| 487 |
00479 <span class="keywordflow">break</span>; |
| 488 |
00480 |
| 489 |
00481 <span class="keywordflow">default</span>: |
| 490 |
00482 <span class="keywordflow">return</span>(<a class="code" href="util_8h.html#a8">xstrndup</a>(macro, <a class="code" href="spf_8h.html#a5">SPF_MAX_STR</a>)); |
| 491 |
00483 } <span class="comment">/* switch */</span> |
| 492 |
00484 |
| 493 |
00485 <span class="keywordflow">return</span>(NULL); |
| 494 |
00486 } |
| 495 |
00487 |
| 496 |
00488 |
| 497 |
00489 <span class="comment">/* MACRO_eatmore</span> |
| 498 |
00490 <span class="comment">*</span> |
| 499 |
00491 <span class="comment">* Author: James Couzens <jcouzens@codeshare.ca></span> |
| 500 |
00492 <span class="comment">*</span> |
| 501 |
00493 <span class="comment">* Date: 01/22/04</span> |
| 502 |
00494 <span class="comment">* Date: 07/02/04 James Couzens <jcouzens@codeshare.ca> (compiler warnings)</span> |
| 503 |
00495 <span class="comment">*</span> |
| 504 |
00496 <span class="comment">* Desc:</span> |
| 505 |
00497 <span class="comment">* This function is called whenever a macro is more than one character</span> |
| 506 |
00498 <span class="comment">* long and so we have to 'eatmore' ;-) macro is the unexpanded macro and</span> |
| 507 |
00499 <span class="comment">* s is the expanded string of the original macro would have been returned as</span> |
| 508 |
00500 <span class="comment">* had it been single. Returns the expanded macro using dynamically allocated</span> |
| 509 |
00501 <span class="comment">* memory upon success and in the process will FREE s after allocating more</span> |
| 510 |
00502 <span class="comment">* memory (if necessary) and copying it over. Returns NULL upon failure.</span> |
| 511 |
00503 <span class="comment">*</span> |
| 512 |
00504 <span class="comment">*/</span> |
| 513 |
<a name="l00505"></a><a class="code" href="macro_8h.html#a3">00505</a> <span class="keywordtype">char</span> *<a class="code" href="macro_8h.html#a3">MACRO_eatmore</a>(<span class="keywordtype">char</span> *macro, <span class="keywordtype">char</span> *s) |
| 514 |
00506 { |
| 515 |
00507 size_t i; <span class="comment">/* how much of the buffer we are using */</span> |
| 516 |
00508 |
| 517 |
00509 <span class="keywordtype">char</span> delim; <span class="comment">/* marco is a delimiter modifier */</span> |
| 518 |
00510 |
| 519 |
00511 <span class="keywordtype">char</span> *cp; <span class="comment">/* working pointer */</span> |
| 520 |
00512 <span class="keywordtype">char</span> *buf; <span class="comment">/* return buffer */</span> |
| 521 |
00513 <span class="keywordtype">char</span> *rev_str; <span class="comment">/* temporary buffer */</span> |
| 522 |
00514 <span class="keywordtype">char</span> *d_buf; <span class="comment">/* temporary buffer */</span> |
| 523 |
00515 |
| 524 |
00516 u_int8_t n_dot; <span class="comment">/* number of 'delimiter' items in a string */</span> |
| 525 |
00517 u_int8_t rev; <span class="comment">/* string to be reversed */</span> |
| 526 |
00518 u_int8_t digit; <span class="comment">/* macro is a digit modifier */</span> |
| 527 |
00519 |
| 528 |
00520 |
| 529 |
00521 <span class="keywordflow">if</span> (macro == NULL) |
| 530 |
00522 { |
| 531 |
00523 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Passed a NULL string. Abort!\n"</span>); |
| 532 |
00524 <span class="keywordflow">return</span>(NULL); |
| 533 |
00525 } |
| 534 |
00526 |
| 535 |
00527 <a class="code" href="util_8h.html#a9">xprintf</a>(<span class="stringliteral">"Called with macro (%s) and string (%s)\n"</span>, macro, s); |
| 536 |
00528 |
| 537 |
00529 <span class="comment">/* need a proper value for this not just SPF_MAX_MACRO need to know the MAXIMUM</span> |
| 538 |
00530 <span class="comment"> * expandable length of a macro. */</span> |
| 539 |
00531 |
| 540 |
00532 cp = macro; |
| 541 |
00533 rev = 0; |
| 542 |
00534 digit = 0; |
| 543 |
00535 delim = <span class="charliteral">'.'</span>; |
| 544 |
00536 buf = NULL; |
| 545 |
00537 rev_str = NULL; |
| 546 |
00538 |
| 547 |
00539 <span class="keywordflow">while</span> (*cp) |
| 548 |
00540 { |
| 549 |
00541 <span class="keywordflow">if</span> (isdigit(*cp)) |
| 550 |
00542 { |
| 551 |
00543 digit = atoi(cp); |
| 552 |
00544 } |
| 553 |
00545 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (<a class="code" href="util_8h.html#a39">UTIL_is_spf_delim</a>(*cp) == <a class="code" href="spf_8h.html#a87a50">SPF_TRUE</a>) |
| 554 |
00546 { |
| 555 |
00547 delim = *cp; |
| 556 |
00548 } |
| 557 |
00549 <span class="keywordflow">else</span> <span class="keywordflow">if</span> ((*cp == <span class="charliteral">'r'</span>) || (*cp == <span class="charliteral">'R'</span>)) |
| 558 |
00550 { |
| 559 |
00551 rev = 1; |
| 560 |
00552 } |
| 561 |
00553 cp++; |
| 562 |
00554 } |
| 563 |
00555 |
| 564 |
00556 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"mac:(%s) r:(%i) dig:(%i) dlm: (%c)\n"</span>, |
| 565 |
00557 macro, rev, digit, delim); |
| 566 |
00558 |
| 567 |
00559 i = 0; |
| 568 |
00560 <span class="comment">/* reverse the string */</span> |
| 569 |
00561 <span class="keywordflow">if</span> (rev == 1) |
| 570 |
00562 { |
| 571 |
00563 <span class="comment">/*delim = '.';*/</span> |
| 572 |
00564 rev_str = <a class="code" href="util_8h.html#a56">UTIL_reverse</a>(s, delim); |
| 573 |
00565 s = NULL; |
| 574 |
00566 } |
| 575 |
00567 |
| 576 |
00568 <span class="keywordflow">if</span> (s == NULL) |
| 577 |
00569 { |
| 578 |
00570 cp = rev_str; |
| 579 |
00571 } |
| 580 |
00572 <span class="keywordflow">else</span> |
| 581 |
00573 { |
| 582 |
00574 cp = s; |
| 583 |
00575 } |
| 584 |
00576 |
| 585 |
00577 <span class="comment">/* exercise digit modifier on string */</span> |
| 586 |
00578 <span class="keywordflow">if</span> (digit > 0) |
| 587 |
00579 { |
| 588 |
00580 n_dot = <a class="code" href="util_8h.html#a38">UTIL_count_delim</a>(cp, delim); |
| 589 |
00581 |
| 590 |
00582 <span class="keywordflow">if</span> (digit > n_dot) |
| 591 |
00583 { |
| 592 |
00584 digit = n_dot; |
| 593 |
00585 } |
| 594 |
00586 |
| 595 |
00587 <span class="keywordflow">if</span> ((d_buf = <a class="code" href="util_8h.html#a37">UTIL_split_strr</a>(cp, delim, digit)) != NULL) |
| 596 |
00588 { |
| 597 |
00589 i = strlen(d_buf); |
| 598 |
00590 } |
| 599 |
00591 <span class="keywordflow">else</span> |
| 600 |
00592 { |
| 601 |
00593 d_buf = cp; |
| 602 |
00594 i = strlen(d_buf); |
| 603 |
00595 } |
| 604 |
00596 |
| 605 |
00597 buf = <a class="code" href="util_8h.html#a4">xmalloc</a>(i + 1); |
| 606 |
00598 memcpy(buf, d_buf, (i + 1)); |
| 607 |
00599 |
| 608 |
00600 <span class="keywordflow">if</span> (d_buf != cp) |
| 609 |
00601 { |
| 610 |
00602 <a class="code" href="util_8h.html#a6">xfree</a>(d_buf); |
| 611 |
00603 } |
| 612 |
00604 } |
| 613 |
00605 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (rev == 1) |
| 614 |
00606 { |
| 615 |
00607 buf = <a class="code" href="util_8h.html#a8">xstrndup</a>(rev_str, <a class="code" href="spf_8h.html#a4">SPF_MAX_MACRO</a>); |
| 616 |
00608 } |
| 617 |
00609 |
| 618 |
00610 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Returning (%s) (%i bytes)\n"</span>, buf, strlen(buf)); |
| 619 |
00611 |
| 620 |
00612 <span class="keywordflow">if</span> (rev == 1) |
| 621 |
00613 { |
| 622 |
00614 <a class="code" href="util_8h.html#a6">xfree</a>(rev_str); |
| 623 |
00615 } |
| 624 |
00616 |
| 625 |
00617 <span class="keywordflow">return</span>(buf); |
| 626 |
00618 } |
| 627 |
00619 |
| 628 |
00620 |
| 629 |
00621 <span class="comment">/* MACRO_addbuf</span> |
| 630 |
00622 <span class="comment">*</span> |
| 631 |
00623 <span class="comment">* Author: Sean Comeau <scomeau@obscurity.org></span> |
| 632 |
00624 <span class="comment">* Author: James Couznes <jcouzens@codeshare.ca></span> |
| 633 |
00625 <span class="comment">*</span> |
| 634 |
00626 <span class="comment">* Date: 01/18/04</span> |
| 635 |
00627 <span class="comment">* Updated: 01/24/04</span> |
| 636 |
00628 <span class="comment">*</span> |
| 637 |
00629 <span class="comment">*</span> |
| 638 |
00630 <span class="comment">* Desc:</span> |
| 639 |
00631 <span class="comment">* Appends nodes to a master list which is passed of type</span> |
| 640 |
00632 <span class="comment">* strbuf_t. The nodes are of type strbuf_node_t and appended on</span> |
| 641 |
00633 <span class="comment">* the end and the list is reordered to reflect this. Returns</span> |
| 642 |
00634 <span class="comment">* SPF_TRUE upon success and SPF_FALSE</span> |
| 643 |
00635 <span class="comment">* upon failure.</span> |
| 644 |
00636 <span class="comment">*</span> |
| 645 |
00637 <span class="comment">*/</span> |
| 646 |
<a name="l00638"></a><a class="code" href="macro_8h.html#a4">00638</a> <a class="code" href="spf_8h.html#a35">SPF_BOOL</a> <a class="code" href="macro_8h.html#a4">MACRO_addbuf</a>(<a class="code" href="structstrbuf__s.html">strbuf_t</a> *master, <span class="keywordtype">char</span> *s, size_t size) |
| 647 |
00639 { |
| 648 |
00640 <a class="code" href="structstrbuf__node__s.html">strbuf_node_t</a> *c_node = NULL; <span class="comment">/* c_node working node */</span> |
| 649 |
00641 <a class="code" href="structstrbuf__node__s.html">strbuf_node_t</a> *new_node = NULL; <span class="comment">/* newly allocated node */</span> |
| 650 |
00642 <a class="code" href="structstrbuf__node__s.html">strbuf_node_t</a> *prev_node = NULL; <span class="comment">/* previous working node */</span> |
| 651 |
00643 |
| 652 |
00644 |
| 653 |
00645 <span class="keywordflow">if</span> (s == NULL) |
| 654 |
00646 { |
| 655 |
00647 <a class="code" href="util_8h.html#a13">xepprintf</a>(<span class="stringliteral">"Passed a NULL string. Abort!\n"</span>); |
| 656 |
00648 |
| 657 |
00649 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a49">SPF_FALSE</a>); |
| 658 |
00650 } |
| 659 |
00651 |
| 660 |
00652 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Called with (%s) %i (%i) bytes.\n"</span>, s, size, strlen(s)); |
| 661 |
00653 |
| 662 |
00654 new_node = <a class="code" href="util_8h.html#a4">xmalloc</a>(<a class="code" href="util_8h.html#a3">SIZEOF</a>(<a class="code" href="structstrbuf__node__s.html">strbuf_node_t</a>)); |
| 663 |
00655 new_node-><a class="code" href="structstrbuf__node__s.html#o1">s</a> = <a class="code" href="util_8h.html#a4">xmalloc</a>(size + 1); |
| 664 |
00656 |
| 665 |
00657 strncpy(new_node-><a class="code" href="structstrbuf__node__s.html#o1">s</a>, s, size); |
| 666 |
00658 new_node-><a class="code" href="structstrbuf__node__s.html#o0">len</a> = size; |
| 667 |
00659 new_node-><a class="code" href="structstrbuf__node__s.html#o2">next</a> = NULL; |
| 668 |
00660 |
| 669 |
00661 <a class="code" href="util_8h.html#a10">xvprintf</a>(<span class="stringliteral">"Added (%s) to node of len: %i)\n"</span>, new_node-><a class="code" href="structstrbuf__node__s.html#o1">s</a>, |
| 670 |
00662 new_node-><a class="code" href="structstrbuf__node__s.html#o0">len</a>); |
| 671 |
00663 |
| 672 |
00664 prev_node = NULL; |
| 673 |
00665 c_node = master-><a class="code" href="structstrbuf__s.html#o0">head</a>; |
| 674 |
00666 |
| 675 |
00667 <span class="comment">/* reorder the list with the NEW element on the end */</span> |
| 676 |
00668 <span class="keywordflow">while</span> (c_node != NULL) |
| 677 |
00669 { |
| 678 |
00670 prev_node = c_node; |
| 679 |
00671 c_node = c_node-><a class="code" href="structstrbuf__node__s.html#o2">next</a>; |
| 680 |
00672 } |
| 681 |
00673 |
| 682 |
00674 <span class="keywordflow">if</span> (prev_node != NULL) |
| 683 |
00675 { |
| 684 |
00676 new_node-><a class="code" href="structstrbuf__node__s.html#o2">next</a> = prev_node-><a class="code" href="structstrbuf__node__s.html#o2">next</a>; |
| 685 |
00677 prev_node-><a class="code" href="structstrbuf__node__s.html#o2">next</a> = new_node; |
| 686 |
00678 } |
| 687 |
00679 <span class="keywordflow">else</span> |
| 688 |
00680 { |
| 689 |
00681 master-><a class="code" href="structstrbuf__s.html#o0">head</a> = new_node; |
| 690 |
00682 } |
| 691 |
00683 |
| 692 |
00684 master-><a class="code" href="structstrbuf__s.html#o1">elements</a>++; |
| 693 |
00685 |
| 694 |
00686 <span class="keywordflow">return</span>(<a class="code" href="spf_8h.html#a87a50">SPF_TRUE</a>); |
| 695 |
00687 } |
| 696 |
00688 |
| 697 |
00689 <span class="comment">/* end macro.c */</span> |
| 698 |
00690 |
| 699 |
</div></pre><hr size="1"><address style="align: right;"><small>Generated on Thu Sep 16 18:10:46 2004 for libSPF v1.0 by |
| 700 |
<a href="http://www.doxygen.org/index.html"> |
| 701 |
<img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.8 </small></address> |
| 702 |
</body> |
| 703 |
</html> |