home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / pcomm-2.0.2 / part03 / cmd.c next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  6.9 KB  |  364 lines

  1. /*
  2.  * Pcomm script commands.  Since these will be executed from a forked
  3.  * shell script, the commands, queries, and responses are sent thru an
  4.  * IPC channel to Pcomm.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <curses.h>
  9. #include "cmd.h"
  10. #include "config.h"
  11. #include "dial_dir.h"
  12. #include "extrnl.h"
  13. #include "modem.h"
  14. #include "param.h"
  15. #include "status.h"
  16. #include "xmodem.h"
  17.  
  18. static void send_reply(), do_xfer(), cmode(), tmode();
  19.  
  20. void
  21. cmd_input()
  22. {
  23.     extern int fd;
  24.     int cmd, arg1, i, n, ret_code;
  25.     char buf[256], *s, arg2[256], *strchr(), *str_rep();
  26.     char *entry, *strtok(), ld_code;
  27.     void hang_up(), line_set(), pexit(), screen_dump(), log_toggle();
  28.     void lpr_toggle();
  29.  
  30.     if (ipc_read(status->cmd_ipc, buf, 256))
  31.         return;
  32.  
  33.                     /* parse the comand line */
  34.     if (sscanf(buf, "%d %d %s\n", &cmd, &arg1, arg2) != 3)
  35.         return;
  36.  
  37.     ret_code = 0;
  38.     switch(cmd) {
  39.         case SET:        /* the SET command */
  40.             switch(arg1) {
  41.                 case BAUD:    
  42.                     dir->baud[0] = (unsigned int) atoi(arg2);
  43.                     line_set();
  44.                     break;
  45.                 case PARITY:
  46.                     dir->parity[0] = arg2[0];
  47.                     line_set();
  48.                     break;
  49.                 case DATA_BITS:
  50.                     dir->data_bits[0] = atoi(arg2);
  51.                     line_set();
  52.                     break;
  53.                 case STOP_BITS:
  54.                     dir->stop_bits[0] = atoi(arg2);
  55.                     line_set();
  56.                     break;
  57.                 case DUPLEX:
  58.                     dir->duplex[0] = arg2[0];
  59.                     line_set();
  60.                     break;
  61.                 case AUX:
  62.                     dir->aux[0] = str_rep(dir->aux[0], arg2);
  63.                     break;
  64.                 case HOT_KEY:
  65.                     param->hot_key = atoi(arg2);
  66.                     break;
  67.                 case ASCII_HOT:
  68.                     param->ascii_hot = str_rep(param->ascii_hot, arg2);
  69.                     break;
  70.                 case FLOW_CTRL:
  71.                     param->flow_ctrl = str_rep(param->flow_ctrl, arg2);
  72.                     break;
  73.                 case CR_IN:    
  74.                     param->cr_in = str_rep(param->cr_in, arg2);
  75.                     break;
  76.                 case CR_OUT:
  77.                     param->cr_out = str_rep(param->cr_out, arg2);
  78.                     break;
  79.                 case LOGFILE:
  80.                     param->logfile = str_rep(param->logfile, arg2);
  81.                     break;
  82.                 case DUMPFILE:
  83.                     param->dumpfile = str_rep(param->dumpfile, arg2);
  84.                     break;
  85.                 case STRIP:    
  86.                     param->strip = str_rep(param->strip, arg2);
  87.                     break;
  88.                 case LOCAL_ECHO:    
  89.                     param->local_echo = str_rep(param->local_echo, arg2);
  90.                     break;
  91.                 case EXPAND:
  92.                     param->expand = str_rep(param->expand, arg2);
  93.                     break;
  94.                 case CR_DELAY:
  95.                     param->cr_delay = atoi(arg2);
  96.                     break;
  97.                 case PACE:
  98.                     param->pace = str_rep(param->pace, arg2);
  99.                     break;
  100.                 case CR_UP:
  101.                     param->cr_up = str_rep(param->cr_up, arg2);
  102.                     break;
  103.                 case LF_UP:
  104.                     param->lf_up = str_rep(param->lf_up, arg2);
  105.                     break;
  106.                 case TIMER:
  107.                     param->timer = atoi(arg2);
  108.                     break;
  109.                 case CR_DN:
  110.                     param->cr_dn = str_rep(param->cr_dn, arg2);
  111.                     break;
  112.                 case LF_DN:    
  113.                     param->lf_dn = str_rep(param->lf_dn, arg2);
  114.                     break;
  115.                 default:
  116.                     break;
  117.             }
  118.             break;
  119.         case QUERY:        /* the QUERY commands */
  120.             switch(arg1) {
  121.                 case TTY_NAME:
  122.                     if (modem->t_cur != -1)
  123.                         send_reply(modem->tty[modem->t_cur]);
  124.                     else
  125.                         send_reply("NONE");
  126.                     ret_code = 99;
  127.                     break;
  128.                 case MODEM_NAME:
  129.                     if (modem->m_cur != -1)
  130.                         send_reply(modem->mname[modem->m_cur]);
  131.                     else
  132.                         send_reply("NONE");
  133.                     ret_code = 99;
  134.                     break;
  135.                 default:
  136.                     break;
  137.             }
  138.             break;
  139.         case IF:        /* the IF commands */
  140.             switch(arg1) {
  141.                 case CONNECTED:
  142.                     if (status->connected)
  143.                         ret_code = 1;
  144.                     break;
  145.                 case LOG_STATUS:
  146.                     if (status->log_status)
  147.                         ret_code = 1;
  148.                     break;
  149.                 case PRINTER_STATUS:
  150.                     if (status->print_status)
  151.                         ret_code = 1;
  152.                     break;
  153.                 default:
  154.                     break;
  155.             }
  156.             break;
  157.         case DIAL:
  158.             cmode();
  159.             ld_code = '\0';
  160.             s = arg2;
  161.             if (strchr("+-@#", *s)) {
  162.                 ld_code = *s;
  163.                 s++;
  164.             }
  165.  
  166.             if (arg1 == 1) {    /* manual dial */
  167.                 dir->name[0] = str_rep(dir->number[0], s);
  168.                 dir->number[0] = str_rep(dir->number[0], s);
  169.                 dir->d_cur = 0;
  170.                 dir->q_num[0] = 0;
  171.             }
  172.             else {
  173.                 n = atoi(s);
  174.                 if (n == 0 || n > NUM_DIR) {
  175.                     tmode();
  176.                     break;
  177.                 }
  178.  
  179.                 dir->d_cur = n;
  180.                 dir->q_num[0] = n;
  181.             }
  182.             dir->q_ld[0] = ld_code;
  183.             dir->q_num[1] = -1;
  184.             ret_code = dial_win(1);
  185.             tmode();
  186.             break;
  187.         case REDIAL:
  188.             cmode();
  189.             entry = strtok(arg2, " ");
  190.             for (i=0; i<NUM_QUEUE; i++) {
  191.                 if (entry == NULL) {
  192.                     dir->q_num[i] = -1;
  193.                     break;
  194.                 }
  195.  
  196.                 ld_code = '\0';
  197.                 if (strchr("+-@#", *entry)) {
  198.                     ld_code = *entry;
  199.                     entry++;
  200.                 }
  201.  
  202.                 n = atoi(entry);
  203.                 if (n > NUM_DIR || *dir->number[n] == '\0')
  204.                     continue;
  205.  
  206.                 dir->q_ld[i] = ld_code;
  207.                 dir->q_num[i] = n;
  208.                 entry = strtok((char *) NULL, " \t");
  209.             }
  210.             ret_code = dial_win(10);
  211.             tmode();
  212.             break;
  213.         case EXIT:
  214.             pexit();
  215.             break;
  216.         case CLEAR_SCREEN:
  217.             fixterm();
  218.             erase();
  219.             refresh();
  220.             resetterm();
  221.             break;
  222.         case CHG_DIR:
  223.             chdir(arg2);
  224.             break;
  225.         case HANG_UP:
  226.             hang_up(QUIET);
  227.             break;
  228.         case PRINTER:
  229.             if (status->print_status + arg1 == 1)
  230.                 lpr_toggle();
  231.             break;
  232.         case MODEM_BREAK:
  233.             tty_break(fd);
  234.             break;
  235.         case SEND:
  236.             do_xfer(arg1, UP_LOAD, arg2);
  237.             break;
  238.         case RECEIVE:
  239.             do_xfer(arg1, DOWN_LOAD, arg2);
  240.             break;
  241.         case SCREEN_DUMP:
  242.             screen_dump();
  243.             break;
  244.         case DATA_LOG:
  245.             if (status->log_status + arg1 == 1) {
  246.                 if (arg1 == 1 && !strcmp(status->log_path, "NOT_DEFINED"))
  247.                     status->log_path = str_rep(status->log_path, param->logfile);
  248.                 log_toggle();
  249.             }
  250.             break;
  251.         default:
  252.             break;
  253.     }
  254.     if (ret_code != 99) {
  255.         sprintf(buf, "%d", ret_code);
  256.         send_reply(buf);
  257.     }
  258.     return;
  259. }
  260.  
  261. /*
  262.  * Send a string back to pcomm_cmd
  263.  */
  264.  
  265. static void
  266. send_reply(s)
  267. char *s;
  268. {
  269.     char buf[256];
  270.  
  271.     sprintf(buf, "%254.254s\n", s);
  272.     if (ipc_write(status->cmd_ipc, buf, 256))
  273.         fprintf(stderr, "Can't write to IPC\n");
  274.     return;
  275. }
  276.  
  277. /*
  278.  * Put the screen in the "curses" mode
  279.  */
  280.  
  281. static void
  282. cmode()
  283. {
  284.     void load_vs(), st_line();
  285.  
  286.     fixterm();
  287.     load_vs();
  288.     st_line("");
  289.     return;
  290. }
  291.  
  292. /*
  293.  * Put the screen in the "terminal" mode
  294.  */
  295.  
  296. static void
  297. tmode()
  298. {
  299.     extern int fd;
  300.     void term_mode();
  301.  
  302.     if (fd != -1) {
  303.         touchwin(stdscr);
  304.         refresh();
  305.     }
  306.     resetterm();
  307.     term_mode();
  308.     return;
  309. }
  310.  
  311. /*
  312.  * Do the file transfers
  313.  */
  314.  
  315. static void
  316. do_xfer(type, up, files)
  317. int type, up;
  318. char *files;
  319. {
  320.     int n, is_batch, num_extrnl;
  321.     char buf[256], *strcpy(), *strcat();
  322.     void xfer_win(), xfer_ascii(), do_extrnl();
  323.  
  324.     num_extrnl = (up == UP_LOAD) ? extrnl->up_entries : extrnl->dn_entries;
  325.     
  326.     is_batch = 0;
  327.     switch(type) {
  328.         case MODEM7:
  329.         case YMODEM:
  330.         case YMODEM_G:        /* built-in batch protocols */
  331.             is_batch++;
  332.             /* FALLTHRU */
  333.         case XMODEM:
  334.         case XMODEM_1k:        /* non-batch built-ins */
  335.             cmode();
  336.             if (up == UP_LOAD || !is_batch)
  337.                 xfer_win(files, up, type);
  338.             else
  339.                 xfer_win("", up, type);
  340.             tmode();
  341.             break;
  342.         case XASCII:        /* ascii xfer, yuck! */
  343.             xfer_ascii(files, up);
  344.             break;
  345.         case EXT_1:
  346.         case EXT_2:
  347.         case EXT_3:        /* one of the externals */
  348.             n = type -NUM_INTERNAL -1;
  349.             if (n > num_extrnl)
  350.                 break;
  351.             strcpy(buf, extrnl->command[up][n]);
  352.                     /* see if we need to add files */
  353.             if (extrnl->prompt[up][n] == 'Y') {
  354.                 strcat(buf, " ");
  355.                 strcat(buf, files);
  356.             }
  357.             do_extrnl(buf);
  358.             break;
  359.         default:
  360.             break;
  361.     }
  362.     return;
  363. }
  364.