home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / ncom.lzh / pbconvert.c < prev    next >
C/C++ Source or Header  |  1991-04-06  |  14KB  |  361 lines

  1. /* PbConvert for use with NComm V1.92.  */
  2. /* Public Domain by Torkel Lodberg 1991 */
  3.  
  4. #include <stdio.h>
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <intuition/intuition.h>
  8. #include <proto/intuition.h>
  9. #include <proto/graphics.h>
  10. #include <proto/exec.h>
  11. #include <fcntl.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14.  
  15. #define OLD_FILETYPE_PHONE  5
  16. #define NEW_FILETYPE_PHONE  6
  17.  
  18. static struct pb_Data2 {                 /* V1.8 file format    ***/
  19.     UBYTE        name[41];
  20.     UBYTE        phone[61];
  21.     UBYTE        comment[41];
  22.     UBYTE        pass_word[30];
  23.     UBYTE        script_file[33];
  24.     UBYTE        config_file[33];
  25.     UBYTE        dummy;
  26.     UBYTE       char_set;    /*** 0-11        ***/
  27.     UBYTE        baud;        /*** 0-6 ( 300 - 19200)    ***/
  28.     UBYTE        data_length;    /*** 7 or 8        ***/
  29.     UBYTE        parity;        /*** None, odd, even    ***/
  30.     UBYTE        stop_bits;    /*** 1 - 2        ***/
  31.     UBYTE        duplex;        /*** Full - half    ***/
  32. };
  33.  
  34. static struct pb_Data {        /*** V1.92-> file format ***/
  35.     UBYTE           name[41];
  36.     UBYTE           phone[61];
  37.     UBYTE           comment[41];
  38.     UBYTE           pass_word[30];
  39.     UBYTE           script_file[33];
  40.     UBYTE           config_file[33];
  41.     UBYTE           macro_file[33];
  42.     UBYTE           char_set;     /*** 0-12        ***/
  43.     UBYTE           baud;     /*** 0-11 (300 - 115200)    ***/
  44.     UBYTE           data_length; /*** 7 or 8        ***/
  45.     UBYTE           parity;     /*** None, odd, even    ***/
  46.     UBYTE           stop_bits;     /*** 1 - 2        ***/
  47.     UBYTE           duplex;     /*** Full - half    ***/
  48.     UBYTE           swap_del_bs; /*** Swap Del/BS       ***/
  49.     UBYTE           protocol;    /*** Protocol (0-6)    ***/
  50.     UBYTE           future[62];
  51. };
  52.  
  53. static int fd = NULL;   /* File handles */
  54. static int fe = NULL;
  55.  
  56. int wb_open;            /* Started from workbench? */
  57.  
  58. void cleanup(text)      /* Exit program cleanly */
  59. char *text;
  60. {
  61.         if (wb_open) printf("\n");
  62.         if (text) printf("%s\n",text);
  63.         if (fd > 0) close(fd);
  64.         if (fe > 0) close(fe);
  65.         if (wb_open) Delay(100);
  66.         exit(0);
  67. }
  68.  
  69. void brk()              /* Called by every CTRL-C / D keypress */
  70. {
  71.         printf("*** BREAK\n");
  72.         cleanup(NULL);
  73. }
  74.  
  75. void main(argc, argv)   /* Main program */
  76. int argc;
  77. char *argv[];
  78. {
  79.     int    status, i;
  80.     char    ch;
  81.         struct  pb_Data data;
  82.         struct  pb_Data2 data2;
  83.  
  84.         char    answer  [256];
  85.         char    infile  [256];
  86.         char    outfile [256];
  87.  
  88.         int     baudchange = NULL;
  89.         int     datachange = NULL;
  90.         int     paritychange = NULL;
  91.         int     stopchange = NULL;
  92.         int     protochange = NULL;
  93.  
  94.         int     oldformat = NULL, removeit = NULL;
  95.         int     baud, data_length, parity, stop_bits, protocol;
  96.  
  97.         if (!argc) wb_open = TRUE;
  98.            else wb_open = FALSE;
  99.  
  100.         if (onbreak(&brk)) cleanup(NULL);
  101.         chkabort();
  102.  
  103.         if (argc < 3) {
  104.                 printf("PbConvert for use with NComm V1.92. May remove passwords\n");
  105.                 printf("and filenames from phonebooks, as well as converting the\n");
  106.                 printf("NComm V1.8 file format. Optional global serial changes.\n");
  107.                 printf("Public Domain by Torkel Lodberg 1991\n\n");
  108.  
  109.                 if (argc) {
  110.                    printf("   Usage: PbConvert <infile> <outfile> [-b] [-d] [-p] [-s] [-x] [-r] [-e]\n");
  111.                    printf(" Options: [-b]nnnnnn where nnnnnn is the baudrate (300-115200)\n");
  112.                    printf("          [-d]n where n is the data length (7-8)\n");
  113.                    printf("          [-p]n where n is the parity (n)one, (o)dd, (e)ven\n");
  114.                    printf("          [-s]n where n is the number of stop bits (1-2)\n");
  115.                    printf("          [-x]n where n is the protocol (X/Y/B/G/Z/K/E)\n");
  116.                    printf("          [-r]emove filenames and passwords\n");
  117.                    printf("          [-e]xpect input file to be of old format (v1.8)\n\n");
  118.                    cleanup(NULL);
  119.                 }
  120.         }
  121.  
  122.         if (argc) {
  123.            strcpy(infile, argv[1]);
  124.            strcpy(outfile, argv[2]);
  125.  
  126.            for (i = 3; i < argc; i++) {
  127.                    if (!strncmp(argv[i], "-b", 2)) {
  128.                            baudchange = TRUE;
  129.                            switch (atoi(&argv[i][2])) {
  130.                                    case    300: baud = 0; break;
  131.                                    case    600: baud = 1; break;
  132.                                    case   1200: baud = 2; break;
  133.                                    case   2400: baud = 3; break;
  134.                                    case   4800: baud = 4; break;
  135.                                    case   9600: baud = 5; break;
  136.                                    case  19200: baud = 6; break;
  137.                                    case  38400: baud = 7; break;
  138.                                    case  57600: baud = 8; break;
  139.                                    case  76800: baud = 9; break;
  140.                                    case 115200: baud = 10; break;
  141.                                    case  31250: baud = 11; break;
  142.  
  143.                                       default: cleanup("Invalid baud rate!");
  144.                            }
  145.                    }
  146.                    if (!strncmp(argv[i], "-d", 2)) {
  147.                            datachange = TRUE;
  148.                            switch (atoi(&argv[i][2])) {
  149.                                    case 8: data_length = 0; break;
  150.                                    case 7: data_length = 1; break;
  151.                                   default: cleanup("Invalid data length!");
  152.                            }
  153.                    }
  154.                    if (!strncmp(argv[i], "-p", 2)) {
  155.                            paritychange = TRUE;
  156.                            switch (toupper(argv[i][2])) {
  157.                                    case 'N': parity = 0; break;
  158.                                    case 'O': parity = 1; break;
  159.                                    case 'E': parity = 2; break;
  160.                                     default: cleanup("Invalid parity!");
  161.                            }
  162.                    }
  163.                    if (!strncmp(argv[i], "-s", 2)) {
  164.                            stopchange = TRUE;
  165.                            switch (atoi(&argv[i][2])) {
  166.                                    case 1: stop_bits = 0; break;
  167.                                    case 2: stop_bits = 1; break;
  168.                                   default: cleanup("Invalid number of stop bits!");
  169.                            }
  170.                    }
  171.                    if (!strncmp(argv[i], "-x", 2)) {
  172.                            protochange = TRUE;
  173.                            switch (toupper(argv[i][2])) {
  174.                                    case 'X': protocol = 0; break;
  175.                                    case 'Y': protocol = 1; break;
  176.                                    case 'B': protocol = 2; break;
  177.                                    case 'G': protocol = 3; break;
  178.                                    case 'Z': protocol = 4; break;
  179.                                    case 'K': protocol = 5; break;
  180.                                    case 'E': protocol = 6; break;
  181.  
  182.                                   default: cleanup("Invalid protocol!");
  183.                            }
  184.                    }
  185.                    if (!strncmp(argv[i], "-r", 2)) {
  186.                            removeit = TRUE;
  187.                    }
  188.                    if (!strncmp(argv[i], "-e", 2)) {
  189.                            oldformat = TRUE;
  190.                    }
  191.            }
  192.         } else {
  193.            strcpy(infile, "NComm:NComm.phone");
  194.            strcpy(outfile, "RAM:NComm.phone");
  195.  
  196.            printf("Enter name of phonebook input file (Enter=NComm:NComm.phone): ");
  197.            gets(answer);
  198.            if (strlen(answer)) strcpy(infile, answer);
  199.  
  200.            printf("Enter name of phonebook output file (Enter=RAM:NComm.phone): ");
  201.            gets(answer);
  202.            if (strlen(answer)) strcpy(outfile, answer);
  203.  
  204.            oldformat = TRUE;
  205.            printf("\nExpect old file format (Enter=Yes): ");
  206.            gets(answer);
  207.            if (toupper(answer[0]) == 'N') oldformat = FALSE;
  208.  
  209.            printf("\nNew baud rate (Enter=No Change): ");
  210.            gets(answer);
  211.            if (strlen(answer)) {
  212.                 baudchange = TRUE;
  213.                 switch (atoi(answer)) {
  214.                    case    300: baud = 0; break;
  215.                    case    600: baud = 1; break;
  216.                    case   1200: baud = 2; break;
  217.                    case   2400: baud = 3; break;
  218.                    case   4800: baud = 4; break;
  219.                    case   9600: baud = 5; break;
  220.                    case  19200: baud = 6; break;
  221.                    case  38400: baud = 7; break;
  222.                    case  57600: baud = 8; break;
  223.                    case  76800: baud = 9; break;
  224.                    case 115200: baud = 10; break;
  225.                    case  31250: baud = 11; break;
  226.  
  227.                    default: cleanup("Invalid baud rate!");
  228.                 }
  229.            }
  230.  
  231.            printf("New data length (Enter=No Change): ");
  232.            gets(answer);
  233.            if (strlen(answer)) {
  234.                 datachange = TRUE;
  235.                 switch (atoi(answer)) {
  236.                    case 8: data_length = 0; break;
  237.                    case 7: data_length = 1; break;
  238.                    default: cleanup("Invalid data length!");
  239.                  }
  240.            }
  241.  
  242.            printf("New parity (Enter=No Change): ");
  243.            gets(answer);
  244.            if (strlen(answer)) {
  245.                 paritychange = TRUE;
  246.                 switch (toupper(answer[0])) {
  247.                    case 'N': parity = 0; break;
  248.                    case 'O': parity = 1; break;
  249.                    case 'E': parity = 2; break;
  250.                    default: cleanup("Invalid parity!");
  251.                 }
  252.            }
  253.  
  254.            printf("New number of stop bits (Enter=No Change): ");
  255.            gets(answer);
  256.            if (strlen(answer)) {
  257.                 stopchange = TRUE;
  258.                 switch (atoi(answer)) {
  259.                    case 1: stop_bits = 0; break;
  260.                    case 2: stop_bits = 1; break;
  261.                    default: cleanup("Invalid number of stop bits!");
  262.                 }
  263.            }
  264.  
  265.            printf("New protocol (Enter=No Change): ");
  266.            gets(answer);
  267.            if (strlen(answer)) {
  268.                 protochange = TRUE;
  269.                 switch (toupper(answer[0])) {
  270.                    case 'X': protocol = 0; break;
  271.                    case 'Y': protocol = 1; break;
  272.                    case 'B': protocol = 2; break;
  273.                    case 'G': protocol = 3; break;
  274.                    case 'Z': protocol = 4; break;
  275.                    case 'K': protocol = 5; break;
  276.                    case 'E': protocol = 6; break;
  277.  
  278.                    default: cleanup("Invalid protocol!");
  279.                 }
  280.            }
  281.  
  282.            removeit = FALSE;
  283.            printf("\nRemove filenames and passwords (Enter=No): ");
  284.            gets(answer);
  285.            if (toupper(answer[0] == 'Y')) removeit = TRUE;
  286.         }
  287.  
  288.     if((fd = open(infile, O_RDONLY, NULL)) == -1
  289.       ||read (fd, &ch, 1) != 1)
  290.         if (argc) cleanup("Cannot open data-file");
  291.  
  292.         if (oldformat) {
  293.             if (ch != OLD_FILETYPE_PHONE)
  294.                 cleanup("Error reading NComm.phone: Illegal file type");
  295.                 ch = NEW_FILETYPE_PHONE;
  296.         } else {
  297.             if (ch < NEW_FILETYPE_PHONE)
  298.                 cleanup("Error reading NComm.phone: Illegal file type");
  299.         }
  300.  
  301.     if((fe = open(outfile, O_RDWR|O_CREAT|O_TRUNC, NULL)) == -1)
  302.         cleanup("Cannot create file!");
  303.  
  304.     write (fe, &ch, 1);
  305.  
  306.         if (oldformat) {
  307.             while((status = read(fd, (char *) &data2, sizeof(struct pb_Data2))) == sizeof(struct pb_Data2)) {
  308.                         if (removeit) {
  309.                            memset(data2.pass_word, 0, sizeof(data2.pass_word));
  310.                            memset(data2.script_file, 0, sizeof(data2.script_file));
  311.                            memset(data2.config_file, 0, sizeof(data2.config_file));
  312.                         }
  313.  
  314.                         if (baudchange) data2.baud = baud;
  315.                         if (datachange) data2.data_length = data_length;
  316.                         if (paritychange) data2.parity = parity;
  317.                         if (stopchange) data2.stop_bits = stop_bits;
  318.  
  319.                         strcpy(data.name, data2.name);
  320.                         strcpy(data.phone, data2.phone);
  321.                         strcpy(data.comment, data2.comment);
  322.                         strcpy(data.pass_word, data2.pass_word);
  323.                         strcpy(data.script_file, data2.script_file);
  324.                         strcpy(data.config_file, data2.config_file);
  325.                         memset(data.macro_file, 0, sizeof(data.macro_file));
  326.  
  327.                         data.char_set = data2.char_set;
  328.                         data.baud = data2.baud;
  329.                         data.data_length = data2.data_length;
  330.                         data.parity = data2.parity;
  331.                         data.stop_bits = data2.stop_bits;
  332.                         data.duplex = data2.duplex;
  333.                         memset(data.future, 0, sizeof(data.future));
  334.  
  335.                 status = write(fe, (char *) &data, sizeof(struct pb_Data));
  336.                 if(status != sizeof(struct pb_Data))
  337.                                 cleanup("Error while writing!");
  338.                 }
  339.         } else {
  340.             while((status = read(fd, (char *) &data, sizeof(struct pb_Data))) == sizeof(struct pb_Data)) {
  341.                         if (removeit) {
  342.                            memset(data.pass_word, 0, sizeof(data.pass_word));
  343.                            memset(data.script_file, 0, sizeof(data.script_file));
  344.                            memset(data.config_file, 0, sizeof(data.config_file));
  345.                            memset(data.macro_file, 0, sizeof(data.macro_file));
  346.                         }
  347.  
  348.                         if (baudchange) data.baud = baud;
  349.                         if (datachange) data.data_length = data_length;
  350.                         if (paritychange) data.parity = parity;
  351.                         if (stopchange) data.stop_bits = stop_bits;
  352.                         if (protochange) data.protocol = protocol;
  353.  
  354.                 status = write(fe, (char *) &data, sizeof(struct pb_Data));
  355.                 if(status != sizeof(struct pb_Data))
  356.                                 cleanup("Error while writing!");
  357.                 }
  358.         }
  359.     cleanup(NULL);
  360. }
  361.