IDENTIFICATION DIVISION.
PROGRAM-ID. WriteHTML.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OutputFile ASSIGN TO "output.html"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD OutputFile.
01 HTML-LINE PIC X(80).
WORKING-STORAGE SECTION.
01 IDX PIC 9(2) VALUE 1.
01 HTML-CONTENT OCCURS 10 TIMES.
05 LINE-ITEM PIC X(80).
01 BROWSER-TABLE.
05 BROWSERS OCCURS 3 TIMES INDEXED BY BROWSER-ID.
10 BROWSER-NAME PIC X(20).
10 BROWSER-COMMAND PIC X(255).
01 USER-INPUT PIC X(20) VALUE SPACES.
01 SELECTED-BROWSER-COMMAND PIC X(255) VALUE SPACES.
01 SYSTEM-RETURN-CODE PIC S9(4) COMP VALUE 0.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM INIT-HTML-CONTENT
PERFORM INIT-BROWSER-TABLE
OPEN OUTPUT OutputFile
PERFORM WRITE-LINES
CLOSE OutputFile
DISPLAY "HTML file 'output.html' has been written successfully."
*> コマンドライン引数を取得
ACCEPT USER-INPUT FROM COMMAND-LINE
*> 入力に応じたブラウザを選択
PERFORM SEARCH-BROWSER
*> ブラウザコマンドが見つからない場合
IF SELECTED-BROWSER-COMMAND = SPACES
DISPLAY "Invalid browser specified. Using 'w3m' as default."
MOVE "w3m output.html" TO SELECTED-BROWSER-COMMAND
END-IF
*> システムコマンド実行
CALL "SYSTEM" USING BY CONTENT SELECTED-BROWSER-COMMAND
RETURNING SYSTEM-RETURN-CODE
IF SYSTEM-RETURN-CODE NOT = 0
DISPLAY "Failed to launch browser with return code: " SYSTEM-RETURN-CODE
END-IF
STOP RUN.
INIT-HTML-CONTENT.
MOVE "<!DOCTYPE html>" TO LINE-ITEM(1).
MOVE "<html lang=""ja"">" TO LINE-ITEM(2).
MOVE " <head>" TO LINE-ITEM(3).
MOVE " <meta charset=""UTF-8"">" TO LINE-ITEM(4).
MOVE " <title>タイトル</title>" TO LINE-ITEM(5).
MOVE " </head>" TO LINE-ITEM(6).
MOVE " <body>" TO LINE-ITEM(7).
MOVE " Hello HTML!" TO LINE-ITEM(8).
MOVE " </body>" TO LINE-ITEM(9).
MOVE "</html>" TO LINE-ITEM(10).
INIT-BROWSER-TABLE.
MOVE "w3m" TO BROWSER-NAME(1)
MOVE "w3m output.html" TO BROWSER-COMMAND(1)
MOVE "firefox" TO BROWSER-NAME(2)
MOVE "firefox output.html" TO BROWSER-COMMAND(2)
MOVE "vivaldi" TO BROWSER-NAME(3)
MOVE "vivaldi output.html" TO BROWSER-COMMAND(3).
SEARCH-BROWSER.
SET BROWSER-ID TO 1
PERFORM VARYING BROWSER-ID FROM 1 BY 1 UNTIL BROWSER-ID > 3
IF USER-INPUT = BROWSER-NAME(BROWSER-ID)
MOVE BROWSER-COMMAND(BROWSER-ID) TO SELECTED-BROWSER-COMMAND
EXIT PERFORM
END-IF
END-PERFORM.
WRITE-LINES.
PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > 10
MOVE LINE-ITEM(IDX) TO HTML-LINE
WRITE HTML-LINE
END-PERFORM.