common/src/fixpath.c

changeset 494
e64f2cb57d05
child 564
befbad2e4d87
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/common/src/fixpath.c	Fri Oct 26 14:29:57 2012 -0700
     1.3 @@ -0,0 +1,334 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +#include <Windows.h>
    1.30 +#include <io.h>
    1.31 +#include <stdio.h>
    1.32 +#include <string.h>
    1.33 +#include <malloc.h>
    1.34 +
    1.35 +/*
    1.36 + * Test if pos points to /cygdrive/_/ where _ can
    1.37 + * be any character.
    1.38 + */
    1.39 +int is_cygdrive_here(int pos, char *in, int len)
    1.40 +{
    1.41 +  // Length of /cygdrive/c/ is 12
    1.42 +  if (pos+12 > len) return 0;
    1.43 +  if (in[pos+11]=='/' &&
    1.44 +      in[pos+9]=='/' &&
    1.45 +      in[pos+8]=='e' &&
    1.46 +      in[pos+7]=='v' &&
    1.47 +      in[pos+6]=='i' &&
    1.48 +      in[pos+5]=='r' &&
    1.49 +      in[pos+4]=='d' &&
    1.50 +      in[pos+3]=='g' &&
    1.51 +      in[pos+2]=='y' &&
    1.52 +      in[pos+1]=='c' &&
    1.53 +      in[pos+0]=='/') {
    1.54 +    return 1;
    1.55 +  }
    1.56 +  return 0;
    1.57 +}
    1.58 +
    1.59 +/*
    1.60 + * Replace /cygdrive/_/ with _:/
    1.61 + * Works in place since drive letter is always
    1.62 + * shorter than /cygdrive/
    1.63 + */
    1.64 +char *replace_cygdrive_cygwin(char *in)
    1.65 +{
    1.66 +  int len = strlen(in);
    1.67 +  char *out = malloc(len+1);
    1.68 +  int i,j;
    1.69 +
    1.70 +  if (len < 12) {
    1.71 +    strcpy(out, in);
    1.72 +    return out;
    1.73 +  }
    1.74 +  for (i = 0, j = 0; i<len;) {
    1.75 +    if (is_cygdrive_here(i, in, len)) {
    1.76 +      out[j++] = in[i+10];
    1.77 +      out[j++] = ':';
    1.78 +      i+=11;
    1.79 +    } else {
    1.80 +      out[j] = in[i];
    1.81 +      i++;
    1.82 +      j++;
    1.83 +    }
    1.84 +  }
    1.85 +  out[j] = 0;
    1.86 +  return out;
    1.87 +}
    1.88 +
    1.89 +void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen)
    1.90 +{
    1.91 +  while ( (addlen+*u+1) > *bl) {
    1.92 +    *bl *= 2;
    1.93 +    *b = realloc(*b, *bl);
    1.94 +  }
    1.95 +  memcpy(*b+*u, add, addlen);
    1.96 +  *u += addlen;
    1.97 +}
    1.98 +
    1.99 +/*
   1.100 + * Creates a new string from in where the first occurance of sub is
   1.101 + * replaced by rep.
   1.102 + */
   1.103 +char *replace_substring(char *in, char *sub, char *rep)
   1.104 +{
   1.105 +  int in_len = strlen(in);
   1.106 +  int sub_len = strlen(sub);
   1.107 +  int rep_len = strlen(rep);
   1.108 +  char *out = malloc(in_len - sub_len + rep_len + 1);
   1.109 +  char *p;
   1.110 +
   1.111 +  if (!(p = strstr(in, sub))) {
   1.112 +    // If sub isn't a substring of in, just return in.
   1.113 +    return in;
   1.114 +  }
   1.115 +
   1.116 +  // Copy characters from beginning of in to start of sub.
   1.117 +  strncpy(out, in, p - in);
   1.118 +  out[p - in] = '\0';
   1.119 +
   1.120 +  sprintf(out + (p - in), "%s%s", rep, p + sub_len);
   1.121 +
   1.122 +  return out;
   1.123 +}
   1.124 +
   1.125 +char* msys_path_list; // @-separated list of paths prefix to look for
   1.126 +char* msys_path_list_end; // Points to last \0 in msys_path_list.
   1.127 +
   1.128 +void setup_msys_path_list(char* argument)
   1.129 +{
   1.130 +  char* p;
   1.131 +  char* drive_letter_pos;
   1.132 +
   1.133 +  msys_path_list = strdup(&argument[2]);
   1.134 +  msys_path_list_end = &msys_path_list[strlen(msys_path_list)];
   1.135 +
   1.136 +  // Convert all at-sign (@) in path list to \0.
   1.137 +  // @ was chosen as separator to minimize risk of other tools messing around with it
   1.138 +  p = msys_path_list;
   1.139 +  do {
   1.140 +    if (p[1] == ':') {
   1.141 +      // msys has mangled our path list, restore it from c:/... to /c/...
   1.142 +      drive_letter_pos = p+1;
   1.143 +      *drive_letter_pos = *p;
   1.144 +      *p = '/';
   1.145 +    }
   1.146 +
   1.147 +    // Look for an @ in the list
   1.148 +    p = strchr(p, '@');
   1.149 +    if (p != NULL) {
   1.150 +      *p = '\0';
   1.151 +      p++;
   1.152 +    }
   1.153 +  } while (p != NULL);
   1.154 +}
   1.155 +
   1.156 +char *replace_cygdrive_msys(char *in)
   1.157 +{
   1.158 +  char* str;
   1.159 +  char* prefix;
   1.160 +  char* p;
   1.161 +
   1.162 +  str = strdup(in);
   1.163 +
   1.164 +  // For each prefix in the path list, search for it and replace /c/... with c:/...
   1.165 +  for (prefix = msys_path_list; prefix < msys_path_list_end && prefix != NULL; prefix += strlen(prefix)+1) {
   1.166 +    p=str;
   1.167 +    while ((p = strstr(p, prefix))) {
   1.168 +      char* drive_letter = p+1;
   1.169 +      *p = *drive_letter;
   1.170 +      *drive_letter = ':';
   1.171 +      p++;
   1.172 +    }
   1.173 +  }
   1.174 +
   1.175 +  return str;
   1.176 +}
   1.177 +
   1.178 +char*(*replace_cygdrive)(char *in) = NULL;
   1.179 +
   1.180 +char *files_to_delete[1024];
   1.181 +int num_files_to_delete = 0;
   1.182 +
   1.183 +char *fix_at_file(char *in)
   1.184 +{
   1.185 +  char *tmpdir;
   1.186 +  char name[2048];
   1.187 +  char *atname;
   1.188 +  char *buffer;
   1.189 +  size_t buflen=65536;
   1.190 +  size_t used=0;
   1.191 +  size_t len;
   1.192 +  int rc;
   1.193 +  FILE *atout;
   1.194 +  FILE *atin;
   1.195 +  char block[2048];
   1.196 +  size_t blocklen;
   1.197 +  char *fixed;
   1.198 +
   1.199 +  atin = fopen(in+1, "r");
   1.200 +  if (atin == NULL) {
   1.201 +    fprintf(stderr, "Could not read at file %s\n", in+1);
   1.202 +    exit(-1);
   1.203 +  }
   1.204 +
   1.205 +  tmpdir = getenv("TMP");
   1.206 +  if (tmpdir == NULL) {
   1.207 +    tmpdir = "c:/cygwin/tmp";
   1.208 +  }
   1.209 +  _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir);
   1.210 +
   1.211 +  rc = _mktemp_s(name, strlen(name)+1);
   1.212 +  if (rc) {
   1.213 +    fprintf(stderr, "Could not create temporary file name for at file!\n");
   1.214 +    exit(-1);
   1.215 +  }
   1.216 +
   1.217 +  atout = fopen(name, "w");
   1.218 +  if (atout == NULL) {
   1.219 +    fprintf(stderr, "Could not open temporary file for writing! %s\n", name);
   1.220 +    exit(-1);
   1.221 +  }
   1.222 +
   1.223 +  buffer = malloc(buflen);
   1.224 +  while((blocklen = fread(block,1,sizeof(block),atin)) > 0) {
   1.225 +    append(&buffer, &buflen, &used, block, blocklen);
   1.226 +  }
   1.227 +  buffer[used] = 0;
   1.228 +  if (getenv("DEBUG_FIXPATH") != NULL) {
   1.229 +    fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer);
   1.230 +  }
   1.231 +  fixed = replace_cygdrive(buffer);
   1.232 +  if (getenv("DEBUG_FIXPATH") != NULL) {
   1.233 +    fprintf(stderr, "fixpath converted to @-file %s is: %s\n", name, fixed);
   1.234 +  }
   1.235 +  fwrite(fixed, strlen(fixed), 1, atout);
   1.236 +  fclose(atin);
   1.237 +  fclose(atout);
   1.238 +  free(fixed);
   1.239 +  free(buffer);
   1.240 +  files_to_delete[num_files_to_delete] = malloc(strlen(name)+1);
   1.241 +  strcpy(files_to_delete[num_files_to_delete], name);
   1.242 +  num_files_to_delete++;
   1.243 +  atname = malloc(strlen(name)+2);
   1.244 +  atname[0] = '@';
   1.245 +  strcpy(atname+1, name);
   1.246 +  return atname;
   1.247 +}
   1.248 +
   1.249 +int main(int argc, char **argv)
   1.250 +{
   1.251 +    STARTUPINFO si;
   1.252 +    PROCESS_INFORMATION pi;
   1.253 +    unsigned short rc;
   1.254 +
   1.255 +    char *new_at_file;
   1.256 +    char *old_at_file;
   1.257 +    char *line;
   1.258 +    int i;
   1.259 +    DWORD exitCode;
   1.260 +
   1.261 +    if (argc<3 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm')) {
   1.262 +        fprintf(stderr, "Usage: fixpath -c|m<path@path@...> /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt");
   1.263 +        exit(0);
   1.264 +    }
   1.265 +
   1.266 +    if (getenv("DEBUG_FIXPATH") != NULL) {
   1.267 +      fprintf(stderr, "fixpath input line >%s<\n", strstr(GetCommandLine(), argv[1]));
   1.268 +    }
   1.269 +
   1.270 +    if (argv[1][1] == 'c' && argv[1][2] == '\0') {
   1.271 +      if (getenv("DEBUG_FIXPATH") != NULL) {
   1.272 +        fprintf(stderr, "using cygwin mode\n");
   1.273 +      }
   1.274 +      replace_cygdrive = replace_cygdrive_cygwin;
   1.275 +    } else if (argv[1][1] == 'm') {
   1.276 +      if (getenv("DEBUG_FIXPATH") != NULL) {
   1.277 +        fprintf(stderr, "using msys mode, with path list: %s\n", &argv[1][2]);
   1.278 +      }
   1.279 +      setup_msys_path_list(argv[1]);
   1.280 +      replace_cygdrive = replace_cygdrive_msys;
   1.281 +    } else {
   1.282 +      fprintf(stderr, "Unknown mode: %s\n", argv[1]);
   1.283 +      exit(-1);
   1.284 +    }
   1.285 +    line = replace_cygdrive(strstr(GetCommandLine(), argv[2]));
   1.286 +
   1.287 +    for (i=1; i<argc; ++i) {
   1.288 +       if (argv[i][0] == '@') {
   1.289 +          // Found at-file! Fix it!
   1.290 +          old_at_file = replace_cygdrive(argv[i]);
   1.291 +          new_at_file = fix_at_file(old_at_file);
   1.292 +          line = replace_substring(line, old_at_file, new_at_file);
   1.293 +       }
   1.294 +    }
   1.295 +
   1.296 +    if (getenv("DEBUG_FIXPATH") != NULL) {
   1.297 +      fprintf(stderr, "fixpath converted line >%s<\n", line);
   1.298 +    }
   1.299 +
   1.300 +    ZeroMemory(&si,sizeof(si));
   1.301 +    si.cb=sizeof(si);
   1.302 +    ZeroMemory(&pi,sizeof(pi));
   1.303 +
   1.304 +    rc = CreateProcess(NULL,
   1.305 +                       line,
   1.306 +                       0,
   1.307 +                       0,
   1.308 +                       TRUE,
   1.309 +                       0,
   1.310 +                       0,
   1.311 +                       0,
   1.312 +                       &si,
   1.313 +                       &pi);
   1.314 +    if(!rc)
   1.315 +    {
   1.316 +      //Could not start process;
   1.317 +      fprintf(stderr, "Could not start process!\n");
   1.318 +      exit(-1);
   1.319 +    }
   1.320 +
   1.321 +    WaitForSingleObject(pi.hProcess,INFINITE);
   1.322 +    GetExitCodeProcess(pi.hProcess,&exitCode);
   1.323 +
   1.324 +    if (getenv("DEBUG_FIXPATH") != NULL) {
   1.325 +      for (i=0; i<num_files_to_delete; ++i) {
   1.326 +        fprintf(stderr, "Not deleting temporary fixpath file %s\n",
   1.327 +                files_to_delete[i]);
   1.328 +      }
   1.329 +    }
   1.330 +    else {
   1.331 +      for (i=0; i<num_files_to_delete; ++i) {
   1.332 +        remove(files_to_delete[i]);
   1.333 +      }
   1.334 +    }
   1.335 +
   1.336 +    exit(exitCode);
   1.337 +}

mercurial