]> err.no Git - pkg-config/blob - poptparse.c
Print out \r\n on windows, not just \n
[pkg-config] / poptparse.c
1 /*
2 Copyright (c) 1998  Red Hat Software
3  
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10  
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13  
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  
21 Except as contained in this notice, the name of the X Consortium shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from the X Consortium.
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #ifdef HAVE_ALLOCA_H
31 # include <alloca.h>
32 #else
33 # ifdef _AIX
34 #  pragma alloca
35 # endif
36 #endif
37
38 #ifdef HAVE_MALLOC_H
39 # include <malloc.h>
40 #endif
41
42 #include <ctype.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "popt.h"
47
48 int poptParseArgvString(char * s, int * argcPtr, char *** argvPtr) {
49     char * buf = strcpy(alloca(strlen(s) + 1), s);
50     char * bufStart = buf;
51     char * src, * dst;
52     char quote = '\0';
53     int argvAlloced = 5;
54     char ** argv = malloc(sizeof(*argv) * argvAlloced);
55     char ** argv2;
56     int argc = 0;
57     int i;
58
59     src = s;
60     dst = buf;
61     argv[argc] = buf;
62
63     memset(buf, '\0', strlen(s) + 1);
64
65     while (*src) {
66         if (quote == *src) {
67             quote = '\0';
68         } else if (quote) {
69             if (*src == '\\') {
70                 src++;
71                 if (!*src) {
72                     free(argv);
73                     return POPT_ERROR_BADQUOTE;
74                 }
75                 if (*src != quote) *buf++ = '\\';
76             }
77             *buf++ = *src;
78         } else if (isspace(*src)) {
79             if (*argv[argc]) {
80                 buf++, argc++;
81                 if (argc == argvAlloced) {
82                     argvAlloced += 5;
83                     argv = realloc(argv, sizeof(*argv) * argvAlloced);
84                 }
85                 argv[argc] = buf;
86             }
87         } else switch (*src) {
88           case '"':
89           case '\'':
90             quote = *src;
91             break;
92           case '\\':
93             src++;
94             if (!*src) {
95                 free(argv);
96                 return POPT_ERROR_BADQUOTE;
97             }
98             /* fallthrough */
99           default:
100             *buf++ = *src;
101         }
102
103         src++;
104     }
105
106     if (strlen(argv[argc])) {
107         argc++, buf++;
108     }
109
110     dst = malloc(argc * sizeof(*argv) + (buf - bufStart));
111     argv2 = (void *) dst;
112     dst += argc * sizeof(*argv);
113     memcpy(argv2, argv, argc * sizeof(*argv));
114     memcpy(dst, bufStart, buf - bufStart);
115
116     for (i = 0; i < argc; i++) {
117         argv2[i] = dst + (argv[i] - bufStart);
118     }
119
120     free(argv);
121
122     *argvPtr = argv2;
123     *argcPtr = argc;
124
125     return 0;
126 }