detab,entabの拡張2014年11月08日 21:45

detab,entabを拡張します。引数でタブ位置の情報を渡せるようにします。

     detab 1 5 10 15 20
     entab 2 6 11 18 30

さらに、

     detab 1 +5
     entab 5 +6

はじめのは、タブ位置をそのまま引数で渡します。次のは、最初の引数を元に、次の引数で、インクリメンタルにタブ位置を計算します。

最初の版settab2.forを示します。

c settab2.for -- set initial tab stops
      subroutine settab(tabs)
      integer tabs(81)                  ! MAXLINE(81)
      integer*1 argv(81)                ! MAXLINE(81)
      integer argc,junk,t,tpos
      integer ctoi,getarg,mod

      do 10 tpos=1,81
          tabs(tpos) = 0                ! NO(0)
   10     continue
   
      if (getarg(1,argv,81) .eq. -1) then ! MAXLINE(81) EOF(-1)
          tpos = 1
          while (tpos .le. 81) do       ! MAXLINE(81)
              if (mod(i,8) .eq. 1) then
                  tabs(i) = 1           ! YES(1)
              endif
              tpos = tpos + 1
          end while
      else
          junk = 1
          tpos = ctoi(argv,junk)
          if ((tpos .gt. 0) .and. (tpos .le. 81)) then
              tabs(tpos) = 1            ! YES(1)
          end if
          if (getarg(2,argv,81) .ne. -1) then ! MAXLINE(81)
              if (argv(1) .eq. 43) then ! PLUS(43)
                  junk = 1
                  t = ctoi(argv,junk)
                  while (tpos .le. 81) do
                      tabs(tpos) = 1    ! YES(1)
                      tpos = tpos + t
                  end while
              else
                  junk = 1
                  tpos = ctoi(argv,junk)
                  if ((tpos .gt. 0) .and. (tpos .le. 81)) then
                      tabs(tpos) = 1    ! YES(1)
                  end if
                  argc = 3
                  while (getarg(argc,argv,81) .ne. -1) do ! MAXLINE(81) EOF(-1)
                      junk = 1
                      tpos = ctoi(argv,junk)
                      if ((tpos .gt. 0) .and. (tpos .le. 81)) then ! MAXLINE(81)
                          tabs(tpos) = 1 ! YES(1)
                      end if
                      argc = argc + 1
                  end while
              end if
          else
              tabs(tpos) = 1            ! YES(1)
          endif
      end if
      return
      end

見通しが悪く、長ったらしくなってしまいました。

ここで、下請けルーチン、インクリメンタル型の引数を確認するtabincr(),タブ位置が範囲に入っているかを調べるtabbound()を導入します。

tabincr()は以下の通り。

c tabincr.for -- return YES if incrimentsl tab set
      integer function tabincr()
      integer*1 argv(81)                ! MAXLINE(81)
      integer getarg

      if (getarg(2,argv,81) .eq. -1) then ! MAXLINE(81) EOF(-1)
          tabincr = 0                   ! NO(0)
      else
          if (argv(1) .eq. 43) then     ! PLUS(43)
              tabincr = 1               ! YES(1)
          else
              tabincr = 0               ! NO(0)
          end if
      end if
      return
      end

tabbound()は以下の通り。

c tabbound -- check tab position boundary
      integer function tabbound(tab,tabmin,tabmax)
      integer tab,tabmin,tabmax

      if ((tabmin .ge. tab) .and. (tab .le. tabmax)) then
          tabbound = 1                  ! YES(1)
      else
          tabbound = 0                  ! NO(0)
      end if
      return
      end

これらを使ったsettab()は以下の通り。

c settab3.for -- set initial tab stops
      subroutine settab(tabs)
      integer tabs(81)                  ! MAXLINE(81)
      integer*1 argv(81)                ! MAXLINE(81)
      integer argc,junk,tincr,tpos
      integer ctoi,getarg,mod,tabincr

      do 10 tpos=1,81
          tabs(tpos) = 0                ! NO(0)
   10     continue
   
      if (getarg(1,argv,81) .eq. -1) then ! MAXLINE(81) EOF(-1)
          tpos = 1
          while (tpos .le. 81) do       ! MAXLINE(81)
              if (mod(tpos,8) .eq. 1) then
                  tabs(tpos) = 1        ! YES(1)
              endif
              tpos = tpos + 1
          end while
      else
          junk = 1
          tpos = ctoi(argv,junk)
          if (tabbound(tpos,1,81) .eq. 1) then ! YES(1)
              if (tabincr() .eq. 1) then ! YES(1)
                  tabs(tpos) = 1        ! YES(1)
                  junk = getarg(2,argv,81) ! MAXLINE(81)
                  junk = 1
                  tincr = ctoi(argv,junk)
                  tpos = tpos + tincr
                  while (tpos .le. 81) do ! MAXLINE(81)
                      tabs(tpos) = 1    ! YES(1)
                      tpos = tpos + tincr
                  end while
              else
                  argc = 2
                  while (getarg(argc,argv,81) .ne. -1) do ! MAXLINE(81) EOF(-1)
                      junk = 1
                      tpos = ctoi(argv,junk)
                      if (tabbound(tpos,1,81) .eq. 1) then ! MAXLINE(1) YES(1)
                          tabs(tpos) = 1 ! YES(1)
                      end if
                      argc = argc + 1
                  end while
              end if
          end if
      end if
      return
      end

多少は見通しが良くなったでしょうか。