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
20 #include "sys/cygwin.h"
23 linebuf::finish (bool cmdlenoverflow_ok)
29 if (ix-- > MAXCYGWINCMDLEN && cmdlenoverflow_ok)
30 ix = MAXCYGWINCMDLEN - 1;
36 linebuf::add (const char *what, int len)
38 size_t newix = ix + len;
39 if (newix >= alloced || !buf)
41 alloced += LINE_BUF_CHUNK + newix;
42 buf = (char *) realloc (buf, alloced + 1);
44 memcpy (buf + ix, what, len);
50 linebuf::prepend (const char *what, int len)
54 if ((newix = ix + len) >= alloced)
56 alloced += LINE_BUF_CHUNK + newix;
57 buf = (char *) realloc (buf, alloced + 1);
60 if ((buflen = strlen (buf)))
61 memmove (buf + len, buf, buflen + 1);
64 memcpy (buf, what, len);
69 linebuf::fromargv (av& newargv, char *real_path, bool cmdlenoverflow_ok)
72 for (int i = 0; i < newargv.argc; i++)
77 newargv.dup_maybe (i);
78 a = i ? newargv[i] : (char *) real_path;
80 if (len != 0 && !strpbrk (a, " \t\n\r\""))
85 /* Handle embedded special characters " and \.
86 A " is always preceded by a \.
87 A \ is not special unless it precedes a ". If it does,
88 then all preceding \'s must be doubled to avoid having
89 the Windows command line parser interpret the \ as quoting
90 the ". This rule applies to a string of \'s before the end
91 of the string, since cygwin/windows uses a " to delimit the
93 for (; (p = strpbrk (a, "\"\\")); a = ++p)
96 /* Find length of string of backslashes */
97 int n = strspn (p, "\\");
99 add ("\\\"", 2); /* No backslashes, so it must be a ".
100 The " has to be protected with a backslash. */
103 add (p, n); /* Add the run of backslashes */
104 /* Need to double up all of the preceding
105 backslashes if they precede a quote or EOS. */
106 if (!p[n] || p[n] == '"')
108 p += n - 1; /* Point to last backslash */
118 finish (cmdlenoverflow_ok);
120 if (ix >= MAXWINCMDLEN)
122 debug_printf ("command line too long (>32K), return E2BIG");
131 av::unshift (const char *what, int conv)
134 av = (char **) crealloc (argv, (argc + 2) * sizeof (char *));
139 memmove (argv + 1, argv, (argc + 1) * sizeof (char *));
141 char *buf = tp.c_get ();
144 cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_RELATIVE, what, buf,
146 char *p = strchr (buf, '\0') - 4;
147 if (p > buf && ascii_strcasematch (p, ".exe"))
151 *argv = cstrdup1 (what);