;***********************************************************************
; The following two defines should be customized with the dialing      *
; directory entry for your local TYMNET number and your DOW JONES      *
; password.                                                            *
;***********************************************************************

integer hcntr,maxstory,abend,storycnt,storypos
string news,storykey,storycode,slashchk,storylist
define TYMNET_ENTRY "3"
define PASSWORD "PUTITHERE"


proc main                       ; start of main procedure
   SET ENQ OFF
   FOPEN 0 "ABEND.DOW" "WT"
   FPUTS 0 "DOW CONNECTION FAILED"
   FCLOSE 0
   clear                        ; clear screen
   locate 0 0                   ; locate cursor at 0,0
   box 0 0 4 28 14              ; draw box
   atsay 2 2 14 "Logging onto DOW JONES..."  ; put message in box
   locate 6 0                   ; cursor to row 6

   if not fromddir              ; if not linked to a dial directory entry
      dial TYMNET_ENTRY         ; dial directory entry for TYMNET - make sure E71
   endif
   pause 1
   when disconnect call abendrun
   call logdow
   call getstkq
   call gethead
   call parsehead
   call getstory
   cwhen disconnect
   dos "ERASE ABEND.DOW" NOCLEAR
   transmit "disc^M"
   waitquiet 5 30
   quit
endproc
;********************************************************************
; PROC LOGDOW                                                       *
;      Logon to Dow Jones using TYMNET                              *
;********************************************************************
proc logdow
   pause 2
   transmit "^M"                ; transmit a carriage return
   waitfor "identifier" 45      ; wait for TYMNET prompt
   transmit "A"                 ; send an A (no carriage return)
   waitfor "log in: "           ; wait for TYMNET login prompt
   transmit "DOW1;;"            ; send "DOW1;;"
   waitfor "WHAT SERVICE PLEASE?"   ; wait for DOW prompt
   transmit "DJNS^M"            ; DOW JONES news service
   waitfor "PASSWORD"           ; wait for password prompt
   transmit PASSWORD            ; send password
   transmit "^M"                ; followed by a carriage return
   waitfor "ENTER QUERY"
endproc
;********************************************************************
; PROC: GETHEAD                                                     *
;       Log into DJNEWS and get the first two pages of headline     *
;       codes. Save then in a file.                                 *
;********************************************************************
proc gethead
   pause 1
   transmit "//djnews^M"
   waitfor "^S^Q"
   pause 1
   dos "DEL DJNEWS.SAV" NOCLEAR
   dos "COPY DJNEWS.TXT DJNEWS.SAV" NOCLEAR
   dos "DEL DJNEWS.TXT" NOCLEAR
   log open "djnews.txt"
   transmit ".i/tob 01^M"
   waitfor "^S^Q" 60
   if not waitfor
      log close
      call abendrun
      endif
   pause 1
   transmit "02^M"
   waitfor "^S^Q" 60
   if not waitfor
      log close
      call abendrun
      endif
   log close
   pause 1
endproc
;********************************************************************
; PROC: PARSEHEAD                                                   *
;       Read the captured DJNEWS file, and build a list of          *
;       story codes to be pulled down.                              *
;********************************************************************
proc parsehead
  set fgets_crlf off
  fopen 0 "djnews.txt" "rt"
  abend=0
  call findmax        ;check for headline, and get maximum story count
  if abend == 1
    fclose 0
    call abendrun
    endif
  call bldstory       ; build a list of story ID's
  message "return from bldstory"
  if storycnt == 0
    call abendrun
    endif
endproc

;********************************************************************
; PROC: FINDMAX                                                     *
;       Check captured list of headlines to make sure it is         *
;       valid. Then read the story id for the first story to        *
;       determine how many stories are present in the list.         *
;********************************************************************
proc findmax
  message "PROC: findmax"
  ; check for the "HEADLINE" string in the file
  while not eof 0
    fgets 0 news
    find news "HEADLINE" hcntr
    if found
      exitwhile
    else
      hcntr=0
      endif
    endwhile
  if hcntr == 0
    message "headline NOT found"
    abend=1
    goto findmax_end
    endif
  ; if here, we have a valid list of headlines, so read the first
  ; headline, and check the story id. Then determine how many
  ; stories are present in the list.
  fgets 0 news
  substr storykey news 1 1
  find "BCDEFGHIJKLMNOPQRSTUVWXYZ" storykey
  if found
    maxstory=26
  else
    substr storykey news 2 1
    find "ABCDEFGHIJKLMNOPQRSTUVWXYZ" storykey maxstory
    maxstory=maxstory+1
    endif
  findmax_end:
endproc
;********************************************************************
; PROC: BLDSTORY                                                    *
;       Read the list of headlines, and build a string of two       *
;       digit story codes. A maximum of 10 story id's will be       *
;       saved. Check the headline to make sure it does not have     *
;       an asterisk as the first position (unfinished stories).     *
;       At this point, the first headline is still in the           *
;       variable NEWS                                               *
;********************************************************************
proc bldstory
  message "PROC: bldstory"
  storycnt=0
  storylist="                    "
  while not eof 0
    find news "*" hcntr
    if found
       if hcntr == 11
          goto readend
          endif
       endif
    substr storycode news 1 2
    storypos=storycnt*2
    strupdt storylist storycode storypos 2
    storycnt=storycnt + 1
    if storycnt == 10
       exitwhile
       endif
    fgets 0 news
    readend:
    ; position to next headline
    while forever
       fgets 0 news
       if eof 0
          goto bldend
          endif
       substr slashchk news 6 1
       find slashchk "/"
       if found
          exitwhile
          endif
       endwhile
    endwhile
bldend:
fclose 0
endproc
;***********************************************************************
; PROC: GETSTORY                                                       *
;       Using the storylist that has been generated, feed each two     *
; digit code to dow jones, and capture all the stories to a file.      *
;***********************************************************************

proc getstory
hcntr = 0
dos "ERASE DOWSTORY.TXT" NOCLEAR
log open "DOWSTORY.TXT"
while hcntr < storycnt
 storypos = hcntr * 2
 substr storycode storylist storypos 2
 transmit storycode
 transmit "^M"
 when 0 "PRESS RETURN FOR MORE" TRANSMIT "^M"
 waitfor "-END-" 300
 if waitfor
    pause 2
    hcntr = hcntr + 1
    endif
 endwhile
 CWHEN 0
 LOG CLOSE
endproc
;********************************************************************
; PROC GETSTKQ                                                      *
;      Go to Historical qoutes, and get last 12 days                *
;********************************************************************
proc getstkq
   pause 1
   transmit "//hq^M"
   waitfor "^S^Q" 60
   if not waitfor
      call abendrun
      endif
   pause 1
   DOS "DEL DJSTKS.SAV" NOCLEAR
   DOS "COPY DJSTKS.TXT DJSTKS.SAV" NOCLEAR
   DOS "DEL DJSTKS.TXT" NOCLEAR
   log open "djstks.txt"
   transmit "UST P1^M"
   waitfor "^S^Q" 60
   if not waitfor
      log close
      call abendrun
      endif
   log close
endproc
;********************************************************************
; PROC abendrun                                                     *
;      Abend the job                                                *
;********************************************************************
proc abendrun
  message "abend detected"
  transmit "disc"
  transmit "^M"
  quit
endproc
