home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / books / argv / argv.txt
Text File  |  1992-07-23  |  20KB  |  466 lines

  1. ------------------------------
  2.  
  3. Date: 9 May 91 21:18:36 GMT
  4. From: imagen!atari!apratt@sun.com (Allan Pratt)
  5. Subject: ARGV spec repost as promised
  6. To: Info-Atari16@naucse.cse.nau.edu
  7.  
  8. In a discussion about Pexec and command lines, the question of the
  9. ARGV standard came up.  Here is our documentation on this convention.
  10. It dates from August 22, 1990.
  11.  
  12. GEMDOS EXTENDED ARGUMENT (ARGV) SPECIFICATION
  13.  
  14. Introduction
  15.  
  16. The Pexec() function of GEMDOS allows a program to pass to a child
  17. process a command line up to 125 characters long, with arguments
  18. separated by spaces.  No provision is made in GEMDOS for the child to
  19. know its own name.  This makes it difficult for C programs to correctly
  20. fill in argv[0], the standard place where a C program finds the command
  21. which invoked it.  Because the command line arguments are separated by
  22. spaces, it is difficult to pass an argument with an embedded space.
  23. This document will specify a method of passing arguments which allows
  24. arbitrary argument length, embedded spaces, and support for argv[0].
  25.  
  26. Standard Argument Passing
  27.  
  28. The Pexec Cookbook specifies how to use Pexec() to launch a child
  29. process, passing a command tail (argument string) and an environment.
  30. Before getting into the extended argument scheme, let's review how
  31. arguments are normally passed to a child.
  32.  
  33. A parent process builds a command line into an argument string - a null
  34. terminated string whose first byte contains the length of the rest of
  35. the string - and its address is passed as one of the arguments to
  36. Pexec().  GEMDOS copies this argument string to the basepage which it
  37. creates for the child.  Thus the parent is responsible for gathering
  38. all the child's arguments into one string.  This is normally handled by
  39. a library exec() function.  The child is responsible for parsing the
  40. string of space-separated arguments back into an array of strings.
  41. This parsing is normally handled by the child's startup code.
  42.  
  43. Evolution
  44.  
  45. Several methods of bypassing the limits imposed by Pexec() have been
  46. used by GEMDOS programs.  Some allow a user to specify a file on the
  47. command line which contains the rest of the arguments.  Others get a
  48. pointer to the arguments, or the arguments themselves, from the
  49. environment string.  Most MS-DOS programs use a command file for the
  50. extra arguments.  This can be inconvenient for a user, cluttering the
  51. file system with command files, and making the operation of batch files
  52. and makefiles more confusing.
  53.  
  54. Several "standards" have arisen on the ST which use the environment to
  55. pass arguments.  While more convenient than command files, these
  56. standards have other problems.  Some rely on sharing memory between
  57. parent and child processes.  Some take advantage of undocumented
  58. features of the operating system to get argv[0].  Others give the
  59. child process no way to validate that the arguments it finds are
  60. intended for it.
  61.  
  62. Rationale
  63.  
  64. In order to pass more than the standard 125 characters worth of
  65. arguments to a child, or to let the child find its name, the parent
  66. must place the extra information in a place where the child can access
  67. it safely and legally.  The most convenient place is in the child's
  68. environment string.  An environment string is a series of
  69. null-terminated strings of the format "VARIABLE=value" (e.g.
  70. PATH=c:\bin,c:\etc, or ShellP=YES).   The last null-terminated string
  71. in the environment is followed by a zero byte, thus two consecutive
  72. nulls indicates the end of the environment.   The environment is
  73. allocated for the child by GEMDOS, it is owned by the child, and its
  74. contents can be specified by the parent.
  75.  
  76. The child must have some way of knowing that the arguments which
  77. it finds in its environment are intended for it.  The child may have
  78. been invoked by a parent which does not conform to this specification.
  79. Such a parent would leave _its_ arguments in the environment, and could
  80. pass that environment on to the child.  The child would mistakenly
  81. interpret its parent's arguments as its own.
  82.  
  83. Placing arguments in the environment passed to the child gets around
  84. all of the command line limits of the standard Pexec() command tail.
  85. Because there is no limit on the length of the environment, arbitrary
  86. length arguments are supported.  Arguments placed in the environment
  87. are null terminated, so they may contain spaces.  A parent can also
  88. place the name of the command with which it invokes the child in the
  89. child's environment, providing support for argv[0].  Validation of the
  90. extended arguments can be placed in the standard Pexec() command line,
  91. by assigning a special meaning to an invalid length byte.
  92.  
  93. The GEMDOS Extended Argument Specification
  94.  
  95. This specification uses the convention that the presence of an
  96. environment variable named ARGV (all upper case) indicates that extended
  97. arguments are being passed to the child in its environment.  This means
  98. that ARGV is a "boolean" environment variable.  For the purpose of this
  99. specification, its value is not significant, but its presence indicates
  100. that the strings following it are the arguments for the child.
  101. Implementations of this specification are free to give the ARGV
  102. environment variable any value.  The ARGV environment variable must be
  103. the last one in the environment passed to the child, so that the child
  104. can truncate its environment at that point, and treat everything before
  105. the ARGV as environment, and everything after it as arguments.
  106.  
  107. The first argument to the child (argv[0]) is the first string in the
  108. environment after the ARGV variable.  This argument is the "pathname"
  109. parameter passed by the parent to Pexec().  The remaining arguments are
  110. those that the child would normally find in the command tail in its
  111. basepage.  Even if all of the arguments would normally fit in a child's
  112. command tail, the parent should set up the arguments in the environment
  113. to take advantage of the benefits of this extended argument scheme.
  114.  
  115. As many arguments as will fit in the command tail will be passed there
  116. as well as in the environment, to support non-conforming programs.  As
  117. a flag that arguments are also in the environment, the length byte of
  118. the command tail will be 127 (hex 7f).  Non-conforming programs should
  119. not have a problem with this length byte, because it is longer than the
  120. maximum 125 bytes allowed by Pexec().
  121.  
  122. As an aside, the Pexec Cookbook erroneously implies that a command line
  123. can be 126 or 127 characters long.  In fact, GEMDOS only copies to the
  124. child's basepage up to 125 bytes, or until it encounters a null, from
  125. the argument string passed to Pexec().  It ignores the length byte,
  126. placing a null at the same place it found one or at the 126th byte if
  127. no null is found.  This has several implications: the length byte is
  128. not validated by GEMDOS (necessitating validation in the child's
  129. startup code, but also making this extended argument spec possible),
  130. and the null terminator _can_ be located after the end of the real
  131. command tail (the Desktop places a CR character after the command tail
  132. and before the null).  The ARGSTART.S startup code listing below
  133. demonstrates how to correctly validate and parse a GEMDOS command tail.
  134.  
  135. A child which finds an ARGV environment variable can use the command
  136. tail length byte value of 127 to validate that the arguments following
  137. the variable are valid, and not just left over from a non-conforming
  138. parent which left its own ARGV arguments in the environment.
  139.  
  140. Because the strings in the environment following an ARGV variable are
  141. not environment variables, a child should truncate its own environment
  142. at the ARGV variable by changing the 'A' to a null.
  143.  
  144. Implementation: Parental Responsibilities
  145.  
  146. To pass arguments in the environment, a parent must create an
  147. environment string for the child.  This can be achieved by first
  148. allocating as much space as is used in the parent's own environment,
  149. plus enough room for the ARGV variable and the arguments to the child,
  150. and then copying the parent's environment to the newly allocated area.
  151. Next, the ARGV variable must be appended, since it must be the last
  152. variable in the child's environment string.  Following the ARGV variable
  153. is the null-terminated pathname of the child as passed to Pexec(), then
  154. the null-terminated arguments to the child, followed by a final null
  155. byte indicating the end of the environment.
  156.  
  157. After setting up the arguments in the environment, the parent must
  158. place as many arguments as it can fit in the command tail it passes
  159. to Pexec().  This way, a child which does not conform to this
  160. specification can still get arguments from the command tail in its
  161. basepage.  When placing arguments in the environment, the parent must
  162. set the first (length) byte of the command tail to 127 (hex 7f),
  163. validating the arguments in the environment.
  164.  
  165. Here is an example execv() library routine in C.  It uses three local
  166. utility routines, e_strlen(), e_strcpy(), and str0cpy() for getting
  167. environment size and copying strings into the environment created for
  168. the child.
  169.  
  170.  
  171. /* EXECV.C - example execv() library routine
  172.  * ================================================================
  173.  * 890910 kbad
  174.  */
  175.  
  176. long Malloc( long nbytes );
  177. long Pexec( short mode, char *filename, char *tail, char *env );
  178. long Mfree( void *address );
  179.  
  180. /* Return the total length of the characters and null terminators in
  181.  *   an array of strings.
  182.  * `strings' is an array of pointers to strings, with a null pointer
  183.  *   as the last element.
  184.  */
  185. static long
  186. e_strlen( char *strings[] )
  187. {
  188.     char    *pstring;
  189.     long    length = 0;
  190.  
  191.     while( *strings != 0 ) {        /* Until reaching null pointer,    */
  192.     pstring = *strings++;        /* get a string pointer,        */
  193.     do {                /* find the length of this string,  */
  194.         ++length;            /* using do-while to count the    */
  195.     } while( *pstring++ != 0 ); /* null terminator.            */
  196.     }
  197.     return length;            /* Return total length of all strings */
  198. }
  199.  
  200. /* Copy a string, including the null terminator, and return a pointer
  201.  * to the end of the destination string.
  202.  */
  203. static char *
  204. str0cpy( char *dest, char *source )
  205. {
  206.     do { /* use do-while to include null terminator */
  207.     *dest++ = *source;
  208.     } while( *source++ != 0 );
  209.     return dest;
  210. }
  211.  
  212. /* Copy an array of strings into an environment string, and return a pointer
  213.  * to the end of the environment string.
  214.  * `strings' is an array of pointers to strings with a null pointer
  215.  *   as the last element.
  216.  * `envstring' points to the environment string.
  217.  */
  218. static char *
  219. e_strcpy( char *envstring, char *strings[] )
  220. {
  221.     while( *strings != 0 ) {
  222.     envstring = str0cpy( envstring, *strings );
  223.     ++strings;
  224.     }
  225.     return envstring;            /* Return end of environment string */
  226. }
  227.  
  228.  
  229. /* Run a program, passing it arguments according to the
  230.  * GEMDOS Extended Argument Spec.
  231.  *
  232.  * `childname' is the relative path\filename of the child to execute.
  233.  * `args' is an array of pointers to strings to be used as arguments
  234.  *   to the child.  The last array element must be a null pointer.
  235.  * `environ' is a global array of pointers to strings
  236.  *   which make up the caller's environment.
  237.  */
  238. long
  239. execv( char *childname, char *args[] )
  240. {
  241.     long    envsize, ret;
  242.     char    *parg, *penvargs, *childenv, *pchildenv;
  243.     short    lentail;
  244.     char    argch, tail[128], *ptail;
  245. static  char    argvar[] = "ARGV=";
  246. extern  char    *environ[];
  247.  
  248. /*
  249.  * Find out how much memory we'll need for the child's environment
  250.  */
  251.     envsize = e_strlen( environ );    /* length of environment    */
  252.     envsize += e_strlen( args );    /* plus command tail args    */
  253. /* plus length of argv[0] */
  254.     parg = childname;
  255.     do { /* use do-while to include null terminator */
  256.     ++envsize;
  257.     } while( *parg++ != 0 );
  258. /* plus length of ARGV environment variable and final null */
  259.     envsize += 7;
  260.     envsize += envsize & 1; /* even # of bytes */
  261. /*
  262.  * Allocate and fill in the child's environment
  263.  */
  264.     ret = Malloc( envsize );
  265.     if( ret < 0 )
  266.     return ret; /* Malloc error */
  267.     childenv = (char *)ret;
  268.     pchildenv = e_strcpy( childenv, environ );     /* copy caller environment */
  269.     pchildenv = str0cpy( pchildenv, argvar );     /* append ARGV variable */
  270.     pchildenv = str0cpy( pchildenv, childname ); /* append argv[0] */
  271.     penvargs = pchildenv;             /* save start of args */
  272.     pchildenv = e_strcpy( pchildenv, args );     /* append args */
  273.     *pchildenv = 0;                 /* terminate environment */
  274. /* put as much in the command tail as will fit */
  275.     lentail = 0;
  276.     ptail = &tail[1];
  277.     while( (lentail++ < 126) && (penvargs < pchildenv) ) {
  278.     argch = *penvargs++;
  279.     if( argch == 0 ) {
  280.         *ptail++ = ' ';
  281.     } else {
  282.         *ptail++ = argch;
  283.     }
  284.     }
  285. /* terminate command tail and validate ARGV */
  286.     *ptail = 0;
  287.     tail[0] = 127;
  288. /*
  289.  * Execute child, returning the return code from Pexec()
  290.  */
  291.     ret = Pexec( 0, childname, tail, childenv );
  292.     Mfree( childenv );
  293.     return ret;
  294. }
  295. /* End of execv() example code */
  296.  
  297.  
  298. Implementation: Prenatal Responsibilities
  299.  
  300. A program's startup code must handle getting extended arguments out of
  301. the environment.  The startup code should get the basepage pointer off
  302. the stack, then get the environment pointer from the basepage, and
  303. search the environment for "ARGV=".  If "ARGV=" is found, the command
  304. line length byte in the basepage is checked.  If the command line
  305. length byte is 127, then the arguments in the environment are valid.
  306. The first argument begins after the first null following the "ARGV=".
  307. It is important not to assume that the null follows immediately after
  308. the "ARGV=", because some implementations may assign a value to the
  309. ARGV environment variable.  After setting up an array of pointers to the
  310. arguments, the startup code should set the 'A' of the "ARGV" variable
  311. to null, thus separating the environment from the argument strings
  312. (remember: a double null terminates the environment).
  313.  
  314. Here is some example C startup code which shows how a child could
  315. look for arguments in its environment:
  316.  
  317. * ARGSTART.S - example C startup code
  318. * using GEMDOS Extended Argument Specification
  319. * ================================================================
  320. * 890910 kbad
  321.  
  322. .globl        _main        ; external, C entry point
  323. .globl        _argv0        ; external, name used for argv[0] if no ARGV
  324. .globl        _stksize    ; external, size of application stack
  325. .globl        _basepage    ; allocated here, -> program's basepage
  326. .globl        _environ    ; allocated here, -> envp[]
  327. .globl        _argvecs    ; allocated here, -> argv[]
  328. .globl        _stklimit    ; allocated here, -> lower limit of stack
  329. .BSS
  330. _basepage:    ds.l    1
  331. _environ:    ds.l    1
  332. _argvecs:    ds.l    1
  333. _stklimit:    ds.l    1
  334. .TEXT
  335. _start:
  336.     move.l    4(sp),a5    ; get basepage
  337.     move.l    a5,_basepage    ; save it
  338.     move.l    24(a5),a0    ; bss base
  339.     add.l    28(a5),a0    ; plus bss size = envp[] base
  340.     move.l    a0,_environ    ; save start of envp[]
  341.     move.l    a0,a1        ; start of env/arg vectors
  342.     move.l    44(a5),a2    ; basepage environment pointer
  343.     tst.b    (a2)        ; empty environment?
  344.     beq.s    nargv        ; yes, no envp[]
  345.  
  346.     lea.l    (sp),a4        ; use dummy return pc on stack for ARGV test
  347. * --- fill in the envp[] array
  348. nxenv:    move.l    a2,(a1)+    ; envp[n]
  349.     move.l    a2,a3
  350. nxen1:    tst.b    (a2)+
  351.     bne.s    nxen1        ; get the end of this variable
  352.     tst.b    (a2)        ; end of env?
  353.     beq.s    xenv
  354. * --- check for ARGV
  355.     move.b    (a3)+,-(a4)    ; get 1st 4 bytes of this var
  356.     move.b    (a3)+,-(a4)
  357.     move.b    (a3)+,-(a4)
  358.     move.b    (a3)+,-(a4)
  359.     cmp.l    #'VGRA',(a4)+    ; is it ARGV?
  360.     bne.s    nxenv
  361.     cmp.b    #'=',(a3)    ; is it ARGV=?
  362.     bne.s    nxenv
  363.     clr.b    -4(a3)        ; ARGV marks the end of our environment
  364.     cmp.b    #127,$80(a5)    ; command line validation?
  365.     bne.s    nargv        ; nope... and we're done with the env.
  366. * --- got an ARGV=, create argv[] array
  367.     clr.l    (a1)+        ; terminate envp[]
  368.     move.l    a1,_argvecs    ; save base of argv[]
  369. nxarg:    move.l    a2,(a1)+    ; argv[n]
  370. nxar1:    tst.b    (a2)+
  371.     bne.s    nxar1
  372.     tst.b    (a2)
  373.     bne.s    nxarg
  374. * --- end of environment
  375. xenv:    move.l    _argvecs,d0    ; if we got an argv[]
  376.     bne.s    argok        ; don't parse command tail
  377. * --- No ARGV, parse the command tail
  378. * NOTE: This code parses the command tail IN PLACE.  This can cause problems
  379. *       because the default DTA set up by GEMDOS for a program is located
  380. *       in the command tail part of the basepage.  You should use Fsetdta()
  381. *       to set up your own DTA before performing any operations which could
  382. *       use the DTA if you want to preserve the arguments in the command tail.
  383. nargv:    clr.l    (a1)+        ; terminate envp[]
  384.     move.l    a1,_argvecs    ; base of argv[]
  385.     move.l    #_argv0,(a1)+    ; default name for argv[0]
  386.     lea    128(a5),a2    ; command tail
  387.     move.b    (a2)+,d2    ; length byte
  388.     ext    d2
  389.     moveq    #125,d1        ; validate length
  390.     cmp    d1,d2
  391.     bcs.s    valen
  392.     move    d1,d2        ; if invalid length, copy all of tail
  393. valen:    clr.b    0(a2,d2)    ; null tail because desktop inserts <cr>
  394.     moveq    #' ',d1        ; space terminator
  395. get1:    move.b    (a2)+,d2    ; null byte?
  396.     beq.s    argok        ; if so, we're done
  397.     cmp.b    d1,d2        ; strip leading spaces
  398.     beq.s    get1
  399.     subq    #1,a2        ; unstrip start char
  400.     move.l    a2,(a1)+    ; and store that arg
  401. get2:    move.b    (a2)+,d2    ; next char
  402.     beq.s    argok        ; if null, we're done
  403.     cmp.b    d1,d2        ; if not space...
  404.     bne.s    get2        ; keep looking
  405.     clr.b    -1(a2)        ; terminate argv[argc] in the command tail
  406.     bra.s    get1        ; get next arg
  407. argok:    clr.l    (a1)+        ; terminate argv[]
  408. * --- allocate stack
  409.     move.l    a1,_stklimit    ; end of env/arg vectors is stack limit
  410.     add.l    _stksize,a1    ; allocate _stksize bytes of stack
  411.     move.l    a1,sp        ; set initial stack pointer
  412. * --- release unused memory
  413.     sub.l    a5,a1        ; size to keep
  414.     move.l    a1,-(sp)
  415.     move.l    a5,-(sp)    ; base of block to shrink
  416.     pea    $4a0000        ; Mshrink fn code + junk word of 0
  417.     trap    #1
  418.     lea    12(sp),sp    ; pop args
  419. *
  420. * Everything beyond here depends on implementation.
  421. * At this point, _environ points to envp[], _argvecs points to argv[],
  422. * and _stklimit points to the end of the argv array.  Thus argc can
  423. * be calculated as ((_stklimit-_argvecs)/4)-1.
  424. * _main could be invoked as follows:
  425. *
  426.     move.l    a5,-(sp)    ; basepage
  427.     move.l    _environ,-(sp)    ; envp[]
  428.     move.l    _argvecs,-(sp)    ; argv[]
  429.     move.l    _stklimit,d0    ; 4 bytes past end of argv[]
  430.     sub.l    (sp),d0        ; (argc+1) * sizeof( char * )
  431.     asr.l    #2,d0        ; argc+1
  432.     subq    #1,d0        ; argc
  433.     move    d0,-(sp)
  434.     jsr    _main        ; call mainline
  435.     lea    14(sp),sp    ; pop args
  436.  
  437.  
  438. A Final Note
  439.  
  440. This specification was formulated with careful deliberation, and with
  441. input from several companies and developers who have created
  442. development tools for GEMDOS.  The Mark Williams extended argument
  443. passing scheme was the main influence for this specification, because
  444. it has been in use, and supported by Mark Williams and other companies
  445. for several years.  This specification is very similar to the Mark
  446. Williams scheme, with the following important exceptions:
  447.  
  448. 1) Under the specification, the arguments after the ARGV environment
  449. variable may be validated by checking the command tail length byte.
  450. The Mark Williams execve() library function uses the command tail
  451. length byte as a telltale, but it is not checked by the crts0 startup
  452. code.  This validation is important for the reasons mentioned in the
  453. Rationale section above.
  454.  
  455. 2) The specification allows the ARGV environment variable to take on any
  456. value.  Mark Williams uses the value of ARGV as an iovector, which is
  457. described in the Mark Williams documentation.  The iovector should no
  458. longer be needed, as its primary purpose was to simplify the MWC
  459. implementation of the C library function isatty().
  460.  
  461. 3) Some versions of the MWC startup code do not require the ARGV= to
  462. have an `='.  Because ARGV is an actual environment variable in the
  463. specification, the equals character is required.
  464.  
  465. ------------------------------
  466.