]>
git.saurik.com Git - apple/libc.git/blob - gen/wordexp.c
2 * Copyright 1994, University Corporation for Atmospheric Research
3 * See ../COPYRIGHT file for copying and redistribution conditions.
6 * Reproduction of ../COPYRIGHT file:
8 *********************************************************************
10 Copyright 1995-2002 University Corporation for Atmospheric Research/Unidata
12 Portions of this software were developed by the Unidata Program at the
13 University Corporation for Atmospheric Research.
15 Access and use of this software shall impose the following obligations
16 and understandings on the user. The user is granted the right, without
17 any fee or cost, to use, copy, modify, alter, enhance and distribute
18 this software, and any derivative works thereof, and its supporting
19 documentation for any purpose whatsoever, provided that this entire
20 notice appears in all copies of the software, derivative works and
21 supporting documentation. Further, UCAR requests that the user credit
22 UCAR/Unidata in any publications that result from the use of this
23 software or in any product that includes this software. The names UCAR
24 and/or Unidata, however, may not be used in any advertising or publicity
25 to endorse or promote any products or commercial entity unless specific
26 written permission is obtained from UCAR/Unidata. The user also
27 understands that UCAR/Unidata is not obligated to provide the user with
28 any support, consulting, training or assistance of any kind with regard
29 to the use, operation and performance of this software nor to provide
30 the user with any updates, revisions, new versions or "bug fixes."
32 THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR
33 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL,
36 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
37 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
38 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
39 WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE.
41 *********************************************************************
44 /* $Id: wordexp.c,v 1.13 2002/12/26 16:46:46 steve Exp $ */
47 #include "ldmconfig.h"
51 * Hack to provide POSIX 1003.2-1992 _interface_.
52 * NOT fully functional
55 #include "xlocale_private.h"
66 * Translate return value from wordexp() into a string
69 s_wrde_err(int wrde_err
)
72 case 0: return "No Error";
73 case WRDE_BADCHAR
: return "WRDE_BADCHAR";
74 case WRDE_BADVAL
: return "WRDE_BADVAL";
75 case WRDE_CMDSUB
: return "WRDE_CMDSUB";
76 case WRDE_NOSPACE
: return "WRDE_NOSPACE";
77 case WRDE_SYNTAX
: return "WRDE_SYNTAX";
80 return "Unknown Error";
86 wordexp(const char *words
, wordexp_t
*pwordexp
, int flags
)
92 enum states
{ARGSTART
, IN_QUOTE
, NOT_IN_QUOTE
, DONE
};
93 enum classes
{EOS
, SPACE
, QUOTE
, OTHER
};
99 locale_t loc
= __current_locale();
101 /* devour leading white space */
102 for(ccp
= words
; *ccp
!= 0 && isspace_l(*ccp
, loc
); )
107 pwordexp
->we_wordc
= 0;
108 pwordexp
->we_wordv
= NULL
;
112 /* If every other character was a space ... */
113 #define MAXNARGS(str) ((strlen(str) +1)/2 +1)
114 argv
= (char **)calloc(MAXNARGS(ccp
), sizeof(char *));
120 argbuf
= malloc(strlen(words
)+1); /* where each arg is built */
136 else if (isspace_l(*sp
, loc
))
157 state
= NOT_IN_QUOTE
;
163 case EOS
: /* unmatched quote */
165 status
= WRDE_SYNTAX
;
169 state
= NOT_IN_QUOTE
;
182 argv
[argc
++] = strdup(argbuf
);
188 argv
[argc
++] = strdup(argbuf
);
205 pwordexp
->we_wordc
= argc
;
206 pwordexp
->we_wordv
= argv
;
215 wordfree(wordexp_t
*pwordexp
)
217 if(pwordexp
== NULL
|| pwordexp
->we_wordv
== NULL
)
219 if(*pwordexp
->we_wordv
)
220 free(*pwordexp
->we_wordv
);
221 free(pwordexp
->we_wordv
);
232 main(int argc
, char *argv
[])
239 while(fgets(strbuf
, sizeof(strbuf
), stdin
) != NULL
)
242 char *cp
= strrchr(strbuf
,'\n');
246 fprintf(stdout
, "\t%s\n", strbuf
);
247 status
= wordexp(strbuf
, &wrdexp
, WRDE_SHOWERR
);
250 fprintf(stderr
, "wordexp: %s\n", s_wrde_err(status
));
254 fprintf(stdout
, "\t%d:\n", wrdexp
.we_wordc
);
255 for(cpp
= wrdexp
.we_wordv
;
256 cpp
< &wrdexp
.we_wordv
[wrdexp
.we_wordc
]; cpp
++)
258 fprintf(stdout
, "\t\t%s\n", *cpp
);