]>
git.saurik.com Git - apple/libc.git/blob - gen/wordexp.c
a75b9441a40502678da991474cc4016049ab5ab2
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
64 * Translate return value from wordexp() into a string
67 s_wrde_err(int wrde_err
)
70 case 0: return "No Error";
71 case WRDE_BADCHAR
: return "WRDE_BADCHAR";
72 case WRDE_BADVAL
: return "WRDE_BADVAL";
73 case WRDE_CMDSUB
: return "WRDE_CMDSUB";
74 case WRDE_NOSPACE
: return "WRDE_NOSPACE";
75 case WRDE_SYNTAX
: return "WRDE_SYNTAX";
78 return "Unknown Error";
84 wordexp(const char *words
, wordexp_t
*pwordexp
, int flags
)
90 enum states
{ARGSTART
, IN_QUOTE
, NOT_IN_QUOTE
, DONE
};
91 enum classes
{EOS
, SPACE
, QUOTE
, OTHER
};
98 /* devour leading white space */
99 for(ccp
= words
; *ccp
!= 0 && isspace(*ccp
); )
104 pwordexp
->we_wordc
= 0;
105 pwordexp
->we_wordv
= NULL
;
109 /* If every other character was a space ... */
110 #define MAXNARGS(str) ((strlen(str) +1)/2 +1)
111 argv
= (char **)calloc(MAXNARGS(ccp
), sizeof(char *));
117 argbuf
= malloc(strlen(words
)+1); /* where each arg is built */
133 else if (isspace(*sp
))
154 state
= NOT_IN_QUOTE
;
160 case EOS
: /* unmatched quote */
162 status
= WRDE_SYNTAX
;
166 state
= NOT_IN_QUOTE
;
179 argv
[argc
++] = strdup(argbuf
);
185 argv
[argc
++] = strdup(argbuf
);
202 pwordexp
->we_wordc
= argc
;
203 pwordexp
->we_wordv
= argv
;
212 wordfree(wordexp_t
*pwordexp
)
214 if(pwordexp
== NULL
|| pwordexp
->we_wordv
== NULL
)
216 if(*pwordexp
->we_wordv
)
217 free(*pwordexp
->we_wordv
);
218 free(pwordexp
->we_wordv
);
229 main(int argc
, char *argv
[])
236 while(fgets(strbuf
, sizeof(strbuf
), stdin
) != NULL
)
239 char *cp
= strrchr(strbuf
,'\n');
243 fprintf(stdout
, "\t%s\n", strbuf
);
244 status
= wordexp(strbuf
, &wrdexp
, WRDE_SHOWERR
);
247 fprintf(stderr
, "wordexp: %s\n", s_wrde_err(status
));
251 fprintf(stdout
, "\t%d:\n", wrdexp
.we_wordc
);
252 for(cpp
= wrdexp
.we_wordv
;
253 cpp
< &wrdexp
.we_wordv
[wrdexp
.we_wordc
]; cpp
++)
255 fprintf(stdout
, "\t\t%s\n", *cpp
);