int main(int argc, char **argv) { int res; inout_chan *chan; initialize_libIVC_library(); printf("libIVC initialized\n"); chan = connect_two_way("c_rot13"); printf("Connected!\n"); while(1) { char *input_line = NULL, *output_line = NULL; int len; len = read_unknown_chan(chan, (void**)&input_line); if(!len) { printf("Error reading from input channel!\n"); abort(); } input_line = haskell_to_c_string(input_line, len); printf("input: %s\n", input_line); input_line = rot13string(input_line); printf("rot13: %s\n", input_line); output_line = c_to_haskell_string(input_line); res = write_chan(chan, output_line, strlen(output_line)); printf("res: %d\n", res); free(input_line); free(output_line); } }
short main (short argc, char *argv[]) { unsigned short status=0; /* TX and RX status */ unsigned short tries; /* Attempts to send a file */ unsigned short cmp_size; /* Size after compression */ unsigned short data_written; /* Data written to the file */ unsigned short data_read; /* Data read from the file */ char *file_name; /* filename */ char *function; /* Receive, Transmit */ char *com_port; /* Communications adapter port */ file_name = get_inp (argc, argv); /* Get file name */ if (file_name == NULL ) { disp(); /* Display usage message */ return JM_FNF; } function = get_fun (argc, argv); /* Get function 'R' or 'S' */ if (function == NULL) { disp(); /* Display usage message */ return JM_CMD; } com_port = get_prt (argc, argv); /* Get port '1 to 4 ' */ if (com_port == NULL) { disp(); /* Display usage message */ return JM_CMD; } port = get_port(*com_port); /* Convert port to an offset */ /****************************************************************************/ /* Allocate buffers */ /****************************************************************************/ in_buffer = allocate_memory(DAT_LEN); /* Get some memory for input */ if (in_buffer == NULL) return JM_MEM; /* No memory available */ out_buffer = allocate_memory(DAT_LEN); /* Get some memory for output */ if (out_buffer == NULL) return JM_MEM; /* No memory available */ comp_buffer=allocate_memory(DAT_LEN); /* Get memory for compression */ if (comp_buffer == NULL) return JM_MEM; /* No memory available */ file_buffer=allocate_memory(DAT_LEN); /* Get memory for file buffer */ if (file_buffer == NULL) return JM_MEM; /* No memory available */ int_buffer =allocate_memory(DAT_LEN); /* Memory for interrupt buffer */ if (int_buffer == NULL) return JM_MEM; /* No memory available */ /****************************************************************************/ screen (SCR_SGN,NULL,NULL); /* Write signon screen */ syst.s_len = BLK_SIZ; syst.s_byt = 0; syst.s_blk = 0; syst.s_sta = okay; switch(*function) /* Functions are TX and RX */ { /****************************************************************************/ /* Receive JMODEM file */ /****************************************************************************/ case 'R': { if (!file_io(CREATE, &handle, &file_name, NULL) ) { buff = (JBUF *) in_buffer; /* Assign type JBUF */ open_chan(port); /* Open com channel */ screen (SCR_STA,NULL,NULL); /* Write status block */ status = rx_sync(); /* Synchronize */ if (!status) screen (SCR_SYR,NULL,NULL); data_written = 0xFFFF; tries = 10; /* Attempts to receive */ while ( (data_written) /* Write file okay */ && (!user_abort ) /* No break key */ && (!status ) /* Recev block okay */ && (tries--) ) /* 10 retries */ { time(&start); /* Get starting time */ screen (SCR_SYS,&syst,NULL); /* Show status block */ status = recv_blk ( /* Receive data-block*/ &syst.s_len, /* Block length */ in_buffer); /* Input buffer */ if (status) /* If bad */ break; /* Abort the WHILE */ if( (!(calc_crc(GET_CRC, /* Calculate CRC */ syst.s_len, /* Amount to check */ in_buffer) )) /* Receiver buffer */ && ( buff->blk_num == /* Check block also */ (unsigned char) (syst.s_blk +1))) /* Block number */ { syst.s_sta = okay; /* Text pointer */ tries=10; /* Reset count */ syst.s_len -= OVRHD; /* Subtract overhead */ *out_buffer = ACK; /* Good */ write_chan(1,out_buffer); /* Send the ACK */ /* If data was compressed */ if ( (buff->blk_typ & COMP) == COMP) { syst.s_len = decode ( /* Decode the data */ syst.s_len, /* Data-block length */ &buff->blk_dat, /* Where to start */ file_buffer); /* Where to put data */ } else /* Data was normal (not compressed, just copy ) */ { memcpy (file_buffer,&buff->blk_dat,syst.s_len); } /* Write to the file */ data_written = file_io( WRITE , /* Function */ &handle, /* File handle */ &file_buffer , /* Where data is */ syst.s_len ); /* Amount to write */ syst.s_byt += data_written; /* Total bytes */ syst.s_blk++; /* Block number */ time(&finish); /* Get end time */ if (finish - start) /* Check div/0 */ syst.s_cps = (short) /* Calc Block CPS */ (data_written / (finish - start) ); /* Check for end-of-file */ if ( (buff->blk_typ & EOF_) == EOF_) { /* This was the end of file */ file_io(CLOSE, /* Function */ &handle, /* Open handle */ &file_name, /* Name not used */ NULL); /* Buffer not used */ close_chan(port); /* Close the port */ status = JM_NRM; /* Set status */ goto cleanup; /* exit routine */ } } else { *out_buffer = NAK; /* Bad block */ syst.s_sta = retry; /* Char pointer */ write_chan(1,out_buffer); /* Send the NAK */ } } close_chan(port); /* Aborted */ file_io(DELETE, /* Function */ &handle, /* File handle */ &file_name, /* Name */ NULL); /* Buffer not used */ status = JM_ABT; break; /* Exit if() {} */ } else /* Can't create file */ { status = JM_CRE; break; /* Exit while() {} */ } break; /* Exit case 'R' */ } /****************************************************************************/ /* Send JMODEM file */ /****************************************************************************/ case 'S': /* Send JMODEM file */ { if (!file_io(OPEN_READ, &handle, &file_name, NULL) ) { buff = (JBUF *)out_buffer; /* Assign type JBUF */ syst.s_byt = 0; /* Restore byte count */ open_chan(port); /* Open COM port */ data_read = 0xFFFF; /* Initialize */ screen (SCR_STA,NULL,NULL); /* Write status block */ status = tx_sync(); /* Synchronize */ if (!status) screen (SCR_SYT,NULL,NULL); while ( (!user_abort) /* Ctrl - break */ && (!status) ) /* sent okay */ { time(&start); /* Get starting time */ data_read = file_io( READ , /* Read a record */ &handle , /* File pointer */ &file_buffer , /* Where to put */ syst.s_len ); /* Amount to read */ if (!data_read) /* Past end of file */ break; syst.s_byt += (long) data_read; /* Running count */ screen (SCR_SYS,&syst,NULL); /* Show status block */ buff->blk_num = (unsigned char) ++syst.s_blk; /* Block number */ if (data_read != syst.s_len) /* Byte request */ buff->blk_typ = EOF_; /* Into control-byte */ else buff->blk_typ = NORM; /* Normal block */ cmp_size = encode (data_read, /* Encode size */ file_buffer, /* Source */ comp_buffer); /* Destination */ if ( cmp_size < data_read ) /* If compressed */ { buff->len = (cmp_size+OVRHD); /* Length of block */ buff->blk_typ |= COMP; /* Show compressed */ memcpy (&buff->blk_dat, /* Start of data */ comp_buffer, /* Copy from here */ cmp_size); /* This much */ } else /* Not compressed */ { buff->len = (data_read+OVRHD);/* Length of block */ memcpy (&buff->blk_dat, /* Copy to */ file_buffer, /* Copy from */ data_read); /* This amount */ } calc_crc(SET_CRC, /* Calculate CRC */ buff->len , /* Length of block */ out_buffer); /* Where data is */ status = send_blk( /* Send the block */ buff->len, /* Block length */ &syst, /* Read block ptr. */ out_buffer); /* Buffer pointer */ time(&finish); /* Get end time */ if (finish - start) /* Guard div/zero */ syst.s_cps = (short) /* Calc Block CPS */ (data_read / (finish - start) ); if ( buff->blk_typ == EOF_) /* Last record */ break; } close_chan(port); /* Close the port */ if (status) syst.s_sta = abrt; /* A text pointer */ else syst.s_sta = done; /* A text pointer */ file_io(CLOSE, &handle, &file_name, NULL); /* Close the file */ screen (SCR_SYS,&syst,NULL); /* Show status block */ } else /* File not found */ { status = JM_FNF; } break; /* End of CASE 'S' */ } } cleanup: free (in_buffer); /* Free buffers */ free (out_buffer); free (comp_buffer); free (file_buffer); /* Five-second timer to display error messages */ if (status != JM_NRM) { time(&finish); start = 0; finish += 5; while ( finish > start ) time(&start); } screen (SCR_END,NULL,NULL); /* Clear the screen */ return status; /* Normal exit */ }