3 This software is a copyrighted work licensed under the terms of the
4 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
17 #include "sys/cygwin.h"
20 linebuf::finish (bool cmdlenoverflow_ok
)
26 if (ix
-- > MAXCYGWINCMDLEN
&& cmdlenoverflow_ok
)
27 ix
= MAXCYGWINCMDLEN
- 1;
33 linebuf::add (const char *what
, int len
)
35 size_t newix
= ix
+ len
;
36 if (newix
>= alloced
|| !buf
)
38 alloced
+= LINE_BUF_CHUNK
+ newix
;
39 buf
= (char *) realloc (buf
, alloced
+ 1);
41 memcpy (buf
+ ix
, what
, len
);
47 linebuf::prepend (const char *what
, int len
)
51 if ((newix
= ix
+ len
) >= alloced
)
53 alloced
+= LINE_BUF_CHUNK
+ newix
;
54 buf
= (char *) realloc (buf
, alloced
+ 1);
57 if ((buflen
= strlen (buf
)))
58 memmove (buf
+ len
, buf
, buflen
+ 1);
61 memcpy (buf
, what
, len
);
66 linebuf::fromargv (av
& newargv
, const char *real_path
, bool cmdlenoverflow_ok
)
69 for (int i
= 0; i
< newargv
.argc
; i
++)
74 a
= i
? newargv
[i
] : (char *) real_path
;
76 if (len
!= 0 && !strpbrk (a
, " \t\n\r\""))
81 /* Handle embedded special characters " and \.
82 A " is always preceded by a \.
83 A \ is not special unless it precedes a ". If it does,
84 then all preceding \'s must be doubled to avoid having
85 the Windows command line parser interpret the \ as quoting
86 the ". This rule applies to a string of \'s before the end
87 of the string, since cygwin/windows uses a " to delimit the
89 for (; (p
= strpbrk (a
, "\"\\")); a
= ++p
)
92 /* Find length of string of backslashes */
93 int n
= strspn (p
, "\\");
95 add ("\\\"", 2); /* No backslashes, so it must be a ".
96 The " has to be protected with a backslash. */
99 add (p
, n
); /* Add the run of backslashes */
100 /* Need to double up all of the preceding
101 backslashes if they precede a quote or EOS. */
102 if (!p
[n
] || p
[n
] == '"')
104 p
+= n
- 1; /* Point to last backslash */
114 finish (cmdlenoverflow_ok
);
116 if (ix
>= MAXWINCMDLEN
)
118 debug_printf ("command line too long (>32K), return E2BIG");
127 av::unshift (const char *what
)
130 av
= (char **) crealloc (argv
, (argc
+ 2) * sizeof (char *));
135 memmove (argv
+ 1, argv
, (argc
+ 1) * sizeof (char *));
136 *argv
= cstrdup1 (what
);