LoginSignup
16
9

COBOLを40年ぶりにうごかしてみた:dockerでcobol。docker(81)プログラムちょい替え(4)

Last updated at Posted at 2019-02-01

COBOLについて議論している人たちがいる。

そういう暇があったらCOBOL動かそう。

とても楽しい。

40年前と比べたら地獄と天国。

JavaScriptを勉強しはじめた。COBOLみたいなもんだと思った。COBOLは出力を設計し、入力との論理関係を記述する言語。
JavaScriptが書ける人で、英語が嫌いじゃない人は、90%の確率でCOBOLが3時間で書けるようになると思った。

ゼロから始めるJavaScript。英語のYoutubeで勉強しようと思った。初日。

COBOLがない計算機の前に座って、40年ぶりにCOBOL動かした。

お題は、
Beginning COBOL for Programmers
https://www.apress.com/jp/book/9781430262534

githubにソースがある。
https://github.com/Apress/beg-cobol-for-programmers

<この項は書きかけです。順次追記します。>

docker hubからの利用

下で導入したcobolのシステムをdocker hubからrunするだけで、もうあなたはCOBOL World
なお、下記はMacosのdockerで、ユーザ名がwatashiの場合。Documentsの下でcobolというフォルダがある場合。

$ docker run -v /Users/watashi/Documents/cobol:/home/cobol -it kaizenjapan/cobol /bin/bash

/Users/watashi/Documents/cobol
のところは、自分が読み書きできる場所を指定してください。

COBOLを動かす場所まで移動して
pwd
コマンドを実行して、コピペするのが間違いない。

現在の知見

  1. 左側に行番号があってもなくてもよい。
  2. 行番号がない場合は、コンパイルオプションをつけないと空白またはタブが必要。
  3. 「cobc -x ソースファイル名」 で実行プログラムを作成できる。
  4. 「./実行プログラム名」 で実行できる。
  5. 上記2つをスクリプトにしてみる。
  6. 先頭にタブを入れるプログラムをcobolで作ってみる。(未着手)
  7. -freeをつければ左の行番号、空白、タブが要らない
  8. ファイルの読み込みで大文字小文字の違いでエラーになることあり。

ファイル名の大文字、小文字の違いでエラーになる場合

# ../co.sh Listing5-6
libcob: File does not exist (STATUS = 35) File : 'Listing5-6-TData.Dat'
# mv Listing5-6-TData.dat Listing5-6-TData.Dat
# ./Listing5-6
Teresa Casey              08712351 LM042
Padraig Quinlan           08712352 LM051
Kevin Tucker              08712353 LM051
Maria Donovan             08712354 LM042
Liam Lorigan              98712355 LM110
Fiachra Luo               98712356 LM051

プログラムの中のファイル指定を変更してもよい。

Listing5-6.cbl
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing5-6. 
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
   SELECT StudentFile ASSIGN TO "Listing5-6-TData.Dat" 
    ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentDetails.
   88  EndOfStudentFile  VALUE HIGH-VALUES.
   02  StudentId       PIC X(8). 
   02  StudentName     PIC X(25).
   02  CourseCode      PIC X(5). 

PROCEDURE DIVISION.
Begin.
   OPEN INPUT StudentFile 
   READ StudentFile 
      AT END SET EndOfStudentFile TO TRUE
   END-READ
   PERFORM UNTIL EndOfStudentFile 
      DISPLAY StudentName SPACE StudentId SPACE CourseCode 
      READ StudentFile 
         AT END SET EndOfStudentFile TO TRUE
      END-READ 
   END-PERFORM
   CLOSE StudentFile 
   STOP RUN.
   SELECT StudentFile ASSIGN TO "Listing5-6-TData.Dat" 

   SELECT StudentFile ASSIGN TO "Listing5-6-TData.dat" 

にしてもよい。

##スクリプトにしてみる。

#!/bin/bash

cobc -x -free $1.cbl
./$1

