Browse Source

* patch by Sergei Gorelkin improving win32 command line handling, resolves #10175

git-svn-id: trunk@11640 -
florian 17 years ago
parent
commit
93d86a60e4
1 changed files with 19 additions and 41 deletions
  1. 19 41
      rtl/win32/system.pp

+ 19 - 41
rtl/win32/system.pp

@@ -140,24 +140,15 @@ function SysReAllocStringLen(var bstr:pointer;psz: pointer;
                               Parameter Handling
 *****************************************************************************}
 
-var
-  ModuleName : array[0..255] of char;
-
-function GetCommandFile:pchar;
-begin
-  GetModuleFileName(0,@ModuleName,255);
-  GetCommandFile:=@ModuleName;
-end;
-
-
 procedure setup_arguments;
 var
   arglen,
   count   : longint;
   argstart,
   pc,arg  : pchar;
-  quote   : char;
+  quote   : Boolean;
   argvlen : longint;
+  buf: array[0..259] of char;  // need MAX_PATH bytes, not 256!
 
   procedure allocarg(idx,len:longint);
     var
@@ -183,13 +174,10 @@ begin
   count:=0;
   argv:=nil;
   argvlen:=0;
-  pc:=getcommandfile;
-  Arglen:=0;
-  repeat
-    Inc(Arglen);
-  until (pc[Arglen]=#0);
-  allocarg(count,arglen);
-  move(pc^,argv[count]^,arglen+1);
+  ArgLen := GetModuleFileName(0, @buf[0], sizeof(buf));
+  buf[ArgLen] := #0; // be safe
+  allocarg(0,arglen);
+  move(buf,argv[0]^,arglen+1);
   { Setup cmdline variable }
   cmdline:=GetCommandLine;
   { process arguments }
@@ -205,7 +193,7 @@ begin
      if pc^=#0 then
       break;
      { calc argument length }
-     quote:=' ';
+     quote:=False;
      argstart:=pc;
      arglen:=0;
      while (pc^<>#0) do
@@ -213,20 +201,15 @@ begin
         case pc^ of
           #1..#32 :
             begin
-              if quote<>' ' then
+              if quote then
                inc(arglen)
               else
                break;
             end;
           '"' :
-            if pchar(pc+1)^<>'"' then
-            begin
-              if quote='"' then
-               quote:=' '
+            if pc[1]<>'"' then
+              quote := not quote
               else
-               quote:='"';
-            end
-            else
               inc(pc);
           else
             inc(arglen);
@@ -238,7 +221,7 @@ begin
      If Count<>0 then
       begin
         allocarg(count,arglen);
-        quote:=' ';
+        quote:=False;
         pc:=argstart;
         arg:=argv[count];
         while (pc^<>#0) do
@@ -246,7 +229,7 @@ begin
            case pc^ of
              #1..#32 :
                begin
-                 if quote<>' ' then
+                 if quote then
                   begin
                     arg^:=pc^;
                     inc(arg);
@@ -255,14 +238,9 @@ begin
                   break;
                end;
              '"' :
-               if pchar(pc+1)^<>'"' then
-                begin
-                  if quote='"' then
-                   quote:=' '
+               if pc[1]<>'"' then
+                 quote := not quote
                   else
-                   quote:='"';
-                end
-               else
                 inc(pc);
              else
                begin
@@ -279,11 +257,11 @@ begin
  {$EndIf SYSTEM_DEBUG_STARTUP}
      inc(count);
    end;
-  { get argc and create an nil entry }
+  { get argc }
   argc:=count;
-  allocarg(argc,0);
-  { free unused memory }
-  sysreallocmem(argv,(argc+1)*sizeof(pointer));
+  { free unused memory, leaving a nil entry at the end }
+  sysreallocmem(argv,(count+1)*sizeof(pointer));
+  argv[count] := nil;
 end;
 
 
@@ -1188,7 +1166,7 @@ begin
   { some misc Win32 stuff }
   hprevinst:=0;
   if not IsLibrary then
-    SysInstance:=getmodulehandle(GetCommandFile);
+    SysInstance:=getmodulehandle(nil);
 
   MainInstance:=SysInstance;