logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

wordexp.3p (15246B)


  1. '\" et
  2. .TH WORDEXP "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. wordexp,
  12. wordfree
  13. \(em perform word expansions
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <wordexp.h>
  18. .P
  19. int wordexp(const char *restrict \fIwords\fP, wordexp_t *restrict \fIpwordexp\fP,
  20. int \fIflags\fP);
  21. void wordfree(wordexp_t *\fIpwordexp\fP);
  22. .fi
  23. .SH DESCRIPTION
  24. The
  25. \fIwordexp\fR()
  26. function shall perform word expansions as described in the Shell and Utilities volume of POSIX.1\(hy2017,
  27. .IR "Section 2.6" ", " "Word Expansions",
  28. subject to quoting as described in the Shell and Utilities volume of POSIX.1\(hy2017,
  29. .IR "Section 2.2" ", " "Quoting",
  30. and place the list of expanded words into the structure pointed to by
  31. .IR pwordexp .
  32. .P
  33. The
  34. .IR words
  35. argument is a pointer to a string containing one or more words to be
  36. expanded. The expansions shall be the same as would be performed by the
  37. command line interpreter if
  38. .IR words
  39. were the part of a command line representing the arguments to a
  40. utility. Therefore, the application shall ensure that
  41. .IR words
  42. does not contain an unquoted
  43. <newline>
  44. character or any of the unquoted shell special characters
  45. .BR '|' ,
  46. .BR '&' ,
  47. .BR ';' ,
  48. .BR '<' ,
  49. .BR '>'
  50. except in the context of command substitution as specified in the Shell and Utilities volume of POSIX.1\(hy2017,
  51. .IR "Section 2.6.3" ", " "Command Substitution".
  52. It also shall not contain unquoted parentheses or braces, except
  53. in the context of command or variable substitution. The application
  54. shall ensure that every member of
  55. .IR words
  56. which it expects to have expanded by
  57. \fIwordexp\fR()
  58. does not contain an unquoted initial comment character. The application
  59. shall also ensure that any words which it intends to be ignored
  60. (because they begin or continue a comment) are deleted from
  61. .IR words .
  62. If the argument
  63. .IR words
  64. contains an unquoted comment character (\c
  65. <number-sign>)
  66. that is the beginning of a token,
  67. \fIwordexp\fR()
  68. shall either treat the comment character as a regular character, or
  69. interpret it as a comment indicator and ignore the remainder of
  70. .IR words .
  71. .P
  72. The structure type
  73. .BR wordexp_t
  74. is defined in the
  75. .IR <wordexp.h>
  76. header and includes at least the following members:
  77. .TS
  78. center box tab(!);
  79. cB | cB | cB
  80. lw(1.25i)B | lw(1.25i)I | lw(2.5i).
  81. Member Type!Member Name!Description
  82. _
  83. size_t!we_wordc!Count of words matched by \fIwords\fP.
  84. char **!we_wordv!Pointer to list of expanded words.
  85. size_t!we_offs!T{
  86. Slots to reserve at the beginning of \fIpwordexp\fP\->\fIwe_wordv\fR.
  87. T}
  88. .TE
  89. .P
  90. The
  91. \fIwordexp\fR()
  92. function shall store the number of generated words into
  93. \fIpwordexp\fP\->\fIwe_wordc\fP and a pointer to a list of pointers to
  94. words in \fIpwordexp\fP\->\fIwe_wordv\fP. Each individual field created
  95. during field splitting (see the Shell and Utilities volume of POSIX.1\(hy2017,
  96. .IR "Section 2.6.5" ", " "Field Splitting")
  97. or pathname expansion (see the Shell and Utilities volume of POSIX.1\(hy2017,
  98. .IR "Section 2.6.6" ", " "Pathname Expansion")
  99. shall be a separate word in the \fIpwordexp\fP\->\fIwe_wordv\fP
  100. list. The words shall be in order as described in the Shell and Utilities volume of POSIX.1\(hy2017,
  101. .IR "Section 2.6" ", " "Word Expansions".
  102. The first pointer after the last word pointer shall be a null pointer.
  103. The expansion of special parameters described in the Shell and Utilities volume of POSIX.1\(hy2017,
  104. .IR "Section 2.5.2" ", " "Special Parameters"
  105. is unspecified.
  106. .P
  107. It is the caller's responsibility to allocate the storage pointed to by
  108. .IR pwordexp .
  109. The
  110. \fIwordexp\fR()
  111. function shall allocate other space as needed, including memory
  112. pointed to by \fIpwordexp\fP\->\fIwe_wordv\fP. The
  113. \fIwordfree\fR()
  114. function frees any memory associated with
  115. .IR pwordexp
  116. from a previous call to
  117. \fIwordexp\fR().
  118. .P
  119. The
  120. .IR flags
  121. argument is used to control the behavior of
  122. \fIwordexp\fR().
  123. The value of
  124. .IR flags
  125. is the bitwise-inclusive OR of zero or more of the following constants,
  126. which are defined in
  127. .IR <wordexp.h> :
  128. .IP WRDE_APPEND 14
  129. Append words generated to the ones from a previous call to
  130. \fIwordexp\fR().
  131. .IP WRDE_DOOFFS 14
  132. Make use of \fIpwordexp\fP\->\fIwe_offs\fP. If this flag is set,
  133. \fIpwordexp\fP\->\fIwe_offs\fP is used to specify how many null
  134. pointers to add to the beginning of \fIpwordexp\fP\->\fIwe_wordv\fP.
  135. In other words, \fIpwordexp\fP\->\fIwe_wordv\fP shall point to
  136. \fIpwordexp\fP\->\fIwe_offs\fP null pointers, followed by
  137. \fIpwordexp\fP\->\fIwe_wordc\fP word pointers, followed by a null
  138. pointer.
  139. .IP WRDE_NOCMD 14
  140. If the implementation supports the utilities defined in the Shell and Utilities volume of POSIX.1\(hy2017,
  141. fail if command substitution, as specified in the Shell and Utilities volume of POSIX.1\(hy2017,
  142. .IR "Section 2.6.3" ", " "Command Substitution",
  143. is requested.
  144. .IP WRDE_REUSE 14
  145. The
  146. .IR pwordexp
  147. argument was passed to a previous successful call to
  148. \fIwordexp\fR(),
  149. and has not been passed to
  150. \fIwordfree\fR().
  151. The result shall be the same as if the application had called
  152. \fIwordfree\fR()
  153. and then called
  154. \fIwordexp\fR()
  155. without WRDE_REUSE.
  156. .IP WRDE_SHOWERR 14
  157. Do not redirect
  158. .IR stderr
  159. to
  160. .BR /dev/null .
  161. .IP WRDE_UNDEF 14
  162. Report error on an attempt to expand an undefined shell variable.
  163. .P
  164. The WRDE_APPEND flag can be used to append a new set of words to those
  165. generated by a previous call to
  166. \fIwordexp\fR().
  167. The following rules apply to applications when two or more calls to
  168. \fIwordexp\fR()
  169. are made with the same value of
  170. .IR pwordexp
  171. and without intervening calls to
  172. \fIwordfree\fR():
  173. .IP " 1." 4
  174. The first such call shall not set WRDE_APPEND. All subsequent calls
  175. shall set it.
  176. .IP " 2." 4
  177. All of the calls shall set WRDE_DOOFFS, or all shall not set it.
  178. .IP " 3." 4
  179. After the second and each subsequent call,
  180. \fIpwordexp\fP\->\fIwe_wordv\fP shall point to a list containing the
  181. following:
  182. .RS 4
  183. .IP " a." 4
  184. Zero or more null pointers, as specified by WRDE_DOOFFS and
  185. \fIpwordexp\fP\->\fIwe_offs\fP
  186. .IP " b." 4
  187. Pointers to the words that were in the \fIpwordexp\fP\->\fIwe_wordv\fP
  188. list before the call, in the same order as before
  189. .IP " c." 4
  190. Pointers to the new words generated by the latest call, in the
  191. specified order
  192. .RE
  193. .IP " 4." 4
  194. The count returned in \fIpwordexp\fP\->\fIwe_wordc\fP shall be the
  195. total number of words from all of the calls.
  196. .IP " 5." 4
  197. The application can change any of the fields after a call to
  198. \fIwordexp\fR(),
  199. but if it does it shall reset them to the original value before a
  200. subsequent call, using the same
  201. .IR pwordexp
  202. value, to
  203. \fIwordfree\fR()
  204. or
  205. \fIwordexp\fR()
  206. with the WRDE_APPEND or WRDE_REUSE flag.
  207. .P
  208. If the implementation supports the utilities defined in the Shell and Utilities volume of POSIX.1\(hy2017,
  209. and
  210. .IR words
  211. contains an unquoted character\(em\c
  212. <newline>,
  213. .BR '|' ,
  214. .BR '&' ,
  215. .BR ';' ,
  216. .BR '<' ,
  217. .BR '>' ,
  218. .BR '(' ,
  219. .BR ')' ,
  220. .BR '{' ,
  221. .BR '}' \(em\c
  222. in an inappropriate context,
  223. \fIwordexp\fR()
  224. shall fail, and the number of expanded words shall be 0.
  225. .P
  226. Unless WRDE_SHOWERR is set in
  227. .IR flags ,
  228. \fIwordexp\fR()
  229. shall redirect
  230. .IR stderr
  231. to
  232. .BR /dev/null
  233. for any utilities executed as a result of command substitution while
  234. expanding
  235. .IR words .
  236. If WRDE_SHOWERR is set,
  237. \fIwordexp\fR()
  238. may write messages to
  239. .IR stderr
  240. if syntax errors are detected while expanding
  241. .IR words ,
  242. unless the
  243. .IR stderr
  244. stream has wide orientation in which case the behavior is undefined.
  245. It is unspecified whether any write errors encountered while
  246. outputting such messages will affect the
  247. .IR stderr
  248. error indicator or the value of
  249. .IR errno .
  250. .P
  251. The application shall ensure that if WRDE_DOOFFS is set, then
  252. \fIpwordexp\fP\->\fIwe_offs\fP has the same value for each
  253. \fIwordexp\fR()
  254. call and
  255. \fIwordfree\fR()
  256. call using a given
  257. .IR pwordexp .
  258. .P
  259. The results are unspecified if WRDE_APPEND and WRDE_REUSE are
  260. both specified.
  261. .br
  262. .P
  263. The following constants are defined as error return values:
  264. .IP WRDE_BADCHAR 14
  265. One of the unquoted characters\(em\c
  266. <newline>,
  267. .BR '|' ,
  268. .BR '&' ,
  269. .BR ';' ,
  270. .BR '<' ,
  271. .BR '>' ,
  272. .BR '(' ,
  273. .BR ')' ,
  274. .BR '{' ,
  275. .BR '}' \(em\c
  276. appears in
  277. .IR words
  278. in an inappropriate context.
  279. .IP WRDE_BADVAL 14
  280. Reference to undefined shell variable when WRDE_UNDEF is set in
  281. .IR flags .
  282. .IP WRDE_CMDSUB 14
  283. Command substitution requested when WRDE_NOCMD was set in
  284. .IR flags .
  285. .IP WRDE_NOSPACE 14
  286. Attempt to allocate memory failed.
  287. .IP WRDE_SYNTAX 14
  288. Shell syntax error, such as unbalanced parentheses or unterminated
  289. string.
  290. .SH "RETURN VALUE"
  291. Upon successful completion,
  292. \fIwordexp\fR()
  293. shall return 0. Otherwise, a non-zero value, as described in
  294. .IR <wordexp.h> ,
  295. shall be returned to indicate an error. If
  296. \fIwordexp\fR()
  297. returns the value WRDE_NOSPACE, then \fIpwordexp\fP\->\fIwe_wordc\fP
  298. and \fIpwordexp\fP\->\fIwe_wordv\fP shall be updated to reflect any
  299. words that were successfully expanded. In other error cases, if the
  300. WRDE_APPEND flag was specified, \fIpwordexp\fP->\fIwe_wordc\fP and
  301. \fIpwordexp\fP->\fIwe_wordv\fP shall not be modified.
  302. .P
  303. The
  304. \fIwordfree\fR()
  305. function shall not return a value.
  306. .SH ERRORS
  307. No errors are defined.
  308. .LP
  309. .IR "The following sections are informative."
  310. .SH EXAMPLES
  311. None.
  312. .SH "APPLICATION USAGE"
  313. The
  314. \fIwordexp\fR()
  315. function is intended to be used by an application that wants to do all
  316. of the shell's expansions on a word or words obtained from a user. For
  317. example, if the application prompts for a pathname (or list of
  318. pathnames) and then uses
  319. \fIwordexp\fR()
  320. to process the input, the user could respond with anything that would
  321. be valid as input to the shell.
  322. .P
  323. The WRDE_NOCMD flag is provided for applications that, for security or
  324. other reasons, want to prevent a user from executing shell commands.
  325. Disallowing unquoted shell special characters also prevents unwanted
  326. side-effects, such as executing a command or writing a file.
  327. .P
  328. POSIX.1\(hy2008 does not require the
  329. \fIwordexp\fR()
  330. function to be thread-safe if passed an expression referencing an
  331. environment variable while any other thread is concurrently modifying
  332. any environment variable; see
  333. .IR "\fIexec\fR\^".
  334. .P
  335. Even though the WRDE_SHOWERR flag allows the implementation to write
  336. messages to
  337. .IR stderr
  338. during command substitution or syntax errors, this standard does not
  339. provide any way to detect write failures during the output of such
  340. messages.
  341. .P
  342. Applications which use wide-character output functions with
  343. .IR stderr
  344. should ensure that any calls to
  345. \fIwordexp\fR()
  346. do not write to
  347. .IR stderr ,
  348. by avoiding use of the WRDE_SHOWERR flag.
  349. .SH RATIONALE
  350. This function was included as an alternative to
  351. \fIglob\fR().
  352. There had been continuing controversy over exactly what features should
  353. be included in
  354. \fIglob\fR().
  355. It is hoped that by providing
  356. \fIwordexp\fR()
  357. (which provides all of the shell word expansions, but which may be slow
  358. to execute) and
  359. \fIglob\fR()
  360. (which is faster, but which only performs pathname expansion, without
  361. tilde or parameter expansion) this will satisfy the majority of
  362. applications.
  363. .P
  364. While
  365. \fIwordexp\fR()
  366. could be implemented entirely as a library routine, it is expected that
  367. most implementations run a shell in a subprocess to do the expansion.
  368. .P
  369. Two different approaches have been proposed for how the required
  370. information might be presented to the shell and the results returned.
  371. They are presented here as examples.
  372. .P
  373. One proposal is to extend the
  374. .IR echo
  375. utility by adding a
  376. .BR \-q
  377. option. This option would cause
  378. .IR echo
  379. to add a
  380. <backslash>
  381. before each
  382. <backslash>
  383. and
  384. <blank>
  385. that occurs within an argument. The
  386. \fIwordexp\fR()
  387. function could then invoke the shell as follows:
  388. .sp
  389. .RS 4
  390. .nf
  391. (void) strcpy(buffer, "echo -q");
  392. (void) strcat(buffer, \fIwords\fP);
  393. if ((flags & WRDE_SHOWERR) == 0)
  394. (void) strcat(buffer, "2>/dev/null");
  395. f = popen(buffer, "r");
  396. .fi
  397. .P
  398. .RE
  399. .P
  400. The
  401. \fIwordexp\fR()
  402. function would read the resulting output, remove unquoted
  403. <backslash>
  404. characters, and break into words at unquoted
  405. <blank>
  406. characters. If the WRDE_NOCMD flag was set,
  407. \fIwordexp\fR()
  408. would have to scan
  409. .IR words
  410. before starting the subshell to make sure that there would be no
  411. command substitution. In any case, it would have to scan
  412. .IR words
  413. for unquoted special characters.
  414. .P
  415. Another proposal is to add the following options to
  416. .IR sh :
  417. .IP "\fB\-w\fP\ \fIwordlist\fR" 6
  418. .br
  419. This option provides a wordlist expansion service to applications. The
  420. words in
  421. .IR wordlist
  422. shall be expanded and the following written to standard output:
  423. .RS 6
  424. .IP " 1." 4
  425. The count of the number of words after expansion, in decimal, followed
  426. by a null byte
  427. .IP " 2." 4
  428. The number of bytes needed to represent the expanded words (not
  429. including null separators), in decimal, followed by a null byte
  430. .IP " 3." 4
  431. The expanded words, each terminated by a null byte
  432. .P
  433. If an error is encountered during word expansion,
  434. .IR sh
  435. exits with a non-zero status after writing the former to report any
  436. words successfully expanded
  437. .RE
  438. .IP "\fB\-P\fP" 6
  439. Run in ``protected'' mode. If specified with the
  440. .BR \-w
  441. option, no command substitution shall be performed.
  442. .P
  443. With these options,
  444. \fIwordexp\fR()
  445. could be implemented fairly simply by creating a subprocess using
  446. \fIfork\fR()
  447. and executing
  448. .IR sh
  449. using the line:
  450. .sp
  451. .RS 4
  452. .nf
  453. execl(<\fIshell path\fP>, "sh", "-P", "-w", \fIwords\fP, (char *)0);
  454. .fi
  455. .P
  456. .RE
  457. .P
  458. after directing standard error to
  459. .BR /dev/null .
  460. .P
  461. It seemed objectionable for a library routine to write messages to
  462. standard error, unless explicitly requested, so
  463. \fIwordexp\fR()
  464. is required to redirect standard error to
  465. .BR /dev/null
  466. to ensure that no messages are generated, even for commands executed
  467. for command substitution. The WRDE_SHOWERR flag can be specified to
  468. request that error messages be written.
  469. .P
  470. The WRDE_REUSE flag allows the implementation to avoid the expense of
  471. freeing and reallocating memory, if that is possible. A minimal
  472. implementation can call
  473. \fIwordfree\fR()
  474. when WRDE_REUSE is set.
  475. .SH "FUTURE DIRECTIONS"
  476. None.
  477. .SH "SEE ALSO"
  478. .IR "\fIexec\fR\^",
  479. .IR "\fIfnmatch\fR\^(\|)",
  480. .IR "\fIglob\fR\^(\|)"
  481. .P
  482. The Base Definitions volume of POSIX.1\(hy2017,
  483. .IR "\fB<wordexp.h>\fP"
  484. .P
  485. The Shell and Utilities volume of POSIX.1\(hy2017,
  486. .IR "Chapter 2" ", " "Shell Command Language"
  487. .\"
  488. .SH COPYRIGHT
  489. Portions of this text are reprinted and reproduced in electronic form
  490. from IEEE Std 1003.1-2017, Standard for Information Technology
  491. -- Portable Operating System Interface (POSIX), The Open Group Base
  492. Specifications Issue 7, 2018 Edition,
  493. Copyright (C) 2018 by the Institute of
  494. Electrical and Electronics Engineers, Inc and The Open Group.
  495. In the event of any discrepancy between this version and the original IEEE and
  496. The Open Group Standard, the original IEEE and The Open Group Standard
  497. is the referee document. The original Standard can be obtained online at
  498. http://www.opengroup.org/unix/online.html .
  499. .PP
  500. Any typographical or formatting errors that appear
  501. in this page are most likely
  502. to have been introduced during the conversion of the source files to
  503. man page format. To report such errors, see
  504. https://www.kernel.org/doc/man-pages/reporting_bugs.html .