コマンド行の引数2014年10月05日 11:01

コマンドラインのプログラムには、引数をつけることで、プログラムに様々な 情報を与えています。この引数を受け取る関数getargを作ります。これは、 Watcom Fortran77の拡張機能iargc(),igetarg()を使い実現します。プログラムは次の通り。

c getarg.for -- get n-th command line argument
      integer function getarg(n,arg,maxsiz)
      integer n, maxsiz
      integer*1 arg(maxsiz)
      
      character*100 carg
      integer*1 iarg(100)
      equivalence (carg,iarg)
      integer l
      integer iargc,igetarg

      if (n .ge. iargc()) then
          getarg = -1                   ! EOF(-1)
          arg(1) = -2                   ! EOS(-2), return NULL string
          return
      endif
      l = igetarg(n,carg)
      i = 1
      while (i .le. l .and. i .le. maxsiz) do
          arg(i) = iarg(i)
          i = i + 1
      end while
      arg(i) = -2                       ! EOS(-2) <<< BUF FIX 10/12
      getarg = i - 1                  ! <<< BUF FIX 10/12  
      return
      end

igetc()は、コマンドラインの引数の数を返します。これには、コマンド自身も含まれます。igetarg(n,str)は、 n番目の引数をcharacter型の文字列strにコピーし長さを返します。strはこのままでは使えないので、integer*1の 文字列に変換し利用します。getarg(n,arg,maxsiz)は、n番目の引数を最大maxsiz文字分argにコピーし、 引数があればYES(1)を そうでなければNO(0)を返します。

getarg()を使った例題echoを示します。コマンドラインの引数を表示する簡単なものです。

まずは、RATFOR版

# echo.r4 -- echo command line arguments
      character arg(MAXARG)
      integer i,n
      integer getarg

      for (n = 1; getarg(n,arg,MAXARG) != EOF; n = n + 1) {
          for (i = 1; arg(i) != EOS; i = i + 1)
              call putc(arg(i))
          call putc(BLANK)
          }
      call putc(NEWLINE)
      stop
      end

Watcom Fortran77版

c echo.for -- echo command line arguments
      program echo
      integer*1 arg(100)                ! MAXARG(100)
      integer i,n
      integer getarg

      n = 1
      while (getarg(n,arg,100) .ne. -1) do ! MAXARG(100) EOF(-1)
          i = 1
          while(arg(i) .ne. -2) do      ! EOS(-2)
              call putc(arg(i))
              i = i + 1
          end while
          call putc(32)                 ! BLANK(32)
          n = n + 1
      end while
      call putc(10)                     ! NEWLINE(10)
      stop
      end

早速、コンパイルしてみましょう。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fc getarg
Open Watcom FORTRAN 77/32 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
..\src\getarg.for: 23 ステートメント, 95 バイト, 7 拡張メッセージ, 0 警告エラー, 0 エラー
        1 個のファイルを移動しました。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fo getarg
Open Watcom Library Manager Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fc echo
Open Watcom FORTRAN 77/32 Optimizing Compiler Version 1.9
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
..\src\echo.for: 17 ステートメント, 112 バイト, 5 拡張メッセージ, 0 警告エラー, 0 エラー
        1 個のファイルを移動しました。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>fl echo
Open Watcom Linker Version 1.9
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
オブジェクトファイルを読込み中
ライブラリを検索中
a Windows NT character-mode 実行可能 を作成中
        1 個のファイルを移動しました。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>

テストします。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>cd ..\exe

C:\Users\Hiroya\Documents\ratfor\fortran\exe>.\echo one two three
one two three

C:\Users\Hiroya\Documents\ratfor\fortran\exe>.\echo one "t   w   o" three
one t   w   o three

C:\Users\Hiroya\Documents\ratfor\fortran\exe>

ダブルクォートで囲むと、ブランクも含めて引き渡すことができます。