/****************************************************************************

                                   MFJTerm

  Amateur Radio terminal program for the MFJ-1278 Multimode Data Controller

*****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include "mfjterm.h"
#include "init.h"
#include "inout.h"
#include "timefuncs.h"


int cur_colors;

int amt2log_fifo, log2amt_fifo;

FILE *capf, *bbs_capf;
int cap = 0, bbs_cap = 0, bbs_capf_open = 0;

char capsign[2] = {' ','C'};
char modesign[6] = {'A','I','R','P','V','H'};
char shiftsign[2] = {' ','+'};
char bellsign[2] = {' ',''-1};
char c_alarmsign[2] = {' ',''};
char scansign[2] = {' ','S'};
char autocqsign[2] = {' ','A'};

int opmode, last_opmode = 0xffff;

char cq_str[] = "CQ CQ CQ DE ";
char cq_respstr[2][50];

char *over_str[3] = {   " BTU +?",
                       " BTU>",
                       " PSE K K K"};

int ascii_spd, rtty_spd, pactor_spd;
int ascii_inv, rtty_inv, amtor_inv;
int ascii_shift, rtty_shift;
int delta_inv, delta_shift;
int usos;

int fec = 0;

int transmit = FALSE;

char *ascii_cmd_str[2]  = {"MODE HA, ","MODE VA, "};
char *rtty_cmd_str[2] = {"MODE HB, ","MODE VB, "};
char *pactor_fec_cmd[2] = {"U1","U2"};

char *speed_str[9]  = {"45","50","57","75","100","110","150","200","300"};
char *pactspeed_str[2] = {"100","200"};
char *a_rinv_str[2] = {"RINV OFF","RINV ON"};
char *a_tinv_str[2] = {"TINV OFF","TINV ON"};
char *b_rinv_str[2] = {"RXINVERT OFF","RXINVERT ON"};
char *b_tinv_str[2] = {"TXINVERT OFF","TXINVERT ON"};
char *usos_str[2]   = {"UNSHIFT OFF","UNSHIFT ON"};
char *usos_str2[2]  = {"USOS OFF","USOS ON "};

char fkstr[12][71];                  /* Function key buffers */

char callsign[11];                   /* MY callsign */
char my_selcall[5];                  /* MY Selcall  */
char destcall[11];                   /* Placeholder for destination callsign */
char selcall[5];                     /* Destination Selcall placeholder */
int cs_grab = 0;

int old_min = 0xffff;

int scan = 0;
char scanstr[31];
char scancrit[31];

int use_dsp_as_alarm = FALSE;

int wait_string = FALSE;
int w_found = FALSE;
int  w_instr, w_cmpstr;

int cmdflag;
int cmdstr;

int scrsav_flag = 0;

struct termios tnc_old_settings, tnc_settings;

char tnc_dev[81];
char lib_path[81];
char pager[81];
char editor[81];

int tnc;

fd_set io_set;

int tnc_x = 0, tnc_y = 1, console_x = 0, console_y = 20;

int pc_translate = FALSE;

int bell = FALSE;

speed_t bps;

char tmp[81];



int main(int argc, char **argv)
{
       int res;
       struct timeval timeout;

       init(argc, argv);

       while (1) {
               FD_ZERO(&io_set);
               FD_SET(tnc, &io_set);
               FD_SET(0, &io_set);
               timeout.tv_sec = 0;
               timeout.tv_usec = 400000;

               res = select(tnc + 1, &io_set, NULL, NULL, &timeout);

               if (res > 0) {
                       if (FD_ISSET(0, &io_set))
                               read_key();
                       if (FD_ISSET(tnc, &io_set))
                               read_tnc();
               }
               chk_time(1);
       }
}