ファイルとして作るには

cd ..
vi co.sh

iをおしてから、上記をコピペ

[esc]キーを押して

:wq

現在のフォルダにadd.cblというファイルがあり、co.shが一つしたのディレクトリにあれば

$ ../co.sh add

chap07

Number OK/ERROR
Listing 7-1 Reading the Employee file.cbl E1->O
Listing 7-2 Reading the Employee file and displaying the records.cbl O
Listing 7-3 Writing and Reading the Employee file.cbl O
Listing 7-4 Displays the value of the gadgets in stock.cbl E2
Listing 7-5 Adds two records and then displays stock values again.cbl E2

E1
In paragraph 'Begin':
Error: Unknown statement 'Listing'
No such file or directory

最後にListingとうい文字列のある行が付け加わっていた。
削除したらエラーは取れた。

E2:
Error: syntax error, unexpected $undefined, expecting "end of file"
No such file or directory

chap08

8-4

Listing8-4.cbl:32: Error: No previous data item of level 08
../co.sh: line 4: ./Listing8-4: No such file or directory

Listing8-4.cbl:36: Error: No previous data item of level 04

Listing8-4.cbl:32: Error: PICTURE clause required for 'MOBandDOB'

ファイル名が長いと
File does not exist (STATUS = 35)

8-5

ファイル名が長い。短くすると通った。

Chapter 9

9.2

# ../co.sh Listing9-2
Listing9-2.cbl:9: Error: syntax error, unexpected DATA
../co.sh: line 4: ./Listing9-2: No such file or directory
# cat Listing9-2.cbl
IDENTIFICATION DIVISION.
PROGRAM-ID. Listing9-2.
AUTHOR. Michael Coughlan.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
    CURRENCY SIGN IS "?"
    
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Edit1     PIC  ???,??9.99.

PROCEDURE DIVISION.
Begin.
   MOVE 12345.95 TO Edit1
   DISPLAY "Edit1 = " Edit1
   STOP RUN.

7行目の最後にピリオドがない。CURRENCY SIGN IS "?"の後にピリオドを付け加えたらコンパイル通った。

# ../co.sh Listing9-2
Edit1 = ?12,345.95

9.3

# ../co.sh Listing9-3
Listing9-3.cbl:15: Error: Invalid picture string - '$$$,$$9.99'
Listing9-3.cbl:17: Error: Invalid picture string - '???,??9.99'
Listing9-3.cbl: In paragraph 'Begin':
Listing9-3.cbl:30: Error: 'PrnPoundValue' is not numeric or numeric-edited name
../co.sh: line 4: ./Listing9-3: No such file or directory

背景

日本でCOBOLが落ち目なのは、PCのCOBOLを真面目にやる人がすくなかったからだけだと思う。

座って1時間で、COBOLを導入し、ソースをもってきて、コンパイルして動かす。

30年ほど前に、マイクロソフトのカンファレンスでサンフランシスコ近郊に行った時には、PCでCOBOLうごかしている人たちいっぱいいた。

日本は当時、その100倍以上する機材でうごかしていたような気がする。
富士通、日立ではPCのCOBOLやってた人もいた気がする。

私はPCでアセンブラ, BASIC, メインフレームでCOBL, FORTRAN, またPCに戻ってCと移って行ったのでPCでCOBOLは仕事ではうごかしていない。

COBOLは紙カードを打って、穴をあけてcodingしていた。
それを紙カードリーダにかける順番を待って、よみこんで、コンパイルして実行するのに、

それぞれの機材が混んでいると1日に午前と午後の2回しかコンパイルできないことも。

んで、エラーがあると、また紙カード打ち直して。
紙カードリーダにかけて。

コンパイルの実行や、走らせるのを
JCL(Job Control Language)という、今で言うスクリプト言語で書いたものも、
紙カードでパンチしたり。あれ、何打ったかわすれちゃった。

