⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regfree.html

📁 posix标准英文,html格式
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<dt>REG_ERANGE</dt><dd>Invalid endpoint in range expression.</dd><dt>REG_ESPACE</dt><dd>Out of memory.</dd><dt>REG_BADRPT</dt><dd><tt>'?'</tt>, <tt>'*'</tt>, or <tt>'+'</tt> not preceded by valid regular expression.</dd></dl><p>The <i>regerror</i>() function provides a mapping from error codes returned by <i>regcomp</i>() and <i>regexec</i>() tounspecified printable strings. It generates a string corresponding to the value of the <i>errcode</i> argument, which theapplication shall ensure is the last non-zero value returned by <i>regcomp</i>() or <i>regexec</i>() with the given value of<i>preg</i>. If <i>errcode</i> is not such a value, the content of the generated string is unspecified.</p><p>If <i>preg</i> is a null pointer, but <i>errcode</i> is a value returned by a previous call to <i>regexec</i>() or<i>regcomp</i>(), the <i>regerror</i>() still generates an error string corresponding to the value of <i>errcode</i>, but it mightnot be as detailed under some implementations.</p><p>If the <i>errbuf_size</i> argument is not 0, <i>regerror</i>() shall place the generated string into the buffer of size<i>errbuf_size</i> bytes pointed to by <i>errbuf</i>. If the string (including the terminating null) cannot fit in the buffer,<i>regerror</i>() shall truncate the string and null-terminate the result.</p><p>If <i>errbuf_size</i> is 0, <i>regerror</i>() shall ignore the <i>errbuf</i> argument, and return the size of the buffer neededto hold the generated string.</p><p>If the <i>preg</i> argument to <i>regexec</i>() or <i>regfree</i>() is not a compiled regular expression returned by<i>regcomp</i>(), the result is undefined. A <i>preg</i> is no longer treated as a compiled regular expression after it is given to<i>regfree</i>().</p></blockquote><h4><a name="tag_03_603_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, the <i>regcomp</i>() function shall return 0. Otherwise, it shall return an integer value indicatingan error as described in <a href="../basedefs/regex.h.html"><i>&lt;regex.h&gt;</i></a>, and the content of <i>preg</i> isundefined. If a code is returned, the interpretation shall be as given in <a href="../basedefs/regex.h.html"><i>&lt;regex.h&gt;</i></a>.</p><p>If <i>regcomp</i>() detects an invalid RE, it may return REG_BADPAT, or it may return one of the error codes that more preciselydescribes the error.</p><p>Upon successful completion, the <i>regexec</i>() function shall return 0. Otherwise, it shall return REG_NOMATCH to indicate nomatch.</p><p>Upon successful completion, the <i>regerror</i>() function shall return the number of bytes needed to hold the entire generatedstring, including the null termination. If the return value is greater than <i>errbuf_size</i>, the string returned in the bufferpointed to by <i>errbuf</i> has been truncated.</p><p>The <i>regfree</i>() function shall not return a value.</p></blockquote><h4><a name="tag_03_603_05"></a>ERRORS</h4><blockquote><p>No errors are defined.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_603_06"></a>EXAMPLES</h4><blockquote><pre><tt>#include &lt;regex.h&gt;<br>/* * Match string against the extended regular expression in * pattern, treating errors as no match. * * Return 1 for match, 0 for no match. */<br>intmatch(const char *string, char *pattern){    int    status;    regex_t    re;<br>    if (regcomp(&amp;re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {        return(0);      /* Report error. */    }    status = regexec(&amp;re, string, (size_t) 0, NULL, 0);    regfree(&amp;re);    if (status != 0) {        return(0);      /* Report error. */    }    return(1);}</tt></pre><p>The following demonstrates how the REG_NOTBOL flag could be used with <i>regexec</i>() to find all substrings in a line thatmatch a pattern supplied by a user. (For simplicity of the example, very little error checking is done.)</p><pre><tt>(void) regcomp (&amp;re, pattern, 0);/* This call to regexec() finds the first match on the line. */error = regexec (&amp;re, &amp;buffer[0], 1, &amp;pm, 0);while (error == 0) {  /* While matches found. */    /* Substring found between pm.rm_so and pm.rm_eo. */    /* This call to regexec() finds the next match. */    error = regexec (&amp;re, buffer + pm.rm_eo, 1, &amp;pm, REG_NOTBOL);}</tt></pre></blockquote><h4><a name="tag_03_603_07"></a>APPLICATION USAGE</h4><blockquote><p>An application could use:</p><pre><tt>regerror(code,preg,(char *)NULL,(size_t)0)</tt></pre><p>to find out how big a buffer is needed for the generated string, <a href="../functions/malloc.html"><i>malloc</i>()</a> a bufferto hold the string, and then call <i>regerror</i>() again to get the string. Alternatively, it could allocate a fixed, staticbuffer that is big enough to hold most strings, and then use <a href="../functions/malloc.html"><i>malloc</i>()</a> to allocate alarger buffer if it finds that this is too small.</p><p>To match a pattern as described in the Shell and Utilities volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../utilities/xcu_chap02.html#tag_02_13">Section 2.13, Pattern Matching Notation</a>, use the <a href="../functions/fnmatch.html"><i>fnmatch</i>()</a> function.</p></blockquote><h4><a name="tag_03_603_08"></a>RATIONALE</h4><blockquote><p>The <i>regexec</i>() function must fill in all <i>nmatch</i> elements of <i>pmatch</i>, where <i>nmatch</i> and <i>pmatch</i>are supplied by the application, even if some elements of <i>pmatch</i> do not correspond to subexpressions in <i>pattern</i>. Theapplication writer should note that there is probably no reason for using a value of <i>nmatch</i> that is larger than<i>preg</i>-&gt; <i>re_nsub</i>+1.</p><p>The REG_NEWLINE flag supports a use of RE matching that is needed in some applications like text editors. In such applications,the user supplies an RE asking the application to find a line that matches the given expression. An anchor in such an RE anchors atthe beginning or end of any line. Such an application can pass a sequence of &lt;newline&gt;-separated lines to <i>regexec</i>() asa single long string and specify REG_NEWLINE to <i>regcomp</i>() to get the desired behavior. The application must ensure thatthere are no explicit &lt;newline&gt;s in <i>pattern</i> if it wants to ensure that any match occurs entirely within a singleline.</p><p>The REG_NEWLINE flag affects the behavior of <i>regexec</i>(), but it is in the <i>cflags</i> parameter to <i>regcomp</i>() toallow flexibility of implementation. Some implementations will want to generate the same compiled RE in <i>regcomp</i>() regardlessof the setting of REG_NEWLINE and have <i>regexec</i>() handle anchors differently based on the setting of the flag. Otherimplementations will generate different compiled REs based on the REG_NEWLINE.</p><p>The REG_ICASE flag supports the operations taken by the <a href="../utilities/grep.html"><i>grep</i></a> <b>-i</b> option andthe historical implementations of <a href="../utilities/ex.html"><i>ex</i></a> and <a href="../utilities/vi.html"><i>vi</i></a>.Including this flag will make it easier for application code to be written that does the same thing as these utilities.</p><p>The substrings reported in <i>pmatch</i>[] are defined using offsets from the start of the string rather than pointers. Sincethis is a new interface, there should be no impact on historical implementations or applications, and offsets should be just aseasy to use as pointers. The change to offsets was made to facilitate future extensions in which the string to be searched ispresented to <i>regexec</i>() in blocks, allowing a string to be searched that is not all in memory at once.</p><p>The type <b>regoff_t</b> is used for the elements of <i>pmatch</i>[] to ensure that the application can represent either thelargest possible array in memory (important for an application conforming to the Shell and Utilities volume ofIEEE&nbsp;Std&nbsp;1003.1-2001) or the largest possible file (important for an application using the extension where a file issearched in chunks).</p><p>The standard developers rejected the inclusion of a <i>regsub</i>() function that would be used to do substitutions for amatched RE. While such a routine would be useful to some applications, its utility would be much more limited than the matchingfunction described here. Both RE parsing and substitution are possible to implement without support other than that required by theISO&nbsp;C standard, but matching is much more complex than substituting. The only difficult part of substitution, given theinformation supplied by <i>regexec</i>(), is finding the next character in a string when there can be multi-byte characters. Thatis a much larger issue, and one that needs a more general solution.</p><p>The <i>errno</i> variable has not been used for error returns to avoid filling the <i>errno</i> name space for this feature.</p><p>The interface is defined so that the matched substrings <i>rm_sp</i> and <i>rm_ep</i> are in a separate <b>regmatch_t</b>structure instead of in <b>regex_t</b>. This allows a single compiled RE to be used simultaneously in several contexts; in<i>main</i>() and a signal handler, perhaps, or in multiple threads of lightweight processes. (The <i>preg</i> argument to<i>regexec</i>() is declared with type <b>const</b>, so the implementation is not permitted to use the structure to storeintermediate results.) It also allows an application to request an arbitrary number of substrings from an RE. The number ofsubexpressions in the RE is reported in <i>re_nsub</i> in <i>preg</i>. With this change to <i>regexec</i>(), consideration wasgiven to dropping the REG_NOSUB flag since the user can now specify this with a zero <i>nmatch</i> argument to <i>regexec</i>().However, keeping REG_NOSUB allows an implementation to use a different (perhaps more efficient) algorithm if it knows in<i>regcomp</i>() that no subexpressions need be reported. The implementation is only required to fill in <i>pmatch</i> if<i>nmatch</i> is not zero and if REG_NOSUB is not specified. Note that the <b>size_t</b> type, as defined in the ISO&nbsp;Cstandard, is unsigned, so the description of <i>regexec</i>() does not need to address negative values of <i>nmatch</i>.</p><p>REG_NOTBOL was added to allow an application to do repeated searches for the same pattern in a line. If the pattern contains acircumflex character that should match the beginning of a line, then the pattern should only match when matched against thebeginning of the line. Without the REG_NOTBOL flag, the application could rewrite the expression for subsequent matches, but in thegeneral case this would require parsing the expression. The need for REG_NOTEOL is not as clear; it was added for symmetry.</p><p>The addition of the <i>regerror</i>() function addresses the historical need for conforming application programs to have accessto error information more than &quot;Function failed to compile/match your RE for unknown reasons&quot;.</p><p>This interface provides for two different methods of dealing with error conditions. The specific error codes (REG_EBRACE, forexample), defined in <a href="../basedefs/regex.h.html"><i>&lt;regex.h&gt;</i></a>, allow an application to recover from an errorif it is so able. Many applications, especially those that use patterns supplied by a user, will not try to deal with specificerror cases, but will just use <i>regerror</i>() to obtain a human-readable error message to present to the user.</p><p>The <i>regerror</i>() function uses a scheme similar to <a href="../functions/confstr.html"><i>confstr</i>()</a> to deal withthe problem of allocating memory to hold the generated string. The scheme used by <a href="../functions/strerror.html"><i>strerror</i>()</a> in the ISO&nbsp;C standard was considered unacceptable since it createsdifficulties for multi-threaded applications.</p><p>The <i>preg</i> argument is provided to <i>regerror</i>() to allow an implementation to generate a more descriptive message thanwould be possible with <i>errcode</i> alone. An implementation might, for example, save the character offset of the offendingcharacter of the pattern in a field of <i>preg</i>, and then include that in the generated message string. The implementation mayalso ignore <i>preg</i>.</p><p>A REG_FILENAME flag was considered, but omitted. This flag caused <i>regexec</i>() to match patterns as described in the Shelland Utilities volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../utilities/xcu_chap02.html#tag_02_13">Section 2.13, PatternMatching Notation</a> instead of REs. This service is now provided by the <a href="../functions/fnmatch.html"><i>fnmatch</i>()</a>function.</p><p>Notice that there is a difference in philosophy between the ISO&nbsp;POSIX-2:1993 standard and IEEE&nbsp;Std&nbsp;1003.1-2001 inhow to handle a &quot;bad&quot; regular expression. The ISO&nbsp;POSIX-2:1993 standard says that many bad constructs &quot;produce undefinedresults&quot;, or that &quot;the interpretation is undefined&quot;. IEEE&nbsp;Std&nbsp;1003.1-2001, however, says that the interpretation ofsuch REs is unspecified. The term &quot;undefined&quot; means that the action by the application is an error, of similar severity topassing a bad pointer to a function.</p><p>The <i>regcomp</i>() and <i>regexec</i>() functions are required to accept any null-terminated string as the <i>pattern</i>argument. If the meaning of the string is &quot;undefined&quot;, the behavior of the function is &quot;unspecified&quot;.IEEE&nbsp;Std&nbsp;1003.1-2001 does not specify how the functions will interpret the pattern; they might return error codes, orthey might do pattern matching in some completely unexpected way, but they should not do something like abort the process.</p></blockquote><h4><a name="tag_03_603_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_603_10"></a>SEE ALSO</h4><blockquote><p><a href="fnmatch.html"><i>fnmatch</i>()</a>, <a href="glob.html"><i>glob</i>()</a>, Shell and Utilities volume ofIEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../utilities/xcu_chap02.html#tag_02_13">Section 2.13, Pattern Matching Notation</a>, BaseDefinitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap09.html">Chapter 9, Regular Expressions</a>, <ahref="../basedefs/regex.h.html"><i>&lt;regex.h&gt;</i></a>, <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a></p></blockquote><h4><a name="tag_03_603_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 4. Derived from the ISO&nbsp;POSIX-2 standard.</p></blockquote><h4><a name="tag_03_603_12"></a>Issue 5</h4><blockquote><p>Moved from POSIX2 C-language Binding to BASE.</p></blockquote><h4><a name="tag_03_603_13"></a>Issue 6</h4><blockquote><p>In the SYNOPSIS, the optional include of the <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> header isremoved.</p><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>The requirement to include <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> has been removed. Although <ahref="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> was required for conforming implementations of previous POSIXspecifications, it was not required for UNIX applications.</p></li></ul><p>The DESCRIPTION is updated to avoid use of the term &quot;must&quot; for application requirements.</p><p>The REG_ENOSYS constant is removed.</p><p>The <b>restrict</b> keyword is added to the <i>regcomp</i>(), <i>regerror</i>(), and <i>regexec</i>() prototypes for alignmentwith the ISO/IEC&nbsp;9899:1999 standard.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -