pattern.texi 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  1. @node Pattern Matching, I/O Overview, Searching and Sorting, Top
  2. @c %MENU% Matching shell ``globs'' and regular expressions
  3. @chapter Pattern Matching
  4. @Theglibc{} provides pattern matching facilities for two kinds of
  5. patterns: regular expressions and file-name wildcards. The library also
  6. provides a facility for expanding variable and command references and
  7. parsing text into words in the way the shell does.
  8. @menu
  9. * Wildcard Matching:: Matching a wildcard pattern against a single string.
  10. * Globbing:: Finding the files that match a wildcard pattern.
  11. * Regular Expressions:: Matching regular expressions against strings.
  12. * Word Expansion:: Expanding shell variables, nested commands,
  13. arithmetic, and wildcards.
  14. This is what the shell does with shell commands.
  15. @end menu
  16. @node Wildcard Matching
  17. @section Wildcard Matching
  18. @pindex fnmatch.h
  19. This section describes how to match a wildcard pattern against a
  20. particular string. The result is a yes or no answer: does the
  21. string fit the pattern or not. The symbols described here are all
  22. declared in @file{fnmatch.h}.
  23. @deftypefun int fnmatch (const char *@var{pattern}, const char *@var{string}, int @var{flags})
  24. @standards{POSIX.2, fnmatch.h}
  25. @safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  26. @c fnmatch @mtsenv @mtslocale @ascuheap @acsmem
  27. @c strnlen dup ok
  28. @c mbsrtowcs
  29. @c memset dup ok
  30. @c malloc dup @ascuheap @acsmem
  31. @c mbsinit dup ok
  32. @c free dup @ascuheap @acsmem
  33. @c FCT = internal_fnwmatch @mtsenv @mtslocale @ascuheap @acsmem
  34. @c FOLD @mtslocale
  35. @c towlower @mtslocale
  36. @c EXT @mtsenv @mtslocale @ascuheap @acsmem
  37. @c STRLEN = wcslen dup ok
  38. @c getenv @mtsenv
  39. @c malloc dup @ascuheap @acsmem
  40. @c MEMPCPY = wmempcpy dup ok
  41. @c FCT dup @mtsenv @mtslocale @ascuheap @acsmem
  42. @c STRCAT = wcscat dup ok
  43. @c free dup @ascuheap @acsmem
  44. @c END @mtsenv
  45. @c getenv @mtsenv
  46. @c MEMCHR = wmemchr dup ok
  47. @c getenv @mtsenv
  48. @c IS_CHAR_CLASS = is_char_class @mtslocale
  49. @c wctype @mtslocale
  50. @c BTOWC ok
  51. @c ISWCTYPE ok
  52. @c auto findidx dup ok
  53. @c elem_hash dup ok
  54. @c memcmp dup ok
  55. @c collseq_table_lookup dup ok
  56. @c NO_LEADING_PERIOD ok
  57. This function tests whether the string @var{string} matches the pattern
  58. @var{pattern}. It returns @code{0} if they do match; otherwise, it
  59. returns the nonzero value @code{FNM_NOMATCH}. The arguments
  60. @var{pattern} and @var{string} are both strings.
  61. The argument @var{flags} is a combination of flag bits that alter the
  62. details of matching. See below for a list of the defined flags.
  63. In @theglibc{}, @code{fnmatch} might sometimes report ``errors'' by
  64. returning nonzero values that are not equal to @code{FNM_NOMATCH}.
  65. @end deftypefun
  66. These are the available flags for the @var{flags} argument:
  67. @vtable @code
  68. @item FNM_FILE_NAME
  69. @standards{GNU, fnmatch.h}
  70. Treat the @samp{/} character specially, for matching file names. If
  71. this flag is set, wildcard constructs in @var{pattern} cannot match
  72. @samp{/} in @var{string}. Thus, the only way to match @samp{/} is with
  73. an explicit @samp{/} in @var{pattern}.
  74. @item FNM_PATHNAME
  75. @standards{POSIX.2, fnmatch.h}
  76. This is an alias for @code{FNM_FILE_NAME}; it comes from POSIX.2. We
  77. don't recommend this name because we don't use the term ``pathname'' for
  78. file names.
  79. @item FNM_PERIOD
  80. @standards{POSIX.2, fnmatch.h}
  81. Treat the @samp{.} character specially if it appears at the beginning of
  82. @var{string}. If this flag is set, wildcard constructs in @var{pattern}
  83. cannot match @samp{.} as the first character of @var{string}.
  84. If you set both @code{FNM_PERIOD} and @code{FNM_FILE_NAME}, then the
  85. special treatment applies to @samp{.} following @samp{/} as well as to
  86. @samp{.} at the beginning of @var{string}. (The shell uses the
  87. @code{FNM_PERIOD} and @code{FNM_FILE_NAME} flags together for matching
  88. file names.)
  89. @item FNM_NOESCAPE
  90. @standards{POSIX.2, fnmatch.h}
  91. Don't treat the @samp{\} character specially in patterns. Normally,
  92. @samp{\} quotes the following character, turning off its special meaning
  93. (if any) so that it matches only itself. When quoting is enabled, the
  94. pattern @samp{\?} matches only the string @samp{?}, because the question
  95. mark in the pattern acts like an ordinary character.
  96. If you use @code{FNM_NOESCAPE}, then @samp{\} is an ordinary character.
  97. @item FNM_LEADING_DIR
  98. @standards{GNU, fnmatch.h}
  99. Ignore a trailing sequence of characters starting with a @samp{/} in
  100. @var{string}; that is to say, test whether @var{string} starts with a
  101. directory name that @var{pattern} matches.
  102. If this flag is set, either @samp{foo*} or @samp{foobar} as a pattern
  103. would match the string @samp{foobar/frobozz}.
  104. @item FNM_CASEFOLD
  105. @standards{POSIX.1-2024, fnmatch.h}
  106. Ignore case in comparing @var{string} to @var{pattern}.
  107. This macro was originally a GNU extension, but was added in
  108. POSIX.1-2024.
  109. @item FNM_EXTMATCH
  110. @standards{GNU, fnmatch.h}
  111. @cindex Korn Shell
  112. @pindex ksh
  113. Besides the normal patterns, also recognize the extended patterns
  114. introduced in @file{ksh}. The patterns are written in the form
  115. explained in the following table where @var{pattern-list} is a @code{|}
  116. separated list of patterns.
  117. @table @code
  118. @item ?(@var{pattern-list})
  119. The pattern matches if zero or one occurrences of any of the patterns
  120. in the @var{pattern-list} allow matching the input string.
  121. @item *(@var{pattern-list})
  122. The pattern matches if zero or more occurrences of any of the patterns
  123. in the @var{pattern-list} allow matching the input string.
  124. @item +(@var{pattern-list})
  125. The pattern matches if one or more occurrences of any of the patterns
  126. in the @var{pattern-list} allow matching the input string.
  127. @item @@(@var{pattern-list})
  128. The pattern matches if exactly one occurrence of any of the patterns in
  129. the @var{pattern-list} allows matching the input string.
  130. @item !(@var{pattern-list})
  131. The pattern matches if the input string cannot be matched with any of
  132. the patterns in the @var{pattern-list}.
  133. @end table
  134. @end vtable
  135. @node Globbing
  136. @section Globbing
  137. @cindex globbing
  138. The archetypal use of wildcards is for matching against the files in a
  139. directory, and making a list of all the matches. This is called
  140. @dfn{globbing}.
  141. You could do this using @code{fnmatch}, by reading the directory entries
  142. one by one and testing each one with @code{fnmatch}. But that would be
  143. slow (and complex, since you would have to handle subdirectories by
  144. hand).
  145. The library provides a function @code{glob} to make this particular use
  146. of wildcards convenient. @code{glob} and the other symbols in this
  147. section are declared in @file{glob.h}.
  148. @menu
  149. * Calling Glob:: Basic use of @code{glob}.
  150. * Flags for Globbing:: Flags that enable various options in @code{glob}.
  151. * More Flags for Globbing:: GNU specific extensions to @code{glob}.
  152. @end menu
  153. @node Calling Glob
  154. @subsection Calling @code{glob}
  155. The result of globbing is a vector of file names (strings). To return
  156. this vector, @code{glob} uses a special data type, @code{glob_t}, which
  157. is a structure. You pass @code{glob} the address of the structure, and
  158. it fills in the structure's fields to tell you about the results.
  159. @deftp {Data Type} glob_t
  160. @standards{POSIX.2, glob.h}
  161. This data type holds a pointer to a word vector. More precisely, it
  162. records both the address of the word vector and its size. The GNU
  163. implementation contains some more fields which are non-standard
  164. extensions.
  165. @table @code
  166. @item gl_pathc
  167. The number of elements in the vector, excluding the initial null entries
  168. if the GLOB_DOOFFS flag is used (see gl_offs below).
  169. @item gl_pathv
  170. The address of the vector. This field has type @w{@code{char **}}.
  171. @item gl_offs
  172. The offset of the first real element of the vector, from its nominal
  173. address in the @code{gl_pathv} field. Unlike the other fields, this
  174. is always an input to @code{glob}, rather than an output from it.
  175. If you use a nonzero offset, then that many elements at the beginning of
  176. the vector are left empty. (The @code{glob} function fills them with
  177. null pointers.)
  178. The @code{gl_offs} field is meaningful only if you use the
  179. @code{GLOB_DOOFFS} flag. Otherwise, the offset is always zero
  180. regardless of what is in this field, and the first real element comes at
  181. the beginning of the vector.
  182. @item gl_closedir
  183. The address of an alternative implementation of the @code{closedir}
  184. function. It is used if the @code{GLOB_ALTDIRFUNC} bit is set in
  185. the flag parameter. The type of this field is
  186. @w{@code{void (*) (void *)}}.
  187. This is a GNU extension.
  188. @item gl_readdir
  189. The address of an alternative implementation of the @code{readdir}
  190. function used to read the contents of a directory. It is used if the
  191. @code{GLOB_ALTDIRFUNC} bit is set in the flag parameter. The type of
  192. this field is @w{@code{struct dirent *(*) (void *)}}.
  193. An implementation of @code{gl_readdir} needs to initialize the following
  194. members of the @code{struct dirent} object:
  195. @table @code
  196. @item d_type
  197. This member should be set to the file type of the entry if it is known.
  198. Otherwise, the value @code{DT_UNKNOWN} can be used. The @code{glob}
  199. function may use the specified file type to avoid callbacks in cases
  200. where the file type indicates that the data is not required.
  201. @item d_ino
  202. This member needs to be non-zero, otherwise @code{glob} may skip the
  203. current entry and call the @code{gl_readdir} callback function again to
  204. retrieve another entry.
  205. @item d_name
  206. This member must be set to the name of the entry. It must be
  207. null-terminated.
  208. @end table
  209. The example below shows how to allocate a @code{struct dirent} object
  210. containing a given name.
  211. @smallexample
  212. @include mkdirent.c.texi
  213. @end smallexample
  214. The @code{glob} function reads the @code{struct dirent} members listed
  215. above and makes a copy of the file name in the @code{d_name} member
  216. immediately after the @code{gl_readdir} callback function returns.
  217. Future invocations of any of the callback functions may deallocate or
  218. reuse the buffer. It is the responsibility of the caller of the
  219. @code{glob} function to allocate and deallocate the buffer, around the
  220. call to @code{glob} or using the callback functions. For example, an
  221. application could allocate the buffer in the @code{gl_readdir} callback
  222. function, and deallocate it in the @code{gl_closedir} callback function.
  223. The @code{gl_readdir} member is a GNU extension.
  224. @item gl_opendir
  225. The address of an alternative implementation of the @code{opendir}
  226. function. It is used if the @code{GLOB_ALTDIRFUNC} bit is set in
  227. the flag parameter. The type of this field is
  228. @w{@code{void *(*) (const char *)}}.
  229. This is a GNU extension.
  230. @item gl_stat
  231. The address of an alternative implementation of the @code{stat} function
  232. to get information about an object in the filesystem. It is used if the
  233. @code{GLOB_ALTDIRFUNC} bit is set in the flag parameter. The type of
  234. this field is @w{@code{int (*) (const char *, struct stat *)}}.
  235. This is a GNU extension.
  236. @item gl_lstat
  237. The address of an alternative implementation of the @code{lstat}
  238. function to get information about an object in the filesystems, not
  239. following symbolic links. It is used if the @code{GLOB_ALTDIRFUNC} bit
  240. is set in the flag parameter. The type of this field is @code{@w{int
  241. (*) (const char *,} @w{struct stat *)}}.
  242. This is a GNU extension.
  243. @item gl_flags
  244. The flags used when @code{glob} was called. In addition, @code{GLOB_MAGCHAR}
  245. might be set. See @ref{Flags for Globbing} for more details.
  246. This is a GNU extension.
  247. @end table
  248. @end deftp
  249. For use in the @code{glob64} function @file{glob.h} contains another
  250. definition for a very similar type. @code{glob64_t} differs from
  251. @code{glob_t} only in the types of the members @code{gl_readdir},
  252. @code{gl_stat}, and @code{gl_lstat}.
  253. @deftp {Data Type} glob64_t
  254. @standards{GNU, glob.h}
  255. This data type holds a pointer to a word vector. More precisely, it
  256. records both the address of the word vector and its size. The GNU
  257. implementation contains some more fields which are non-standard
  258. extensions.
  259. @table @code
  260. @item gl_pathc
  261. The number of elements in the vector, excluding the initial null entries
  262. if the GLOB_DOOFFS flag is used (see gl_offs below).
  263. @item gl_pathv
  264. The address of the vector. This field has type @w{@code{char **}}.
  265. @item gl_offs
  266. The offset of the first real element of the vector, from its nominal
  267. address in the @code{gl_pathv} field. Unlike the other fields, this
  268. is always an input to @code{glob}, rather than an output from it.
  269. If you use a nonzero offset, then that many elements at the beginning of
  270. the vector are left empty. (The @code{glob} function fills them with
  271. null pointers.)
  272. The @code{gl_offs} field is meaningful only if you use the
  273. @code{GLOB_DOOFFS} flag. Otherwise, the offset is always zero
  274. regardless of what is in this field, and the first real element comes at
  275. the beginning of the vector.
  276. @item gl_closedir
  277. The address of an alternative implementation of the @code{closedir}
  278. function. It is used if the @code{GLOB_ALTDIRFUNC} bit is set in
  279. the flag parameter. The type of this field is
  280. @w{@code{void (*) (void *)}}.
  281. This is a GNU extension.
  282. @item gl_readdir
  283. The address of an alternative implementation of the @code{readdir64}
  284. function used to read the contents of a directory. It is used if the
  285. @code{GLOB_ALTDIRFUNC} bit is set in the flag parameter. The type of
  286. this field is @w{@code{struct dirent64 *(*) (void *)}}.
  287. This is a GNU extension.
  288. @item gl_opendir
  289. The address of an alternative implementation of the @code{opendir}
  290. function. It is used if the @code{GLOB_ALTDIRFUNC} bit is set in
  291. the flag parameter. The type of this field is
  292. @w{@code{void *(*) (const char *)}}.
  293. This is a GNU extension.
  294. @item gl_stat
  295. The address of an alternative implementation of the @code{stat64} function
  296. to get information about an object in the filesystem. It is used if the
  297. @code{GLOB_ALTDIRFUNC} bit is set in the flag parameter. The type of
  298. this field is @w{@code{int (*) (const char *, struct stat64 *)}}.
  299. This is a GNU extension.
  300. @item gl_lstat
  301. The address of an alternative implementation of the @code{lstat64}
  302. function to get information about an object in the filesystems, not
  303. following symbolic links. It is used if the @code{GLOB_ALTDIRFUNC} bit
  304. is set in the flag parameter. The type of this field is @code{@w{int
  305. (*) (const char *,} @w{struct stat64 *)}}.
  306. This is a GNU extension.
  307. @item gl_flags
  308. The flags used when @code{glob} was called. In addition, @code{GLOB_MAGCHAR}
  309. might be set. See @ref{Flags for Globbing} for more details.
  310. This is a GNU extension.
  311. @end table
  312. @end deftp
  313. @deftypefun int glob (const char *@var{pattern}, int @var{flags}, int (*@var{errfunc}) (const char *@var{filename}, int @var{error-code}), glob_t *@var{vector-ptr})
  314. @standards{POSIX.2, glob.h}
  315. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtsenv{} @mtascusig{:ALRM} @mtascutimer{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  316. @c glob @mtasurace:utent @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  317. @c strlen dup ok
  318. @c strchr dup ok
  319. @c malloc dup @ascuheap @acsmem
  320. @c mempcpy dup ok
  321. @c next_brace_sub ok
  322. @c free dup @ascuheap @acsmem
  323. @c globfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
  324. @c glob_pattern_p ok
  325. @c glob_pattern_type dup ok
  326. @c getenv dup @mtsenv
  327. @c GET_LOGIN_NAME_MAX ok
  328. @c getlogin_r dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  329. @c GETPW_R_SIZE_MAX ok
  330. @c getpwnam_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  331. @c realloc dup @ascuheap @acsmem
  332. @c memcpy dup ok
  333. @c memchr dup ok
  334. @c *pglob->gl_stat user-supplied
  335. @c stat64 dup ok
  336. @c S_ISDIR dup ok
  337. @c strdup dup @ascuheap @acsmem
  338. @c glob_pattern_type ok
  339. @c glob_in_dir @mtsenv @mtslocale @asucorrupt @ascuheap @acucorrupt @acsfd @acsmem
  340. @c strlen dup ok
  341. @c glob_pattern_type dup ok
  342. @c malloc dup @ascuheap @acsmem
  343. @c mempcpy dup ok
  344. @c *pglob->gl_stat user-supplied
  345. @c stat64 dup ok
  346. @c free dup @ascuheap @acsmem
  347. @c *pglob->gl_opendir user-supplied
  348. @c opendir dup @ascuheap @acsmem @acsfd
  349. @c dirfd dup ok
  350. @c *pglob->gl_readdir user-supplied
  351. @c CONVERT_DIRENT_DIRENT64 ok
  352. @c readdir64 ok [protected by exclusive use of the stream]
  353. @c REAL_DIR_ENTRY ok
  354. @c DIRENT_MIGHT_BE_DIR ok
  355. @c fnmatch dup @mtsenv @mtslocale @ascuheap @acsmem
  356. @c DIRENT_MIGHT_BE_SYMLINK ok
  357. @c link_exists_p ok
  358. @c link_exists2_p ok
  359. @c strlen dup ok
  360. @c mempcpy dup ok
  361. @c *pglob->gl_stat user-supplied
  362. @c fxstatat64 dup ok
  363. @c realloc dup @ascuheap @acsmem
  364. @c pglob->gl_closedir user-supplied
  365. @c closedir @ascuheap @acsmem @acsfd
  366. @c prefix_array dup @asucorrupt @ascuheap @acucorrupt @acsmem
  367. @c strlen dup ok
  368. @c malloc dup @ascuheap @acsmem
  369. @c free dup @ascuheap @acsmem
  370. @c mempcpy dup ok
  371. @c strcpy dup ok
  372. The function @code{glob} does globbing using the pattern @var{pattern}
  373. in the current directory. It puts the result in a newly allocated
  374. vector, and stores the size and address of this vector into
  375. @code{*@var{vector-ptr}}. The argument @var{flags} is a combination of
  376. bit flags; see @ref{Flags for Globbing}, for details of the flags.
  377. The result of globbing is a sequence of file names. The function
  378. @code{glob} allocates a string for each resulting word, then
  379. allocates a vector of type @code{char **} to store the addresses of
  380. these strings. The last element of the vector is a null pointer.
  381. This vector is called the @dfn{word vector}.
  382. To return this vector, @code{glob} stores both its address and its
  383. length (number of elements, not counting the terminating null pointer)
  384. into @code{*@var{vector-ptr}}.
  385. Normally, @code{glob} sorts the file names alphabetically before
  386. returning them. You can turn this off with the flag @code{GLOB_NOSORT}
  387. if you want to get the information as fast as possible. Usually it's
  388. a good idea to let @code{glob} sort them---if you process the files in
  389. alphabetical order, the users will have a feel for the rate of progress
  390. that your application is making.
  391. If @code{glob} succeeds, it returns 0. Otherwise, it returns one
  392. of these error codes:
  393. @vtable @code
  394. @item GLOB_ABORTED
  395. @standards{POSIX.2, glob.h}
  396. There was an error opening a directory, and you used the flag
  397. @code{GLOB_ERR} or your specified @var{errfunc} returned a nonzero
  398. value.
  399. @iftex
  400. See below
  401. @end iftex
  402. @ifinfo
  403. @xref{Flags for Globbing},
  404. @end ifinfo
  405. for an explanation of the @code{GLOB_ERR} flag and @var{errfunc}.
  406. @item GLOB_NOMATCH
  407. @standards{POSIX.2, glob.h}
  408. The pattern didn't match any existing files. If you use the
  409. @code{GLOB_NOCHECK} flag, then you never get this error code, because
  410. that flag tells @code{glob} to @emph{pretend} that the pattern matched
  411. at least one file.
  412. @item GLOB_NOSPACE
  413. @standards{POSIX.2, glob.h}
  414. It was impossible to allocate memory to hold the result.
  415. @end vtable
  416. In the event of an error, @code{glob} stores information in
  417. @code{*@var{vector-ptr}} about all the matches it has found so far.
  418. It is important to notice that the @code{glob} function will not fail if
  419. it encounters directories or files which cannot be handled without the
  420. LFS interfaces. The implementation of @code{glob} is supposed to use
  421. these functions internally. This at least is the assumption made by
  422. the Unix standard. The GNU extension of allowing the user to provide their
  423. own directory handling and @code{stat} functions complicates things a
  424. bit. If these callback functions are used and a large file or directory
  425. is encountered @code{glob} @emph{can} fail.
  426. @end deftypefun
  427. @deftypefun int glob64 (const char *@var{pattern}, int @var{flags}, int (*@var{errfunc}) (const char *@var{filename}, int @var{error-code}), glob64_t *@var{vector-ptr})
  428. @standards{GNU, glob.h}
  429. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtsenv{} @mtascusig{:ALRM} @mtascutimer{} @mtslocale{}}@asunsafe{@ascudlopen{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  430. @c Same code as glob, but with glob64_t #defined as glob_t.
  431. The @code{glob64} function was added as part of the Large File Summit
  432. extensions but is not part of the original LFS proposal. The reason for
  433. this is simple: it is not necessary. The necessity for a @code{glob64}
  434. function is added by the extensions of the GNU @code{glob}
  435. implementation which allows the user to provide their own directory handling
  436. and @code{stat} functions. The @code{readdir} and @code{stat} functions
  437. do depend on the choice of @code{_FILE_OFFSET_BITS} since the definition
  438. of the types @code{struct dirent} and @code{struct stat} will change
  439. depending on the choice.
  440. Besides this difference, @code{glob64} works just like @code{glob} in
  441. all aspects.
  442. This function is a GNU extension.
  443. @end deftypefun
  444. @node Flags for Globbing
  445. @subsection Flags for Globbing
  446. This section describes the standard flags that you can specify in the
  447. @var{flags} argument to @code{glob}. Choose the flags you want,
  448. and combine them with the C bitwise OR operator @code{|}.
  449. Note that there are @ref{More Flags for Globbing} available as GNU extensions.
  450. @vtable @code
  451. @item GLOB_APPEND
  452. @standards{POSIX.2, glob.h}
  453. Append the words from this expansion to the vector of words produced by
  454. previous calls to @code{glob}. This way you can effectively expand
  455. several words as if they were concatenated with spaces between them.
  456. In order for appending to work, you must not modify the contents of the
  457. word vector structure between calls to @code{glob}. And, if you set
  458. @code{GLOB_DOOFFS} in the first call to @code{glob}, you must also
  459. set it when you append to the results.
  460. Note that the pointer stored in @code{gl_pathv} may no longer be valid
  461. after you call @code{glob} the second time, because @code{glob} might
  462. have relocated the vector. So always fetch @code{gl_pathv} from the
  463. @code{glob_t} structure after each @code{glob} call; @strong{never} save
  464. the pointer across calls.
  465. @item GLOB_DOOFFS
  466. @standards{POSIX.2, glob.h}
  467. Leave blank slots at the beginning of the vector of words.
  468. The @code{gl_offs} field says how many slots to leave.
  469. The blank slots contain null pointers.
  470. @item GLOB_ERR
  471. @standards{POSIX.2, glob.h}
  472. Give up right away and report an error if there is any difficulty
  473. reading the directories that must be read in order to expand @var{pattern}
  474. fully. Such difficulties might include a directory in which you don't
  475. have the requisite access. Normally, @code{glob} tries its best to keep
  476. on going despite any errors, reading whatever directories it can.
  477. You can exercise even more control than this by specifying an
  478. error-handler function @var{errfunc} when you call @code{glob}. If
  479. @var{errfunc} is not a null pointer, then @code{glob} doesn't give up
  480. right away when it can't read a directory; instead, it calls
  481. @var{errfunc} with two arguments, like this:
  482. @smallexample
  483. (*@var{errfunc}) (@var{filename}, @var{error-code})
  484. @end smallexample
  485. @noindent
  486. The argument @var{filename} is the name of the directory that
  487. @code{glob} couldn't open or couldn't read, and @var{error-code} is the
  488. @code{errno} value that was reported to @code{glob}.
  489. If the error handler function returns nonzero, then @code{glob} gives up
  490. right away. Otherwise, it continues.
  491. @item GLOB_MARK
  492. @standards{POSIX.2, glob.h}
  493. If the pattern matches the name of a directory, append @samp{/} to the
  494. directory's name when returning it.
  495. @item GLOB_NOCHECK
  496. @standards{POSIX.2, glob.h}
  497. If the pattern doesn't match any file names, return the pattern itself
  498. as if it were a file name that had been matched. (Normally, when the
  499. pattern doesn't match anything, @code{glob} returns that there were no
  500. matches.)
  501. @item GLOB_NOESCAPE
  502. @standards{POSIX.2, glob.h}
  503. Don't treat the @samp{\} character specially in patterns. Normally,
  504. @samp{\} quotes the following character, turning off its special meaning
  505. (if any) so that it matches only itself. When quoting is enabled, the
  506. pattern @samp{\?} matches only the string @samp{?}, because the question
  507. mark in the pattern acts like an ordinary character.
  508. If you use @code{GLOB_NOESCAPE}, then @samp{\} is an ordinary character.
  509. @code{glob} does its work by calling the function @code{fnmatch}
  510. repeatedly. It handles the flag @code{GLOB_NOESCAPE} by turning on the
  511. @code{FNM_NOESCAPE} flag in calls to @code{fnmatch}.
  512. @item GLOB_NOSORT
  513. @standards{POSIX.2, glob.h}
  514. Don't sort the file names; return them in no particular order.
  515. (In practice, the order will depend on the order of the entries in
  516. the directory.) The only reason @emph{not} to sort is to save time.
  517. @end vtable
  518. @node More Flags for Globbing
  519. @subsection More Flags for Globbing
  520. Beside the flags described in the last section, the GNU implementation of
  521. @code{glob} allows a few more flags which are also defined in the
  522. @file{glob.h} file. Some of the extensions implement functionality
  523. which is available in modern shell implementations.
  524. @vtable @code
  525. @item GLOB_PERIOD
  526. @standards{GNU, glob.h}
  527. The @code{.} character (period) is treated special. It cannot be
  528. matched by wildcards. @xref{Wildcard Matching}, @code{FNM_PERIOD}.
  529. @item GLOB_MAGCHAR
  530. @standards{GNU, glob.h}
  531. The @code{GLOB_MAGCHAR} value is not to be given to @code{glob} in the
  532. @var{flags} parameter. Instead, @code{glob} sets this bit in the
  533. @var{gl_flags} element of the @var{glob_t} structure provided as the
  534. result if the pattern used for matching contains any wildcard character.
  535. @item GLOB_ALTDIRFUNC
  536. @standards{GNU, glob.h}
  537. Instead of using the normal functions for accessing the
  538. filesystem the @code{glob} implementation uses the user-supplied
  539. functions specified in the structure pointed to by @var{pglob}
  540. parameter. For more information about the functions refer to the
  541. sections about directory handling see @ref{Accessing Directories}, and
  542. @ref{Reading Attributes}.
  543. @item GLOB_BRACE
  544. @standards{GNU, glob.h}
  545. If this flag is given, the handling of braces in the pattern is changed.
  546. It is now required that braces appear correctly grouped. I.e., for each
  547. opening brace there must be a closing one. Braces can be used
  548. recursively. So it is possible to define one brace expression in
  549. another one. It is important to note that the range of each brace
  550. expression is completely contained in the outer brace expression (if
  551. there is one).
  552. The string between the matching braces is separated into single
  553. expressions by splitting at @code{,} (comma) characters. The commas
  554. themselves are discarded. Please note what we said above about recursive
  555. brace expressions. The commas used to separate the subexpressions must
  556. be at the same level. Commas in brace subexpressions are not matched.
  557. They are used during expansion of the brace expression of the deeper
  558. level. The example below shows this
  559. @smallexample
  560. glob ("@{foo/@{,bar,biz@},baz@}", GLOB_BRACE, NULL, &result)
  561. @end smallexample
  562. @noindent
  563. is equivalent to the sequence
  564. @smallexample
  565. glob ("foo/", GLOB_BRACE, NULL, &result)
  566. glob ("foo/bar", GLOB_BRACE|GLOB_APPEND, NULL, &result)
  567. glob ("foo/biz", GLOB_BRACE|GLOB_APPEND, NULL, &result)
  568. glob ("baz", GLOB_BRACE|GLOB_APPEND, NULL, &result)
  569. @end smallexample
  570. @noindent
  571. if we leave aside error handling.
  572. @item GLOB_NOMAGIC
  573. @standards{GNU, glob.h}
  574. If the pattern contains no wildcard constructs (it is a literal file name),
  575. return it as the sole ``matching'' word, even if no file exists by that name.
  576. @item GLOB_TILDE
  577. @standards{GNU, glob.h}
  578. If this flag is used the character @code{~} (tilde) is handled specially
  579. if it appears at the beginning of the pattern. Instead of being taken
  580. verbatim it is used to represent the home directory of a known user.
  581. If @code{~} is the only character in pattern or it is followed by a
  582. @code{/} (slash), the home directory of the process owner is
  583. substituted. Using @code{getlogin} and @code{getpwnam} the information
  584. is read from the system databases. As an example take user @code{bart}
  585. with his home directory at @file{/home/bart}. For him a call like
  586. @smallexample
  587. glob ("~/bin/*", GLOB_TILDE, NULL, &result)
  588. @end smallexample
  589. @noindent
  590. would return the contents of the directory @file{/home/bart/bin}.
  591. Instead of referring to the own home directory it is also possible to
  592. name the home directory of other users. To do so one has to append the
  593. user name after the tilde character. So the contents of user
  594. @code{homer}'s @file{bin} directory can be retrieved by
  595. @smallexample
  596. glob ("~homer/bin/*", GLOB_TILDE, NULL, &result)
  597. @end smallexample
  598. If the user name is not valid or the home directory cannot be determined
  599. for some reason the pattern is left untouched and itself used as the
  600. result. I.e., if in the last example @code{home} is not available the
  601. tilde expansion yields to @code{"~homer/bin/*"} and @code{glob} is not
  602. looking for a directory named @code{~homer}.
  603. This functionality is equivalent to what is available in C-shells if the
  604. @code{nonomatch} flag is set.
  605. @item GLOB_TILDE_CHECK
  606. @standards{GNU, glob.h}
  607. If this flag is used @code{glob} behaves as if @code{GLOB_TILDE} is
  608. given. The only difference is that if the user name is not available or
  609. the home directory cannot be determined for other reasons this leads to
  610. an error. @code{glob} will return @code{GLOB_NOMATCH} instead of using
  611. the pattern itself as the name.
  612. This functionality is equivalent to what is available in C-shells if
  613. the @code{nonomatch} flag is not set.
  614. @item GLOB_ONLYDIR
  615. @standards{GNU, glob.h}
  616. If this flag is used the globbing function takes this as a
  617. @strong{hint} that the caller is only interested in directories
  618. matching the pattern. If the information about the type of the file
  619. is easily available non-directories will be rejected but no extra
  620. work will be done to determine the information for each file. I.e.,
  621. the caller must still be able to filter directories out.
  622. This functionality is only available with the GNU @code{glob}
  623. implementation. It is mainly used internally to increase the
  624. performance but might be useful for a user as well and therefore is
  625. documented here.
  626. @end vtable
  627. Calling @code{glob} will in most cases allocate resources which are used
  628. to represent the result of the function call. If the same object of
  629. type @code{glob_t} is used in multiple call to @code{glob} the resources
  630. are freed or reused so that no leaks appear. But this does not include
  631. the time when all @code{glob} calls are done.
  632. @deftypefun void globfree (glob_t *@var{pglob})
  633. @standards{POSIX.2, glob.h}
  634. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  635. @c globfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
  636. @c free dup @ascuheap @acsmem
  637. The @code{globfree} function frees all resources allocated by previous
  638. calls to @code{glob} associated with the object pointed to by
  639. @var{pglob}. This function should be called whenever the currently used
  640. @code{glob_t} typed object isn't used anymore.
  641. @end deftypefun
  642. @deftypefun void globfree64 (glob64_t *@var{pglob})
  643. @standards{GNU, glob.h}
  644. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  645. This function is equivalent to @code{globfree} but it frees records of
  646. type @code{glob64_t} which were allocated by @code{glob64}.
  647. @end deftypefun
  648. @node Regular Expressions
  649. @section Regular Expression Matching
  650. @Theglibc{} supports two interfaces for matching regular
  651. expressions. One is the standard POSIX.2 interface, and the other is
  652. what @theglibc{} has had for many years.
  653. Both interfaces are declared in the header file @file{regex.h}.
  654. If you define @w{@code{_POSIX_C_SOURCE}}, then only the POSIX.2
  655. functions, structures, and constants are declared.
  656. @c !!! we only document the POSIX.2 interface here!!
  657. @menu
  658. * POSIX Regexp Compilation:: Using @code{regcomp} to prepare to match.
  659. * Flags for POSIX Regexps:: Syntax variations for @code{regcomp}.
  660. * Matching POSIX Regexps:: Using @code{regexec} to match the compiled
  661. pattern that you get from @code{regcomp}.
  662. * Regexp Subexpressions:: Finding which parts of the string were matched.
  663. * Subexpression Complications:: Find points of which parts were matched.
  664. * Regexp Cleanup:: Freeing storage; reporting errors.
  665. @end menu
  666. @node POSIX Regexp Compilation
  667. @subsection POSIX Regular Expression Compilation
  668. Before you can actually match a regular expression, you must
  669. @dfn{compile} it. This is not true compilation---it produces a special
  670. data structure, not machine instructions. But it is like ordinary
  671. compilation in that its purpose is to enable you to ``execute'' the
  672. pattern fast. (@xref{Matching POSIX Regexps}, for how to use the
  673. compiled regular expression for matching.)
  674. There is a special data type for compiled regular expressions:
  675. @deftp {Data Type} regex_t
  676. @standards{POSIX.2, regex.h}
  677. This type of object holds a compiled regular expression.
  678. It is actually a structure. It has just one field that your programs
  679. should look at:
  680. @table @code
  681. @item re_nsub
  682. This field holds the number of parenthetical subexpressions in the
  683. regular expression that was compiled.
  684. @end table
  685. There are several other fields, but we don't describe them here, because
  686. only the functions in the library should use them.
  687. @end deftp
  688. After you create a @code{regex_t} object, you can compile a regular
  689. expression into it by calling @code{regcomp}.
  690. @deftypefun int regcomp (regex_t *restrict @var{compiled}, const char *restrict @var{pattern}, int @var{cflags})
  691. @standards{POSIX.2, regex.h}
  692. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
  693. @c All of the issues have to do with memory allocation and multi-byte
  694. @c character handling present in the input string, or implied by ranges
  695. @c or inverted character classes.
  696. @c (re_)malloc @ascuheap @acsmem
  697. @c re_compile_internal @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  698. @c (re_)realloc @ascuheap @acsmem [no @asucorrupt @acucorrupt for we zero the buffer]
  699. @c init_dfa @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  700. @c (re_)malloc @ascuheap @acsmem
  701. @c calloc @ascuheap @acsmem
  702. @c _NL_CURRENT ok
  703. @c _NL_CURRENT_WORD ok
  704. @c btowc @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  705. @c libc_lock_init ok
  706. @c re_string_construct @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  707. @c re_string_construct_common ok
  708. @c re_string_realloc_buffers @ascuheap @acsmem
  709. @c (re_)realloc dup @ascuheap @acsmem
  710. @c build_wcs_upper_buffer @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  711. @c isascii ok
  712. @c mbsinit ok
  713. @c toupper ok
  714. @c mbrtowc dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  715. @c iswlower @mtslocale
  716. @c towupper @mtslocale
  717. @c wcrtomb dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  718. @c (re_)malloc dup @ascuheap @acsmem
  719. @c build_upper_buffer ok (@mtslocale but optimized)
  720. @c islower ok
  721. @c toupper ok
  722. @c build_wcs_buffer @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  723. @c mbrtowc dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  724. @c re_string_translate_buffer ok
  725. @c parse @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  726. @c fetch_token @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  727. @c peek_token @mtslocale
  728. @c re_string_eoi ok
  729. @c re_string_peek_byte ok
  730. @c re_string_cur_idx ok
  731. @c re_string_length ok
  732. @c re_string_peek_byte_case @mtslocale
  733. @c re_string_peek_byte dup ok
  734. @c re_string_is_single_byte_char ok
  735. @c isascii ok
  736. @c re_string_peek_byte dup ok
  737. @c re_string_wchar_at ok
  738. @c re_string_skip_bytes ok
  739. @c re_string_skip_bytes dup ok
  740. @c parse_reg_exp @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  741. @c parse_branch @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  742. @c parse_expression @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  743. @c create_token_tree dup @ascuheap @acsmem
  744. @c re_string_eoi dup ok
  745. @c re_string_first_byte ok
  746. @c fetch_token dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  747. @c create_tree dup @ascuheap @acsmem
  748. @c parse_sub_exp @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  749. @c fetch_token dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  750. @c parse_reg_exp dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  751. @c postorder() @ascuheap @acsmem
  752. @c free_tree @ascuheap @acsmem
  753. @c free_token dup @ascuheap @acsmem
  754. @c create_tree dup @ascuheap @acsmem
  755. @c parse_bracket_exp @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  756. @c _NL_CURRENT dup ok
  757. @c _NL_CURRENT_WORD dup ok
  758. @c calloc dup @ascuheap @acsmem
  759. @c (re_)free dup @ascuheap @acsmem
  760. @c peek_token_bracket ok
  761. @c re_string_eoi dup ok
  762. @c re_string_peek_byte dup ok
  763. @c re_string_first_byte dup ok
  764. @c re_string_cur_idx dup ok
  765. @c re_string_length dup ok
  766. @c re_string_skip_bytes dup ok
  767. @c bitset_set ok
  768. @c re_string_skip_bytes ok
  769. @c parse_bracket_element @mtslocale
  770. @c re_string_char_size_at ok
  771. @c re_string_wchar_at dup ok
  772. @c re_string_skip_bytes dup ok
  773. @c parse_bracket_symbol @mtslocale
  774. @c re_string_eoi dup ok
  775. @c re_string_fetch_byte_case @mtslocale
  776. @c re_string_fetch_byte ok
  777. @c re_string_first_byte dup ok
  778. @c isascii ok
  779. @c re_string_char_size_at dup ok
  780. @c re_string_skip_bytes dup ok
  781. @c re_string_fetch_byte dup ok
  782. @c re_string_peek_byte dup ok
  783. @c re_string_skip_bytes dup ok
  784. @c peek_token_bracket dup ok
  785. @c auto build_range_exp @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  786. @c auto lookup_collation_sequence_value @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  787. @c btowc dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  788. @c collseq_table_lookup ok
  789. @c auto seek_collating_symbol_entry dup ok
  790. @c (re_)realloc dup @ascuheap @acsmem
  791. @c collseq_table_lookup dup ok
  792. @c bitset_set dup ok
  793. @c (re_)realloc dup @ascuheap @acsmem
  794. @c build_equiv_class @mtslocale @ascuheap @acsmem
  795. @c _NL_CURRENT ok
  796. @c auto findidx ok
  797. @c bitset_set dup ok
  798. @c (re_)realloc dup @ascuheap @acsmem
  799. @c auto build_collating_symbol @ascuheap @acsmem
  800. @c auto seek_collating_symbol_entry ok
  801. @c bitset_set dup ok
  802. @c (re_)realloc dup @ascuheap @acsmem
  803. @c build_charclass @mtslocale @ascuheap @acsmem
  804. @c (re_)realloc dup @ascuheap @acsmem
  805. @c bitset_set dup ok
  806. @c isalnum ok
  807. @c iscntrl ok
  808. @c isspace ok
  809. @c isalpha ok
  810. @c isdigit ok
  811. @c isprint ok
  812. @c isupper ok
  813. @c isblank ok
  814. @c isgraph ok
  815. @c ispunct ok
  816. @c isxdigit ok
  817. @c bitset_not ok
  818. @c bitset_mask ok
  819. @c create_token_tree dup @ascuheap @acsmem
  820. @c create_tree dup @ascuheap @acsmem
  821. @c free_charset dup @ascuheap @acsmem
  822. @c init_word_char @mtslocale
  823. @c isalnum ok
  824. @c build_charclass_op @mtslocale @ascuheap @acsmem
  825. @c calloc dup @ascuheap @acsmem
  826. @c build_charclass dup @mtslocale @ascuheap @acsmem
  827. @c (re_)free dup @ascuheap @acsmem
  828. @c free_charset dup @ascuheap @acsmem
  829. @c bitset_set dup ok
  830. @c bitset_not dup ok
  831. @c bitset_mask dup ok
  832. @c create_token_tree dup @ascuheap @acsmem
  833. @c create_tree dup @ascuheap @acsmem
  834. @c parse_dup_op @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  835. @c re_string_cur_idx dup ok
  836. @c fetch_number @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  837. @c fetch_token dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  838. @c re_string_set_index ok
  839. @c postorder() @ascuheap @acsmem
  840. @c free_tree dup @ascuheap @acsmem
  841. @c mark_opt_subexp ok
  842. @c duplicate_tree @ascuheap @acsmem
  843. @c create_token_tree dup @ascuheap @acsmem
  844. @c create_tree dup @ascuheap @acsmem
  845. @c postorder() @ascuheap @acsmem
  846. @c free_tree dup @ascuheap @acsmem
  847. @c fetch_token dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  848. @c parse_branch dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  849. @c create_tree dup @ascuheap @acsmem
  850. @c create_tree @ascuheap @acsmem
  851. @c create_token_tree @ascuheap @acsmem
  852. @c (re_)malloc dup @ascuheap @acsmem
  853. @c analyze @ascuheap @acsmem
  854. @c (re_)malloc dup @ascuheap @acsmem
  855. @c preorder() @ascuheap @acsmem
  856. @c optimize_subexps ok
  857. @c calc_next ok
  858. @c link_nfa_nodes @ascuheap @acsmem
  859. @c re_node_set_init_1 @ascuheap @acsmem
  860. @c (re_)malloc dup @ascuheap @acsmem
  861. @c re_node_set_init_2 @ascuheap @acsmem
  862. @c (re_)malloc dup @ascuheap @acsmem
  863. @c postorder() @ascuheap @acsmem
  864. @c lower_subexps @ascuheap @acsmem
  865. @c lower_subexp @ascuheap @acsmem
  866. @c create_tree dup @ascuheap @acsmem
  867. @c calc_first @ascuheap @acsmem
  868. @c re_dfa_add_node @ascuheap @acsmem
  869. @c (re_)realloc dup @ascuheap @acsmem
  870. @c re_node_set_init_empty ok
  871. @c calc_eclosure @ascuheap @acsmem
  872. @c calc_eclosure_iter @ascuheap @acsmem
  873. @c re_node_set_alloc @ascuheap @acsmem
  874. @c (re_)malloc dup @ascuheap @acsmem
  875. @c duplicate_node_closure @ascuheap @acsmem
  876. @c re_node_set_empty ok
  877. @c duplicate_node @ascuheap @acsmem
  878. @c re_dfa_add_node dup @ascuheap @acsmem
  879. @c re_node_set_insert @ascuheap @acsmem
  880. @c (re_)realloc dup @ascuheap @acsmem
  881. @c search_duplicated_node ok
  882. @c re_node_set_merge @ascuheap @acsmem
  883. @c (re_)realloc dup @ascuheap @acsmem
  884. @c re_node_set_free @ascuheap @acsmem
  885. @c (re_)free dup @ascuheap @acsmem
  886. @c re_node_set_insert dup @ascuheap @acsmem
  887. @c re_node_set_free dup @ascuheap @acsmem
  888. @c calc_inveclosure @ascuheap @acsmem
  889. @c re_node_set_init_empty dup ok
  890. @c re_node_set_insert_last @ascuheap @acsmem
  891. @c (re_)realloc dup @ascuheap @acsmem
  892. @c optimize_utf8 ok
  893. @c create_initial_state @ascuheap @acsmem
  894. @c re_node_set_init_copy @ascuheap @acsmem
  895. @c (re_)malloc dup @ascuheap @acsmem
  896. @c re_node_set_init_empty dup ok
  897. @c re_node_set_contains ok
  898. @c re_node_set_merge dup @ascuheap @acsmem
  899. @c re_acquire_state_context @ascuheap @acsmem
  900. @c calc_state_hash ok
  901. @c re_node_set_compare ok
  902. @c create_cd_newstate @ascuheap @acsmem
  903. @c calloc dup @ascuheap @acsmem
  904. @c re_node_set_init_copy dup @ascuheap @acsmem
  905. @c (re_)free dup @ascuheap @acsmem
  906. @c free_state @ascuheap @acsmem
  907. @c re_node_set_free dup @ascuheap @acsmem
  908. @c (re_)free dup @ascuheap @acsmem
  909. @c NOT_SATISFY_PREV_CONSTRAINT ok
  910. @c re_node_set_remove_at ok
  911. @c register_state @ascuheap @acsmem
  912. @c re_node_set_alloc dup @ascuheap @acsmem
  913. @c re_node_set_insert_last dup @ascuheap @acsmem
  914. @c (re_)realloc dup @ascuheap @acsmem
  915. @c re_node_set_free dup @ascuheap @acsmem
  916. @c free_workarea_compile @ascuheap @acsmem
  917. @c (re_)free dup @ascuheap @acsmem
  918. @c re_string_destruct @ascuheap @acsmem
  919. @c (re_)free dup @ascuheap @acsmem
  920. @c free_dfa_content @ascuheap @acsmem
  921. @c free_token @ascuheap @acsmem
  922. @c free_charset @ascuheap @acsmem
  923. @c (re_)free dup @ascuheap @acsmem
  924. @c (re_)free dup @ascuheap @acsmem
  925. @c (re_)free dup @ascuheap @acsmem
  926. @c re_node_set_free dup @ascuheap @acsmem
  927. @c re_compile_fastmap @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  928. @c re_compile_fastmap_iter @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  929. @c re_set_fastmap ok
  930. @c tolower ok
  931. @c mbrtowc dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  932. @c wcrtomb dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  933. @c towlower @mtslocale
  934. @c _NL_CURRENT ok
  935. @c (re_)free @ascuheap @acsmem
  936. The function @code{regcomp} ``compiles'' a regular expression into a
  937. data structure that you can use with @code{regexec} to match against a
  938. string. The compiled regular expression format is designed for
  939. efficient matching. @code{regcomp} stores it into @code{*@var{compiled}}.
  940. It's up to you to allocate an object of type @code{regex_t} and pass its
  941. address to @code{regcomp}.
  942. The argument @var{cflags} lets you specify various options that control
  943. the syntax and semantics of regular expressions. @xref{Flags for POSIX
  944. Regexps}.
  945. If you use the flag @code{REG_NOSUB}, then @code{regcomp} omits from
  946. the compiled regular expression the information necessary to record
  947. how subexpressions actually match. In this case, you might as well
  948. pass @code{0} for the @var{matchptr} and @var{nmatch} arguments when
  949. you call @code{regexec}.
  950. If you don't use @code{REG_NOSUB}, then the compiled regular expression
  951. does have the capacity to record how subexpressions match. Also,
  952. @code{regcomp} tells you how many subexpressions @var{pattern} has, by
  953. storing the number in @code{@var{compiled}->re_nsub}. You can use that
  954. value to decide how long an array to allocate to hold information about
  955. subexpression matches.
  956. @code{regcomp} returns @code{0} if it succeeds in compiling the regular
  957. expression; otherwise, it returns a nonzero error code (see the table
  958. below). You can use @code{regerror} to produce an error message string
  959. describing the reason for a nonzero value; see @ref{Regexp Cleanup}.
  960. @end deftypefun
  961. Here are the possible nonzero values that @code{regcomp} can return:
  962. @vtable @code
  963. @item REG_BADBR
  964. @standards{POSIX.2, regex.h}
  965. There was an invalid @samp{\@{@dots{}\@}} construct in the regular
  966. expression. A valid @samp{\@{@dots{}\@}} construct must contain either
  967. a single number, or two numbers in increasing order separated by a
  968. comma.
  969. @item REG_BADPAT
  970. @standards{POSIX.2, regex.h}
  971. There was a syntax error in the regular expression.
  972. @item REG_BADRPT
  973. @standards{POSIX.2, regex.h}
  974. A repetition operator such as @samp{?} or @samp{*} appeared in a bad
  975. position (with no preceding subexpression to act on).
  976. @item REG_ECOLLATE
  977. @standards{POSIX.2, regex.h}
  978. The regular expression referred to an invalid collating element (one not
  979. defined in the current locale for string collation). @xref{Locale
  980. Categories}.
  981. @item REG_ECTYPE
  982. @standards{POSIX.2, regex.h}
  983. The regular expression referred to an invalid character class name.
  984. @item REG_EESCAPE
  985. @standards{POSIX.2, regex.h}
  986. The regular expression ended with @samp{\}.
  987. @item REG_ESUBREG
  988. @standards{POSIX.2, regex.h}
  989. There was an invalid number in the @samp{\@var{digit}} construct.
  990. @item REG_EBRACK
  991. @standards{POSIX.2, regex.h}
  992. There were unbalanced square brackets in the regular expression.
  993. @item REG_EPAREN
  994. @standards{POSIX.2, regex.h}
  995. An extended regular expression had unbalanced parentheses,
  996. or a basic regular expression had unbalanced @samp{\(} and @samp{\)}.
  997. @item REG_EBRACE
  998. @standards{POSIX.2, regex.h}
  999. The regular expression had unbalanced @samp{\@{} and @samp{\@}}.
  1000. @item REG_ERANGE
  1001. @standards{POSIX.2, regex.h}
  1002. One of the endpoints in a range expression was invalid.
  1003. @item REG_ESPACE
  1004. @standards{POSIX.2, regex.h}
  1005. @code{regcomp} ran out of memory.
  1006. @end vtable
  1007. @node Flags for POSIX Regexps
  1008. @subsection Flags for POSIX Regular Expressions
  1009. These are the bit flags that you can use in the @var{cflags} operand when
  1010. compiling a regular expression with @code{regcomp}.
  1011. @vtable @code
  1012. @item REG_EXTENDED
  1013. @standards{POSIX.2, regex.h}
  1014. Treat the pattern as an extended regular expression, rather than as a
  1015. basic regular expression.
  1016. @item REG_ICASE
  1017. @standards{POSIX.2, regex.h}
  1018. Ignore case when matching letters.
  1019. @item REG_NOSUB
  1020. @standards{POSIX.2, regex.h}
  1021. Don't bother storing the contents of the @var{matchptr} array.
  1022. @item REG_NEWLINE
  1023. @standards{POSIX.2, regex.h}
  1024. Treat a newline in @var{string} as dividing @var{string} into multiple
  1025. lines, so that @samp{$} can match before the newline and @samp{^} can
  1026. match after. Also, don't permit @samp{.} to match a newline, and don't
  1027. permit @samp{[^@dots{}]} to match a newline.
  1028. Otherwise, newline acts like any other ordinary character.
  1029. @end vtable
  1030. @node Matching POSIX Regexps
  1031. @subsection Matching a Compiled POSIX Regular Expression
  1032. Once you have compiled a regular expression, as described in @ref{POSIX
  1033. Regexp Compilation}, you can match it against strings using
  1034. @code{regexec}. A match anywhere inside the string counts as success,
  1035. unless the regular expression contains anchor characters (@samp{^} or
  1036. @samp{$}).
  1037. @deftypefun int regexec (const regex_t *restrict @var{compiled}, const char *restrict @var{string}, size_t @var{nmatch}, regmatch_t @var{matchptr}[restrict], int @var{eflags})
  1038. @standards{POSIX.2, regex.h}
  1039. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
  1040. @c libc_lock_lock @asulock @aculock
  1041. @c re_search_internal @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1042. @c re_string_allocate @ascuheap @acsmem
  1043. @c re_string_construct_common dup ok
  1044. @c re_string_realloc_buffers dup @ascuheap @acsmem
  1045. @c match_ctx_init @ascuheap @acsmem
  1046. @c (re_)malloc dup @ascuheap @acsmem
  1047. @c re_string_byte_at ok
  1048. @c re_string_first_byte dup ok
  1049. @c check_matching @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1050. @c re_string_cur_idx dup ok
  1051. @c acquire_init_state_context dup @ascuheap @acsmem
  1052. @c re_string_context_at ok
  1053. @c re_string_byte_at dup ok
  1054. @c bitset_contain ok
  1055. @c re_acquire_state_context dup @ascuheap @acsmem
  1056. @c check_subexp_matching_top @ascuheap @acsmem
  1057. @c match_ctx_add_subtop @ascuheap @acsmem
  1058. @c (re_)realloc dup @ascuheap @acsmem
  1059. @c calloc dup @ascuheap @acsmem
  1060. @c transit_state_bkref @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1061. @c re_string_cur_idx dup ok
  1062. @c re_string_context_at dup ok
  1063. @c NOT_SATISFY_NEXT_CONSTRAINT ok
  1064. @c get_subexp @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1065. @c re_string_get_buffer ok
  1066. @c search_cur_bkref_entry ok
  1067. @c clean_state_log_if_needed @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1068. @c extend_buffers @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1069. @c re_string_realloc_buffers dup @ascuheap @acsmem
  1070. @c (re_)realloc dup @ascuheap @acsmem
  1071. @c build_wcs_upper_buffer dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1072. @c build_upper_buffer dup ok (@mtslocale but optimized)
  1073. @c build_wcs_buffer dup @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1074. @c re_string_translate_buffer dup ok
  1075. @c get_subexp_sub @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1076. @c check_arrival @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1077. @c (re_)realloc dup @ascuheap @acsmem
  1078. @c re_string_context_at dup ok
  1079. @c re_node_set_init_1 dup @ascuheap @acsmem
  1080. @c check_arrival_expand_ecl @ascuheap @acsmem
  1081. @c re_node_set_alloc dup @ascuheap @acsmem
  1082. @c find_subexp_node ok
  1083. @c re_node_set_merge dup @ascuheap @acsmem
  1084. @c re_node_set_free dup @ascuheap @acsmem
  1085. @c check_arrival_expand_ecl_sub @ascuheap @acsmem
  1086. @c re_node_set_contains dup ok
  1087. @c re_node_set_insert dup @ascuheap @acsmem
  1088. @c re_node_set_free dup @ascuheap @acsmem
  1089. @c re_node_set_init_copy dup @ascuheap @acsmem
  1090. @c re_node_set_init_empty dup ok
  1091. @c expand_bkref_cache @ascuheap @acsmem
  1092. @c search_cur_bkref_entry dup ok
  1093. @c re_node_set_contains dup ok
  1094. @c re_node_set_init_1 dup @ascuheap @acsmem
  1095. @c check_arrival_expand_ecl dup @ascuheap @acsmem
  1096. @c re_node_set_merge dup @ascuheap @acsmem
  1097. @c re_node_set_init_copy dup @ascuheap @acsmem
  1098. @c re_node_set_insert dup @ascuheap @acsmem
  1099. @c re_node_set_free dup @ascuheap @acsmem
  1100. @c re_acquire_state @ascuheap @acsmem
  1101. @c calc_state_hash dup ok
  1102. @c re_node_set_compare dup ok
  1103. @c create_ci_newstate @ascuheap @acsmem
  1104. @c calloc dup @ascuheap @acsmem
  1105. @c re_node_set_init_copy dup @ascuheap @acsmem
  1106. @c (re_)free dup @ascuheap @acsmem
  1107. @c register_state dup @ascuheap @acsmem
  1108. @c free_state dup @ascuheap @acsmem
  1109. @c re_acquire_state_context dup @ascuheap @acsmem
  1110. @c re_node_set_merge dup @ascuheap @acsmem
  1111. @c check_arrival_add_next_nodes @mtslocale @ascuheap @acsmem
  1112. @c re_node_set_init_empty dup ok
  1113. @c check_node_accept_bytes @mtslocale @ascuheap @acsmem
  1114. @c re_string_byte_at dup ok
  1115. @c re_string_char_size_at dup ok
  1116. @c re_string_elem_size_at @mtslocale
  1117. @c _NL_CURRENT_WORD dup ok
  1118. @c _NL_CURRENT dup ok
  1119. @c auto findidx dup ok
  1120. @c _NL_CURRENT_WORD dup ok
  1121. @c _NL_CURRENT dup ok
  1122. @c collseq_table_lookup dup ok
  1123. @c find_collation_sequence_value @mtslocale
  1124. @c _NL_CURRENT_WORD dup ok
  1125. @c _NL_CURRENT dup ok
  1126. @c auto findidx dup ok
  1127. @c wcscoll @mtslocale @ascuheap @acsmem
  1128. @c re_node_set_empty dup ok
  1129. @c re_node_set_merge dup @ascuheap @acsmem
  1130. @c re_node_set_free dup @ascuheap @acsmem
  1131. @c re_node_set_insert dup @ascuheap @acsmem
  1132. @c re_acquire_state dup @ascuheap @acsmem
  1133. @c check_node_accept ok
  1134. @c re_string_byte_at dup ok
  1135. @c bitset_contain dup ok
  1136. @c re_string_context_at dup ok
  1137. @c NOT_SATISFY_NEXT_CONSTRAINT dup ok
  1138. @c match_ctx_add_entry @ascuheap @acsmem
  1139. @c (re_)realloc dup @ascuheap @acsmem
  1140. @c (re_)free dup @ascuheap @acsmem
  1141. @c clean_state_log_if_needed dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1142. @c extend_buffers dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1143. @c find_subexp_node dup ok
  1144. @c calloc dup @ascuheap @acsmem
  1145. @c check_arrival dup ***
  1146. @c match_ctx_add_sublast @ascuheap @acsmem
  1147. @c (re_)realloc dup @ascuheap @acsmem
  1148. @c re_acquire_state_context dup @ascuheap @acsmem
  1149. @c re_node_set_init_union @ascuheap @acsmem
  1150. @c (re_)malloc dup @ascuheap @acsmem
  1151. @c re_node_set_init_copy dup @ascuheap @acsmem
  1152. @c re_node_set_init_empty dup ok
  1153. @c re_node_set_free dup @ascuheap @acsmem
  1154. @c check_subexp_matching_top dup @ascuheap @acsmem
  1155. @c check_halt_state_context ok
  1156. @c re_string_context_at dup ok
  1157. @c check_halt_node_context ok
  1158. @c NOT_SATISFY_NEXT_CONSTRAINT dup ok
  1159. @c re_string_eoi dup ok
  1160. @c extend_buffers dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1161. @c transit_state @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1162. @c transit_state_mb @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1163. @c re_string_context_at dup ok
  1164. @c NOT_SATISFY_NEXT_CONSTRAINT dup ok
  1165. @c check_node_accept_bytes dup @mtslocale @ascuheap @acsmem
  1166. @c re_string_cur_idx dup ok
  1167. @c clean_state_log_if_needed @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1168. @c re_node_set_init_union dup @ascuheap @acsmem
  1169. @c re_acquire_state_context dup @ascuheap @acsmem
  1170. @c re_string_fetch_byte dup ok
  1171. @c re_string_context_at dup ok
  1172. @c build_trtable @ascuheap @acsmem
  1173. @c (re_)malloc dup @ascuheap @acsmem
  1174. @c group_nodes_into_DFAstates @ascuheap @acsmem
  1175. @c bitset_empty dup ok
  1176. @c bitset_set dup ok
  1177. @c bitset_merge dup ok
  1178. @c bitset_set_all ok
  1179. @c bitset_clear ok
  1180. @c bitset_contain dup ok
  1181. @c bitset_copy ok
  1182. @c re_node_set_init_copy dup @ascuheap @acsmem
  1183. @c re_node_set_insert dup @ascuheap @acsmem
  1184. @c re_node_set_init_1 dup @ascuheap @acsmem
  1185. @c re_node_set_free dup @ascuheap @acsmem
  1186. @c re_node_set_alloc dup @ascuheap @acsmem
  1187. @c malloc dup @ascuheap @acsmem
  1188. @c free dup @ascuheap @acsmem
  1189. @c re_node_set_free dup @ascuheap @acsmem
  1190. @c bitset_empty ok
  1191. @c re_node_set_empty dup ok
  1192. @c re_node_set_merge dup @ascuheap @acsmem
  1193. @c re_acquire_state_context dup @ascuheap @acsmem
  1194. @c bitset_merge ok
  1195. @c calloc dup @ascuheap @acsmem
  1196. @c bitset_contain dup ok
  1197. @c merge_state_with_log @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1198. @c re_string_cur_idx dup ok
  1199. @c re_node_set_init_union dup @ascuheap @acsmem
  1200. @c re_string_context_at dup ok
  1201. @c re_node_set_free dup @ascuheap @acsmem
  1202. @c check_subexp_matching_top @ascuheap @acsmem
  1203. @c match_ctx_add_subtop dup @ascuheap @acsmem
  1204. @c transit_state_bkref dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1205. @c find_recover_state
  1206. @c re_string_cur_idx dup ok
  1207. @c re_string_skip_bytes dup ok
  1208. @c merge_state_with_log dup @mtslocale @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsmem @acsfd
  1209. @c check_halt_state_context dup ok
  1210. @c prune_impossible_nodes @mtslocale @ascuheap @acsmem
  1211. @c (re_)malloc dup @ascuheap @acsmem
  1212. @c sift_ctx_init ok
  1213. @c re_node_set_init_empty dup ok
  1214. @c sift_states_backward @mtslocale @ascuheap @acsmem
  1215. @c re_node_set_init_1 dup @ascuheap @acsmem
  1216. @c update_cur_sifted_state @mtslocale @ascuheap @acsmem
  1217. @c add_epsilon_src_nodes @ascuheap @acsmem
  1218. @c re_acquire_state dup @ascuheap @acsmem
  1219. @c re_node_set_alloc dup @ascuheap @acsmem
  1220. @c re_node_set_merge dup @ascuheap @acsmem
  1221. @c re_node_set_add_intersect @ascuheap @acsmem
  1222. @c (re_)realloc dup @ascuheap @acsmem
  1223. @c check_subexp_limits @ascuheap @acsmem
  1224. @c sub_epsilon_src_nodes @ascuheap @acsmem
  1225. @c re_node_set_init_empty dup ok
  1226. @c re_node_set_contains dup ok
  1227. @c re_node_set_add_intersect dup @ascuheap @acsmem
  1228. @c re_node_set_free dup @ascuheap @acsmem
  1229. @c re_node_set_remove_at dup ok
  1230. @c re_node_set_contains dup ok
  1231. @c re_acquire_state dup @ascuheap @acsmem
  1232. @c sift_states_bkref @mtslocale @ascuheap @acsmem
  1233. @c search_cur_bkref_entry dup ok
  1234. @c check_dst_limits ok
  1235. @c search_cur_bkref_entry dup ok
  1236. @c check_dst_limits_calc_pos ok
  1237. @c check_dst_limits_calc_pos_1 ok
  1238. @c re_node_set_init_copy dup @ascuheap @acsmem
  1239. @c re_node_set_insert dup @ascuheap @acsmem
  1240. @c sift_states_backward dup @mtslocale @ascuheap @acsmem
  1241. @c merge_state_array dup @ascuheap @acsmem
  1242. @c re_node_set_remove ok
  1243. @c re_node_set_contains dup ok
  1244. @c re_node_set_remove_at dup ok
  1245. @c re_node_set_free dup @ascuheap @acsmem
  1246. @c re_node_set_free dup @ascuheap @acsmem
  1247. @c re_node_set_empty dup ok
  1248. @c build_sifted_states @mtslocale @ascuheap @acsmem
  1249. @c sift_states_iter_mb @mtslocale @ascuheap @acsmem
  1250. @c check_node_accept_bytes dup @mtslocale @ascuheap @acsmem
  1251. @c check_node_accept dup ok
  1252. @c check_dst_limits dup ok
  1253. @c re_node_set_insert dup @ascuheap @acsmem
  1254. @c re_node_set_free dup @ascuheap @acsmem
  1255. @c check_halt_state_context dup ok
  1256. @c merge_state_array @ascuheap @acsmem
  1257. @c re_node_set_init_union dup @ascuheap @acsmem
  1258. @c re_acquire_state dup @ascuheap @acsmem
  1259. @c re_node_set_free dup @ascuheap @acsmem
  1260. @c (re_)free dup @ascuheap @acsmem
  1261. @c set_regs @ascuheap @acsmem
  1262. @c (re_)malloc dup @ascuheap @acsmem
  1263. @c re_node_set_init_empty dup ok
  1264. @c free_fail_stack_return @ascuheap @acsmem
  1265. @c re_node_set_free dup @ascuheap @acsmem
  1266. @c (re_)free dup @ascuheap @acsmem
  1267. @c update_regs ok
  1268. @c re_node_set_free dup @ascuheap @acsmem
  1269. @c pop_fail_stack @ascuheap @acsmem
  1270. @c re_node_set_free dup @ascuheap @acsmem
  1271. @c (re_)free dup @ascuheap @acsmem
  1272. @c (re_)free dup @ascuheap @acsmem
  1273. @c (re_)free dup @ascuheap @acsmem
  1274. @c match_ctx_free @ascuheap @acsmem
  1275. @c match_ctx_clean @ascuheap @acsmem
  1276. @c (re_)free dup @ascuheap @acsmem
  1277. @c (re_)free dup @ascuheap @acsmem
  1278. @c re_string_destruct dup @ascuheap @acsmem
  1279. @c libc_lock_unlock @aculock
  1280. This function tries to match the compiled regular expression
  1281. @code{*@var{compiled}} against @var{string}.
  1282. @code{regexec} returns @code{0} if the regular expression matches;
  1283. otherwise, it returns a nonzero value. See the table below for
  1284. what nonzero values mean. You can use @code{regerror} to produce an
  1285. error message string describing the reason for a nonzero value;
  1286. see @ref{Regexp Cleanup}.
  1287. The argument @var{eflags} is a word of bit flags that enable various
  1288. options.
  1289. If you want to get information about what part of @var{string} actually
  1290. matched the regular expression or its subexpressions, use the arguments
  1291. @var{matchptr} and @var{nmatch}. Otherwise, pass @code{0} for
  1292. @var{nmatch}, and @code{NULL} for @var{matchptr}. @xref{Regexp
  1293. Subexpressions}.
  1294. @end deftypefun
  1295. You must match the regular expression with the same set of current
  1296. locales that were in effect when you compiled the regular expression.
  1297. The function @code{regexec} accepts the following flags in the
  1298. @var{eflags} argument:
  1299. @vtable @code
  1300. @item REG_NOTBOL
  1301. @standards{POSIX.2, regex.h}
  1302. Do not regard the beginning of the specified string as the beginning of
  1303. a line; more generally, don't make any assumptions about what text might
  1304. precede it.
  1305. @item REG_NOTEOL
  1306. @standards{POSIX.2, regex.h}
  1307. Do not regard the end of the specified string as the end of a line; more
  1308. generally, don't make any assumptions about what text might follow it.
  1309. @end vtable
  1310. Here are the possible nonzero values that @code{regexec} can return:
  1311. @vtable @code
  1312. @item REG_NOMATCH
  1313. @standards{POSIX.2, regex.h}
  1314. The pattern didn't match the string. This isn't really an error.
  1315. @item REG_ESPACE
  1316. @standards{POSIX.2, regex.h}
  1317. @code{regexec} ran out of memory.
  1318. @end vtable
  1319. @node Regexp Subexpressions
  1320. @subsection Match Results with Subexpressions
  1321. When @code{regexec} matches parenthetical subexpressions of
  1322. @var{pattern}, it records which parts of @var{string} they match. It
  1323. returns that information by storing the offsets into an array whose
  1324. elements are structures of type @code{regmatch_t}. The first element of
  1325. the array (index @code{0}) records the part of the string that matched
  1326. the entire regular expression. Each other element of the array records
  1327. the beginning and end of the part that matched a single parenthetical
  1328. subexpression.
  1329. @deftp {Data Type} regmatch_t
  1330. @standards{POSIX.2, regex.h}
  1331. This is the data type of the @var{matchptr} array that you pass to
  1332. @code{regexec}. It contains two structure fields, as follows:
  1333. @table @code
  1334. @item rm_so
  1335. The offset in @var{string} of the beginning of a substring. Add this
  1336. value to @var{string} to get the address of that part.
  1337. @item rm_eo
  1338. The offset in @var{string} of the end of the substring.
  1339. @end table
  1340. @end deftp
  1341. @deftp {Data Type} regoff_t
  1342. @standards{POSIX.2, regex.h}
  1343. @code{regoff_t} is an alias for another signed integer type.
  1344. The fields of @code{regmatch_t} have type @code{regoff_t}.
  1345. @end deftp
  1346. The @code{regmatch_t} elements correspond to subexpressions
  1347. positionally; the first element (index @code{1}) records where the first
  1348. subexpression matched, the second element records the second
  1349. subexpression, and so on. The order of the subexpressions is the order
  1350. in which they begin.
  1351. When you call @code{regexec}, you specify how long the @var{matchptr}
  1352. array is, with the @var{nmatch} argument. This tells @code{regexec} how
  1353. many elements to store. If the actual regular expression has more than
  1354. @var{nmatch} subexpressions, then you won't get offset information about
  1355. the rest of them. But this doesn't alter whether the pattern matches a
  1356. particular string or not.
  1357. If you don't want @code{regexec} to return any information about where
  1358. the subexpressions matched, you can either supply @code{0} for
  1359. @var{nmatch}, or use the flag @code{REG_NOSUB} when you compile the
  1360. pattern with @code{regcomp}.
  1361. @node Subexpression Complications
  1362. @subsection Complications in Subexpression Matching
  1363. Sometimes a subexpression matches a substring of no characters. This
  1364. happens when @samp{f\(o*\)} matches the string @samp{fum}. (It really
  1365. matches just the @samp{f}.) In this case, both of the offsets identify
  1366. the point in the string where the null substring was found. In this
  1367. example, the offsets are both @code{1}.
  1368. Sometimes the entire regular expression can match without using some of
  1369. its subexpressions at all---for example, when @samp{ba\(na\)*} matches the
  1370. string @samp{ba}, the parenthetical subexpression is not used. When
  1371. this happens, @code{regexec} stores @code{-1} in both fields of the
  1372. element for that subexpression.
  1373. Sometimes matching the entire regular expression can match a particular
  1374. subexpression more than once---for example, when @samp{ba\(na\)*}
  1375. matches the string @samp{bananana}, the parenthetical subexpression
  1376. matches three times. When this happens, @code{regexec} usually stores
  1377. the offsets of the last part of the string that matched the
  1378. subexpression. In the case of @samp{bananana}, these offsets are
  1379. @code{6} and @code{8}.
  1380. But the last match is not always the one that is chosen. It's more
  1381. accurate to say that the last @emph{opportunity} to match is the one
  1382. that takes precedence. What this means is that when one subexpression
  1383. appears within another, then the results reported for the inner
  1384. subexpression reflect whatever happened on the last match of the outer
  1385. subexpression. For an example, consider @samp{\(ba\(na\)*s \)*} matching
  1386. the string @samp{bananas bas }. The last time the inner expression
  1387. actually matches is near the end of the first word. But it is
  1388. @emph{considered} again in the second word, and fails to match there.
  1389. @code{regexec} reports nonuse of the ``na'' subexpression.
  1390. Another place where this rule applies is when the regular expression
  1391. @smallexample
  1392. \(ba\(na\)*s \|nefer\(ti\)* \)*
  1393. @end smallexample
  1394. @noindent
  1395. matches @samp{bananas nefertiti}. The ``na'' subexpression does match
  1396. in the first word, but it doesn't match in the second word because the
  1397. other alternative is used there. Once again, the second repetition of
  1398. the outer subexpression overrides the first, and within that second
  1399. repetition, the ``na'' subexpression is not used. So @code{regexec}
  1400. reports nonuse of the ``na'' subexpression.
  1401. @node Regexp Cleanup
  1402. @subsection POSIX Regexp Matching Cleanup
  1403. When you are finished using a compiled regular expression, you can
  1404. free the storage it uses by calling @code{regfree}.
  1405. @deftypefun void regfree (regex_t *@var{compiled})
  1406. @standards{POSIX.2, regex.h}
  1407. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  1408. @c (re_)free dup @ascuheap @acsmem
  1409. @c free_dfa_content dup @ascuheap @acsmem
  1410. Calling @code{regfree} frees all the storage that @code{*@var{compiled}}
  1411. points to. This includes various internal fields of the @code{regex_t}
  1412. structure that aren't documented in this manual.
  1413. @code{regfree} does not free the object @code{*@var{compiled}} itself.
  1414. @end deftypefun
  1415. You should always free the space in a @code{regex_t} structure with
  1416. @code{regfree} before using the structure to compile another regular
  1417. expression.
  1418. When @code{regcomp} or @code{regexec} reports an error, you can use
  1419. the function @code{regerror} to turn it into an error message string.
  1420. @deftypefun size_t regerror (int @var{errcode}, const regex_t *restrict @var{compiled}, char *restrict @var{buffer}, size_t @var{length})
  1421. @standards{POSIX.2, regex.h}
  1422. @safety{@prelim{}@mtsafe{@mtsenv{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1423. @c regerror calls gettext, strcmp and mempcpy or memcpy.
  1424. This function produces an error message string for the error code
  1425. @var{errcode}, and stores the string in @var{length} bytes of memory
  1426. starting at @var{buffer}. For the @var{compiled} argument, supply the
  1427. same compiled regular expression structure that @code{regcomp} or
  1428. @code{regexec} was working with when it got the error. Alternatively,
  1429. you can supply @code{NULL} for @var{compiled}; you will still get a
  1430. meaningful error message, but it might not be as detailed.
  1431. If the error message can't fit in @var{length} bytes (including a
  1432. terminating null character), then @code{regerror} truncates it.
  1433. The string that @code{regerror} stores is always null-terminated
  1434. even if it has been truncated.
  1435. The return value of @code{regerror} is the minimum length needed to
  1436. store the entire error message. If this is less than @var{length}, then
  1437. the error message was not truncated, and you can use it. Otherwise, you
  1438. should call @code{regerror} again with a larger buffer.
  1439. Here is a function which uses @code{regerror}, but always dynamically
  1440. allocates a buffer for the error message:
  1441. @smallexample
  1442. char *get_regerror (int errcode, regex_t *compiled)
  1443. @{
  1444. size_t length = regerror (errcode, compiled, NULL, 0);
  1445. char *buffer = xmalloc (length);
  1446. (void) regerror (errcode, compiled, buffer, length);
  1447. return buffer;
  1448. @}
  1449. @end smallexample
  1450. @end deftypefun
  1451. @node Word Expansion
  1452. @section Shell-Style Word Expansion
  1453. @cindex word expansion
  1454. @cindex expansion of shell words
  1455. @dfn{Word expansion} means the process of splitting a string into
  1456. @dfn{words} and substituting for variables, commands, and wildcards
  1457. just as the shell does.
  1458. For example, when you write @samp{ls -l foo.c}, this string is split
  1459. into three separate words---@samp{ls}, @samp{-l} and @samp{foo.c}.
  1460. This is the most basic function of word expansion.
  1461. When you write @samp{ls *.c}, this can become many words, because
  1462. the word @samp{*.c} can be replaced with any number of file names.
  1463. This is called @dfn{wildcard expansion}, and it is also a part of
  1464. word expansion.
  1465. When you use @samp{echo $PATH} to print your path, you are taking
  1466. advantage of @dfn{variable substitution}, which is also part of word
  1467. expansion.
  1468. Ordinary programs can perform word expansion just like the shell by
  1469. calling the library function @code{wordexp}.
  1470. @menu
  1471. * Expansion Stages:: What word expansion does to a string.
  1472. * Calling Wordexp:: How to call @code{wordexp}.
  1473. * Flags for Wordexp:: Options you can enable in @code{wordexp}.
  1474. * Wordexp Example:: A sample program that does word expansion.
  1475. * Tilde Expansion:: Details of how tilde expansion works.
  1476. * Variable Substitution:: Different types of variable substitution.
  1477. @end menu
  1478. @node Expansion Stages
  1479. @subsection The Stages of Word Expansion
  1480. When word expansion is applied to a sequence of words, it performs the
  1481. following transformations in the order shown here:
  1482. @enumerate
  1483. @item
  1484. @cindex tilde expansion
  1485. @dfn{Tilde expansion}: Replacement of @samp{~foo} with the name of
  1486. the home directory of @samp{foo}.
  1487. @item
  1488. Next, three different transformations are applied in the same step,
  1489. from left to right:
  1490. @itemize @bullet
  1491. @item
  1492. @cindex variable substitution
  1493. @cindex substitution of variables and commands
  1494. @dfn{Variable substitution}: Environment variables are substituted for
  1495. references such as @samp{$foo}.
  1496. @item
  1497. @cindex command substitution
  1498. @dfn{Command substitution}: Constructs such as @w{@samp{`cat foo`}} and
  1499. the equivalent @w{@samp{$(cat foo)}} are replaced with the output from
  1500. the inner command.
  1501. @item
  1502. @cindex arithmetic expansion
  1503. @dfn{Arithmetic expansion}: Constructs such as @samp{$(($x-1))} are
  1504. replaced with the result of the arithmetic computation.
  1505. @end itemize
  1506. @item
  1507. @cindex field splitting
  1508. @dfn{Field splitting}: subdivision of the text into @dfn{words}.
  1509. @item
  1510. @cindex wildcard expansion
  1511. @dfn{Wildcard expansion}: The replacement of a construct such as @samp{*.c}
  1512. with a list of @samp{.c} file names. Wildcard expansion applies to an
  1513. entire word at a time, and replaces that word with 0 or more file names
  1514. that are themselves words.
  1515. @item
  1516. @cindex quote removal
  1517. @cindex removal of quotes
  1518. @dfn{Quote removal}: The deletion of string-quotes, now that they have
  1519. done their job by inhibiting the above transformations when appropriate.
  1520. @end enumerate
  1521. For the details of these transformations, and how to write the constructs
  1522. that use them, see @w{@cite{The BASH Manual}} (to appear).
  1523. @node Calling Wordexp
  1524. @subsection Calling @code{wordexp}
  1525. All the functions, constants and data types for word expansion are
  1526. declared in the header file @file{wordexp.h}.
  1527. Word expansion produces a vector of words (strings). To return this
  1528. vector, @code{wordexp} uses a special data type, @code{wordexp_t}, which
  1529. is a structure. You pass @code{wordexp} the address of the structure,
  1530. and it fills in the structure's fields to tell you about the results.
  1531. @deftp {Data Type} {wordexp_t}
  1532. @standards{POSIX.2, wordexp.h}
  1533. This data type holds a pointer to a word vector. More precisely, it
  1534. records both the address of the word vector and its size.
  1535. @table @code
  1536. @item we_wordc
  1537. The number of elements in the vector.
  1538. @item we_wordv
  1539. The address of the vector. This field has type @w{@code{char **}}.
  1540. @item we_offs
  1541. The offset of the first real element of the vector, from its nominal
  1542. address in the @code{we_wordv} field. Unlike the other fields, this
  1543. is always an input to @code{wordexp}, rather than an output from it.
  1544. If you use a nonzero offset, then that many elements at the beginning of
  1545. the vector are left empty. (The @code{wordexp} function fills them with
  1546. null pointers.)
  1547. The @code{we_offs} field is meaningful only if you use the
  1548. @code{WRDE_DOOFFS} flag. Otherwise, the offset is always zero
  1549. regardless of what is in this field, and the first real element comes at
  1550. the beginning of the vector.
  1551. @end table
  1552. @end deftp
  1553. @deftypefun int wordexp (const char *@var{words}, wordexp_t *@var{word-vector-ptr}, int @var{flags})
  1554. @standards{POSIX.2, wordexp.h}
  1555. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtasuconst{:@mtsenv{}} @mtsenv{} @mtascusig{:ALRM} @mtascutimer{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuintl{} @ascuheap{} @asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1556. @c wordexp @mtasurace:utent @mtasuconst:@mtsenv @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuintl @ascuheap @asucorrupt @asulock @acucorrupt @aculock @acsfd @acsmem
  1557. @c w_newword ok
  1558. @c wordfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
  1559. @c calloc dup @ascuheap @acsmem
  1560. @c getenv dup @mtsenv
  1561. @c strcpy dup ok
  1562. @c parse_backslash @ascuheap @acsmem
  1563. @c w_addchar dup @ascuheap @acsmem
  1564. @c parse_dollars @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1565. @c w_addchar dup @ascuheap @acsmem
  1566. @c parse_arith @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1567. @c w_newword dup ok
  1568. @c parse_dollars dup @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1569. @c parse_backtick dup @ascuplugin @ascuheap @aculock @acsfd @acsmem
  1570. @c parse_qtd_backslash dup @ascuheap @acsmem
  1571. @c eval_expr @mtslocale
  1572. @c eval_expr_multidiv @mtslocale
  1573. @c eval_expr_val @mtslocale
  1574. @c isspace dup @mtslocale
  1575. @c eval_expr dup @mtslocale
  1576. @c isspace dup @mtslocale
  1577. @c isspace dup @mtslocale
  1578. @c free dup @ascuheap @acsmem
  1579. @c w_addchar dup @ascuheap @acsmem
  1580. @c w_addstr dup @ascuheap @acsmem
  1581. @c itoa_word dup ok
  1582. @c parse_comm @ascuplugin @ascuheap @aculock @acsfd @acsmem
  1583. @c w_newword dup ok
  1584. @c pthread_setcancelstate @ascuplugin @ascuheap @acsmem
  1585. @c (disable cancellation around exec_comm; it may do_cancel the
  1586. @c second time, if async cancel is enabled)
  1587. @c THREAD_ATOMIC_CMPXCHG_VAL dup ok
  1588. @c do_cancel @ascuplugin @ascuheap @acsmem
  1589. @c THREAD_ATOMIC_BIT_SET dup ok
  1590. @c pthread_unwind @ascuplugin @ascuheap @acsmem
  1591. @c Unwind_ForcedUnwind if available @ascuplugin @ascuheap @acsmem
  1592. @c libc_unwind_longjmp otherwise
  1593. @c cleanups
  1594. @c exec_comm @ascuplugin @ascuheap @aculock @acsfd @acsmem
  1595. @c pipe2 dup ok
  1596. @c pipe dup ok
  1597. @c fork dup @ascuplugin @aculock
  1598. @c close dup @acsfd
  1599. @c on child: exec_comm_child -> exec or abort
  1600. @c waitpid dup ok
  1601. @c read dup ok
  1602. @c w_addmem dup @ascuheap @acsmem
  1603. @c strchr dup ok
  1604. @c w_addword dup @ascuheap @acsmem
  1605. @c w_newword dup ok
  1606. @c w_addchar dup @ascuheap @acsmem
  1607. @c free dup @ascuheap @acsmem
  1608. @c kill dup ok
  1609. @c free dup @ascuheap @acsmem
  1610. @c parse_param @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1611. @c reads from __libc_argc and __libc_argv without guards
  1612. @c w_newword dup ok
  1613. @c isalpha dup @mtslocale^^
  1614. @c w_addchar dup @ascuheap @acsmem
  1615. @c isalnum dup @mtslocale^^
  1616. @c isdigit dup @mtslocale^^
  1617. @c strchr dup ok
  1618. @c itoa_word dup ok
  1619. @c atoi dup @mtslocale
  1620. @c getpid dup ok
  1621. @c w_addstr dup @ascuheap @acsmem
  1622. @c free dup @ascuheap @acsmem
  1623. @c strlen dup ok
  1624. @c malloc dup @ascuheap @acsmem
  1625. @c stpcpy dup ok
  1626. @c w_addword dup @ascuheap @acsmem
  1627. @c strdup dup @ascuheap @acsmem
  1628. @c getenv dup @mtsenv
  1629. @c parse_dollars dup @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1630. @c parse_tilde dup @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1631. @c fnmatch dup @mtsenv @mtslocale @ascuheap @acsmem
  1632. @c mempcpy dup ok
  1633. @c _ dup @ascuintl
  1634. @c fxprintf dup @aculock
  1635. @c setenv dup @mtasuconst:@mtsenv @ascuheap @asulock @acucorrupt @aculock @acsmem
  1636. @c strspn dup ok
  1637. @c strcspn dup ok
  1638. @c parse_backtick @ascuplugin @ascuheap @aculock @acsfd @acsmem
  1639. @c w_newword dup ok
  1640. @c exec_comm dup @ascuplugin @ascuheap @aculock @acsfd @acsmem
  1641. @c free dup @ascuheap @acsmem
  1642. @c parse_qtd_backslash dup @ascuheap @acsmem
  1643. @c parse_backslash dup @ascuheap @acsmem
  1644. @c w_addchar dup @ascuheap @acsmem
  1645. @c parse_dquote @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1646. @c parse_dollars dup @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1647. @c parse_backtick dup @ascuplugin @ascuheap @aculock @acsfd @acsmem
  1648. @c parse_qtd_backslash dup @ascuheap @acsmem
  1649. @c w_addchar dup @ascuheap @acsmem
  1650. @c w_addword dup @ascuheap @acsmem
  1651. @c strdup dup @ascuheap @acsmem
  1652. @c realloc dup @ascuheap @acsmem
  1653. @c free dup @ascuheap @acsmem
  1654. @c parse_squote dup @ascuheap @acsmem
  1655. @c w_addchar dup @ascuheap @acsmem
  1656. @c parse_tilde @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1657. @c strchr dup ok
  1658. @c w_addchar dup @ascuheap @acsmem
  1659. @c getenv dup @mtsenv
  1660. @c w_addstr dup @ascuheap @acsmem
  1661. @c strlen dup ok
  1662. @c w_addmem dup @ascuheap @acsmem
  1663. @c realloc dup @ascuheap @acsmem
  1664. @c free dup @ascuheap @acsmem
  1665. @c mempcpy dup ok
  1666. @c getuid dup ok
  1667. @c getpwuid_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1668. @c getpwnam_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1669. @c parse_glob @mtasurace:utent @mtasuconst:@mtsenv @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1670. @c strchr dup ok
  1671. @c parse_dollars dup @mtasuconst:@mtsenv @mtslocale @mtsenv @ascudlopen @ascuplugin @ascuintl @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1672. @c parse_qtd_backslash @ascuheap @acsmem
  1673. @c w_addchar dup @ascuheap @acsmem
  1674. @c parse_backslash dup @ascuheap @acsmem
  1675. @c w_addchar dup @ascuheap @acsmem
  1676. @c w_addword dup @ascuheap @acsmem
  1677. @c w_newword dup ok
  1678. @c do_parse_glob @mtasurace:utent @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @aculock @acsfd @acsmem
  1679. @c glob dup @mtasurace:utent @mtsenv @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @aculock @acsfd @acsmem [auto glob_t avoids @asucorrupt @acucorrupt]
  1680. @c w_addstr dup @ascuheap @acsmem
  1681. @c w_addchar dup @ascuheap @acsmem
  1682. @c globfree dup @ascuheap @acsmem [auto glob_t avoids @asucorrupt @acucorrupt]
  1683. @c free dup @ascuheap @acsmem
  1684. @c w_newword dup ok
  1685. @c strdup dup @ascuheap @acsmem
  1686. @c w_addword dup @ascuheap @acsmem
  1687. @c wordfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
  1688. @c strchr dup ok
  1689. @c w_addchar dup @ascuheap @acsmem
  1690. @c realloc dup @ascuheap @acsmem
  1691. @c free dup @ascuheap @acsmem
  1692. @c free dup @ascuheap @acsmem
  1693. Perform word expansion on the string @var{words}, putting the result in
  1694. a newly allocated vector, and store the size and address of this vector
  1695. into @code{*@var{word-vector-ptr}}. The argument @var{flags} is a
  1696. combination of bit flags; see @ref{Flags for Wordexp}, for details of
  1697. the flags.
  1698. You shouldn't use any of the characters @samp{|&;<>} in the string
  1699. @var{words} unless they are quoted; likewise for newline. If you use
  1700. these characters unquoted, you will get the @code{WRDE_BADCHAR} error
  1701. code. Don't use parentheses or braces unless they are quoted or part of
  1702. a word expansion construct. If you use quotation characters @samp{'"`},
  1703. they should come in pairs that balance.
  1704. The results of word expansion are a sequence of words. The function
  1705. @code{wordexp} allocates a string for each resulting word, then
  1706. allocates a vector of type @code{char **} to store the addresses of
  1707. these strings. The last element of the vector is a null pointer.
  1708. This vector is called the @dfn{word vector}.
  1709. To return this vector, @code{wordexp} stores both its address and its
  1710. length (number of elements, not counting the terminating null pointer)
  1711. into @code{*@var{word-vector-ptr}}.
  1712. If @code{wordexp} succeeds, it returns 0. Otherwise, it returns one
  1713. of these error codes:
  1714. @vtable @code
  1715. @item WRDE_BADCHAR
  1716. @standards{POSIX.2, wordexp.h}
  1717. The input string @var{words} contains an unquoted invalid character such
  1718. as @samp{|}.
  1719. @item WRDE_BADVAL
  1720. @standards{POSIX.2, wordexp.h}
  1721. The input string refers to an undefined shell variable, and you used the flag
  1722. @code{WRDE_UNDEF} to forbid such references.
  1723. @item WRDE_CMDSUB
  1724. @standards{POSIX.2, wordexp.h}
  1725. The input string uses command substitution, and you used the flag
  1726. @code{WRDE_NOCMD} to forbid command substitution.
  1727. @item WRDE_NOSPACE
  1728. @standards{POSIX.2, wordexp.h}
  1729. It was impossible to allocate memory to hold the result. In this case,
  1730. @code{wordexp} can store part of the results---as much as it could
  1731. allocate room for.
  1732. @item WRDE_SYNTAX
  1733. @standards{POSIX.2, wordexp.h}
  1734. There was a syntax error in the input string. For example, an unmatched
  1735. quoting character is a syntax error. This error code is also used to
  1736. signal division by zero and overflow in arithmetic expansion.
  1737. @end vtable
  1738. @end deftypefun
  1739. @deftypefun void wordfree (wordexp_t *@var{word-vector-ptr})
  1740. @standards{POSIX.2, wordexp.h}
  1741. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
  1742. @c wordfree dup @asucorrupt @ascuheap @acucorrupt @acsmem
  1743. @c free dup @ascuheap @acsmem
  1744. Free the storage used for the word-strings and vector that
  1745. @code{*@var{word-vector-ptr}} points to. This does not free the
  1746. structure @code{*@var{word-vector-ptr}} itself---only the other
  1747. data it points to.
  1748. @end deftypefun
  1749. @node Flags for Wordexp
  1750. @subsection Flags for Word Expansion
  1751. This section describes the flags that you can specify in the
  1752. @var{flags} argument to @code{wordexp}. Choose the flags you want,
  1753. and combine them with the C operator @code{|}.
  1754. @vtable @code
  1755. @item WRDE_APPEND
  1756. @standards{POSIX.2, wordexp.h}
  1757. Append the words from this expansion to the vector of words produced by
  1758. previous calls to @code{wordexp}. This way you can effectively expand
  1759. several words as if they were concatenated with spaces between them.
  1760. In order for appending to work, you must not modify the contents of the
  1761. word vector structure between calls to @code{wordexp}. And, if you set
  1762. @code{WRDE_DOOFFS} in the first call to @code{wordexp}, you must also
  1763. set it when you append to the results.
  1764. @item WRDE_DOOFFS
  1765. @standards{POSIX.2, wordexp.h}
  1766. Leave blank slots at the beginning of the vector of words.
  1767. The @code{we_offs} field says how many slots to leave.
  1768. The blank slots contain null pointers.
  1769. @item WRDE_NOCMD
  1770. @standards{POSIX.2, wordexp.h}
  1771. Don't do command substitution; if the input requests command substitution,
  1772. report an error.
  1773. @item WRDE_REUSE
  1774. @standards{POSIX.2, wordexp.h}
  1775. Reuse a word vector made by a previous call to @code{wordexp}.
  1776. Instead of allocating a new vector of words, this call to @code{wordexp}
  1777. will use the vector that already exists (making it larger if necessary).
  1778. Note that the vector may move, so it is not safe to save an old pointer
  1779. and use it again after calling @code{wordexp}. You must fetch
  1780. @code{we_pathv} anew after each call.
  1781. @item WRDE_SHOWERR
  1782. @standards{POSIX.2, wordexp.h}
  1783. Do show any error messages printed by commands run by command substitution.
  1784. More precisely, allow these commands to inherit the standard error output
  1785. stream of the current process. By default, @code{wordexp} gives these
  1786. commands a standard error stream that discards all output.
  1787. @item WRDE_UNDEF
  1788. @standards{POSIX.2, wordexp.h}
  1789. If the input refers to a shell variable that is not defined, report an
  1790. error.
  1791. @end vtable
  1792. @node Wordexp Example
  1793. @subsection @code{wordexp} Example
  1794. Here is an example of using @code{wordexp} to expand several strings
  1795. and use the results to run a shell command. It also shows the use of
  1796. @code{WRDE_APPEND} to concatenate the expansions and of @code{wordfree}
  1797. to free the space allocated by @code{wordexp}.
  1798. @smallexample
  1799. int
  1800. expand_and_execute (const char *program, const char **options)
  1801. @{
  1802. wordexp_t result;
  1803. pid_t pid
  1804. int status, i;
  1805. /* @r{Expand the string for the program to run.} */
  1806. switch (wordexp (program, &result, 0))
  1807. @{
  1808. case 0: /* @r{Successful}. */
  1809. break;
  1810. case WRDE_NOSPACE:
  1811. /* @r{If the error was @code{WRDE_NOSPACE},}
  1812. @r{then perhaps part of the result was allocated.} */
  1813. wordfree (&result);
  1814. default: /* @r{Some other error.} */
  1815. return -1;
  1816. @}
  1817. /* @r{Expand the strings specified for the arguments.} */
  1818. for (i = 0; options[i] != NULL; i++)
  1819. @{
  1820. if (wordexp (options[i], &result, WRDE_APPEND))
  1821. @{
  1822. wordfree (&result);
  1823. return -1;
  1824. @}
  1825. @}
  1826. pid = fork ();
  1827. if (pid == 0)
  1828. @{
  1829. /* @r{This is the child process. Execute the command.} */
  1830. execv (result.we_wordv[0], result.we_wordv);
  1831. exit (EXIT_FAILURE);
  1832. @}
  1833. else if (pid < 0)
  1834. /* @r{The fork failed. Report failure.} */
  1835. status = -1;
  1836. else
  1837. /* @r{This is the parent process. Wait for the child to complete.} */
  1838. if (waitpid (pid, &status, 0) != pid)
  1839. status = -1;
  1840. wordfree (&result);
  1841. return status;
  1842. @}
  1843. @end smallexample
  1844. @node Tilde Expansion
  1845. @subsection Details of Tilde Expansion
  1846. It's a standard part of shell syntax that you can use @samp{~} at the
  1847. beginning of a file name to stand for your own home directory. You
  1848. can use @samp{~@var{user}} to stand for @var{user}'s home directory.
  1849. @dfn{Tilde expansion} is the process of converting these abbreviations
  1850. to the directory names that they stand for.
  1851. Tilde expansion applies to the @samp{~} plus all following characters up
  1852. to whitespace or a slash. It takes place only at the beginning of a
  1853. word, and only if none of the characters to be transformed is quoted in
  1854. any way.
  1855. Plain @samp{~} uses the value of the environment variable @code{HOME}
  1856. as the proper home directory name. @samp{~} followed by a user name
  1857. uses @code{getpwname} to look up that user in the user database, and
  1858. uses whatever directory is recorded there. Thus, @samp{~} followed
  1859. by your own name can give different results from plain @samp{~}, if
  1860. the value of @code{HOME} is not really your home directory.
  1861. @node Variable Substitution
  1862. @subsection Details of Variable Substitution
  1863. Part of ordinary shell syntax is the use of @samp{$@var{variable}} to
  1864. substitute the value of a shell variable into a command. This is called
  1865. @dfn{variable substitution}, and it is one part of doing word expansion.
  1866. There are two basic ways you can write a variable reference for
  1867. substitution:
  1868. @table @code
  1869. @item $@{@var{variable}@}
  1870. If you write braces around the variable name, then it is completely
  1871. unambiguous where the variable name ends. You can concatenate
  1872. additional letters onto the end of the variable value by writing them
  1873. immediately after the close brace. For example, @samp{$@{foo@}s}
  1874. expands into @samp{tractors}.
  1875. @item $@var{variable}
  1876. If you do not put braces around the variable name, then the variable
  1877. name consists of all the alphanumeric characters and underscores that
  1878. follow the @samp{$}. The next punctuation character ends the variable
  1879. name. Thus, @samp{$foo-bar} refers to the variable @code{foo} and expands
  1880. into @samp{tractor-bar}.
  1881. @end table
  1882. When you use braces, you can also use various constructs to modify the
  1883. value that is substituted, or test it in various ways.
  1884. @table @code
  1885. @item $@{@var{variable}:-@var{default}@}
  1886. Substitute the value of @var{variable}, but if that is empty or
  1887. undefined, use @var{default} instead.
  1888. @item $@{@var{variable}:=@var{default}@}
  1889. Substitute the value of @var{variable}, but if that is empty or
  1890. undefined, use @var{default} instead and set the variable to
  1891. @var{default}.
  1892. @item $@{@var{variable}:?@var{message}@}
  1893. If @var{variable} is defined and not empty, substitute its value.
  1894. Otherwise, print @var{message} as an error message on the standard error
  1895. stream, and consider word expansion a failure.
  1896. @c ??? How does wordexp report such an error?
  1897. @c WRDE_BADVAL is returned.
  1898. @item $@{@var{variable}:+@var{replacement}@}
  1899. Substitute @var{replacement}, but only if @var{variable} is defined and
  1900. nonempty. Otherwise, substitute nothing for this construct.
  1901. @end table
  1902. @table @code
  1903. @item $@{#@var{variable}@}
  1904. Substitute a numeral which expresses in base ten the number of
  1905. characters in the value of @var{variable}. @samp{$@{#foo@}} stands for
  1906. @samp{7}, because @samp{tractor} is seven characters.
  1907. @end table
  1908. These variants of variable substitution let you remove part of the
  1909. variable's value before substituting it. The @var{prefix} and
  1910. @var{suffix} are not mere strings; they are wildcard patterns, just
  1911. like the patterns that you use to match multiple file names. But
  1912. in this context, they match against parts of the variable value
  1913. rather than against file names.
  1914. @table @code
  1915. @item $@{@var{variable}%%@var{suffix}@}
  1916. Substitute the value of @var{variable}, but first discard from that
  1917. variable any portion at the end that matches the pattern @var{suffix}.
  1918. If there is more than one alternative for how to match against
  1919. @var{suffix}, this construct uses the longest possible match.
  1920. Thus, @samp{$@{foo%%r*@}} substitutes @samp{t}, because the largest
  1921. match for @samp{r*} at the end of @samp{tractor} is @samp{ractor}.
  1922. @item $@{@var{variable}%@var{suffix}@}
  1923. Substitute the value of @var{variable}, but first discard from that
  1924. variable any portion at the end that matches the pattern @var{suffix}.
  1925. If there is more than one alternative for how to match against
  1926. @var{suffix}, this construct uses the shortest possible alternative.
  1927. Thus, @samp{$@{foo%r*@}} substitutes @samp{tracto}, because the shortest
  1928. match for @samp{r*} at the end of @samp{tractor} is just @samp{r}.
  1929. @item $@{@var{variable}##@var{prefix}@}
  1930. Substitute the value of @var{variable}, but first discard from that
  1931. variable any portion at the beginning that matches the pattern @var{prefix}.
  1932. If there is more than one alternative for how to match against
  1933. @var{prefix}, this construct uses the longest possible match.
  1934. Thus, @samp{$@{foo##*t@}} substitutes @samp{or}, because the largest
  1935. match for @samp{*t} at the beginning of @samp{tractor} is @samp{tract}.
  1936. @item $@{@var{variable}#@var{prefix}@}
  1937. Substitute the value of @var{variable}, but first discard from that
  1938. variable any portion at the beginning that matches the pattern @var{prefix}.
  1939. If there is more than one alternative for how to match against
  1940. @var{prefix}, this construct uses the shortest possible alternative.
  1941. Thus, @samp{$@{foo#*t@}} substitutes @samp{ractor}, because the shortest
  1942. match for @samp{*t} at the beginning of @samp{tractor} is just @samp{t}.
  1943. @end table