1時間に10回も20回もコンパイルできる今とでは、地獄と天国だって。

100分の1の費用でできることを、やらずに下降線をたどるままに今日まで来たんだろうか。

動かすのが1時間、docker hubに登録するのが30分。この資料の整理時間を入れて。1時間半の間に、お風呂にお湯をはったり、お湯をわかして緑茶を4杯飲んだりの雑用もこなして。

留意点

今回gitから持って来たソースは、前処理が2つ要る。

  1. ファイル名がPCだと空白の処理がうまくできない場合がある。空白のない名前のファイルに移動(mv)。

  2. 行頭にtab もしくは行番号を入れる。そうしないとコンパイルエラーになることがある。

cobol文書

日本工業規格
JIS X3002:2011(ISO/IEC 1989:2002)電子計算機プログラム言語 COBOLInformation technology-Programming languages-COBO
http://kikakurui.com/x3/X3002-2011-01.html

ISO/IEC 1989:2014
https://www.iso.org/standard/51416.html
Information technology -- Programming languages, their environments and system software interfaces -- Programming language COBOL

#Beginning COBOL for Programmers
https://www.apress.com/jp/book/9781430262534

$ docker run -v /home/ogawakiyoshi/Documents/cobol/home/cobol -it ubuntu /bin/bash

# apt update; apt upgrade

# apt install open-cobol, vim, git

# git clone https://github.com/Apress/beg-cobol-for-programmers.git
IDENTIFICATION DIVISION.
PROGRAM-ID.  ShortestProgram.
PROCEDURE DIVISION.
DisplayPrompt.
     DISPLAY "I did it".
# cobc -x 'Listing 2-1  Shortest COBOL program.cbl' 
Listing 2-1  Shortest COBOL program.cbl:1: Error: Invalid indicator 'F' at column 7

今日から始めるCOBOL(1/2)
http://blog.adjust-work.com/680/

000010 IDENTIFICATION                   DIVISION.
000020 PROGRAM-ID.                      SAMPLE-01.
000030*
000040 ENVIRONMENT                      DIVISION.
000050*
000060 DATA                             DIVISION.
000070*
000080 PROCEDURE                        DIVISION.
000090 MAIN.
000100     DISPLAY "Hello world!"  UPON CONSOLE.
000110     STOP RUN.
# cobs -x hoge.cob
# ./hoge
Hello world!

行番号があっても大丈夫。

       IDENTIFICATION                   DIVISION.
       PROGRAM-ID.                      SAMPLE-01.
       *
       ENVIRONMENT                      DIVISION.
       *
       PROCEDURE                        DIVISION.
            DISPLAY "I did it!".

行番号がなくても大丈夫。

# cobc -x Listing\ 2-3\ \ The\ DoCalc\ example\ program.cbl 
gcc: error: 2-3: No such file or directory
gcc: error: The: No such file or directory
gcc: error: DoCalc: No such file or directory
gcc: error: example: No such file or directory
gcc: error: program: No such file or directory
root@266eec874239:/home/ogawakiyoshi/Documents/cobol/beg-cobol-for-programmers/978-1-4302-6253-4_Coughlan_Ch02# cobc -x 'Listing 2-3  The DoCalc example program.cbl' 
gcc: error: 2-3: No such file or directory
gcc: error: The: No such file or directory
gcc: error: DoCalc: No such file or directory
gcc: error: example: No such file or directory
gcc: error: program: No such file or directory

# cp 'Listing 2-3  The DoCalc example program.cbl' docal.cbl
# cobc -x docal.cbl
# ./docal.cbl
Please enter two single digit numbers 
1
2
Result is = 03

拡張子はcobでもcblでも大丈夫。

        IDENTIFICATION DIVISION.
        PROGRAM-ID. CobolGreeting.
        *>Program to display COBOL greetings 
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01  IterNum   PIC 9 VALUE 5.

        PROCEDURE DIVISION.
        BeginProgram.
           PERFORM DisplayGreeting IterNum TIMES.
           STOP RUN.
   
        DisplayGreeting.
           DISPLAY "Greetings from COBOL".
