3 Copyright 2003, 2004, 2005, 2006, 2008 Red Hat, Inc.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
19 #include "sys/cygwin.h"
22 linebuf::finish (bool cmdlenoverflow_ok)
28 if (ix-- > MAXCYGWINCMDLEN && cmdlenoverflow_ok)
29 ix = MAXCYGWINCMDLEN - 1;
35 linebuf::add (const char *what, int len)
37 size_t newix = ix + len;
38 if (newix >= alloced || !buf)
40 alloced += LINE_BUF_CHUNK + newix;
41 buf = (char *) realloc (buf, alloced + 1);
43 memcpy (buf + ix, what, len);
49 linebuf::prepend (const char *what, int len)
53 if ((newix = ix + len) >= alloced)
55 alloced += LINE_BUF_CHUNK + newix;
56 buf = (char *) realloc (buf, alloced + 1);
59 if ((buflen = strlen (buf)))
60 memmove (buf + len, buf, buflen + 1);
63 memcpy (buf, what, len);
68 linebuf::fromargv (av& newargv, const char *real_path, bool cmdlenoverflow_ok)
71 for (int i = 0; i < newargv.argc; i++)
76 newargv.dup_maybe (i);
77 a = i ? newargv[i] : (char *) real_path;
79 if (len != 0 && !strpbrk (a, " \t\n\r\""))
84 /* Handle embedded special characters " and \.
85 A " is always preceded by a \.
86 A \ is not special unless it precedes a ". If it does,
87 then all preceding \'s must be doubled to avoid having
88 the Windows command line parser interpret the \ as quoting
89 the ". This rule applies to a string of \'s before the end
90 of the string, since cygwin/windows uses a " to delimit the
92 for (; (p = strpbrk (a, "\"\\")); a = ++p)
95 /* Find length of string of backslashes */
96 int n = strspn (p, "\\");
98 add ("\\\"", 2); /* No backslashes, so it must be a ".
99 The " has to be protected with a backslash. */
102 add (p, n); /* Add the run of backslashes */
103 /* Need to double up all of the preceding
104 backslashes if they precede a quote or EOS. */
105 if (!p[n] || p[n] == '"')
107 p += n - 1; /* Point to last backslash */
117 finish (cmdlenoverflow_ok);
119 if (ix >= MAXWINCMDLEN)
121 debug_printf ("command line too long (>32K), return E2BIG");
130 av::unshift (const char *what, int conv)
133 av = (char **) crealloc (argv, (argc + 2) * sizeof (char *));
138 memmove (argv + 1, argv, (argc + 1) * sizeof (char *));
140 char *buf = tp.c_get ();
143 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_RELATIVE, what, buf,
145 char *p = strchr (buf, '\0') - 4;
146 if (p > buf && ascii_strcasematch (p, ".exe"))
150 *argv = cstrdup1 (what);