home *** CD-ROM | disk | FTP | other *** search
AMOS Source Code | 1992-02-26 | 4.1 KB | 133 lines |
- ' This is a routine by Michael D. Cox (aj639@Cleveland.freenet.edu)
- ' I was looking through the RKMs and saw an assembler routine to determine
- ' all the command line arguments for a command. So, I was bored and
- ' decided to convert it to AMOS, which was real simple.
- '
- ' These lines just setup an example.
- ' _MAXARGC is a variable that has the maximum number of arguments your program
- ' will accept.
- '
- ' ARGV$(_MAXARGC) is an array that will hold the FINAL arguments.
- ' CMDLINE$ will actually become CMDLINE$ = Command Line$ when you have
- ' compiled your program and are ready to distribute it.
- ' _TOTAL_ARGS will be assigned the actual number of arguments
- '
- ' Unfold the procedure and it will explain what happens
- '
- ' You may modify this to handle whatever other type of arguments you need
- ' but please send me a copy just so I can see what changes were made.
- ' Thanks!!
- ' Michael Cox, October 7, 1993, 19:10 EDT
- ' (703)742-2991 (voice), (703)742-2543 (fax)
- ' 11128 Rock Garden Drive, Fairfax VA 22030-4935 USA
- '
- _MAXARGC=6
- Dim ARGV$(_MAXARGC)
- Shared ARGV$()
- CMDLINE$="first second "+Chr$(34)+"third arg"+Chr$(34)+" "+Chr$(34)+"*Njim"+Chr$(34)+" *E4"
- Print CMDLINE$
- _GET_ARGS[CMDLINE$,_MAXARGC]
- _TOTAL_ARGS=Param
- For X=0 To _TOTAL_ARGS-1
- Print ARGV$(X)
- Next
- Procedure _GET_ARGS[CMDLN$,ARGC]
- ' Here we just set the array to empty strings
- For X=0 To ARGC-1
- ARGV$(X)=""
- Next X
- ' We now assign our needed variables
- ' CMDLN_LEN = Length of the CMDLN$ so we know when we have reached the end
- CMDLN_LEN=Len(CMDLN$)
- ' CURR_ARG = Current command line argument we are processing
- CURR_ARG=0
- ' CURR_POS = Position within CMDLN$
- CURR_POS=1
- '
- ' We begin to find the first character in the argument
- _START_ARG:
- If CURR_POS>CMDLN_LEN : Rem Make sure we have not gone past EOL
- Goto ARG_EXIT
- End If
- ' Compare current chr to a space or a tab (chr$(9)) which seperate args
- If(Mid$(CMDLN$,CURR_POS,1)=" ") or(Mid$(CMDLN$,CURR_POS,1)=Chr$(9))
- Inc CURR_POS
- Goto _START_ARG
- End If
- ' Just a check to make sure we have not found an extra arg
- If(CURR_ARG+1)>ARGC
- Goto ARG_EXIT
- End If
- ' See if the chracter is a double quote (")
- If Mid$(CMDLN$,CURR_POS,1)=Chr$(34)
- Inc CURR_POS
- Goto _QUOTE
- End If
- ' Well, if we get to here we have found the first character
- ARGV$(CURR_ARG)=Mid$(CMDLN$,CURR_POS,1)
- Inc CURR_POS
- '
- ' Begin looking for the next character
- _NEXT_CHR:
- If CURR_POS>CMDLN_LEN
- Goto ARG_EXIT
- End If
- If(Mid$(CMDLN$,CURR_POS,1)=" ") or(Mid$(CMDLN$,CURR_POS,1)=Chr$(9))
- Goto _END_ARG
- End If
- ' If it is not a space or tab or end of the line, must be the next character
- ARGV$(CURR_ARG)=ARGV$(CURR_ARG)+Mid$(CMDLN$,CURR_POS,1)
- Inc CURR_POS
- ' So, find the next one and keep going . . .
- Goto _NEXT_CHR
- '
- ' We found all the characters in an argument
- _END_ARG:
- Inc CURR_ARG
- Inc CURR_POS
- Goto _START_ARG
- '
- ' Our routine to handle quotes which are used to put around arguments that
- ' may have spaces or other special characters in them.
- _QUOTE:
- If CURR_POS>CMDLN_LEN
- Goto ARG_EXIT
- End If
- If Mid$(CMDLN$,CURR_POS,1)=Chr$(34)
- Goto _END_ARG
- End If
- ' A splat (*) is the BCPL equivalent to the ESC command for doing ANSI codes
- If Mid$(CMDLN$,CURR_POS,1)<>"*"
- Goto _ADD_QUOTE_CHR
- End If
- Inc CURR_POS
- ' A *N is a newline or chr$(10)
- If Upper$(Mid$(CMDLN$,CURR_POS,1))<>"N"
- Goto _CHECK_ESC
- End If
- ARGV$(CURR_ARG)=ARGV$(CURR_ARG)+Chr$(10)
- Inc CURR_POS
- Goto _QUOTE
- '
- ' A *E is an actual ESC or chr$(27)
- _CHECK_ESC:
- If Upper$(Mid$(CMDLN$,CURR_POS,1))<>"E"
- Goto _ADD_QUOTE_CHR
- End If
- ARGV$(CURR_ARG)=ARGV$(CURR_ARG)+Chr$(27)
- Inc CURR_POS
- Goto _QUOTE
- '
- ' We made it to here, meaning there were no special escape codes found
- ' so add it to the current argument string
- _ADD_QUOTE_CHR:
- ARGV$(CURR_ARG)=ARGV$(CURR_ARG)+Mid$(CMDLN$,CURR_POS,1)
- Inc CURR_POS
- Goto _QUOTE
- '
- ' Goto here when we have found all arguments
- ARG_EXIT:
- CMDLN$=""
- CMDLN_LEN=0
- CURR_POS=0
- End Proc[CURR_ARG+1]