# cp 'Listing 2-2  The COBOL Greeting program.cbl'  tcgp.cbl
# cobc -x tcgp.cbl
# ./tcgp.cbl
Greetings from COBOL
Greetings from COBOL
Greetings from COBOL
Greetings from COBOL
Greetings from COBOL

説明 01 Iternum PIC 9 VALUE 5.
が変数の定義と値を5で初期化

PERFORM DisplayGreeting IterNum TIMES.
で、IterNum回数DisplayGreetingルーチンを実行。

COBOLって、英語が読めればわかる言語。すごく簡単。COBOLERって職業いらない。英語がよみかきできれば計算して結果を出せる。
データの構造定義も楽。関数呼び出し風じゃないのが抵抗がある人はいるかも。英語は関数呼び出しじゃないからいいじゃん。

DATA DIVISION
PROCEDURE DIVISION
ってデータと処理が分離してあって構造的。

dockerに保存

うごかしているdockerとは別のPCのOSのコマンドプロンプトで

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
266eec874239        ubuntu              "/bin/bash"         About an hour ago   Up About an hour                        ecstatic_easley

$ docker commit 266eec874239  kaizenjapan/cobol
sha256:2eaafd2ced044b906e15b2276e1fe5987b7202f4816065efd725cd3988bc7e98
$ docker push kaizenjapan/cobol
The push refers to repository [docker.io/kaizenjapan/cobol]
98f34bdd40b3: Pushed 
27a216ffe825: Mounted from library/ubuntu 
9e9d3c3a7458: Mounted from library/ubuntu 
7604c8714555: Mounted from library/ubuntu 
adcb570ae9ac: Mounted from library/ubuntu 
latest: digest: sha256:dfaf8a3e5c910197465469fac68d35948dcabdc0341dccb668196599b5fd5bb3 size: 1363

参考資料(reference)

QiitaでもCOBOL
https://qiita.com/kaizen_nagoya/items/60596bad631956127cf8

help

# cobc --help
Usage: cobc [options] file...

