unixutil.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2013 by the Free Pascal development team
  4. DO NOT ADD ROUTINES TO THIS FILE!
  5. THE ROUTINES IN THIS FILE ARE INTERNAL AND NOT FOR END USER USAGE!
  6. Background: This unit contains leftovers from the unix restructure that
  7. shouldn't be in the interface of unit baseunix/unix, but are needed
  8. in these units. (at the time routines were still being moved
  9. from baseunix to unix, and unit baseunix couldn't depend on unix)
  10. The routines are fairly OS independent but can't move to
  11. OS independent because the lowlevel units baseunix/unix depend
  12. on them. If they need to be generally accessable, copy these
  13. functions to a general purpose, OS independent, supportable unit.
  14. See the file COPYING.FPC, included in this distribution,
  15. for details about the copyright.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. **********************************************************************}
  20. unit unixutil;
  21. interface
  22. uses BaseUnix;
  23. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  24. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):ppchar;
  25. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):ppchar; // const ?
  26. implementation
  27. function ArrayStringToPPchar(const S:Array of RawByteString;reserveentries:Longint):ppchar; // const ?
  28. // Extra allocate reserveentries pchar's at the beginning (default param=0 after 1.0.x ?)
  29. // Note: for internal use by skilled programmers only
  30. // if "s" goes out of scope in the parent procedure, the pointer is dangling.
  31. var p : ppchar;
  32. i : LongInt;
  33. begin
  34. if High(s)<Low(s) Then Exit(NIL);
  35. Getmem(p,sizeof(pchar)*(high(s)-low(s)+ReserveEntries+2)); // one more for NIL, one more
  36. // for cmd
  37. if p=nil then
  38. begin
  39. {$ifdef xunix}
  40. fpseterrno(ESysEnomem);
  41. {$endif}
  42. exit(NIL);
  43. end;
  44. for i:=low(s) to high(s) do
  45. p[i+Reserveentries]:=pchar(s[i]);
  46. p[high(s)+1+Reserveentries]:=nil;
  47. ArrayStringToPPchar:=p;
  48. end;
  49. Function StringToPPChar(Var S:RawByteString;ReserveEntries:integer):ppchar;
  50. {
  51. Create a PPChar to structure of pchars which are the arguments specified
  52. in the string S. Especially useful for creating an ArgV for Exec-calls
  53. }
  54. begin
  55. StringToPPChar:=StringToPPChar(PChar(S),ReserveEntries);
  56. end;
  57. Function StringToPPChar(S: PChar;ReserveEntries:integer):ppchar;
  58. var
  59. i,nr : longint;
  60. Buf : ^char;
  61. p : ppchar;
  62. begin
  63. buf:=s;
  64. nr:=1;
  65. while (buf^<>#0) do // count nr of args
  66. begin
  67. while (buf^ in [' ',#9,#10]) do // Kill separators.
  68. inc(buf);
  69. inc(nr);
  70. if buf^='"' Then // quotes argument?
  71. begin
  72. inc(buf);
  73. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  74. inc(buf);
  75. if buf^='"' then // skip closing quote.
  76. inc(buf);
  77. end
  78. else
  79. begin // else std
  80. while not (buf^ in [' ',#0,#9,#10]) do
  81. inc(buf);
  82. end;
  83. end;
  84. getmem(p,(ReserveEntries+nr)*sizeof(pchar));
  85. StringToPPChar:=p;
  86. if p=nil then
  87. begin
  88. {$ifdef xunix}
  89. fpseterrno(ESysEnomem);
  90. {$endif}
  91. exit;
  92. end;
  93. for i:=1 to ReserveEntries do inc(p); // skip empty slots
  94. buf:=s;
  95. while (buf^<>#0) do
  96. begin
  97. while (buf^ in [' ',#9,#10]) do // Kill separators.
  98. begin
  99. buf^:=#0;
  100. inc(buf);
  101. end;
  102. if buf^='"' Then // quotes argument?
  103. begin
  104. inc(buf);
  105. p^:=buf;
  106. inc(p);
  107. p^:=nil;
  108. while not (buf^ in [#0,'"']) do // then end of argument is end of string or next quote
  109. inc(buf);
  110. if buf^='"' then // skip closing quote.
  111. begin
  112. buf^:=#0;
  113. inc(buf);
  114. end;
  115. end
  116. else
  117. begin
  118. p^:=buf;
  119. inc(p);
  120. p^:=nil;
  121. while not (buf^ in [' ',#0,#9,#10]) do
  122. inc(buf);
  123. end;
  124. end;
  125. end;
  126. end.