Options:
  --help                Display this message
  --version, -V         Display compiler version
  -v                    Display the programs invoked by the compiler
  -x                    Build an executable program
  -m                    Build a dynamically loadable module (default)
  -std=<dialect>        Compile for a specific dialect :
                          cobol2002   Cobol 2002
                          cobol85     Cobol 85
                          ibm         IBM Compatible
                          mvs         MVS Compatible
                          bs2000      BS2000 Compatible
                          mf          Micro Focus Compatible
                          default     When not specified
                        See config/default.conf and config/*.conf
  -free                 Use free source format
  -fixed                Use fixed source format (default)
  -O, -O2, -Os          Enable optimization
  -g                    Produce debugging information in the output
  -debug                Enable all run-time error checking
  -o <file>             Place the output into <file>
  -b                    Combine all input files into a single
                        dynamically loadable module
  -E                    Preprocess only; do not compile, assemble or link
  -C                    Translation only; convert COBOL to C
  -S                    Compile only; output assembly file
  -c                    Compile and assemble, but do not link
  -t <file>             Generate and place a program listing into <file>
  -I <directory>        Add <directory> to copy/include search path
  -L <directory>        Add <directory> to library search path
  -l <lib>              Link the library <lib>
  -D <define>           Pass <define> to the C compiler
  -conf=<file>          User defined dialect configuration - See -std=
  --list-reserved       Display reserved words
  --list-intrinsics     Display intrinsic functions
  --list-mnemonics      Display mnemonic names
  -save-temps(=<dir>)   Save intermediate files (default current directory)
  -MT <target>          Set target file used in dependency list
  -MF <file>            Place dependency list into <file>
  -ext <extension>      Add default file extension

  -W                    Enable ALL warnings
  -Wall                 Enable all warnings except as noted below
  -Wobsolete            Warn if obsolete features are used
  -Warchaic             Warn if archaic features are used
  -Wredefinition        Warn incompatible redefinition of data items
  -Wconstant            Warn inconsistent constant
  -Wparentheses         Warn lack of parentheses around AND within OR
  -Wstrict-typing       Warn type mismatch strictly
  -Wimplicit-define     Warn implicitly defined data items
  -Wcall-params         Warn non 01/77 items for CALL params (NOT set with -Wall)
  -Wcolumn-overflow     Warn text after column 72, FIXED format (NOT set with -Wall)
  -Wterminator          Warn lack of scope terminator END-XXX (NOT set with -Wall)
  -Wtruncate            Warn possible field truncation (NOT set with -Wall)
  -Wlinkage             Warn dangling LINKAGE items (NOT set with -Wall)
  -Wunreachable         Warn unreachable statements (NOT set with -Wall)

  -ftrace               Generate trace code (Executed SECTION/PARAGRAPH)
  -ftraceall            Generate trace code (Executed SECTION/PARAGRAPH/STATEMENTS)
  -fsyntax-only         Syntax error checking only; don't emit any output
  -fdebugging-line      Enable debugging lines ('D' in indicator column)
  -fsource-location     Generate source location code (Turned on by -debug or -g)
  -fimplicit-init       Do automatic initialization of the Cobol runtime system
  -fsign-ascii          Numeric display sign ASCII (Default on ASCII machines)
  -fsign-ebcdic         Numeric display sign EBCDIC (Default on EBCDIC machines)
  -fstack-check         PERFORM stack checking (Turned on by -debug or -g)
  -ffold-copy-lower     Fold COPY subject to lower case (Default no transformation)
  -ffold-copy-upper     Fold COPY subject to upper case (Default no transformation)
  -fnotrunc             Do not truncate binary fields according to PICTURE
  -ffunctions-all       Allow use of intrinsic functions without FUNCTION keyword
  -fmfcomment           '*' or '/' in column 1 treated as comment (FIXED only)
  -fnull-param          Pass extra NULL terminating pointers on CALL statements

参考資料(reference)

そういえば弊社もCOBOL保守してるけど全然触ったこと無いなという人向け、COBOLを完全理解できる記事
https://qiita.com/e99h2121/items/47857957fd549c83b5cb

2020年はCOBOL元年なので1時間でHello Worldに挑戦
https://qiita.com/glassmonkey/items/695e0b021cdcb3092b98

オブジェクト指向言語じゃない、COBOLで依存性逆転の法則を実践してみる
https://qiita.com/madilloar/items/f5126dbd03f69b5b986c

自己参照(self reference)

QiitaでもCOBOL

3大オブジェクト指向言語

プログラミング言語教育のXYZ

廃棄ソフトウェアまたは現在使っていないソフトウェア

データサイエンティストの気づき!「勉強して仕事に役立てない人。大嫌い!!」『それ自分かも?』ってなった!!!

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 初稿 20190201 午後8時
ver. 0.02 docker hub登録 20190201 午後9時
ver. 0.03 留意点追記 20190201 午後20時ちょい前
ver. 0.04 スクリプト作成、-free、ファイル名の大文字小文字関係のエラー追記 20190203 午前
ver. 0.05 chap07 ファイルエラー 20190203 夜
ver. 0.06 chap08 ファイル名の長さ 20190203 深夜
ver. 0.07 書籍紹介 20190206 午前
ver. 0.08 表題追記 20190514
ver. 0.09 一部加筆 20190515
ver. 0.10 URL追記 20210615
ver. 0.11 加筆 20220527
ver. 0.12 @R2-2021 さんのコメントでのご指摘にもとづき訂正。20220604
ver. 0.13 ありがとう追記 20230423

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

このエントリーをはてなブックマークに追加
http://b.hatena.ne.jp/guide/bbutton

16
9
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
9